From: Volodymyr Brynza Date: Fri, 12 May 2017 08:46:24 +0000 (+0300) Subject: Add timed wait to thread loop join to avoid timeouts X-Git-Tag: submit/tizen_3.0/20170518.094523~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e990698aa7a6ba49a3c4893207d2c87c9107f99e;p=platform%2Fupstream%2Fmurphy.git Add timed wait to thread loop join to avoid timeouts Change-Id: Iae53916efcbd3591f7e3b94bf8551fc29ead7b96 Signed-off-by: Volodymyr Brynza --- diff --git a/src/common/glib-glue.c b/src/common/glib-glue.c index 21f3dfb..9bfcdf1 100644 --- a/src/common/glib-glue.c +++ b/src/common/glib-glue.c @@ -35,9 +35,11 @@ #include #include +#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); }