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->impl;
178 if G_UNLIKELY (mutex->impl == NULL)
180 impl = g_rec_mutex_impl_new ();
181 if (InterlockedCompareExchangePointer (&mutex->impl, impl, NULL) != NULL)
182 g_rec_mutex_impl_free (impl);
190 g_rec_mutex_init (GRecMutex *mutex)
192 mutex->impl = g_rec_mutex_impl_new ();
196 g_rec_mutex_clear (GRecMutex *mutex)
199 g_rec_mutex_impl_free (mutex->impl);
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->impl);
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_timedwait (GCond *cond,
306 GMutex *entered_mutex,
313 GetSystemTimeAsFileTime (&ft);
314 memmove (&now, &ft, sizeof (FILETIME));
316 now -= G_GINT64_CONSTANT (116444736000000000);
319 span = abs_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);
331 g_cond_timed_wait (GCond *cond,
332 GMutex *entered_mutex,
339 micros = abs_time->tv_sec;
341 micros += abs_time->tv_usec;
343 return g_cond_timedwait (cond, entered_mutex, micros);
347 g_cond_wait (cond, entered_mutex);
354 typedef struct _GPrivateDestructor GPrivateDestructor;
356 struct _GPrivateDestructor
359 GDestroyNotify notify;
360 GPrivateDestructor *next;
363 static GPrivateDestructor * volatile g_private_destructors;
364 static CRITICAL_SECTION g_private_lock;
367 g_private_get_impl (GPrivate *key)
369 DWORD impl = (DWORD) key->p;
371 if G_UNLIKELY (impl == 0)
373 EnterCriticalSection (&g_private_lock);
374 impl = (DWORD) key->p;
377 GPrivateDestructor *destructor;
381 if (impl == TLS_OUT_OF_INDEXES)
382 g_thread_abort (0, "TlsAlloc");
384 if (key->notify != NULL)
386 destructor = malloc (sizeof (GPrivateDestructor));
387 if G_UNLIKELY (destructor == NULL)
388 g_thread_abort (errno, "malloc");
389 destructor->index = impl;
390 destructor->notify = key->notify;
391 destructor->next = g_private_destructors;
393 /* We need to do an atomic store due to the unlocked
394 * access to the destructor list from the thread exit
397 * It can double as a sanity check...
399 if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
400 destructor->next) != destructor->next)
401 g_thread_abort (0, "g_private_get_impl(1)");
404 /* Ditto, due to the unlocked access on the fast path */
405 if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
406 g_thread_abort (0, "g_private_get_impl(2)");
408 LeaveCriticalSection (&g_private_lock);
415 g_private_get (GPrivate *key)
417 return TlsGetValue (g_private_get_impl (key));
421 g_private_set (GPrivate *key,
424 TlsSetValue (g_private_get_impl (key), value);
428 g_private_replace (GPrivate *key,
431 DWORD impl = g_private_get_impl (key);
434 old = TlsGetValue (impl);
435 if (old && key->notify)
437 TlsSetValue (impl, value);
443 #include "gthreadprivate.h"
445 #define win32_check_for_error(what) G_STMT_START{ \
447 g_error ("file %s: line %d (%s): error %s during %s", \
448 __FILE__, __LINE__, G_STRFUNC, \
449 g_win32_error_message (GetLastError ()), #what); \
452 #define G_MUTEX_SIZE (sizeof (gpointer))
454 static DWORD g_thread_self_tls;
456 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
458 typedef struct _GThreadData GThreadData;
468 g_system_thread_self (gpointer thread)
470 GThreadData *self = TlsGetValue (g_thread_self_tls);
474 /* This should only happen for the main thread! */
475 HANDLE handle = GetCurrentThread ();
476 HANDLE process = GetCurrentProcess ();
477 self = g_new (GThreadData, 1);
478 win32_check_for_error (DuplicateHandle (process, handle, process,
479 &self->thread, 0, FALSE,
480 DUPLICATE_SAME_ACCESS));
481 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
484 self->joinable = FALSE;
487 *(GThreadData **)thread = self;
491 g_system_thread_exit (void)
493 GThreadData *self = TlsGetValue (g_thread_self_tls);
494 gboolean dtors_called;
498 GPrivateDestructor *dtor;
500 /* We go by the POSIX book on this one.
502 * If we call a destructor then there is a chance that some new
503 * TLS variables got set by code called in that destructor.
505 * Loop until nothing is left.
507 dtors_called = FALSE;
509 for (dtor = g_private_destructors; dtor; dtor = dtor->next)
513 value = TlsGetValue (dtor->index);
514 if (value != NULL && dtor->notify != NULL)
516 /* POSIX says to clear this before the call */
517 TlsSetValue (dtor->index, NULL);
518 dtor->notify (value);
523 while (dtors_called);
529 win32_check_for_error (CloseHandle (self->thread));
532 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
535 if (g_thread_impl_vtable.CallThisOnThreadExit)
536 g_thread_impl_vtable.CallThisOnThreadExit ();
541 static guint __stdcall
542 g_thread_proxy (gpointer data)
544 GThreadData *self = (GThreadData*) data;
546 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
548 self->func (self->data);
550 g_system_thread_exit ();
552 g_assert_not_reached ();
558 g_system_thread_create (GThreadFunc func,
568 g_return_if_fail (func);
570 retval = g_new(GThreadData, 1);
574 retval->joinable = joinable;
576 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
579 if (retval->thread == NULL)
581 gchar *win_error = g_win32_error_message (GetLastError ());
582 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
583 "Error creating thread: %s", win_error);
589 *(GThreadData **)thread = retval;
593 g_thread_yield (void)
599 g_system_thread_join (gpointer thread)
601 GThreadData *target = *(GThreadData **)thread;
603 g_return_if_fail (target->joinable);
605 win32_check_for_error (WAIT_FAILED !=
606 WaitForSingleObject (target->thread, INFINITE));
608 win32_check_for_error (CloseHandle (target->thread));
613 g_system_thread_equal (gpointer thread1,
616 return ((GSystemThread*)thread1)->dummy_pointer == ((GSystemThread*)thread2)->dummy_pointer;
620 g_system_thread_set_name (const gchar *name)
622 /* FIXME: implement */
625 /* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
627 static CRITICAL_SECTION g_thread_xp_lock;
628 static DWORD g_thread_xp_waiter_tls;
630 /* {{{2 GThreadWaiter utility class for CONDITION_VARIABLE emulation */
631 typedef struct _GThreadXpWaiter GThreadXpWaiter;
632 struct _GThreadXpWaiter
635 volatile GThreadXpWaiter *next;
638 static GThreadXpWaiter *
639 g_thread_xp_waiter_get (void)
641 GThreadXpWaiter *waiter;
643 waiter = TlsGetValue (g_thread_xp_waiter_tls);
645 if G_UNLIKELY (waiter == NULL)
647 waiter = malloc (sizeof (GThreadXpWaiter));
649 g_thread_abort (GetLastError (), "malloc");
650 waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
651 if (waiter->event == NULL)
652 g_thread_abort (GetLastError (), "CreateEvent");
654 TlsSetValue (g_thread_xp_waiter_tls, waiter);
660 static void __stdcall
661 g_thread_xp_CallThisOnThreadExit (void)
663 GThreadXpWaiter *waiter;
665 waiter = TlsGetValue (g_thread_xp_waiter_tls);
669 TlsSetValue (g_thread_xp_waiter_tls, NULL);
670 CloseHandle (waiter->event);
675 /* {{{2 SRWLock emulation */
678 CRITICAL_SECTION writer_lock;
679 gboolean ever_shared; /* protected by writer_lock */
680 gboolean writer_locked; /* protected by writer_lock */
682 /* below is only ever touched if ever_shared becomes true */
683 CRITICAL_SECTION atomicity;
684 GThreadXpWaiter *queued_writer; /* protected by atomicity lock */
685 gint num_readers; /* protected by atomicity lock */
688 static void __stdcall
689 g_thread_xp_InitializeSRWLock (gpointer mutex)
691 *(GThreadSRWLock * volatile *) mutex = NULL;
694 static void __stdcall
695 g_thread_xp_DeleteSRWLock (gpointer mutex)
697 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
701 if (lock->ever_shared)
702 DeleteCriticalSection (&lock->atomicity);
704 DeleteCriticalSection (&lock->writer_lock);
709 static GThreadSRWLock * __stdcall
710 g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
712 GThreadSRWLock *result;
714 /* It looks like we're missing some barriers here, but this code only
715 * ever runs on Windows XP, which in turn only ever runs on hardware
716 * with a relatively rigid memory model. The 'volatile' will take
717 * care of the compiler.
721 if G_UNLIKELY (result == NULL)
723 EnterCriticalSection (&g_thread_xp_lock);
725 result = malloc (sizeof (GThreadSRWLock));
728 g_thread_abort (errno, "malloc");
730 InitializeCriticalSection (&result->writer_lock);
731 result->writer_locked = FALSE;
732 result->ever_shared = FALSE;
735 LeaveCriticalSection (&g_thread_xp_lock);
741 static void __stdcall
742 g_thread_xp_AcquireSRWLockExclusive (gpointer mutex)
744 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
746 EnterCriticalSection (&lock->writer_lock);
748 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
749 * Detect the deadlock that would occur on later Windows version.
751 g_assert (!lock->writer_locked);
752 lock->writer_locked = TRUE;
754 if (lock->ever_shared)
756 GThreadXpWaiter *waiter = NULL;
758 EnterCriticalSection (&lock->atomicity);
759 if (lock->num_readers > 0)
760 lock->queued_writer = waiter = g_thread_xp_waiter_get ();
761 LeaveCriticalSection (&lock->atomicity);
764 WaitForSingleObject (waiter->event, INFINITE);
766 lock->queued_writer = NULL;
770 static BOOLEAN __stdcall
771 g_thread_xp_TryAcquireSRWLockExclusive (gpointer mutex)
773 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
775 if (!TryEnterCriticalSection (&lock->writer_lock))
778 /* CRITICAL_SECTION is reentrant, but SRWLock is not.
779 * Ensure that this properly returns FALSE (as SRWLock would).
781 if G_UNLIKELY (lock->writer_locked)
783 LeaveCriticalSection (&lock->writer_lock);
787 lock->writer_locked = TRUE;
789 if (lock->ever_shared)
793 EnterCriticalSection (&lock->atomicity);
794 available = lock->num_readers == 0;
795 LeaveCriticalSection (&lock->atomicity);
799 LeaveCriticalSection (&lock->writer_lock);
807 static void __stdcall
808 g_thread_xp_ReleaseSRWLockExclusive (gpointer mutex)
810 GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
812 lock->writer_locked = FALSE;
814 /* We need this until we fix some weird parts of GLib that try to
815 * unlock freshly-allocated mutexes.
818 LeaveCriticalSection (&lock->writer_lock);
822 g_thread_xp_srwlock_become_reader (GThreadSRWLock *lock)
824 if G_UNLIKELY (!lock->ever_shared)
826 InitializeCriticalSection (&lock->atomicity);
827 lock->queued_writer = NULL;
828 lock->num_readers = 0;
830 lock->ever_shared = TRUE;
833 EnterCriticalSection (&lock->atomicity);
835 LeaveCriticalSection (&lock->atomicity);
838 static void __stdcall
839 g_thread_xp_AcquireSRWLockShared (gpointer mutex)
841 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
843 EnterCriticalSection (&lock->writer_lock);
845 /* See g_thread_xp_AcquireSRWLockExclusive */
846 g_assert (!lock->writer_locked);
848 g_thread_xp_srwlock_become_reader (lock);
850 LeaveCriticalSection (&lock->writer_lock);
853 static BOOLEAN __stdcall
854 g_thread_xp_TryAcquireSRWLockShared (gpointer mutex)
856 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
858 if (!TryEnterCriticalSection (&lock->writer_lock))
861 /* See g_thread_xp_AcquireSRWLockExclusive */
862 if G_UNLIKELY (lock->writer_locked)
864 LeaveCriticalSection (&lock->writer_lock);
868 g_thread_xp_srwlock_become_reader (lock);
870 LeaveCriticalSection (&lock->writer_lock);
875 static void __stdcall
876 g_thread_xp_ReleaseSRWLockShared (gpointer mutex)
878 GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
880 EnterCriticalSection (&lock->atomicity);
884 if (lock->num_readers == 0 && lock->queued_writer)
885 SetEvent (lock->queued_writer->event);
887 LeaveCriticalSection (&lock->atomicity);
890 /* {{{2 CONDITION_VARIABLE emulation */
893 volatile GThreadXpWaiter *first;
894 volatile GThreadXpWaiter **last_ptr;
895 } GThreadXpCONDITION_VARIABLE;
897 static void __stdcall
898 g_thread_xp_InitializeConditionVariable (gpointer cond)
900 *(GThreadXpCONDITION_VARIABLE * volatile *) cond = NULL;
903 static void __stdcall
904 g_thread_xp_DeleteConditionVariable (gpointer cond)
906 GThreadXpCONDITION_VARIABLE *cv = *(GThreadXpCONDITION_VARIABLE * volatile *) cond;
912 static GThreadXpCONDITION_VARIABLE * __stdcall
913 g_thread_xp_get_condition_variable (GThreadXpCONDITION_VARIABLE * volatile *cond)
915 GThreadXpCONDITION_VARIABLE *result;
917 /* It looks like we're missing some barriers here, but this code only
918 * ever runs on Windows XP, which in turn only ever runs on hardware
919 * with a relatively rigid memory model. The 'volatile' will take
920 * care of the compiler.
924 if G_UNLIKELY (result == NULL)
926 result = malloc (sizeof (GThreadXpCONDITION_VARIABLE));
929 g_thread_abort (errno, "malloc");
931 result->first = NULL;
932 result->last_ptr = &result->first;
934 if (InterlockedCompareExchangePointer (cond, result, NULL) != NULL)
944 static BOOL __stdcall
945 g_thread_xp_SleepConditionVariableSRW (gpointer cond,
950 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
951 GThreadXpWaiter *waiter = g_thread_xp_waiter_get ();
956 EnterCriticalSection (&g_thread_xp_lock);
957 *cv->last_ptr = waiter;
958 cv->last_ptr = &waiter->next;
959 LeaveCriticalSection (&g_thread_xp_lock);
961 g_mutex_unlock (mutex);
962 status = WaitForSingleObject (waiter->event, timeout);
964 if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
965 g_thread_abort (GetLastError (), "WaitForSingleObject");
967 g_mutex_lock (mutex);
969 return status == WAIT_OBJECT_0;
972 static void __stdcall
973 g_thread_xp_WakeConditionVariable (gpointer cond)
975 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
976 volatile GThreadXpWaiter *waiter;
978 EnterCriticalSection (&g_thread_xp_lock);
982 cv->first = waiter->next;
983 if (cv->first == NULL)
984 cv->last_ptr = &cv->first;
986 LeaveCriticalSection (&g_thread_xp_lock);
989 SetEvent (waiter->event);
992 static void __stdcall
993 g_thread_xp_WakeAllConditionVariable (gpointer cond)
995 GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
996 volatile GThreadXpWaiter *waiter;
998 EnterCriticalSection (&g_thread_xp_lock);
1001 cv->last_ptr = &cv->first;
1002 LeaveCriticalSection (&g_thread_xp_lock);
1004 while (waiter != NULL)
1006 volatile GThreadXpWaiter *next;
1008 next = waiter->next;
1009 SetEvent (waiter->event);
1016 g_thread_xp_init (void)
1018 static const GThreadImplVtable g_thread_xp_impl_vtable = {
1019 g_thread_xp_CallThisOnThreadExit,
1020 g_thread_xp_InitializeSRWLock,
1021 g_thread_xp_DeleteSRWLock,
1022 g_thread_xp_AcquireSRWLockExclusive,
1023 g_thread_xp_TryAcquireSRWLockExclusive,
1024 g_thread_xp_ReleaseSRWLockExclusive,
1025 g_thread_xp_AcquireSRWLockShared,
1026 g_thread_xp_TryAcquireSRWLockShared,
1027 g_thread_xp_ReleaseSRWLockShared,
1028 g_thread_xp_InitializeConditionVariable,
1029 g_thread_xp_DeleteConditionVariable,
1030 g_thread_xp_SleepConditionVariableSRW,
1031 g_thread_xp_WakeAllConditionVariable,
1032 g_thread_xp_WakeConditionVariable
1035 InitializeCriticalSection (&g_thread_xp_lock);
1036 g_thread_xp_waiter_tls = TlsAlloc ();
1038 g_thread_impl_vtable = g_thread_xp_impl_vtable;
1044 g_thread_lookup_native_funcs (void)
1046 GThreadImplVtable native_vtable = { 0, };
1049 kernel32 = GetModuleHandle ("KERNEL32.DLL");
1051 if (kernel32 == NULL)
1054 #define GET_FUNC(name) if ((native_vtable.name = (void *) GetProcAddress (kernel32, #name)) == NULL) return FALSE
1055 GET_FUNC(InitializeSRWLock);
1056 GET_FUNC(AcquireSRWLockExclusive);
1057 GET_FUNC(TryAcquireSRWLockExclusive);
1058 GET_FUNC(ReleaseSRWLockExclusive);
1059 GET_FUNC(AcquireSRWLockShared);
1060 GET_FUNC(TryAcquireSRWLockShared);
1061 GET_FUNC(ReleaseSRWLockShared);
1063 GET_FUNC(InitializeConditionVariable);
1064 GET_FUNC(SleepConditionVariableSRW);
1065 GET_FUNC(WakeAllConditionVariable);
1066 GET_FUNC(WakeConditionVariable);
1069 g_thread_impl_vtable = native_vtable;
1074 G_GNUC_INTERNAL void
1075 g_thread_DllMain (void)
1077 if (g_thread_lookup_native_funcs ())
1078 fprintf (stderr, "(debug) GThread using native mode\n");
1081 fprintf (stderr, "(debug) GThread using Windows XP mode\n");
1082 g_thread_xp_init ();
1085 win32_check_for_error (TLS_OUT_OF_INDEXES != (g_thread_self_tls = TlsAlloc ()));
1086 InitializeCriticalSection (&g_private_lock);
1089 /* vim:set foldmethod=marker: */