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)
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>
25 #include <mono/utils/mach-support.h>
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
;
50 static __thread guint32 tls_small_id MONO_TLS_FAST
;
52 static MonoNativeTlsKey small_id_key
;
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)
65 mono_hazard_pointer_clear_all (MonoThreadHazardPointers
*hp
, int retain
)
68 mono_hazard_pointer_clear (hp
, 0);
70 mono_hazard_pointer_clear (hp
, 1);
72 mono_hazard_pointer_clear (hp
, 2);
76 If return non null Hazard Pointer 1 holds the return value.
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);
88 mono_hazard_pointer_clear_all (hp
, 1);
89 return mono_hazard_pointer_get_val (hp
, 1);
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);
102 mono_hazard_pointer_clear_all (hp
, -1);
107 mono_thread_info_remove (MonoThreadInfo
*info
)
109 MonoThreadHazardPointers
*hp
= mono_hazard_pointer_get ();
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);
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
);
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
;
138 mono_native_tls_set_value (small_id_key
, GUINT_TO_POINTER (small_id
+ 1));
144 register_thread (MonoThreadInfo
*info
, gpointer baseptr
)
146 int small_id
= mono_thread_info_register_small_id ();
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");
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
);
174 mono_thread_info_suspend_unlock ();
179 unregister_thread (void *arg
)
181 MonoThreadInfo
*info
= arg
;
182 int small_id
= info
->small_id
;
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
195 #ifndef HAVE_KW_THREAD
196 mono_native_tls_set_value (small_id_key
, GUINT_TO_POINTER (info
->small_id
+ 1));
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
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
);
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));
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.
256 mono_threads_unregister_current_thread (MonoThreadInfo
*info
)
259 g_assert (mono_thread_info_get_tid (info
) == mono_native_thread_id_get ());
260 result
= mono_thread_info_remove (info
);
265 mono_thread_info_current (void)
267 MonoThreadInfo
*info
= (MonoThreadInfo
*)mono_native_tls_get_value (thread_info_key
);
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.
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);
292 mono_thread_info_get_small_id (void)
294 #ifdef HAVE_KW_THREAD
297 gpointer val
= mono_native_tls_get_value (small_id_key
);
300 return GPOINTER_TO_INT (val
) - 1;
305 mono_thread_info_list_head (void)
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
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.
323 mono_threads_attach_tools_thread (void)
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
;
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");
346 info
= mono_native_tls_get_value (thread_info_key
);
348 info
= g_malloc0 (thread_info_size
);
349 THREADS_DEBUG ("attaching %p\n", info
);
350 if (!register_thread (info
, baseptr
))
352 } else if (threads_callbacks
.thread_attach
) {
353 threads_callbacks
.thread_attach (info
);
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");
369 info
= mono_native_tls_get_value (thread_info_key
);
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
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))
394 mono_threads_init (MonoThreadInfoCallbacks
*callbacks
, size_t info_size
)
397 threads_callbacks
= *callbacks
;
398 thread_info_size
= info_size
;
400 res
= mono_native_tls_alloc (&thread_info_key
, NULL
);
401 res
= mono_native_tls_alloc (&thread_exited_key
, NULL
);
403 res
= mono_native_tls_alloc (&thread_info_key
, unregister_thread
);
404 res
= mono_native_tls_alloc (&thread_exited_key
, thread_exited_dtor
);
408 #ifndef HAVE_KW_THREAD
409 res
= mono_native_tls_alloc (&small_id_key
, NULL
);
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
);
423 mono_threads_inited
= TRUE
;
425 g_assert (sizeof (MonoNativeThreadId
) <= sizeof (uintptr_t));
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*/
455 *error_condition
= "Thread not found";
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";
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
);
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";
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
);
495 mono_thread_info_self_suspend (void)
498 MonoThreadInfo
*info
= mono_thread_info_current ();
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
);
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
);
523 mono_thread_info_core_resume (MonoThreadInfo
*info
)
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
);
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
);
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
);
555 result
= mono_threads_core_resume (info
);
557 info
->thread_state
&= ~SUSPEND_STATE_MASK
;
562 MONO_SEM_POST (&info
->suspend_semaphore
);
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*/
577 result
= mono_thread_info_core_resume (info
);
580 mono_hazard_pointer_clear (hp
, 1);
585 mono_thread_info_finish_suspend (MonoThreadInfo
*info
)
587 mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region
, FALSE
);
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.
608 is_thread_in_critical_region (MonoThreadInfo
*info
)
613 if (info
->inside_critical_region
)
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
])
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
));
627 method
= mono_jit_info_get_method (ji
);
629 return threads_callbacks
.mono_method_is_critical (method
);
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
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 ();
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 ();
660 /*WARNING: We now are in interrupt context until we resume the thread. */
661 if (!is_thread_in_critical_region (info
))
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 ();
670 THREADS_DEBUG ("restarted thread %p\n", (gpointer
)id
);
672 if (!sleep_duration
) {
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 ();
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.
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.
716 mono_thread_info_suspend_lock (void)
718 MONO_SEM_WAIT_UNITERRUPTIBLE (&global_suspend_semaphore
);
722 mono_thread_info_suspend_unlock (void)
724 MONO_SEM_POST (&global_suspend_semaphore
);
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
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 ())
752 hp
= mono_hazard_pointer_get ();
753 info
= mono_thread_info_lookup (tid
); /*info on HP1*/
757 if (mono_thread_info_run_state (info
) > STATE_RUNNING
) {
758 mono_hazard_pointer_clear (hp
, 1);
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.
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)
782 #if defined(HOST_WIN32)
783 return !disable_new_interrupt
;
785 #if defined (__i386__) || defined(__x86_64__)
786 return !disable_new_interrupt
;
788 #if defined(__arm__) || defined(__aarch64__)
789 return !disable_new_interrupt
;
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.
801 mono_thread_info_set_is_async_context (gboolean async_context
)
803 MonoThreadInfo
*info
= mono_thread_info_current ();
806 info
->is_async_context
= async_context
;
810 mono_thread_info_is_async_context (void)
812 MonoThreadInfo
*info
= mono_thread_info_current ();
815 return info
->is_async_context
;
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.
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.
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
);
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));
854 mono_thread_info_yield (void)
856 return mono_threads_core_yield ();
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.
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.
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
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
912 mono_threads_open_thread_handle (HANDLE handle
, MonoNativeThreadId tid
)
914 return mono_threads_core_open_thread_handle (handle
, tid
);
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 ().
929 mono_thread_info_prepare_interrupt (HANDLE thread_handle
)
931 return mono_threads_core_prepare_interrupt (thread_handle
);
935 mono_thread_info_finish_interrupt (gpointer wait_handle
)
937 mono_threads_core_finish_interrupt (wait_handle
);
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
);
950 mono_thread_info_self_interrupt (void)
952 mono_threads_core_self_interrupt ();
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. */
963 mono_threads_add_async_job (MonoThreadInfo
*info
, MonoAsyncJob job
)
965 MonoAsyncJob old_job
;
967 old_job
= info
->service_requests
;
970 } while (InterlockedCompareExchange (&info
->service_requests
, old_job
| job
, old_job
) != old_job
);
975 mono_threads_consume_async_jobs (void)
977 MonoThreadInfo
*info
= (MonoThreadInfo
*)mono_native_tls_get_value (thread_info_key
);
982 return InterlockedExchange (&info
->service_requests
, 0);
987 * Return TRUE is the thread is in an usable (suspendable) state
990 mono_thread_info_is_live (MonoThreadInfo
*info
)
992 return mono_thread_info_run_state (info
) == STATE_RUNNING
;