2 * mono-threads.c: Low-level threading
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * Copyright 2011 Novell, Inc (http://www.novell.com)
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 #include <mono/utils/mono-compiler.h>
12 #include <mono/utils/mono-semaphore.h>
13 #include <mono/utils/mono-threads.h>
14 #include <mono/utils/mono-tls.h>
15 #include <mono/utils/hazard-pointer.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/domain-internals.h>
21 #define THREADS_DEBUG(...)
22 //#define THREADS_DEBUG(...) g_message(__VA_ARGS__)
25 Mutex that makes sure only a single thread can be suspending others.
26 Suspend is a very racy operation since it requires restarting until
27 the target thread is not on an unsafe region.
29 We could implement this using critical regions, but would be much much
30 harder for an operation that is hardly performance critical.
32 The GC has to acquire this lock before starting a STW to make sure
33 a runtime suspend won't make it wronly see a thread in a safepoint
34 when it is in fact not.
36 static CRITICAL_SECTION global_suspend_lock;
39 static int thread_info_size;
40 static MonoThreadInfoCallbacks threads_callbacks;
41 static MonoThreadInfoRuntimeCallbacks runtime_callbacks;
42 static MonoNativeTlsKey thread_info_key, small_id_key;
43 static MonoLinkedListSet thread_list;
44 static gboolean disable_new_interrupt = FALSE;
47 mono_hazard_pointer_clear_all (MonoThreadHazardPointers *hp, int retain)
50 mono_hazard_pointer_clear (hp, 0);
52 mono_hazard_pointer_clear (hp, 1);
54 mono_hazard_pointer_clear (hp, 2);
58 If return non null Hazard Pointer 1 holds the return value.
61 mono_thread_info_lookup (MonoNativeThreadId id)
63 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
65 if (!mono_lls_find (&thread_list, hp, (uintptr_t)id)) {
66 mono_hazard_pointer_clear_all (hp, -1);
70 mono_hazard_pointer_clear_all (hp, 1);
71 return mono_hazard_pointer_get_val (hp, 1);
75 mono_thread_info_insert (MonoThreadInfo *info)
77 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
79 if (!mono_lls_insert (&thread_list, hp, (MonoLinkedListSetNode*)info)) {
80 mono_hazard_pointer_clear_all (hp, -1);
84 mono_hazard_pointer_clear_all (hp, -1);
89 mono_thread_info_remove (MonoThreadInfo *info)
91 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
94 THREADS_DEBUG ("removing info %p\n", info);
95 res = mono_lls_remove (&thread_list, hp, (MonoLinkedListSetNode*)info);
96 mono_hazard_pointer_clear_all (hp, -1);
101 free_thread_info (gpointer mem)
103 MonoThreadInfo *info = mem;
105 DeleteCriticalSection (&info->suspend_lock);
106 MONO_SEM_DESTROY (&info->resume_semaphore);
107 MONO_SEM_DESTROY (&info->finish_resume_semaphore);
108 mono_threads_platform_free (info);
114 mono_thread_info_register_small_id (void)
116 int small_id = mono_thread_small_id_alloc ();
117 mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (small_id + 1));
122 register_thread (MonoThreadInfo *info, gpointer baseptr)
124 int small_id = mono_thread_info_register_small_id ();
126 mono_thread_info_set_tid (info, mono_native_thread_id_get ());
127 info->small_id = small_id;
129 InitializeCriticalSection (&info->suspend_lock);
130 MONO_SEM_INIT (&info->resume_semaphore, 0);
131 MONO_SEM_INIT (&info->finish_resume_semaphore, 0);
133 /*set TLS early so SMR works */
134 mono_native_tls_set_value (thread_info_key, info);
136 THREADS_DEBUG ("registering info %p tid %p small id %x\n", info, mono_thread_info_get_tid (info), info->small_id);
138 if (threads_callbacks.thread_register) {
139 if (threads_callbacks.thread_register (info, baseptr) == NULL) {
140 g_warning ("thread registation failed\n");
146 mono_threads_platform_register (info);
148 /*If this fail it means a given thread has been registered twice, which doesn't make sense. */
149 result = mono_thread_info_insert (info);
155 unregister_thread (void *arg)
157 MonoThreadInfo *info = arg;
158 int small_id = info->small_id;
161 THREADS_DEBUG ("unregistering info %p\n", info);
164 * TLS destruction order is not reliable so small_id might be cleaned up
167 mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (info->small_id + 1));
170 The unregister callback is reposible for calling mono_threads_unregister_current_thread
171 since it usually needs to be done in sync with the GC does a stop-the-world.
173 if (threads_callbacks.thread_unregister)
174 threads_callbacks.thread_unregister (info);
176 mono_threads_unregister_current_thread (info);
178 /*now it's safe to free the thread info.*/
179 mono_thread_hazardous_free_or_queue (info, free_thread_info);
180 mono_thread_small_id_free (small_id);
184 * Removes the current thread from the thread list.
185 * This must be called from the thread unregister callback and nowhere else.
186 * The current thread must be passed as TLS might have already been cleaned up.
189 mono_threads_unregister_current_thread (MonoThreadInfo *info)
192 g_assert (mono_thread_info_get_tid (info) == mono_native_thread_id_get ());
193 result = mono_thread_info_remove (info);
199 mono_thread_info_current (void)
201 return mono_native_tls_get_value (thread_info_key);
205 mono_thread_info_get_small_id (void)
207 gpointer val = mono_native_tls_get_value (small_id_key);
210 return GPOINTER_TO_INT (val) - 1;
214 mono_thread_info_list_head (void)
220 mono_thread_info_attach (void *baseptr)
222 MonoThreadInfo *info = mono_native_tls_get_value (thread_info_key);
224 info = g_malloc0 (thread_info_size);
225 THREADS_DEBUG ("attaching %p\n", info);
226 if (!register_thread (info, baseptr))
228 } else if (threads_callbacks.thread_attach) {
229 threads_callbacks.thread_attach (info);
235 mono_thread_info_dettach (void)
237 MonoThreadInfo *info = mono_native_tls_get_value (thread_info_key);
239 THREADS_DEBUG ("detaching %p\n", info);
240 unregister_thread (info);
245 mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
248 threads_callbacks = *callbacks;
249 thread_info_size = info_size;
251 res = mono_native_tls_alloc (&thread_info_key, NULL);
253 res = mono_native_tls_alloc (&thread_info_key, unregister_thread);
257 res = mono_native_tls_alloc (&small_id_key, NULL);
260 InitializeCriticalSection (&global_suspend_lock);
262 mono_lls_init (&thread_list, NULL);
263 mono_thread_smr_init ();
264 mono_threads_init_platform ();
266 g_assert (sizeof (MonoNativeThreadId) <= sizeof (uintptr_t));
270 mono_threads_runtime_init (MonoThreadInfoRuntimeCallbacks *callbacks)
272 runtime_callbacks = *callbacks;
275 MonoThreadInfoCallbacks *
276 mono_threads_get_callbacks (void)
278 return &threads_callbacks;
281 MonoThreadInfoRuntimeCallbacks *
282 mono_threads_get_runtime_callbacks (void)
284 return &runtime_callbacks;
288 The return value is only valid until a matching mono_thread_info_resume is called
290 static MonoThreadInfo*
291 mono_thread_info_suspend_sync (MonoNativeThreadId tid, gboolean interrupt_kernel)
293 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
294 MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
298 EnterCriticalSection (&info->suspend_lock);
300 /*thread is on the process of detaching*/
301 if (mono_thread_info_run_state (info) > STATE_RUNNING) {
302 mono_hazard_pointer_clear (hp, 1);
306 THREADS_DEBUG ("suspend %x IN COUNT %d\n", tid, info->suspend_count);
308 if (info->suspend_count) {
309 ++info->suspend_count;
310 mono_hazard_pointer_clear (hp, 1);
311 LeaveCriticalSection (&info->suspend_lock);
315 if (!mono_threads_core_suspend (info)) {
316 LeaveCriticalSection (&info->suspend_lock);
317 mono_hazard_pointer_clear (hp, 1);
321 if (interrupt_kernel)
322 mono_threads_core_interrupt (info);
324 ++info->suspend_count;
325 info->thread_state |= STATE_SUSPENDED;
326 LeaveCriticalSection (&info->suspend_lock);
327 mono_hazard_pointer_clear (hp, 1);
333 mono_thread_info_self_suspend (void)
336 MonoThreadInfo *info = mono_thread_info_current ();
340 EnterCriticalSection (&info->suspend_lock);
342 THREADS_DEBUG ("self suspend IN COUNT %d\n", info->suspend_count);
344 g_assert (info->suspend_count == 0);
345 ++info->suspend_count;
347 info->thread_state |= STATE_SELF_SUSPENDED;
349 ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&info->suspend_state, NULL);
352 LeaveCriticalSection (&info->suspend_lock);
354 while (MONO_SEM_WAIT (&info->resume_semaphore) != 0) {
355 /*if (EINTR != errno) ABORT("sem_wait failed"); */
358 g_assert (!info->async_target); /*FIXME this should happen normally for suspend. */
359 MONO_SEM_POST (&info->finish_resume_semaphore);
363 mono_thread_info_resume_internal (MonoThreadInfo *info)
366 if (mono_thread_info_suspend_state (info) == STATE_SELF_SUSPENDED) {
367 MONO_SEM_POST (&info->resume_semaphore);
368 while (MONO_SEM_WAIT (&info->finish_resume_semaphore) != 0) {
369 /* g_assert (errno == EINTR); */
373 result = mono_threads_core_resume (info);
375 info->thread_state &= ~SUSPEND_STATE_MASK;
380 mono_thread_info_resume (MonoNativeThreadId tid)
382 gboolean result = TRUE;
383 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
384 MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
388 EnterCriticalSection (&info->suspend_lock);
390 THREADS_DEBUG ("resume %x IN COUNT %d\n",tid, info->suspend_count);
392 if (info->suspend_count <= 0) {
393 LeaveCriticalSection (&info->suspend_lock);
394 mono_hazard_pointer_clear (hp, 1);
399 * The theory here is that if we manage to suspend the thread it means it did not
400 * start cleanup since it take the same lock.
402 g_assert (mono_thread_info_get_tid (info));
404 if (--info->suspend_count == 0)
405 result = mono_thread_info_resume_internal (info);
407 LeaveCriticalSection (&info->suspend_lock);
408 mono_hazard_pointer_clear (hp, 1);
414 FIXME fix cardtable WB to be out of line and check with the runtime if the target is not the
415 WB trampoline. Another option is to encode wb ranges in MonoJitInfo, but that is somewhat hard.
418 is_thread_in_critical_region (MonoThreadInfo *info)
421 MonoJitInfo *ji = mono_jit_info_table_find (
422 info->suspend_state.unwind_data [MONO_UNWIND_DATA_DOMAIN],
423 MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
430 return threads_callbacks.mono_method_is_critical (method);
435 If we are trying to suspend a target that is on a critical region
436 and running a syscall we risk looping forever if @interrupt_kernel is FALSE.
437 So, be VERY carefull in calling this with @interrupt_kernel == FALSE.
440 mono_thread_info_safe_suspend_sync (MonoNativeThreadId id, gboolean interrupt_kernel)
442 MonoThreadInfo *info = NULL;
443 int sleep_duration = 0;
445 /*FIXME: unify this with self-suspend*/
446 g_assert (id != mono_native_thread_id_get ());
448 mono_thread_info_suspend_lock ();
451 if (!(info = mono_thread_info_suspend_sync (id, interrupt_kernel))) {
452 g_warning ("failed to suspend thread %p, hopefully it is dead", (gpointer)id);
453 mono_thread_info_suspend_unlock ();
456 /*WARNING: We now are in interrupt context until we resume the thread. */
457 if (!is_thread_in_critical_region (info))
460 if (!mono_thread_info_resume (id)) {
461 g_warning ("failed to result thread %p, hopefully it is dead", (gpointer)id);
462 mono_thread_info_suspend_unlock ();
465 THREADS_DEBUG ("restarted thread %p\n", (gpointer)id);
467 if (!sleep_duration) {
475 g_usleep (sleep_duration);
477 sleep_duration += 10;
480 mono_thread_info_suspend_unlock ();
485 Inject an assynchronous call into the target thread. The target thread must be suspended and
486 only a single async call can be setup for a given suspend cycle.
487 This async call must cause stack unwinding as the current implementation doesn't save enough state
488 to resume execution of the top-of-stack function. It's an acceptable limitation since this is
489 currently used only to deliver exceptions.
492 mono_thread_info_setup_async_call (MonoThreadInfo *info, void (*target_func)(void*), void *user_data)
494 g_assert (info->suspend_count);
495 /*FIXME this is a bad assert, we probably should do proper locking and fail if one is already set*/
496 g_assert (!info->async_target);
497 info->async_target = target_func;
498 /* This is not GC tracked */
499 info->user_data = user_data;
503 The suspend lock is held during any suspend in progress.
504 A GC that has safepoints must take this lock as part of its
505 STW to make sure no unsafe pending suspend is in progress.
508 mono_thread_info_suspend_lock (void)
510 EnterCriticalSection (&global_suspend_lock);
514 mono_thread_info_suspend_unlock (void)
516 LeaveCriticalSection (&global_suspend_lock);
520 mono_thread_info_disable_new_interrupt (gboolean disable)
522 disable_new_interrupt = disable;
525 Disabled by default for now.
526 To enable this we need mini to implement the callbacks by MonoThreadInfoRuntimeCallbacks
527 which means mono-context and setup_async_callback, and we need a mono-threads backend.
530 mono_thread_info_new_interrupt_enabled (void)
532 /*We need STW gc events to work correctly*/
533 #if defined (HAVE_BOEHM_GC) && !defined (USE_INCLUDED_LIBGC)
537 #if defined(HOST_WIN32)
540 #if defined (__i386__)
541 return !disable_new_interrupt;