1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: solaris thread system implementation
5 * Copyright 1998-2001 Sebastian Wilhelmi; University of Karlsruhe
6 * Copyright 2001 Hans Breuer
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GLib Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 /* The GMutex and GCond implementations in this file are some of the
32 * lowest-level code in GLib. All other parts of GLib (messages,
33 * memory, slices, etc) assume that they can freely use these facilities
34 * without risking recursion.
36 * As such, these functions are NOT permitted to call any other part of
39 * The thread manipulation functions (create, exit, join, etc.) have
40 * more freedom -- they can do as they please.
46 #include "gthreadprivate.h"
56 g_thread_abort (gint status,
57 const gchar *function)
59 fprintf (stderr, "GLib (gthread-win32.c): Unexpected error from C library during '%s': %s. Aborting.\n",
60 strerror (status), function);
64 /* Starting with Vista and Windows 2008, we have access to the
65 * CONDITION_VARIABLE and SRWLock primatives on Windows, which are
66 * pretty reasonable approximations of the primatives specified in
67 * POSIX 2001 (pthread_cond_t and pthread_mutex_t respectively).
69 * Both of these types are structs containing a single pointer. That
70 * pointer is used as an atomic bitfield to support user-space mutexes
71 * that only get the kernel involved in cases of contention (similar
72 * to how futex()-based mutexes work on Linux). The biggest advantage
73 * of these new types is that they can be statically initialised to
74 * zero. That means that they are completely ABI compatible with our
75 * GMutex and GCond APIs.
77 * Unfortunately, Windows XP lacks these facilities and GLib still
78 * needs to support Windows XP. Our approach here is as follows:
80 * - avoid depending on structure declarations at compile-time by
81 * declaring our own GMutex and GCond strutures to be
82 * ABI-compatible with SRWLock and CONDITION_VARIABLE and using
85 * - avoid a hard dependency on the symbols used to manipulate these
86 * structures by doing a dynamic lookup of those symbols at
89 * - if the symbols are not available, emulate them using other
92 * Using this approach also allows us to easily build a GLib that lacks
93 * support for Windows XP or to remove this code entirely when XP is no
94 * longer supported (end of line is currently April 8, 2014).
98 void (__stdcall * CallThisOnThreadExit) (void); /* fake */
100 void (__stdcall * InitializeSRWLock) (gpointer lock);
101 void (__stdcall * DeleteSRWLock) (gpointer lock); /* fake */
102 void (__stdcall * AcquireSRWLockExclusive) (gpointer lock);
103 BOOLEAN (__stdcall * TryAcquireSRWLockExclusive) (gpointer lock);
104 void (__stdcall * ReleaseSRWLockExclusive) (gpointer lock);
105 void (__stdcall * AcquireSRWLockShared) (gpointer lock);
106 BOOLEAN (__stdcall * TryAcquireSRWLockShared) (gpointer lock);
107 void (__stdcall * ReleaseSRWLockShared) (gpointer lock);
109 void (__stdcall * InitializeConditionVariable) (gpointer cond);
110 void (__stdcall * DeleteConditionVariable) (gpointer cond); /* fake */
111 BOOL (__stdcall * SleepConditionVariableSRW) (gpointer cond,
115 void (__stdcall * WakeAllConditionVariable) (gpointer cond);
116 void (__stdcall * WakeConditionVariable) (gpointer cond);
119 static GThreadImplVtable g_thread_impl_vtable;
123 g_mutex_init (GMutex *mutex)
125 g_thread_impl_vtable.InitializeSRWLock (mutex);
129 g_mutex_clear (GMutex *mutex)
131 if (g_thread_impl_vtable.DeleteSRWLock != NULL)
132 g_thread_impl_vtable.DeleteSRWLock (mutex);
136 g_mutex_lock (GMutex *mutex)
138 g_thread_impl_vtable.AcquireSRWLockExclusive (mutex);
142 g_mutex_trylock (GMutex *mutex)
144 return g_thread_impl_vtable.TryAcquireSRWLockExclusive (mutex);
148 g_mutex_unlock (GMutex *mutex)
150 g_thread_impl_vtable.ReleaseSRWLockExclusive (mutex);
155 static CRITICAL_SECTION *
156 g_rec_mutex_impl_new (void)
158 CRITICAL_SECTION *cs;
160 cs = g_slice_new (CRITICAL_SECTION);
161 InitializeCriticalSection (cs);
167 g_rec_mutex_impl_free (CRITICAL_SECTION *cs)
169 DeleteCriticalSection (cs);
170 g_slice_free (CRITICAL_SECTION, cs);
173 static CRITICAL_SECTION *
174 g_rec_mutex_get_impl (GRecMutex *mutex)
176 CRITICAL_SECTION *impl = mutex->p;
178 if G_UNLIKELY (mutex->p == NULL)
180 impl = g_rec_mutex_impl_new ();
181 if (InterlockedCompareExchangePointer (&mutex->p, impl, NULL) != NULL)
182 g_rec_mutex_impl_free (impl);
190 g_rec_mutex_init (GRecMutex *mutex)
192 mutex->p = g_rec_mutex_impl_new ();
196 g_rec_mutex_clear (GRecMutex *mutex)
198 g_rec_mutex_impl_free (mutex->p);
202 g_rec_mutex_lock (GRecMutex *mutex)
204 EnterCriticalSection (g_rec_mutex_get_impl (mutex));
208 g_rec_mutex_unlock (GRecMutex *mutex)
210 LeaveCriticalSection (mutex->p);
214 g_rec_mutex_trylock (GRecMutex *mutex)
216 return TryEnterCriticalSection (g_rec_mutex_get_impl (mutex));
222 g_rw_lock_init (GRWLock *lock)
224 g_thread_impl_vtable.InitializeSRWLock (lock);
228 g_rw_lock_clear (GRWLock *lock)
230 if (g_thread_impl_vtable.DeleteSRWLock != NULL)
231 g_thread_impl_vtable.DeleteSRWLock (lock);
235 g_rw_lock_writer_lock (GRWLock *lock)
237 g_thread_impl_vtable.AcquireSRWLockExclusive (lock);
241 g_rw_lock_writer_trylock (GRWLock *lock)
243 return g_thread_impl_vtable.TryAcquireSRWLockExclusive (lock);
247 g_rw_lock_writer_unlock (GRWLock *lock)
249 g_thread_impl_vtable.ReleaseSRWLockExclusive (lock);
253 g_rw_lock_reader_lock (GRWLock *lock)
255 g_thread_impl_vtable.AcquireSRWLockShared (lock);
259 g_rw_lock_reader_trylock (GRWLock *lock)
261 return g_thread_impl_vtable.TryAcquireSRWLockShared (lock);
265 g_rw_lock_reader_unlock (GRWLock *lock)
267 g_thread_impl_vtable.ReleaseSRWLockShared (lock);
272 g_cond_init (GCond *cond)
274 g_thread_impl_vtable.InitializeConditionVariable (cond);
278 g_cond_clear (GCond *cond)
280 if (g_thread_impl_vtable.DeleteConditionVariable)
281 g_thread_impl_vtable.DeleteConditionVariable (cond);
285 g_cond_signal (GCond *cond)
287 g_thread_impl_vtable.WakeConditionVariable (cond);
291 g_cond_broadcast (GCond *cond)
293 g_thread_impl_vtable.WakeAllConditionVariable (cond);
297 g_cond_wait (GCond *cond,
298 GMutex *entered_mutex)
300 g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, INFINITE, 0);
304 g_cond_timedwait (GCond *cond,
305 GMutex *entered_mutex,
312 GetSystemTimeAsFileTime (&ft);
313 memmove (&now, &ft, sizeof (FILETIME));
315 now -= G_GINT64_CONSTANT (116444736000000000);
318 span = abs_time - now;
320 if G_UNLIKELY (span < 0)
323 if G_UNLIKELY (span > G_GINT64_CONSTANT (1000) * G_MAXINT32)
326 return g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, span / 1000, 0);
330 g_cond_timed_wait (GCond *cond,
331 GMutex *entered_mutex,
338 micros = abs_time->tv_sec;
340 micros += abs_time->tv_usec;
342 return g_cond_timedwait (cond, entered_mutex, micros);
346 g_cond_wait (cond, entered_mutex);
353 typedef struct _GPrivateDestructor GPrivateDestructor;
355 struct _GPrivateDestructor
358 GDestroyNotify notify;
359 GPrivateDestructor *next;
362 static GPrivateDestructor * volatile g_private_destructors;
363 static CRITICAL_SECTION g_private_lock;
366 g_private_get_impl (GPrivate *key)
368 DWORD impl = (DWORD) key->p;
370 if G_UNLIKELY (impl == 0)
372 EnterCriticalSection (&g_private_lock);
373 impl = (DWORD) key->p;
376 GPrivateDestructor *destructor;
380 if (impl == TLS_OUT_OF_INDEXES)
381 g_thread_abort (0, "TlsAlloc");
383 if (key->notify != NULL)
385 destructor = malloc (sizeof (GPrivateDestructor));
386 if G_UNLIKELY (destructor == NULL)
387 g_thread_abort (errno, "malloc");
388 destructor->index = impl;
389 destructor->notify = key->notify;
390 destructor->next = g_private_destructors;
392 /* We need to do an atomic store due to the unlocked
393 * access to the destructor list from the thread exit
396 * It can double as a sanity check...
398 if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
399 destructor->next) != destructor->next)
400 g_thread_abort (0, "g_private_get_impl(1)");
403 /* Ditto, due to the unlocked access on the fast path */
404 if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
405 g_thread_abort (0, "g_private_get_impl(2)");
407 LeaveCriticalSection (&g_private_lock);
414 g_private_get (GPrivate *key)
416 return TlsGetValue (g_private_get_impl (key));
420 g_private_set (GPrivate *key,
423 TlsSetValue (g_private_get_impl (key), value);
427 g_private_replace (GPrivate *key,
430 DWORD impl = g_private_get_impl (key);
433 old = TlsGetValue (impl);
434 if (old && key->notify)
436 TlsSetValue (impl, value);
442 #include "gthreadprivate.h"
444 #define win32_check_for_error(what) G_STMT_START{ \
446 g_error ("file %s: line %d (%s): error %s during %s", \
447 __FILE__, __LINE__, G_STRFUNC, \
448 g_win32_error_message (GetLastError ()), #what); \
451 #define G_MUTEX_SIZE (sizeof (gpointer))
453 static DWORD g_thread_self_tls;
455 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
457 typedef struct _GThreadData GThreadData;
474 g_system_thread_free (GRealThread *thread)
476 GThreadWin32 *wt = (GThreadWin32 *) thread;
478 g_slice_free (GThreadWin32, wt);
482 g_system_thread_exit (void)
487 static guint __stdcall
488 g_thread_win32_proxy (gpointer data)
490 GThreadData *self = (GThreadData*) data;
492 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
494 self->func (self->data);
496 g_system_thread_exit ();
498 g_assert_not_reached ();
504 g_system_thread_new (GThreadFunc func,
509 GThreadWin32 *thread;
513 thread = g_slice_new0 (GThreadWin32);
514 retval = g_new(GThreadData, 1);
516 retval->data = thread;
518 retval->joinable = joinable;
520 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy,
523 if (retval->thread == NULL)
525 gchar *win_error = g_win32_error_message (GetLastError ());
526 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
527 "Error creating thread: %s", win_error);
530 g_slice_free (GThreadWin32, thread);
534 thread->data = retval;
536 return (GRealThread *) thread;
540 g_thread_yield (void)
546 g_system_thread_wait (GRealThread *thread)
548 GThreadWin32 *wt = (GThreadWin32 *) thread;
549 GThreadData *target = wt->data;
551 g_return_if_fail (target->joinable);
553 win32_check_for_error (WAIT_FAILED !=
554 WaitForSingleObject (target->thread, INFINITE));
556 win32_check_for_error (CloseHandle (target->thread));
561 g_system_thread_set_name (const gchar *name)
563 /* FIXME: implement */
566 /* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
568 static CRITICAL_SECTION g_thread_xp_lock;
569 static DWORD g_thread_xp_waiter_tls;
571 /* {{{2 GThreadWaiter utility class for CONDITION_VARIABLE emulation */
572 typedef struct _GThreadXpWaiter GThreadXpWaiter;
573 struct _GThreadXpWaiter
576 volatile GThreadXpWaiter *next;
579 static GThreadXpWaiter *
580 g_thread_xp_waiter_get (void)
582 GThreadXpWaiter *waiter;
584 waiter = TlsGetValue (g_thread_xp_waiter_tls);
586 if G_UNLIKELY (waiter == NULL)
588 waiter = malloc (sizeof (GThreadXpWaiter));
590 g_thread_abort (GetLastError (), "malloc");
591 waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
592 if (waiter->event == NULL)
593 g_thread_abort (GetLastError (), "CreateEvent");
595 TlsSetValue (g_thread_xp_waiter_tls, waiter);
601 static void __stdcall
602 g_thread_xp_CallThisOnThreadExit (void)
604 GThreadXpWaiter *waiter;
606 waiter = TlsGetValue (g_thread_xp_waiter_tls);
610 TlsSetValue (g_thread_xp_waiter_tls, NULL);
611 CloseHandle (waiter->event);
616 /* {{{2 SRWLock emulation */
619 CRITICAL_SECTION writer_lock;
620 gboolean ever_shared; /* protected by writer_lock */
621 gboolean writer_locked; /* protected by writer_lock */
623 /* below is only ever touched if ever_shared becomes true */
624 CRITICAL_SECTION atomicity;
625 GThreadXpWaiter *queued_writer; /* protected by atomicity lock */
626 gint num_readers; /* protected by atomicity lock */
629 static void __stdcall
630 g_thread_xp_InitializeSRWLock (gpointer mutex)
632 *(GThreadSRWLock * volatile *) mutex = NULL;
635 static void __stdcall
636 g_thread_xp_DeleteSRWLock (gpointer mutex)
638 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
642 if (lock->ever_shared)
643 DeleteCriticalSection (&lock->atomicity);
645 DeleteCriticalSection (&lock->writer_lock);
650 static GThreadSRWLock * __stdcall
651 g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
653 GThreadSRWLock *result;
655 /* It looks like we're missing some barriers here, but this code only
656 * ever runs on Windows XP, which in turn only ever runs on hardware
657 * with a relatively rigid memory model. The 'volatile' will take
658 * care of the compiler.
662 if G_UNLIKELY (result == NULL)
664 EnterCriticalSection (&g_thread_xp_lock);
666 result = malloc (sizeof (GThreadSRWLock));
669 g_thread_abort (errno, "malloc");
671 InitializeCriticalSection (&result->writer_lock);
672 result->writer_locked = FALSE;
673 result->ever_shared = FALSE;
676 LeaveCriticalSection (&g_thread_xp_lock);
682 static void __stdcall
683 g_thread_xp_AcquireSRWLockExclusive (gpointer mutex)
685 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
687 EnterCriticalSection (&lock->writer_lock);
689 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
690 * Detect the deadlock that would occur on later Windows version.
692 g_assert (!lock->writer_locked);
693 lock->writer_locked = TRUE;
695 if (lock->ever_shared)
697 GThreadXpWaiter *waiter = NULL;
699 EnterCriticalSection (&lock->atomicity);
700 if (lock->num_readers > 0)
701 lock->queued_writer = waiter = g_thread_xp_waiter_get ();
702 LeaveCriticalSection (&lock->atomicity);
705 WaitForSingleObject (waiter->event, INFINITE);
707 lock->queued_writer = NULL;
711 static BOOLEAN __stdcall
712 g_thread_xp_TryAcquireSRWLockExclusive (gpointer mutex)
714 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
716 if (!TryEnterCriticalSection (&lock->writer_lock))
719 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
720 * Ensure that this properly returns FALSE (as SRWLock would).
722 if G_UNLIKELY (lock->writer_locked)
724 LeaveCriticalSection (&lock->writer_lock);
728 lock->writer_locked = TRUE;
730 if (lock->ever_shared)
734 EnterCriticalSection (&lock->atomicity);
735 available = lock->num_readers == 0;
736 LeaveCriticalSection (&lock->atomicity);
740 LeaveCriticalSection (&lock->writer_lock);
748 static void __stdcall
749 g_thread_xp_ReleaseSRWLockExclusive (gpointer mutex)
751 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
753 lock->writer_locked = FALSE;
755 /* We need this until we fix some weird parts of GLib that try to
756 * unlock freshly-allocated mutexes.
759 LeaveCriticalSection (&lock->writer_lock);
763 g_thread_xp_srwlock_become_reader (GThreadSRWLock *lock)
765 if G_UNLIKELY (!lock->ever_shared)
767 InitializeCriticalSection (&lock->atomicity);
768 lock->queued_writer = NULL;
769 lock->num_readers = 0;
771 lock->ever_shared = TRUE;
774 EnterCriticalSection (&lock->atomicity);
776 LeaveCriticalSection (&lock->atomicity);
779 static void __stdcall
780 g_thread_xp_AcquireSRWLockShared (gpointer mutex)
782 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
784 EnterCriticalSection (&lock->writer_lock);
786 /* See g_thread_xp_AcquireSRWLockExclusive */
787 g_assert (!lock->writer_locked);
789 g_thread_xp_srwlock_become_reader (lock);
791 LeaveCriticalSection (&lock->writer_lock);
794 static BOOLEAN __stdcall
795 g_thread_xp_TryAcquireSRWLockShared (gpointer mutex)
797 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
799 if (!TryEnterCriticalSection (&lock->writer_lock))
802 /* See g_thread_xp_AcquireSRWLockExclusive */
803 if G_UNLIKELY (lock->writer_locked)
805 LeaveCriticalSection (&lock->writer_lock);
809 g_thread_xp_srwlock_become_reader (lock);
811 LeaveCriticalSection (&lock->writer_lock);
816 static void __stdcall
817 g_thread_xp_ReleaseSRWLockShared (gpointer mutex)
819 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
821 EnterCriticalSection (&lock->atomicity);
825 if (lock->num_readers == 0 && lock->queued_writer)
826 SetEvent (lock->queued_writer->event);
828 LeaveCriticalSection (&lock->atomicity);
831 /* {{{2 CONDITION_VARIABLE emulation */
834 volatile GThreadXpWaiter *first;
835 volatile GThreadXpWaiter **last_ptr;
836 } GThreadXpCONDITION_VARIABLE;
838 static void __stdcall
839 g_thread_xp_InitializeConditionVariable (gpointer cond)
841 *(GThreadXpCONDITION_VARIABLE * volatile *) cond = NULL;
844 static void __stdcall
845 g_thread_xp_DeleteConditionVariable (gpointer cond)
847 GThreadXpCONDITION_VARIABLE *cv = *(GThreadXpCONDITION_VARIABLE * volatile *) cond;
853 static GThreadXpCONDITION_VARIABLE * __stdcall
854 g_thread_xp_get_condition_variable (GThreadXpCONDITION_VARIABLE * volatile *cond)
856 GThreadXpCONDITION_VARIABLE *result;
858 /* It looks like we're missing some barriers here, but this code only
859 * ever runs on Windows XP, which in turn only ever runs on hardware
860 * with a relatively rigid memory model. The 'volatile' will take
861 * care of the compiler.
865 if G_UNLIKELY (result == NULL)
867 result = malloc (sizeof (GThreadXpCONDITION_VARIABLE));
870 g_thread_abort (errno, "malloc");
872 result->first = NULL;
873 result->last_ptr = &result->first;
875 if (InterlockedCompareExchangePointer (cond, result, NULL) != NULL)
885 static BOOL __stdcall
886 g_thread_xp_SleepConditionVariableSRW (gpointer cond,
891 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
892 GThreadXpWaiter *waiter = g_thread_xp_waiter_get ();
897 EnterCriticalSection (&g_thread_xp_lock);
898 *cv->last_ptr = waiter;
899 cv->last_ptr = &waiter->next;
900 LeaveCriticalSection (&g_thread_xp_lock);
902 g_mutex_unlock (mutex);
903 status = WaitForSingleObject (waiter->event, timeout);
905 if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
906 g_thread_abort (GetLastError (), "WaitForSingleObject");
908 g_mutex_lock (mutex);
910 return status == WAIT_OBJECT_0;
913 static void __stdcall
914 g_thread_xp_WakeConditionVariable (gpointer cond)
916 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
917 volatile GThreadXpWaiter *waiter;
919 EnterCriticalSection (&g_thread_xp_lock);
923 cv->first = waiter->next;
924 if (cv->first == NULL)
925 cv->last_ptr = &cv->first;
927 LeaveCriticalSection (&g_thread_xp_lock);
930 SetEvent (waiter->event);
933 static void __stdcall
934 g_thread_xp_WakeAllConditionVariable (gpointer cond)
936 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
937 volatile GThreadXpWaiter *waiter;
939 EnterCriticalSection (&g_thread_xp_lock);
942 cv->last_ptr = &cv->first;
943 LeaveCriticalSection (&g_thread_xp_lock);
945 while (waiter != NULL)
947 volatile GThreadXpWaiter *next;
950 SetEvent (waiter->event);
957 g_thread_xp_init (void)
959 static const GThreadImplVtable g_thread_xp_impl_vtable = {
960 g_thread_xp_CallThisOnThreadExit,
961 g_thread_xp_InitializeSRWLock,
962 g_thread_xp_DeleteSRWLock,
963 g_thread_xp_AcquireSRWLockExclusive,
964 g_thread_xp_TryAcquireSRWLockExclusive,
965 g_thread_xp_ReleaseSRWLockExclusive,
966 g_thread_xp_AcquireSRWLockShared,
967 g_thread_xp_TryAcquireSRWLockShared,
968 g_thread_xp_ReleaseSRWLockShared,
969 g_thread_xp_InitializeConditionVariable,
970 g_thread_xp_DeleteConditionVariable,
971 g_thread_xp_SleepConditionVariableSRW,
972 g_thread_xp_WakeAllConditionVariable,
973 g_thread_xp_WakeConditionVariable
976 InitializeCriticalSection (&g_thread_xp_lock);
977 g_thread_xp_waiter_tls = TlsAlloc ();
979 g_thread_impl_vtable = g_thread_xp_impl_vtable;
985 g_thread_lookup_native_funcs (void)
987 GThreadImplVtable native_vtable = { 0, };
990 kernel32 = GetModuleHandle ("KERNEL32.DLL");
992 if (kernel32 == NULL)
995 #define GET_FUNC(name) if ((native_vtable.name = (void *) GetProcAddress (kernel32, #name)) == NULL) return FALSE
996 GET_FUNC(InitializeSRWLock);
997 GET_FUNC(AcquireSRWLockExclusive);
998 GET_FUNC(TryAcquireSRWLockExclusive);
999 GET_FUNC(ReleaseSRWLockExclusive);
1000 GET_FUNC(AcquireSRWLockShared);
1001 GET_FUNC(TryAcquireSRWLockShared);
1002 GET_FUNC(ReleaseSRWLockShared);
1004 GET_FUNC(InitializeConditionVariable);
1005 GET_FUNC(SleepConditionVariableSRW);
1006 GET_FUNC(WakeAllConditionVariable);
1007 GET_FUNC(WakeConditionVariable);
1010 g_thread_impl_vtable = native_vtable;
1015 G_GNUC_INTERNAL void
1016 g_thread_win32_init (void)
1018 if (g_thread_lookup_native_funcs ())
1019 fprintf (stderr, "(debug) GThread using native mode\n");
1022 fprintf (stderr, "(debug) GThread using Windows XP mode\n");
1023 g_thread_xp_init ();
1026 win32_check_for_error (TLS_OUT_OF_INDEXES != (g_thread_self_tls = TlsAlloc ()));
1027 InitializeCriticalSection (&g_private_lock);
1030 G_GNUC_INTERNAL void
1031 g_thread_win32_thread_detach (void)
1033 GThreadData *self = TlsGetValue (g_thread_self_tls);
1034 gboolean dtors_called;
1038 GPrivateDestructor *dtor;
1040 /* We go by the POSIX book on this one.
1042 * If we call a destructor then there is a chance that some new
1043 * TLS variables got set by code called in that destructor.
1045 * Loop until nothing is left.
1047 dtors_called = FALSE;
1049 for (dtor = g_private_destructors; dtor; dtor = dtor->next)
1053 value = TlsGetValue (dtor->index);
1054 if (value != NULL && dtor->notify != NULL)
1056 /* POSIX says to clear this before the call */
1057 TlsSetValue (dtor->index, NULL);
1058 dtor->notify (value);
1059 dtors_called = TRUE;
1063 while (dtors_called);
1067 if (!self->joinable)
1069 win32_check_for_error (CloseHandle (self->thread));
1072 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
1075 if (g_thread_impl_vtable.CallThisOnThreadExit)
1076 g_thread_impl_vtable.CallThisOnThreadExit ();
1079 /* vim:set foldmethod=marker: */