posix threads: joinable tweaks
authorRyan Lortie <desrt@desrt.ca>
Thu, 13 Oct 2011 03:40:02 +0000 (23:40 -0400)
committerRyan Lortie <desrt@desrt.ca>
Thu, 13 Oct 2011 03:40:02 +0000 (23:40 -0400)
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

index c6d6c0a..a1fdf84 100644 (file)
@@ -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