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/.
38 #include "gthreadprivate.h"
41 #define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
49 #define win32_check_for_error(what) G_STMT_START{ \
51 g_error ("file %s: line %d (%s): error %s during %s", \
52 __FILE__, __LINE__, G_STRFUNC, \
53 g_win32_error_message (GetLastError ()), #what); \
56 #define G_MUTEX_SIZE (sizeof (gpointer))
58 static DWORD g_thread_self_tls;
59 static DWORD g_private_tls;
60 static DWORD g_cond_event_tls;
61 static CRITICAL_SECTION g_thread_global_spinlock;
63 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
65 /* As noted in the docs, GPrivate is a limited resource, here we take
66 * a rather low maximum to save memory, use GStaticPrivate instead. */
67 #define G_PRIVATE_MAX 100
69 static GDestroyNotify g_private_destructors[G_PRIVATE_MAX];
71 static guint g_private_next = 0;
73 typedef struct _GThreadData GThreadData;
85 CRITICAL_SECTION lock;
89 g_mutex_new_win32_impl (void)
91 CRITICAL_SECTION *cs = g_new (CRITICAL_SECTION, 1);
92 gpointer *retval = g_new (gpointer, 1);
94 InitializeCriticalSection (cs);
96 return (GMutex *) retval;
100 g_mutex_free_win32_impl (GMutex *mutex)
102 gpointer *ptr = (gpointer *) mutex;
103 CRITICAL_SECTION *cs = (CRITICAL_SECTION *) *ptr;
105 DeleteCriticalSection (cs);
110 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
111 functions from gmem.c and gmessages.c; */
114 g_mutex_lock_win32_impl (GMutex *mutex)
116 EnterCriticalSection (*(CRITICAL_SECTION **)mutex);
120 g_mutex_trylock_win32_impl (GMutex * mutex)
122 return TryEnterCriticalSection (*(CRITICAL_SECTION **)mutex);
126 g_mutex_unlock_win32_impl (GMutex *mutex)
128 LeaveCriticalSection (*(CRITICAL_SECTION **)mutex);
132 g_cond_new_win32_impl (void)
134 GCond *retval = g_new (GCond, 1);
136 retval->array = g_ptr_array_new ();
137 InitializeCriticalSection (&retval->lock);
143 g_cond_signal_win32_impl (GCond * cond)
145 EnterCriticalSection (&cond->lock);
147 if (cond->array->len > 0)
149 SetEvent (g_ptr_array_index (cond->array, 0));
150 g_ptr_array_remove_index (cond->array, 0);
153 LeaveCriticalSection (&cond->lock);
157 g_cond_broadcast_win32_impl (GCond * cond)
160 EnterCriticalSection (&cond->lock);
162 for (i = 0; i < cond->array->len; i++)
163 SetEvent (g_ptr_array_index (cond->array, i));
165 g_ptr_array_set_size (cond->array, 0);
166 LeaveCriticalSection (&cond->lock);
170 g_cond_wait_internal (GCond *cond,
171 GMutex *entered_mutex,
175 HANDLE event = TlsGetValue (g_cond_event_tls);
179 win32_check_for_error (event = CreateEvent (0, FALSE, FALSE, NULL));
180 TlsSetValue (g_cond_event_tls, event);
183 EnterCriticalSection (&cond->lock);
185 /* The event must not be signaled. Check this */
186 g_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
188 g_ptr_array_add (cond->array, event);
189 LeaveCriticalSection (&cond->lock);
191 g_mutex_unlock (entered_mutex);
193 win32_check_for_error (WAIT_FAILED !=
194 (retval = WaitForSingleObject (event, milliseconds)));
196 g_mutex_lock (entered_mutex);
198 if (retval == WAIT_TIMEOUT)
200 EnterCriticalSection (&cond->lock);
201 g_ptr_array_remove (cond->array, event);
203 /* In the meantime we could have been signaled, so we must again
204 * wait for the signal, this time with no timeout, to reset
205 * it. retval is set again to honour the late arrival of the
207 win32_check_for_error (WAIT_FAILED !=
208 (retval = WaitForSingleObject (event, 0)));
210 LeaveCriticalSection (&cond->lock);
213 #ifndef G_DISABLE_ASSERT
214 EnterCriticalSection (&cond->lock);
216 /* Now event must not be inside the array, check this */
217 g_assert (g_ptr_array_remove (cond->array, event) == FALSE);
219 LeaveCriticalSection (&cond->lock);
220 #endif /* !G_DISABLE_ASSERT */
222 return retval != WAIT_TIMEOUT;
226 g_cond_wait_win32_impl (GCond *cond,
227 GMutex *entered_mutex)
229 g_return_if_fail (cond != NULL);
230 g_return_if_fail (entered_mutex != NULL);
232 g_cond_wait_internal (cond, entered_mutex, INFINITE);
236 g_cond_timed_wait_win32_impl (GCond *cond,
237 GMutex *entered_mutex,
240 GTimeVal current_time;
243 g_return_val_if_fail (cond != NULL, FALSE);
244 g_return_val_if_fail (entered_mutex != NULL, FALSE);
250 g_get_current_time (¤t_time);
251 if (abs_time->tv_sec < current_time.tv_sec ||
252 (abs_time->tv_sec == current_time.tv_sec &&
253 abs_time->tv_usec <= current_time.tv_usec))
256 to_wait = (abs_time->tv_sec - current_time.tv_sec) * 1000 +
257 (abs_time->tv_usec - current_time.tv_usec) / 1000;
260 return g_cond_wait_internal (cond, entered_mutex, to_wait);
264 g_cond_free_win32_impl (GCond * cond)
266 DeleteCriticalSection (&cond->lock);
267 g_ptr_array_free (cond->array, TRUE);
272 g_private_new_win32_impl (GDestroyNotify destructor)
275 EnterCriticalSection (&g_thread_global_spinlock);
276 if (g_private_next >= G_PRIVATE_MAX)
280 "Too many GPrivate allocated. Their number is limited to %d.",
282 MessageBox (NULL, buf, NULL, MB_ICONERROR|MB_SETFOREGROUND);
283 if (IsDebuggerPresent ())
287 g_private_destructors[g_private_next] = destructor;
288 result = GUINT_TO_POINTER (g_private_next);
290 LeaveCriticalSection (&g_thread_global_spinlock);
295 /* NOTE: the functions g_private_get and g_private_set may not use
296 functions from gmem.c and gmessages.c */
299 g_private_set_win32_impl (GPrivate * private_key, gpointer value)
301 gpointer* array = TlsGetValue (g_private_tls);
302 guint index = GPOINTER_TO_UINT (private_key);
304 if (index >= G_PRIVATE_MAX)
309 array = (gpointer*) calloc (G_PRIVATE_MAX, sizeof (gpointer));
310 TlsSetValue (g_private_tls, array);
313 array[index] = value;
317 g_private_get_win32_impl (GPrivate * private_key)
319 gpointer* array = TlsGetValue (g_private_tls);
320 guint index = GPOINTER_TO_UINT (private_key);
322 if (index >= G_PRIVATE_MAX || !array)
329 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
331 GThreadData *target = *(GThreadData **)thread;
336 case G_THREAD_PRIORITY_LOW:
337 native_prio = THREAD_PRIORITY_BELOW_NORMAL;
340 case G_THREAD_PRIORITY_NORMAL:
341 native_prio = THREAD_PRIORITY_NORMAL;
344 case G_THREAD_PRIORITY_HIGH:
345 native_prio = THREAD_PRIORITY_ABOVE_NORMAL;
348 case G_THREAD_PRIORITY_URGENT:
349 native_prio = THREAD_PRIORITY_HIGHEST;
353 g_return_if_reached ();
356 win32_check_for_error (SetThreadPriority (target->thread, native_prio));
360 g_thread_self_win32_impl (gpointer thread)
362 GThreadData *self = TlsGetValue (g_thread_self_tls);
366 /* This should only happen for the main thread! */
367 HANDLE handle = GetCurrentThread ();
368 HANDLE process = GetCurrentProcess ();
369 self = g_new (GThreadData, 1);
370 win32_check_for_error (DuplicateHandle (process, handle, process,
371 &self->thread, 0, FALSE,
372 DUPLICATE_SAME_ACCESS));
373 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
376 self->joinable = FALSE;
379 *(GThreadData **)thread = self;
383 g_thread_exit_win32_impl (void)
385 GThreadData *self = TlsGetValue (g_thread_self_tls);
386 guint i, private_max;
387 gpointer *array = TlsGetValue (g_private_tls);
388 HANDLE event = TlsGetValue (g_cond_event_tls);
390 EnterCriticalSection (&g_thread_global_spinlock);
391 private_max = g_private_next;
392 LeaveCriticalSection (&g_thread_global_spinlock);
396 gboolean some_data_non_null;
399 some_data_non_null = FALSE;
400 for (i = 0; i < private_max; i++)
402 GDestroyNotify destructor = g_private_destructors[i];
403 GDestroyNotify data = array[i];
406 some_data_non_null = TRUE;
410 if (destructor && data)
413 } while (some_data_non_null);
417 win32_check_for_error (TlsSetValue (g_private_tls, NULL));
424 win32_check_for_error (CloseHandle (self->thread));
427 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
433 win32_check_for_error (TlsSetValue (g_cond_event_tls, NULL));
439 static guint __stdcall
440 g_thread_proxy (gpointer data)
442 GThreadData *self = (GThreadData*) data;
444 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
446 self->func (self->data);
448 g_thread_exit_win32_impl ();
450 g_assert_not_reached ();
456 g_thread_create_win32_impl (GThreadFunc func,
461 GThreadPriority priority,
468 g_return_if_fail (func);
469 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
470 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
472 retval = g_new(GThreadData, 1);
476 retval->joinable = joinable;
478 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
481 if (retval->thread == 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);
491 *(GThreadData **)thread = retval;
493 g_thread_set_priority_win32_impl (thread, priority);
497 g_thread_yield_win32_impl (void)
503 g_thread_join_win32_impl (gpointer thread)
505 GThreadData *target = *(GThreadData **)thread;
507 g_return_if_fail (target->joinable);
509 win32_check_for_error (WAIT_FAILED !=
510 WaitForSingleObject (target->thread, INFINITE));
512 win32_check_for_error (CloseHandle (target->thread));
516 GThreadFunctions g_thread_functions_for_glib_use =
518 g_mutex_new_win32_impl, /* mutex */
519 g_mutex_lock_win32_impl,
520 g_mutex_trylock_win32_impl,
521 g_mutex_unlock_win32_impl,
522 g_mutex_free_win32_impl,
523 g_cond_new_win32_impl, /* condition */
524 g_cond_signal_win32_impl,
525 g_cond_broadcast_win32_impl,
526 g_cond_wait_win32_impl,
527 g_cond_timed_wait_win32_impl,
528 g_cond_free_win32_impl,
529 g_private_new_win32_impl, /* private thread data */
530 g_private_get_win32_impl,
531 g_private_set_win32_impl,
532 g_thread_create_win32_impl, /* thread */
533 g_thread_yield_win32_impl,
534 g_thread_join_win32_impl,
535 g_thread_exit_win32_impl,
536 g_thread_set_priority_win32_impl,
537 g_thread_self_win32_impl,
538 NULL /* no equal function necessary */
542 _g_thread_impl_init (void)
544 static gboolean beenhere = FALSE;
551 win32_check_for_error (TLS_OUT_OF_INDEXES !=
552 (g_thread_self_tls = TlsAlloc ()));
553 win32_check_for_error (TLS_OUT_OF_INDEXES !=
554 (g_private_tls = TlsAlloc ()));
555 win32_check_for_error (TLS_OUT_OF_INDEXES !=
556 (g_cond_event_tls = TlsAlloc ()));
557 InitializeCriticalSection (&g_thread_global_spinlock);