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.1 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, see <http://www.gnu.org/licenses/>.
23 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
24 * file for a list of people on the GLib Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GLib at ftp://ftp.gtk.org/pub/gtk/.
29 /* The GMutex and GCond implementations in this file are some of the
30 * lowest-level code in GLib. All other parts of GLib (messages,
31 * memory, slices, etc) assume that they can freely use these facilities
32 * without risking recursion.
34 * As such, these functions are NOT permitted to call any other part of
37 * The thread manipulation functions (create, exit, join, etc.) have
38 * more freedom -- they can do as they please.
44 #include "glib-init.h"
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.
80 g_mutex_init (GMutex
*mutex
)
82 InitializeSRWLock ((gpointer
) mutex
);
86 g_mutex_clear (GMutex
*mutex
)
91 g_mutex_lock (GMutex
*mutex
)
93 AcquireSRWLockExclusive ((gpointer
) mutex
);
97 g_mutex_trylock (GMutex
*mutex
)
99 return TryAcquireSRWLockExclusive ((gpointer
) mutex
);
103 g_mutex_unlock (GMutex
*mutex
)
105 ReleaseSRWLockExclusive ((gpointer
) mutex
);
110 static CRITICAL_SECTION
*
111 g_rec_mutex_impl_new (void)
113 CRITICAL_SECTION
*cs
;
115 cs
= g_slice_new (CRITICAL_SECTION
);
116 InitializeCriticalSection (cs
);
122 g_rec_mutex_impl_free (CRITICAL_SECTION
*cs
)
124 DeleteCriticalSection (cs
);
125 g_slice_free (CRITICAL_SECTION
, cs
);
128 static CRITICAL_SECTION
*
129 g_rec_mutex_get_impl (GRecMutex
*mutex
)
131 CRITICAL_SECTION
*impl
= mutex
->p
;
133 if G_UNLIKELY (mutex
->p
== NULL
)
135 impl
= g_rec_mutex_impl_new ();
136 if (InterlockedCompareExchangePointer (&mutex
->p
, impl
, NULL
) != NULL
)
137 g_rec_mutex_impl_free (impl
);
145 g_rec_mutex_init (GRecMutex
*mutex
)
147 mutex
->p
= g_rec_mutex_impl_new ();
151 g_rec_mutex_clear (GRecMutex
*mutex
)
153 g_rec_mutex_impl_free (mutex
->p
);
157 g_rec_mutex_lock (GRecMutex
*mutex
)
159 EnterCriticalSection (g_rec_mutex_get_impl (mutex
));
163 g_rec_mutex_unlock (GRecMutex
*mutex
)
165 LeaveCriticalSection (mutex
->p
);
169 g_rec_mutex_trylock (GRecMutex
*mutex
)
171 return TryEnterCriticalSection (g_rec_mutex_get_impl (mutex
));
177 g_rw_lock_init (GRWLock
*lock
)
179 InitializeSRWLock ((gpointer
) lock
);
183 g_rw_lock_clear (GRWLock
*lock
)
188 g_rw_lock_writer_lock (GRWLock
*lock
)
190 AcquireSRWLockExclusive ((gpointer
) lock
);
194 g_rw_lock_writer_trylock (GRWLock
*lock
)
196 return TryAcquireSRWLockExclusive ((gpointer
) lock
);
200 g_rw_lock_writer_unlock (GRWLock
*lock
)
202 ReleaseSRWLockExclusive ((gpointer
) lock
);
206 g_rw_lock_reader_lock (GRWLock
*lock
)
208 AcquireSRWLockShared ((gpointer
) lock
);
212 g_rw_lock_reader_trylock (GRWLock
*lock
)
214 return TryAcquireSRWLockShared ((gpointer
) lock
);
218 g_rw_lock_reader_unlock (GRWLock
*lock
)
220 ReleaseSRWLockShared ((gpointer
) lock
);
225 g_cond_init (GCond
*cond
)
227 InitializeConditionVariable ((gpointer
) cond
);
231 g_cond_clear (GCond
*cond
)
236 g_cond_signal (GCond
*cond
)
238 WakeConditionVariable ((gpointer
) cond
);
242 g_cond_broadcast (GCond
*cond
)
244 WakeAllConditionVariable ((gpointer
) cond
);
248 g_cond_wait (GCond
*cond
,
249 GMutex
*entered_mutex
)
251 SleepConditionVariableSRW ((gpointer
) cond
, (gpointer
) entered_mutex
, INFINITE
, 0);
255 g_cond_wait_until (GCond
*cond
,
256 GMutex
*entered_mutex
,
259 gint64 span
, start_time
;
263 start_time
= g_get_monotonic_time ();
266 span
= end_time
- start_time
;
268 if G_UNLIKELY (span
< 0)
270 else if G_UNLIKELY (span
> G_GINT64_CONSTANT (1000) * (DWORD
) INFINITE
)
271 span_millis
= INFINITE
;
273 /* Round up so we don't time out too early */
274 span_millis
= (span
+ 1000 - 1) / 1000;
276 /* We never want to wait infinitely */
277 if (span_millis
>= INFINITE
)
278 span_millis
= INFINITE
- 1;
280 signalled
= SleepConditionVariableSRW ((gpointer
) cond
, (gpointer
) entered_mutex
, span_millis
, 0);
284 /* In case we didn't wait long enough after a timeout, wait again for the
286 start_time
= g_get_monotonic_time ();
288 while (start_time
< end_time
);
295 typedef struct _GPrivateDestructor GPrivateDestructor
;
297 struct _GPrivateDestructor
300 GDestroyNotify notify
;
301 GPrivateDestructor
*next
;
304 static GPrivateDestructor
* volatile g_private_destructors
;
305 static CRITICAL_SECTION g_private_lock
;
308 g_private_get_impl (GPrivate
*key
)
310 DWORD impl
= (DWORD
) key
->p
;
312 if G_UNLIKELY (impl
== 0)
314 EnterCriticalSection (&g_private_lock
);
315 impl
= (DWORD
) key
->p
;
318 GPrivateDestructor
*destructor
;
322 if (impl
== TLS_OUT_OF_INDEXES
)
323 g_thread_abort (0, "TlsAlloc");
325 if (key
->notify
!= NULL
)
327 destructor
= malloc (sizeof (GPrivateDestructor
));
328 if G_UNLIKELY (destructor
== NULL
)
329 g_thread_abort (errno
, "malloc");
330 destructor
->index
= impl
;
331 destructor
->notify
= key
->notify
;
332 destructor
->next
= g_private_destructors
;
334 /* We need to do an atomic store due to the unlocked
335 * access to the destructor list from the thread exit
338 * It can double as a sanity check...
340 if (InterlockedCompareExchangePointer (&g_private_destructors
, destructor
,
341 destructor
->next
) != destructor
->next
)
342 g_thread_abort (0, "g_private_get_impl(1)");
345 /* Ditto, due to the unlocked access on the fast path */
346 if (InterlockedCompareExchangePointer (&key
->p
, impl
, NULL
) != NULL
)
347 g_thread_abort (0, "g_private_get_impl(2)");
349 LeaveCriticalSection (&g_private_lock
);
356 g_private_get (GPrivate
*key
)
358 return TlsGetValue (g_private_get_impl (key
));
362 g_private_set (GPrivate
*key
,
365 TlsSetValue (g_private_get_impl (key
), value
);
369 g_private_replace (GPrivate
*key
,
372 DWORD impl
= g_private_get_impl (key
);
375 old
= TlsGetValue (impl
);
376 if (old
&& key
->notify
)
378 TlsSetValue (impl
, value
);
383 #define win32_check_for_error(what) G_STMT_START{ \
385 g_error ("file %s: line %d (%s): error %s during %s", \
386 __FILE__, __LINE__, G_STRFUNC, \
387 g_win32_error_message (GetLastError ()), #what); \
390 #define G_MUTEX_SIZE (sizeof (gpointer))
392 typedef BOOL (__stdcall
*GTryEnterCriticalSectionFunc
) (CRITICAL_SECTION
*);
403 g_system_thread_free (GRealThread
*thread
)
405 GThreadWin32
*wt
= (GThreadWin32
*) thread
;
407 win32_check_for_error (CloseHandle (wt
->handle
));
408 g_slice_free (GThreadWin32
, wt
);
412 g_system_thread_exit (void)
417 static guint __stdcall
418 g_thread_win32_proxy (gpointer data
)
420 GThreadWin32
*self
= data
;
424 g_system_thread_exit ();
426 g_assert_not_reached ();
432 g_system_thread_new (GThreadFunc func
,
436 GThreadWin32
*thread
;
439 thread
= g_slice_new0 (GThreadWin32
);
440 thread
->proxy
= func
;
442 thread
->handle
= (HANDLE
) _beginthreadex (NULL
, stack_size
, g_thread_win32_proxy
, thread
, 0, &ignore
);
444 if (thread
->handle
== NULL
)
446 gchar
*win_error
= g_win32_error_message (GetLastError ());
447 g_set_error (error
, G_THREAD_ERROR
, G_THREAD_ERROR_AGAIN
,
448 "Error creating thread: %s", win_error
);
450 g_slice_free (GThreadWin32
, thread
);
454 return (GRealThread
*) thread
;
458 g_thread_yield (void)
464 g_system_thread_wait (GRealThread
*thread
)
466 GThreadWin32
*wt
= (GThreadWin32
*) thread
;
468 win32_check_for_error (WAIT_FAILED
!= WaitForSingleObject (wt
->handle
, INFINITE
));
471 #define EXCEPTION_SET_THREAD_NAME ((DWORD) 0x406D1388)
474 static void *SetThreadName_VEH_handle
= NULL
;
476 static LONG __stdcall
477 SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo
)
479 if (ExceptionInfo
->ExceptionRecord
!= NULL
&&
480 ExceptionInfo
->ExceptionRecord
->ExceptionCode
== EXCEPTION_SET_THREAD_NAME
)
481 return EXCEPTION_CONTINUE_EXECUTION
;
483 return EXCEPTION_CONTINUE_SEARCH
;
487 typedef struct _THREADNAME_INFO
489 DWORD dwType
; /* must be 0x1000 */
490 LPCSTR szName
; /* pointer to name (in user addr space) */
491 DWORD dwThreadID
; /* thread ID (-1=caller thread) */
492 DWORD dwFlags
; /* reserved for future use, must be zero */
496 SetThreadName (DWORD dwThreadID
,
499 THREADNAME_INFO info
;
502 info
.dwType
= 0x1000;
503 info
.szName
= szThreadName
;
504 info
.dwThreadID
= dwThreadID
;
507 infosize
= sizeof (info
) / sizeof (DWORD
);
512 RaiseException (EXCEPTION_SET_THREAD_NAME
, 0, infosize
, (DWORD
*) &info
);
514 __except (EXCEPTION_EXECUTE_HANDLER
)
518 /* Without a debugger we *must* have an exception handler,
519 * otherwise raising an exception will crash the process.
521 if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle
== NULL
))
524 RaiseException (EXCEPTION_SET_THREAD_NAME
, 0, infosize
, (DWORD
*) &info
);
529 g_system_thread_set_name (const gchar
*name
)
531 SetThreadName ((DWORD
) -1, name
);
537 g_thread_win32_init (void)
539 InitializeCriticalSection (&g_private_lock
);
542 SetThreadName_VEH_handle
= AddVectoredExceptionHandler (1, &SetThreadName_VEH
);
543 if (SetThreadName_VEH_handle
== NULL
)
545 /* This is bad, but what can we do? */
551 g_thread_win32_thread_detach (void)
553 gboolean dtors_called
;
557 GPrivateDestructor
*dtor
;
559 /* We go by the POSIX book on this one.
561 * If we call a destructor then there is a chance that some new
562 * TLS variables got set by code called in that destructor.
564 * Loop until nothing is left.
566 dtors_called
= FALSE
;
568 for (dtor
= g_private_destructors
; dtor
; dtor
= dtor
->next
)
572 value
= TlsGetValue (dtor
->index
);
573 if (value
!= NULL
&& dtor
->notify
!= NULL
)
575 /* POSIX says to clear this before the call */
576 TlsSetValue (dtor
->index
, NULL
);
577 dtor
->notify (value
);
582 while (dtors_called
);
586 g_thread_win32_process_detach (void)
589 if (SetThreadName_VEH_handle
!= NULL
)
591 RemoveVectoredExceptionHandler (SetThreadName_VEH_handle
);
592 SetThreadName_VEH_handle
= NULL
;
597 /* vim:set foldmethod=marker: */