From a693cd5086bcef31ad7615c1a56ecd986c34664a Mon Sep 17 00:00:00 2001 From: hyunil park Date: Wed, 26 Nov 2025 16:18:45 +0900 Subject: [PATCH] [single] Refactor: Replace busy waiting with a condition variable in close Replaced the busy waiting loop (g_usleep) in ml_single_close with `g_cond_wait` to improve CPU efficiency. - Modified to broadcast a signal immediately after completing an invoke tasek. - Updated to wait on the condition variable instead of polling the `invoking` state every 1ms. Signed-off-by: hyunil park --- c/src/ml-api-inference-single.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/c/src/ml-api-inference-single.c b/c/src/ml-api-inference-single.c index 03df6326..f7210a81 100644 --- a/c/src/ml-api-inference-single.c +++ b/c/src/ml-api-inference-single.c @@ -564,6 +564,7 @@ invoke_thread (void *arg) } single_h->input = single_h->output = NULL; + g_cond_broadcast (&single_h->cond); } else if (single_h->state == RUNNING) single_h->state = IDLE; g_mutex_unlock (&single_h->mutex); @@ -1335,7 +1336,6 @@ int ml_single_close (ml_single_h single) { ml_single *single_h; - gboolean invoking; check_feature_state (ML_FEATURE_INFERENCE); @@ -1350,21 +1350,12 @@ ml_single_close (ml_single_h single) single_h->state = JOIN_REQUESTED; g_cond_broadcast (&single_h->cond); - invoking = single_h->invoking; - ML_SINGLE_HANDLE_UNLOCK (single_h); - /** Wait until invoke process is finished */ - while (invoking) { - _ml_logd ("Wait 1 ms until invoke is finished and close the handle."); - g_usleep (1000); - invoking = single_h->invoking; - /** - * single_h->invoking is the only protected value here and we are - * doing a read-only operation and do not need to project its value - * after the assignment. - * Thus, we do not need to lock single_h here. - */ + while (single_h->invoking) { + _ml_logd ("Wait until invoke is finished and close the handle."); + g_cond_wait (&single_h->cond, &single_h->mutex); } + ML_SINGLE_HANDLE_UNLOCK (single_h); if (single_h->thread != NULL) g_thread_join (single_h->thread);