win32 threads: simplify

Merge the GThreadData with the GThreadWin32 struct.  Drop the extra TLS
variable.

Close the handle on _free(), which means that there is no leak if
g_system_thread_join() isn't called.

Remove all internal concept of joinability.
This commit is contained in:
Ryan Lortie 2011-10-12 23:22:31 -04:00
parent dfd466979b
commit 45736d33bb

View File

@ -450,24 +450,14 @@ g_private_replace (GPrivate *key,
#define G_MUTEX_SIZE (sizeof (gpointer)) #define G_MUTEX_SIZE (sizeof (gpointer))
static DWORD g_thread_self_tls;
typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *); typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
typedef struct _GThreadData GThreadData;
struct _GThreadData
{
GThreadFunc func;
gpointer data;
HANDLE thread;
gboolean joinable;
};
typedef struct typedef struct
{ {
GRealThread thread; GRealThread thread;
GThreadData *data; GThreadFunc proxy;
HANDLE handle;
} GThreadWin32; } GThreadWin32;
void void
@ -475,6 +465,7 @@ g_system_thread_free (GRealThread *thread)
{ {
GThreadWin32 *wt = (GThreadWin32 *) thread; GThreadWin32 *wt = (GThreadWin32 *) thread;
win32_check_for_error (CloseHandle (wt->handle));
g_slice_free (GThreadWin32, wt); g_slice_free (GThreadWin32, wt);
} }
@ -487,11 +478,9 @@ g_system_thread_exit (void)
static guint __stdcall static guint __stdcall
g_thread_win32_proxy (gpointer data) g_thread_win32_proxy (gpointer data)
{ {
GThreadData *self = (GThreadData*) data; GThreadWin32 *self = data;
win32_check_for_error (TlsSetValue (g_thread_self_tls, self)); self->proxy (self);
self->func (self->data);
g_system_thread_exit (); g_system_thread_exit ();
@ -508,31 +497,22 @@ g_system_thread_new (GThreadFunc func,
{ {
GThreadWin32 *thread; GThreadWin32 *thread;
guint ignore; guint ignore;
GThreadData *retval;
thread = g_slice_new0 (GThreadWin32); thread = g_slice_new0 (GThreadWin32);
retval = g_new(GThreadData, 1); thread->proxy = func;
retval->func = func;
retval->data = thread;
retval->joinable = joinable; thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread, 0, &ignore);
retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, if (thread->handle == NULL)
retval, 0, &ignore);
if (retval->thread == NULL)
{ {
gchar *win_error = g_win32_error_message (GetLastError ()); gchar *win_error = g_win32_error_message (GetLastError ());
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
"Error creating thread: %s", win_error); "Error creating thread: %s", win_error);
g_free (retval);
g_free (win_error); g_free (win_error);
g_slice_free (GThreadWin32, thread); g_slice_free (GThreadWin32, thread);
return NULL; return NULL;
} }
thread->data = retval;
return (GRealThread *) thread; return (GRealThread *) thread;
} }
@ -546,15 +526,8 @@ void
g_system_thread_wait (GRealThread *thread) g_system_thread_wait (GRealThread *thread)
{ {
GThreadWin32 *wt = (GThreadWin32 *) thread; GThreadWin32 *wt = (GThreadWin32 *) thread;
GThreadData *target = wt->data;
g_return_if_fail (target->joinable); win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE));
win32_check_for_error (WAIT_FAILED !=
WaitForSingleObject (target->thread, INFINITE));
win32_check_for_error (CloseHandle (target->thread));
g_free (target);
} }
void void
@ -1023,14 +996,12 @@ g_thread_win32_init (void)
g_thread_xp_init (); g_thread_xp_init ();
} }
win32_check_for_error (TLS_OUT_OF_INDEXES != (g_thread_self_tls = TlsAlloc ()));
InitializeCriticalSection (&g_private_lock); InitializeCriticalSection (&g_private_lock);
} }
G_GNUC_INTERNAL void G_GNUC_INTERNAL void
g_thread_win32_thread_detach (void) g_thread_win32_thread_detach (void)
{ {
GThreadData *self = TlsGetValue (g_thread_self_tls);
gboolean dtors_called; gboolean dtors_called;
do do
@ -1062,16 +1033,6 @@ g_thread_win32_thread_detach (void)
} }
while (dtors_called); while (dtors_called);
if (self)
{
if (!self->joinable)
{
win32_check_for_error (CloseHandle (self->thread));
g_free (self);
}
win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
}
if (g_thread_impl_vtable.CallThisOnThreadExit) if (g_thread_impl_vtable.CallThisOnThreadExit)
g_thread_impl_vtable.CallThisOnThreadExit (); g_thread_impl_vtable.CallThisOnThreadExit ();
} }