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.
This commit is contained in:
Ryan Lortie 2011-10-12 23:40:02 -04:00
parent f970dfc734
commit dbf20d585f

View 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