Add timed wait to thread loop join to avoid timeouts 82/117782/4
authorVolodymyr Brynza <v.brynza@samsung.com>
Fri, 12 May 2017 08:46:24 +0000 (11:46 +0300)
committerVolodymyr Brynza <v.brynza@samsung.com>
Fri, 12 May 2017 08:46:24 +0000 (11:46 +0300)
Change-Id: Iae53916efcbd3591f7e3b94bf8551fc29ead7b96
Signed-off-by: Volodymyr Brynza <v.brynza@samsung.com>
src/common/glib-glue.c

index 21f3dfb3df4072c66cfaa2e75b527d19d5b58a19..9bfcdf1c9675c7032ed023eb11b390477f5c07d2 100644 (file)
 #include <murphy/common/mm.h>
 #include <murphy/common/mainloop.h>
 
+#define MURPHY_LOOP_WAIT_TIME_THREAD 1
 
 static GMutex g_murphy_glue_callback_lock;
 static GMutex g_murphy_glue_internal_lock;
+static GCond g_murphy_loop_cond;
 
 
 typedef struct {
@@ -385,16 +387,21 @@ static void unregister(void *data)
     glib_glue_t *glue = (glib_glue_t *)data;
     GMainContext *def_ctx = g_main_context_default();
     GMainContext *loop_ctx = g_main_loop_get_context(glue->gml);
+    g_mutex_lock(&g_murphy_glue_internal_lock);
     if (loop_ctx && def_ctx != loop_ctx) {
         GSource *idle = g_idle_source_new();
+        gint64 end_time = 0;
 
         g_source_set_callback(idle, (GSourceFunc) g_main_loop_quit,
             glue->gml, NULL);
 
         g_source_attach(idle, g_main_loop_get_context(glue->gml));
         g_source_unref(idle);
-        g_thread_join(glue->worker);
-        glue->worker = NULL;
+        end_time = g_get_monotonic_time() + MURPHY_LOOP_WAIT_TIME_THREAD * G_TIME_SPAN_SECOND;
+        if (g_cond_wait_until(&g_murphy_loop_cond, &g_murphy_glue_internal_lock, end_time)) {
+            g_thread_join(glue->worker);
+            glue->worker = NULL;
+        }
     }
 
     if (glue->gml) {
@@ -403,6 +410,7 @@ static void unregister(void *data)
     }
 
     mrp_free(glue);
+    g_mutex_unlock(&g_murphy_glue_internal_lock);
 }
 
 
@@ -431,6 +439,10 @@ thread_main (gpointer data)
 
     g_main_context_pop_thread_default (thread_main_context);
 
+    g_mutex_lock(&g_murphy_glue_internal_lock);
+    g_cond_signal(&g_murphy_loop_cond);
+    g_mutex_unlock(&g_murphy_glue_internal_lock);
+
     return NULL;
 }
 
@@ -497,6 +509,7 @@ MRP_INIT static void mrp_main_loop_init_lock()
 {
     g_mutex_init(&g_murphy_glue_callback_lock);
     g_mutex_init(&g_murphy_glue_internal_lock);
+    g_cond_init(&g_murphy_loop_cond);
 }
 
 
@@ -504,4 +517,5 @@ MRP_EXIT static void mrp_main_loop_clear_lock()
 {
     g_mutex_clear(&g_murphy_glue_callback_lock);
     g_mutex_clear(&g_murphy_glue_internal_lock);
+    g_cond_clear(&g_murphy_loop_cond);
 }