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.
47 #include "gthreadprivate.h"
57 g_thread_abort (gint status,
58 const gchar *function)
60 fprintf (stderr, "GLib (gthread-win32.c): Unexpected error from C library during '%s': %s. Aborting.\n",
61 strerror (status), function);
65 /* Starting with Vista and Windows 2008, we have access to the
66 * CONDITION_VARIABLE and SRWLock primatives on Windows, which are
67 * pretty reasonable approximations of the primatives specified in
68 * POSIX 2001 (pthread_cond_t and pthread_mutex_t respectively).
70 * Both of these types are structs containing a single pointer. That
71 * pointer is used as an atomic bitfield to support user-space mutexes
72 * that only get the kernel involved in cases of contention (similar
73 * to how futex()-based mutexes work on Linux). The biggest advantage
74 * of these new types is that they can be statically initialised to
75 * zero. That means that they are completely ABI compatible with our
76 * GMutex and GCond APIs.
78 * Unfortunately, Windows XP lacks these facilities and GLib still
79 * needs to support Windows XP. Our approach here is as follows:
81 * - avoid depending on structure declarations at compile-time by
82 * declaring our own GMutex and GCond strutures to be
83 * ABI-compatible with SRWLock and CONDITION_VARIABLE and using
86 * - avoid a hard dependency on the symbols used to manipulate these
87 * structures by doing a dynamic lookup of those symbols at
90 * - if the symbols are not available, emulate them using other
93 * Using this approach also allows us to easily build a GLib that lacks
94 * support for Windows XP or to remove this code entirely when XP is no
95 * longer supported (end of line is currently April 8, 2014).
99 void (__stdcall * CallThisOnThreadExit) (void); /* fake */
101 void (__stdcall * InitializeSRWLock) (gpointer lock);
102 void (__stdcall * DeleteSRWLock) (gpointer lock); /* fake */
103 void (__stdcall * AcquireSRWLockExclusive) (gpointer lock);
104 BOOLEAN (__stdcall * TryAcquireSRWLockExclusive) (gpointer lock);
105 void (__stdcall * ReleaseSRWLockExclusive) (gpointer lock);
106 void (__stdcall * AcquireSRWLockShared) (gpointer lock);
107 BOOLEAN (__stdcall * TryAcquireSRWLockShared) (gpointer lock);
108 void (__stdcall * ReleaseSRWLockShared) (gpointer lock);
110 void (__stdcall * InitializeConditionVariable) (gpointer cond);
111 void (__stdcall * DeleteConditionVariable) (gpointer cond); /* fake */
112 BOOL (__stdcall * SleepConditionVariableSRW) (gpointer cond,
116 void (__stdcall * WakeAllConditionVariable) (gpointer cond);
117 void (__stdcall * WakeConditionVariable) (gpointer cond);
120 static GThreadImplVtable g_thread_impl_vtable;
124 g_mutex_init (GMutex *mutex)
126 g_thread_impl_vtable.InitializeSRWLock (mutex);
130 g_mutex_clear (GMutex *mutex)
132 if (g_thread_impl_vtable.DeleteSRWLock != NULL)
133 g_thread_impl_vtable.DeleteSRWLock (mutex);
137 g_mutex_lock (GMutex *mutex)
139 g_thread_impl_vtable.AcquireSRWLockExclusive (mutex);
143 g_mutex_trylock (GMutex *mutex)
145 return g_thread_impl_vtable.TryAcquireSRWLockExclusive (mutex);
149 g_mutex_unlock (GMutex *mutex)
151 g_thread_impl_vtable.ReleaseSRWLockExclusive (mutex);
156 static CRITICAL_SECTION *
157 g_rec_mutex_impl_new (void)
159 CRITICAL_SECTION *cs;
161 cs = g_slice_new (CRITICAL_SECTION);
162 InitializeCriticalSection (cs);
168 g_rec_mutex_impl_free (CRITICAL_SECTION *cs)
170 DeleteCriticalSection (cs);
171 g_slice_free (CRITICAL_SECTION, cs);
174 static CRITICAL_SECTION *
175 g_rec_mutex_get_impl (GRecMutex *mutex)
177 CRITICAL_SECTION *impl = mutex->p;
179 if G_UNLIKELY (mutex->p == NULL)
181 impl = g_rec_mutex_impl_new ();
182 if (InterlockedCompareExchangePointer (&mutex->p, impl, NULL) != NULL)
183 g_rec_mutex_impl_free (impl);
191 g_rec_mutex_init (GRecMutex *mutex)
193 mutex->p = g_rec_mutex_impl_new ();
197 g_rec_mutex_clear (GRecMutex *mutex)
199 g_rec_mutex_impl_free (mutex->p);
203 g_rec_mutex_lock (GRecMutex *mutex)
205 EnterCriticalSection (g_rec_mutex_get_impl (mutex));
209 g_rec_mutex_unlock (GRecMutex *mutex)
211 LeaveCriticalSection (mutex->p);
215 g_rec_mutex_trylock (GRecMutex *mutex)
217 return TryEnterCriticalSection (g_rec_mutex_get_impl (mutex));
223 g_rw_lock_init (GRWLock *lock)
225 g_thread_impl_vtable.InitializeSRWLock (lock);
229 g_rw_lock_clear (GRWLock *lock)
231 if (g_thread_impl_vtable.DeleteSRWLock != NULL)
232 g_thread_impl_vtable.DeleteSRWLock (lock);
236 g_rw_lock_writer_lock (GRWLock *lock)
238 g_thread_impl_vtable.AcquireSRWLockExclusive (lock);
242 g_rw_lock_writer_trylock (GRWLock *lock)
244 return g_thread_impl_vtable.TryAcquireSRWLockExclusive (lock);
248 g_rw_lock_writer_unlock (GRWLock *lock)
250 g_thread_impl_vtable.ReleaseSRWLockExclusive (lock);
254 g_rw_lock_reader_lock (GRWLock *lock)
256 g_thread_impl_vtable.AcquireSRWLockShared (lock);
260 g_rw_lock_reader_trylock (GRWLock *lock)
262 return g_thread_impl_vtable.TryAcquireSRWLockShared (lock);
266 g_rw_lock_reader_unlock (GRWLock *lock)
268 g_thread_impl_vtable.ReleaseSRWLockShared (lock);
273 g_cond_init (GCond *cond)
275 g_thread_impl_vtable.InitializeConditionVariable (cond);
279 g_cond_clear (GCond *cond)
281 if (g_thread_impl_vtable.DeleteConditionVariable)
282 g_thread_impl_vtable.DeleteConditionVariable (cond);
286 g_cond_signal (GCond *cond)
288 g_thread_impl_vtable.WakeConditionVariable (cond);
292 g_cond_broadcast (GCond *cond)
294 g_thread_impl_vtable.WakeAllConditionVariable (cond);
298 g_cond_wait (GCond *cond,
299 GMutex *entered_mutex)
301 g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, INFINITE, 0);
305 g_cond_wait_until (GCond *cond,
306 GMutex *entered_mutex,
313 GetSystemTimeAsFileTime (&ft);
314 memmove (&now, &ft, sizeof (FILETIME));
316 now -= G_GINT64_CONSTANT (116444736000000000);
319 span = end_time - now;
321 if G_UNLIKELY (span < 0)
324 if G_UNLIKELY (span > G_GINT64_CONSTANT (1000) * G_MAXINT32)
327 return g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, span / 1000, 0);
332 typedef struct _GPrivateDestructor GPrivateDestructor;
334 struct _GPrivateDestructor
337 GDestroyNotify notify;
338 GPrivateDestructor *next;
341 static GPrivateDestructor * volatile g_private_destructors;
342 static CRITICAL_SECTION g_private_lock;
345 g_private_get_impl (GPrivate *key)
347 DWORD impl = (DWORD) key->p;
349 if G_UNLIKELY (impl == 0)
351 EnterCriticalSection (&g_private_lock);
352 impl = (DWORD) key->p;
355 GPrivateDestructor *destructor;
359 if (impl == TLS_OUT_OF_INDEXES)
360 g_thread_abort (0, "TlsAlloc");
362 if (key->notify != NULL)
364 destructor = malloc (sizeof (GPrivateDestructor));
365 if G_UNLIKELY (destructor == NULL)
366 g_thread_abort (errno, "malloc");
367 destructor->index = impl;
368 destructor->notify = key->notify;
369 destructor->next = g_private_destructors;
371 /* We need to do an atomic store due to the unlocked
372 * access to the destructor list from the thread exit
375 * It can double as a sanity check...
377 if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
378 destructor->next) != destructor->next)
379 g_thread_abort (0, "g_private_get_impl(1)");
382 /* Ditto, due to the unlocked access on the fast path */
383 if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
384 g_thread_abort (0, "g_private_get_impl(2)");
386 LeaveCriticalSection (&g_private_lock);
393 g_private_get (GPrivate *key)
395 return TlsGetValue (g_private_get_impl (key));
399 g_private_set (GPrivate *key,
402 TlsSetValue (g_private_get_impl (key), value);
406 g_private_replace (GPrivate *key,
409 DWORD impl = g_private_get_impl (key);
412 old = TlsGetValue (impl);
413 if (old && key->notify)
415 TlsSetValue (impl, value);
420 #define win32_check_for_error(what) G_STMT_START{ \
422 g_error ("file %s: line %d (%s): error %s during %s", \
423 __FILE__, __LINE__, G_STRFUNC, \
424 g_win32_error_message (GetLastError ()), #what); \
427 #define G_MUTEX_SIZE (sizeof (gpointer))
429 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
440 g_system_thread_free (GRealThread *thread)
442 GThreadWin32 *wt = (GThreadWin32 *) thread;
444 win32_check_for_error (CloseHandle (wt->handle));
445 g_slice_free (GThreadWin32, wt);
449 g_system_thread_exit (void)
454 static guint __stdcall
455 g_thread_win32_proxy (gpointer data)
457 GThreadWin32 *self = data;
461 g_system_thread_exit ();
463 g_assert_not_reached ();
469 g_system_thread_new (GThreadFunc func,
473 GThreadWin32 *thread;
476 thread = g_slice_new0 (GThreadWin32);
477 thread->proxy = func;
479 thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread, 0, &ignore);
481 if (thread->handle == NULL)
483 gchar *win_error = g_win32_error_message (GetLastError ());
484 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
485 "Error creating thread: %s", win_error);
487 g_slice_free (GThreadWin32, thread);
491 return (GRealThread *) thread;
495 g_thread_yield (void)
501 g_system_thread_wait (GRealThread *thread)
503 GThreadWin32 *wt = (GThreadWin32 *) thread;
505 win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE));
509 g_system_thread_set_name (const gchar *name)
511 /* FIXME: implement */
514 /* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
516 static CRITICAL_SECTION g_thread_xp_lock;
517 static DWORD g_thread_xp_waiter_tls;
519 /* {{{2 GThreadWaiter utility class for CONDITION_VARIABLE emulation */
520 typedef struct _GThreadXpWaiter GThreadXpWaiter;
521 struct _GThreadXpWaiter
524 volatile GThreadXpWaiter *next;
527 static GThreadXpWaiter *
528 g_thread_xp_waiter_get (void)
530 GThreadXpWaiter *waiter;
532 waiter = TlsGetValue (g_thread_xp_waiter_tls);
534 if G_UNLIKELY (waiter == NULL)
536 waiter = malloc (sizeof (GThreadXpWaiter));
538 g_thread_abort (GetLastError (), "malloc");
539 waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
540 if (waiter->event == NULL)
541 g_thread_abort (GetLastError (), "CreateEvent");
543 TlsSetValue (g_thread_xp_waiter_tls, waiter);
549 static void __stdcall
550 g_thread_xp_CallThisOnThreadExit (void)
552 GThreadXpWaiter *waiter;
554 waiter = TlsGetValue (g_thread_xp_waiter_tls);
558 TlsSetValue (g_thread_xp_waiter_tls, NULL);
559 CloseHandle (waiter->event);
564 /* {{{2 SRWLock emulation */
567 CRITICAL_SECTION writer_lock;
568 gboolean ever_shared; /* protected by writer_lock */
569 gboolean writer_locked; /* protected by writer_lock */
571 /* below is only ever touched if ever_shared becomes true */
572 CRITICAL_SECTION atomicity;
573 GThreadXpWaiter *queued_writer; /* protected by atomicity lock */
574 gint num_readers; /* protected by atomicity lock */
577 static void __stdcall
578 g_thread_xp_InitializeSRWLock (gpointer mutex)
580 *(GThreadSRWLock * volatile *) mutex = NULL;
583 static void __stdcall
584 g_thread_xp_DeleteSRWLock (gpointer mutex)
586 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
590 if (lock->ever_shared)
591 DeleteCriticalSection (&lock->atomicity);
593 DeleteCriticalSection (&lock->writer_lock);
598 static GThreadSRWLock * __stdcall
599 g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
601 GThreadSRWLock *result;
603 /* It looks like we're missing some barriers here, but this code only
604 * ever runs on Windows XP, which in turn only ever runs on hardware
605 * with a relatively rigid memory model. The 'volatile' will take
606 * care of the compiler.
610 if G_UNLIKELY (result == NULL)
612 EnterCriticalSection (&g_thread_xp_lock);
614 result = malloc (sizeof (GThreadSRWLock));
617 g_thread_abort (errno, "malloc");
619 InitializeCriticalSection (&result->writer_lock);
620 result->writer_locked = FALSE;
621 result->ever_shared = FALSE;
624 LeaveCriticalSection (&g_thread_xp_lock);
630 static void __stdcall
631 g_thread_xp_AcquireSRWLockExclusive (gpointer mutex)
633 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
635 EnterCriticalSection (&lock->writer_lock);
637 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
638 * Detect the deadlock that would occur on later Windows version.
640 g_assert (!lock->writer_locked);
641 lock->writer_locked = TRUE;
643 if (lock->ever_shared)
645 GThreadXpWaiter *waiter = NULL;
647 EnterCriticalSection (&lock->atomicity);
648 if (lock->num_readers > 0)
649 lock->queued_writer = waiter = g_thread_xp_waiter_get ();
650 LeaveCriticalSection (&lock->atomicity);
653 WaitForSingleObject (waiter->event, INFINITE);
655 lock->queued_writer = NULL;
659 static BOOLEAN __stdcall
660 g_thread_xp_TryAcquireSRWLockExclusive (gpointer mutex)
662 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
664 if (!TryEnterCriticalSection (&lock->writer_lock))
667 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
668 * Ensure that this properly returns FALSE (as SRWLock would).
670 if G_UNLIKELY (lock->writer_locked)
672 LeaveCriticalSection (&lock->writer_lock);
676 lock->writer_locked = TRUE;
678 if (lock->ever_shared)
682 EnterCriticalSection (&lock->atomicity);
683 available = lock->num_readers == 0;
684 LeaveCriticalSection (&lock->atomicity);
688 LeaveCriticalSection (&lock->writer_lock);
696 static void __stdcall
697 g_thread_xp_ReleaseSRWLockExclusive (gpointer mutex)
699 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
701 lock->writer_locked = FALSE;
703 /* We need this until we fix some weird parts of GLib that try to
704 * unlock freshly-allocated mutexes.
707 LeaveCriticalSection (&lock->writer_lock);
711 g_thread_xp_srwlock_become_reader (GThreadSRWLock *lock)
713 if G_UNLIKELY (!lock->ever_shared)
715 InitializeCriticalSection (&lock->atomicity);
716 lock->queued_writer = NULL;
717 lock->num_readers = 0;
719 lock->ever_shared = TRUE;
722 EnterCriticalSection (&lock->atomicity);
724 LeaveCriticalSection (&lock->atomicity);
727 static void __stdcall
728 g_thread_xp_AcquireSRWLockShared (gpointer mutex)
730 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
732 EnterCriticalSection (&lock->writer_lock);
734 /* See g_thread_xp_AcquireSRWLockExclusive */
735 g_assert (!lock->writer_locked);
737 g_thread_xp_srwlock_become_reader (lock);
739 LeaveCriticalSection (&lock->writer_lock);
742 static BOOLEAN __stdcall
743 g_thread_xp_TryAcquireSRWLockShared (gpointer mutex)
745 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
747 if (!TryEnterCriticalSection (&lock->writer_lock))
750 /* See g_thread_xp_AcquireSRWLockExclusive */
751 if G_UNLIKELY (lock->writer_locked)
753 LeaveCriticalSection (&lock->writer_lock);
757 g_thread_xp_srwlock_become_reader (lock);
759 LeaveCriticalSection (&lock->writer_lock);
764 static void __stdcall
765 g_thread_xp_ReleaseSRWLockShared (gpointer mutex)
767 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
769 EnterCriticalSection (&lock->atomicity);
773 if (lock->num_readers == 0 && lock->queued_writer)
774 SetEvent (lock->queued_writer->event);
776 LeaveCriticalSection (&lock->atomicity);
779 /* {{{2 CONDITION_VARIABLE emulation */
782 volatile GThreadXpWaiter *first;
783 volatile GThreadXpWaiter **last_ptr;
784 } GThreadXpCONDITION_VARIABLE;
786 static void __stdcall
787 g_thread_xp_InitializeConditionVariable (gpointer cond)
789 *(GThreadXpCONDITION_VARIABLE * volatile *) cond = NULL;
792 static void __stdcall
793 g_thread_xp_DeleteConditionVariable (gpointer cond)
795 GThreadXpCONDITION_VARIABLE *cv = *(GThreadXpCONDITION_VARIABLE * volatile *) cond;
801 static GThreadXpCONDITION_VARIABLE * __stdcall
802 g_thread_xp_get_condition_variable (GThreadXpCONDITION_VARIABLE * volatile *cond)
804 GThreadXpCONDITION_VARIABLE *result;
806 /* It looks like we're missing some barriers here, but this code only
807 * ever runs on Windows XP, which in turn only ever runs on hardware
808 * with a relatively rigid memory model. The 'volatile' will take
809 * care of the compiler.
813 if G_UNLIKELY (result == NULL)
815 result = malloc (sizeof (GThreadXpCONDITION_VARIABLE));
818 g_thread_abort (errno, "malloc");
820 result->first = NULL;
821 result->last_ptr = &result->first;
823 if (InterlockedCompareExchangePointer (cond, result, NULL) != NULL)
833 static BOOL __stdcall
834 g_thread_xp_SleepConditionVariableSRW (gpointer cond,
839 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
840 GThreadXpWaiter *waiter = g_thread_xp_waiter_get ();
845 EnterCriticalSection (&g_thread_xp_lock);
846 *cv->last_ptr = waiter;
847 cv->last_ptr = &waiter->next;
848 LeaveCriticalSection (&g_thread_xp_lock);
850 g_mutex_unlock (mutex);
851 status = WaitForSingleObject (waiter->event, timeout);
853 if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
854 g_thread_abort (GetLastError (), "WaitForSingleObject");
856 g_mutex_lock (mutex);
858 return status == WAIT_OBJECT_0;
861 static void __stdcall
862 g_thread_xp_WakeConditionVariable (gpointer cond)
864 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
865 volatile GThreadXpWaiter *waiter;
867 EnterCriticalSection (&g_thread_xp_lock);
871 cv->first = waiter->next;
872 if (cv->first == NULL)
873 cv->last_ptr = &cv->first;
875 LeaveCriticalSection (&g_thread_xp_lock);
878 SetEvent (waiter->event);
881 static void __stdcall
882 g_thread_xp_WakeAllConditionVariable (gpointer cond)
884 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
885 volatile GThreadXpWaiter *waiter;
887 EnterCriticalSection (&g_thread_xp_lock);
890 cv->last_ptr = &cv->first;
891 LeaveCriticalSection (&g_thread_xp_lock);
893 while (waiter != NULL)
895 volatile GThreadXpWaiter *next;
898 SetEvent (waiter->event);
905 g_thread_xp_init (void)
907 static const GThreadImplVtable g_thread_xp_impl_vtable = {
908 g_thread_xp_CallThisOnThreadExit,
909 g_thread_xp_InitializeSRWLock,
910 g_thread_xp_DeleteSRWLock,
911 g_thread_xp_AcquireSRWLockExclusive,
912 g_thread_xp_TryAcquireSRWLockExclusive,
913 g_thread_xp_ReleaseSRWLockExclusive,
914 g_thread_xp_AcquireSRWLockShared,
915 g_thread_xp_TryAcquireSRWLockShared,
916 g_thread_xp_ReleaseSRWLockShared,
917 g_thread_xp_InitializeConditionVariable,
918 g_thread_xp_DeleteConditionVariable,
919 g_thread_xp_SleepConditionVariableSRW,
920 g_thread_xp_WakeAllConditionVariable,
921 g_thread_xp_WakeConditionVariable
924 InitializeCriticalSection (&g_thread_xp_lock);
925 g_thread_xp_waiter_tls = TlsAlloc ();
927 g_thread_impl_vtable = g_thread_xp_impl_vtable;
933 g_thread_lookup_native_funcs (void)
935 GThreadImplVtable native_vtable = { 0, };
938 kernel32 = GetModuleHandle ("KERNEL32.DLL");
940 if (kernel32 == NULL)
943 #define GET_FUNC(name) if ((native_vtable.name = (void *) GetProcAddress (kernel32, #name)) == NULL) return FALSE
944 GET_FUNC(InitializeSRWLock);
945 GET_FUNC(AcquireSRWLockExclusive);
946 GET_FUNC(TryAcquireSRWLockExclusive);
947 GET_FUNC(ReleaseSRWLockExclusive);
948 GET_FUNC(AcquireSRWLockShared);
949 GET_FUNC(TryAcquireSRWLockShared);
950 GET_FUNC(ReleaseSRWLockShared);
952 GET_FUNC(InitializeConditionVariable);
953 GET_FUNC(SleepConditionVariableSRW);
954 GET_FUNC(WakeAllConditionVariable);
955 GET_FUNC(WakeConditionVariable);
958 g_thread_impl_vtable = native_vtable;
964 g_thread_win32_init (void)
966 if (g_thread_lookup_native_funcs ())
967 fprintf (stderr, "(debug) GThread using native mode\n");
970 fprintf (stderr, "(debug) GThread using Windows XP mode\n");
974 InitializeCriticalSection (&g_private_lock);
978 g_thread_win32_thread_detach (void)
980 gboolean dtors_called;
984 GPrivateDestructor *dtor;
986 /* We go by the POSIX book on this one.
988 * If we call a destructor then there is a chance that some new
989 * TLS variables got set by code called in that destructor.
991 * Loop until nothing is left.
993 dtors_called = FALSE;
995 for (dtor = g_private_destructors; dtor; dtor = dtor->next)
999 value = TlsGetValue (dtor->index);
1000 if (value != NULL && dtor->notify != NULL)
1002 /* POSIX says to clear this before the call */
1003 TlsSetValue (dtor->index, NULL);
1004 dtor->notify (value);
1005 dtors_called = TRUE;
1009 while (dtors_called);
1011 if (g_thread_impl_vtable.CallThisOnThreadExit)
1012 g_thread_impl_vtable.CallThisOnThreadExit ();
1015 /* vim:set foldmethod=marker: */