Skip to content

Commit 472be75

Browse files
songgotmyungjoo
authored andcommitted
[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 85df469 commit 472be75

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ invoke_thread (void *arg)
564564
}
565565

566566
single_h->input = single_h->output = NULL;
567+
g_cond_broadcast (&single_h->cond);
567568
} else if (single_h->state == RUNNING)
568569
single_h->state = IDLE;
569570
g_mutex_unlock (&single_h->mutex);
@@ -1344,7 +1345,6 @@ int
13441345
ml_single_close (ml_single_h single)
13451346
{
13461347
ml_single *single_h;
1347-
gboolean invoking;
13481348

13491349
check_feature_state (ML_FEATURE_INFERENCE);
13501350

@@ -1359,21 +1359,12 @@ ml_single_close (ml_single_h single)
13591359

13601360
single_h->state = JOIN_REQUESTED;
13611361
g_cond_broadcast (&single_h->cond);
1362-
invoking = single_h->invoking;
1363-
ML_SINGLE_HANDLE_UNLOCK (single_h);
1364-
13651362
/** Wait until invoke process is finished */
1366-
while (invoking) {
1367-
_ml_logd ("Wait 1 ms until invoke is finished and close the handle.");
1368-
g_usleep (1000);
1369-
invoking = single_h->invoking;
1370-
/**
1371-
* single_h->invoking is the only protected value here and we are
1372-
* doing a read-only operation and do not need to project its value
1373-
* after the assignment.
1374-
* Thus, we do not need to lock single_h here.
1375-
*/
1363+
while (single_h->invoking) {
1364+
_ml_logd ("Wait until invoke is finished and close the handle.");
1365+
g_cond_wait (&single_h->cond, &single_h->mutex);
13761366
}
1367+
ML_SINGLE_HANDLE_UNLOCK (single_h);
13771368

13781369
if (single_h->thread != NULL)
13791370
g_thread_join (single_h->thread);

0 commit comments

Comments
 (0)