@@ -495,22 +495,19 @@ __process_output (ml_single * single_h, ml_tensors_data_h output)
495495static void *
496496invoke_thread (void * arg )
497497{
498- ml_single * single_h ;
498+ ml_single * single_h = ( ml_single * ) arg ;
499499 ml_tensors_data_h input , output ;
500500 gboolean alloc_output = FALSE;
501-
502- single_h = (ml_single * ) arg ;
501+ int status ;
503502
504503 g_mutex_lock (& single_h -> mutex );
505504
506- while (single_h -> state <= RUNNING ) {
507- int status = ML_ERROR_NONE ;
508-
505+ while (TRUE) {
509506 /** wait for data */
510507 while (single_h -> state != RUNNING ) {
511- g_cond_wait (& single_h -> cond , & single_h -> mutex );
512508 if (single_h -> state == JOIN_REQUESTED )
513- goto exit ;
509+ goto exit_thread ;
510+ g_cond_wait (& single_h -> cond , & single_h -> mutex );
514511 }
515512
516513 input = single_h -> input ;
@@ -526,34 +523,34 @@ invoke_thread (void *arg)
526523 /* Clear input data after invoke is done. */
527524 ml_tensors_data_destroy (input );
528525 single_h -> invoking = FALSE;
526+ single_h -> status = status ;
529527
530528 if (status != ML_ERROR_NONE || single_h -> state == JOIN_REQUESTED ) {
529+ /* If error occurred or join requested during invocation */
531530 if (alloc_output ) {
532531 single_h -> destroy_data_list =
533532 g_list_remove (single_h -> destroy_data_list , output );
534533 ml_tensors_data_destroy (output );
535534 }
536-
535+ /* If join requested, exit immediately without broadcast */
537536 if (single_h -> state == JOIN_REQUESTED )
538- goto exit ;
539- goto wait_for_next ;
537+ goto exit_thread ;
538+ } else {
539+ /* Process output data on success */
540+ if (alloc_output )
541+ __process_output (single_h , output );
540542 }
541543
542- if (alloc_output )
543- __process_output (single_h , output );
544-
545- /** loop over to wait for the next element */
546- wait_for_next :
547- single_h -> status = status ;
544+ /*Reset state and notify */
548545 if (single_h -> state == RUNNING )
549546 single_h -> state = IDLE ;
547+
550548 g_cond_broadcast (& single_h -> cond );
551549 }
552550
553- exit :
554- /* Do not set IDLE if JOIN_REQUESTED */
551+ exit_thread :
552+ /* Cleanup resources on exit */
555553 if (single_h -> state == JOIN_REQUESTED ) {
556- /* Release input and output data */
557554 if (single_h -> input )
558555 ml_tensors_data_destroy (single_h -> input );
559556
@@ -562,10 +559,11 @@ invoke_thread (void *arg)
562559 g_list_remove (single_h -> destroy_data_list , single_h -> output );
563560 ml_tensors_data_destroy (single_h -> output );
564561 }
565-
566562 single_h -> input = single_h -> output = NULL ;
567- } else if (single_h -> state == RUNNING )
563+ } else if (single_h -> state == RUNNING ) {
568564 single_h -> state = IDLE ;
565+ }
566+
569567 g_mutex_unlock (& single_h -> mutex );
570568 return NULL ;
571569}
0 commit comments