[threading] Introduce mono_thread_info_is_live and move all thread state testing...
[mono-project.git] / mono / utils / mono-threads.c
blob02510ab5a5194cf60546bba0da53980e0d3788dd
1 /*
2 * mono-threads.c: Low-level threading
4 * Author:
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * Copyright 2011 Novell, Inc (http://www.novell.com)
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9 */
11 #include <config.h>
13 #include <mono/utils/mono-compiler.h>
14 #include <mono/utils/mono-semaphore.h>
15 #include <mono/utils/mono-threads.h>
16 #include <mono/utils/mono-tls.h>
17 #include <mono/utils/hazard-pointer.h>
18 #include <mono/utils/mono-memory-model.h>
19 #include <mono/utils/mono-mmap.h>
20 #include <mono/utils/atomic.h>
22 #include <errno.h>
24 #if defined(__MACH__)
25 #include <mono/utils/mach-support.h>
26 #endif
28 #define THREADS_DEBUG(...)
29 //#define THREADS_DEBUG(...) g_message(__VA_ARGS__)
32 Mutex that makes sure only a single thread can be suspending others.
33 Suspend is a very racy operation since it requires restarting until
34 the target thread is not on an unsafe region.
36 We could implement this using critical regions, but would be much much
37 harder for an operation that is hardly performance critical.
39 The GC has to acquire this lock before starting a STW to make sure
40 a runtime suspend won't make it wronly see a thread in a safepoint
41 when it is in fact not.
43 static MonoSemType global_suspend_semaphore;
45 static size_t thread_info_size;
46 static MonoThreadInfoCallbacks threads_callbacks;
47 static MonoThreadInfoRuntimeCallbacks runtime_callbacks;
48 static MonoNativeTlsKey thread_info_key, thread_exited_key;
49 #ifdef HAVE_KW_THREAD
50 static __thread guint32 tls_small_id MONO_TLS_FAST;
51 #else
52 static MonoNativeTlsKey small_id_key;
53 #endif
54 static MonoLinkedListSet thread_list;
55 static gboolean disable_new_interrupt = FALSE;
56 static gboolean mono_threads_inited = FALSE;
58 static void mono_threads_unregister_current_thread (MonoThreadInfo *info);
60 #define mono_thread_info_run_state(info) (((MonoThreadInfo*)info)->thread_state & RUN_STATE_MASK)
61 #define mono_thread_info_suspend_state(info) (((MonoThreadInfo*)info)->thread_state & SUSPEND_STATE_MASK)
64 static inline void
65 mono_hazard_pointer_clear_all (MonoThreadHazardPointers *hp, int retain)
67 if (retain != 0)
68 mono_hazard_pointer_clear (hp, 0);
69 if (retain != 1)
70 mono_hazard_pointer_clear (hp, 1);
71 if (retain != 2)
72 mono_hazard_pointer_clear (hp, 2);
76 If return non null Hazard Pointer 1 holds the return value.
78 MonoThreadInfo*
79 mono_thread_info_lookup (MonoNativeThreadId id)
81 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
83 if (!mono_lls_find (&thread_list, hp, (uintptr_t)id)) {
84 mono_hazard_pointer_clear_all (hp, -1);
85 return NULL;
88 mono_hazard_pointer_clear_all (hp, 1);
89 return mono_hazard_pointer_get_val (hp, 1);
92 static gboolean
93 mono_thread_info_insert (MonoThreadInfo *info)
95 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
97 if (!mono_lls_insert (&thread_list, hp, (MonoLinkedListSetNode*)info)) {
98 mono_hazard_pointer_clear_all (hp, -1);
99 return FALSE;
102 mono_hazard_pointer_clear_all (hp, -1);
103 return TRUE;
106 static gboolean
107 mono_thread_info_remove (MonoThreadInfo *info)
109 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
110 gboolean res;
112 THREADS_DEBUG ("removing info %p\n", info);
113 res = mono_lls_remove (&thread_list, hp, (MonoLinkedListSetNode*)info);
114 mono_hazard_pointer_clear_all (hp, -1);
115 return res;
118 static void
119 free_thread_info (gpointer mem)
121 MonoThreadInfo *info = mem;
123 MONO_SEM_DESTROY (&info->suspend_semaphore);
124 MONO_SEM_DESTROY (&info->resume_semaphore);
125 MONO_SEM_DESTROY (&info->finish_resume_semaphore);
126 mono_threads_platform_free (info);
128 g_free (info);
132 mono_thread_info_register_small_id (void)
134 int small_id = mono_thread_small_id_alloc ();
135 #ifdef HAVE_KW_THREAD
136 tls_small_id = small_id;
137 #else
138 mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (small_id + 1));
139 #endif
140 return small_id;
143 static void*
144 register_thread (MonoThreadInfo *info, gpointer baseptr)
146 int small_id = mono_thread_info_register_small_id ();
147 gboolean result;
148 mono_thread_info_set_tid (info, mono_native_thread_id_get ());
149 info->small_id = small_id;
151 MONO_SEM_INIT (&info->suspend_semaphore, 1);
152 MONO_SEM_INIT (&info->resume_semaphore, 0);
153 MONO_SEM_INIT (&info->finish_resume_semaphore, 0);
155 /*set TLS early so SMR works */
156 mono_native_tls_set_value (thread_info_key, info);
158 THREADS_DEBUG ("registering info %p tid %p small id %x\n", info, mono_thread_info_get_tid (info), info->small_id);
160 if (threads_callbacks.thread_register) {
161 if (threads_callbacks.thread_register (info, baseptr) == NULL) {
162 // g_warning ("thread registation failed\n");
163 g_free (info);
164 return NULL;
168 mono_threads_platform_register (info);
169 info->thread_state = STATE_RUNNING;
170 mono_thread_info_suspend_lock ();
171 /*If this fail it means a given thread has been registered twice, which doesn't make sense. */
172 result = mono_thread_info_insert (info);
173 g_assert (result);
174 mono_thread_info_suspend_unlock ();
175 return info;
178 static void
179 unregister_thread (void *arg)
181 MonoThreadInfo *info = arg;
182 int small_id = info->small_id;
183 g_assert (info);
185 THREADS_DEBUG ("unregistering info %p\n", info);
187 mono_native_tls_set_value (thread_exited_key, GUINT_TO_POINTER (1));
189 mono_threads_core_unregister (info);
192 * TLS destruction order is not reliable so small_id might be cleaned up
193 * before us.
195 #ifndef HAVE_KW_THREAD
196 mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (info->small_id + 1));
197 #endif
199 info->thread_state = STATE_SHUTTING_DOWN;
202 First perform the callback that requires no locks.
203 This callback has the potential of taking other locks, so we do it before.
204 After it completes, the thread remains functional.
206 if (threads_callbacks.thread_detach)
207 threads_callbacks.thread_detach (info);
209 mono_thread_info_suspend_lock ();
212 Now perform the callback that must be done under locks.
213 This will render the thread useless and non-suspendable, so it must
214 be done while holding the suspend lock to give no other thread chance
215 to suspend it.
217 if (threads_callbacks.thread_unregister)
218 threads_callbacks.thread_unregister (info);
219 mono_threads_unregister_current_thread (info);
221 info->thread_state = STATE_DEAD;
222 mono_thread_info_suspend_unlock ();
224 /*now it's safe to free the thread info.*/
225 mono_thread_hazardous_free_or_queue (info, free_thread_info, TRUE, FALSE);
226 mono_thread_small_id_free (small_id);
229 static void
230 thread_exited_dtor (void *arg)
232 #if defined(__MACH__)
234 * Since we use pthread dtors to clean up thread data, if a thread
235 * is attached to the runtime by another pthread dtor after our dtor
236 * has ran, it will never be detached, leading to various problems
237 * since the thread ids etc. will be reused while they are still in
238 * the threads hashtables etc.
239 * Dtors are called in a loop until all user tls entries are 0,
240 * but the loop has a maximum count (4), so if we set the tls
241 * variable every time, it will remain set when system tls dtors
242 * are ran. This allows mono_thread_info_is_exiting () to detect
243 * whenever the thread is exiting, even if it is executed from a
244 * system tls dtor (i.e. obj-c dealloc methods).
246 mono_native_tls_set_value (thread_exited_key, GUINT_TO_POINTER (1));
247 #endif
251 * Removes the current thread from the thread list.
252 * This must be called from the thread unregister callback and nowhere else.
253 * The current thread must be passed as TLS might have already been cleaned up.
255 static void
256 mono_threads_unregister_current_thread (MonoThreadInfo *info)
258 gboolean result;
259 g_assert (mono_thread_info_get_tid (info) == mono_native_thread_id_get ());
260 result = mono_thread_info_remove (info);
261 g_assert (result);
264 MonoThreadInfo*
265 mono_thread_info_current (void)
267 MonoThreadInfo *info = (MonoThreadInfo*)mono_native_tls_get_value (thread_info_key);
268 if (info)
269 return info;
271 info = mono_thread_info_lookup (mono_native_thread_id_get ()); /*info on HP1*/
274 We might be called during thread cleanup, but we cannot be called after cleanup as happened.
275 The way to distinguish between before, during and after cleanup is the following:
277 -If the TLS key is set, cleanup has not begun;
278 -If the TLS key is clean, but the thread remains registered, cleanup is in progress;
279 -If the thread is nowhere to be found, cleanup has finished.
281 We cannot function after cleanup since there's no way to ensure what will happen.
283 g_assert (info);
285 /*We're looking up the current thread which will not be freed until we finish running, so no need to keep it on a HP */
286 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
288 return info;
292 mono_thread_info_get_small_id (void)
294 #ifdef HAVE_KW_THREAD
295 return tls_small_id;
296 #else
297 gpointer val = mono_native_tls_get_value (small_id_key);
298 if (!val)
299 return -1;
300 return GPOINTER_TO_INT (val) - 1;
301 #endif
304 MonoLinkedListSet*
305 mono_thread_info_list_head (void)
307 return &thread_list;
311 * mono_threads_attach_tools_thread
313 * Attach the current thread as a tool thread. DON'T USE THIS FUNCTION WITHOUT READING ALL DISCLAIMERS.
315 * A tools thread is a very special kind of thread that needs access to core runtime facilities but should
316 * not be counted as a regular thread for high order facilities such as executing managed code or accessing
317 * the managed heap.
319 * This is intended only to tools such as a profiler than needs to be able to use our lock-free support when
320 * doing things like resolving backtraces in their background processing thread.
322 void
323 mono_threads_attach_tools_thread (void)
325 int dummy = 0;
326 MonoThreadInfo *info;
328 /* Must only be called once */
329 g_assert (!mono_native_tls_get_value (thread_info_key));
331 info = mono_thread_info_attach (&dummy);
332 info->tools_thread = TRUE;
335 MonoThreadInfo*
336 mono_thread_info_attach (void *baseptr)
338 MonoThreadInfo *info;
339 if (!mono_threads_inited)
341 /* This can happen from DllMain(DLL_THREAD_ATTACH) on Windows, if a
342 * thread is created before an embedding API user initialized Mono. */
343 THREADS_DEBUG ("mono_thread_info_attach called before mono_threads_init\n");
344 return NULL;
346 info = mono_native_tls_get_value (thread_info_key);
347 if (!info) {
348 info = g_malloc0 (thread_info_size);
349 THREADS_DEBUG ("attaching %p\n", info);
350 if (!register_thread (info, baseptr))
351 return NULL;
352 } else if (threads_callbacks.thread_attach) {
353 threads_callbacks.thread_attach (info);
355 return info;
358 void
359 mono_thread_info_detach (void)
361 MonoThreadInfo *info;
362 if (!mono_threads_inited)
364 /* This can happen from DllMain(THREAD_DETACH) on Windows, if a thread
365 * is created before an embedding API user initialized Mono. */
366 THREADS_DEBUG ("mono_thread_info_detach called before mono_threads_init\n");
367 return;
369 info = mono_native_tls_get_value (thread_info_key);
370 if (info) {
371 THREADS_DEBUG ("detaching %p\n", info);
372 unregister_thread (info);
373 mono_native_tls_set_value (thread_info_key, NULL);
378 * mono_thread_info_is_exiting:
380 * Return whenever the current thread is exiting, i.e. it is running pthread
381 * dtors.
383 gboolean
384 mono_thread_info_is_exiting (void)
386 #if defined(__MACH__)
387 if (mono_native_tls_get_value (thread_exited_key) == GUINT_TO_POINTER (1))
388 return TRUE;
389 #endif
390 return FALSE;
393 void
394 mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
396 gboolean res;
397 threads_callbacks = *callbacks;
398 thread_info_size = info_size;
399 #ifdef HOST_WIN32
400 res = mono_native_tls_alloc (&thread_info_key, NULL);
401 res = mono_native_tls_alloc (&thread_exited_key, NULL);
402 #else
403 res = mono_native_tls_alloc (&thread_info_key, unregister_thread);
404 res = mono_native_tls_alloc (&thread_exited_key, thread_exited_dtor);
405 #endif
406 g_assert (res);
408 #ifndef HAVE_KW_THREAD
409 res = mono_native_tls_alloc (&small_id_key, NULL);
410 #endif
411 g_assert (res);
413 MONO_SEM_INIT (&global_suspend_semaphore, 1);
415 mono_lls_init (&thread_list, NULL);
416 mono_thread_smr_init ();
417 mono_threads_init_platform ();
419 #if defined(__MACH__)
420 mono_mach_init (thread_info_key);
421 #endif
423 mono_threads_inited = TRUE;
425 g_assert (sizeof (MonoNativeThreadId) <= sizeof (uintptr_t));
428 void
429 mono_threads_runtime_init (MonoThreadInfoRuntimeCallbacks *callbacks)
431 runtime_callbacks = *callbacks;
434 MonoThreadInfoCallbacks *
435 mono_threads_get_callbacks (void)
437 return &threads_callbacks;
440 MonoThreadInfoRuntimeCallbacks *
441 mono_threads_get_runtime_callbacks (void)
443 return &runtime_callbacks;
447 The return value is only valid until a matching mono_thread_info_resume is called
449 static MonoThreadInfo*
450 mono_thread_info_suspend_sync (MonoNativeThreadId tid, gboolean interrupt_kernel, const char **error_condition)
452 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
453 MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
454 if (!info) {
455 *error_condition = "Thread not found";
456 return NULL;
459 MONO_SEM_WAIT_UNITERRUPTIBLE (&info->suspend_semaphore);
461 /*thread is on the process of detaching*/
462 if (mono_thread_info_run_state (info) > STATE_RUNNING) {
463 mono_hazard_pointer_clear (hp, 1);
464 *error_condition = "Thread is detaching";
465 return NULL;
468 THREADS_DEBUG ("suspend %x IN COUNT %d\n", tid, info->suspend_count);
470 if (info->suspend_count) {
471 ++info->suspend_count;
472 mono_hazard_pointer_clear (hp, 1);
473 MONO_SEM_POST (&info->suspend_semaphore);
474 return info;
477 if (!mono_threads_core_suspend (info, interrupt_kernel)) {
478 MONO_SEM_POST (&info->suspend_semaphore);
479 mono_hazard_pointer_clear (hp, 1);
480 *error_condition = "Could not suspend thread";
481 return NULL;
484 if (interrupt_kernel)
485 mono_threads_core_interrupt (info);
487 ++info->suspend_count;
488 info->thread_state |= STATE_SUSPENDED;
489 MONO_SEM_POST (&info->suspend_semaphore);
491 return info;
494 void
495 mono_thread_info_self_suspend (void)
497 gboolean ret;
498 MonoThreadInfo *info = mono_thread_info_current ();
499 if (!info)
500 return;
502 MONO_SEM_WAIT_UNITERRUPTIBLE (&info->suspend_semaphore);
504 THREADS_DEBUG ("self suspend IN COUNT %d\n", info->suspend_count);
506 g_assert (info->suspend_count == 0);
507 ++info->suspend_count;
509 info->thread_state |= STATE_SELF_SUSPENDED;
511 ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&info->suspend_state, NULL);
512 g_assert (ret);
514 MONO_SEM_POST (&info->suspend_semaphore);
516 MONO_SEM_WAIT_UNITERRUPTIBLE (&info->resume_semaphore);
518 g_assert (!info->async_target); /*FIXME this should happen normally for suspend. */
519 MONO_SEM_POST (&info->finish_resume_semaphore);
522 static gboolean
523 mono_thread_info_core_resume (MonoThreadInfo *info)
525 gboolean result;
526 MonoNativeThreadId tid = mono_thread_info_get_tid (info);
527 if (info->create_suspended) {
528 /* Have to special case this, as the normal suspend/resume pair are racy, they don't work if he resume is received before the suspend */
529 info->create_suspended = FALSE;
530 mono_threads_core_resume_created (info, tid);
531 return TRUE;
534 MONO_SEM_WAIT_UNITERRUPTIBLE (&info->suspend_semaphore);
536 THREADS_DEBUG ("resume %x IN COUNT %d\n", tid, info->suspend_count);
538 if (info->suspend_count <= 0) {
539 MONO_SEM_POST (&info->suspend_semaphore);
540 return FALSE;
544 * The theory here is that if we manage to suspend the thread it means it did not
545 * start cleanup since it take the same lock.
547 g_assert (mono_thread_info_get_tid (info));
549 if (--info->suspend_count == 0) {
550 if (mono_thread_info_suspend_state (info) == STATE_SELF_SUSPENDED) {
551 MONO_SEM_POST (&info->resume_semaphore);
552 MONO_SEM_WAIT_UNITERRUPTIBLE (&info->finish_resume_semaphore);
553 result = TRUE;
554 } else {
555 result = mono_threads_core_resume (info);
557 info->thread_state &= ~SUSPEND_STATE_MASK;
558 } else {
559 result = TRUE;
562 MONO_SEM_POST (&info->suspend_semaphore);
563 return result;
566 gboolean
567 mono_thread_info_resume (MonoNativeThreadId tid)
569 gboolean result; /* don't initialize it so the compiler can catch unitilized paths. */
570 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
571 MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
573 if (!info) {
574 result = FALSE;
575 goto cleanup;
577 result = mono_thread_info_core_resume (info);
579 cleanup:
580 mono_hazard_pointer_clear (hp, 1);
581 return result;
584 void
585 mono_thread_info_finish_suspend (MonoThreadInfo *info)
587 mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, FALSE);
590 void
591 mono_thread_info_finish_suspend_and_resume (MonoThreadInfo *info)
593 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
595 /*Resume can access info after the target has resumed, so we must ensure it won't touch freed memory. */
596 mono_hazard_pointer_set (hp, 1, info);
597 mono_thread_info_core_resume (info);
598 mono_hazard_pointer_clear (hp, 1);
600 mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, FALSE);
604 FIXME fix cardtable WB to be out of line and check with the runtime if the target is not the
605 WB trampoline. Another option is to encode wb ranges in MonoJitInfo, but that is somewhat hard.
607 static gboolean
608 is_thread_in_critical_region (MonoThreadInfo *info)
610 MonoMethod *method;
611 MonoJitInfo *ji;
613 if (info->inside_critical_region)
614 return TRUE;
616 /* The target thread might be shutting down and the domain might be null, which means no managed code left to run. */
617 if (!info->suspend_state.unwind_data [MONO_UNWIND_DATA_DOMAIN])
618 return FALSE;
620 ji = mono_jit_info_table_find (
621 info->suspend_state.unwind_data [MONO_UNWIND_DATA_DOMAIN],
622 MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
624 if (!ji)
625 return FALSE;
627 method = mono_jit_info_get_method (ji);
629 return threads_callbacks.mono_method_is_critical (method);
633 WARNING:
634 If we are trying to suspend a target that is on a critical region
635 and running a syscall we risk looping forever if @interrupt_kernel is FALSE.
636 So, be VERY carefull in calling this with @interrupt_kernel == FALSE.
638 Info is not put on a hazard pointer as a suspended thread cannot exit and be freed.
640 This function MUST be matched with mono_thread_info_finish_suspend or mono_thread_info_finish_suspend_and_resume
642 MonoThreadInfo*
643 mono_thread_info_safe_suspend_sync (MonoNativeThreadId id, gboolean interrupt_kernel)
645 MonoThreadInfo *info = NULL;
646 int sleep_duration = 0;
648 /*FIXME: unify this with self-suspend*/
649 g_assert (id != mono_native_thread_id_get ());
651 mono_thread_info_suspend_lock ();
653 for (;;) {
654 const char *suspend_error = "Unknown error";
655 if (!(info = mono_thread_info_suspend_sync (id, interrupt_kernel, &suspend_error))) {
656 // g_warning ("failed to suspend thread %p due to %s, hopefully it is dead", (gpointer)id, suspend_error);
657 mono_thread_info_suspend_unlock ();
658 return NULL;
660 /*WARNING: We now are in interrupt context until we resume the thread. */
661 if (!is_thread_in_critical_region (info))
662 break;
664 if (!mono_thread_info_core_resume (info)) {
665 // g_warning ("failed to resume thread %p, hopefully it is dead", (gpointer)id);
666 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
667 mono_thread_info_suspend_unlock ();
668 return NULL;
670 THREADS_DEBUG ("restarted thread %p\n", (gpointer)id);
672 if (!sleep_duration) {
673 #ifdef HOST_WIN32
674 SwitchToThread ();
675 #else
676 sched_yield ();
677 #endif
679 else {
680 g_usleep (sleep_duration);
682 sleep_duration += 10;
685 /* XXX this clears HP 1, so we restated it again */
686 mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, TRUE);
687 mono_thread_info_suspend_unlock ();
689 return info;
693 Inject an assynchronous call into the target thread. The target thread must be suspended and
694 only a single async call can be setup for a given suspend cycle.
695 This async call must cause stack unwinding as the current implementation doesn't save enough state
696 to resume execution of the top-of-stack function. It's an acceptable limitation since this is
697 currently used only to deliver exceptions.
699 void
700 mono_thread_info_setup_async_call (MonoThreadInfo *info, void (*target_func)(void*), void *user_data)
702 g_assert (info->suspend_count);
703 /*FIXME this is a bad assert, we probably should do proper locking and fail if one is already set*/
704 g_assert (!info->async_target);
705 info->async_target = target_func;
706 /* This is not GC tracked */
707 info->user_data = user_data;
711 The suspend lock is held during any suspend in progress.
712 A GC that has safepoints must take this lock as part of its
713 STW to make sure no unsafe pending suspend is in progress.
715 void
716 mono_thread_info_suspend_lock (void)
718 MONO_SEM_WAIT_UNITERRUPTIBLE (&global_suspend_semaphore);
721 void
722 mono_thread_info_suspend_unlock (void)
724 MONO_SEM_POST (&global_suspend_semaphore);
727 void
728 mono_thread_info_disable_new_interrupt (gboolean disable)
730 disable_new_interrupt = disable;
734 * This is a very specific function whose only purpose is to
735 * break a given thread from socket syscalls.
737 * This only exists because linux won't fail a call to connect
738 * if the underlying is closed.
740 * TODO We should cleanup and unify this with the other syscall abort
741 * facility.
743 void
744 mono_thread_info_abort_socket_syscall_for_close (MonoNativeThreadId tid)
746 MonoThreadHazardPointers *hp;
747 MonoThreadInfo *info;
749 if (tid == mono_native_thread_id_get () || !mono_threads_core_needs_abort_syscall ())
750 return;
752 hp = mono_hazard_pointer_get ();
753 info = mono_thread_info_lookup (tid); /*info on HP1*/
754 if (!info)
755 return;
757 if (mono_thread_info_run_state (info) > STATE_RUNNING) {
758 mono_hazard_pointer_clear (hp, 1);
759 return;
762 mono_thread_info_suspend_lock ();
764 mono_threads_core_abort_syscall (info);
766 mono_hazard_pointer_clear (hp, 1);
767 mono_thread_info_suspend_unlock ();
771 Disabled by default for now.
772 To enable this we need mini to implement the callbacks by MonoThreadInfoRuntimeCallbacks
773 which means mono-context and setup_async_callback, and we need a mono-threads backend.
775 gboolean
776 mono_thread_info_new_interrupt_enabled (void)
778 /*We need STW gc events to work correctly*/
779 #if defined (HAVE_BOEHM_GC) && !defined (USE_INCLUDED_LIBGC)
780 return FALSE;
781 #endif
782 #if defined(HOST_WIN32)
783 return !disable_new_interrupt;
784 #endif
785 #if defined (__i386__) || defined(__x86_64__)
786 return !disable_new_interrupt;
787 #endif
788 #if defined(__arm__) || defined(__aarch64__)
789 return !disable_new_interrupt;
790 #endif
791 return FALSE;
795 * mono_thread_info_set_is_async_context:
797 * Set whenever the current thread is in an async context. Some runtime functions might behave
798 * differently while in an async context in order to be async safe.
800 void
801 mono_thread_info_set_is_async_context (gboolean async_context)
803 MonoThreadInfo *info = mono_thread_info_current ();
805 if (info)
806 info->is_async_context = async_context;
809 gboolean
810 mono_thread_info_is_async_context (void)
812 MonoThreadInfo *info = mono_thread_info_current ();
814 if (info)
815 return info->is_async_context;
816 else
817 return FALSE;
821 * mono_threads_create_thread:
823 * Create a new thread executing START with argument ARG. Store its id into OUT_TID.
824 * Returns: a windows or io-layer handle for the thread.
826 HANDLE
827 mono_threads_create_thread (LPTHREAD_START_ROUTINE start, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
829 return mono_threads_core_create_thread (start, arg, stack_size, creation_flags, out_tid);
833 * mono_thread_info_get_stack_bounds:
835 * Return the address and size of the current threads stack. Return NULL as the
836 * stack address if the stack address cannot be determined.
838 void
839 mono_thread_info_get_stack_bounds (guint8 **staddr, size_t *stsize)
841 guint8 *current = (guint8 *)&stsize;
842 mono_threads_core_get_stack_bounds (staddr, stsize);
843 if (!*staddr)
844 return;
846 /* Sanity check the result */
847 g_assert ((current > *staddr) && (current < *staddr + *stsize));
849 /* When running under emacs, sometimes staddr is not aligned to a page size */
850 *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
853 gboolean
854 mono_thread_info_yield (void)
856 return mono_threads_core_yield ();
859 gpointer
860 mono_thread_info_tls_get (THREAD_INFO_TYPE *info, MonoTlsKey key)
862 return ((MonoThreadInfo*)info)->tls [key];
866 * mono_threads_info_tls_set:
868 * Set the TLS key to VALUE in the info structure. This can be used to obtain
869 * values of TLS variables for threads other than the current thread.
870 * This should only be used for infrequently changing TLS variables, and it should
871 * be paired with setting the real TLS variable since this provides no GC tracking.
873 void
874 mono_thread_info_tls_set (THREAD_INFO_TYPE *info, MonoTlsKey key, gpointer value)
876 ((MonoThreadInfo*)info)->tls [key] = value;
880 * mono_thread_info_exit:
882 * Exit the current thread.
883 * This function doesn't return.
885 void
886 mono_thread_info_exit (void)
888 mono_threads_core_exit (0);
892 * mono_thread_info_open_handle:
894 * Return a io-layer/win32 handle for the current thread.
895 * The handle need to be closed by calling CloseHandle () when it is no
896 * longer needed.
898 HANDLE
899 mono_thread_info_open_handle (void)
901 return mono_threads_core_open_handle ();
905 * mono_threads_open_thread_handle:
907 * Return a io-layer/win32 handle for the thread identified by HANDLE/TID.
908 * The handle need to be closed by calling CloseHandle () when it is no
909 * longer needed.
911 HANDLE
912 mono_threads_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
914 return mono_threads_core_open_thread_handle (handle, tid);
917 void
918 mono_thread_info_set_name (MonoNativeThreadId tid, const char *name)
920 mono_threads_core_set_name (tid, name);
924 * mono_thread_info_prepare_interrupt:
926 * See wapi_prepare_interrupt ().
928 gpointer
929 mono_thread_info_prepare_interrupt (HANDLE thread_handle)
931 return mono_threads_core_prepare_interrupt (thread_handle);
934 void
935 mono_thread_info_finish_interrupt (gpointer wait_handle)
937 mono_threads_core_finish_interrupt (wait_handle);
940 void
941 mono_thread_info_interrupt (HANDLE thread_handle)
943 gpointer wait_handle;
945 wait_handle = mono_thread_info_prepare_interrupt (thread_handle);
946 mono_thread_info_finish_interrupt (wait_handle);
949 void
950 mono_thread_info_self_interrupt (void)
952 mono_threads_core_self_interrupt ();
955 void
956 mono_thread_info_clear_interruption (void)
958 mono_threads_core_clear_interruption ();
961 /* info must be self or be held in a hazard pointer. */
962 gboolean
963 mono_threads_add_async_job (MonoThreadInfo *info, MonoAsyncJob job)
965 MonoAsyncJob old_job;
966 do {
967 old_job = info->service_requests;
968 if (old_job & job)
969 return FALSE;
970 } while (InterlockedCompareExchange (&info->service_requests, old_job | job, old_job) != old_job);
971 return TRUE;
974 MonoAsyncJob
975 mono_threads_consume_async_jobs (void)
977 MonoThreadInfo *info = (MonoThreadInfo*)mono_native_tls_get_value (thread_info_key);
979 if (!info)
980 return 0;
982 return InterlockedExchange (&info->service_requests, 0);
987 * Return TRUE is the thread is in an usable (suspendable) state
989 gboolean
990 mono_thread_info_is_live (MonoThreadInfo *info)
992 return mono_thread_info_run_state (info) == STATE_RUNNING;