Skip to content

Commit 4a4ddc4

Browse files
committed
[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 <hyunil46.park@samsung.com>
1 parent b80c7f5 commit 4a4ddc4

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

c/src/ml-api-inference-single.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ invoke_thread (void *arg)
526526
/* Clear input data after invoke is done. */
527527
ml_tensors_data_destroy (input);
528528
single_h->invoking = FALSE;
529+
g_cond_broadcast (&single_h->cond);
529530

530531
if (status != ML_ERROR_NONE || single_h->state == JOIN_REQUESTED) {
531532
if (alloc_output) {
@@ -1350,20 +1351,10 @@ ml_single_close (ml_single_h single)
13501351

13511352
single_h->state = JOIN_REQUESTED;
13521353
g_cond_broadcast (&single_h->cond);
1353-
invoking = single_h->invoking;
1354-
ML_SINGLE_HANDLE_UNLOCK (single_h);
1355-
13561354
/** Wait until invoke process is finished */
1357-
while (invoking) {
1358-
_ml_logd ("Wait 1 ms until invoke is finished and close the handle.");
1359-
g_usleep (1000);
1360-
invoking = single_h->invoking;
1361-
/**
1362-
* single_h->invoking is the only protected value here and we are
1363-
* doing a read-only operation and do not need to project its value
1364-
* after the assignment.
1365-
* Thus, we do not need to lock single_h here.
1366-
*/
1355+
while (single_h->invoking) {
1356+
_ml_logd ("Wait until invoke is finished and close the handle.");
1357+
g_cond_wait (&single_h->cond, &single_h->mutex);
13671358
}
13681359

13691360
if (single_h->thread != NULL)

0 commit comments

Comments
 (0)