From dbf20d585fd53c612a1dd8ef07275d2affcf7fc0 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Wed, 12 Oct 2011 23:40:02 -0400 Subject: [PATCH] posix threads: joinable tweaks Make the POSIX backend a little bit more like the win32 one in terms of how we deal with joinability. Calling g_system_thread_join() is now optional, and g_system_thread_wait() can be safely called by multiple threads. There is no longer any internal concept of joinability. --- glib/gthread-posix.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c index c6d6c0a..a1fdf84 100644 --- a/glib/gthread-posix.c +++ b/glib/gthread-posix.c @@ -1074,6 +1074,8 @@ typedef struct GRealThread thread; pthread_t system_thread; + gboolean joined; + GMutex lock; } GThreadPosix; void @@ -1081,6 +1083,11 @@ g_system_thread_free (GRealThread *thread) { GThreadPosix *pt = (GThreadPosix *) thread; + if (!pt->joined) + pthread_detach (pt->system_thread); + + g_mutex_clear (&pt->lock); + g_slice_free (GThreadPosix, pt); } @@ -1110,9 +1117,6 @@ g_system_thread_new (GThreadFunc thread_func, } #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */ - posix_check_cmd (pthread_attr_setdetachstate (&attr, - joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED)); - ret = pthread_create (&thread->system_thread, &attr, (void* (*)(void*))thread_func, thread); posix_check_cmd (pthread_attr_destroy (&attr)); @@ -1127,6 +1131,8 @@ g_system_thread_new (GThreadFunc thread_func, posix_check_err (ret, "pthread_create"); + g_mutex_init (&thread->lock); + return (GRealThread *) thread; } @@ -1148,8 +1154,16 @@ void g_system_thread_wait (GRealThread *thread) { GThreadPosix *pt = (GThreadPosix *) thread; - gpointer ignore; - posix_check_cmd (pthread_join (pt->system_thread, &ignore)); + + g_mutex_lock (&pt->lock); + + if (!pt->joined) + { + posix_check_cmd (pthread_join (pt->system_thread, NULL)); + pt->joined = TRUE; + } + + g_mutex_unlock (&pt->lock); } void -- 2.7.4