tpl_wayland_egl_thread: Protect twe_thread_create() with mutex 16/150616/7
authorHoyub Lee <hoyub.lee@samsung.com>
Fri, 15 Sep 2017 08:46:48 +0000 (17:46 +0900)
committerjoonbum.ko <joonbum.ko@samsung.com>
Mon, 25 Sep 2017 02:47:52 +0000 (11:47 +0900)
Currently, main thread does not wait for newly creating threads which
has its own initialization process. As initalization happens on newly
created thread, without protecting, undesirable behaviour can occur.

Therefore, change main thread to wait newly creating thread with mutex.

Change-Id: Id32092d77c35bd7b8d705057a1b07798820e8e20
Signed-off-by: Hoyub Lee <hoyub.lee@samsung.com>
src/tpl_wayland_egl_thread.c

index 8bba755..5f2d3a1 100644 (file)
@@ -34,6 +34,9 @@ struct _twe_thread_context {
        GMainLoop *twe_loop;
 
        twe_tdm_source *tdm_source;
+
+       GMutex thread_mutex;
+       GCond  thread_cond;
 };
 
 struct _twe_thread {
@@ -118,8 +121,12 @@ static gpointer
 _twe_thread_loop(gpointer data)
 {
        twe_thread_context *ctx = data;
-       char *env = getenv("TPL_WAIT_VBLANK");
 
+       char *env = NULL;
+
+       g_mutex_lock(&ctx->thread_mutex);
+
+       env = getenv("TPL_WAIT_VBLANK");
        if (env == NULL || atoi(env)) {
                twe_tdm_source *tdm_source = _twe_thread_tdm_source_create();
 
@@ -135,6 +142,9 @@ _twe_thread_loop(gpointer data)
                }
        }
 
+       g_cond_signal(&ctx->thread_cond);
+       g_mutex_unlock(&ctx->thread_mutex);
+
        g_main_loop_run(ctx->twe_loop);
 
        return ctx;
@@ -388,8 +398,16 @@ twe_thread_create(void)
                _twe_ctx->twe_loop = g_main_loop_new(context, FALSE);
                g_main_context_unref(context);
 
+               g_mutex_init(&_twe_ctx->thread_mutex);
+               g_cond_init(&_twe_ctx->thread_cond);
+
+               g_mutex_lock(&_twe_ctx->thread_mutex);
                _twe_ctx->twe_thread = g_thread_new("twe_thread", _twe_thread_loop,
                                                                                        _twe_ctx);
+               g_cond_wait(&_twe_ctx->thread_cond,
+                                       &_twe_ctx->thread_mutex);
+               g_mutex_unlock(&_twe_ctx->thread_mutex);
+
                _twe_ctx->ref_cnt = 0;
        }
 
@@ -415,6 +433,9 @@ twe_thread_destroy(twe_thread* thread)
                g_thread_join(thread->ctx->twe_thread);
                g_main_loop_unref(thread->ctx->twe_loop);
 
+               g_mutex_clear(&thread->ctx->thread_mutex);
+               g_cond_clear(&thread->ctx->thread_cond);
+
                free(_twe_ctx);
                _twe_ctx = NULL;
        }