From 37420c342bf9582a0de32d46f78716e913119400 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 27 Nov 2011 19:28:21 +0100 Subject: io_thread: fix race condition during startup Ensure that the io.thread variable is set before entering the event loop. --- src/io_thread.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/io_thread.c b/src/io_thread.c index ffa513d49..2091ecd05 100644 --- a/src/io_thread.c +++ b/src/io_thread.c @@ -34,6 +34,7 @@ static struct { void io_thread_run(void) { + assert(io_thread_inside()); assert(io.context != NULL); assert(io.loop != NULL); @@ -43,6 +44,11 @@ io_thread_run(void) static gpointer io_thread_func(G_GNUC_UNUSED gpointer arg) { + /* lock+unlock to synchronize with io_thread_start(), to be + sure that io.thread is set */ + g_mutex_lock(io.mutex); + g_mutex_unlock(io.mutex); + io_thread_run(); return NULL; } @@ -67,7 +73,9 @@ io_thread_start(GError **error_r) assert(io.loop != NULL); assert(io.thread == NULL); + g_mutex_lock(io.mutex); io.thread = g_thread_create(io_thread_func, NULL, true, error_r); + g_mutex_unlock(io.mutex); if (io.thread == NULL) return false; -- cgit v1.2.3