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 %ld during %s", \
51 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
52 GetLastError (), #what); \
55 #define G_MUTEX_SIZE (sizeof (HANDLE))
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 *retval = g_new (CRITICAL_SECTION, 1);
98 InitializeCriticalSection (retval);
99 return (GMutex *) retval;
103 g_mutex_free_win32_cs_impl (GMutex *mutex)
108 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
109 functions from gmem.c and gmessages.c; */
112 g_mutex_lock_win32_cs_impl (GMutex *mutex)
114 EnterCriticalSection ((CRITICAL_SECTION *)mutex);
118 g_mutex_trylock_win32_cs_impl (GMutex * mutex)
120 return try_enter_critical_section ((CRITICAL_SECTION *)mutex);
124 g_mutex_unlock_win32_cs_impl (GMutex *mutex)
126 LeaveCriticalSection ((CRITICAL_SECTION *)mutex);
130 g_mutex_new_win32_impl (void)
133 win32_check_for_error (handle = CreateMutex (NULL, FALSE, NULL));
134 return (GMutex *) handle;
138 g_mutex_free_win32_impl (GMutex *mutex)
140 win32_check_for_error (CloseHandle ((HANDLE) mutex));
143 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
144 functions from gmem.c and gmessages.c; */
147 g_mutex_lock_win32_impl (GMutex *mutex)
149 WaitForSingleObject ((HANDLE) mutex, INFINITE);
153 g_mutex_trylock_win32_impl (GMutex * mutex)
156 win32_check_for_error (WAIT_FAILED !=
157 (result = WaitForSingleObject ((HANDLE)mutex, 0)));
158 return result != WAIT_TIMEOUT;
162 g_mutex_unlock_win32_impl (GMutex *mutex)
164 ReleaseMutex ((HANDLE) mutex);
168 g_cond_new_win32_impl (void)
170 GCond *retval = g_new (GCond, 1);
172 retval->array = g_ptr_array_new ();
173 InitializeCriticalSection (&retval->lock);
179 g_cond_signal_win32_impl (GCond * cond)
181 EnterCriticalSection (&cond->lock);
183 if (cond->array->len > 0)
185 SetEvent (g_ptr_array_index (cond->array, 0));
186 g_ptr_array_remove_index (cond->array, 0);
189 LeaveCriticalSection (&cond->lock);
193 g_cond_broadcast_win32_impl (GCond * cond)
196 EnterCriticalSection (&cond->lock);
198 for (i = 0; i < cond->array->len; i++)
199 SetEvent (g_ptr_array_index (cond->array, i));
201 g_ptr_array_set_size (cond->array, 0);
202 LeaveCriticalSection (&cond->lock);
206 g_cond_wait_internal (GCond *cond,
207 GMutex *entered_mutex,
211 HANDLE event = TlsGetValue (g_cond_event_tls);
215 win32_check_for_error (event = CreateEvent (0, FALSE, FALSE, NULL));
216 TlsSetValue (g_cond_event_tls, event);
219 EnterCriticalSection (&cond->lock);
221 /* The event must not be signaled. Check this */
222 g_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
224 g_ptr_array_add (cond->array, event);
225 LeaveCriticalSection (&cond->lock);
227 g_mutex_unlock (entered_mutex);
229 win32_check_for_error (WAIT_FAILED !=
230 (retval = WaitForSingleObject (event, milliseconds)));
232 g_mutex_lock (entered_mutex);
234 if (retval == WAIT_TIMEOUT)
236 EnterCriticalSection (&cond->lock);
237 g_ptr_array_remove (cond->array, event);
239 /* In the meantime we could have been signaled, so we must again
240 * wait for the signal, this time with no timeout, to reset
241 * it. retval is set again to honour the late arrival of the
243 win32_check_for_error (WAIT_FAILED !=
244 (retval = WaitForSingleObject (event, 0)));
246 LeaveCriticalSection (&cond->lock);
249 #ifndef G_DISABLE_ASSERT
250 EnterCriticalSection (&cond->lock);
252 /* Now event must not be inside the array, check this */
253 g_assert (g_ptr_array_remove (cond->array, event) == FALSE);
255 LeaveCriticalSection (&cond->lock);
256 #endif /* !G_DISABLE_ASSERT */
258 return retval != WAIT_TIMEOUT;
262 g_cond_wait_win32_impl (GCond *cond,
263 GMutex *entered_mutex)
265 g_return_if_fail (cond != NULL);
266 g_return_if_fail (entered_mutex != NULL);
268 g_cond_wait_internal (cond, entered_mutex, INFINITE);
272 g_cond_timed_wait_win32_impl (GCond *cond,
273 GMutex *entered_mutex,
276 GTimeVal current_time;
279 g_return_val_if_fail (cond != NULL, FALSE);
280 g_return_val_if_fail (entered_mutex != NULL, FALSE);
282 g_get_current_time (¤t_time);
283 to_wait = (abs_time->tv_sec - current_time.tv_sec) * 1000 +
284 (abs_time->tv_usec - current_time.tv_usec) / 1000;
286 return g_cond_wait_internal (cond, entered_mutex, to_wait);
290 g_cond_free_win32_impl (GCond * cond)
292 g_ptr_array_free (cond->array, TRUE);
297 g_private_new_win32_impl (GDestroyNotify destructor)
300 EnterCriticalSection (&g_thread_global_spinlock);
301 if (g_private_next >= G_PRIVATE_MAX)
302 g_error ("Too many GPrivate allocated. Their number is limited to %d.\n"
303 "Use GStaticPrivate instead.\n", G_PRIVATE_MAX);
304 g_private_destructors[g_private_next] = destructor;
305 result = GUINT_TO_POINTER (g_private_next);
307 LeaveCriticalSection (&g_thread_global_spinlock);
312 /* NOTE: the functions g_private_get and g_private_set may not use
313 functions from gmem.c and gmessages.c */
316 g_private_set_win32_impl (GPrivate * private_key, gpointer value)
318 gpointer* array = TlsGetValue (g_private_tls);
319 guint index = GPOINTER_TO_UINT (private_key);
321 if (index >= G_PRIVATE_MAX)
326 array = (gpointer*) calloc (G_PRIVATE_MAX, sizeof (gpointer));
327 TlsSetValue (g_private_tls, array);
330 array[index] = value;
334 g_private_get_win32_impl (GPrivate * private_key)
336 gpointer* array = TlsGetValue (g_private_tls);
337 guint index = GPOINTER_TO_UINT (private_key);
339 if (index >= G_PRIVATE_MAX || !array)
346 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
348 GThreadData *target = *(GThreadData **)thread;
350 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
351 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
353 win32_check_for_error (SetThreadPriority (target->thread,
354 g_thread_priority_map [priority]));
358 g_thread_self_win32_impl (gpointer thread)
360 GThreadData *self = TlsGetValue (g_thread_self_tls);
364 /* This should only happen for the main thread! */
365 HANDLE handle = GetCurrentThread ();
366 HANDLE process = GetCurrentProcess ();
367 self = g_new (GThreadData, 1);
368 win32_check_for_error (DuplicateHandle (process, handle, process,
369 &self->thread, 0, FALSE,
370 DUPLICATE_SAME_ACCESS));
371 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
374 self->joinable = FALSE;
377 *(GThreadData **)thread = self;
381 g_thread_exit_win32_impl (void)
383 GThreadData *self = TlsGetValue (g_thread_self_tls);
384 guint i, private_max;
385 gpointer *array = TlsGetValue (g_private_tls);
386 HANDLE event = TlsGetValue (g_cond_event_tls);
388 EnterCriticalSection (&g_thread_global_spinlock);
389 private_max = g_private_next;
390 LeaveCriticalSection (&g_thread_global_spinlock);
394 for (i = 0; i < private_max; i++)
396 GDestroyNotify destructor = g_private_destructors[i];
397 GDestroyNotify data = array[i];
398 if (destructor && data)
404 win32_check_for_error (TlsSetValue (g_private_tls, NULL));
411 win32_check_for_error (CloseHandle (self->thread));
414 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
420 win32_check_for_error (TlsSetValue (g_cond_event_tls, NULL));
426 static guint __stdcall
427 g_thread_proxy (gpointer data)
429 GThreadData *self = (GThreadData*) data;
431 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
433 self->func (self->data);
435 g_thread_exit_win32_impl ();
437 g_assert_not_reached ();
443 g_thread_create_win32_impl (GThreadFunc func,
448 GThreadPriority priority,
455 g_return_if_fail (func);
456 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
457 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
459 retval = g_new(GThreadData, 1);
463 retval->joinable = joinable;
465 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
468 if (retval->thread == NULL)
471 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
472 "Error creating thread: %ld", GetLastError());
476 *(GThreadData **)thread = retval;
478 g_thread_set_priority_win32_impl (thread, priority);
482 g_thread_yield_win32_impl (void)
488 g_thread_join_win32_impl (gpointer thread)
490 GThreadData *target = *(GThreadData **)thread;
492 g_return_if_fail (target->joinable);
494 win32_check_for_error (WAIT_FAILED !=
495 WaitForSingleObject (target->thread, INFINITE));
497 win32_check_for_error (CloseHandle (target->thread));
501 static GThreadFunctions g_thread_functions_for_glib_use_default =
503 g_mutex_new_win32_impl, /* mutex */
504 g_mutex_lock_win32_impl,
505 g_mutex_trylock_win32_impl,
506 g_mutex_unlock_win32_impl,
507 g_mutex_free_win32_impl,
508 g_cond_new_win32_impl, /* condition */
509 g_cond_signal_win32_impl,
510 g_cond_broadcast_win32_impl,
511 g_cond_wait_win32_impl,
512 g_cond_timed_wait_win32_impl,
513 g_cond_free_win32_impl,
514 g_private_new_win32_impl, /* private thread data */
515 g_private_get_win32_impl,
516 g_private_set_win32_impl,
517 g_thread_create_win32_impl, /* thread */
518 g_thread_yield_win32_impl,
519 g_thread_join_win32_impl,
520 g_thread_exit_win32_impl,
521 g_thread_set_priority_win32_impl,
522 g_thread_self_win32_impl
525 #define HAVE_G_THREAD_IMPL_INIT
527 g_thread_impl_init ()
531 win32_check_for_error (TLS_OUT_OF_INDEXES !=
532 (g_thread_self_tls = TlsAlloc ()));
533 win32_check_for_error (TLS_OUT_OF_INDEXES !=
534 (g_private_tls = TlsAlloc ()));
535 win32_check_for_error (TLS_OUT_OF_INDEXES !=
536 (g_cond_event_tls = TlsAlloc ()));
537 InitializeCriticalSection (&g_thread_global_spinlock);
539 /* Here we are looking for TryEnterCriticalSection in KERNEL32.DLL,
540 * if it is found, we can use the faster critical sections instead
541 * of mutexes. Note however that
542 * http://www2.awl.com/cseng/titles/0-201-63465-1/csmutx.htm indicates,
543 * that critical sections might not be ideal after all on SMP machines */
544 kernel32 = GetModuleHandle ("KERNEL32.DLL");
547 try_enter_critical_section = (GTryEnterCriticalSectionFunc)
548 GetProcAddress(kernel32, "TryEnterCriticalSection");
550 /* Even if TryEnterCriticalSection is found, it is not
551 * necessarily working..., we have to check it */
552 if (try_enter_critical_section &&
553 try_enter_critical_section (&g_thread_global_spinlock))
555 LeaveCriticalSection (&g_thread_global_spinlock);
557 g_thread_functions_for_glib_use_default.mutex_new =
558 g_mutex_new_win32_cs_impl;
559 g_thread_functions_for_glib_use_default.mutex_lock =
560 g_mutex_lock_win32_cs_impl;
561 g_thread_functions_for_glib_use_default.mutex_trylock =
562 g_mutex_trylock_win32_cs_impl;
563 g_thread_functions_for_glib_use_default.mutex_unlock =
564 g_mutex_unlock_win32_cs_impl;
565 g_thread_functions_for_glib_use_default.mutex_free =
566 g_mutex_free_win32_cs_impl;