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. This allows us to use them directly and still support:
76 * GMutex mutex = G_MUTEX_INIT;
80 * GCond cond = G_COND_INIT;
82 * Unfortunately, Windows XP lacks these facilities and GLib still
83 * needs to support Windows XP. Our approach here is as follows:
85 * - avoid depending on structure declarations at compile-time by
86 * declaring our own GMutex and GCond strutures to be
87 * ABI-compatible with SRWLock and CONDITION_VARIABLE and using
90 * - avoid a hard dependency on the symbols used to manipulate these
91 * structures by doing a dynamic lookup of those symbols at
94 * - if the symbols are not available, emulate them using other
97 * Using this approach also allows us to easily build a GLib that lacks
98 * support for Windows XP or to remove this code entirely when XP is no
99 * longer supported (end of line is currently April 8, 2014).
103 void (__stdcall * CallThisOnThreadExit) (void); /* fake */
105 void (__stdcall * InitializeSRWLock) (gpointer lock);
106 void (__stdcall * DeleteSRWLock) (gpointer lock); /* fake */
107 void (__stdcall * AcquireSRWLockExclusive) (gpointer lock);
108 BOOLEAN (__stdcall * TryAcquireSRWLockExclusive) (gpointer lock);
109 void (__stdcall * ReleaseSRWLockExclusive) (gpointer lock);
110 void (__stdcall * AcquireSRWLockShared) (gpointer lock);
111 BOOLEAN (__stdcall * TryAcquireSRWLockShared) (gpointer lock);
112 void (__stdcall * ReleaseSRWLockShared) (gpointer lock);
114 void (__stdcall * InitializeConditionVariable) (gpointer cond);
115 void (__stdcall * DeleteConditionVariable) (gpointer cond); /* fake */
116 BOOL (__stdcall * SleepConditionVariableSRW) (gpointer cond,
120 void (__stdcall * WakeAllConditionVariable) (gpointer cond);
121 void (__stdcall * WakeConditionVariable) (gpointer cond);
124 static GThreadImplVtable g_thread_impl_vtable;
128 g_mutex_init (GMutex *mutex)
130 g_thread_impl_vtable.InitializeSRWLock (mutex);
134 g_mutex_clear (GMutex *mutex)
136 if (g_thread_impl_vtable.DeleteSRWLock != NULL)
137 g_thread_impl_vtable.DeleteSRWLock (mutex);
141 g_mutex_lock (GMutex *mutex)
143 g_thread_impl_vtable.AcquireSRWLockExclusive (mutex);
147 g_mutex_trylock (GMutex *mutex)
149 return g_thread_impl_vtable.TryAcquireSRWLockExclusive (mutex);
153 g_mutex_unlock (GMutex *mutex)
155 g_thread_impl_vtable.ReleaseSRWLockExclusive (mutex);
160 static CRITICAL_SECTION *
161 g_rec_mutex_impl_new (void)
163 CRITICAL_SECTION *cs;
165 cs = g_slice_new (CRITICAL_SECTION);
166 InitializeCriticalSection (cs);
172 g_rec_mutex_impl_free (CRITICAL_SECTION *cs)
174 DeleteCriticalSection (cs);
175 g_slice_free (CRITICAL_SECTION, cs);
178 static CRITICAL_SECTION *
179 g_rec_mutex_get_impl (GRecMutex *mutex)
181 CRITICAL_SECTION *impl = mutex->impl;
183 if G_UNLIKELY (mutex->impl == NULL)
185 impl = g_rec_mutex_impl_new ();
186 if (InterlockedCompareExchangePointer (&mutex->impl, impl, NULL) != NULL)
187 g_rec_mutex_impl_free (impl);
195 g_rec_mutex_init (GRecMutex *mutex)
197 mutex->impl = g_rec_mutex_impl_new ();
201 g_rec_mutex_clear (GRecMutex *mutex)
204 g_rec_mutex_impl_free (mutex->impl);
208 g_rec_mutex_lock (GRecMutex *mutex)
210 EnterCriticalSection (g_rec_mutex_get_impl (mutex));
214 g_rec_mutex_unlock (GRecMutex *mutex)
216 LeaveCriticalSection (mutex->impl);
220 g_rec_mutex_trylock (GRecMutex *mutex)
222 return TryEnterCriticalSection (g_rec_mutex_get_impl (mutex));
228 g_rw_lock_init (GRWLock *lock)
230 g_thread_impl_vtable.InitializeSRWLock (lock);
234 g_rw_lock_clear (GRWLock *lock)
236 if (g_thread_impl_vtable.DeleteSRWLock != NULL)
237 g_thread_impl_vtable.DeleteSRWLock (lock);
241 g_rw_lock_writer_lock (GRWLock *lock)
243 g_thread_impl_vtable.AcquireSRWLockExclusive (lock);
247 g_rw_lock_writer_trylock (GRWLock *lock)
249 return g_thread_impl_vtable.TryAcquireSRWLockExclusive (lock);
253 g_rw_lock_writer_unlock (GRWLock *lock)
255 g_thread_impl_vtable.ReleaseSRWLockExclusive (lock);
259 g_rw_lock_reader_lock (GRWLock *lock)
261 g_thread_impl_vtable.AcquireSRWLockShared (lock);
265 g_rw_lock_reader_trylock (GRWLock *lock)
267 return g_thread_impl_vtable.TryAcquireSRWLockShared (lock);
271 g_rw_lock_reader_unlock (GRWLock *lock)
273 g_thread_impl_vtable.ReleaseSRWLockShared (lock);
278 g_cond_init (GCond *cond)
280 g_thread_impl_vtable.InitializeConditionVariable (cond);
284 g_cond_clear (GCond *cond)
286 if (g_thread_impl_vtable.DeleteConditionVariable)
287 g_thread_impl_vtable.DeleteConditionVariable (cond);
291 g_cond_signal (GCond *cond)
293 g_thread_impl_vtable.WakeConditionVariable (cond);
297 g_cond_broadcast (GCond *cond)
299 g_thread_impl_vtable.WakeAllConditionVariable (cond);
303 g_cond_wait (GCond *cond,
304 GMutex *entered_mutex)
306 g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, INFINITE, 0);
310 g_cond_timedwait (GCond *cond,
311 GMutex *entered_mutex,
318 GetSystemTimeAsFileTime (&ft);
319 memmove (&now, &ft, sizeof (FILETIME));
321 now -= G_GINT64_CONSTANT (116444736000000000);
324 span = abs_time - now;
326 if G_UNLIKELY (span < 0)
329 if G_UNLIKELY (span > G_GINT64_CONSTANT (1000) * G_MAXINT32)
332 return g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, span / 1000, 0);
336 g_cond_timed_wait (GCond *cond,
337 GMutex *entered_mutex,
344 micros = abs_time->tv_sec;
346 micros += abs_time->tv_usec;
348 return g_cond_timedwait (cond, entered_mutex, micros);
352 g_cond_wait (cond, entered_mutex);
359 typedef struct _GPrivateDestructor GPrivateDestructor;
361 struct _GPrivateDestructor
364 GDestroyNotify notify;
365 GPrivateDestructor *next;
368 static GPrivateDestructor * volatile g_private_destructors;
369 static CRITICAL_SECTION g_private_lock;
372 g_private_get_impl (GPrivate *key)
374 DWORD impl = (DWORD) key->p;
376 if G_UNLIKELY (impl == 0)
378 EnterCriticalSection (&g_private_lock);
379 impl = (DWORD) key->p;
382 GPrivateDestructor *destructor;
386 if (impl == TLS_OUT_OF_INDEXES)
387 g_thread_abort (0, "TlsAlloc");
389 if (key->notify != NULL)
391 destructor = malloc (sizeof (GPrivateDestructor));
392 if G_UNLIKELY (destructor == NULL)
393 g_thread_abort (errno, "malloc");
394 destructor->index = impl;
395 destructor->notify = key->notify;
396 destructor->next = g_private_destructors;
398 /* We need to do an atomic store due to the unlocked
399 * access to the destructor list from the thread exit
402 * It can double as a sanity check...
404 if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
405 destructor->next) != destructor->next)
406 g_thread_abort (0, "g_private_get_impl(1)");
409 /* Ditto, due to the unlocked access on the fast path */
410 if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
411 g_thread_abort (0, "g_private_get_impl(2)");
413 LeaveCriticalSection (&g_private_lock);
420 g_private_get (GPrivate *key)
422 return TlsGetValue (g_private_get_impl (key));
426 g_private_set (GPrivate *key,
429 TlsSetValue (g_private_get_impl (key), value);
433 g_private_replace (GPrivate *key,
436 DWORD impl = g_private_get_impl (key);
439 old = TlsGetValue (impl);
440 if (old && key->notify)
442 TlsSetValue (impl, value);
448 #include "gthreadprivate.h"
450 #define win32_check_for_error(what) G_STMT_START{ \
452 g_error ("file %s: line %d (%s): error %s during %s", \
453 __FILE__, __LINE__, G_STRFUNC, \
454 g_win32_error_message (GetLastError ()), #what); \
457 #define G_MUTEX_SIZE (sizeof (gpointer))
459 static DWORD g_thread_self_tls;
461 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
463 typedef struct _GThreadData GThreadData;
473 g_system_thread_self (gpointer thread)
475 GThreadData *self = TlsGetValue (g_thread_self_tls);
479 /* This should only happen for the main thread! */
480 HANDLE handle = GetCurrentThread ();
481 HANDLE process = GetCurrentProcess ();
482 self = g_new (GThreadData, 1);
483 win32_check_for_error (DuplicateHandle (process, handle, process,
484 &self->thread, 0, FALSE,
485 DUPLICATE_SAME_ACCESS));
486 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
489 self->joinable = FALSE;
492 *(GThreadData **)thread = self;
496 g_system_thread_exit (void)
498 GThreadData *self = TlsGetValue (g_thread_self_tls);
499 gboolean dtors_called;
503 GPrivateDestructor *dtor;
505 /* We go by the POSIX book on this one.
507 * If we call a destructor then there is a chance that some new
508 * TLS variables got set by code called in that destructor.
510 * Loop until nothing is left.
512 dtors_called = FALSE;
514 for (dtor = g_private_destructors; dtor; dtor = dtor->next)
518 value = TlsGetValue (dtor->index);
519 if (value != NULL && dtor->notify != NULL)
521 /* POSIX says to clear this before the call */
522 TlsSetValue (dtor->index, NULL);
523 dtor->notify (value);
528 while (dtors_called);
534 win32_check_for_error (CloseHandle (self->thread));
537 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
540 if (g_thread_impl_vtable.CallThisOnThreadExit)
541 g_thread_impl_vtable.CallThisOnThreadExit ();
546 static guint __stdcall
547 g_thread_proxy (gpointer data)
549 GThreadData *self = (GThreadData*) data;
551 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
553 self->func (self->data);
555 g_system_thread_exit ();
557 g_assert_not_reached ();
563 g_system_thread_create (GThreadFunc func,
573 g_return_if_fail (func);
575 retval = g_new(GThreadData, 1);
579 retval->joinable = joinable;
581 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
584 if (retval->thread == NULL)
586 gchar *win_error = g_win32_error_message (GetLastError ());
587 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
588 "Error creating thread: %s", win_error);
594 *(GThreadData **)thread = retval;
598 g_thread_yield (void)
604 g_system_thread_join (gpointer thread)
606 GThreadData *target = *(GThreadData **)thread;
608 g_return_if_fail (target->joinable);
610 win32_check_for_error (WAIT_FAILED !=
611 WaitForSingleObject (target->thread, INFINITE));
613 win32_check_for_error (CloseHandle (target->thread));
618 g_system_thread_equal (gpointer thread1,
621 return ((GSystemThread*)thread1)->dummy_pointer == ((GSystemThread*)thread2)->dummy_pointer;
625 g_system_thread_set_name (const gchar *name)
627 /* FIXME: implement */
630 /* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
632 static CRITICAL_SECTION g_thread_xp_lock;
633 static DWORD g_thread_xp_waiter_tls;
635 /* {{{2 GThreadWaiter utility class for CONDITION_VARIABLE emulation */
636 typedef struct _GThreadXpWaiter GThreadXpWaiter;
637 struct _GThreadXpWaiter
640 volatile GThreadXpWaiter *next;
643 static GThreadXpWaiter *
644 g_thread_xp_waiter_get (void)
646 GThreadXpWaiter *waiter;
648 waiter = TlsGetValue (g_thread_xp_waiter_tls);
650 if G_UNLIKELY (waiter == NULL)
652 waiter = malloc (sizeof (GThreadXpWaiter));
654 g_thread_abort (GetLastError (), "malloc");
655 waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
656 if (waiter->event == NULL)
657 g_thread_abort (GetLastError (), "CreateEvent");
659 TlsSetValue (g_thread_xp_waiter_tls, waiter);
665 static void __stdcall
666 g_thread_xp_CallThisOnThreadExit (void)
668 GThreadXpWaiter *waiter;
670 waiter = TlsGetValue (g_thread_xp_waiter_tls);
674 TlsSetValue (g_thread_xp_waiter_tls, NULL);
675 CloseHandle (waiter->event);
680 /* {{{2 SRWLock emulation */
683 CRITICAL_SECTION writer_lock;
684 gboolean ever_shared; /* protected by writer_lock */
685 gboolean writer_locked; /* protected by writer_lock */
687 /* below is only ever touched if ever_shared becomes true */
688 CRITICAL_SECTION atomicity;
689 GThreadXpWaiter *queued_writer; /* protected by atomicity lock */
690 gint num_readers; /* protected by atomicity lock */
693 static void __stdcall
694 g_thread_xp_InitializeSRWLock (gpointer mutex)
696 *(GThreadSRWLock * volatile *) mutex = NULL;
699 static void __stdcall
700 g_thread_xp_DeleteSRWLock (gpointer mutex)
702 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
706 if (lock->ever_shared)
707 DeleteCriticalSection (&lock->atomicity);
709 DeleteCriticalSection (&lock->writer_lock);
714 static GThreadSRWLock * __stdcall
715 g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
717 GThreadSRWLock *result;
719 /* It looks like we're missing some barriers here, but this code only
720 * ever runs on Windows XP, which in turn only ever runs on hardware
721 * with a relatively rigid memory model. The 'volatile' will take
722 * care of the compiler.
726 if G_UNLIKELY (result == NULL)
728 EnterCriticalSection (&g_thread_xp_lock);
730 result = malloc (sizeof (GThreadSRWLock));
733 g_thread_abort (errno, "malloc");
735 InitializeCriticalSection (&result->writer_lock);
736 result->writer_locked = FALSE;
737 result->ever_shared = FALSE;
740 LeaveCriticalSection (&g_thread_xp_lock);
746 static void __stdcall
747 g_thread_xp_AcquireSRWLockExclusive (gpointer mutex)
749 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
751 EnterCriticalSection (&lock->writer_lock);
753 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
754 * Detect the deadlock that would occur on later Windows version.
756 g_assert (!lock->writer_locked);
757 lock->writer_locked = TRUE;
759 if (lock->ever_shared)
761 GThreadXpWaiter *waiter = NULL;
763 EnterCriticalSection (&lock->atomicity);
764 if (lock->num_readers > 0)
765 lock->queued_writer = waiter = g_thread_xp_waiter_get ();
766 LeaveCriticalSection (&lock->atomicity);
769 WaitForSingleObject (waiter->event, INFINITE);
771 lock->queued_writer = NULL;
775 static BOOLEAN __stdcall
776 g_thread_xp_TryAcquireSRWLockExclusive (gpointer mutex)
778 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
780 if (!TryEnterCriticalSection (&lock->writer_lock))
783 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
784 * Ensure that this properly returns FALSE (as SRWLock would).
786 if G_UNLIKELY (lock->writer_locked)
788 LeaveCriticalSection (&lock->writer_lock);
792 lock->writer_locked = TRUE;
794 if (lock->ever_shared)
798 EnterCriticalSection (&lock->atomicity);
799 available = lock->num_readers == 0;
800 LeaveCriticalSection (&lock->atomicity);
804 LeaveCriticalSection (&lock->writer_lock);
812 static void __stdcall
813 g_thread_xp_ReleaseSRWLockExclusive (gpointer mutex)
815 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
817 lock->writer_locked = FALSE;
819 /* We need this until we fix some weird parts of GLib that try to
820 * unlock freshly-allocated mutexes.
823 LeaveCriticalSection (&lock->writer_lock);
827 g_thread_xp_srwlock_become_reader (GThreadSRWLock *lock)
829 if G_UNLIKELY (!lock->ever_shared)
831 InitializeCriticalSection (&lock->atomicity);
832 lock->queued_writer = NULL;
833 lock->num_readers = 0;
835 lock->ever_shared = TRUE;
838 EnterCriticalSection (&lock->atomicity);
840 LeaveCriticalSection (&lock->atomicity);
843 static void __stdcall
844 g_thread_xp_AcquireSRWLockShared (gpointer mutex)
846 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
848 EnterCriticalSection (&lock->writer_lock);
850 /* See g_thread_xp_AcquireSRWLockExclusive */
851 g_assert (!lock->writer_locked);
853 g_thread_xp_srwlock_become_reader (lock);
855 LeaveCriticalSection (&lock->writer_lock);
858 static BOOLEAN __stdcall
859 g_thread_xp_TryAcquireSRWLockShared (gpointer mutex)
861 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
863 if (!TryEnterCriticalSection (&lock->writer_lock))
866 /* See g_thread_xp_AcquireSRWLockExclusive */
867 if G_UNLIKELY (lock->writer_locked)
869 LeaveCriticalSection (&lock->writer_lock);
873 g_thread_xp_srwlock_become_reader (lock);
875 LeaveCriticalSection (&lock->writer_lock);
880 static void __stdcall
881 g_thread_xp_ReleaseSRWLockShared (gpointer mutex)
883 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
885 EnterCriticalSection (&lock->atomicity);
889 if (lock->num_readers == 0 && lock->queued_writer)
890 SetEvent (lock->queued_writer->event);
892 LeaveCriticalSection (&lock->atomicity);
895 /* {{{2 CONDITION_VARIABLE emulation */
898 volatile GThreadXpWaiter *first;
899 volatile GThreadXpWaiter **last_ptr;
900 } GThreadXpCONDITION_VARIABLE;
902 static void __stdcall
903 g_thread_xp_InitializeConditionVariable (gpointer cond)
905 *(GThreadXpCONDITION_VARIABLE * volatile *) cond = NULL;
908 static void __stdcall
909 g_thread_xp_DeleteConditionVariable (gpointer cond)
911 GThreadXpCONDITION_VARIABLE *cv = *(GThreadXpCONDITION_VARIABLE * volatile *) cond;
917 static GThreadXpCONDITION_VARIABLE * __stdcall
918 g_thread_xp_get_condition_variable (GThreadXpCONDITION_VARIABLE * volatile *cond)
920 GThreadXpCONDITION_VARIABLE *result;
922 /* It looks like we're missing some barriers here, but this code only
923 * ever runs on Windows XP, which in turn only ever runs on hardware
924 * with a relatively rigid memory model. The 'volatile' will take
925 * care of the compiler.
929 if G_UNLIKELY (result == NULL)
931 result = malloc (sizeof (GThreadXpCONDITION_VARIABLE));
934 g_thread_abort (errno, "malloc");
936 result->first = NULL;
937 result->last_ptr = &result->first;
939 if (InterlockedCompareExchangePointer (cond, result, NULL) != NULL)
949 static BOOL __stdcall
950 g_thread_xp_SleepConditionVariableSRW (gpointer cond,
955 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
956 GThreadXpWaiter *waiter = g_thread_xp_waiter_get ();
961 EnterCriticalSection (&g_thread_xp_lock);
962 *cv->last_ptr = waiter;
963 cv->last_ptr = &waiter->next;
964 LeaveCriticalSection (&g_thread_xp_lock);
966 g_mutex_unlock (mutex);
967 status = WaitForSingleObject (waiter->event, timeout);
969 if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
970 g_thread_abort (GetLastError (), "WaitForSingleObject");
972 g_mutex_lock (mutex);
974 return status == WAIT_OBJECT_0;
977 static void __stdcall
978 g_thread_xp_WakeConditionVariable (gpointer cond)
980 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
981 volatile GThreadXpWaiter *waiter;
983 EnterCriticalSection (&g_thread_xp_lock);
987 cv->first = waiter->next;
988 if (cv->first == NULL)
989 cv->last_ptr = &cv->first;
991 LeaveCriticalSection (&g_thread_xp_lock);
994 SetEvent (waiter->event);
997 static void __stdcall
998 g_thread_xp_WakeAllConditionVariable (gpointer cond)
1000 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
1001 volatile GThreadXpWaiter *waiter;
1003 EnterCriticalSection (&g_thread_xp_lock);
1006 cv->last_ptr = &cv->first;
1007 LeaveCriticalSection (&g_thread_xp_lock);
1009 while (waiter != NULL)
1011 volatile GThreadXpWaiter *next;
1013 next = waiter->next;
1014 SetEvent (waiter->event);
1021 g_thread_xp_init (void)
1023 static const GThreadImplVtable g_thread_xp_impl_vtable = {
1024 g_thread_xp_CallThisOnThreadExit,
1025 g_thread_xp_InitializeSRWLock,
1026 g_thread_xp_DeleteSRWLock,
1027 g_thread_xp_AcquireSRWLockExclusive,
1028 g_thread_xp_TryAcquireSRWLockExclusive,
1029 g_thread_xp_ReleaseSRWLockExclusive,
1030 g_thread_xp_AcquireSRWLockShared,
1031 g_thread_xp_TryAcquireSRWLockShared,
1032 g_thread_xp_ReleaseSRWLockShared,
1033 g_thread_xp_InitializeConditionVariable,
1034 g_thread_xp_DeleteConditionVariable,
1035 g_thread_xp_SleepConditionVariableSRW,
1036 g_thread_xp_WakeAllConditionVariable,
1037 g_thread_xp_WakeConditionVariable
1040 InitializeCriticalSection (&g_thread_xp_lock);
1041 g_thread_xp_waiter_tls = TlsAlloc ();
1043 g_thread_impl_vtable = g_thread_xp_impl_vtable;
1049 g_thread_lookup_native_funcs (void)
1051 GThreadImplVtable native_vtable = { 0, };
1054 kernel32 = GetModuleHandle ("KERNEL32.DLL");
1056 if (kernel32 == NULL)
1059 #define GET_FUNC(name) if ((native_vtable.name = (void *) GetProcAddress (kernel32, #name)) == NULL) return FALSE
1060 GET_FUNC(InitializeSRWLock);
1061 GET_FUNC(AcquireSRWLockExclusive);
1062 GET_FUNC(TryAcquireSRWLockExclusive);
1063 GET_FUNC(ReleaseSRWLockExclusive);
1064 GET_FUNC(AcquireSRWLockShared);
1065 GET_FUNC(TryAcquireSRWLockShared);
1066 GET_FUNC(ReleaseSRWLockShared);
1068 GET_FUNC(InitializeConditionVariable);
1069 GET_FUNC(SleepConditionVariableSRW);
1070 GET_FUNC(WakeAllConditionVariable);
1071 GET_FUNC(WakeConditionVariable);
1074 g_thread_impl_vtable = native_vtable;
1079 G_GNUC_INTERNAL void
1080 g_thread_DllMain (void)
1082 if (g_thread_lookup_native_funcs ())
1083 fprintf (stderr, "(debug) GThread using native mode\n");
1086 fprintf (stderr, "(debug) GThread using Windows XP mode\n");
1087 g_thread_xp_init ();
1090 win32_check_for_error (TLS_OUT_OF_INDEXES != (g_thread_self_tls = TlsAlloc ()));
1091 InitializeCriticalSection (&g_private_lock);
1094 /* vim:set foldmethod=marker: */