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 it */
241 win32_check_for_error (WAIT_FAILED != WaitForSingleObject (event, 0));
243 LeaveCriticalSection (&cond->lock);
246 #ifndef G_DISABLE_ASSERT
247 EnterCriticalSection (&cond->lock);
249 /* Now event must not be inside the array, check this */
250 g_assert (g_ptr_array_remove (cond->array, event) == FALSE);
252 LeaveCriticalSection (&cond->lock);
253 #endif /* !G_DISABLE_ASSERT */
255 return retval != WAIT_TIMEOUT;
259 g_cond_wait_win32_impl (GCond *cond,
260 GMutex *entered_mutex)
262 g_return_if_fail (cond != NULL);
263 g_return_if_fail (entered_mutex != NULL);
265 g_cond_wait_internal (cond, entered_mutex, INFINITE);
269 g_cond_timed_wait_win32_impl (GCond *cond,
270 GMutex *entered_mutex,
273 GTimeVal current_time;
276 g_return_val_if_fail (cond != NULL, FALSE);
277 g_return_val_if_fail (entered_mutex != NULL, FALSE);
279 g_get_current_time (¤t_time);
280 to_wait = (abs_time->tv_sec - current_time.tv_sec) * 1000 +
281 (abs_time->tv_usec - current_time.tv_usec) / 1000;
283 return g_cond_wait_internal (cond, entered_mutex, to_wait);
287 g_cond_free_win32_impl (GCond * cond)
289 g_ptr_array_free (cond->array, TRUE);
294 g_private_new_win32_impl (GDestroyNotify destructor)
297 EnterCriticalSection (&g_thread_global_spinlock);
298 if (g_private_next >= G_PRIVATE_MAX)
299 g_error ("Too many GPrivate allocated. Their number is limited to %d.\n"
300 "Use GStaticPrivate instead.\n", G_PRIVATE_MAX);
301 g_private_destructors[g_private_next] = destructor;
302 result = GUINT_TO_POINTER (g_private_next);
304 LeaveCriticalSection (&g_thread_global_spinlock);
309 /* NOTE: the functions g_private_get and g_private_set may not use
310 functions from gmem.c and gmessages.c */
313 g_private_set_win32_impl (GPrivate * private_key, gpointer value)
315 gpointer* array = TlsGetValue (g_private_tls);
316 guint index = GPOINTER_TO_UINT (private_key);
318 if (index >= G_PRIVATE_MAX)
323 array = (gpointer*) calloc (G_PRIVATE_MAX, sizeof (gpointer));
324 TlsSetValue (g_private_tls, array);
327 array[index] = value;
331 g_private_get_win32_impl (GPrivate * private_key)
333 gpointer* array = TlsGetValue (g_private_tls);
334 guint index = GPOINTER_TO_UINT (private_key);
336 if (index >= G_PRIVATE_MAX || !array)
343 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
345 GThreadData *target = *(GThreadData **)thread;
347 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
348 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
350 win32_check_for_error (SetThreadPriority (target->thread,
351 g_thread_priority_map [priority]));
355 g_thread_self_win32_impl (gpointer thread)
357 GThreadData *self = TlsGetValue (g_thread_self_tls);
361 /* This should only happen for the main thread! */
362 HANDLE handle = GetCurrentThread ();
363 HANDLE process = GetCurrentProcess ();
364 self = g_new (GThreadData, 1);
365 win32_check_for_error (DuplicateHandle (process, handle, process,
366 &self->thread, 0, FALSE,
367 DUPLICATE_SAME_ACCESS));
368 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
371 self->joinable = FALSE;
374 *(GThreadData **)thread = self;
378 g_thread_exit_win32_impl (void)
380 GThreadData *self = TlsGetValue (g_thread_self_tls);
381 guint i, private_max;
382 gpointer *array = TlsGetValue (g_private_tls);
383 HANDLE event = TlsGetValue (g_cond_event_tls);
385 EnterCriticalSection (&g_thread_global_spinlock);
386 private_max = g_private_next;
387 LeaveCriticalSection (&g_thread_global_spinlock);
391 for (i = 0; i < private_max; i++)
393 GDestroyNotify destructor = g_private_destructors[i];
394 GDestroyNotify data = array[i];
395 if (destructor && data)
401 win32_check_for_error (TlsSetValue (g_private_tls, NULL));
408 win32_check_for_error (CloseHandle (self->thread));
411 win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
417 win32_check_for_error (TlsSetValue (g_cond_event_tls, NULL));
423 static guint __stdcall
424 g_thread_proxy (gpointer data)
426 GThreadData *self = (GThreadData*) data;
428 win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
430 self->func (self->data);
432 g_thread_exit_win32_impl ();
434 g_assert_not_reached ();
440 g_thread_create_win32_impl (GThreadFunc func,
445 GThreadPriority priority,
452 g_return_if_fail (func);
453 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
454 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
456 retval = g_new(GThreadData, 1);
460 retval->joinable = joinable;
462 retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
465 if (retval->thread == NULL)
468 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
469 "Error creating thread: %ld", GetLastError());
473 *(GThreadData **)thread = retval;
475 g_thread_set_priority_win32_impl (thread, priority);
479 g_thread_yield_win32_impl (void)
485 g_thread_join_win32_impl (gpointer thread)
487 GThreadData *target = *(GThreadData **)thread;
489 g_return_if_fail (target->joinable);
491 win32_check_for_error (WAIT_FAILED !=
492 WaitForSingleObject (target->thread, INFINITE));
494 win32_check_for_error (CloseHandle (target->thread));
498 static GThreadFunctions g_thread_functions_for_glib_use_default =
500 g_mutex_new_win32_impl, /* mutex */
501 g_mutex_lock_win32_impl,
502 g_mutex_trylock_win32_impl,
503 g_mutex_unlock_win32_impl,
504 g_mutex_free_win32_impl,
505 g_cond_new_win32_impl, /* condition */
506 g_cond_signal_win32_impl,
507 g_cond_broadcast_win32_impl,
508 g_cond_wait_win32_impl,
509 g_cond_timed_wait_win32_impl,
510 g_cond_free_win32_impl,
511 g_private_new_win32_impl, /* private thread data */
512 g_private_get_win32_impl,
513 g_private_set_win32_impl,
514 g_thread_create_win32_impl, /* thread */
515 g_thread_yield_win32_impl,
516 g_thread_join_win32_impl,
517 g_thread_exit_win32_impl,
518 g_thread_set_priority_win32_impl,
519 g_thread_self_win32_impl
522 #define HAVE_G_THREAD_IMPL_INIT
524 g_thread_impl_init ()
528 win32_check_for_error (TLS_OUT_OF_INDEXES !=
529 (g_thread_self_tls = TlsAlloc ()));
530 win32_check_for_error (TLS_OUT_OF_INDEXES !=
531 (g_private_tls = TlsAlloc ()));
532 win32_check_for_error (TLS_OUT_OF_INDEXES !=
533 (g_cond_event_tls = TlsAlloc ()));
534 InitializeCriticalSection (&g_thread_global_spinlock);
536 /* Here we are looking for TryEnterCriticalSection in KERNEL32.DLL,
537 * if it is found, we can use the faster critical sections instead
538 * of mutexes. Note however that
539 * http://www2.awl.com/cseng/titles/0-201-63465-1/csmutx.htm indicates,
540 * that critical sections might not be ideal after all on SMP machines */
541 kernel32 = GetModuleHandle ("KERNEL32.DLL");
544 try_enter_critical_section = (GTryEnterCriticalSectionFunc)
545 GetProcAddress(kernel32, "TryEnterCriticalSection");
547 /* Even if TryEnterCriticalSection is found, it is not
548 * necessarily working..., we have to check it */
549 if (try_enter_critical_section &&
550 try_enter_critical_section (&g_thread_global_spinlock))
552 LeaveCriticalSection (&g_thread_global_spinlock);
554 g_thread_functions_for_glib_use_default.mutex_new =
555 g_mutex_new_win32_cs_impl;
556 g_thread_functions_for_glib_use_default.mutex_lock =
557 g_mutex_lock_win32_cs_impl;
558 g_thread_functions_for_glib_use_default.mutex_trylock =
559 g_mutex_trylock_win32_cs_impl;
560 g_thread_functions_for_glib_use_default.mutex_unlock =
561 g_mutex_unlock_win32_cs_impl;
562 g_thread_functions_for_glib_use_default.mutex_free =
563 g_mutex_free_win32_cs_impl;