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/.
48 #define win32_check_for_error(what) G_STMT_START{ \
50 g_error ("file %s: line %d (%s): error %s during %s", \
51 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
52 g_win32_error_message (GetLastError ()), #what); \
55 #define G_MUTEX_SIZE (sizeof (gpointer))
57 #define PRIORITY_LOW_VALUE THREAD_PRIORITY_BELOW_NORMAL
58 #define PRIORITY_NORMAL_VALUE THREAD_PRIORITY_NORMAL
59 #define PRIORITY_HIGH_VALUE THREAD_PRIORITY_ABOVE_NORMAL
60 #define PRIORITY_URGENT_VALUE THREAD_PRIORITY_HIGHEST
62 static DWORD g_thread_self_tls;
63 static DWORD g_private_tls;
64 static DWORD g_cond_event_tls;
65 static CRITICAL_SECTION g_thread_global_spinlock;
67 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
69 static GTryEnterCriticalSectionFunc try_enter_critical_section = NULL;
71 /* As noted in the docs, GPrivate is a limited resource, here we take
72 * a rather low maximum to save memory, use GStaticPrivate instead. */
73 #define G_PRIVATE_MAX 16
75 static GDestroyNotify g_private_destructors[G_PRIVATE_MAX];
77 static guint g_private_next = 0;
79 typedef struct _GThreadData GThreadData;
91 CRITICAL_SECTION lock;
95 g_mutex_new_win32_cs_impl (void)
97 CRITICAL_SECTION *cs = g_new (CRITICAL_SECTION, 1);
98 gpointer *retval = g_new (gpointer, 1);
100 InitializeCriticalSection (cs);
102 return (GMutex *) retval;
106 g_mutex_free_win32_cs_impl (GMutex *mutex)
108 gpointer *ptr = (gpointer *) mutex;
109 CRITICAL_SECTION *cs = (CRITICAL_SECTION *) *ptr;
111 DeleteCriticalSection (cs);
116 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
117 functions from gmem.c and gmessages.c; */
120 g_mutex_lock_win32_cs_impl (GMutex *mutex)
122 EnterCriticalSection (*(CRITICAL_SECTION **)mutex);
126 g_mutex_trylock_win32_cs_impl (GMutex * mutex)
128 return try_enter_critical_section (*(CRITICAL_SECTION **)mutex);
132 g_mutex_unlock_win32_cs_impl (GMutex *mutex)
134 LeaveCriticalSection (*(CRITICAL_SECTION **)mutex);
138 g_mutex_new_win32_impl (void)
142 win32_check_for_error (handle = CreateMutex (NULL, FALSE, NULL));
143 retval = g_new (HANDLE, 1);
145 return (GMutex *) retval;
149 g_mutex_free_win32_impl (GMutex *mutex)
151 win32_check_for_error (CloseHandle (*(HANDLE *) mutex));
155 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
156 functions from gmem.c and gmessages.c; */
159 g_mutex_lock_win32_impl (GMutex *mutex)
161 WaitForSingleObject (*(HANDLE *) mutex, INFINITE);
165 g_mutex_trylock_win32_impl (GMutex * mutex)
168 win32_check_for_error (WAIT_FAILED !=
169 (result = WaitForSingleObject (*(HANDLE *)mutex, 0)));
170 return result != WAIT_TIMEOUT;
174 g_mutex_unlock_win32_impl (GMutex *mutex)
176 ReleaseMutex (*(HANDLE *) mutex);
180 g_cond_new_win32_impl (void)
182 GCond *retval = g_new (GCond, 1);
184 retval->array = g_ptr_array_new ();
185 InitializeCriticalSection (&retval->lock);
191 g_cond_signal_win32_impl (GCond * cond)
193 EnterCriticalSection (&cond->lock);
195 if (cond->array->len > 0)
197 SetEvent (g_ptr_array_index (cond->array, 0));
198 g_ptr_array_remove_index (cond->array, 0);
201 LeaveCriticalSection (&cond->lock);
205 g_cond_broadcast_win32_impl (GCond * cond)
208 EnterCriticalSection (&cond->lock);
210 for (i = 0; i < cond->array->len; i++)
211 SetEvent (g_ptr_array_index (cond->array, i));
213 g_ptr_array_set_size (cond->array, 0);
214 LeaveCriticalSection (&cond->lock);
218 g_cond_wait_internal (GCond *cond,
219 GMutex *entered_mutex,
223 HANDLE event = TlsGetValue (g_cond_event_tls);
227 win32_check_for_error (event = CreateEvent (0, FALSE, FALSE, NULL));
228 TlsSetValue (g_cond_event_tls, event);
231 EnterCriticalSection (&cond->lock);
233 /* The event must not be signaled. Check this */
234 g_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
236 g_ptr_array_add (cond->array, event);
237 LeaveCriticalSection (&cond->lock);
239 g_mutex_unlock (entered_mutex);
241 win32_check_for_error (WAIT_FAILED !=
242 (retval = WaitForSingleObject (event, milliseconds)));
244 g_mutex_lock (entered_mutex);
246 if (retval == WAIT_TIMEOUT)
248 EnterCriticalSection (&cond->lock);
249 g_ptr_array_remove (cond->array, event);
251 /* In the meantime we could have been signaled, so we must again
252 * wait for the signal, this time with no timeout, to reset
253 * it. retval is set again to honour the late arrival of the
255 win32_check_for_error (WAIT_FAILED !=
256 (retval = WaitForSingleObject (event, 0)));
258 LeaveCriticalSection (&cond->lock);
261 #ifndef G_DISABLE_ASSERT
262 EnterCriticalSection (&cond->lock);
264 /* Now event must not be inside the array, check this */
265 g_assert (g_ptr_array_remove (cond->array, event) == FALSE);
267 LeaveCriticalSection (&cond->lock);
268 #endif /* !G_DISABLE_ASSERT */
270 return retval != WAIT_TIMEOUT;
274 g_cond_wait_win32_impl (GCond *cond,
275 GMutex *entered_mutex)
277 g_return_if_fail (cond != NULL);
278 g_return_if_fail (entered_mutex != NULL);
280 g_cond_wait_internal (cond, entered_mutex, INFINITE);
284 g_cond_timed_wait_win32_impl (GCond *cond,
285 GMutex *entered_mutex,
288 GTimeVal current_time;
291 g_return_val_if_fail (cond != NULL, FALSE);
292 g_return_val_if_fail (entered_mutex != NULL, FALSE);
298 g_get_current_time (¤t_time);
299 if (abs_time->tv_sec < current_time.tv_sec ||
300 (abs_time->tv_sec == current_time.tv_sec &&
301 abs_time->tv_usec <= current_time.tv_usec))
304 to_wait = (abs_time->tv_sec - current_time.tv_sec) * 1000 +
305 (abs_time->tv_usec - current_time.tv_usec) / 1000;
308 return g_cond_wait_internal (cond, entered_mutex, to_wait);
312 g_cond_free_win32_impl (GCond * cond)
314 DeleteCriticalSection (&cond->lock);
315 g_ptr_array_free (cond->array, TRUE);
320 g_private_new_win32_impl (GDestroyNotify destructor)
323 EnterCriticalSection (&g_thread_global_spinlock);
324 if (g_private_next >= G_PRIVATE_MAX)
325 g_error ("Too many GPrivate allocated. Their number is limited to %d.\n"
326 "Use GStaticPrivate instead.\n", G_PRIVATE_MAX);
327 g_private_destructors[g_private_next] = destructor;
328 result = GUINT_TO_POINTER (g_private_next);
330 LeaveCriticalSection (&g_thread_global_spinlock);
335 /* NOTE: the functions g_private_get and g_private_set may not use
336 functions from gmem.c and gmessages.c */
339 g_private_set_win32_impl (GPrivate * private_key, gpointer value)
341 gpointer* array = TlsGetValue (g_private_tls);
342 guint index = GPOINTER_TO_UINT (private_key);
344 if (index >= G_PRIVATE_MAX)
349 array = (gpointer*) calloc (G_PRIVATE_MAX, sizeof (gpointer));
350 TlsSetValue (g_private_tls, array);
353 array[index] = value;
357 g_private_get_win32_impl (GPrivate * private_key)
359 gpointer* array = TlsGetValue (g_private_tls);
360 guint index = GPOINTER_TO_UINT (private_key);
362 if (index >= G_PRIVATE_MAX || !array)
369 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
371 GThreadData *target = *(GThreadData **)thread;
373 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
374 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
376 win32_check_for_error (SetThreadPriority (target->thread,
377 g_thread_priority_map [priority]));
381 g_thread_self_win32_impl (gpointer thread)
383 GThreadData *self = TlsGetValue (g_thread_self_tls);
387 /* This should only happen for the main thread! */
388 HANDLE handle = GetCurrentThread ();
389 HANDLE process = GetCurrentProcess ();
390 self = g_new (GThreadData, 1);
391 win32_check_for_error (DuplicateHandle (process, handle, process,
392 &self->thread, 0, FALSE,
393 DUPLICATE_SAME_ACCESS));
394 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
397 self->joinable = FALSE;
400 *(GThreadData **)thread = self;
404 g_thread_exit_win32_impl (void)
406 GThreadData *self = TlsGetValue (g_thread_self_tls);
407 guint i, private_max;
408 gpointer *array = TlsGetValue (g_private_tls);
409 HANDLE event = TlsGetValue (g_cond_event_tls);
411 EnterCriticalSection (&g_thread_global_spinlock);
412 private_max = g_private_next;
413 LeaveCriticalSection (&g_thread_global_spinlock);
417 for (i = 0; i < private_max; i++)
419 GDestroyNotify destructor = g_private_destructors[i];
420 GDestroyNotify data = array[i];
421 if (destructor && data)
427 win32_check_for_error (TlsSetValue (g_private_tls, NULL));
434 win32_check_for_error (CloseHandle (self->thread));
437 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
443 win32_check_for_error (TlsSetValue (g_cond_event_tls, NULL));
449 static guint __stdcall
450 g_thread_proxy (gpointer data)
452 GThreadData *self = (GThreadData*) data;
454 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
456 self->func (self->data);
458 g_thread_exit_win32_impl ();
460 g_assert_not_reached ();
466 g_thread_create_win32_impl (GThreadFunc func,
471 GThreadPriority priority,
478 g_return_if_fail (func);
479 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
480 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
482 retval = g_new(GThreadData, 1);
486 retval->joinable = joinable;
488 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
491 if (retval->thread == NULL)
493 gchar *win_error = g_win32_error_message (GetLastError ());
494 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
495 "Error creating thread: %s", win_error);
501 *(GThreadData **)thread = retval;
503 g_thread_set_priority_win32_impl (thread, priority);
507 g_thread_yield_win32_impl (void)
513 g_thread_join_win32_impl (gpointer thread)
515 GThreadData *target = *(GThreadData **)thread;
517 g_return_if_fail (target->joinable);
519 win32_check_for_error (WAIT_FAILED !=
520 WaitForSingleObject (target->thread, INFINITE));
522 win32_check_for_error (CloseHandle (target->thread));
526 static GThreadFunctions g_thread_functions_for_glib_use_default =
528 g_mutex_new_win32_impl, /* mutex */
529 g_mutex_lock_win32_impl,
530 g_mutex_trylock_win32_impl,
531 g_mutex_unlock_win32_impl,
532 g_mutex_free_win32_impl,
533 g_cond_new_win32_impl, /* condition */
534 g_cond_signal_win32_impl,
535 g_cond_broadcast_win32_impl,
536 g_cond_wait_win32_impl,
537 g_cond_timed_wait_win32_impl,
538 g_cond_free_win32_impl,
539 g_private_new_win32_impl, /* private thread data */
540 g_private_get_win32_impl,
541 g_private_set_win32_impl,
542 g_thread_create_win32_impl, /* thread */
543 g_thread_yield_win32_impl,
544 g_thread_join_win32_impl,
545 g_thread_exit_win32_impl,
546 g_thread_set_priority_win32_impl,
547 g_thread_self_win32_impl,
548 NULL /* no equal function necessary */
551 #define HAVE_G_THREAD_IMPL_INIT
553 g_thread_impl_init ()
555 static gboolean beenhere = FALSE;
563 win32_check_for_error (TLS_OUT_OF_INDEXES !=
564 (g_thread_self_tls = TlsAlloc ()));
565 win32_check_for_error (TLS_OUT_OF_INDEXES !=
566 (g_private_tls = TlsAlloc ()));
567 win32_check_for_error (TLS_OUT_OF_INDEXES !=
568 (g_cond_event_tls = TlsAlloc ()));
569 InitializeCriticalSection (&g_thread_global_spinlock);
571 /* Here we are looking for TryEnterCriticalSection in KERNEL32.DLL,
572 * if it is found, we can use the faster critical sections instead
573 * of mutexes. Note however that
574 * http://www2.awl.com/cseng/titles/0-201-63465-1/csmutx.htm indicates,
575 * that critical sections might not be ideal after all on SMP machines */
576 kernel32 = GetModuleHandle ("KERNEL32.DLL");
579 try_enter_critical_section = (GTryEnterCriticalSectionFunc)
580 GetProcAddress(kernel32, "TryEnterCriticalSection");
582 /* Even if TryEnterCriticalSection is found, it is not
583 * necessarily working..., we have to check it */
584 if (try_enter_critical_section &&
585 try_enter_critical_section (&g_thread_global_spinlock))
587 LeaveCriticalSection (&g_thread_global_spinlock);
589 g_thread_functions_for_glib_use_default.mutex_new =
590 g_mutex_new_win32_cs_impl;
591 g_thread_functions_for_glib_use_default.mutex_lock =
592 g_mutex_lock_win32_cs_impl;
593 g_thread_functions_for_glib_use_default.mutex_trylock =
594 g_mutex_trylock_win32_cs_impl;
595 g_thread_functions_for_glib_use_default.mutex_unlock =
596 g_mutex_unlock_win32_cs_impl;
597 g_thread_functions_for_glib_use_default.mutex_free =
598 g_mutex_free_win32_cs_impl;