utils_gthread: Change to use g_cond_wait_until to wait idle 26/284926/1
authorJoonbum Ko <joonbum.ko@samsung.com>
Wed, 30 Nov 2022 04:47:29 +0000 (13:47 +0900)
committerJoonbum Ko <joonbum.ko@samsung.com>
Thu, 1 Dec 2022 04:38:54 +0000 (13:38 +0900)
 - In some cases, signal was sent by idle_callback but
  there is a problem that this signal is lost and g_cond_wait
  cannot be awakened.

 - Above case can cause deadlock problem.

 - So set 200ms timeout ms with using g_cond_wait_until instead of
  g_cond_wait.

Change-Id: Ib0a549616f74a9fb39e78f7b37b1b8b09e16031d
Signed-off-by: Joonbum Ko <joonbum.ko@samsung.com>
src/tpl_utils_gthread.c

index 500507f..b3e48ef 100644 (file)
@@ -492,6 +492,8 @@ tpl_gthread_wait_idle(tpl_gthread *gthread)
        TPL_CHECK_ON_NULL_RETURN(gthread);
 
        GSource *idle_source = NULL;
+       gint64 end_time;
+       gboolean ret = TRUE;
 
        TPL_DEBUG("[WAIT IDLE] BEGIN");
 
@@ -512,8 +514,18 @@ tpl_gthread_wait_idle(tpl_gthread *gthread)
        g_source_attach(idle_source, g_main_loop_get_context(gthread->loop));
        g_source_unref(idle_source);
 
+       /* 200ms timeout */
+       end_time = g_get_monotonic_time() +
+                               (200 * G_TIME_SPAN_MILLISECOND);
+
        while (!gthread->is_idle) {
-               g_cond_wait(&gthread->idle_cond, &gthread->idle_mutex);
+               ret = g_cond_wait_until(&gthread->idle_cond,
+                                                               &gthread->idle_mutex,
+                                                               end_time);
+               if (!ret) {
+                       TPL_ERR("wait_idle timeout!");
+                       break;
+               }
        }
        g_mutex_unlock(&gthread->idle_mutex);