2 * threads.c: Thread support internal calls
5 * Dick Porter (dick@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
20 #if defined(__OpenBSD__) || defined(__FreeBSD__)
22 #include <pthread_np.h>
25 #include <mono/metadata/object.h>
26 #include <mono/metadata/domain-internals.h>
27 #include <mono/metadata/profiler-private.h>
28 #include <mono/metadata/threads.h>
29 #include <mono/metadata/threadpool.h>
30 #include <mono/metadata/threads-types.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/environment.h>
33 #include <mono/metadata/monitor.h>
34 #include <mono/metadata/gc-internal.h>
35 #include <mono/metadata/marshal.h>
36 #include <mono/metadata/runtime.h>
37 #include <mono/io-layer/io-layer.h>
39 #include <mono/io-layer/threads.h>
41 #include <mono/metadata/object-internals.h>
42 #include <mono/metadata/mono-debug-debugger.h>
43 #include <mono/utils/mono-compiler.h>
44 #include <mono/utils/mono-mmap.h>
45 #include <mono/utils/mono-membar.h>
46 #include <mono/utils/mono-time.h>
47 #include <mono/utils/mono-threads.h>
48 #include <mono/utils/hazard-pointer.h>
49 #include <mono/utils/mono-tls.h>
50 #include <mono/utils/atomic.h>
52 #include <mono/metadata/gc-internal.h>
54 #ifdef PLATFORM_ANDROID
57 extern int tkill (pid_t tid, int signal);
60 #if defined(PLATFORM_MACOSX) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
61 void *pthread_get_stackaddr_np(pthread_t);
62 size_t pthread_get_stacksize_np(pthread_t);
65 /*#define THREAD_DEBUG(a) do { a; } while (0)*/
66 #define THREAD_DEBUG(a)
67 /*#define THREAD_WAIT_DEBUG(a) do { a; } while (0)*/
68 #define THREAD_WAIT_DEBUG(a)
69 /*#define LIBGC_DEBUG(a) do { a; } while (0)*/
70 #define LIBGC_DEBUG(a)
72 #define SPIN_TRYLOCK(i) (InterlockedCompareExchange (&(i), 1, 0) == 0)
73 #define SPIN_LOCK(i) do { \
74 if (SPIN_TRYLOCK (i)) \
78 #define SPIN_UNLOCK(i) i = 0
80 /* Provide this for systems with glib < 2.6 */
81 #ifndef G_GSIZE_FORMAT
82 # if GLIB_SIZEOF_LONG == 8
83 # define G_GSIZE_FORMAT "lu"
85 # define G_GSIZE_FORMAT "u"
91 guint32 (*func)(void *);
107 typedef struct _MonoThreadDomainTls MonoThreadDomainTls;
108 struct _MonoThreadDomainTls {
109 MonoThreadDomainTls *next;
117 MonoThreadDomainTls *freelist;
120 /* Number of cached culture objects in the MonoThread->cached_culture_info array
121 * (per-type): we use the first NUM entries for CultureInfo and the last for
122 * UICultureInfo. So the size of the array is really NUM_CACHED_CULTURES * 2.
124 #define NUM_CACHED_CULTURES 4
125 #define CULTURES_START_IDX 0
126 #define UICULTURES_START_IDX NUM_CACHED_CULTURES
128 /* Controls access to the 'threads' hash table */
129 #define mono_threads_lock() EnterCriticalSection (&threads_mutex)
130 #define mono_threads_unlock() LeaveCriticalSection (&threads_mutex)
131 static CRITICAL_SECTION threads_mutex;
133 /* Controls access to context static data */
134 #define mono_contexts_lock() EnterCriticalSection (&contexts_mutex)
135 #define mono_contexts_unlock() LeaveCriticalSection (&contexts_mutex)
136 static CRITICAL_SECTION contexts_mutex;
138 /* Holds current status of static data heap */
139 static StaticDataInfo thread_static_info;
140 static StaticDataInfo context_static_info;
142 /* The hash of existing threads (key is thread ID, value is
143 * MonoInternalThread*) that need joining before exit
145 static MonoGHashTable *threads=NULL;
148 * Threads which are starting up and they are not in the 'threads' hash yet.
149 * When handle_store is called for a thread, it will be removed from this hash table.
150 * Protected by mono_threads_lock ().
152 static MonoGHashTable *threads_starting_up = NULL;
154 /* Maps a MonoThread to its start argument */
155 /* Protected by mono_threads_lock () */
156 static MonoGHashTable *thread_start_args = NULL;
158 /* The TLS key that holds the MonoObject assigned to each thread */
159 static MonoNativeTlsKey current_object_key;
161 #ifdef MONO_HAVE_FAST_TLS
162 /* we need to use both the Tls* functions and __thread because
163 * the gc needs to see all the threads
165 MONO_FAST_TLS_DECLARE(tls_current_object);
166 #define SET_CURRENT_OBJECT(x) do { \
167 MONO_FAST_TLS_SET (tls_current_object, x); \
168 mono_native_tls_set_value (current_object_key, x); \
170 #define GET_CURRENT_OBJECT() ((MonoInternalThread*) MONO_FAST_TLS_GET (tls_current_object))
172 #define SET_CURRENT_OBJECT(x) mono_native_tls_set_value (current_object_key, x)
173 #define GET_CURRENT_OBJECT() (MonoInternalThread*) mono_native_tls_get_value (current_object_key)
176 /* function called at thread start */
177 static MonoThreadStartCB mono_thread_start_cb = NULL;
179 /* function called at thread attach */
180 static MonoThreadAttachCB mono_thread_attach_cb = NULL;
182 /* function called at thread cleanup */
183 static MonoThreadCleanupFunc mono_thread_cleanup_fn = NULL;
185 /* function called to notify the runtime about a pending exception on the current thread */
186 static MonoThreadNotifyPendingExcFunc mono_thread_notify_pending_exc_fn = NULL;
188 /* The default stack size for each thread */
189 static guint32 default_stacksize = 0;
190 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
192 static void thread_adjust_static_data (MonoInternalThread *thread);
193 static void mono_free_static_data (gpointer* static_data, gboolean threadlocal);
194 static void mono_init_static_data_info (StaticDataInfo *static_data);
195 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
196 static gboolean mono_thread_resume (MonoInternalThread* thread);
197 static void mono_thread_start (MonoThread *thread);
198 static void signal_thread_state_change (MonoInternalThread *thread);
199 static void abort_thread_internal (MonoInternalThread *thread, gboolean can_raise_exception, gboolean install_async_abort);
200 static void suspend_thread_internal (MonoInternalThread *thread, gboolean interrupt);
201 static void self_suspend_internal (MonoInternalThread *thread);
202 static gboolean resume_thread_internal (MonoInternalThread *thread);
204 static MonoException* mono_thread_execute_interruption (MonoInternalThread *thread);
205 static void ref_stack_destroy (gpointer rs);
207 /* Spin lock for InterlockedXXX 64 bit functions */
208 #define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
209 #define mono_interlocked_unlock() LeaveCriticalSection (&interlocked_mutex)
210 static CRITICAL_SECTION interlocked_mutex;
212 /* global count of thread interruptions requested */
213 static gint32 thread_interruption_requested = 0;
215 /* Event signaled when a thread changes its background mode */
216 static HANDLE background_change_event;
218 static gboolean shutting_down = FALSE;
220 static gint32 managed_thread_id_counter = 0;
223 get_next_managed_thread_id (void)
225 return InterlockedIncrement (&managed_thread_id_counter);
229 mono_thread_get_tls_key (void)
231 return current_object_key;
235 mono_thread_get_tls_offset (void)
238 MONO_THREAD_VAR_OFFSET (tls_current_object,offset);
242 /* handle_store() and handle_remove() manage the array of threads that
243 * still need to be waited for when the main thread exits.
245 * If handle_store() returns FALSE the thread must not be started
246 * because Mono is shutting down.
248 static gboolean handle_store(MonoThread *thread, gboolean force_attach)
250 mono_threads_lock ();
252 THREAD_DEBUG (g_message ("%s: thread %p ID %"G_GSIZE_FORMAT, __func__, thread, (gsize)thread->internal_thread->tid));
254 if (threads_starting_up)
255 mono_g_hash_table_remove (threads_starting_up, thread);
257 if (shutting_down && !force_attach) {
258 mono_threads_unlock ();
263 MONO_GC_REGISTER_ROOT_FIXED (threads);
264 threads=mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
267 /* We don't need to duplicate thread->handle, because it is
268 * only closed when the thread object is finalized by the GC.
270 g_assert (thread->internal_thread);
271 mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->internal_thread->tid),
272 thread->internal_thread);
274 mono_threads_unlock ();
279 static gboolean handle_remove(MonoInternalThread *thread)
282 gsize tid = thread->tid;
284 THREAD_DEBUG (g_message ("%s: thread ID %"G_GSIZE_FORMAT, __func__, tid));
286 mono_threads_lock ();
289 /* We have to check whether the thread object for the
290 * tid is still the same in the table because the
291 * thread might have been destroyed and the tid reused
292 * in the meantime, in which case the tid would be in
293 * the table, but with another thread object.
295 if (mono_g_hash_table_lookup (threads, (gpointer)tid) == thread) {
296 mono_g_hash_table_remove (threads, (gpointer)tid);
305 mono_threads_unlock ();
307 /* Don't close the handle here, wait for the object finalizer
308 * to do it. Otherwise, the following race condition applies:
310 * 1) Thread exits (and handle_remove() closes the handle)
312 * 2) Some other handle is reassigned the same slot
314 * 3) Another thread tries to join the first thread, and
315 * blocks waiting for the reassigned handle to be signalled
316 * (which might never happen). This is possible, because the
317 * thread calling Join() still has a reference to the first
323 static void ensure_synch_cs_set (MonoInternalThread *thread)
325 CRITICAL_SECTION *synch_cs;
327 if (thread->synch_cs != NULL) {
331 synch_cs = g_new0 (CRITICAL_SECTION, 1);
332 InitializeCriticalSection (synch_cs);
334 if (InterlockedCompareExchangePointer ((gpointer *)&thread->synch_cs,
335 synch_cs, NULL) != NULL) {
336 /* Another thread must have installed this CS */
337 DeleteCriticalSection (synch_cs);
343 * NOTE: this function can be called also for threads different from the current one:
344 * make sure no code called from it will ever assume it is run on the thread that is
345 * getting cleaned up.
347 static void thread_cleanup (MonoInternalThread *thread)
349 g_assert (thread != NULL);
351 if (thread->abort_state_handle) {
352 mono_gchandle_free (thread->abort_state_handle);
353 thread->abort_state_handle = 0;
355 thread->abort_exc = NULL;
356 thread->current_appcontext = NULL;
359 * This is necessary because otherwise we might have
360 * cross-domain references which will not get cleaned up when
361 * the target domain is unloaded.
363 if (thread->cached_culture_info) {
365 for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i)
366 mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
369 ensure_synch_cs_set (thread);
371 EnterCriticalSection (thread->synch_cs);
373 thread->state |= ThreadState_Stopped;
374 thread->state &= ~ThreadState_Background;
376 LeaveCriticalSection (thread->synch_cs);
379 An interruption request has leaked to cleanup. Adjust the global counter.
381 This can happen is the abort source thread finds the abortee (this) thread
382 in unmanaged code. If this thread never trips back to managed code or check
383 the local flag it will be left set and positively unbalance the global counter.
385 Leaving the counter unbalanced will cause a performance degradation since all threads
386 will now keep checking their local flags all the time.
388 if (InterlockedExchange (&thread->interruption_requested, 0))
389 InterlockedDecrement (&thread_interruption_requested);
391 /* if the thread is not in the hash it has been removed already */
392 if (!handle_remove (thread)) {
393 if (thread == mono_thread_internal_current ()) {
394 mono_domain_unset ();
395 mono_memory_barrier ();
397 /* This needs to be called even if handle_remove () fails */
398 if (mono_thread_cleanup_fn)
399 mono_thread_cleanup_fn (thread);
402 mono_release_type_locks (thread);
404 mono_profiler_thread_end (thread->tid);
406 if (thread == mono_thread_internal_current ()) {
408 * This will signal async signal handlers that the thread has exited.
409 * The profiler callback needs this to be set, so it cannot be done earlier.
411 mono_domain_unset ();
412 mono_memory_barrier ();
415 if (thread == mono_thread_internal_current ())
416 mono_thread_pop_appdomain_ref ();
418 thread->cached_culture_info = NULL;
420 mono_free_static_data (thread->static_data, TRUE);
421 thread->static_data = NULL;
422 ref_stack_destroy (thread->appdomain_refs);
423 thread->appdomain_refs = NULL;
425 if (mono_thread_cleanup_fn)
426 mono_thread_cleanup_fn (thread);
428 if (mono_gc_is_moving ()) {
429 MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
430 thread->thread_pinning_ref = NULL;
436 get_thread_static_data (MonoInternalThread *thread, guint32 offset)
439 g_assert ((offset & 0x80000000) == 0);
440 offset &= 0x7fffffff;
441 idx = (offset >> 24) - 1;
442 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
446 get_current_thread_ptr_for_domain (MonoDomain *domain, MonoInternalThread *thread)
448 static MonoClassField *current_thread_field = NULL;
452 if (!current_thread_field) {
453 current_thread_field = mono_class_get_field_from_name (mono_defaults.thread_class, "current_thread");
454 g_assert (current_thread_field);
457 mono_class_vtable (domain, mono_defaults.thread_class);
458 mono_domain_lock (domain);
459 offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, current_thread_field));
460 mono_domain_unlock (domain);
463 return get_thread_static_data (thread, offset);
467 set_current_thread_for_domain (MonoDomain *domain, MonoInternalThread *thread, MonoThread *current)
469 MonoThread **current_thread_ptr = get_current_thread_ptr_for_domain (domain, thread);
471 g_assert (current->obj.vtable->domain == domain);
473 g_assert (!*current_thread_ptr);
474 *current_thread_ptr = current;
477 static MonoInternalThread*
478 create_internal_thread_object (void)
480 MonoVTable *vt = mono_class_vtable (mono_get_root_domain (), mono_defaults.internal_thread_class);
481 return (MonoInternalThread*)mono_gc_alloc_mature (vt);
485 create_thread_object (MonoDomain *domain)
487 MonoVTable *vt = mono_class_vtable (domain, mono_defaults.thread_class);
488 return (MonoThread*)mono_gc_alloc_mature (vt);
492 new_thread_with_internal (MonoDomain *domain, MonoInternalThread *internal)
494 MonoThread *thread = create_thread_object (domain);
495 MONO_OBJECT_SETREF (thread, internal_thread, internal);
500 init_root_domain_thread (MonoInternalThread *thread, MonoThread *candidate)
502 MonoDomain *domain = mono_get_root_domain ();
504 if (!candidate || candidate->obj.vtable->domain != domain)
505 candidate = new_thread_with_internal (domain, thread);
506 set_current_thread_for_domain (domain, thread, candidate);
507 g_assert (!thread->root_domain_thread);
508 MONO_OBJECT_SETREF (thread, root_domain_thread, candidate);
511 static guint32 WINAPI start_wrapper_internal(void *data)
513 MonoThreadInfo *info;
514 struct StartInfo *start_info=(struct StartInfo *)data;
515 guint32 (*start_func)(void *);
519 * We don't create a local to hold start_info->obj, so hopefully it won't get pinned during a
522 MonoInternalThread *internal = start_info->obj->internal_thread;
523 MonoObject *start_delegate = start_info->delegate;
524 MonoDomain *domain = start_info->obj->obj.vtable->domain;
526 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper", __func__, GetCurrentThreadId ()));
528 /* We can be sure start_info->obj->tid and
529 * start_info->obj->handle have been set, because the thread
530 * was created suspended, and these values were set before the
534 info = mono_thread_info_current ();
536 internal->thread_info = info;
541 SET_CURRENT_OBJECT (internal);
543 mono_monitor_init_tls ();
545 /* Every thread references the appdomain which created it */
546 mono_thread_push_appdomain_ref (domain);
548 if (!mono_domain_set (domain, FALSE)) {
549 /* No point in raising an appdomain_unloaded exception here */
550 /* FIXME: Cleanup here */
551 mono_thread_pop_appdomain_ref ();
555 start_func = start_info->func;
556 start_arg = start_info->start_arg;
558 /* We have to do this here because mono_thread_new_init()
559 requires that root_domain_thread is set up. */
560 thread_adjust_static_data (internal);
561 init_root_domain_thread (internal, start_info->obj);
563 /* This MUST be called before any managed code can be
564 * executed, as it calls the callback function that (for the
565 * jit) sets the lmf marker.
567 mono_thread_new_init (tid, &tid, start_func);
568 internal->stack_ptr = &tid;
570 LIBGC_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT",%d) Setting thread stack to %p", __func__, GetCurrentThreadId (), getpid (), thread->stack_ptr));
572 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), internal));
574 /* On 2.0 profile (and higher), set explicitly since state might have been
576 if (internal->apartment_state == ThreadApartmentState_Unknown)
577 internal->apartment_state = ThreadApartmentState_MTA;
579 mono_thread_init_apartment_state ();
581 if(internal->start_notify!=NULL) {
582 /* Let the thread that called Start() know we're
585 ReleaseSemaphore (internal->start_notify, 1, NULL);
588 mono_threads_lock ();
589 mono_g_hash_table_remove (thread_start_args, start_info->obj);
590 mono_threads_unlock ();
592 mono_thread_set_execution_context (start_info->obj->ec_to_set);
593 start_info->obj->ec_to_set = NULL;
596 THREAD_DEBUG (g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
600 * Call this after calling start_notify, since the profiler callback might want
601 * to lock the thread, and the lock is held by thread_start () which waits for
604 mono_profiler_thread_start (tid);
606 /* start_func is set only for unmanaged start functions */
608 start_func (start_arg);
611 g_assert (start_delegate != NULL);
612 args [0] = start_arg;
613 /* we may want to handle the exception here. See comment below on unhandled exceptions */
614 mono_runtime_delegate_invoke (start_delegate, args, NULL);
617 /* If the thread calls ExitThread at all, this remaining code
618 * will not be executed, but the main thread will eventually
619 * call thread_cleanup() on this thread's behalf.
622 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper terminating", __func__, GetCurrentThreadId ()));
624 /* Do any cleanup needed for apartment state. This
625 * cannot be done in thread_cleanup since thread_cleanup could be
626 * called for a thread other than the current thread.
627 * mono_thread_cleanup_apartment_state cleans up apartment
628 * for the current thead */
629 mono_thread_cleanup_apartment_state ();
631 thread_cleanup (internal);
633 /* Remove the reference to the thread object in the TLS data,
634 * so the thread object can be finalized. This won't be
635 * reached if the thread threw an uncaught exception, so those
636 * thread handles will stay referenced :-( (This is due to
637 * missing support for scanning thread-specific data in the
638 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
641 SET_CURRENT_OBJECT (NULL);
646 static guint32 WINAPI start_wrapper(void *data)
650 /* Avoid scanning the frames above this frame during a GC */
651 mono_gc_set_stack_end ((void*)&dummy);
653 return start_wrapper_internal (data);
656 void mono_thread_new_init (intptr_t tid, gpointer stack_start, gpointer func)
658 if (mono_thread_start_cb) {
659 mono_thread_start_cb (tid, stack_start, func);
663 void mono_threads_set_default_stacksize (guint32 stacksize)
665 default_stacksize = stacksize;
668 guint32 mono_threads_get_default_stacksize (void)
670 return default_stacksize;
674 * mono_create_thread:
676 * This is a wrapper around CreateThread which handles differences in the type of
677 * the the 'tid' argument.
679 gpointer mono_create_thread (WapiSecurityAttributes *security,
680 guint32 stacksize, WapiThreadStart start,
681 gpointer param, guint32 create, gsize *tid)
688 res = mono_threads_CreateThread (security, stacksize, start, param, create, &real_tid);
692 res = CreateThread (security, stacksize, start, param, create, tid);
699 * The thread start argument may be an object reference, and there is
700 * no ref to keep it alive when the new thread is started but not yet
701 * registered with the collector. So we store it in a GC tracked hash
704 * LOCKING: Assumes the threads lock is held.
707 register_thread_start_argument (MonoThread *thread, struct StartInfo *start_info)
709 if (thread_start_args == NULL) {
710 MONO_GC_REGISTER_ROOT_FIXED (thread_start_args);
711 thread_start_args = mono_g_hash_table_new (NULL, NULL);
713 mono_g_hash_table_insert (thread_start_args, thread, start_info->start_arg);
717 * mono_thread_create_internal:
719 * If NO_DETACH is TRUE, then the thread is not detached using pthread_detach (). This is needed to fix the race condition where waiting for a thred to exit only waits for its exit event to be
720 * signalled, which can cause shutdown crashes if the thread shutdown code accesses data already freed by the runtime shutdown.
721 * Currently, this is only used for the finalizer thread.
724 mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread, gboolean no_detach, guint32 stack_size)
727 MonoInternalThread *internal;
728 HANDLE thread_handle;
729 struct StartInfo *start_info;
731 guint32 create_flags;
733 thread = create_thread_object (domain);
734 internal = create_internal_thread_object ();
735 MONO_OBJECT_SETREF (thread, internal_thread, internal);
737 start_info=g_new0 (struct StartInfo, 1);
738 start_info->func = func;
739 start_info->obj = thread;
740 start_info->start_arg = arg;
742 mono_threads_lock ();
744 mono_threads_unlock ();
748 if (threads_starting_up == NULL) {
749 MONO_GC_REGISTER_ROOT_FIXED (threads_starting_up);
750 threads_starting_up = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
753 register_thread_start_argument (thread, start_info);
754 mono_g_hash_table_insert (threads_starting_up, thread, thread);
755 mono_threads_unlock ();
758 stack_size = default_stacksize_for_thread (internal);
760 /* Create suspended, so we can do some housekeeping before the thread
763 create_flags = CREATE_SUSPENDED;
766 create_flags |= CREATE_NO_DETACH;
768 thread_handle = mono_create_thread (NULL, stack_size, (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
770 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
771 if (thread_handle == NULL) {
772 /* The thread couldn't be created, so throw an exception */
773 mono_threads_lock ();
774 mono_g_hash_table_remove (threads_starting_up, thread);
775 mono_threads_unlock ();
777 mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
781 internal->handle=thread_handle;
783 internal->apartment_state=ThreadApartmentState_Unknown;
784 internal->managed_id = get_next_managed_thread_id ();
785 if (mono_gc_is_moving ()) {
786 internal->thread_pinning_ref = internal;
787 MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
790 internal->synch_cs = g_new0 (CRITICAL_SECTION, 1);
791 InitializeCriticalSection (internal->synch_cs);
793 internal->threadpool_thread = threadpool_thread;
794 if (threadpool_thread)
795 mono_thread_set_state (internal, ThreadState_Background);
797 if (handle_store (thread, FALSE))
798 ResumeThread (thread_handle);
800 /* Check that the managed and unmanaged layout of MonoInternalThread matches */
801 if (mono_check_corlib_version () == NULL)
802 g_assert (((char*)&internal->unused2 - (char*)internal) == mono_defaults.internal_thread_class->fields [mono_defaults.internal_thread_class->field.count - 1].offset);
808 mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
810 mono_thread_create_internal (domain, func, arg, FALSE, FALSE, 0);
813 #if defined(HOST_WIN32) && defined(__GNUC__)
814 static __inline__ __attribute__((always_inline))
815 /* This is not defined by gcc */
817 __readfsdword (unsigned long offset)
820 // __asm__("movl %%fs:%a[offset], %k[value]" : [value] "=q" (value) : [offset] "irm" (offset));
821 __asm__ volatile ("movl %%fs:%1,%0"
822 : "=r" (value) ,"=m" ((*(volatile long *) offset)));
828 * mono_thread_get_stack_bounds:
830 * Return the address and size of the current threads stack. Return NULL as the
831 * stack address if the stack address cannot be determined.
834 mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
836 #if defined(HOST_WIN32)
838 /* http://en.wikipedia.org/wiki/Win32_Thread_Information_Block */
839 void* tib = (void*)__readfsdword(0x18);
840 guint8 *stackTop = (guint8*)*(int*)((char*)tib + 4);
841 guint8 *stackBottom = (guint8*)*(int*)((char*)tib + 8);
843 *staddr = stackBottom;
844 *stsize = stackTop - stackBottom;
847 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
849 *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
850 *stsize = pthread_get_stacksize_np (pthread_self());
852 /* staddr points to the start of the stack, not the end */
855 /* When running under emacs, sometimes staddr is not aligned to a page size */
856 *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize() - 1));
859 #elif (defined(HAVE_PTHREAD_GETATTR_NP) || defined(HAVE_PTHREAD_ATTR_GET_NP)) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
863 guint8 *current = (guint8*)&attr;
866 *stsize = (size_t)-1;
868 pthread_attr_init (&attr);
870 #if defined(HAVE_PTHREAD_GETATTR_NP)
872 pthread_getattr_np (pthread_self(), &attr);
874 #elif defined(HAVE_PTHREAD_ATTR_GET_NP)
876 pthread_attr_get_np (pthread_self(), &attr);
879 #error Cannot determine which API is needed to retrieve pthread attributes.
882 pthread_attr_getstack (&attr, (void**)staddr, stsize);
883 pthread_attr_destroy (&attr);
886 g_assert ((current > *staddr) && (current < *staddr + *stsize));
888 /* When running under emacs, sometimes staddr is not aligned to a page size */
889 *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
892 #elif defined(__OpenBSD__)
894 /* TODO : Determine if this code is actually still needed. It may already be covered by the case above. */
897 guint8 *current = (guint8*)&attr;
900 *stsize = (size_t)-1;
902 pthread_attr_init (&attr);
907 rslt = pthread_stackseg_np(pthread_self(), &ss);
908 g_assert (rslt == 0);
910 *staddr = (guint8*)((size_t)ss.ss_sp - ss.ss_size);
911 *stsize = ss.ss_size;
913 pthread_attr_destroy (&attr);
916 g_assert ((current > *staddr) && (current < *staddr + *stsize));
918 /* When running under emacs, sometimes staddr is not aligned to a page size */
919 *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
922 #elif defined(sun) || defined(__native_client__)
923 /* Solaris/Illumos, NaCl */
925 pthread_attr_init (&attr);
926 pthread_attr_getstacksize (&attr, &stsize);
927 pthread_attr_destroy (&attr);
932 /* FIXME: It'd be better to use the 'error' preprocessor macro here so we know
933 at compile-time if the target platform isn't supported. */
934 #warning "Unable to determine how to retrieve a thread's stack-bounds for this platform in 'mono_thread_get_stack_bounds()'."
942 mono_thread_attach (MonoDomain *domain)
944 return mono_thread_attach_full (domain, FALSE);
948 mono_thread_attach_full (MonoDomain *domain, gboolean force_attach)
950 MonoThreadInfo *info;
951 MonoInternalThread *thread;
952 MonoThread *current_thread;
953 HANDLE thread_handle;
956 if ((thread = mono_thread_internal_current ())) {
957 if (domain != mono_domain_get ())
958 mono_domain_set (domain, TRUE);
959 /* Already attached */
960 return mono_thread_current ();
963 if (!mono_gc_register_thread (&domain)) {
964 g_error ("Thread %"G_GSIZE_FORMAT" calling into managed code is not registered with the GC. On UNIX, this can be fixed by #include-ing <gc.h> before <pthread.h> in the file containing the thread creation code.", GetCurrentThreadId ());
967 thread = create_internal_thread_object ();
969 thread_handle = GetCurrentThread ();
970 g_assert (thread_handle);
972 tid=GetCurrentThreadId ();
975 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
976 * refer to the thread from other threads for things like aborting.
978 DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
979 THREAD_ALL_ACCESS, TRUE, 0);
981 thread->handle=thread_handle;
983 #ifdef PLATFORM_ANDROID
984 thread->android_tid = (gpointer) gettid ();
986 thread->apartment_state=ThreadApartmentState_Unknown;
987 thread->managed_id = get_next_managed_thread_id ();
988 if (mono_gc_is_moving ()) {
989 thread->thread_pinning_ref = thread;
990 MONO_GC_REGISTER_ROOT_PINNING (thread->thread_pinning_ref);
993 thread->stack_ptr = &tid;
995 thread->synch_cs = g_new0 (CRITICAL_SECTION, 1);
996 InitializeCriticalSection (thread->synch_cs);
998 THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
1000 info = mono_thread_info_current ();
1002 thread->thread_info = info;
1004 current_thread = new_thread_with_internal (domain, thread);
1006 if (!handle_store (current_thread, force_attach)) {
1007 /* Mono is shutting down, so just wait for the end */
1012 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
1014 SET_CURRENT_OBJECT (thread);
1015 mono_domain_set (domain, TRUE);
1017 mono_monitor_init_tls ();
1019 thread_adjust_static_data (thread);
1021 init_root_domain_thread (thread, current_thread);
1022 if (domain != mono_get_root_domain ())
1023 set_current_thread_for_domain (domain, thread, current_thread);
1026 if (mono_thread_attach_cb) {
1030 mono_thread_get_stack_bounds (&staddr, &stsize);
1033 mono_thread_attach_cb (tid, &tid);
1035 mono_thread_attach_cb (tid, staddr + stsize);
1038 // FIXME: Need a separate callback
1039 mono_profiler_thread_start (tid);
1041 return current_thread;
1045 mono_thread_detach (MonoThread *thread)
1047 g_return_if_fail (thread != NULL);
1049 THREAD_DEBUG (g_message ("%s: mono_thread_detach for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->internal_thread->tid));
1051 thread_cleanup (thread->internal_thread);
1053 SET_CURRENT_OBJECT (NULL);
1054 mono_domain_unset ();
1056 /* Don't need to CloseHandle this thread, even though we took a
1057 * reference in mono_thread_attach (), because the GC will do it
1058 * when the Thread object is finalised.
1065 MonoInternalThread *thread = mono_thread_internal_current ();
1067 THREAD_DEBUG (g_message ("%s: mono_thread_exit for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
1069 thread_cleanup (thread);
1070 SET_CURRENT_OBJECT (NULL);
1071 mono_domain_unset ();
1073 /* we could add a callback here for embedders to use. */
1074 if (mono_thread_get_main () && (thread == mono_thread_get_main ()->internal_thread))
1075 exit (mono_environment_exitcode_get ());
1080 ves_icall_System_Threading_Thread_ConstructInternalThread (MonoThread *this)
1082 MonoInternalThread *internal = create_internal_thread_object ();
1084 internal->state = ThreadState_Unstarted;
1085 internal->apartment_state = ThreadApartmentState_Unknown;
1086 internal->managed_id = get_next_managed_thread_id ();
1088 InterlockedCompareExchangePointer ((gpointer)&this->internal_thread, internal, NULL);
1091 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
1094 guint32 (*start_func)(void *);
1095 struct StartInfo *start_info;
1098 MonoInternalThread *internal;
1100 THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__, this, start));
1102 if (!this->internal_thread)
1103 ves_icall_System_Threading_Thread_ConstructInternalThread (this);
1104 internal = this->internal_thread;
1106 ensure_synch_cs_set (internal);
1108 EnterCriticalSection (internal->synch_cs);
1110 if ((internal->state & ThreadState_Unstarted) == 0) {
1111 LeaveCriticalSection (internal->synch_cs);
1112 mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
1116 if ((internal->state & ThreadState_Aborted) != 0) {
1117 LeaveCriticalSection (internal->synch_cs);
1122 /* This is freed in start_wrapper */
1123 start_info = g_new0 (struct StartInfo, 1);
1124 start_info->func = start_func;
1125 start_info->start_arg = this->start_obj; /* FIXME: GC object stored in unmanaged memory */
1126 start_info->delegate = start;
1127 start_info->obj = this;
1128 g_assert (this->obj.vtable->domain == mono_domain_get ());
1130 internal->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
1131 if (internal->start_notify==NULL) {
1132 LeaveCriticalSection (internal->synch_cs);
1133 g_warning ("%s: CreateSemaphore error 0x%x", __func__, GetLastError ());
1134 g_free (start_info);
1138 mono_threads_lock ();
1139 register_thread_start_argument (this, start_info);
1140 if (threads_starting_up == NULL) {
1141 MONO_GC_REGISTER_ROOT_FIXED (threads_starting_up);
1142 threads_starting_up = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
1144 mono_g_hash_table_insert (threads_starting_up, this, this);
1145 mono_threads_unlock ();
1147 thread=mono_create_thread(NULL, default_stacksize_for_thread (internal), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
1148 CREATE_SUSPENDED, &tid);
1150 LeaveCriticalSection (internal->synch_cs);
1151 mono_threads_lock ();
1152 mono_g_hash_table_remove (threads_starting_up, this);
1153 mono_threads_unlock ();
1154 g_warning("%s: CreateThread error 0x%x", __func__, GetLastError());
1158 internal->handle=thread;
1160 if (mono_gc_is_moving ()) {
1161 internal->thread_pinning_ref = internal;
1162 MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
1166 /* Don't call handle_store() here, delay it to Start.
1167 * We can't join a thread (trying to will just block
1168 * forever) until it actually starts running, so don't
1169 * store the handle till then.
1172 mono_thread_start (this);
1174 internal->state &= ~ThreadState_Unstarted;
1176 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread));
1178 LeaveCriticalSection (internal->synch_cs);
1183 void ves_icall_System_Threading_InternalThread_Thread_free_internal (MonoInternalThread *this, HANDLE thread)
1185 MONO_ARCH_SAVE_REGS;
1187 THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
1190 CloseHandle (thread);
1192 if (this->synch_cs) {
1193 CRITICAL_SECTION *synch_cs = this->synch_cs;
1194 this->synch_cs = NULL;
1195 DeleteCriticalSection (synch_cs);
1200 void *name = this->name;
1206 static void mono_thread_start (MonoThread *thread)
1208 MonoInternalThread *internal = thread->internal_thread;
1210 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), internal, (gsize)internal->tid));
1212 /* Only store the handle when the thread is about to be
1213 * launched, to avoid the main thread deadlocking while trying
1214 * to clean up a thread that will never be signalled.
1216 if (!handle_store (thread, FALSE))
1219 ResumeThread (internal->handle);
1221 if(internal->start_notify!=NULL) {
1222 /* Wait for the thread to set up its TLS data etc, so
1223 * theres no potential race condition if someone tries
1224 * to look up the data believing the thread has
1228 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for thread %p (%"G_GSIZE_FORMAT") to start", __func__, GetCurrentThreadId (), internal, (gsize)internal->tid));
1230 WaitForSingleObjectEx (internal->start_notify, INFINITE, FALSE);
1231 CloseHandle (internal->start_notify);
1232 internal->start_notify = NULL;
1235 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Done launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), internal, (gsize)internal->tid));
1238 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
1241 MonoInternalThread *thread = mono_thread_internal_current ();
1243 THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
1245 mono_thread_current_check_pending_interrupt ();
1248 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1250 res = SleepEx(ms,TRUE);
1252 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1254 if (res == WAIT_IO_COMPLETION) { /* we might have been interrupted */
1255 MonoException* exc = mono_thread_execute_interruption (thread);
1257 mono_raise_exception (exc);
1269 void ves_icall_System_Threading_Thread_SpinWait_nop (void)
1274 ves_icall_System_Threading_Thread_GetDomainID (void)
1276 MONO_ARCH_SAVE_REGS;
1278 return mono_domain_get()->domain_id;
1282 ves_icall_System_Threading_Thread_Yield (void)
1285 return SwitchToThread ();
1287 return sched_yield () == 0;
1292 * mono_thread_get_name:
1294 * Return the name of the thread. NAME_LEN is set to the length of the name.
1295 * Return NULL if the thread has no name. The returned memory is owned by the
1299 mono_thread_get_name (MonoInternalThread *this_obj, guint32 *name_len)
1303 ensure_synch_cs_set (this_obj);
1305 EnterCriticalSection (this_obj->synch_cs);
1307 if (!this_obj->name) {
1311 *name_len = this_obj->name_len;
1312 res = g_new (gunichar2, this_obj->name_len);
1313 memcpy (res, this_obj->name, sizeof (gunichar2) * this_obj->name_len);
1316 LeaveCriticalSection (this_obj->synch_cs);
1322 ves_icall_System_Threading_Thread_GetName_internal (MonoInternalThread *this_obj)
1326 ensure_synch_cs_set (this_obj);
1328 EnterCriticalSection (this_obj->synch_cs);
1330 if (!this_obj->name)
1333 str = mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
1335 LeaveCriticalSection (this_obj->synch_cs);
1341 mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean managed)
1343 ensure_synch_cs_set (this_obj);
1345 EnterCriticalSection (this_obj->synch_cs);
1347 if (this_obj->flags & MONO_THREAD_FLAG_NAME_SET) {
1348 LeaveCriticalSection (this_obj->synch_cs);
1350 mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
1354 this_obj->name = g_new (gunichar2, mono_string_length (name));
1355 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
1356 this_obj->name_len = mono_string_length (name);
1359 this_obj->name = NULL;
1362 this_obj->flags |= MONO_THREAD_FLAG_NAME_SET;
1364 LeaveCriticalSection (this_obj->synch_cs);
1365 if (this_obj->name) {
1366 char *tname = mono_string_to_utf8 (name);
1367 mono_profiler_thread_name (this_obj->tid, tname);
1373 ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name)
1375 mono_thread_set_name_internal (this_obj, name, TRUE);
1378 /* If the array is already in the requested domain, we just return it,
1379 otherwise we return a copy in that domain. */
1381 byte_array_to_domain (MonoArray *arr, MonoDomain *domain)
1388 if (mono_object_domain (arr) == domain)
1391 copy = mono_array_new (domain, mono_defaults.byte_class, arr->max_length);
1392 mono_gc_memmove (mono_array_addr (copy, guint8, 0), mono_array_addr (arr, guint8, 0), arr->max_length);
1397 ves_icall_System_Threading_Thread_ByteArrayToRootDomain (MonoArray *arr)
1399 return byte_array_to_domain (arr, mono_get_root_domain ());
1403 ves_icall_System_Threading_Thread_ByteArrayToCurrentDomain (MonoArray *arr)
1405 return byte_array_to_domain (arr, mono_domain_get ());
1409 mono_thread_current (void)
1411 MonoDomain *domain = mono_domain_get ();
1412 MonoInternalThread *internal = mono_thread_internal_current ();
1413 MonoThread **current_thread_ptr;
1415 g_assert (internal);
1416 current_thread_ptr = get_current_thread_ptr_for_domain (domain, internal);
1418 if (!*current_thread_ptr) {
1419 g_assert (domain != mono_get_root_domain ());
1420 *current_thread_ptr = new_thread_with_internal (domain, internal);
1422 return *current_thread_ptr;
1426 mono_thread_internal_current (void)
1428 MonoInternalThread *res = GET_CURRENT_OBJECT ();
1429 THREAD_DEBUG (g_message ("%s: returning %p", __func__, res));
1433 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoInternalThread *this,
1434 int ms, HANDLE thread)
1436 MonoInternalThread *cur_thread = mono_thread_internal_current ();
1439 mono_thread_current_check_pending_interrupt ();
1441 ensure_synch_cs_set (this);
1443 EnterCriticalSection (this->synch_cs);
1445 if ((this->state & ThreadState_Unstarted) != 0) {
1446 LeaveCriticalSection (this->synch_cs);
1448 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
1452 LeaveCriticalSection (this->synch_cs);
1457 THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__, thread, ms));
1459 mono_thread_set_state (cur_thread, ThreadState_WaitSleepJoin);
1461 ret=WaitForSingleObjectEx (thread, ms, TRUE);
1463 mono_thread_clr_state (cur_thread, ThreadState_WaitSleepJoin);
1465 if(ret==WAIT_OBJECT_0) {
1466 THREAD_DEBUG (g_message ("%s: join successful", __func__));
1471 THREAD_DEBUG (g_message ("%s: join failed", __func__));
1477 mono_wait_uninterrupted (MonoInternalThread *thread, gboolean multiple, guint32 numhandles, gpointer *handles, gboolean waitall, gint32 ms, gboolean alertable)
1485 start = (ms == -1) ? 0 : mono_100ns_ticks ();
1488 ret = WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, alertable);
1490 ret = WaitForSingleObjectEx (handles [0], ms, alertable);
1492 if (ret != WAIT_IO_COMPLETION)
1495 exc = mono_thread_execute_interruption (thread);
1497 mono_raise_exception (exc);
1502 /* Re-calculate ms according to the time passed */
1503 diff_ms = (mono_100ns_ticks () - start) / 10000;
1504 if (diff_ms >= ms) {
1508 wait = ms - diff_ms;
1514 /* FIXME: exitContext isnt documented */
1515 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
1521 MonoObject *waitHandle;
1522 MonoInternalThread *thread = mono_thread_internal_current ();
1524 /* Do this WaitSleepJoin check before creating objects */
1525 mono_thread_current_check_pending_interrupt ();
1527 numhandles = mono_array_length(mono_handles);
1528 handles = g_new0(HANDLE, numhandles);
1530 for(i = 0; i < numhandles; i++) {
1531 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
1532 handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
1539 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1541 ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, TRUE, ms, TRUE);
1543 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1547 if(ret==WAIT_FAILED) {
1548 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1550 } else if(ret==WAIT_TIMEOUT) {
1551 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1558 /* FIXME: exitContext isnt documented */
1559 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
1561 HANDLE handles [MAXIMUM_WAIT_OBJECTS];
1565 MonoObject *waitHandle;
1566 MonoInternalThread *thread = mono_thread_internal_current ();
1568 /* Do this WaitSleepJoin check before creating objects */
1569 mono_thread_current_check_pending_interrupt ();
1571 numhandles = mono_array_length(mono_handles);
1572 if (numhandles > MAXIMUM_WAIT_OBJECTS)
1575 for(i = 0; i < numhandles; i++) {
1576 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
1577 handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
1584 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1586 ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, FALSE, ms, TRUE);
1588 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1590 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") returning %d", __func__, GetCurrentThreadId (), ret));
1593 * These need to be here. See MSDN dos on WaitForMultipleObjects.
1595 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
1596 return ret - WAIT_OBJECT_0;
1598 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
1599 return ret - WAIT_ABANDONED_0;
1606 /* FIXME: exitContext isnt documented */
1607 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
1610 MonoInternalThread *thread = mono_thread_internal_current ();
1612 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for %p, %d ms", __func__, GetCurrentThreadId (), handle, ms));
1618 mono_thread_current_check_pending_interrupt ();
1620 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1622 ret = mono_wait_uninterrupted (thread, FALSE, 1, &handle, FALSE, ms, TRUE);
1624 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1626 if(ret==WAIT_FAILED) {
1627 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1629 } else if(ret==WAIT_TIMEOUT) {
1630 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1638 ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, HANDLE toWait, gint32 ms, gboolean exitContext)
1641 MonoInternalThread *thread = mono_thread_internal_current ();
1643 MONO_ARCH_SAVE_REGS;
1648 mono_thread_current_check_pending_interrupt ();
1650 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1652 ret = SignalObjectAndWait (toSignal, toWait, ms, TRUE);
1654 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1656 return (!(ret == WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION || ret == WAIT_FAILED));
1659 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
1663 MONO_ARCH_SAVE_REGS;
1668 mutex = CreateMutex (NULL, owned, NULL);
1670 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
1672 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1680 MonoBoolean ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
1681 MONO_ARCH_SAVE_REGS;
1683 return(ReleaseMutex (handle));
1686 HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name,
1692 MONO_ARCH_SAVE_REGS;
1694 *error = ERROR_SUCCESS;
1696 ret = OpenMutex (rights, FALSE, mono_string_chars (name));
1698 *error = GetLastError ();
1705 HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created)
1709 MONO_ARCH_SAVE_REGS;
1714 sem = CreateSemaphore (NULL, initialCount, maximumCount, NULL);
1716 sem = CreateSemaphore (NULL, initialCount, maximumCount,
1717 mono_string_chars (name));
1719 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1727 gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail)
1731 MONO_ARCH_SAVE_REGS;
1733 *fail = !ReleaseSemaphore (handle, releaseCount, &prevcount);
1738 HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
1742 MONO_ARCH_SAVE_REGS;
1744 *error = ERROR_SUCCESS;
1746 ret = OpenSemaphore (rights, FALSE, mono_string_chars (name));
1748 *error = GetLastError ();
1754 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created)
1758 MONO_ARCH_SAVE_REGS;
1763 event = CreateEvent (NULL, manual, initial, NULL);
1765 event = CreateEvent (NULL, manual, initial,
1766 mono_string_chars (name));
1768 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1776 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
1777 MONO_ARCH_SAVE_REGS;
1779 return (SetEvent(handle));
1782 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
1783 MONO_ARCH_SAVE_REGS;
1785 return (ResetEvent(handle));
1789 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
1790 MONO_ARCH_SAVE_REGS;
1792 CloseHandle (handle);
1795 HANDLE ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name,
1801 MONO_ARCH_SAVE_REGS;
1803 *error = ERROR_SUCCESS;
1805 ret = OpenEvent (rights, FALSE, mono_string_chars (name));
1807 *error = GetLastError ();
1813 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
1815 MONO_ARCH_SAVE_REGS;
1817 return InterlockedIncrement (location);
1820 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
1824 MONO_ARCH_SAVE_REGS;
1826 mono_interlocked_lock ();
1830 mono_interlocked_unlock ();
1836 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
1838 MONO_ARCH_SAVE_REGS;
1840 return InterlockedDecrement(location);
1843 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
1847 MONO_ARCH_SAVE_REGS;
1849 mono_interlocked_lock ();
1853 mono_interlocked_unlock ();
1858 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location, gint32 value)
1860 MONO_ARCH_SAVE_REGS;
1862 return InterlockedExchange(location, value);
1865 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location, MonoObject *value)
1868 res = (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
1869 mono_gc_wbarrier_generic_nostore (location);
1873 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr (gpointer *location, gpointer value)
1875 return InterlockedExchangePointer(location, value);
1878 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location, gfloat value)
1880 IntFloatUnion val, ret;
1882 MONO_ARCH_SAVE_REGS;
1885 ret.ival = InterlockedExchange((gint32 *) location, val.ival);
1891 ves_icall_System_Threading_Interlocked_Exchange_Long (gint64 *location, gint64 value)
1893 #if SIZEOF_VOID_P == 8
1894 return (gint64) InterlockedExchangePointer((gpointer *) location, (gpointer)value);
1899 * According to MSDN, this function is only atomic with regards to the
1900 * other Interlocked functions on 32 bit platforms.
1902 mono_interlocked_lock ();
1905 mono_interlocked_unlock ();
1912 ves_icall_System_Threading_Interlocked_Exchange_Double (gdouble *location, gdouble value)
1914 #if SIZEOF_VOID_P == 8
1915 LongDoubleUnion val, ret;
1918 ret.ival = (gint64)InterlockedExchangePointer((gpointer *) location, (gpointer)val.ival);
1925 * According to MSDN, this function is only atomic with regards to the
1926 * other Interlocked functions on 32 bit platforms.
1928 mono_interlocked_lock ();
1931 mono_interlocked_unlock ();
1937 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand)
1939 MONO_ARCH_SAVE_REGS;
1941 return InterlockedCompareExchange(location, value, comparand);
1944 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location, MonoObject *value, MonoObject *comparand)
1947 res = (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
1948 mono_gc_wbarrier_generic_nostore (location);
1952 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand)
1954 return InterlockedCompareExchangePointer(location, value, comparand);
1957 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location, gfloat value, gfloat comparand)
1959 IntFloatUnion val, ret, cmp;
1961 MONO_ARCH_SAVE_REGS;
1964 cmp.fval = comparand;
1965 ret.ival = InterlockedCompareExchange((gint32 *) location, val.ival, cmp.ival);
1971 ves_icall_System_Threading_Interlocked_CompareExchange_Double (gdouble *location, gdouble value, gdouble comparand)
1973 #if SIZEOF_VOID_P == 8
1974 LongDoubleUnion val, comp, ret;
1977 comp.fval = comparand;
1978 ret.ival = (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)val.ival, (gpointer)comp.ival);
1984 mono_interlocked_lock ();
1986 if (old == comparand)
1988 mono_interlocked_unlock ();
1995 ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64 *location, gint64 value, gint64 comparand)
1997 #if SIZEOF_VOID_P == 4
1998 if ((size_t)location & 0x7) {
2000 mono_interlocked_lock ();
2002 if (old == comparand)
2004 mono_interlocked_unlock ();
2008 return InterlockedCompareExchange64 (location, value, comparand);
2012 ves_icall_System_Threading_Interlocked_CompareExchange_T (MonoObject **location, MonoObject *value, MonoObject *comparand)
2015 res = InterlockedCompareExchangePointer ((gpointer *)location, value, comparand);
2016 mono_gc_wbarrier_generic_nostore (location);
2021 ves_icall_System_Threading_Interlocked_Exchange_T (MonoObject **location, MonoObject *value)
2024 res = InterlockedExchangePointer ((gpointer *)location, value);
2025 mono_gc_wbarrier_generic_nostore (location);
2030 ves_icall_System_Threading_Interlocked_Add_Int (gint32 *location, gint32 value)
2032 #if SIZEOF_VOID_P == 8
2033 /* Should be implemented as a JIT intrinsic */
2034 mono_raise_exception (mono_get_exception_not_implemented (NULL));
2039 mono_interlocked_lock ();
2041 *location = orig + value;
2042 mono_interlocked_unlock ();
2044 return orig + value;
2049 ves_icall_System_Threading_Interlocked_Add_Long (gint64 *location, gint64 value)
2051 #if SIZEOF_VOID_P == 8
2052 /* Should be implemented as a JIT intrinsic */
2053 mono_raise_exception (mono_get_exception_not_implemented (NULL));
2058 mono_interlocked_lock ();
2060 *location = orig + value;
2061 mono_interlocked_unlock ();
2063 return orig + value;
2068 ves_icall_System_Threading_Interlocked_Read_Long (gint64 *location)
2070 #if SIZEOF_VOID_P == 8
2071 /* 64 bit reads are already atomic */
2076 mono_interlocked_lock ();
2078 mono_interlocked_unlock ();
2085 ves_icall_System_Threading_Thread_MemoryBarrier (void)
2087 mono_threads_lock ();
2088 mono_threads_unlock ();
2092 ves_icall_System_Threading_Thread_ClrState (MonoInternalThread* this, guint32 state)
2094 mono_thread_clr_state (this, state);
2096 if (state & ThreadState_Background) {
2097 /* If the thread changes the background mode, the main thread has to
2098 * be notified, since it has to rebuild the list of threads to
2101 SetEvent (background_change_event);
2106 ves_icall_System_Threading_Thread_SetState (MonoInternalThread* this, guint32 state)
2108 mono_thread_set_state (this, state);
2110 if (state & ThreadState_Background) {
2111 /* If the thread changes the background mode, the main thread has to
2112 * be notified, since it has to rebuild the list of threads to
2115 SetEvent (background_change_event);
2120 ves_icall_System_Threading_Thread_GetState (MonoInternalThread* this)
2124 ensure_synch_cs_set (this);
2126 EnterCriticalSection (this->synch_cs);
2128 state = this->state;
2130 LeaveCriticalSection (this->synch_cs);
2135 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoInternalThread *this)
2137 MonoInternalThread *current;
2140 ensure_synch_cs_set (this);
2142 current = mono_thread_internal_current ();
2144 EnterCriticalSection (this->synch_cs);
2146 this->thread_interrupt_requested = TRUE;
2147 throw = current != this && (this->state & ThreadState_WaitSleepJoin);
2149 LeaveCriticalSection (this->synch_cs);
2152 abort_thread_internal (this, TRUE, FALSE);
2156 void mono_thread_current_check_pending_interrupt ()
2158 MonoInternalThread *thread = mono_thread_internal_current ();
2159 gboolean throw = FALSE;
2161 mono_debugger_check_interruption ();
2163 ensure_synch_cs_set (thread);
2165 EnterCriticalSection (thread->synch_cs);
2167 if (thread->thread_interrupt_requested) {
2169 thread->thread_interrupt_requested = FALSE;
2172 LeaveCriticalSection (thread->synch_cs);
2175 mono_raise_exception (mono_get_exception_thread_interrupted ());
2180 mono_thread_get_abort_signal (void)
2184 #elif defined(PLATFORM_ANDROID)
2186 #elif !defined (SIGRTMIN)
2191 #endif /* SIGUSR1 */
2193 static int abort_signum = -1;
2195 if (abort_signum != -1)
2196 return abort_signum;
2197 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
2198 for (i = SIGRTMIN + 1; i < SIGRTMAX; ++i) {
2199 struct sigaction sinfo;
2200 sigaction (i, NULL, &sinfo);
2201 if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
2206 /* fallback to the old way */
2208 #endif /* HOST_WIN32 */
2212 static void CALLBACK interruption_request_apc (ULONG_PTR param)
2214 MonoException* exc = mono_thread_request_interruption (FALSE);
2215 if (exc) mono_raise_exception (exc);
2217 #endif /* HOST_WIN32 */
2220 * signal_thread_state_change
2222 * Tells the thread that his state has changed and it has to enter the new
2223 * state as soon as possible.
2225 static void signal_thread_state_change (MonoInternalThread *thread)
2227 if (thread == mono_thread_internal_current ()) {
2228 /* Do it synchronously */
2229 MonoException *exc = mono_thread_request_interruption (FALSE);
2231 mono_raise_exception (exc);
2235 QueueUserAPC ((PAPCFUNC)interruption_request_apc, thread->handle, NULL);
2237 /* fixme: store the state somewhere */
2238 mono_thread_kill (thread, mono_thread_get_abort_signal ());
2241 * This will cause waits to be broken.
2242 * It will also prevent the thread from entering a wait, so if the thread returns
2243 * from the wait before it receives the abort signal, it will just spin in the wait
2244 * functions in the io-layer until the signal handler calls QueueUserAPC which will
2247 wapi_interrupt_thread (thread->handle);
2248 #endif /* HOST_WIN32 */
2252 ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state)
2254 ensure_synch_cs_set (thread);
2256 EnterCriticalSection (thread->synch_cs);
2258 if ((thread->state & ThreadState_AbortRequested) != 0 ||
2259 (thread->state & ThreadState_StopRequested) != 0 ||
2260 (thread->state & ThreadState_Stopped) != 0)
2262 LeaveCriticalSection (thread->synch_cs);
2266 if ((thread->state & ThreadState_Unstarted) != 0) {
2267 thread->state |= ThreadState_Aborted;
2268 LeaveCriticalSection (thread->synch_cs);
2272 thread->state |= ThreadState_AbortRequested;
2273 if (thread->abort_state_handle)
2274 mono_gchandle_free (thread->abort_state_handle);
2276 thread->abort_state_handle = mono_gchandle_new (state, FALSE);
2277 g_assert (thread->abort_state_handle);
2279 thread->abort_state_handle = 0;
2281 thread->abort_exc = NULL;
2284 * abort_exc is set in mono_thread_execute_interruption(),
2285 * triggered by the call to signal_thread_state_change(),
2286 * below. There's a point between where we have
2287 * abort_state_handle set, but abort_exc NULL, but that's not
2291 LeaveCriticalSection (thread->synch_cs);
2293 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Abort requested for %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
2295 /* During shutdown, we can't wait for other threads */
2297 /* Make sure the thread is awake */
2298 mono_thread_resume (thread);
2300 abort_thread_internal (thread, TRUE, TRUE);
2304 ves_icall_System_Threading_Thread_ResetAbort (void)
2306 MonoInternalThread *thread = mono_thread_internal_current ();
2307 gboolean was_aborting;
2309 ensure_synch_cs_set (thread);
2311 EnterCriticalSection (thread->synch_cs);
2312 was_aborting = thread->state & ThreadState_AbortRequested;
2313 thread->state &= ~ThreadState_AbortRequested;
2314 LeaveCriticalSection (thread->synch_cs);
2316 if (!was_aborting) {
2317 const char *msg = "Unable to reset abort because no abort was requested";
2318 mono_raise_exception (mono_get_exception_thread_state (msg));
2320 thread->abort_exc = NULL;
2321 if (thread->abort_state_handle) {
2322 mono_gchandle_free (thread->abort_state_handle);
2323 /* This is actually not necessary - the handle
2324 only counts if the exception is set */
2325 thread->abort_state_handle = 0;
2330 mono_thread_internal_reset_abort (MonoInternalThread *thread)
2332 ensure_synch_cs_set (thread);
2334 EnterCriticalSection (thread->synch_cs);
2336 thread->state &= ~ThreadState_AbortRequested;
2338 if (thread->abort_exc) {
2339 thread->abort_exc = NULL;
2340 if (thread->abort_state_handle) {
2341 mono_gchandle_free (thread->abort_state_handle);
2342 /* This is actually not necessary - the handle
2343 only counts if the exception is set */
2344 thread->abort_state_handle = 0;
2348 LeaveCriticalSection (thread->synch_cs);
2352 ves_icall_System_Threading_Thread_GetAbortExceptionState (MonoThread *this)
2354 MonoInternalThread *thread = this->internal_thread;
2355 MonoObject *state, *deserialized = NULL, *exc;
2358 if (!thread->abort_state_handle)
2361 state = mono_gchandle_get_target (thread->abort_state_handle);
2364 domain = mono_domain_get ();
2365 if (mono_object_domain (state) == domain)
2368 deserialized = mono_object_xdomain_representation (state, domain, &exc);
2370 if (!deserialized) {
2371 MonoException *invalid_op_exc = mono_get_exception_invalid_operation ("Thread.ExceptionState cannot access an ExceptionState from a different AppDomain");
2373 MONO_OBJECT_SETREF (invalid_op_exc, inner_ex, exc);
2374 mono_raise_exception (invalid_op_exc);
2377 return deserialized;
2381 mono_thread_suspend (MonoInternalThread *thread)
2383 ensure_synch_cs_set (thread);
2385 EnterCriticalSection (thread->synch_cs);
2387 if ((thread->state & ThreadState_Unstarted) != 0 ||
2388 (thread->state & ThreadState_Aborted) != 0 ||
2389 (thread->state & ThreadState_Stopped) != 0)
2391 LeaveCriticalSection (thread->synch_cs);
2395 if ((thread->state & ThreadState_Suspended) != 0 ||
2396 (thread->state & ThreadState_SuspendRequested) != 0 ||
2397 (thread->state & ThreadState_StopRequested) != 0)
2399 LeaveCriticalSection (thread->synch_cs);
2403 thread->state |= ThreadState_SuspendRequested;
2405 LeaveCriticalSection (thread->synch_cs);
2407 suspend_thread_internal (thread, FALSE);
2412 ves_icall_System_Threading_Thread_Suspend (MonoInternalThread *thread)
2414 if (!mono_thread_suspend (thread))
2415 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
2419 mono_thread_resume (MonoInternalThread *thread)
2421 ensure_synch_cs_set (thread);
2423 EnterCriticalSection (thread->synch_cs);
2425 if ((thread->state & ThreadState_SuspendRequested) != 0) {
2426 thread->state &= ~ThreadState_SuspendRequested;
2427 LeaveCriticalSection (thread->synch_cs);
2431 if ((thread->state & ThreadState_Suspended) == 0 ||
2432 (thread->state & ThreadState_Unstarted) != 0 ||
2433 (thread->state & ThreadState_Aborted) != 0 ||
2434 (thread->state & ThreadState_Stopped) != 0)
2436 LeaveCriticalSection (thread->synch_cs);
2440 return resume_thread_internal (thread);
2444 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
2446 if (!thread->internal_thread || !mono_thread_resume (thread->internal_thread))
2447 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
2451 mono_threads_is_critical_method (MonoMethod *method)
2453 switch (method->wrapper_type) {
2454 case MONO_WRAPPER_RUNTIME_INVOKE:
2455 case MONO_WRAPPER_XDOMAIN_INVOKE:
2456 case MONO_WRAPPER_XDOMAIN_DISPATCH:
2463 find_wrapper (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
2468 if (mono_threads_is_critical_method (m)) {
2469 *((gboolean*)data) = TRUE;
2476 is_running_protected_wrapper (void)
2478 gboolean found = FALSE;
2479 mono_stack_walk (find_wrapper, &found);
2483 void mono_thread_internal_stop (MonoInternalThread *thread)
2485 ensure_synch_cs_set (thread);
2487 EnterCriticalSection (thread->synch_cs);
2489 if ((thread->state & ThreadState_StopRequested) != 0 ||
2490 (thread->state & ThreadState_Stopped) != 0)
2492 LeaveCriticalSection (thread->synch_cs);
2496 /* Make sure the thread is awake */
2497 mono_thread_resume (thread);
2499 thread->state |= ThreadState_StopRequested;
2500 thread->state &= ~ThreadState_AbortRequested;
2502 LeaveCriticalSection (thread->synch_cs);
2504 abort_thread_internal (thread, TRUE, TRUE);
2507 void mono_thread_stop (MonoThread *thread)
2509 mono_thread_internal_stop (thread->internal_thread);
2513 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
2515 return *((volatile gint8 *) (ptr));
2519 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
2521 return *((volatile gint16 *) (ptr));
2525 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
2527 return *((volatile gint32 *) (ptr));
2531 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
2533 #if SIZEOF_VOID_P == 8
2534 return *((volatile gint64 *) (ptr));
2536 if ((size_t)ptr & 0x7) {
2538 mono_interlocked_lock ();
2539 value = *(gint64 *)ptr;
2540 mono_interlocked_unlock ();
2543 return InterlockedCompareExchange64 (ptr, 0, 0); /*Must ensure atomicity of the operation. */
2548 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
2550 return (void *) *((volatile void **) ptr);
2554 ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr)
2556 return *((volatile double *) (ptr));
2560 ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr)
2562 return *((volatile float *) (ptr));
2566 ves_icall_System_Threading_Volatile_Read_T (void *ptr)
2568 return (MonoObject*)*((volatile MonoObject**)ptr);
2572 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
2574 *((volatile gint8 *) ptr) = value;
2578 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
2580 *((volatile gint16 *) ptr) = value;
2584 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
2586 *((volatile gint32 *) ptr) = value;
2590 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
2592 *((volatile gint64 *) ptr) = value;
2596 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
2598 *((volatile void **) ptr) = value;
2602 ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, void *value)
2604 mono_gc_wbarrier_generic_store (ptr, value);
2608 ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double value)
2610 *((volatile double *) ptr) = value;
2614 ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float value)
2616 *((volatile float *) ptr) = value;
2620 ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value)
2622 *((volatile MonoObject **) ptr) = value;
2623 mono_gc_wbarrier_generic_nostore (ptr);
2627 mono_thread_init_tls (void)
2629 MONO_FAST_TLS_INIT (tls_current_object);
2630 mono_native_tls_alloc (¤t_object_key, NULL);
2633 void mono_thread_init (MonoThreadStartCB start_cb,
2634 MonoThreadAttachCB attach_cb)
2636 InitializeCriticalSection(&threads_mutex);
2637 InitializeCriticalSection(&interlocked_mutex);
2638 InitializeCriticalSection(&contexts_mutex);
2640 background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2641 g_assert(background_change_event != NULL);
2643 mono_init_static_data_info (&thread_static_info);
2644 mono_init_static_data_info (&context_static_info);
2646 THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__, current_object_key));
2648 mono_thread_start_cb = start_cb;
2649 mono_thread_attach_cb = attach_cb;
2651 /* Get a pseudo handle to the current process. This is just a
2652 * kludge so that wapi can build a process handle if needed.
2653 * As a pseudo handle is returned, we don't need to clean
2656 GetCurrentProcess ();
2659 void mono_thread_cleanup (void)
2661 #if !defined(HOST_WIN32) && !defined(RUN_IN_SUBTHREAD)
2662 /* The main thread must abandon any held mutexes (particularly
2663 * important for named mutexes as they are shared across
2664 * processes, see bug 74680.) This will happen when the
2665 * thread exits, but if it's not running in a subthread it
2666 * won't exit in time.
2668 /* Using non-w32 API is a nasty kludge, but I couldn't find
2669 * anything in the documentation that would let me do this
2670 * here yet still be safe to call on windows.
2672 _wapi_thread_signal_self (mono_environment_exitcode_get ());
2676 /* This stuff needs more testing, it seems one of these
2677 * critical sections can be locked when mono_thread_cleanup is
2680 DeleteCriticalSection (&threads_mutex);
2681 DeleteCriticalSection (&interlocked_mutex);
2682 DeleteCriticalSection (&contexts_mutex);
2683 DeleteCriticalSection (&delayed_free_table_mutex);
2684 DeleteCriticalSection (&small_id_mutex);
2685 CloseHandle (background_change_event);
2688 mono_native_tls_free (current_object_key);
2692 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
2694 mono_thread_cleanup_fn = func;
2698 mono_thread_set_manage_callback (MonoThread *thread, MonoThreadManageCallback func)
2700 thread->internal_thread->manage_callback = func;
2703 void mono_threads_install_notify_pending_exc (MonoThreadNotifyPendingExcFunc func)
2705 mono_thread_notify_pending_exc_fn = func;
2709 static void print_tids (gpointer key, gpointer value, gpointer user)
2711 /* GPOINTER_TO_UINT breaks horribly if sizeof(void *) >
2712 * sizeof(uint) and a cast to uint would overflow
2714 /* Older versions of glib don't have G_GSIZE_FORMAT, so just
2715 * print this as a pointer.
2717 g_message ("Waiting for: %p", key);
2722 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
2723 MonoInternalThread *threads[MAXIMUM_WAIT_OBJECTS];
2727 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
2731 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
2733 ret=WaitForMultipleObjectsEx(wait->num, wait->handles, TRUE, timeout, TRUE);
2735 if(ret==WAIT_FAILED) {
2736 /* See the comment in build_wait_tids() */
2737 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
2741 for(i=0; i<wait->num; i++)
2742 CloseHandle (wait->handles[i]);
2744 if (ret == WAIT_TIMEOUT)
2747 for(i=0; i<wait->num; i++) {
2748 gsize tid = wait->threads[i]->tid;
2750 mono_threads_lock ();
2751 if(mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
2752 /* This thread must have been killed, because
2753 * it hasn't cleaned itself up. (It's just
2754 * possible that the thread exited before the
2755 * parent thread had a chance to store the
2756 * handle, and now there is another pointer to
2757 * the already-exited thread stored. In this
2758 * case, we'll just get two
2759 * mono_profiler_thread_end() calls for the
2763 mono_threads_unlock ();
2764 THREAD_DEBUG (g_message ("%s: cleaning up after thread %p (%"G_GSIZE_FORMAT")", __func__, wait->threads[i], tid));
2765 thread_cleanup (wait->threads[i]);
2767 mono_threads_unlock ();
2772 static void wait_for_tids_or_state_change (struct wait_data *wait, guint32 timeout)
2774 guint32 i, ret, count;
2776 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
2778 /* Add the thread state change event, so it wakes up if a thread changes
2779 * to background mode.
2782 if (count < MAXIMUM_WAIT_OBJECTS) {
2783 wait->handles [count] = background_change_event;
2787 ret=WaitForMultipleObjectsEx (count, wait->handles, FALSE, timeout, TRUE);
2789 if(ret==WAIT_FAILED) {
2790 /* See the comment in build_wait_tids() */
2791 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
2795 for(i=0; i<wait->num; i++)
2796 CloseHandle (wait->handles[i]);
2798 if (ret == WAIT_TIMEOUT)
2801 if (ret < wait->num) {
2802 gsize tid = wait->threads[ret]->tid;
2803 mono_threads_lock ();
2804 if (mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
2805 /* See comment in wait_for_tids about thread cleanup */
2806 mono_threads_unlock ();
2807 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
2808 thread_cleanup (wait->threads [ret]);
2810 mono_threads_unlock ();
2814 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
2816 struct wait_data *wait=(struct wait_data *)user;
2818 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
2820 MonoInternalThread *thread=(MonoInternalThread *)value;
2822 /* Ignore background threads, we abort them later */
2823 /* Do not lock here since it is not needed and the caller holds threads_lock */
2824 if (thread->state & ThreadState_Background) {
2825 THREAD_DEBUG (g_message ("%s: ignoring background thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2826 return; /* just leave, ignore */
2829 if (mono_gc_is_finalizer_internal_thread (thread)) {
2830 THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2834 if (thread == mono_thread_internal_current ()) {
2835 THREAD_DEBUG (g_message ("%s: ignoring current thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2839 if (mono_thread_get_main () && (thread == mono_thread_get_main ()->internal_thread)) {
2840 THREAD_DEBUG (g_message ("%s: ignoring main thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2844 if (thread->flags & MONO_THREAD_FLAG_DONT_MANAGE) {
2845 THREAD_DEBUG (g_message ("%s: ignoring thread %" G_GSIZE_FORMAT "with DONT_MANAGE flag set.", __func__, (gsize)thread->tid));
2849 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2850 if (handle == NULL) {
2851 THREAD_DEBUG (g_message ("%s: ignoring unopenable thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2855 THREAD_DEBUG (g_message ("%s: Invoking mono_thread_manage callback on thread %p", __func__, thread));
2856 if ((thread->manage_callback == NULL) || (thread->manage_callback (thread->root_domain_thread) == TRUE)) {
2857 wait->handles[wait->num]=handle;
2858 wait->threads[wait->num]=thread;
2861 THREAD_DEBUG (g_message ("%s: adding thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2863 THREAD_DEBUG (g_message ("%s: ignoring (because of callback) thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2868 /* Just ignore the rest, we can't do anything with
2875 remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
2877 struct wait_data *wait=(struct wait_data *)user;
2878 gsize self = GetCurrentThreadId ();
2879 MonoInternalThread *thread = value;
2882 if (wait->num >= MAXIMUM_WAIT_OBJECTS)
2885 /* The finalizer thread is not a background thread */
2886 if (thread->tid != self && (thread->state & ThreadState_Background) != 0 &&
2887 !(thread->flags & MONO_THREAD_FLAG_DONT_MANAGE)) {
2889 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2893 /* printf ("A: %d\n", wait->num); */
2894 wait->handles[wait->num]=thread->handle;
2895 wait->threads[wait->num]=thread;
2898 THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT"\n", __func__, (gsize)thread->tid));
2899 mono_thread_internal_stop (thread);
2903 return (thread->tid != self && !mono_gc_is_finalizer_internal_thread (thread));
2907 * mono_threads_set_shutting_down:
2909 * Is called by a thread that wants to shut down Mono. If the runtime is already
2910 * shutting down, the calling thread is suspended/stopped, and this function never
2914 mono_threads_set_shutting_down (void)
2916 MonoInternalThread *current_thread = mono_thread_internal_current ();
2918 mono_threads_lock ();
2920 if (shutting_down) {
2921 mono_threads_unlock ();
2923 /* Make sure we're properly suspended/stopped */
2925 EnterCriticalSection (current_thread->synch_cs);
2927 if ((current_thread->state & ThreadState_SuspendRequested) ||
2928 (current_thread->state & ThreadState_AbortRequested) ||
2929 (current_thread->state & ThreadState_StopRequested)) {
2930 LeaveCriticalSection (current_thread->synch_cs);
2931 mono_thread_execute_interruption (current_thread);
2933 current_thread->state |= ThreadState_Stopped;
2934 LeaveCriticalSection (current_thread->synch_cs);
2937 /*since we're killing the thread, unset the current domain.*/
2938 mono_domain_unset ();
2940 /* Wake up other threads potentially waiting for us */
2943 shutting_down = TRUE;
2945 /* Not really a background state change, but this will
2946 * interrupt the main thread if it is waiting for all
2947 * the other threads.
2949 SetEvent (background_change_event);
2951 mono_threads_unlock ();
2955 void mono_thread_manage (void)
2957 struct wait_data wait_data;
2958 struct wait_data *wait = &wait_data;
2960 memset (wait, 0, sizeof (struct wait_data));
2961 /* join each thread that's still running */
2962 THREAD_DEBUG (g_message ("%s: Joining each running thread...", __func__));
2964 mono_threads_lock ();
2966 THREAD_DEBUG (g_message("%s: No threads", __func__));
2967 mono_threads_unlock ();
2970 mono_threads_unlock ();
2973 mono_threads_lock ();
2974 if (shutting_down) {
2975 /* somebody else is shutting down */
2976 mono_threads_unlock ();
2979 THREAD_DEBUG (g_message ("%s: There are %d threads to join", __func__, mono_g_hash_table_size (threads));
2980 mono_g_hash_table_foreach (threads, print_tids, NULL));
2982 ResetEvent (background_change_event);
2984 /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
2985 memset (wait->threads, 0, MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
2986 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
2987 mono_threads_unlock ();
2989 /* Something to wait for */
2990 wait_for_tids_or_state_change (wait, INFINITE);
2992 THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
2993 } while(wait->num>0);
2995 /* Mono is shutting down, so just wait for the end */
2996 if (!mono_runtime_try_shutdown ()) {
2997 /*FIXME mono_thread_suspend probably should call mono_thread_execute_interruption when self interrupting. */
2998 mono_thread_suspend (mono_thread_internal_current ());
2999 mono_thread_execute_interruption (mono_thread_internal_current ());
3003 * Remove everything but the finalizer thread and self.
3004 * Also abort all the background threads
3007 mono_threads_lock ();
3010 /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
3011 memset (wait->threads, 0, MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
3012 mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
3014 mono_threads_unlock ();
3016 THREAD_DEBUG (g_message ("%s: wait->num is now %d", __func__, wait->num));
3018 /* Something to wait for */
3019 wait_for_tids (wait, INFINITE);
3021 } while (wait->num > 0);
3024 * give the subthreads a chance to really quit (this is mainly needed
3025 * to get correct user and system times from getrusage/wait/time(1)).
3026 * This could be removed if we avoid pthread_detach() and use pthread_join().
3033 static void terminate_thread (gpointer key, gpointer value, gpointer user)
3035 MonoInternalThread *thread=(MonoInternalThread *)value;
3037 if(thread->tid != (gsize)user) {
3038 /*TerminateThread (thread->handle, -1);*/
3042 void mono_thread_abort_all_other_threads (void)
3044 gsize self = GetCurrentThreadId ();
3046 mono_threads_lock ();
3047 THREAD_DEBUG (g_message ("%s: There are %d threads to abort", __func__,
3048 mono_g_hash_table_size (threads));
3049 mono_g_hash_table_foreach (threads, print_tids, NULL));
3051 mono_g_hash_table_foreach (threads, terminate_thread, (gpointer)self);
3053 mono_threads_unlock ();
3057 collect_threads_for_suspend (gpointer key, gpointer value, gpointer user_data)
3059 MonoInternalThread *thread = (MonoInternalThread*)value;
3060 struct wait_data *wait = (struct wait_data*)user_data;
3064 * We try to exclude threads early, to avoid running into the MAXIMUM_WAIT_OBJECTS
3066 * This needs no locking.
3068 if ((thread->state & ThreadState_Suspended) != 0 ||
3069 (thread->state & ThreadState_Stopped) != 0)
3072 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
3073 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
3077 wait->handles [wait->num] = handle;
3078 wait->threads [wait->num] = thread;
3084 * mono_thread_suspend_all_other_threads:
3086 * Suspend all managed threads except the finalizer thread and this thread. It is
3087 * not possible to resume them later.
3089 void mono_thread_suspend_all_other_threads (void)
3091 struct wait_data wait_data;
3092 struct wait_data *wait = &wait_data;
3094 gsize self = GetCurrentThreadId ();
3096 guint32 eventidx = 0;
3097 gboolean starting, finished;
3099 memset (wait, 0, sizeof (struct wait_data));
3101 * The other threads could be in an arbitrary state at this point, i.e.
3102 * they could be starting up, shutting down etc. This means that there could be
3103 * threads which are not even in the threads hash table yet.
3107 * First we set a barrier which will be checked by all threads before they
3108 * are added to the threads hash table, and they will exit if the flag is set.
3109 * This ensures that no threads could be added to the hash later.
3110 * We will use shutting_down as the barrier for now.
3112 g_assert (shutting_down);
3115 * We make multiple calls to WaitForMultipleObjects since:
3116 * - we can only wait for MAXIMUM_WAIT_OBJECTS threads
3117 * - some threads could exit without becoming suspended
3122 * Make a copy of the hashtable since we can't do anything with
3123 * threads while threads_mutex is held.
3126 /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
3127 memset (wait->threads, 0, MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
3128 mono_threads_lock ();
3129 mono_g_hash_table_foreach (threads, collect_threads_for_suspend, wait);
3130 mono_threads_unlock ();
3132 events = g_new0 (gpointer, wait->num);
3134 /* Get the suspended events that we'll be waiting for */
3135 for (i = 0; i < wait->num; ++i) {
3136 MonoInternalThread *thread = wait->threads [i];
3137 gboolean signal_suspend = FALSE;
3139 if ((thread->tid == self) || mono_gc_is_finalizer_internal_thread (thread) || (thread->flags & MONO_THREAD_FLAG_DONT_MANAGE)) {
3140 //CloseHandle (wait->handles [i]);
3141 wait->threads [i] = NULL; /* ignore this thread in next loop */
3145 ensure_synch_cs_set (thread);
3147 EnterCriticalSection (thread->synch_cs);
3149 if (thread->suspended_event == NULL) {
3150 thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
3151 if (thread->suspended_event == NULL) {
3152 /* Forget this one and go on to the next */
3153 LeaveCriticalSection (thread->synch_cs);
3158 if ((thread->state & ThreadState_Suspended) != 0 ||
3159 (thread->state & ThreadState_StopRequested) != 0 ||
3160 (thread->state & ThreadState_Stopped) != 0) {
3161 LeaveCriticalSection (thread->synch_cs);
3162 CloseHandle (wait->handles [i]);
3163 wait->threads [i] = NULL; /* ignore this thread in next loop */
3167 if ((thread->state & ThreadState_SuspendRequested) == 0)
3168 signal_suspend = TRUE;
3170 events [eventidx++] = thread->suspended_event;
3172 /* Convert abort requests into suspend requests */
3173 if ((thread->state & ThreadState_AbortRequested) != 0)
3174 thread->state &= ~ThreadState_AbortRequested;
3176 thread->state |= ThreadState_SuspendRequested;
3178 LeaveCriticalSection (thread->synch_cs);
3180 /* Signal the thread to suspend */
3181 if (mono_thread_info_new_interrupt_enabled ())
3182 suspend_thread_internal (thread, TRUE);
3183 else if (signal_suspend)
3184 signal_thread_state_change (thread);
3187 /*Only wait on the suspend event if we are using the old path */
3188 if (eventidx > 0 && !mono_thread_info_new_interrupt_enabled ()) {
3189 WaitForMultipleObjectsEx (eventidx, events, TRUE, 100, FALSE);
3190 for (i = 0; i < wait->num; ++i) {
3191 MonoInternalThread *thread = wait->threads [i];
3196 ensure_synch_cs_set (thread);
3198 EnterCriticalSection (thread->synch_cs);
3199 if ((thread->state & ThreadState_Suspended) != 0) {
3200 CloseHandle (thread->suspended_event);
3201 thread->suspended_event = NULL;
3203 LeaveCriticalSection (thread->synch_cs);
3207 if (eventidx <= 0) {
3209 * If there are threads which are starting up, we wait until they
3210 * are suspended when they try to register in the threads hash.
3211 * This is guaranteed to finish, since the threads which can create new
3212 * threads get suspended after a while.
3213 * FIXME: The finalizer thread can still create new threads.
3215 mono_threads_lock ();
3216 if (threads_starting_up)
3217 starting = mono_g_hash_table_size (threads_starting_up) > 0;
3220 mono_threads_unlock ();
3232 collect_threads (gpointer key, gpointer value, gpointer user_data)
3234 MonoInternalThread *thread = (MonoInternalThread*)value;
3235 struct wait_data *wait = (struct wait_data*)user_data;
3238 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
3239 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
3243 wait->handles [wait->num] = handle;
3244 wait->threads [wait->num] = thread;
3249 static gboolean thread_dump_requested;
3251 static G_GNUC_UNUSED gboolean
3252 print_stack_frame_to_string (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer data)
3254 GString *p = (GString*)data;
3255 MonoMethod *method = NULL;
3257 method = mono_jit_info_get_method (frame->ji);
3260 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
3261 g_string_append_printf (p, " %s\n", location);
3264 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
3270 print_thread_dump (MonoInternalThread *thread, MonoThreadInfo *info)
3272 GString* text = g_string_new (0);
3274 GError *error = NULL;
3277 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
3279 g_string_append_printf (text, "\n\"%s\"", name);
3282 else if (thread->threadpool_thread)
3283 g_string_append (text, "\n\"<threadpool thread>\"");
3285 g_string_append (text, "\n\"<unnamed thread>\"");
3288 /* This no longer works with remote unwinding */
3290 wapi_desc = wapi_current_thread_desc ();
3291 g_string_append_printf (text, " tid=0x%p this=0x%p %s\n", (gpointer)(gsize)thread->tid, thread, wapi_desc);
3296 mono_get_eh_callbacks ()->mono_walk_stack_with_state (print_stack_frame_to_string, &info->suspend_state, MONO_UNWIND_SIGNAL_SAFE, text);
3297 mono_thread_info_resume (mono_thread_info_get_tid (info));
3299 fprintf (stdout, "%s", text->str);
3301 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
3302 OutputDebugStringA(text->str);
3305 g_string_free (text, TRUE);
3310 dump_thread (gpointer key, gpointer value, gpointer user)
3312 MonoInternalThread *thread = (MonoInternalThread *)value;
3313 MonoThreadInfo *info;
3315 if (thread == mono_thread_internal_current ())
3319 FIXME This still can hang if we stop a thread during malloc.
3320 FIXME This can hang if we suspend on a critical method and the GC kicks in. A fix might be to have function
3321 that takes a callback and runs it with the target suspended.
3322 We probably should loop a bit around trying to get it to either managed code
3325 info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gpointer)(gsize)thread->tid, FALSE);
3330 print_thread_dump (thread, info);
3334 mono_threads_perform_thread_dump (void)
3336 if (!thread_dump_requested)
3339 printf ("Full thread dump:\n");
3342 * Make a copy of the hashtable since we can't do anything with
3343 * threads while threads_mutex is held.
3345 mono_threads_lock ();
3346 mono_g_hash_table_foreach (threads, dump_thread, NULL);
3347 mono_threads_unlock ();
3349 thread_dump_requested = FALSE;
3353 * mono_threads_request_thread_dump:
3355 * Ask all threads except the current to print their stacktrace to stdout.
3358 mono_threads_request_thread_dump (void)
3360 struct wait_data wait_data;
3361 struct wait_data *wait = &wait_data;
3364 /*The new thread dump code runs out of the finalizer thread. */
3365 if (mono_thread_info_new_interrupt_enabled ()) {
3366 thread_dump_requested = TRUE;
3367 mono_gc_finalize_notify ();
3372 memset (wait, 0, sizeof (struct wait_data));
3375 * Make a copy of the hashtable since we can't do anything with
3376 * threads while threads_mutex is held.
3378 mono_threads_lock ();
3379 mono_g_hash_table_foreach (threads, collect_threads, wait);
3380 mono_threads_unlock ();
3382 for (i = 0; i < wait->num; ++i) {
3383 MonoInternalThread *thread = wait->threads [i];
3385 if (!mono_gc_is_finalizer_internal_thread (thread) &&
3386 (thread != mono_thread_internal_current ()) &&
3387 !thread->thread_dump_requested) {
3388 thread->thread_dump_requested = TRUE;
3390 signal_thread_state_change (thread);
3393 CloseHandle (wait->handles [i]);
3399 gint allocated; /* +1 so that refs [allocated] == NULL */
3403 typedef struct ref_stack RefStack;
3406 ref_stack_new (gint initial_size)
3410 initial_size = MAX (initial_size, 16) + 1;
3411 rs = g_new0 (RefStack, 1);
3412 rs->refs = g_new0 (gpointer, initial_size);
3413 rs->allocated = initial_size;
3418 ref_stack_destroy (gpointer ptr)
3429 ref_stack_push (RefStack *rs, gpointer ptr)
3431 g_assert (rs != NULL);
3433 if (rs->bottom >= rs->allocated) {
3434 rs->refs = g_realloc (rs->refs, rs->allocated * 2 * sizeof (gpointer) + 1);
3435 rs->allocated <<= 1;
3436 rs->refs [rs->allocated] = NULL;
3438 rs->refs [rs->bottom++] = ptr;
3442 ref_stack_pop (RefStack *rs)
3444 if (rs == NULL || rs->bottom == 0)
3448 rs->refs [rs->bottom] = NULL;
3452 ref_stack_find (RefStack *rs, gpointer ptr)
3459 for (refs = rs->refs; refs && *refs; refs++) {
3467 * mono_thread_push_appdomain_ref:
3469 * Register that the current thread may have references to objects in domain
3470 * @domain on its stack. Each call to this function should be paired with a
3471 * call to pop_appdomain_ref.
3474 mono_thread_push_appdomain_ref (MonoDomain *domain)
3476 MonoInternalThread *thread = mono_thread_internal_current ();
3479 /* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
3480 SPIN_LOCK (thread->lock_thread_id);
3481 if (thread->appdomain_refs == NULL)
3482 thread->appdomain_refs = ref_stack_new (16);
3483 ref_stack_push (thread->appdomain_refs, domain);
3484 SPIN_UNLOCK (thread->lock_thread_id);
3489 mono_thread_pop_appdomain_ref (void)
3491 MonoInternalThread *thread = mono_thread_internal_current ();
3494 /* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
3495 SPIN_LOCK (thread->lock_thread_id);
3496 ref_stack_pop (thread->appdomain_refs);
3497 SPIN_UNLOCK (thread->lock_thread_id);
3502 mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain)
3505 SPIN_LOCK (thread->lock_thread_id);
3506 res = ref_stack_find (thread->appdomain_refs, domain);
3507 SPIN_UNLOCK (thread->lock_thread_id);
3512 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
3514 return mono_thread_internal_has_appdomain_ref (thread->internal_thread, domain);
3517 typedef struct abort_appdomain_data {
3518 struct wait_data wait;
3520 } abort_appdomain_data;
3523 collect_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
3525 MonoInternalThread *thread = (MonoInternalThread*)value;
3526 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
3527 MonoDomain *domain = data->domain;
3529 if (mono_thread_internal_has_appdomain_ref (thread, domain)) {
3530 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
3532 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
3533 HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
3536 data->wait.handles [data->wait.num] = handle;
3537 data->wait.threads [data->wait.num] = thread;
3540 /* Just ignore the rest, we can't do anything with
3548 * mono_threads_abort_appdomain_threads:
3550 * Abort threads which has references to the given appdomain.
3553 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
3555 #ifdef __native_client__
3559 abort_appdomain_data user_data;
3561 int orig_timeout = timeout;
3564 THREAD_DEBUG (g_message ("%s: starting abort", __func__));
3566 start_time = mono_msec_ticks ();
3568 mono_threads_lock ();
3570 user_data.domain = domain;
3571 user_data.wait.num = 0;
3572 /* This shouldn't take any locks */
3573 mono_g_hash_table_foreach (threads, collect_appdomain_thread, &user_data);
3574 mono_threads_unlock ();
3576 if (user_data.wait.num > 0) {
3577 /* Abort the threads outside the threads lock */
3578 for (i = 0; i < user_data.wait.num; ++i)
3579 ves_icall_System_Threading_Thread_Abort (user_data.wait.threads [i], NULL);
3582 * We should wait for the threads either to abort, or to leave the
3583 * domain. We can't do the latter, so we wait with a timeout.
3585 wait_for_tids (&user_data.wait, 100);
3588 /* Update remaining time */
3589 timeout -= mono_msec_ticks () - start_time;
3590 start_time = mono_msec_ticks ();
3592 if (orig_timeout != -1 && timeout < 0)
3595 while (user_data.wait.num > 0);
3597 THREAD_DEBUG (g_message ("%s: abort done", __func__));
3603 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
3605 MonoInternalThread *thread = (MonoInternalThread*)value;
3606 MonoDomain *domain = (MonoDomain*)user_data;
3609 /* No locking needed here */
3610 /* FIXME: why no locking? writes to the cache are protected with synch_cs above */
3612 if (thread->cached_culture_info) {
3613 for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i) {
3614 MonoObject *obj = mono_array_get (thread->cached_culture_info, MonoObject*, i);
3615 if (obj && obj->vtable->domain == domain)
3616 mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
3622 * mono_threads_clear_cached_culture:
3624 * Clear the cached_current_culture from all threads if it is in the
3628 mono_threads_clear_cached_culture (MonoDomain *domain)
3630 mono_threads_lock ();
3631 mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
3632 mono_threads_unlock ();
3636 * mono_thread_get_undeniable_exception:
3638 * Return an exception which needs to be raised when leaving a catch clause.
3639 * This is used for undeniable exception propagation.
3642 mono_thread_get_undeniable_exception (void)
3644 MonoInternalThread *thread = mono_thread_internal_current ();
3646 if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
3648 * FIXME: Clear the abort exception and return an AppDomainUnloaded
3649 * exception if the thread no longer references a dying appdomain.
3651 thread->abort_exc->trace_ips = NULL;
3652 thread->abort_exc->stack_trace = NULL;
3653 return thread->abort_exc;
3659 #if MONO_SMALL_CONFIG
3660 #define NUM_STATIC_DATA_IDX 4
3661 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
3665 #define NUM_STATIC_DATA_IDX 8
3666 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
3667 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
3671 static uintptr_t* static_reference_bitmaps [NUM_STATIC_DATA_IDX];
3674 mark_tls_slots (void *addr, MonoGCMarkFunc mark_func)
3677 gpointer *static_data = addr;
3678 for (i = 0; i < NUM_STATIC_DATA_IDX; ++i) {
3681 if (!static_data [i])
3683 numwords = 1 + static_data_size [i] / sizeof (gpointer) / (sizeof(uintptr_t) * 8);
3684 ptr = static_data [i];
3685 for (j = 0; j < numwords; ++j, ptr += sizeof (uintptr_t) * 8) {
3686 uintptr_t bmap = static_reference_bitmaps [i][j];
3689 if ((bmap & 1) && *p) {
3700 * mono_alloc_static_data
3702 * Allocate memory blocks for storing threads or context static data
3705 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset, gboolean threadlocal)
3707 guint idx = (offset >> 24) - 1;
3710 gpointer* static_data = *static_data_ptr;
3712 static void* tls_desc = NULL;
3713 if (mono_gc_user_markers_supported () && !tls_desc)
3714 tls_desc = mono_gc_make_root_descr_user (mark_tls_slots);
3715 static_data = mono_gc_alloc_fixed (static_data_size [0], threadlocal?tls_desc:NULL);
3716 *static_data_ptr = static_data;
3717 static_data [0] = static_data;
3720 for (i = 1; i <= idx; ++i) {
3721 if (static_data [i])
3723 if (mono_gc_user_markers_supported () && threadlocal)
3724 static_data [i] = g_malloc0 (static_data_size [i]);
3726 static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
3731 mono_free_static_data (gpointer* static_data, gboolean threadlocal)
3734 for (i = 1; i < NUM_STATIC_DATA_IDX; ++i) {
3735 gpointer p = static_data [i];
3739 * At this point, the static data pointer array is still registered with the
3740 * GC, so must ensure that mark_tls_slots() will not encounter any invalid
3741 * data. Freeing the individual arrays without first nulling their slots
3742 * would make it possible for mark_tls_slots() to encounter a pointer to
3743 * such an already freed array. See bug #13813.
3745 static_data [i] = NULL;
3746 mono_memory_write_barrier ();
3747 if (mono_gc_user_markers_supported () && threadlocal)
3750 mono_gc_free_fixed (p);
3752 mono_gc_free_fixed (static_data);
3756 * mono_init_static_data_info
3758 * Initializes static data counters
3760 static void mono_init_static_data_info (StaticDataInfo *static_data)
3762 static_data->idx = 0;
3763 static_data->offset = 0;
3764 static_data->freelist = NULL;
3768 * mono_alloc_static_data_slot
3770 * Generates an offset for static data. static_data contains the counters
3771 * used to generate it.
3774 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
3778 if (!static_data->idx && !static_data->offset) {
3780 * we use the first chunk of the first allocation also as
3781 * an array for the rest of the data
3783 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
3785 static_data->offset += align - 1;
3786 static_data->offset &= ~(align - 1);
3787 if (static_data->offset + size >= static_data_size [static_data->idx]) {
3788 static_data->idx ++;
3789 g_assert (size <= static_data_size [static_data->idx]);
3790 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
3791 static_data->offset = 0;
3793 offset = static_data->offset | ((static_data->idx + 1) << 24);
3794 static_data->offset += size;
3799 * ensure thread static fields already allocated are valid for thread
3800 * This function is called when a thread is created or on thread attach.
3803 thread_adjust_static_data (MonoInternalThread *thread)
3807 mono_threads_lock ();
3808 if (thread_static_info.offset || thread_static_info.idx > 0) {
3809 /* get the current allocated size */
3810 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
3811 mono_alloc_static_data (&(thread->static_data), offset, TRUE);
3813 mono_threads_unlock ();
3817 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
3819 MonoInternalThread *thread = value;
3820 guint32 offset = GPOINTER_TO_UINT (user);
3822 mono_alloc_static_data (&(thread->static_data), offset, TRUE);
3825 static MonoThreadDomainTls*
3826 search_tls_slot_in_freelist (StaticDataInfo *static_data, guint32 size, guint32 align)
3828 MonoThreadDomainTls* prev = NULL;
3829 MonoThreadDomainTls* tmp = static_data->freelist;
3831 if (tmp->size == size) {
3833 prev->next = tmp->next;
3835 static_data->freelist = tmp->next;
3844 update_tls_reference_bitmap (guint32 offset, uintptr_t *bitmap, int numbits)
3847 int idx = (offset >> 24) - 1;
3849 if (!static_reference_bitmaps [idx])
3850 static_reference_bitmaps [idx] = g_new0 (uintptr_t, 1 + static_data_size [idx] / sizeof(gpointer) / (sizeof(uintptr_t) * 8));
3851 rb = static_reference_bitmaps [idx];
3853 offset /= sizeof (gpointer);
3854 /* offset is now the bitmap offset */
3855 for (i = 0; i < numbits; ++i) {
3856 if (bitmap [i / sizeof (uintptr_t)] & (1L << (i & (sizeof (uintptr_t) * 8 -1))))
3857 rb [(offset + i) / (sizeof (uintptr_t) * 8)] |= (1L << ((offset + i) & (sizeof (uintptr_t) * 8 -1)));
3862 clear_reference_bitmap (guint32 offset, guint32 size)
3864 int idx = (offset >> 24) - 1;
3866 rb = static_reference_bitmaps [idx];
3868 offset /= sizeof (gpointer);
3869 size /= sizeof (gpointer);
3871 /* offset is now the bitmap offset */
3872 for (; offset < size; ++offset)
3873 rb [offset / (sizeof (uintptr_t) * 8)] &= ~(1L << (offset & (sizeof (uintptr_t) * 8 -1)));
3877 * The offset for a special static variable is composed of three parts:
3878 * a bit that indicates the type of static data (0:thread, 1:context),
3879 * an index in the array of chunks of memory for the thread (thread->static_data)
3880 * and an offset in that chunk of mem. This allows allocating less memory in the
3885 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align, uintptr_t *bitmap, int numbits)
3888 if (static_type == SPECIAL_STATIC_THREAD) {
3889 MonoThreadDomainTls *item;
3890 mono_threads_lock ();
3891 item = search_tls_slot_in_freelist (&thread_static_info, size, align);
3892 /*g_print ("TLS alloc: %d in domain %p (total: %d), cached: %p\n", size, mono_domain_get (), thread_static_info.offset, item);*/
3894 offset = item->offset;
3897 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
3899 update_tls_reference_bitmap (offset, bitmap, numbits);
3900 /* This can be called during startup */
3901 if (threads != NULL)
3902 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
3903 mono_threads_unlock ();
3905 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
3906 mono_contexts_lock ();
3907 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
3908 mono_contexts_unlock ();
3909 offset |= 0x80000000; /* Set the high bit to indicate context static data */
3915 mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset)
3917 /* The high bit means either thread (0) or static (1) data. */
3919 guint32 static_type = (offset & 0x80000000);
3922 offset &= 0x7fffffff;
3923 idx = (offset >> 24) - 1;
3925 if (static_type == 0) {
3926 return get_thread_static_data (thread, offset);
3928 /* Allocate static data block under demand, since we don't have a list
3931 MonoAppContext *context = mono_context_get ();
3932 if (!context->static_data || !context->static_data [idx]) {
3933 mono_contexts_lock ();
3934 mono_alloc_static_data (&(context->static_data), offset, FALSE);
3935 mono_contexts_unlock ();
3937 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
3942 mono_get_special_static_data (guint32 offset)
3944 return mono_get_special_static_data_for_thread (mono_thread_internal_current (), offset);
3953 free_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
3955 MonoInternalThread *thread = value;
3956 TlsOffsetSize *data = user;
3957 int idx = (data->offset >> 24) - 1;
3960 if (!thread->static_data || !thread->static_data [idx])
3962 ptr = ((char*) thread->static_data [idx]) + (data->offset & 0xffffff);
3963 mono_gc_bzero (ptr, data->size);
3967 do_free_special_slot (guint32 offset, guint32 size)
3969 guint32 static_type = (offset & 0x80000000);
3970 /*g_print ("free %s , size: %d, offset: %x\n", field->name, size, offset);*/
3971 if (static_type == 0) {
3973 MonoThreadDomainTls *item = g_new0 (MonoThreadDomainTls, 1);
3974 data.offset = offset & 0x7fffffff;
3976 clear_reference_bitmap (data.offset, data.size);
3977 if (threads != NULL)
3978 mono_g_hash_table_foreach (threads, free_thread_static_data_helper, &data);
3979 item->offset = offset;
3982 if (!mono_runtime_is_shutting_down ()) {
3983 item->next = thread_static_info.freelist;
3984 thread_static_info.freelist = item;
3986 /* We could be called during shutdown after mono_thread_cleanup () is called */
3990 /* FIXME: free context static data as well */
3995 do_free_special (gpointer key, gpointer value, gpointer data)
3997 MonoClassField *field = key;
3998 guint32 offset = GPOINTER_TO_UINT (value);
4001 size = mono_type_size (field->type, &align);
4002 do_free_special_slot (offset, size);
4006 mono_alloc_special_static_data_free (GHashTable *special_static_fields)
4008 mono_threads_lock ();
4009 g_hash_table_foreach (special_static_fields, do_free_special, NULL);
4010 mono_threads_unlock ();
4014 mono_special_static_data_free_slot (guint32 offset, guint32 size)
4016 mono_threads_lock ();
4017 do_free_special_slot (offset, size);
4018 mono_threads_unlock ();
4022 * allocates room in the thread local area for storing an instance of the struct type
4023 * the allocation is kept track of in domain->tlsrec_list.
4026 mono_thread_alloc_tls (MonoReflectionType *type)
4028 MonoDomain *domain = mono_domain_get ();
4030 MonoTlsDataRecord *tlsrec;
4033 gsize default_bitmap [4] = {0};
4034 uint32_t tls_offset;
4038 klass = mono_class_from_mono_type (type->type);
4039 /* TlsDatum is a struct, so we subtract the object header size offset */
4040 bitmap = mono_class_compute_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
4041 size = mono_type_size (type->type, &align);
4042 tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, (uintptr_t*)bitmap, max_set + 1);
4043 if (bitmap != default_bitmap)
4045 tlsrec = g_new0 (MonoTlsDataRecord, 1);
4046 tlsrec->tls_offset = tls_offset;
4047 tlsrec->size = size;
4048 mono_domain_lock (domain);
4049 tlsrec->next = domain->tlsrec_list;
4050 domain->tlsrec_list = tlsrec;
4051 mono_domain_unlock (domain);
4056 mono_thread_destroy_tls (uint32_t tls_offset)
4058 MonoTlsDataRecord *prev = NULL;
4059 MonoTlsDataRecord *cur;
4061 MonoDomain *domain = mono_domain_get ();
4062 mono_domain_lock (domain);
4063 cur = domain->tlsrec_list;
4065 if (cur->tls_offset == tls_offset) {
4067 prev->next = cur->next;
4069 domain->tlsrec_list = cur->next;
4077 mono_domain_unlock (domain);
4079 mono_special_static_data_free_slot (tls_offset, size);
4083 * This is just to ensure cleanup: the finalizers should have taken care, so this is not perf-critical.
4086 mono_thread_destroy_domain_tls (MonoDomain *domain)
4088 while (domain->tlsrec_list)
4089 mono_thread_destroy_tls (domain->tlsrec_list->tls_offset);
4092 static MonoClassField *local_slots = NULL;
4095 /* local tls data to get locals_slot from a thread */
4098 /* index in the locals_slot array */
4103 clear_local_slot (gpointer key, gpointer value, gpointer user_data)
4105 LocalSlotID *sid = user_data;
4106 MonoInternalThread *thread = (MonoInternalThread*)value;
4107 MonoArray *slots_array;
4109 * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
4110 * it is for the right domain, so we need to check if it is allocated an initialized
4111 * for the current thread.
4113 /*g_print ("handling thread %p\n", thread);*/
4114 if (!thread->static_data || !thread->static_data [sid->idx])
4116 slots_array = *(MonoArray **)(((char*) thread->static_data [sid->idx]) + (sid->offset & 0xffffff));
4117 if (!slots_array || sid->slot >= mono_array_length (slots_array))
4119 mono_array_set (slots_array, MonoObject*, sid->slot, NULL);
4123 mono_thread_free_local_slot_values (int slot, MonoBoolean thread_local)
4131 local_slots = mono_class_get_field_from_name (mono_defaults.thread_class, "local_slots");
4133 g_warning ("local_slots field not found in Thread class");
4137 domain = mono_domain_get ();
4138 mono_domain_lock (domain);
4139 if (domain->special_static_fields)
4140 addr = g_hash_table_lookup (domain->special_static_fields, local_slots);
4141 mono_domain_unlock (domain);
4144 /*g_print ("freeing slot %d at %p\n", slot, addr);*/
4145 sid.offset = GPOINTER_TO_UINT (addr);
4146 sid.offset &= 0x7fffffff;
4147 sid.idx = (sid.offset >> 24) - 1;
4148 mono_threads_lock ();
4149 mono_g_hash_table_foreach (threads, clear_local_slot, &sid);
4150 mono_threads_unlock ();
4152 /* FIXME: clear the slot for MonoAppContexts, too */
4157 static void CALLBACK dummy_apc (ULONG_PTR param)
4161 static guint32 dummy_apc (gpointer param)
4168 * mono_thread_execute_interruption
4170 * Performs the operation that the requested thread state requires (abort,
4173 static MonoException* mono_thread_execute_interruption (MonoInternalThread *thread)
4175 ensure_synch_cs_set (thread);
4177 EnterCriticalSection (thread->synch_cs);
4179 /* MonoThread::interruption_requested can only be changed with atomics */
4180 if (InterlockedCompareExchange (&thread->interruption_requested, FALSE, TRUE)) {
4181 /* this will consume pending APC calls */
4182 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
4183 InterlockedDecrement (&thread_interruption_requested);
4185 /* Clear the interrupted flag of the thread so it can wait again */
4186 wapi_clear_interruption ();
4190 if ((thread->state & ThreadState_AbortRequested) != 0) {
4191 LeaveCriticalSection (thread->synch_cs);
4192 if (thread->abort_exc == NULL) {
4194 * This might be racy, but it has to be called outside the lock
4195 * since it calls managed code.
4197 MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
4199 return thread->abort_exc;
4201 else if ((thread->state & ThreadState_SuspendRequested) != 0) {
4202 self_suspend_internal (thread);
4205 else if ((thread->state & ThreadState_StopRequested) != 0) {
4206 /* FIXME: do this through the JIT? */
4208 LeaveCriticalSection (thread->synch_cs);
4210 mono_thread_exit ();
4212 } else if (thread->pending_exception) {
4215 exc = thread->pending_exception;
4216 thread->pending_exception = NULL;
4218 LeaveCriticalSection (thread->synch_cs);
4220 } else if (thread->thread_interrupt_requested) {
4222 thread->thread_interrupt_requested = FALSE;
4223 LeaveCriticalSection (thread->synch_cs);
4225 return(mono_get_exception_thread_interrupted ());
4228 LeaveCriticalSection (thread->synch_cs);
4234 * mono_thread_request_interruption
4236 * A signal handler can call this method to request the interruption of a
4237 * thread. The result of the interruption will depend on the current state of
4238 * the thread. If the result is an exception that needs to be throw, it is
4239 * provided as return value.
4242 mono_thread_request_interruption (gboolean running_managed)
4244 MonoInternalThread *thread = mono_thread_internal_current ();
4246 /* The thread may already be stopping */
4251 if (thread->interrupt_on_stop &&
4252 thread->state & ThreadState_StopRequested &&
4253 thread->state & ThreadState_Background)
4257 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
4259 InterlockedIncrement (&thread_interruption_requested);
4261 if (!running_managed || is_running_protected_wrapper ()) {
4262 /* Can't stop while in unmanaged code. Increase the global interruption
4263 request count. When exiting the unmanaged method the count will be
4264 checked and the thread will be interrupted. */
4267 if (mono_thread_notify_pending_exc_fn && !running_managed)
4268 /* The JIT will notify the thread about the interruption */
4269 /* This shouldn't take any locks */
4270 mono_thread_notify_pending_exc_fn ();
4272 /* this will awake the thread if it is in WaitForSingleObject
4274 /* Our implementation of this function ignores the func argument */
4275 QueueUserAPC ((PAPCFUNC)dummy_apc, thread->handle, NULL);
4279 return mono_thread_execute_interruption (thread);
4283 /*This function should be called by a thread after it has exited all of
4284 * its handle blocks at interruption time.*/
4286 mono_thread_resume_interruption (void)
4288 MonoInternalThread *thread = mono_thread_internal_current ();
4289 gboolean still_aborting;
4291 /* The thread may already be stopping */
4295 ensure_synch_cs_set (thread);
4296 EnterCriticalSection (thread->synch_cs);
4297 still_aborting = (thread->state & ThreadState_AbortRequested) != 0;
4298 LeaveCriticalSection (thread->synch_cs);
4300 /*This can happen if the protected block called Thread::ResetAbort*/
4301 if (!still_aborting)
4304 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
4306 InterlockedIncrement (&thread_interruption_requested);
4309 wapi_self_interrupt ();
4311 return mono_thread_execute_interruption (thread);
4314 gboolean mono_thread_interruption_requested ()
4316 if (thread_interruption_requested) {
4317 MonoInternalThread *thread = mono_thread_internal_current ();
4318 /* The thread may already be stopping */
4320 return (thread->interruption_requested);
4325 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
4327 MonoInternalThread *thread = mono_thread_internal_current ();
4329 /* The thread may already be stopping */
4333 mono_debugger_check_interruption ();
4335 if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
4336 MonoException* exc = mono_thread_execute_interruption (thread);
4337 if (exc) mono_raise_exception (exc);
4342 * Performs the interruption of the current thread, if one has been requested,
4343 * and the thread is not running a protected wrapper.
4345 void mono_thread_interruption_checkpoint ()
4347 mono_thread_interruption_checkpoint_request (FALSE);
4351 * Performs the interruption of the current thread, if one has been requested.
4353 void mono_thread_force_interruption_checkpoint ()
4355 mono_thread_interruption_checkpoint_request (TRUE);
4359 * mono_thread_get_and_clear_pending_exception:
4361 * Return any pending exceptions for the current thread and clear it as a side effect.
4364 mono_thread_get_and_clear_pending_exception (void)
4366 MonoInternalThread *thread = mono_thread_internal_current ();
4368 /* The thread may already be stopping */
4372 if (thread->interruption_requested && !is_running_protected_wrapper ()) {
4373 return mono_thread_execute_interruption (thread);
4376 if (thread->pending_exception) {
4377 MonoException *exc = thread->pending_exception;
4379 thread->pending_exception = NULL;
4387 * mono_set_pending_exception:
4389 * Set the pending exception of the current thread to EXC.
4390 * The exception will be thrown when execution returns to managed code.
4393 mono_set_pending_exception (MonoException *exc)
4395 MonoInternalThread *thread = mono_thread_internal_current ();
4397 /* The thread may already be stopping */
4401 MONO_OBJECT_SETREF (thread, pending_exception, exc);
4403 mono_thread_request_interruption (FALSE);
4407 * mono_thread_interruption_request_flag:
4409 * Returns the address of a flag that will be non-zero if an interruption has
4410 * been requested for a thread. The thread to interrupt may not be the current
4411 * thread, so an additional call to mono_thread_interruption_requested() or
4412 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
4415 gint32* mono_thread_interruption_request_flag ()
4417 return &thread_interruption_requested;
4421 mono_thread_init_apartment_state (void)
4424 MonoInternalThread* thread = mono_thread_internal_current ();
4426 /* Positive return value indicates success, either
4427 * S_OK if this is first CoInitialize call, or
4428 * S_FALSE if CoInitialize already called, but with same
4429 * threading model. A negative value indicates failure,
4430 * probably due to trying to change the threading model.
4432 if (CoInitializeEx(NULL, (thread->apartment_state == ThreadApartmentState_STA)
4433 ? COINIT_APARTMENTTHREADED
4434 : COINIT_MULTITHREADED) < 0) {
4435 thread->apartment_state = ThreadApartmentState_Unknown;
4441 mono_thread_cleanup_apartment_state (void)
4444 MonoInternalThread* thread = mono_thread_internal_current ();
4446 if (thread && thread->apartment_state != ThreadApartmentState_Unknown) {
4453 mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state)
4455 ensure_synch_cs_set (thread);
4457 EnterCriticalSection (thread->synch_cs);
4458 thread->state |= state;
4459 LeaveCriticalSection (thread->synch_cs);
4463 mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state)
4465 ensure_synch_cs_set (thread);
4467 EnterCriticalSection (thread->synch_cs);
4468 thread->state &= ~state;
4469 LeaveCriticalSection (thread->synch_cs);
4473 mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test)
4475 gboolean ret = FALSE;
4477 ensure_synch_cs_set (thread);
4479 EnterCriticalSection (thread->synch_cs);
4481 if ((thread->state & test) != 0) {
4485 LeaveCriticalSection (thread->synch_cs);
4490 //static MonoClassField *execution_context_field;
4493 get_execution_context_addr (void)
4495 MonoDomain *domain = mono_domain_get ();
4496 guint32 offset = domain->execution_context_field_offset;
4499 MonoClassField *field = mono_class_get_field_from_name (mono_defaults.thread_class, "_ec");
4502 g_assert (mono_class_try_get_vtable (domain, mono_defaults.appdomain_class));
4504 mono_domain_lock (domain);
4505 offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, field));
4506 mono_domain_unlock (domain);
4509 domain->execution_context_field_offset = offset;
4512 return (MonoObject**) mono_get_special_static_data (offset);
4516 mono_thread_get_execution_context (void)
4518 return *get_execution_context_addr ();
4522 mono_thread_set_execution_context (MonoObject *ec)
4524 *get_execution_context_addr () = ec;
4527 static gboolean has_tls_get = FALSE;
4530 mono_runtime_set_has_tls_get (gboolean val)
4536 mono_runtime_has_tls_get (void)
4542 mono_thread_kill (MonoInternalThread *thread, int signal)
4544 #ifdef __native_client__
4545 /* Workaround pthread_kill abort() in NaCl glibc. */
4549 /* Win32 uses QueueUserAPC and callers of this are guarded */
4550 g_assert_not_reached ();
4552 # ifdef PTHREAD_POINTER_ID
4553 return pthread_kill ((gpointer)(gsize)(thread->tid), mono_thread_get_abort_signal ());
4555 # ifdef PLATFORM_ANDROID
4556 if (thread->android_tid != 0) {
4558 int old_errno = errno;
4560 ret = tkill ((pid_t) thread->android_tid, signal);
4569 return pthread_kill (thread->tid, mono_thread_get_abort_signal ());
4571 return pthread_kill (thread->tid, mono_thread_get_abort_signal ());
4578 self_interrupt_thread (void *_unused)
4580 MonoThreadInfo *info = mono_thread_info_current ();
4581 MonoException *exc = mono_thread_execute_interruption (mono_thread_internal_current ());
4582 if (exc) /*We must use _with_context since we didn't trampoline into the runtime*/
4583 mono_raise_exception_with_context (exc, &info->suspend_state.ctx);
4584 g_assert_not_reached (); /*this MUST not happen since we can't resume from an async call*/
4588 mono_jit_info_match (MonoJitInfo *ji, gpointer ip)
4592 return ji->code_start <= ip && (char*)ip < (char*)ji->code_start + ji->code_size;
4596 last_managed (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer data)
4598 MonoJitInfo **dest = data;
4604 mono_thread_info_get_last_managed (MonoThreadInfo *info)
4606 MonoJitInfo *ji = NULL;
4609 mono_get_eh_callbacks ()->mono_walk_stack_with_state (last_managed, &info->suspend_state, MONO_UNWIND_SIGNAL_SAFE, &ji);
4614 abort_thread_internal (MonoInternalThread *thread, gboolean can_raise_exception, gboolean install_async_abort)
4617 MonoThreadInfo *info = NULL;
4618 gboolean protected_wrapper;
4619 gboolean running_managed;
4621 if (!mono_thread_info_new_interrupt_enabled ()) {
4622 signal_thread_state_change (thread);
4627 FIXME this is insanely broken, it doesn't cause interruption to happen
4628 synchronously since passing FALSE to mono_thread_request_interruption makes sure it returns NULL
4630 if (thread == mono_thread_internal_current ()) {
4631 /* Do it synchronously */
4632 MonoException *exc = mono_thread_request_interruption (can_raise_exception);
4634 mono_raise_exception (exc);
4636 wapi_interrupt_thread (thread->handle);
4641 /*FIXME we need to check 2 conditions here, request to interrupt this thread or if the target died*/
4642 if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, TRUE))) {
4646 if (mono_get_eh_callbacks ()->mono_install_handler_block_guard (&info->suspend_state)) {
4647 mono_thread_info_resume (mono_thread_info_get_tid (info));
4651 /*someone is already interrupting it*/
4652 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1) {
4653 mono_thread_info_resume (mono_thread_info_get_tid (info));
4656 InterlockedIncrement (&thread_interruption_requested);
4658 ji = mono_thread_info_get_last_managed (info);
4659 protected_wrapper = ji && mono_threads_is_critical_method (mono_jit_info_get_method (ji));
4660 running_managed = mono_jit_info_match (ji, MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
4662 if (!protected_wrapper && running_managed) {
4663 /*We are in managed code*/
4664 /*Set the thread to call */
4665 if (install_async_abort)
4666 mono_thread_info_setup_async_call (info, self_interrupt_thread, NULL);
4667 mono_thread_info_resume (mono_thread_info_get_tid (info));
4669 gpointer interrupt_handle;
4671 * This will cause waits to be broken.
4672 * It will also prevent the thread from entering a wait, so if the thread returns
4673 * from the wait before it receives the abort signal, it will just spin in the wait
4674 * functions in the io-layer until the signal handler calls QueueUserAPC which will
4678 interrupt_handle = wapi_prepare_interrupt_thread (thread->handle);
4680 mono_thread_info_resume (mono_thread_info_get_tid (info));
4682 wapi_finish_interrupt_thread (interrupt_handle);
4685 /*FIXME we need to wait for interruption to complete -- figure out how much into interruption we should wait for here*/
4689 transition_to_suspended (MonoInternalThread *thread)
4691 if ((thread->state & ThreadState_SuspendRequested) == 0) {
4692 g_assert (0); /*FIXME we should not reach this */
4693 /*Make sure we balance the suspend count.*/
4694 mono_thread_info_resume ((MonoNativeThreadId)(gpointer)(gsize)thread->tid);
4696 thread->state &= ~ThreadState_SuspendRequested;
4697 thread->state |= ThreadState_Suspended;
4698 mono_thread_info_finish_suspend ();
4700 LeaveCriticalSection (thread->synch_cs);
4704 suspend_thread_internal (MonoInternalThread *thread, gboolean interrupt)
4706 if (!mono_thread_info_new_interrupt_enabled ()) {
4707 signal_thread_state_change (thread);
4711 EnterCriticalSection (thread->synch_cs);
4712 if (thread == mono_thread_internal_current ()) {
4713 transition_to_suspended (thread);
4714 mono_thread_info_self_suspend ();
4716 MonoThreadInfo *info;
4718 gboolean protected_wrapper;
4719 gboolean running_managed;
4721 /*A null info usually means the thread is already dead. */
4722 if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, interrupt))) {
4723 LeaveCriticalSection (thread->synch_cs);
4727 ji = mono_thread_info_get_last_managed (info);
4728 protected_wrapper = ji && mono_threads_is_critical_method (mono_jit_info_get_method (ji));
4729 running_managed = mono_jit_info_match (ji, MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
4731 if (running_managed && !protected_wrapper) {
4732 transition_to_suspended (thread);
4734 gpointer interrupt_handle;
4736 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 0)
4737 InterlockedIncrement (&thread_interruption_requested);
4740 interrupt_handle = wapi_prepare_interrupt_thread (thread->handle);
4742 mono_thread_info_resume (mono_thread_info_get_tid (info));
4745 wapi_finish_interrupt_thread (interrupt_handle);
4747 LeaveCriticalSection (thread->synch_cs);
4752 /*This is called with @thread synch_cs held and it must release it*/
4754 self_suspend_internal (MonoInternalThread *thread)
4756 if (!mono_thread_info_new_interrupt_enabled ()) {
4757 thread->state &= ~ThreadState_SuspendRequested;
4758 thread->state |= ThreadState_Suspended;
4759 thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
4760 if (thread->suspend_event == NULL) {
4761 LeaveCriticalSection (thread->synch_cs);
4764 if (thread->suspended_event)
4765 SetEvent (thread->suspended_event);
4767 LeaveCriticalSection (thread->synch_cs);
4769 if (shutting_down) {
4770 /* After we left the lock, the runtime might shut down so everything becomes invalid */
4775 WaitForSingleObject (thread->suspend_event, INFINITE);
4777 EnterCriticalSection (thread->synch_cs);
4779 CloseHandle (thread->suspend_event);
4780 thread->suspend_event = NULL;
4781 thread->state &= ~ThreadState_Suspended;
4783 /* The thread that requested the resume will have replaced this event
4784 * and will be waiting for it
4786 SetEvent (thread->resume_event);
4788 LeaveCriticalSection (thread->synch_cs);
4792 transition_to_suspended (thread);
4793 mono_thread_info_self_suspend ();
4796 /*This is called with @thread synch_cs held and it must release it*/
4798 resume_thread_internal (MonoInternalThread *thread)
4800 if (!mono_thread_info_new_interrupt_enabled ()) {
4801 thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
4802 if (thread->resume_event == NULL) {
4803 LeaveCriticalSection (thread->synch_cs);
4807 /* Awake the thread */
4808 SetEvent (thread->suspend_event);
4810 LeaveCriticalSection (thread->synch_cs);
4812 /* Wait for the thread to awake */
4813 WaitForSingleObject (thread->resume_event, INFINITE);
4814 CloseHandle (thread->resume_event);
4815 thread->resume_event = NULL;
4819 LeaveCriticalSection (thread->synch_cs);
4820 /* Awake the thread */
4821 if (!mono_thread_info_resume ((MonoNativeThreadId)(gpointer)(gsize)thread->tid))
4823 EnterCriticalSection (thread->synch_cs);
4824 thread->state &= ~ThreadState_Suspended;
4825 LeaveCriticalSection (thread->synch_cs);
4831 * mono_thread_is_foreign:
4832 * @thread: the thread to query
4834 * This function allows one to determine if a thread was created by the mono runtime and has
4835 * a well defined lifecycle or it's a foreigh one, created by the native environment.
4837 * Returns: true if @thread was not created by the runtime.
4840 mono_thread_is_foreign (MonoThread *thread)
4842 MonoThreadInfo *info = thread->internal_thread->thread_info;
4843 return info->runtime_thread == FALSE;