2 * threads.c: Thread handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Ximian, Inc.
8 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
20 #include <sys/types.h>
23 #include <mono/io-layer/wapi.h>
24 #include <mono/io-layer/wapi-private.h>
25 #include <mono/io-layer/handles-private.h>
26 #include <mono/io-layer/misc-private.h>
27 #include <mono/io-layer/thread-private.h>
28 #include <mono/io-layer/mutex-private.h>
30 #include <mono/utils/mono-threads.h>
31 #include <mono/utils/atomic.h>
32 #include <mono/utils/mono-mutex.h>
34 #ifdef HAVE_VALGRIND_MEMCHECK_H
35 #include <valgrind/memcheck.h>
39 #define DEBUG(...) g_message(__VA_ARGS__)
45 #define WAIT_DEBUG(code) do { code } while (0)
47 #define WAIT_DEBUG(code) do { } while (0)
50 struct _WapiHandleOps _wapi_thread_ops = {
55 NULL, /* special_wait */
59 static mono_once_t thread_ops_once = MONO_ONCE_INIT;
62 thread_ops_init (void)
64 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
65 WAPI_HANDLE_CAP_WAIT);
69 _wapi_thread_cleanup (void)
74 get_current_thread_handle (void)
78 info = mono_thread_info_current ();
80 g_assert (info->handle);
84 static WapiHandle_thread*
85 lookup_thread (HANDLE handle)
87 WapiHandle_thread *thread;
90 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
96 static WapiHandle_thread*
97 get_current_thread (void)
101 handle = get_current_thread_handle ();
102 return lookup_thread (handle);
106 wapi_thread_handle_set_exited (gpointer handle, guint32 exitstatus)
108 WapiHandle_thread *thread_handle;
110 pid_t pid = _wapi_getpid ();
111 pthread_t tid = pthread_self ();
113 if (_wapi_handle_issignalled (handle) ||
114 _wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
115 /* We must have already deliberately finished with
116 * this thread, so don't do any more now
121 DEBUG ("%s: Thread %p terminating", __func__, handle);
123 thread_handle = lookup_thread (handle);
125 DEBUG ("%s: Thread %p abandoning held mutexes", __func__, handle);
127 for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
128 gpointer mutex = g_ptr_array_index (thread_handle->owned_mutexes, i);
130 _wapi_mutex_abandon (mutex, pid, tid);
131 _wapi_thread_disown_mutex (mutex);
133 g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
135 thr_ret = _wapi_handle_lock_handle (handle);
136 g_assert (thr_ret == 0);
138 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
140 thr_ret = _wapi_handle_unlock_handle (handle);
141 g_assert (thr_ret == 0);
143 DEBUG("%s: Recording thread handle %p id %ld status as %d",
144 __func__, handle, thread_handle->id, exitstatus);
146 /* The thread is no longer active, so unref it */
147 _wapi_handle_unref (handle);
151 * wapi_create_thread_handle:
153 * Create a thread handle for the current thread.
156 wapi_create_thread_handle (void)
158 WapiHandle_thread thread_handle = {0}, *thread;
161 mono_once (&thread_ops_once, thread_ops_init);
163 thread_handle.owned_mutexes = g_ptr_array_new ();
165 handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
166 if (handle == _WAPI_HANDLE_INVALID) {
167 g_warning ("%s: error creating thread handle", __func__);
168 SetLastError (ERROR_GEN_FAILURE);
173 thread = lookup_thread (handle);
175 thread->id = pthread_self ();
178 * Hold a reference while the thread is active, because we use
179 * the handle to store thread exit information
181 _wapi_handle_ref (handle);
183 DEBUG ("%s: started thread id %ld", __func__, thread->id);
189 wapi_ref_thread_handle (gpointer handle)
191 _wapi_handle_ref (handle);
195 wapi_get_current_thread_handle (void)
197 return get_current_thread_handle ();
201 * GetCurrentThreadId:
203 * Looks up the thread ID of the current thread. This ID can be
204 * passed to OpenThread() to create a new handle on this thread.
206 * Return value: the thread ID. NB this is defined as DWORD (ie 32
207 * bit) in the MS API, but we need to cope with 64 bit IDs for s390x
208 * and amd64. This doesn't really break the API, it just embraces and
209 * extends it on 64bit platforms :)
212 GetCurrentThreadId (void)
214 MonoNativeThreadId id;
216 id = mono_native_thread_id_get ();
217 return MONO_NATIVE_THREAD_ID_TO_UINT (id);
222 * @ms: The time in milliseconds to suspend for
223 * @alertable: if TRUE, the wait can be interrupted by an APC call
225 * Suspends execution of the current thread for @ms milliseconds. A
226 * value of zero causes the thread to relinquish its time slice. A
227 * value of %INFINITE causes an infinite delay.
230 SleepEx (guint32 ms, gboolean alertable)
234 gpointer current_thread = NULL;
235 #if defined (__linux__) && !defined(PLATFORM_ANDROID)
236 struct timespec start, target;
241 DEBUG("%s: Sleeping for %d ms", __func__, ms);
244 current_thread = get_current_thread_handle ();
246 if (_wapi_thread_cur_apc_pending ())
247 return WAIT_IO_COMPLETION;
255 /* FIXME: check for INFINITE and sleep forever */
259 #if defined (__linux__) && !defined(PLATFORM_ANDROID)
260 /* Use clock_nanosleep () to prevent time drifting problems when nanosleep () is interrupted by signals */
261 ret = clock_gettime (CLOCK_MONOTONIC, &start);
264 target.tv_sec += ms_quot;
265 target.tv_nsec += ms_rem * 1000000;
266 if (target.tv_nsec > 999999999) {
267 target.tv_nsec -= 999999999;
272 ret = clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &target, NULL);
274 if (alertable && _wapi_thread_cur_apc_pending ())
275 return WAIT_IO_COMPLETION;
285 req.tv_nsec=ms_rem*1000000;
288 memset (&rem, 0, sizeof (rem));
289 ret=nanosleep(&req, &rem);
291 if (alertable && _wapi_thread_cur_apc_pending ())
292 return WAIT_IO_COMPLETION;
295 /* Sleep interrupted with rem time remaining */
297 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
299 g_message("%s: Still got %d ms to go", __func__, rems);
305 #endif /* __linux__ */
317 _wapi_thread_cur_apc_pending (void)
319 WapiHandle_thread *thread;
321 thread = lookup_thread (get_current_thread_handle ());
322 return thread->wait_handle == INTERRUPTION_REQUESTED_HANDLE;
326 * wapi_interrupt_thread:
328 * The state of the thread handle HANDLE is set to 'interrupted' which means that
329 * if the thread calls one of the WaitFor functions, the function will return with
330 * WAIT_IO_COMPLETION instead of waiting. Also, if the thread was waiting when
331 * this function was called, the wait will be broken.
332 * It is possible that the wait functions return WAIT_IO_COMPLETION, but the
333 * target thread didn't receive the interrupt signal yet, in this case it should
334 * call the wait function again. This essentially means that the target thread will
335 * busy wait until it is ready to process the interruption.
338 wapi_prepare_interrupt_thread (gpointer thread_handle)
340 WapiHandle_thread *thread;
341 gpointer prev_handle, wait_handle;
343 thread = lookup_thread (thread_handle); /* FIXME this is wrong, move this whole thing to MonoThreads where it can be done lockfree */
346 wait_handle = thread->wait_handle;
349 * Atomically obtain the handle the thread is waiting on, and
350 * change it to a flag value.
352 prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
353 INTERRUPTION_REQUESTED_HANDLE, wait_handle);
354 if (prev_handle == INTERRUPTION_REQUESTED_HANDLE)
355 /* Already interrupted */
357 if (prev_handle == wait_handle)
363 WAIT_DEBUG (printf ("%p: state -> INTERRUPTED.\n", thread->id););
369 wapi_finish_interrupt_thread (gpointer wait_handle)
371 pthread_cond_t *cond;
379 /* If we reach here, then wait_handle is set to the flag value,
380 * which means that the target thread is either
381 * - before the first CAS in timedwait, which means it won't enter the
383 * - it is after the first CAS, so it is already waiting, or it will
384 * enter the wait, and it will be interrupted by the broadcast.
386 idx = GPOINTER_TO_UINT(wait_handle);
387 cond = &_WAPI_PRIVATE_HANDLES(idx).signal_cond;
388 mutex = &_WAPI_PRIVATE_HANDLES(idx).signal_mutex;
390 mono_mutex_lock (mutex);
391 mono_cond_broadcast (cond);
392 mono_mutex_unlock (mutex);
394 /* ref added by set_wait_handle */
395 _wapi_handle_unref (wait_handle);
399 * wapi_self_interrupt:
401 * This is not part of the WIN32 API.
402 * Set the 'interrupted' state of the calling thread if it's NULL.
405 wapi_self_interrupt (void)
407 gpointer wait_handle;
409 wait_handle = wapi_prepare_interrupt_thread (get_current_thread_handle ());
411 /* ref added by set_wait_handle */
412 _wapi_handle_unref (wait_handle);
416 * wapi_clear_interruption:
418 * This is not part of the WIN32 API.
419 * Clear the 'interrupted' state of the calling thread.
420 * This function is signal safe
423 wapi_clear_interruption (void)
425 WapiHandle_thread *thread;
426 gpointer prev_handle;
428 thread = get_current_thread ();
430 prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
431 NULL, INTERRUPTION_REQUESTED_HANDLE);
432 if (prev_handle == INTERRUPTION_REQUESTED_HANDLE)
433 WAIT_DEBUG (printf ("%p: state -> NORMAL.\n", GetCurrentThreadId ()););
437 * wapi_thread_set_wait_handle:
439 * Set the wait handle for the current thread to HANDLE. Return TRUE on success, FALSE
440 * if the thread is in interrupted state, and cannot start waiting.
443 wapi_thread_set_wait_handle (gpointer handle)
445 WapiHandle_thread *thread;
446 gpointer prev_handle;
448 thread = get_current_thread ();
450 prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
452 if (prev_handle == NULL) {
453 /* thread->wait_handle acts as an additional reference to the handle */
454 _wapi_handle_ref (handle);
456 WAIT_DEBUG (printf ("%p: state -> WAITING.\n", GetCurrentThreadId ()););
458 g_assert (prev_handle == INTERRUPTION_REQUESTED_HANDLE);
459 WAIT_DEBUG (printf ("%p: unable to set state to WAITING.\n", GetCurrentThreadId ()););
462 return prev_handle == NULL;
466 * wapi_thread_clear_wait_handle:
468 * Clear the wait handle of the current thread.
471 wapi_thread_clear_wait_handle (gpointer handle)
473 WapiHandle_thread *thread;
474 gpointer prev_handle;
476 thread = get_current_thread ();
478 prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
480 if (prev_handle == handle) {
481 _wapi_handle_unref (handle);
482 WAIT_DEBUG (printf ("%p: state -> NORMAL.\n", GetCurrentThreadId ()););
484 /*It can be NULL if it was asynchronously cleared*/
485 g_assert (prev_handle == INTERRUPTION_REQUESTED_HANDLE || prev_handle == NULL);
486 WAIT_DEBUG (printf ("%p: finished waiting.\n", GetCurrentThreadId ()););
491 _wapi_thread_own_mutex (gpointer mutex)
493 WapiHandle_thread *thread;
495 thread = get_current_thread ();
497 _wapi_handle_ref (mutex);
499 g_ptr_array_add (thread->owned_mutexes, mutex);
503 _wapi_thread_disown_mutex (gpointer mutex)
505 WapiHandle_thread *thread;
507 thread = get_current_thread ();
509 _wapi_handle_unref (mutex);
511 g_ptr_array_remove (thread->owned_mutexes, mutex);
515 wapi_current_thread_desc (void)
517 WapiHandle_thread *thread;
518 gpointer thread_handle;
524 thread_handle = get_current_thread_handle ();
525 thread = lookup_thread (thread_handle);
527 handle = thread->wait_handle;
528 text = g_string_new (0);
529 g_string_append_printf (text, "thread handle %p state : ", thread_handle);
532 g_string_append_printf (text, "not waiting");
533 else if (handle == INTERRUPTION_REQUESTED_HANDLE)
534 g_string_append_printf (text, "interrupted state");
536 g_string_append_printf (text, "waiting on %p : %s ", handle, _wapi_handle_typename[_wapi_handle_type (handle)]);
537 g_string_append_printf (text, " owns (");
538 for (i = 0; i < thread->owned_mutexes->len; i++) {
539 gpointer mutex = g_ptr_array_index (thread->owned_mutexes, i);
541 g_string_append_printf (text, ", %p", mutex);
543 g_string_append_printf (text, "%p", mutex);
545 g_string_append_printf (text, ")");
548 g_string_free (text, FALSE);