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/.
40 #define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
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_STRFUNC, \
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 100
75 static GDestroyNotify g_private_destructors
[G_PRIVATE_MAX
];
77 static guint g_private_next
= 0;
79 /* A "forward" declaration of this structure */
80 static GThreadFunctions g_thread_functions_for_glib_use_default
;
82 typedef struct _GThreadData GThreadData
;
94 CRITICAL_SECTION lock
;
98 g_mutex_new_win32_cs_impl (void)
100 CRITICAL_SECTION
*cs
= g_new (CRITICAL_SECTION
, 1);
101 gpointer
*retval
= g_new (gpointer
, 1);
103 InitializeCriticalSection (cs
);
105 return (GMutex
*) retval
;
109 g_mutex_free_win32_cs_impl (GMutex
*mutex
)
111 gpointer
*ptr
= (gpointer
*) mutex
;
112 CRITICAL_SECTION
*cs
= (CRITICAL_SECTION
*) *ptr
;
114 DeleteCriticalSection (cs
);
119 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
120 functions from gmem.c and gmessages.c; */
123 g_mutex_lock_win32_cs_impl (GMutex
*mutex
)
125 EnterCriticalSection (*(CRITICAL_SECTION
**)mutex
);
129 g_mutex_trylock_win32_cs_impl (GMutex
* mutex
)
131 return try_enter_critical_section (*(CRITICAL_SECTION
**)mutex
);
135 g_mutex_unlock_win32_cs_impl (GMutex
*mutex
)
137 LeaveCriticalSection (*(CRITICAL_SECTION
**)mutex
);
141 g_mutex_new_win32_impl (void)
145 win32_check_for_error (handle
= CreateMutex (NULL
, FALSE
, NULL
));
146 retval
= g_new (HANDLE
, 1);
148 return (GMutex
*) retval
;
152 g_mutex_free_win32_impl (GMutex
*mutex
)
154 win32_check_for_error (CloseHandle (*(HANDLE
*) mutex
));
158 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
159 functions from gmem.c and gmessages.c; */
162 g_mutex_lock_win32_impl (GMutex
*mutex
)
164 WaitForSingleObject (*(HANDLE
*) mutex
, INFINITE
);
168 g_mutex_trylock_win32_impl (GMutex
* mutex
)
171 win32_check_for_error (WAIT_FAILED
!=
172 (result
= WaitForSingleObject (*(HANDLE
*)mutex
, 0)));
173 return result
!= WAIT_TIMEOUT
;
177 g_mutex_unlock_win32_impl (GMutex
*mutex
)
179 ReleaseMutex (*(HANDLE
*) mutex
);
183 g_cond_new_win32_impl (void)
185 GCond
*retval
= g_new (GCond
, 1);
187 retval
->array
= g_ptr_array_new ();
188 InitializeCriticalSection (&retval
->lock
);
194 g_cond_signal_win32_impl (GCond
* cond
)
196 EnterCriticalSection (&cond
->lock
);
198 if (cond
->array
->len
> 0)
200 SetEvent (g_ptr_array_index (cond
->array
, 0));
201 g_ptr_array_remove_index (cond
->array
, 0);
204 LeaveCriticalSection (&cond
->lock
);
208 g_cond_broadcast_win32_impl (GCond
* cond
)
211 EnterCriticalSection (&cond
->lock
);
213 for (i
= 0; i
< cond
->array
->len
; i
++)
214 SetEvent (g_ptr_array_index (cond
->array
, i
));
216 g_ptr_array_set_size (cond
->array
, 0);
217 LeaveCriticalSection (&cond
->lock
);
221 g_cond_wait_internal (GCond
*cond
,
222 GMutex
*entered_mutex
,
226 HANDLE event
= TlsGetValue (g_cond_event_tls
);
230 win32_check_for_error (event
= CreateEvent (0, FALSE
, FALSE
, NULL
));
231 TlsSetValue (g_cond_event_tls
, event
);
234 EnterCriticalSection (&cond
->lock
);
236 /* The event must not be signaled. Check this */
237 g_assert (WaitForSingleObject (event
, 0) == WAIT_TIMEOUT
);
239 g_ptr_array_add (cond
->array
, event
);
240 LeaveCriticalSection (&cond
->lock
);
242 g_thread_functions_for_glib_use_default
.mutex_unlock (entered_mutex
);
244 win32_check_for_error (WAIT_FAILED
!=
245 (retval
= WaitForSingleObject (event
, milliseconds
)));
247 g_thread_functions_for_glib_use_default
.mutex_lock (entered_mutex
);
249 if (retval
== WAIT_TIMEOUT
)
251 EnterCriticalSection (&cond
->lock
);
252 g_ptr_array_remove (cond
->array
, event
);
254 /* In the meantime we could have been signaled, so we must again
255 * wait for the signal, this time with no timeout, to reset
256 * it. retval is set again to honour the late arrival of the
258 win32_check_for_error (WAIT_FAILED
!=
259 (retval
= WaitForSingleObject (event
, 0)));
261 LeaveCriticalSection (&cond
->lock
);
264 #ifndef G_DISABLE_ASSERT
265 EnterCriticalSection (&cond
->lock
);
267 /* Now event must not be inside the array, check this */
268 g_assert (g_ptr_array_remove (cond
->array
, event
) == FALSE
);
270 LeaveCriticalSection (&cond
->lock
);
271 #endif /* !G_DISABLE_ASSERT */
273 return retval
!= WAIT_TIMEOUT
;
277 g_cond_wait_win32_impl (GCond
*cond
,
278 GMutex
*entered_mutex
)
280 g_return_if_fail (cond
!= NULL
);
281 g_return_if_fail (entered_mutex
!= NULL
);
283 g_cond_wait_internal (cond
, entered_mutex
, INFINITE
);
287 g_cond_timed_wait_win32_impl (GCond
*cond
,
288 GMutex
*entered_mutex
,
291 GTimeVal current_time
;
294 g_return_val_if_fail (cond
!= NULL
, FALSE
);
295 g_return_val_if_fail (entered_mutex
!= NULL
, FALSE
);
301 g_get_current_time (¤t_time
);
302 if (abs_time
->tv_sec
< current_time
.tv_sec
||
303 (abs_time
->tv_sec
== current_time
.tv_sec
&&
304 abs_time
->tv_usec
<= current_time
.tv_usec
))
307 to_wait
= (abs_time
->tv_sec
- current_time
.tv_sec
) * 1000 +
308 (abs_time
->tv_usec
- current_time
.tv_usec
) / 1000;
311 return g_cond_wait_internal (cond
, entered_mutex
, to_wait
);
315 g_cond_free_win32_impl (GCond
* cond
)
317 DeleteCriticalSection (&cond
->lock
);
318 g_ptr_array_free (cond
->array
, TRUE
);
323 g_private_new_win32_impl (GDestroyNotify destructor
)
326 EnterCriticalSection (&g_thread_global_spinlock
);
327 if (g_private_next
>= G_PRIVATE_MAX
)
331 "Too many GPrivate allocated. Their number is limited to %d.",
333 MessageBox (NULL
, buf
, NULL
, MB_ICONERROR
|MB_SETFOREGROUND
);
334 if (IsDebuggerPresent ())
338 g_private_destructors
[g_private_next
] = destructor
;
339 result
= GUINT_TO_POINTER (g_private_next
);
341 LeaveCriticalSection (&g_thread_global_spinlock
);
346 /* NOTE: the functions g_private_get and g_private_set may not use
347 functions from gmem.c and gmessages.c */
350 g_private_set_win32_impl (GPrivate
* private_key
, gpointer value
)
352 gpointer
* array
= TlsGetValue (g_private_tls
);
353 guint index
= GPOINTER_TO_UINT (private_key
);
355 if (index
>= G_PRIVATE_MAX
)
360 array
= (gpointer
*) calloc (G_PRIVATE_MAX
, sizeof (gpointer
));
361 TlsSetValue (g_private_tls
, array
);
364 array
[index
] = value
;
368 g_private_get_win32_impl (GPrivate
* private_key
)
370 gpointer
* array
= TlsGetValue (g_private_tls
);
371 guint index
= GPOINTER_TO_UINT (private_key
);
373 if (index
>= G_PRIVATE_MAX
|| !array
)
380 g_thread_set_priority_win32_impl (gpointer thread
, GThreadPriority priority
)
382 GThreadData
*target
= *(GThreadData
**)thread
;
384 g_return_if_fail (priority
>= G_THREAD_PRIORITY_LOW
);
385 g_return_if_fail (priority
<= G_THREAD_PRIORITY_URGENT
);
387 win32_check_for_error (SetThreadPriority (target
->thread
,
388 g_thread_priority_map
[priority
]));
392 g_thread_self_win32_impl (gpointer thread
)
394 GThreadData
*self
= TlsGetValue (g_thread_self_tls
);
398 /* This should only happen for the main thread! */
399 HANDLE handle
= GetCurrentThread ();
400 HANDLE process
= GetCurrentProcess ();
401 self
= g_new (GThreadData
, 1);
402 win32_check_for_error (DuplicateHandle (process
, handle
, process
,
403 &self
->thread
, 0, FALSE
,
404 DUPLICATE_SAME_ACCESS
));
405 win32_check_for_error (TlsSetValue (g_thread_self_tls
, self
));
408 self
->joinable
= FALSE
;
411 *(GThreadData
**)thread
= self
;
415 g_thread_exit_win32_impl (void)
417 GThreadData
*self
= TlsGetValue (g_thread_self_tls
);
418 guint i
, private_max
;
419 gpointer
*array
= TlsGetValue (g_private_tls
);
420 HANDLE event
= TlsGetValue (g_cond_event_tls
);
422 EnterCriticalSection (&g_thread_global_spinlock
);
423 private_max
= g_private_next
;
424 LeaveCriticalSection (&g_thread_global_spinlock
);
428 gboolean some_data_non_null
;
431 some_data_non_null
= FALSE
;
432 for (i
= 0; i
< private_max
; i
++)
434 GDestroyNotify destructor
= g_private_destructors
[i
];
435 GDestroyNotify data
= array
[i
];
438 some_data_non_null
= TRUE
;
442 if (destructor
&& data
)
445 } while (some_data_non_null
);
449 win32_check_for_error (TlsSetValue (g_private_tls
, NULL
));
456 win32_check_for_error (CloseHandle (self
->thread
));
459 win32_check_for_error (TlsSetValue (g_thread_self_tls
, NULL
));
465 win32_check_for_error (TlsSetValue (g_cond_event_tls
, NULL
));
471 static guint __stdcall
472 g_thread_proxy (gpointer data
)
474 GThreadData
*self
= (GThreadData
*) data
;
476 win32_check_for_error (TlsSetValue (g_thread_self_tls
, self
));
478 self
->func (self
->data
);
480 g_thread_exit_win32_impl ();
482 g_assert_not_reached ();
488 g_thread_create_win32_impl (GThreadFunc func
,
493 GThreadPriority priority
,
500 g_return_if_fail (func
);
501 g_return_if_fail (priority
>= G_THREAD_PRIORITY_LOW
);
502 g_return_if_fail (priority
<= G_THREAD_PRIORITY_URGENT
);
504 retval
= g_new(GThreadData
, 1);
508 retval
->joinable
= joinable
;
510 retval
->thread
= (HANDLE
) _beginthreadex (NULL
, stack_size
, g_thread_proxy
,
513 if (retval
->thread
== NULL
)
515 gchar
*win_error
= g_win32_error_message (GetLastError ());
516 g_set_error (error
, G_THREAD_ERROR
, G_THREAD_ERROR_AGAIN
,
517 "Error creating thread: %s", win_error
);
523 *(GThreadData
**)thread
= retval
;
525 g_thread_set_priority_win32_impl (thread
, priority
);
529 g_thread_yield_win32_impl (void)
535 g_thread_join_win32_impl (gpointer thread
)
537 GThreadData
*target
= *(GThreadData
**)thread
;
539 g_return_if_fail (target
->joinable
);
541 win32_check_for_error (WAIT_FAILED
!=
542 WaitForSingleObject (target
->thread
, INFINITE
));
544 win32_check_for_error (CloseHandle (target
->thread
));
549 g_thread_gettime_impl (void)
553 /* Returns 100s of nanoseconds since start of 1601 */
554 GetSystemTimeAsFileTime ((FILETIME
*)&v
);
556 /* Offset to Unix epoch */
557 v
-= G_GINT64_CONSTANT (116444736000000000);
558 /* Convert to nanoseconds */
564 static GThreadFunctions g_thread_functions_for_glib_use_default
=
566 g_mutex_new_win32_impl
, /* mutex */
567 g_mutex_lock_win32_impl
,
568 g_mutex_trylock_win32_impl
,
569 g_mutex_unlock_win32_impl
,
570 g_mutex_free_win32_impl
,
571 g_cond_new_win32_impl
, /* condition */
572 g_cond_signal_win32_impl
,
573 g_cond_broadcast_win32_impl
,
574 g_cond_wait_win32_impl
,
575 g_cond_timed_wait_win32_impl
,
576 g_cond_free_win32_impl
,
577 g_private_new_win32_impl
, /* private thread data */
578 g_private_get_win32_impl
,
579 g_private_set_win32_impl
,
580 g_thread_create_win32_impl
, /* thread */
581 g_thread_yield_win32_impl
,
582 g_thread_join_win32_impl
,
583 g_thread_exit_win32_impl
,
584 g_thread_set_priority_win32_impl
,
585 g_thread_self_win32_impl
,
586 NULL
/* no equal function necessary */
589 #define HAVE_G_THREAD_IMPL_INIT
591 g_thread_impl_init ()
593 static gboolean beenhere
= FALSE
;
601 win32_check_for_error (TLS_OUT_OF_INDEXES
!=
602 (g_thread_self_tls
= TlsAlloc ()));
603 win32_check_for_error (TLS_OUT_OF_INDEXES
!=
604 (g_private_tls
= TlsAlloc ()));
605 win32_check_for_error (TLS_OUT_OF_INDEXES
!=
606 (g_cond_event_tls
= TlsAlloc ()));
607 InitializeCriticalSection (&g_thread_global_spinlock
);
609 /* Here we are looking for TryEnterCriticalSection in KERNEL32.DLL,
610 * if it is found, we can use the in general faster critical
611 * sections instead of mutexes. See
612 * http://world.std.com/~jmhart/csmutx.htm for some discussion.
614 kernel32
= GetModuleHandle ("KERNEL32.DLL");
617 try_enter_critical_section
= (GTryEnterCriticalSectionFunc
)
618 GetProcAddress(kernel32
, "TryEnterCriticalSection");
620 /* Even if TryEnterCriticalSection is found, it is not
621 * necessarily working..., we have to check it */
622 if (try_enter_critical_section
&&
623 try_enter_critical_section (&g_thread_global_spinlock
))
625 LeaveCriticalSection (&g_thread_global_spinlock
);
627 g_thread_functions_for_glib_use_default
.mutex_new
=
628 g_mutex_new_win32_cs_impl
;
629 g_thread_functions_for_glib_use_default
.mutex_lock
=
630 g_mutex_lock_win32_cs_impl
;
631 g_thread_functions_for_glib_use_default
.mutex_trylock
=
632 g_mutex_trylock_win32_cs_impl
;
633 g_thread_functions_for_glib_use_default
.mutex_unlock
=
634 g_mutex_unlock_win32_cs_impl
;
635 g_thread_functions_for_glib_use_default
.mutex_free
=
636 g_mutex_free_win32_cs_impl
;