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)
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);
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 /* This needs to be called even if handle_remove () fails */
394 if (mono_thread_cleanup_fn)
395 mono_thread_cleanup_fn (thread);
398 mono_release_type_locks (thread);
400 mono_profiler_thread_end (thread->tid);
402 if (thread == mono_thread_internal_current ()) {
404 * This will signal async signal handlers that the thread has exited.
405 * The profiler callback needs this to be set, so it cannot be done earlier.
407 mono_domain_unset ();
408 mono_memory_barrier ();
411 if (thread == mono_thread_internal_current ())
412 mono_thread_pop_appdomain_ref ();
414 thread->cached_culture_info = NULL;
416 mono_free_static_data (thread->static_data, TRUE);
417 thread->static_data = NULL;
418 ref_stack_destroy (thread->appdomain_refs);
419 thread->appdomain_refs = NULL;
421 if (mono_thread_cleanup_fn)
422 mono_thread_cleanup_fn (thread);
424 if (mono_gc_is_moving ()) {
425 MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
426 thread->thread_pinning_ref = NULL;
432 get_thread_static_data (MonoInternalThread *thread, guint32 offset)
435 g_assert ((offset & 0x80000000) == 0);
436 offset &= 0x7fffffff;
437 idx = (offset >> 24) - 1;
438 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
442 get_current_thread_ptr_for_domain (MonoDomain *domain, MonoInternalThread *thread)
444 static MonoClassField *current_thread_field = NULL;
448 if (!current_thread_field) {
449 current_thread_field = mono_class_get_field_from_name (mono_defaults.thread_class, "current_thread");
450 g_assert (current_thread_field);
453 mono_class_vtable (domain, mono_defaults.thread_class);
454 mono_domain_lock (domain);
455 offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, current_thread_field));
456 mono_domain_unlock (domain);
459 return get_thread_static_data (thread, offset);
463 set_current_thread_for_domain (MonoDomain *domain, MonoInternalThread *thread, MonoThread *current)
465 MonoThread **current_thread_ptr = get_current_thread_ptr_for_domain (domain, thread);
467 g_assert (current->obj.vtable->domain == domain);
469 g_assert (!*current_thread_ptr);
470 *current_thread_ptr = current;
473 static MonoInternalThread*
474 create_internal_thread_object (void)
476 MonoVTable *vt = mono_class_vtable (mono_get_root_domain (), mono_defaults.internal_thread_class);
477 return (MonoInternalThread*)mono_gc_alloc_mature (vt);
481 create_thread_object (MonoDomain *domain)
483 MonoVTable *vt = mono_class_vtable (domain, mono_defaults.thread_class);
484 return (MonoThread*)mono_gc_alloc_mature (vt);
488 new_thread_with_internal (MonoDomain *domain, MonoInternalThread *internal)
490 MonoThread *thread = create_thread_object (domain);
491 MONO_OBJECT_SETREF (thread, internal_thread, internal);
496 init_root_domain_thread (MonoInternalThread *thread, MonoThread *candidate)
498 MonoDomain *domain = mono_get_root_domain ();
500 if (!candidate || candidate->obj.vtable->domain != domain)
501 candidate = new_thread_with_internal (domain, thread);
502 set_current_thread_for_domain (domain, thread, candidate);
503 g_assert (!thread->root_domain_thread);
504 MONO_OBJECT_SETREF (thread, root_domain_thread, candidate);
507 static guint32 WINAPI start_wrapper_internal(void *data)
509 MonoThreadInfo *info;
510 struct StartInfo *start_info=(struct StartInfo *)data;
511 guint32 (*start_func)(void *);
515 * We don't create a local to hold start_info->obj, so hopefully it won't get pinned during a
518 MonoInternalThread *internal = start_info->obj->internal_thread;
519 MonoObject *start_delegate = start_info->delegate;
520 MonoDomain *domain = start_info->obj->obj.vtable->domain;
522 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper", __func__, GetCurrentThreadId ()));
524 /* We can be sure start_info->obj->tid and
525 * start_info->obj->handle have been set, because the thread
526 * was created suspended, and these values were set before the
530 info = mono_thread_info_current ();
532 internal->thread_info = info;
537 SET_CURRENT_OBJECT (internal);
539 mono_monitor_init_tls ();
541 /* Every thread references the appdomain which created it */
542 mono_thread_push_appdomain_ref (domain);
544 if (!mono_domain_set (domain, FALSE)) {
545 /* No point in raising an appdomain_unloaded exception here */
546 /* FIXME: Cleanup here */
547 mono_thread_pop_appdomain_ref ();
551 start_func = start_info->func;
552 start_arg = start_info->start_arg;
554 /* We have to do this here because mono_thread_new_init()
555 requires that root_domain_thread is set up. */
556 thread_adjust_static_data (internal);
557 init_root_domain_thread (internal, start_info->obj);
559 /* This MUST be called before any managed code can be
560 * executed, as it calls the callback function that (for the
561 * jit) sets the lmf marker.
563 mono_thread_new_init (tid, &tid, start_func);
564 internal->stack_ptr = &tid;
566 LIBGC_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT",%d) Setting thread stack to %p", __func__, GetCurrentThreadId (), getpid (), thread->stack_ptr));
568 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), internal));
570 /* On 2.0 profile (and higher), set explicitly since state might have been
572 if (internal->apartment_state == ThreadApartmentState_Unknown)
573 internal->apartment_state = ThreadApartmentState_MTA;
575 mono_thread_init_apartment_state ();
577 if(internal->start_notify!=NULL) {
578 /* Let the thread that called Start() know we're
581 ReleaseSemaphore (internal->start_notify, 1, NULL);
584 mono_threads_lock ();
585 mono_g_hash_table_remove (thread_start_args, start_info->obj);
586 mono_threads_unlock ();
588 mono_thread_set_execution_context (start_info->obj->ec_to_set);
589 start_info->obj->ec_to_set = NULL;
592 THREAD_DEBUG (g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
596 * Call this after calling start_notify, since the profiler callback might want
597 * to lock the thread, and the lock is held by thread_start () which waits for
600 mono_profiler_thread_start (tid);
602 /* start_func is set only for unmanaged start functions */
604 start_func (start_arg);
607 g_assert (start_delegate != NULL);
608 args [0] = start_arg;
609 /* we may want to handle the exception here. See comment below on unhandled exceptions */
610 mono_runtime_delegate_invoke (start_delegate, args, NULL);
613 /* If the thread calls ExitThread at all, this remaining code
614 * will not be executed, but the main thread will eventually
615 * call thread_cleanup() on this thread's behalf.
618 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper terminating", __func__, GetCurrentThreadId ()));
620 /* Do any cleanup needed for apartment state. This
621 * cannot be done in thread_cleanup since thread_cleanup could be
622 * called for a thread other than the current thread.
623 * mono_thread_cleanup_apartment_state cleans up apartment
624 * for the current thead */
625 mono_thread_cleanup_apartment_state ();
627 thread_cleanup (internal);
629 /* Remove the reference to the thread object in the TLS data,
630 * so the thread object can be finalized. This won't be
631 * reached if the thread threw an uncaught exception, so those
632 * thread handles will stay referenced :-( (This is due to
633 * missing support for scanning thread-specific data in the
634 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
637 SET_CURRENT_OBJECT (NULL);
642 static guint32 WINAPI start_wrapper(void *data)
646 /* Avoid scanning the frames above this frame during a GC */
647 mono_gc_set_stack_end ((void*)&dummy);
649 return start_wrapper_internal (data);
652 void mono_thread_new_init (intptr_t tid, gpointer stack_start, gpointer func)
654 if (mono_thread_start_cb) {
655 mono_thread_start_cb (tid, stack_start, func);
659 void mono_threads_set_default_stacksize (guint32 stacksize)
661 default_stacksize = stacksize;
664 guint32 mono_threads_get_default_stacksize (void)
666 return default_stacksize;
670 * mono_create_thread:
672 * This is a wrapper around CreateThread which handles differences in the type of
673 * the the 'tid' argument.
675 gpointer mono_create_thread (WapiSecurityAttributes *security,
676 guint32 stacksize, WapiThreadStart start,
677 gpointer param, guint32 create, gsize *tid)
684 res = mono_threads_CreateThread (security, stacksize, start, param, create, &real_tid);
688 res = CreateThread (security, stacksize, start, param, create, tid);
695 * The thread start argument may be an object reference, and there is
696 * no ref to keep it alive when the new thread is started but not yet
697 * registered with the collector. So we store it in a GC tracked hash
700 * LOCKING: Assumes the threads lock is held.
703 register_thread_start_argument (MonoThread *thread, struct StartInfo *start_info)
705 if (thread_start_args == NULL) {
706 MONO_GC_REGISTER_ROOT_FIXED (thread_start_args);
707 thread_start_args = mono_g_hash_table_new (NULL, NULL);
709 mono_g_hash_table_insert (thread_start_args, thread, start_info->start_arg);
713 * mono_thread_create_internal:
715 * 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
716 * signalled, which can cause shutdown crashes if the thread shutdown code accesses data already freed by the runtime shutdown.
717 * Currently, this is only used for the finalizer thread.
720 mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread, gboolean no_detach, guint32 stack_size)
723 MonoInternalThread *internal;
724 HANDLE thread_handle;
725 struct StartInfo *start_info;
727 guint32 create_flags;
729 thread = create_thread_object (domain);
730 internal = create_internal_thread_object ();
731 MONO_OBJECT_SETREF (thread, internal_thread, internal);
733 start_info=g_new0 (struct StartInfo, 1);
734 start_info->func = func;
735 start_info->obj = thread;
736 start_info->start_arg = arg;
738 mono_threads_lock ();
740 mono_threads_unlock ();
744 if (threads_starting_up == NULL) {
745 MONO_GC_REGISTER_ROOT_FIXED (threads_starting_up);
746 threads_starting_up = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
749 register_thread_start_argument (thread, start_info);
750 mono_g_hash_table_insert (threads_starting_up, thread, thread);
751 mono_threads_unlock ();
754 stack_size = default_stacksize_for_thread (internal);
756 /* Create suspended, so we can do some housekeeping before the thread
759 create_flags = CREATE_SUSPENDED;
762 create_flags |= CREATE_NO_DETACH;
764 thread_handle = mono_create_thread (NULL, stack_size, (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
766 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
767 if (thread_handle == NULL) {
768 /* The thread couldn't be created, so throw an exception */
769 mono_threads_lock ();
770 mono_g_hash_table_remove (threads_starting_up, thread);
771 mono_threads_unlock ();
773 mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
777 internal->handle=thread_handle;
779 internal->apartment_state=ThreadApartmentState_Unknown;
780 internal->managed_id = get_next_managed_thread_id ();
781 if (mono_gc_is_moving ()) {
782 internal->thread_pinning_ref = internal;
783 MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
786 internal->synch_cs = g_new0 (CRITICAL_SECTION, 1);
787 InitializeCriticalSection (internal->synch_cs);
789 internal->threadpool_thread = threadpool_thread;
790 if (threadpool_thread)
791 mono_thread_set_state (internal, ThreadState_Background);
793 if (handle_store (thread))
794 ResumeThread (thread_handle);
796 /* Check that the managed and unmanaged layout of MonoInternalThread matches */
797 if (mono_check_corlib_version () == NULL)
798 g_assert (((char*)&internal->unused2 - (char*)internal) == mono_defaults.internal_thread_class->fields [mono_defaults.internal_thread_class->field.count - 1].offset);
804 mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
806 mono_thread_create_internal (domain, func, arg, FALSE, FALSE, 0);
809 #if defined(HOST_WIN32) && defined(__GNUC__) && defined(TARGET_X86)
810 static __inline__ __attribute__((always_inline))
811 /* This is not defined by gcc */
813 __readfsdword (unsigned long long offset)
815 unsigned long long value;
816 __asm__("movl %%fs:%a[offset], %k[value]" : [value] "=q" (value) : [offset] "irm" (offset));
822 * mono_thread_get_stack_bounds:
824 * Return the address and size of the current threads stack. Return NULL as the
825 * stack address if the stack address cannot be determined.
828 mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
830 #if defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
831 *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self ());
832 *stsize = pthread_get_stacksize_np (pthread_self ());
834 /* staddr points to the start of the stack, not the end */
836 *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
838 /* FIXME: simplify the mess below */
839 #elif defined(HOST_WIN32)
841 /* http://en.wikipedia.org/wiki/Win32_Thread_Information_Block */
842 void* tib = (void*)__readfsdword(0x18);
843 guint8 *stackTop = (guint8*)*(int*)((char*)tib + 4);
844 guint8 *stackBottom = (guint8*)*(int*)((char*)tib + 8);
846 *staddr = stackBottom;
847 *stsize = stackTop - stackBottom;
850 *stsize = (size_t)-1;
855 guint8 *current = (guint8*)&attr;
858 *stsize = (size_t)-1;
860 pthread_attr_init (&attr);
861 # ifdef HAVE_PTHREAD_GETATTR_NP
862 pthread_getattr_np (pthread_self(), &attr);
864 # ifdef HAVE_PTHREAD_ATTR_GET_NP
865 pthread_attr_get_np (pthread_self(), &attr);
868 pthread_attr_getstacksize (&attr, &stsize);
869 # elif defined(__OpenBSD__)
873 rslt = pthread_stackseg_np(pthread_self(), &ss);
874 g_assert (rslt == 0);
876 *staddr = (guint8*)((size_t)ss.ss_sp - ss.ss_size);
877 *stsize = ss.ss_size;
886 # if !defined(__OpenBSD__)
887 pthread_attr_getstack (&attr, (void**)staddr, stsize);
890 g_assert ((current > *staddr) && (current < *staddr + *stsize));
893 pthread_attr_destroy (&attr);
896 /* When running under emacs, sometimes staddr is not aligned to a page size */
897 *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
901 mono_thread_attach (MonoDomain *domain)
903 MonoInternalThread *thread;
904 MonoThread *current_thread;
905 HANDLE thread_handle;
908 if ((thread = mono_thread_internal_current ())) {
909 if (domain != mono_domain_get ())
910 mono_domain_set (domain, TRUE);
911 /* Already attached */
912 return mono_thread_current ();
915 if (!mono_gc_register_thread (&domain)) {
916 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 ());
919 thread = create_internal_thread_object ();
921 thread_handle = GetCurrentThread ();
922 g_assert (thread_handle);
924 tid=GetCurrentThreadId ();
927 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
928 * refer to the thread from other threads for things like aborting.
930 DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
931 THREAD_ALL_ACCESS, TRUE, 0);
933 thread->handle=thread_handle;
935 #ifdef PLATFORM_ANDROID
936 thread->android_tid = (gpointer) gettid ();
938 thread->apartment_state=ThreadApartmentState_Unknown;
939 thread->managed_id = get_next_managed_thread_id ();
940 if (mono_gc_is_moving ()) {
941 thread->thread_pinning_ref = thread;
942 MONO_GC_REGISTER_ROOT_PINNING (thread->thread_pinning_ref);
945 thread->stack_ptr = &tid;
947 thread->synch_cs = g_new0 (CRITICAL_SECTION, 1);
948 InitializeCriticalSection (thread->synch_cs);
950 THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
952 current_thread = new_thread_with_internal (domain, thread);
954 if (!handle_store (current_thread)) {
955 /* Mono is shutting down, so just wait for the end */
960 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
962 SET_CURRENT_OBJECT (thread);
963 mono_domain_set (domain, TRUE);
965 mono_monitor_init_tls ();
967 thread_adjust_static_data (thread);
969 init_root_domain_thread (thread, current_thread);
970 if (domain != mono_get_root_domain ())
971 set_current_thread_for_domain (domain, thread, current_thread);
974 if (mono_thread_attach_cb) {
978 mono_thread_get_stack_bounds (&staddr, &stsize);
981 mono_thread_attach_cb (tid, &tid);
983 mono_thread_attach_cb (tid, staddr + stsize);
986 // FIXME: Need a separate callback
987 mono_profiler_thread_start (tid);
989 return current_thread;
993 mono_thread_detach (MonoThread *thread)
995 g_return_if_fail (thread != NULL);
997 THREAD_DEBUG (g_message ("%s: mono_thread_detach for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->internal_thread->tid));
999 mono_profiler_thread_end (thread->internal_thread->tid);
1001 thread_cleanup (thread->internal_thread);
1003 SET_CURRENT_OBJECT (NULL);
1004 mono_domain_unset ();
1006 /* Don't need to CloseHandle this thread, even though we took a
1007 * reference in mono_thread_attach (), because the GC will do it
1008 * when the Thread object is finalised.
1015 MonoInternalThread *thread = mono_thread_internal_current ();
1017 THREAD_DEBUG (g_message ("%s: mono_thread_exit for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
1019 thread_cleanup (thread);
1020 SET_CURRENT_OBJECT (NULL);
1021 mono_domain_unset ();
1023 /* we could add a callback here for embedders to use. */
1024 if (mono_thread_get_main () && (thread == mono_thread_get_main ()->internal_thread))
1025 exit (mono_environment_exitcode_get ());
1030 ves_icall_System_Threading_Thread_ConstructInternalThread (MonoThread *this)
1032 MonoInternalThread *internal = create_internal_thread_object ();
1034 internal->state = ThreadState_Unstarted;
1035 internal->apartment_state = ThreadApartmentState_Unknown;
1036 internal->managed_id = get_next_managed_thread_id ();
1038 InterlockedCompareExchangePointer ((gpointer)&this->internal_thread, internal, NULL);
1041 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
1044 guint32 (*start_func)(void *);
1045 struct StartInfo *start_info;
1048 MonoInternalThread *internal;
1050 THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__, this, start));
1052 if (!this->internal_thread)
1053 ves_icall_System_Threading_Thread_ConstructInternalThread (this);
1054 internal = this->internal_thread;
1056 ensure_synch_cs_set (internal);
1058 EnterCriticalSection (internal->synch_cs);
1060 if ((internal->state & ThreadState_Unstarted) == 0) {
1061 LeaveCriticalSection (internal->synch_cs);
1062 mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
1066 if ((internal->state & ThreadState_Aborted) != 0) {
1067 LeaveCriticalSection (internal->synch_cs);
1072 /* This is freed in start_wrapper */
1073 start_info = g_new0 (struct StartInfo, 1);
1074 start_info->func = start_func;
1075 start_info->start_arg = this->start_obj; /* FIXME: GC object stored in unmanaged memory */
1076 start_info->delegate = start;
1077 start_info->obj = this;
1078 g_assert (this->obj.vtable->domain == mono_domain_get ());
1080 internal->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
1081 if (internal->start_notify==NULL) {
1082 LeaveCriticalSection (internal->synch_cs);
1083 g_warning ("%s: CreateSemaphore error 0x%x", __func__, GetLastError ());
1084 g_free (start_info);
1088 mono_threads_lock ();
1089 register_thread_start_argument (this, start_info);
1090 if (threads_starting_up == NULL) {
1091 MONO_GC_REGISTER_ROOT_FIXED (threads_starting_up);
1092 threads_starting_up = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
1094 mono_g_hash_table_insert (threads_starting_up, this, this);
1095 mono_threads_unlock ();
1097 thread=mono_create_thread(NULL, default_stacksize_for_thread (internal), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
1098 CREATE_SUSPENDED, &tid);
1100 LeaveCriticalSection (internal->synch_cs);
1101 mono_threads_lock ();
1102 mono_g_hash_table_remove (threads_starting_up, this);
1103 mono_threads_unlock ();
1104 g_warning("%s: CreateThread error 0x%x", __func__, GetLastError());
1108 internal->handle=thread;
1110 if (mono_gc_is_moving ()) {
1111 internal->thread_pinning_ref = internal;
1112 MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
1116 /* Don't call handle_store() here, delay it to Start.
1117 * We can't join a thread (trying to will just block
1118 * forever) until it actually starts running, so don't
1119 * store the handle till then.
1122 mono_thread_start (this);
1124 internal->state &= ~ThreadState_Unstarted;
1126 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread));
1128 LeaveCriticalSection (internal->synch_cs);
1133 void ves_icall_System_Threading_InternalThread_Thread_free_internal (MonoInternalThread *this, HANDLE thread)
1135 MONO_ARCH_SAVE_REGS;
1137 THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
1140 CloseHandle (thread);
1142 if (this->synch_cs) {
1143 CRITICAL_SECTION *synch_cs = this->synch_cs;
1144 this->synch_cs = NULL;
1145 DeleteCriticalSection (synch_cs);
1150 void *name = this->name;
1156 static void mono_thread_start (MonoThread *thread)
1158 MonoInternalThread *internal = thread->internal_thread;
1160 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), internal, (gsize)internal->tid));
1162 /* Only store the handle when the thread is about to be
1163 * launched, to avoid the main thread deadlocking while trying
1164 * to clean up a thread that will never be signalled.
1166 if (!handle_store (thread))
1169 ResumeThread (internal->handle);
1171 if(internal->start_notify!=NULL) {
1172 /* Wait for the thread to set up its TLS data etc, so
1173 * theres no potential race condition if someone tries
1174 * to look up the data believing the thread has
1178 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for thread %p (%"G_GSIZE_FORMAT") to start", __func__, GetCurrentThreadId (), internal, (gsize)internal->tid));
1180 WaitForSingleObjectEx (internal->start_notify, INFINITE, FALSE);
1181 CloseHandle (internal->start_notify);
1182 internal->start_notify = NULL;
1185 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Done launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), internal, (gsize)internal->tid));
1188 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
1191 MonoInternalThread *thread = mono_thread_internal_current ();
1193 THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
1195 mono_thread_current_check_pending_interrupt ();
1198 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1200 res = SleepEx(ms,TRUE);
1202 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1204 if (res == WAIT_IO_COMPLETION) { /* we might have been interrupted */
1205 MonoException* exc = mono_thread_execute_interruption (thread);
1207 mono_raise_exception (exc);
1219 void ves_icall_System_Threading_Thread_SpinWait_nop (void)
1224 ves_icall_System_Threading_Thread_GetDomainID (void)
1226 MONO_ARCH_SAVE_REGS;
1228 return mono_domain_get()->domain_id;
1232 ves_icall_System_Threading_Thread_Yield (void)
1235 return SwitchToThread ();
1237 return sched_yield () == 0;
1242 * mono_thread_get_name:
1244 * Return the name of the thread. NAME_LEN is set to the length of the name.
1245 * Return NULL if the thread has no name. The returned memory is owned by the
1249 mono_thread_get_name (MonoInternalThread *this_obj, guint32 *name_len)
1253 ensure_synch_cs_set (this_obj);
1255 EnterCriticalSection (this_obj->synch_cs);
1257 if (!this_obj->name) {
1261 *name_len = this_obj->name_len;
1262 res = g_new (gunichar2, this_obj->name_len);
1263 memcpy (res, this_obj->name, sizeof (gunichar2) * this_obj->name_len);
1266 LeaveCriticalSection (this_obj->synch_cs);
1272 ves_icall_System_Threading_Thread_GetName_internal (MonoInternalThread *this_obj)
1276 ensure_synch_cs_set (this_obj);
1278 EnterCriticalSection (this_obj->synch_cs);
1280 if (!this_obj->name)
1283 str = mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
1285 LeaveCriticalSection (this_obj->synch_cs);
1291 mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean managed)
1293 ensure_synch_cs_set (this_obj);
1295 EnterCriticalSection (this_obj->synch_cs);
1297 if (this_obj->flags & MONO_THREAD_FLAG_NAME_SET) {
1298 LeaveCriticalSection (this_obj->synch_cs);
1300 mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
1304 this_obj->name = g_new (gunichar2, mono_string_length (name));
1305 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
1306 this_obj->name_len = mono_string_length (name);
1309 this_obj->name = NULL;
1312 this_obj->flags |= MONO_THREAD_FLAG_NAME_SET;
1314 LeaveCriticalSection (this_obj->synch_cs);
1315 if (this_obj->name) {
1316 char *tname = mono_string_to_utf8 (name);
1317 mono_profiler_thread_name (this_obj->tid, tname);
1323 ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name)
1325 mono_thread_set_name_internal (this_obj, name, TRUE);
1328 /* If the array is already in the requested domain, we just return it,
1329 otherwise we return a copy in that domain. */
1331 byte_array_to_domain (MonoArray *arr, MonoDomain *domain)
1338 if (mono_object_domain (arr) == domain)
1341 copy = mono_array_new (domain, mono_defaults.byte_class, arr->max_length);
1342 mono_gc_memmove (mono_array_addr (copy, guint8, 0), mono_array_addr (arr, guint8, 0), arr->max_length);
1347 ves_icall_System_Threading_Thread_ByteArrayToRootDomain (MonoArray *arr)
1349 return byte_array_to_domain (arr, mono_get_root_domain ());
1353 ves_icall_System_Threading_Thread_ByteArrayToCurrentDomain (MonoArray *arr)
1355 return byte_array_to_domain (arr, mono_domain_get ());
1359 mono_thread_current (void)
1361 MonoDomain *domain = mono_domain_get ();
1362 MonoInternalThread *internal = mono_thread_internal_current ();
1363 MonoThread **current_thread_ptr;
1365 g_assert (internal);
1366 current_thread_ptr = get_current_thread_ptr_for_domain (domain, internal);
1368 if (!*current_thread_ptr) {
1369 g_assert (domain != mono_get_root_domain ());
1370 *current_thread_ptr = new_thread_with_internal (domain, internal);
1372 return *current_thread_ptr;
1376 mono_thread_internal_current (void)
1378 MonoInternalThread *res = GET_CURRENT_OBJECT ();
1379 THREAD_DEBUG (g_message ("%s: returning %p", __func__, res));
1383 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoInternalThread *this,
1384 int ms, HANDLE thread)
1386 MonoInternalThread *cur_thread = mono_thread_internal_current ();
1389 mono_thread_current_check_pending_interrupt ();
1391 ensure_synch_cs_set (this);
1393 EnterCriticalSection (this->synch_cs);
1395 if ((this->state & ThreadState_Unstarted) != 0) {
1396 LeaveCriticalSection (this->synch_cs);
1398 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
1402 LeaveCriticalSection (this->synch_cs);
1407 THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__, thread, ms));
1409 mono_thread_set_state (cur_thread, ThreadState_WaitSleepJoin);
1411 ret=WaitForSingleObjectEx (thread, ms, TRUE);
1413 mono_thread_clr_state (cur_thread, ThreadState_WaitSleepJoin);
1415 if(ret==WAIT_OBJECT_0) {
1416 THREAD_DEBUG (g_message ("%s: join successful", __func__));
1421 THREAD_DEBUG (g_message ("%s: join failed", __func__));
1427 mono_wait_uninterrupted (MonoInternalThread *thread, gboolean multiple, guint32 numhandles, gpointer *handles, gboolean waitall, gint32 ms, gboolean alertable)
1435 start = (ms == -1) ? 0 : mono_100ns_ticks ();
1438 ret = WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, alertable);
1440 ret = WaitForSingleObjectEx (handles [0], ms, alertable);
1442 if (ret != WAIT_IO_COMPLETION)
1445 exc = mono_thread_execute_interruption (thread);
1447 mono_raise_exception (exc);
1452 /* Re-calculate ms according to the time passed */
1453 diff_ms = (mono_100ns_ticks () - start) / 10000;
1454 if (diff_ms >= ms) {
1458 wait = ms - diff_ms;
1464 /* FIXME: exitContext isnt documented */
1465 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
1471 MonoObject *waitHandle;
1472 MonoInternalThread *thread = mono_thread_internal_current ();
1474 /* Do this WaitSleepJoin check before creating objects */
1475 mono_thread_current_check_pending_interrupt ();
1477 numhandles = mono_array_length(mono_handles);
1478 handles = g_new0(HANDLE, numhandles);
1480 for(i = 0; i < numhandles; i++) {
1481 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
1482 handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
1489 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1491 ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, TRUE, ms, TRUE);
1493 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1497 if(ret==WAIT_FAILED) {
1498 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1500 } else if(ret==WAIT_TIMEOUT) {
1501 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1508 /* FIXME: exitContext isnt documented */
1509 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
1511 HANDLE handles [MAXIMUM_WAIT_OBJECTS];
1515 MonoObject *waitHandle;
1516 MonoInternalThread *thread = mono_thread_internal_current ();
1518 /* Do this WaitSleepJoin check before creating objects */
1519 mono_thread_current_check_pending_interrupt ();
1521 numhandles = mono_array_length(mono_handles);
1522 if (numhandles > MAXIMUM_WAIT_OBJECTS)
1525 for(i = 0; i < numhandles; i++) {
1526 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
1527 handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
1534 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1536 ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, FALSE, ms, TRUE);
1538 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1540 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") returning %d", __func__, GetCurrentThreadId (), ret));
1543 * These need to be here. See MSDN dos on WaitForMultipleObjects.
1545 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
1546 return ret - WAIT_OBJECT_0;
1548 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
1549 return ret - WAIT_ABANDONED_0;
1556 /* FIXME: exitContext isnt documented */
1557 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
1560 MonoInternalThread *thread = mono_thread_internal_current ();
1562 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for %p, %d ms", __func__, GetCurrentThreadId (), handle, ms));
1568 mono_thread_current_check_pending_interrupt ();
1570 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1572 ret = mono_wait_uninterrupted (thread, FALSE, 1, &handle, FALSE, ms, TRUE);
1574 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1576 if(ret==WAIT_FAILED) {
1577 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1579 } else if(ret==WAIT_TIMEOUT) {
1580 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1588 ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, HANDLE toWait, gint32 ms, gboolean exitContext)
1591 MonoInternalThread *thread = mono_thread_internal_current ();
1593 MONO_ARCH_SAVE_REGS;
1598 mono_thread_current_check_pending_interrupt ();
1600 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1602 ret = SignalObjectAndWait (toSignal, toWait, ms, TRUE);
1604 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1606 return (!(ret == WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION || ret == WAIT_FAILED));
1609 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
1613 MONO_ARCH_SAVE_REGS;
1618 mutex = CreateMutex (NULL, owned, NULL);
1620 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
1622 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1630 MonoBoolean ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
1631 MONO_ARCH_SAVE_REGS;
1633 return(ReleaseMutex (handle));
1636 HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name,
1642 MONO_ARCH_SAVE_REGS;
1644 *error = ERROR_SUCCESS;
1646 ret = OpenMutex (rights, FALSE, mono_string_chars (name));
1648 *error = GetLastError ();
1655 HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created)
1659 MONO_ARCH_SAVE_REGS;
1664 sem = CreateSemaphore (NULL, initialCount, maximumCount, NULL);
1666 sem = CreateSemaphore (NULL, initialCount, maximumCount,
1667 mono_string_chars (name));
1669 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1677 gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail)
1681 MONO_ARCH_SAVE_REGS;
1683 *fail = !ReleaseSemaphore (handle, releaseCount, &prevcount);
1688 HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
1692 MONO_ARCH_SAVE_REGS;
1694 *error = ERROR_SUCCESS;
1696 ret = OpenSemaphore (rights, FALSE, mono_string_chars (name));
1698 *error = GetLastError ();
1704 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created)
1708 MONO_ARCH_SAVE_REGS;
1713 event = CreateEvent (NULL, manual, initial, NULL);
1715 event = CreateEvent (NULL, manual, initial,
1716 mono_string_chars (name));
1718 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1726 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
1727 MONO_ARCH_SAVE_REGS;
1729 return (SetEvent(handle));
1732 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
1733 MONO_ARCH_SAVE_REGS;
1735 return (ResetEvent(handle));
1739 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
1740 MONO_ARCH_SAVE_REGS;
1742 CloseHandle (handle);
1745 HANDLE ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name,
1751 MONO_ARCH_SAVE_REGS;
1753 *error = ERROR_SUCCESS;
1755 ret = OpenEvent (rights, FALSE, mono_string_chars (name));
1757 *error = GetLastError ();
1763 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
1765 MONO_ARCH_SAVE_REGS;
1767 return InterlockedIncrement (location);
1770 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
1774 MONO_ARCH_SAVE_REGS;
1776 mono_interlocked_lock ();
1780 mono_interlocked_unlock ();
1786 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
1788 MONO_ARCH_SAVE_REGS;
1790 return InterlockedDecrement(location);
1793 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
1797 MONO_ARCH_SAVE_REGS;
1799 mono_interlocked_lock ();
1803 mono_interlocked_unlock ();
1808 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location, gint32 value)
1810 MONO_ARCH_SAVE_REGS;
1812 return InterlockedExchange(location, value);
1815 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location, MonoObject *value)
1818 res = (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
1819 mono_gc_wbarrier_generic_nostore (location);
1823 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr (gpointer *location, gpointer value)
1825 return InterlockedExchangePointer(location, value);
1828 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location, gfloat value)
1830 IntFloatUnion val, ret;
1832 MONO_ARCH_SAVE_REGS;
1835 ret.ival = InterlockedExchange((gint32 *) location, val.ival);
1841 ves_icall_System_Threading_Interlocked_Exchange_Long (gint64 *location, gint64 value)
1843 #if SIZEOF_VOID_P == 8
1844 return (gint64) InterlockedExchangePointer((gpointer *) location, (gpointer)value);
1849 * According to MSDN, this function is only atomic with regards to the
1850 * other Interlocked functions on 32 bit platforms.
1852 mono_interlocked_lock ();
1855 mono_interlocked_unlock ();
1862 ves_icall_System_Threading_Interlocked_Exchange_Double (gdouble *location, gdouble value)
1864 #if SIZEOF_VOID_P == 8
1865 LongDoubleUnion val, ret;
1868 ret.ival = (gint64)InterlockedExchangePointer((gpointer *) location, (gpointer)val.ival);
1875 * According to MSDN, this function is only atomic with regards to the
1876 * other Interlocked functions on 32 bit platforms.
1878 mono_interlocked_lock ();
1881 mono_interlocked_unlock ();
1887 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand)
1889 MONO_ARCH_SAVE_REGS;
1891 return InterlockedCompareExchange(location, value, comparand);
1894 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location, MonoObject *value, MonoObject *comparand)
1897 res = (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
1898 mono_gc_wbarrier_generic_nostore (location);
1902 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand)
1904 return InterlockedCompareExchangePointer(location, value, comparand);
1907 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location, gfloat value, gfloat comparand)
1909 IntFloatUnion val, ret, cmp;
1911 MONO_ARCH_SAVE_REGS;
1914 cmp.fval = comparand;
1915 ret.ival = InterlockedCompareExchange((gint32 *) location, val.ival, cmp.ival);
1921 ves_icall_System_Threading_Interlocked_CompareExchange_Double (gdouble *location, gdouble value, gdouble comparand)
1923 #if SIZEOF_VOID_P == 8
1924 LongDoubleUnion val, comp, ret;
1927 comp.fval = comparand;
1928 ret.ival = (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)val.ival, (gpointer)comp.ival);
1934 mono_interlocked_lock ();
1936 if (old == comparand)
1938 mono_interlocked_unlock ();
1945 ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64 *location, gint64 value, gint64 comparand)
1947 #if SIZEOF_VOID_P == 8
1948 return (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)value, (gpointer)comparand);
1952 mono_interlocked_lock ();
1954 if (old == comparand)
1956 mono_interlocked_unlock ();
1963 ves_icall_System_Threading_Interlocked_CompareExchange_T (MonoObject **location, MonoObject *value, MonoObject *comparand)
1966 res = InterlockedCompareExchangePointer ((gpointer *)location, value, comparand);
1967 mono_gc_wbarrier_generic_nostore (location);
1972 ves_icall_System_Threading_Interlocked_Exchange_T (MonoObject **location, MonoObject *value)
1975 res = InterlockedExchangePointer ((gpointer *)location, value);
1976 mono_gc_wbarrier_generic_nostore (location);
1981 ves_icall_System_Threading_Interlocked_Add_Int (gint32 *location, gint32 value)
1983 #if SIZEOF_VOID_P == 8
1984 /* Should be implemented as a JIT intrinsic */
1985 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1990 mono_interlocked_lock ();
1992 *location = orig + value;
1993 mono_interlocked_unlock ();
1995 return orig + value;
2000 ves_icall_System_Threading_Interlocked_Add_Long (gint64 *location, gint64 value)
2002 #if SIZEOF_VOID_P == 8
2003 /* Should be implemented as a JIT intrinsic */
2004 mono_raise_exception (mono_get_exception_not_implemented (NULL));
2009 mono_interlocked_lock ();
2011 *location = orig + value;
2012 mono_interlocked_unlock ();
2014 return orig + value;
2019 ves_icall_System_Threading_Interlocked_Read_Long (gint64 *location)
2021 #if SIZEOF_VOID_P == 8
2022 /* 64 bit reads are already atomic */
2027 mono_interlocked_lock ();
2029 mono_interlocked_unlock ();
2036 ves_icall_System_Threading_Thread_MemoryBarrier (void)
2038 mono_threads_lock ();
2039 mono_threads_unlock ();
2043 ves_icall_System_Threading_Thread_ClrState (MonoInternalThread* this, guint32 state)
2045 mono_thread_clr_state (this, state);
2047 if (state & ThreadState_Background) {
2048 /* If the thread changes the background mode, the main thread has to
2049 * be notified, since it has to rebuild the list of threads to
2052 SetEvent (background_change_event);
2057 ves_icall_System_Threading_Thread_SetState (MonoInternalThread* this, guint32 state)
2059 mono_thread_set_state (this, state);
2061 if (state & ThreadState_Background) {
2062 /* If the thread changes the background mode, the main thread has to
2063 * be notified, since it has to rebuild the list of threads to
2066 SetEvent (background_change_event);
2071 ves_icall_System_Threading_Thread_GetState (MonoInternalThread* this)
2075 ensure_synch_cs_set (this);
2077 EnterCriticalSection (this->synch_cs);
2079 state = this->state;
2081 LeaveCriticalSection (this->synch_cs);
2086 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoInternalThread *this)
2088 MonoInternalThread *current;
2091 ensure_synch_cs_set (this);
2093 current = mono_thread_internal_current ();
2095 EnterCriticalSection (this->synch_cs);
2097 this->thread_interrupt_requested = TRUE;
2098 throw = current != this && (this->state & ThreadState_WaitSleepJoin);
2100 LeaveCriticalSection (this->synch_cs);
2103 abort_thread_internal (this, TRUE, FALSE);
2107 void mono_thread_current_check_pending_interrupt ()
2109 MonoInternalThread *thread = mono_thread_internal_current ();
2110 gboolean throw = FALSE;
2112 mono_debugger_check_interruption ();
2114 ensure_synch_cs_set (thread);
2116 EnterCriticalSection (thread->synch_cs);
2118 if (thread->thread_interrupt_requested) {
2120 thread->thread_interrupt_requested = FALSE;
2123 LeaveCriticalSection (thread->synch_cs);
2126 mono_raise_exception (mono_get_exception_thread_interrupted ());
2131 mono_thread_get_abort_signal (void)
2135 #elif defined(PLATFORM_ANDROID)
2137 #elif !defined (SIGRTMIN)
2142 #endif /* SIGUSR1 */
2144 static int abort_signum = -1;
2146 if (abort_signum != -1)
2147 return abort_signum;
2148 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
2149 for (i = SIGRTMIN + 1; i < SIGRTMAX; ++i) {
2150 struct sigaction sinfo;
2151 sigaction (i, NULL, &sinfo);
2152 if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
2157 /* fallback to the old way */
2159 #endif /* HOST_WIN32 */
2163 static void CALLBACK interruption_request_apc (ULONG_PTR param)
2165 MonoException* exc = mono_thread_request_interruption (FALSE);
2166 if (exc) mono_raise_exception (exc);
2168 #endif /* HOST_WIN32 */
2171 * signal_thread_state_change
2173 * Tells the thread that his state has changed and it has to enter the new
2174 * state as soon as possible.
2176 static void signal_thread_state_change (MonoInternalThread *thread)
2178 if (thread == mono_thread_internal_current ()) {
2179 /* Do it synchronously */
2180 MonoException *exc = mono_thread_request_interruption (FALSE);
2182 mono_raise_exception (exc);
2186 QueueUserAPC ((PAPCFUNC)interruption_request_apc, thread->handle, NULL);
2188 /* fixme: store the state somewhere */
2189 mono_thread_kill (thread, mono_thread_get_abort_signal ());
2192 * This will cause waits to be broken.
2193 * It will also prevent the thread from entering a wait, so if the thread returns
2194 * from the wait before it receives the abort signal, it will just spin in the wait
2195 * functions in the io-layer until the signal handler calls QueueUserAPC which will
2198 wapi_interrupt_thread (thread->handle);
2199 #endif /* HOST_WIN32 */
2203 ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state)
2205 ensure_synch_cs_set (thread);
2207 EnterCriticalSection (thread->synch_cs);
2209 if ((thread->state & ThreadState_AbortRequested) != 0 ||
2210 (thread->state & ThreadState_StopRequested) != 0 ||
2211 (thread->state & ThreadState_Stopped) != 0)
2213 LeaveCriticalSection (thread->synch_cs);
2217 if ((thread->state & ThreadState_Unstarted) != 0) {
2218 thread->state |= ThreadState_Aborted;
2219 LeaveCriticalSection (thread->synch_cs);
2223 thread->state |= ThreadState_AbortRequested;
2224 if (thread->abort_state_handle)
2225 mono_gchandle_free (thread->abort_state_handle);
2227 thread->abort_state_handle = mono_gchandle_new (state, FALSE);
2228 g_assert (thread->abort_state_handle);
2230 thread->abort_state_handle = 0;
2232 thread->abort_exc = NULL;
2235 * abort_exc is set in mono_thread_execute_interruption(),
2236 * triggered by the call to signal_thread_state_change(),
2237 * below. There's a point between where we have
2238 * abort_state_handle set, but abort_exc NULL, but that's not
2242 LeaveCriticalSection (thread->synch_cs);
2244 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Abort requested for %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
2246 /* During shutdown, we can't wait for other threads */
2248 /* Make sure the thread is awake */
2249 mono_thread_resume (thread);
2251 abort_thread_internal (thread, TRUE, TRUE);
2255 ves_icall_System_Threading_Thread_ResetAbort (void)
2257 MonoInternalThread *thread = mono_thread_internal_current ();
2258 gboolean was_aborting;
2260 ensure_synch_cs_set (thread);
2262 EnterCriticalSection (thread->synch_cs);
2263 was_aborting = thread->state & ThreadState_AbortRequested;
2264 thread->state &= ~ThreadState_AbortRequested;
2265 LeaveCriticalSection (thread->synch_cs);
2267 if (!was_aborting) {
2268 const char *msg = "Unable to reset abort because no abort was requested";
2269 mono_raise_exception (mono_get_exception_thread_state (msg));
2271 thread->abort_exc = NULL;
2272 if (thread->abort_state_handle) {
2273 mono_gchandle_free (thread->abort_state_handle);
2274 /* This is actually not necessary - the handle
2275 only counts if the exception is set */
2276 thread->abort_state_handle = 0;
2281 mono_thread_internal_reset_abort (MonoInternalThread *thread)
2283 ensure_synch_cs_set (thread);
2285 EnterCriticalSection (thread->synch_cs);
2287 thread->state &= ~ThreadState_AbortRequested;
2289 if (thread->abort_exc) {
2290 thread->abort_exc = NULL;
2291 if (thread->abort_state_handle) {
2292 mono_gchandle_free (thread->abort_state_handle);
2293 /* This is actually not necessary - the handle
2294 only counts if the exception is set */
2295 thread->abort_state_handle = 0;
2299 LeaveCriticalSection (thread->synch_cs);
2303 ves_icall_System_Threading_Thread_GetAbortExceptionState (MonoThread *this)
2305 MonoInternalThread *thread = this->internal_thread;
2306 MonoObject *state, *deserialized = NULL, *exc;
2309 if (!thread->abort_state_handle)
2312 state = mono_gchandle_get_target (thread->abort_state_handle);
2315 domain = mono_domain_get ();
2316 if (mono_object_domain (state) == domain)
2319 deserialized = mono_object_xdomain_representation (state, domain, &exc);
2321 if (!deserialized) {
2322 MonoException *invalid_op_exc = mono_get_exception_invalid_operation ("Thread.ExceptionState cannot access an ExceptionState from a different AppDomain");
2324 MONO_OBJECT_SETREF (invalid_op_exc, inner_ex, exc);
2325 mono_raise_exception (invalid_op_exc);
2328 return deserialized;
2332 mono_thread_suspend (MonoInternalThread *thread)
2334 ensure_synch_cs_set (thread);
2336 EnterCriticalSection (thread->synch_cs);
2338 if ((thread->state & ThreadState_Unstarted) != 0 ||
2339 (thread->state & ThreadState_Aborted) != 0 ||
2340 (thread->state & ThreadState_Stopped) != 0)
2342 LeaveCriticalSection (thread->synch_cs);
2346 if ((thread->state & ThreadState_Suspended) != 0 ||
2347 (thread->state & ThreadState_SuspendRequested) != 0 ||
2348 (thread->state & ThreadState_StopRequested) != 0)
2350 LeaveCriticalSection (thread->synch_cs);
2354 thread->state |= ThreadState_SuspendRequested;
2356 LeaveCriticalSection (thread->synch_cs);
2358 suspend_thread_internal (thread, FALSE);
2363 ves_icall_System_Threading_Thread_Suspend (MonoInternalThread *thread)
2365 if (!mono_thread_suspend (thread))
2366 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
2370 mono_thread_resume (MonoInternalThread *thread)
2372 ensure_synch_cs_set (thread);
2374 EnterCriticalSection (thread->synch_cs);
2376 if ((thread->state & ThreadState_SuspendRequested) != 0) {
2377 thread->state &= ~ThreadState_SuspendRequested;
2378 LeaveCriticalSection (thread->synch_cs);
2382 if ((thread->state & ThreadState_Suspended) == 0 ||
2383 (thread->state & ThreadState_Unstarted) != 0 ||
2384 (thread->state & ThreadState_Aborted) != 0 ||
2385 (thread->state & ThreadState_Stopped) != 0)
2387 LeaveCriticalSection (thread->synch_cs);
2391 return resume_thread_internal (thread);
2395 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
2397 if (!thread->internal_thread || !mono_thread_resume (thread->internal_thread))
2398 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
2402 mono_threads_is_critical_method (MonoMethod *method)
2404 switch (method->wrapper_type) {
2405 case MONO_WRAPPER_RUNTIME_INVOKE:
2406 case MONO_WRAPPER_XDOMAIN_INVOKE:
2407 case MONO_WRAPPER_XDOMAIN_DISPATCH:
2414 find_wrapper (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
2419 if (mono_threads_is_critical_method (m)) {
2420 *((gboolean*)data) = TRUE;
2427 is_running_protected_wrapper (void)
2429 gboolean found = FALSE;
2430 mono_stack_walk (find_wrapper, &found);
2434 void mono_thread_internal_stop (MonoInternalThread *thread)
2436 ensure_synch_cs_set (thread);
2438 EnterCriticalSection (thread->synch_cs);
2440 if ((thread->state & ThreadState_StopRequested) != 0 ||
2441 (thread->state & ThreadState_Stopped) != 0)
2443 LeaveCriticalSection (thread->synch_cs);
2447 /* Make sure the thread is awake */
2448 mono_thread_resume (thread);
2450 thread->state |= ThreadState_StopRequested;
2451 thread->state &= ~ThreadState_AbortRequested;
2453 LeaveCriticalSection (thread->synch_cs);
2455 abort_thread_internal (thread, TRUE, TRUE);
2458 void mono_thread_stop (MonoThread *thread)
2460 mono_thread_internal_stop (thread->internal_thread);
2464 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
2466 return *((volatile gint8 *) (ptr));
2470 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
2472 return *((volatile gint16 *) (ptr));
2476 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
2478 return *((volatile gint32 *) (ptr));
2482 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
2484 return *((volatile gint64 *) (ptr));
2488 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
2490 return (void *) *((volatile void **) ptr);
2494 ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr)
2496 return *((volatile double *) (ptr));
2500 ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr)
2502 return *((volatile float *) (ptr));
2506 ves_icall_System_Threading_Volatile_Read_T (void *ptr)
2508 return (MonoObject*)*((volatile MonoObject**)ptr);
2512 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
2514 *((volatile gint8 *) ptr) = value;
2518 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
2520 *((volatile gint16 *) ptr) = value;
2524 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
2526 *((volatile gint32 *) ptr) = value;
2530 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
2532 *((volatile gint64 *) ptr) = value;
2536 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
2538 *((volatile void **) ptr) = value;
2542 ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, void *value)
2544 mono_gc_wbarrier_generic_store (ptr, value);
2548 ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double value)
2550 *((volatile double *) ptr) = value;
2554 ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float value)
2556 *((volatile float *) ptr) = value;
2560 ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value)
2562 *((volatile MonoObject **) ptr) = value;
2563 mono_gc_wbarrier_generic_nostore (ptr);
2566 void mono_thread_init (MonoThreadStartCB start_cb,
2567 MonoThreadAttachCB attach_cb)
2569 InitializeCriticalSection(&threads_mutex);
2570 InitializeCriticalSection(&interlocked_mutex);
2571 InitializeCriticalSection(&contexts_mutex);
2573 background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2574 g_assert(background_change_event != NULL);
2576 mono_init_static_data_info (&thread_static_info);
2577 mono_init_static_data_info (&context_static_info);
2579 MONO_FAST_TLS_INIT (tls_current_object);
2580 mono_native_tls_alloc (¤t_object_key, NULL);
2581 THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__, current_object_key));
2583 mono_thread_start_cb = start_cb;
2584 mono_thread_attach_cb = attach_cb;
2586 /* Get a pseudo handle to the current process. This is just a
2587 * kludge so that wapi can build a process handle if needed.
2588 * As a pseudo handle is returned, we don't need to clean
2591 GetCurrentProcess ();
2594 void mono_thread_cleanup (void)
2596 #if !defined(HOST_WIN32) && !defined(RUN_IN_SUBTHREAD)
2597 /* The main thread must abandon any held mutexes (particularly
2598 * important for named mutexes as they are shared across
2599 * processes, see bug 74680.) This will happen when the
2600 * thread exits, but if it's not running in a subthread it
2601 * won't exit in time.
2603 /* Using non-w32 API is a nasty kludge, but I couldn't find
2604 * anything in the documentation that would let me do this
2605 * here yet still be safe to call on windows.
2607 _wapi_thread_signal_self (mono_environment_exitcode_get ());
2611 /* This stuff needs more testing, it seems one of these
2612 * critical sections can be locked when mono_thread_cleanup is
2615 DeleteCriticalSection (&threads_mutex);
2616 DeleteCriticalSection (&interlocked_mutex);
2617 DeleteCriticalSection (&contexts_mutex);
2618 DeleteCriticalSection (&delayed_free_table_mutex);
2619 DeleteCriticalSection (&small_id_mutex);
2620 CloseHandle (background_change_event);
2623 mono_native_tls_free (current_object_key);
2627 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
2629 mono_thread_cleanup_fn = func;
2633 mono_thread_set_manage_callback (MonoThread *thread, MonoThreadManageCallback func)
2635 thread->internal_thread->manage_callback = func;
2638 void mono_threads_install_notify_pending_exc (MonoThreadNotifyPendingExcFunc func)
2640 mono_thread_notify_pending_exc_fn = func;
2644 static void print_tids (gpointer key, gpointer value, gpointer user)
2646 /* GPOINTER_TO_UINT breaks horribly if sizeof(void *) >
2647 * sizeof(uint) and a cast to uint would overflow
2649 /* Older versions of glib don't have G_GSIZE_FORMAT, so just
2650 * print this as a pointer.
2652 g_message ("Waiting for: %p", key);
2657 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
2658 MonoInternalThread *threads[MAXIMUM_WAIT_OBJECTS];
2662 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
2666 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
2668 ret=WaitForMultipleObjectsEx(wait->num, wait->handles, TRUE, timeout, TRUE);
2670 if(ret==WAIT_FAILED) {
2671 /* See the comment in build_wait_tids() */
2672 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
2676 for(i=0; i<wait->num; i++)
2677 CloseHandle (wait->handles[i]);
2679 if (ret == WAIT_TIMEOUT)
2682 for(i=0; i<wait->num; i++) {
2683 gsize tid = wait->threads[i]->tid;
2685 mono_threads_lock ();
2686 if(mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
2687 /* This thread must have been killed, because
2688 * it hasn't cleaned itself up. (It's just
2689 * possible that the thread exited before the
2690 * parent thread had a chance to store the
2691 * handle, and now there is another pointer to
2692 * the already-exited thread stored. In this
2693 * case, we'll just get two
2694 * mono_profiler_thread_end() calls for the
2698 mono_threads_unlock ();
2699 THREAD_DEBUG (g_message ("%s: cleaning up after thread %p (%"G_GSIZE_FORMAT")", __func__, wait->threads[i], tid));
2700 thread_cleanup (wait->threads[i]);
2702 mono_threads_unlock ();
2707 static void wait_for_tids_or_state_change (struct wait_data *wait, guint32 timeout)
2709 guint32 i, ret, count;
2711 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
2713 /* Add the thread state change event, so it wakes up if a thread changes
2714 * to background mode.
2717 if (count < MAXIMUM_WAIT_OBJECTS) {
2718 wait->handles [count] = background_change_event;
2722 ret=WaitForMultipleObjectsEx (count, wait->handles, FALSE, timeout, TRUE);
2724 if(ret==WAIT_FAILED) {
2725 /* See the comment in build_wait_tids() */
2726 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
2730 for(i=0; i<wait->num; i++)
2731 CloseHandle (wait->handles[i]);
2733 if (ret == WAIT_TIMEOUT)
2736 if (ret < wait->num) {
2737 gsize tid = wait->threads[ret]->tid;
2738 mono_threads_lock ();
2739 if (mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
2740 /* See comment in wait_for_tids about thread cleanup */
2741 mono_threads_unlock ();
2742 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
2743 thread_cleanup (wait->threads [ret]);
2745 mono_threads_unlock ();
2749 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
2751 struct wait_data *wait=(struct wait_data *)user;
2753 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
2755 MonoInternalThread *thread=(MonoInternalThread *)value;
2757 /* Ignore background threads, we abort them later */
2758 /* Do not lock here since it is not needed and the caller holds threads_lock */
2759 if (thread->state & ThreadState_Background) {
2760 THREAD_DEBUG (g_message ("%s: ignoring background thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2761 return; /* just leave, ignore */
2764 if (mono_gc_is_finalizer_internal_thread (thread)) {
2765 THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2769 if (thread == mono_thread_internal_current ()) {
2770 THREAD_DEBUG (g_message ("%s: ignoring current thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2774 if (mono_thread_get_main () && (thread == mono_thread_get_main ()->internal_thread)) {
2775 THREAD_DEBUG (g_message ("%s: ignoring main thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2779 if (thread->flags & MONO_THREAD_FLAG_DONT_MANAGE) {
2780 THREAD_DEBUG (g_message ("%s: ignoring thread %" G_GSIZE_FORMAT "with DONT_MANAGE flag set.", __func__, (gsize)thread->tid));
2784 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2785 if (handle == NULL) {
2786 THREAD_DEBUG (g_message ("%s: ignoring unopenable thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2790 THREAD_DEBUG (g_message ("%s: Invoking mono_thread_manage callback on thread %p", __func__, thread));
2791 if ((thread->manage_callback == NULL) || (thread->manage_callback (thread->root_domain_thread) == TRUE)) {
2792 wait->handles[wait->num]=handle;
2793 wait->threads[wait->num]=thread;
2796 THREAD_DEBUG (g_message ("%s: adding thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2798 THREAD_DEBUG (g_message ("%s: ignoring (because of callback) thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2803 /* Just ignore the rest, we can't do anything with
2810 remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
2812 struct wait_data *wait=(struct wait_data *)user;
2813 gsize self = GetCurrentThreadId ();
2814 MonoInternalThread *thread = value;
2817 if (wait->num >= MAXIMUM_WAIT_OBJECTS)
2820 /* The finalizer thread is not a background thread */
2821 if (thread->tid != self && (thread->state & ThreadState_Background) != 0 &&
2822 !(thread->flags & MONO_THREAD_FLAG_DONT_MANAGE)) {
2824 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2828 /* printf ("A: %d\n", wait->num); */
2829 wait->handles[wait->num]=thread->handle;
2830 wait->threads[wait->num]=thread;
2833 THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT"\n", __func__, (gsize)thread->tid));
2834 mono_thread_internal_stop (thread);
2838 return (thread->tid != self && !mono_gc_is_finalizer_internal_thread (thread));
2842 * mono_threads_set_shutting_down:
2844 * Is called by a thread that wants to shut down Mono. If the runtime is already
2845 * shutting down, the calling thread is suspended/stopped, and this function never
2849 mono_threads_set_shutting_down (void)
2851 MonoInternalThread *current_thread = mono_thread_internal_current ();
2853 mono_threads_lock ();
2855 if (shutting_down) {
2856 mono_threads_unlock ();
2858 /* Make sure we're properly suspended/stopped */
2860 EnterCriticalSection (current_thread->synch_cs);
2862 if ((current_thread->state & ThreadState_SuspendRequested) ||
2863 (current_thread->state & ThreadState_AbortRequested) ||
2864 (current_thread->state & ThreadState_StopRequested)) {
2865 LeaveCriticalSection (current_thread->synch_cs);
2866 mono_thread_execute_interruption (current_thread);
2868 current_thread->state |= ThreadState_Stopped;
2869 LeaveCriticalSection (current_thread->synch_cs);
2872 /*since we're killing the thread, unset the current domain.*/
2873 mono_domain_unset ();
2875 /* Wake up other threads potentially waiting for us */
2878 shutting_down = TRUE;
2880 /* Not really a background state change, but this will
2881 * interrupt the main thread if it is waiting for all
2882 * the other threads.
2884 SetEvent (background_change_event);
2886 mono_threads_unlock ();
2891 * mono_threads_is_shutting_down:
2893 * Returns whether a thread has commenced shutdown of Mono. Note that
2894 * if the function returns FALSE the caller must not assume that
2895 * shutdown is not in progress, because the situation might have
2896 * changed since the function returned. For that reason this function
2897 * is of very limited utility.
2900 mono_threads_is_shutting_down (void)
2902 return shutting_down;
2905 void mono_thread_manage (void)
2907 struct wait_data wait_data;
2908 struct wait_data *wait = &wait_data;
2910 memset (wait, 0, sizeof (struct wait_data));
2911 /* join each thread that's still running */
2912 THREAD_DEBUG (g_message ("%s: Joining each running thread...", __func__));
2914 mono_threads_lock ();
2916 THREAD_DEBUG (g_message("%s: No threads", __func__));
2917 mono_threads_unlock ();
2920 mono_threads_unlock ();
2923 mono_threads_lock ();
2924 if (shutting_down) {
2925 /* somebody else is shutting down */
2926 mono_threads_unlock ();
2929 THREAD_DEBUG (g_message ("%s: There are %d threads to join", __func__, mono_g_hash_table_size (threads));
2930 mono_g_hash_table_foreach (threads, print_tids, NULL));
2932 ResetEvent (background_change_event);
2934 /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
2935 memset (wait->threads, 0, MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
2936 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
2937 mono_threads_unlock ();
2939 /* Something to wait for */
2940 wait_for_tids_or_state_change (wait, INFINITE);
2942 THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
2943 } while(wait->num>0);
2945 mono_runtime_shutdown ();
2947 THREAD_DEBUG (g_message ("%s: threadpool cleanup", __func__));
2948 mono_thread_pool_cleanup ();
2951 * Remove everything but the finalizer thread and self.
2952 * Also abort all the background threads
2955 mono_threads_lock ();
2958 /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
2959 memset (wait->threads, 0, MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
2960 mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
2962 mono_threads_unlock ();
2964 THREAD_DEBUG (g_message ("%s: wait->num is now %d", __func__, wait->num));
2966 /* Something to wait for */
2967 wait_for_tids (wait, INFINITE);
2969 } while (wait->num > 0);
2972 * give the subthreads a chance to really quit (this is mainly needed
2973 * to get correct user and system times from getrusage/wait/time(1)).
2974 * This could be removed if we avoid pthread_detach() and use pthread_join().
2981 static void terminate_thread (gpointer key, gpointer value, gpointer user)
2983 MonoInternalThread *thread=(MonoInternalThread *)value;
2985 if(thread->tid != (gsize)user) {
2986 /*TerminateThread (thread->handle, -1);*/
2990 void mono_thread_abort_all_other_threads (void)
2992 gsize self = GetCurrentThreadId ();
2994 mono_threads_lock ();
2995 THREAD_DEBUG (g_message ("%s: There are %d threads to abort", __func__,
2996 mono_g_hash_table_size (threads));
2997 mono_g_hash_table_foreach (threads, print_tids, NULL));
2999 mono_g_hash_table_foreach (threads, terminate_thread, (gpointer)self);
3001 mono_threads_unlock ();
3005 collect_threads_for_suspend (gpointer key, gpointer value, gpointer user_data)
3007 MonoInternalThread *thread = (MonoInternalThread*)value;
3008 struct wait_data *wait = (struct wait_data*)user_data;
3012 * We try to exclude threads early, to avoid running into the MAXIMUM_WAIT_OBJECTS
3014 * This needs no locking.
3016 if ((thread->state & ThreadState_Suspended) != 0 ||
3017 (thread->state & ThreadState_Stopped) != 0)
3020 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
3021 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
3025 wait->handles [wait->num] = handle;
3026 wait->threads [wait->num] = thread;
3032 * mono_thread_suspend_all_other_threads:
3034 * Suspend all managed threads except the finalizer thread and this thread. It is
3035 * not possible to resume them later.
3037 void mono_thread_suspend_all_other_threads (void)
3039 struct wait_data wait_data;
3040 struct wait_data *wait = &wait_data;
3042 gsize self = GetCurrentThreadId ();
3044 guint32 eventidx = 0;
3045 gboolean starting, finished;
3047 memset (wait, 0, sizeof (struct wait_data));
3049 * The other threads could be in an arbitrary state at this point, i.e.
3050 * they could be starting up, shutting down etc. This means that there could be
3051 * threads which are not even in the threads hash table yet.
3055 * First we set a barrier which will be checked by all threads before they
3056 * are added to the threads hash table, and they will exit if the flag is set.
3057 * This ensures that no threads could be added to the hash later.
3058 * We will use shutting_down as the barrier for now.
3060 g_assert (shutting_down);
3063 * We make multiple calls to WaitForMultipleObjects since:
3064 * - we can only wait for MAXIMUM_WAIT_OBJECTS threads
3065 * - some threads could exit without becoming suspended
3070 * Make a copy of the hashtable since we can't do anything with
3071 * threads while threads_mutex is held.
3074 /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
3075 memset (wait->threads, 0, MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
3076 mono_threads_lock ();
3077 mono_g_hash_table_foreach (threads, collect_threads_for_suspend, wait);
3078 mono_threads_unlock ();
3080 events = g_new0 (gpointer, wait->num);
3082 /* Get the suspended events that we'll be waiting for */
3083 for (i = 0; i < wait->num; ++i) {
3084 MonoInternalThread *thread = wait->threads [i];
3085 gboolean signal_suspend = FALSE;
3087 if ((thread->tid == self) || mono_gc_is_finalizer_internal_thread (thread) || (thread->flags & MONO_THREAD_FLAG_DONT_MANAGE)) {
3088 //CloseHandle (wait->handles [i]);
3089 wait->threads [i] = NULL; /* ignore this thread in next loop */
3093 ensure_synch_cs_set (thread);
3095 EnterCriticalSection (thread->synch_cs);
3097 if (thread->suspended_event == NULL) {
3098 thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
3099 if (thread->suspended_event == NULL) {
3100 /* Forget this one and go on to the next */
3101 LeaveCriticalSection (thread->synch_cs);
3106 if ((thread->state & ThreadState_Suspended) != 0 ||
3107 (thread->state & ThreadState_StopRequested) != 0 ||
3108 (thread->state & ThreadState_Stopped) != 0) {
3109 LeaveCriticalSection (thread->synch_cs);
3110 CloseHandle (wait->handles [i]);
3111 wait->threads [i] = NULL; /* ignore this thread in next loop */
3115 if ((thread->state & ThreadState_SuspendRequested) == 0)
3116 signal_suspend = TRUE;
3118 events [eventidx++] = thread->suspended_event;
3120 /* Convert abort requests into suspend requests */
3121 if ((thread->state & ThreadState_AbortRequested) != 0)
3122 thread->state &= ~ThreadState_AbortRequested;
3124 thread->state |= ThreadState_SuspendRequested;
3126 LeaveCriticalSection (thread->synch_cs);
3128 /* Signal the thread to suspend */
3129 if (mono_thread_info_new_interrupt_enabled ())
3130 suspend_thread_internal (thread, TRUE);
3131 else if (signal_suspend)
3132 signal_thread_state_change (thread);
3135 /*Only wait on the suspend event if we are using the old path */
3136 if (eventidx > 0 && !mono_thread_info_new_interrupt_enabled ()) {
3137 WaitForMultipleObjectsEx (eventidx, events, TRUE, 100, FALSE);
3138 for (i = 0; i < wait->num; ++i) {
3139 MonoInternalThread *thread = wait->threads [i];
3144 ensure_synch_cs_set (thread);
3146 EnterCriticalSection (thread->synch_cs);
3147 if ((thread->state & ThreadState_Suspended) != 0) {
3148 CloseHandle (thread->suspended_event);
3149 thread->suspended_event = NULL;
3151 LeaveCriticalSection (thread->synch_cs);
3155 if (eventidx <= 0) {
3157 * If there are threads which are starting up, we wait until they
3158 * are suspended when they try to register in the threads hash.
3159 * This is guaranteed to finish, since the threads which can create new
3160 * threads get suspended after a while.
3161 * FIXME: The finalizer thread can still create new threads.
3163 mono_threads_lock ();
3164 if (threads_starting_up)
3165 starting = mono_g_hash_table_size (threads_starting_up) > 0;
3168 mono_threads_unlock ();
3180 collect_threads (gpointer key, gpointer value, gpointer user_data)
3182 MonoInternalThread *thread = (MonoInternalThread*)value;
3183 struct wait_data *wait = (struct wait_data*)user_data;
3186 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
3187 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
3191 wait->handles [wait->num] = handle;
3192 wait->threads [wait->num] = thread;
3197 static gboolean thread_dump_requested;
3199 static G_GNUC_UNUSED gboolean
3200 print_stack_frame_to_string (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer data)
3202 GString *p = (GString*)data;
3203 MonoMethod *method = NULL;
3205 method = frame->ji->method;
3208 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
3209 g_string_append_printf (p, " %s\n", location);
3212 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
3218 print_thread_dump (MonoInternalThread *thread, MonoThreadInfo *info)
3220 GString* text = g_string_new (0);
3222 GError *error = NULL;
3225 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
3227 g_string_append_printf (text, "\n\"%s\"", name);
3230 else if (thread->threadpool_thread)
3231 g_string_append (text, "\n\"<threadpool thread>\"");
3233 g_string_append (text, "\n\"<unnamed thread>\"");
3236 /* This no longer works with remote unwinding */
3238 wapi_desc = wapi_current_thread_desc ();
3239 g_string_append_printf (text, " tid=0x%p this=0x%p %s\n", (gpointer)(gsize)thread->tid, thread, wapi_desc);
3244 mono_get_eh_callbacks ()->mono_walk_stack_with_state (print_stack_frame_to_string, &info->suspend_state, MONO_UNWIND_SIGNAL_SAFE, text);
3245 mono_thread_info_resume (mono_thread_info_get_tid (info));
3247 fprintf (stdout, "%s", text->str);
3249 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
3250 OutputDebugStringA(text->str);
3253 g_string_free (text, TRUE);
3258 dump_thread (gpointer key, gpointer value, gpointer user)
3260 MonoInternalThread *thread = (MonoInternalThread *)value;
3261 MonoThreadInfo *info;
3263 if (thread == mono_thread_internal_current ())
3267 FIXME This still can hang if we stop a thread during malloc.
3268 FIXME This can hang if we suspend on a critical method and the GC kicks in. A fix might be to have function
3269 that takes a callback and runs it with the target suspended.
3270 We probably should loop a bit around trying to get it to either managed code
3273 info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gpointer)(gsize)thread->tid, FALSE);
3278 print_thread_dump (thread, info);
3282 mono_threads_perform_thread_dump (void)
3284 if (!thread_dump_requested)
3287 printf ("Full thread dump:\n");
3290 * Make a copy of the hashtable since we can't do anything with
3291 * threads while threads_mutex is held.
3293 mono_threads_lock ();
3294 mono_g_hash_table_foreach (threads, dump_thread, NULL);
3295 mono_threads_unlock ();
3297 thread_dump_requested = FALSE;
3301 * mono_threads_request_thread_dump:
3303 * Ask all threads except the current to print their stacktrace to stdout.
3306 mono_threads_request_thread_dump (void)
3308 struct wait_data wait_data;
3309 struct wait_data *wait = &wait_data;
3312 /*The new thread dump code runs out of the finalizer thread. */
3313 if (mono_thread_info_new_interrupt_enabled ()) {
3314 thread_dump_requested = TRUE;
3315 mono_gc_finalize_notify ();
3320 memset (wait, 0, sizeof (struct wait_data));
3323 * Make a copy of the hashtable since we can't do anything with
3324 * threads while threads_mutex is held.
3326 mono_threads_lock ();
3327 mono_g_hash_table_foreach (threads, collect_threads, wait);
3328 mono_threads_unlock ();
3330 for (i = 0; i < wait->num; ++i) {
3331 MonoInternalThread *thread = wait->threads [i];
3333 if (!mono_gc_is_finalizer_internal_thread (thread) &&
3334 (thread != mono_thread_internal_current ()) &&
3335 !thread->thread_dump_requested) {
3336 thread->thread_dump_requested = TRUE;
3338 signal_thread_state_change (thread);
3341 CloseHandle (wait->handles [i]);
3347 gint allocated; /* +1 so that refs [allocated] == NULL */
3351 typedef struct ref_stack RefStack;
3354 ref_stack_new (gint initial_size)
3358 initial_size = MAX (initial_size, 16) + 1;
3359 rs = g_new0 (RefStack, 1);
3360 rs->refs = g_new0 (gpointer, initial_size);
3361 rs->allocated = initial_size;
3366 ref_stack_destroy (gpointer ptr)
3377 ref_stack_push (RefStack *rs, gpointer ptr)
3379 g_assert (rs != NULL);
3381 if (rs->bottom >= rs->allocated) {
3382 rs->refs = g_realloc (rs->refs, rs->allocated * 2 * sizeof (gpointer) + 1);
3383 rs->allocated <<= 1;
3384 rs->refs [rs->allocated] = NULL;
3386 rs->refs [rs->bottom++] = ptr;
3390 ref_stack_pop (RefStack *rs)
3392 if (rs == NULL || rs->bottom == 0)
3396 rs->refs [rs->bottom] = NULL;
3400 ref_stack_find (RefStack *rs, gpointer ptr)
3407 for (refs = rs->refs; refs && *refs; refs++) {
3415 * mono_thread_push_appdomain_ref:
3417 * Register that the current thread may have references to objects in domain
3418 * @domain on its stack. Each call to this function should be paired with a
3419 * call to pop_appdomain_ref.
3422 mono_thread_push_appdomain_ref (MonoDomain *domain)
3424 MonoInternalThread *thread = mono_thread_internal_current ();
3427 /* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
3428 SPIN_LOCK (thread->lock_thread_id);
3429 if (thread->appdomain_refs == NULL)
3430 thread->appdomain_refs = ref_stack_new (16);
3431 ref_stack_push (thread->appdomain_refs, domain);
3432 SPIN_UNLOCK (thread->lock_thread_id);
3437 mono_thread_pop_appdomain_ref (void)
3439 MonoInternalThread *thread = mono_thread_internal_current ();
3442 /* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
3443 SPIN_LOCK (thread->lock_thread_id);
3444 ref_stack_pop (thread->appdomain_refs);
3445 SPIN_UNLOCK (thread->lock_thread_id);
3450 mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain)
3453 SPIN_LOCK (thread->lock_thread_id);
3454 res = ref_stack_find (thread->appdomain_refs, domain);
3455 SPIN_UNLOCK (thread->lock_thread_id);
3460 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
3462 return mono_thread_internal_has_appdomain_ref (thread->internal_thread, domain);
3465 typedef struct abort_appdomain_data {
3466 struct wait_data wait;
3468 } abort_appdomain_data;
3471 collect_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
3473 MonoInternalThread *thread = (MonoInternalThread*)value;
3474 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
3475 MonoDomain *domain = data->domain;
3477 if (mono_thread_internal_has_appdomain_ref (thread, domain)) {
3478 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
3480 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
3481 HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
3484 data->wait.handles [data->wait.num] = handle;
3485 data->wait.threads [data->wait.num] = thread;
3488 /* Just ignore the rest, we can't do anything with
3496 * mono_threads_abort_appdomain_threads:
3498 * Abort threads which has references to the given appdomain.
3501 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
3503 #ifdef __native_client__
3507 abort_appdomain_data user_data;
3509 int orig_timeout = timeout;
3512 THREAD_DEBUG (g_message ("%s: starting abort", __func__));
3514 start_time = mono_msec_ticks ();
3516 mono_threads_lock ();
3518 user_data.domain = domain;
3519 user_data.wait.num = 0;
3520 /* This shouldn't take any locks */
3521 mono_g_hash_table_foreach (threads, collect_appdomain_thread, &user_data);
3522 mono_threads_unlock ();
3524 if (user_data.wait.num > 0) {
3525 /* Abort the threads outside the threads lock */
3526 for (i = 0; i < user_data.wait.num; ++i)
3527 ves_icall_System_Threading_Thread_Abort (user_data.wait.threads [i], NULL);
3530 * We should wait for the threads either to abort, or to leave the
3531 * domain. We can't do the latter, so we wait with a timeout.
3533 wait_for_tids (&user_data.wait, 100);
3536 /* Update remaining time */
3537 timeout -= mono_msec_ticks () - start_time;
3538 start_time = mono_msec_ticks ();
3540 if (orig_timeout != -1 && timeout < 0)
3543 while (user_data.wait.num > 0);
3545 THREAD_DEBUG (g_message ("%s: abort done", __func__));
3551 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
3553 MonoInternalThread *thread = (MonoInternalThread*)value;
3554 MonoDomain *domain = (MonoDomain*)user_data;
3557 /* No locking needed here */
3558 /* FIXME: why no locking? writes to the cache are protected with synch_cs above */
3560 if (thread->cached_culture_info) {
3561 for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i) {
3562 MonoObject *obj = mono_array_get (thread->cached_culture_info, MonoObject*, i);
3563 if (obj && obj->vtable->domain == domain)
3564 mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
3570 * mono_threads_clear_cached_culture:
3572 * Clear the cached_current_culture from all threads if it is in the
3576 mono_threads_clear_cached_culture (MonoDomain *domain)
3578 mono_threads_lock ();
3579 mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
3580 mono_threads_unlock ();
3584 * mono_thread_get_undeniable_exception:
3586 * Return an exception which needs to be raised when leaving a catch clause.
3587 * This is used for undeniable exception propagation.
3590 mono_thread_get_undeniable_exception (void)
3592 MonoInternalThread *thread = mono_thread_internal_current ();
3594 if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
3596 * FIXME: Clear the abort exception and return an AppDomainUnloaded
3597 * exception if the thread no longer references a dying appdomain.
3599 thread->abort_exc->trace_ips = NULL;
3600 thread->abort_exc->stack_trace = NULL;
3601 return thread->abort_exc;
3607 #if MONO_SMALL_CONFIG
3608 #define NUM_STATIC_DATA_IDX 4
3609 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
3613 #define NUM_STATIC_DATA_IDX 8
3614 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
3615 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
3619 static uintptr_t* static_reference_bitmaps [NUM_STATIC_DATA_IDX];
3622 mark_tls_slots (void *addr, MonoGCMarkFunc mark_func)
3625 gpointer *static_data = addr;
3626 for (i = 0; i < NUM_STATIC_DATA_IDX; ++i) {
3629 if (!static_data [i])
3631 numwords = 1 + static_data_size [i] / sizeof (gpointer) / (sizeof(uintptr_t) * 8);
3632 ptr = static_data [i];
3633 for (j = 0; j < numwords; ++j, ptr += sizeof (uintptr_t) * 8) {
3634 uintptr_t bmap = static_reference_bitmaps [i][j];
3637 if ((bmap & 1) && *p) {
3648 * mono_alloc_static_data
3650 * Allocate memory blocks for storing threads or context static data
3653 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset, gboolean threadlocal)
3655 guint idx = (offset >> 24) - 1;
3658 gpointer* static_data = *static_data_ptr;
3660 static void* tls_desc = NULL;
3661 if (mono_gc_user_markers_supported () && !tls_desc)
3662 tls_desc = mono_gc_make_root_descr_user (mark_tls_slots);
3663 static_data = mono_gc_alloc_fixed (static_data_size [0], threadlocal?tls_desc:NULL);
3664 *static_data_ptr = static_data;
3665 static_data [0] = static_data;
3668 for (i = 1; i <= idx; ++i) {
3669 if (static_data [i])
3671 if (mono_gc_user_markers_supported () && threadlocal)
3672 static_data [i] = g_malloc0 (static_data_size [i]);
3674 static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
3679 mono_free_static_data (gpointer* static_data, gboolean threadlocal)
3682 for (i = 1; i < NUM_STATIC_DATA_IDX; ++i) {
3683 if (!static_data [i])
3685 if (mono_gc_user_markers_supported () && threadlocal)
3686 g_free (static_data [i]);
3688 mono_gc_free_fixed (static_data [i]);
3690 mono_gc_free_fixed (static_data);
3694 * mono_init_static_data_info
3696 * Initializes static data counters
3698 static void mono_init_static_data_info (StaticDataInfo *static_data)
3700 static_data->idx = 0;
3701 static_data->offset = 0;
3702 static_data->freelist = NULL;
3706 * mono_alloc_static_data_slot
3708 * Generates an offset for static data. static_data contains the counters
3709 * used to generate it.
3712 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
3716 if (!static_data->idx && !static_data->offset) {
3718 * we use the first chunk of the first allocation also as
3719 * an array for the rest of the data
3721 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
3723 static_data->offset += align - 1;
3724 static_data->offset &= ~(align - 1);
3725 if (static_data->offset + size >= static_data_size [static_data->idx]) {
3726 static_data->idx ++;
3727 g_assert (size <= static_data_size [static_data->idx]);
3728 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
3729 static_data->offset = 0;
3731 offset = static_data->offset | ((static_data->idx + 1) << 24);
3732 static_data->offset += size;
3737 * ensure thread static fields already allocated are valid for thread
3738 * This function is called when a thread is created or on thread attach.
3741 thread_adjust_static_data (MonoInternalThread *thread)
3745 mono_threads_lock ();
3746 if (thread_static_info.offset || thread_static_info.idx > 0) {
3747 /* get the current allocated size */
3748 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
3749 mono_alloc_static_data (&(thread->static_data), offset, TRUE);
3751 mono_threads_unlock ();
3755 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
3757 MonoInternalThread *thread = value;
3758 guint32 offset = GPOINTER_TO_UINT (user);
3760 mono_alloc_static_data (&(thread->static_data), offset, TRUE);
3763 static MonoThreadDomainTls*
3764 search_tls_slot_in_freelist (StaticDataInfo *static_data, guint32 size, guint32 align)
3766 MonoThreadDomainTls* prev = NULL;
3767 MonoThreadDomainTls* tmp = static_data->freelist;
3769 if (tmp->size == size) {
3771 prev->next = tmp->next;
3773 static_data->freelist = tmp->next;
3782 update_tls_reference_bitmap (guint32 offset, uintptr_t *bitmap, int numbits)
3785 int idx = (offset >> 24) - 1;
3787 if (!static_reference_bitmaps [idx])
3788 static_reference_bitmaps [idx] = g_new0 (uintptr_t, 1 + static_data_size [idx] / sizeof(gpointer) / (sizeof(uintptr_t) * 8));
3789 rb = static_reference_bitmaps [idx];
3791 offset /= sizeof (gpointer);
3792 /* offset is now the bitmap offset */
3793 for (i = 0; i < numbits; ++i) {
3794 if (bitmap [i / sizeof (uintptr_t)] & (1L << (i & (sizeof (uintptr_t) * 8 -1))))
3795 rb [(offset + i) / (sizeof (uintptr_t) * 8)] |= (1L << ((offset + i) & (sizeof (uintptr_t) * 8 -1)));
3800 clear_reference_bitmap (guint32 offset, guint32 size)
3802 int idx = (offset >> 24) - 1;
3804 rb = static_reference_bitmaps [idx];
3806 offset /= sizeof (gpointer);
3807 size /= sizeof (gpointer);
3809 /* offset is now the bitmap offset */
3810 for (; offset < size; ++offset)
3811 rb [offset / (sizeof (uintptr_t) * 8)] &= ~(1L << (offset & (sizeof (uintptr_t) * 8 -1)));
3815 * The offset for a special static variable is composed of three parts:
3816 * a bit that indicates the type of static data (0:thread, 1:context),
3817 * an index in the array of chunks of memory for the thread (thread->static_data)
3818 * and an offset in that chunk of mem. This allows allocating less memory in the
3823 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align, uintptr_t *bitmap, int numbits)
3826 if (static_type == SPECIAL_STATIC_THREAD) {
3827 MonoThreadDomainTls *item;
3828 mono_threads_lock ();
3829 item = search_tls_slot_in_freelist (&thread_static_info, size, align);
3830 /*g_print ("TLS alloc: %d in domain %p (total: %d), cached: %p\n", size, mono_domain_get (), thread_static_info.offset, item);*/
3832 offset = item->offset;
3835 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
3837 update_tls_reference_bitmap (offset, bitmap, numbits);
3838 /* This can be called during startup */
3839 if (threads != NULL)
3840 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
3841 mono_threads_unlock ();
3843 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
3844 mono_contexts_lock ();
3845 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
3846 mono_contexts_unlock ();
3847 offset |= 0x80000000; /* Set the high bit to indicate context static data */
3853 mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset)
3855 /* The high bit means either thread (0) or static (1) data. */
3857 guint32 static_type = (offset & 0x80000000);
3860 offset &= 0x7fffffff;
3861 idx = (offset >> 24) - 1;
3863 if (static_type == 0) {
3864 return get_thread_static_data (thread, offset);
3866 /* Allocate static data block under demand, since we don't have a list
3869 MonoAppContext *context = mono_context_get ();
3870 if (!context->static_data || !context->static_data [idx]) {
3871 mono_contexts_lock ();
3872 mono_alloc_static_data (&(context->static_data), offset, FALSE);
3873 mono_contexts_unlock ();
3875 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
3880 mono_get_special_static_data (guint32 offset)
3882 return mono_get_special_static_data_for_thread (mono_thread_internal_current (), offset);
3891 free_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
3893 MonoInternalThread *thread = value;
3894 TlsOffsetSize *data = user;
3895 int idx = (data->offset >> 24) - 1;
3898 if (!thread->static_data || !thread->static_data [idx])
3900 ptr = ((char*) thread->static_data [idx]) + (data->offset & 0xffffff);
3901 mono_gc_bzero (ptr, data->size);
3905 do_free_special_slot (guint32 offset, guint32 size)
3907 guint32 static_type = (offset & 0x80000000);
3908 /*g_print ("free %s , size: %d, offset: %x\n", field->name, size, offset);*/
3909 if (static_type == 0) {
3911 MonoThreadDomainTls *item = g_new0 (MonoThreadDomainTls, 1);
3912 data.offset = offset & 0x7fffffff;
3914 clear_reference_bitmap (data.offset, data.size);
3915 if (threads != NULL)
3916 mono_g_hash_table_foreach (threads, free_thread_static_data_helper, &data);
3917 item->offset = offset;
3920 if (!mono_runtime_is_shutting_down ()) {
3921 item->next = thread_static_info.freelist;
3922 thread_static_info.freelist = item;
3924 /* We could be called during shutdown after mono_thread_cleanup () is called */
3928 /* FIXME: free context static data as well */
3933 do_free_special (gpointer key, gpointer value, gpointer data)
3935 MonoClassField *field = key;
3936 guint32 offset = GPOINTER_TO_UINT (value);
3939 size = mono_type_size (field->type, &align);
3940 do_free_special_slot (offset, size);
3944 mono_alloc_special_static_data_free (GHashTable *special_static_fields)
3946 mono_threads_lock ();
3947 g_hash_table_foreach (special_static_fields, do_free_special, NULL);
3948 mono_threads_unlock ();
3952 mono_special_static_data_free_slot (guint32 offset, guint32 size)
3954 mono_threads_lock ();
3955 do_free_special_slot (offset, size);
3956 mono_threads_unlock ();
3960 * allocates room in the thread local area for storing an instance of the struct type
3961 * the allocation is kept track of in domain->tlsrec_list.
3964 mono_thread_alloc_tls (MonoReflectionType *type)
3966 MonoDomain *domain = mono_domain_get ();
3968 MonoTlsDataRecord *tlsrec;
3971 gsize default_bitmap [4] = {0};
3972 uint32_t tls_offset;
3976 klass = mono_class_from_mono_type (type->type);
3977 /* TlsDatum is a struct, so we subtract the object header size offset */
3978 bitmap = mono_class_compute_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
3979 size = mono_type_size (type->type, &align);
3980 tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, (uintptr_t*)bitmap, max_set + 1);
3981 if (bitmap != default_bitmap)
3983 tlsrec = g_new0 (MonoTlsDataRecord, 1);
3984 tlsrec->tls_offset = tls_offset;
3985 tlsrec->size = size;
3986 mono_domain_lock (domain);
3987 tlsrec->next = domain->tlsrec_list;
3988 domain->tlsrec_list = tlsrec;
3989 mono_domain_unlock (domain);
3994 mono_thread_destroy_tls (uint32_t tls_offset)
3996 MonoTlsDataRecord *prev = NULL;
3997 MonoTlsDataRecord *cur;
3999 MonoDomain *domain = mono_domain_get ();
4000 mono_domain_lock (domain);
4001 cur = domain->tlsrec_list;
4003 if (cur->tls_offset == tls_offset) {
4005 prev->next = cur->next;
4007 domain->tlsrec_list = cur->next;
4015 mono_domain_unlock (domain);
4017 mono_special_static_data_free_slot (tls_offset, size);
4021 * This is just to ensure cleanup: the finalizers should have taken care, so this is not perf-critical.
4024 mono_thread_destroy_domain_tls (MonoDomain *domain)
4026 while (domain->tlsrec_list)
4027 mono_thread_destroy_tls (domain->tlsrec_list->tls_offset);
4030 static MonoClassField *local_slots = NULL;
4033 /* local tls data to get locals_slot from a thread */
4036 /* index in the locals_slot array */
4041 clear_local_slot (gpointer key, gpointer value, gpointer user_data)
4043 LocalSlotID *sid = user_data;
4044 MonoInternalThread *thread = (MonoInternalThread*)value;
4045 MonoArray *slots_array;
4047 * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
4048 * it is for the right domain, so we need to check if it is allocated an initialized
4049 * for the current thread.
4051 /*g_print ("handling thread %p\n", thread);*/
4052 if (!thread->static_data || !thread->static_data [sid->idx])
4054 slots_array = *(MonoArray **)(((char*) thread->static_data [sid->idx]) + (sid->offset & 0xffffff));
4055 if (!slots_array || sid->slot >= mono_array_length (slots_array))
4057 mono_array_set (slots_array, MonoObject*, sid->slot, NULL);
4061 mono_thread_free_local_slot_values (int slot, MonoBoolean thread_local)
4069 local_slots = mono_class_get_field_from_name (mono_defaults.thread_class, "local_slots");
4071 g_warning ("local_slots field not found in Thread class");
4075 domain = mono_domain_get ();
4076 mono_domain_lock (domain);
4077 if (domain->special_static_fields)
4078 addr = g_hash_table_lookup (domain->special_static_fields, local_slots);
4079 mono_domain_unlock (domain);
4082 /*g_print ("freeing slot %d at %p\n", slot, addr);*/
4083 sid.offset = GPOINTER_TO_UINT (addr);
4084 sid.offset &= 0x7fffffff;
4085 sid.idx = (sid.offset >> 24) - 1;
4086 mono_threads_lock ();
4087 mono_g_hash_table_foreach (threads, clear_local_slot, &sid);
4088 mono_threads_unlock ();
4090 /* FIXME: clear the slot for MonoAppContexts, too */
4095 static void CALLBACK dummy_apc (ULONG_PTR param)
4099 static guint32 dummy_apc (gpointer param)
4106 * mono_thread_execute_interruption
4108 * Performs the operation that the requested thread state requires (abort,
4111 static MonoException* mono_thread_execute_interruption (MonoInternalThread *thread)
4113 ensure_synch_cs_set (thread);
4115 EnterCriticalSection (thread->synch_cs);
4117 /* MonoThread::interruption_requested can only be changed with atomics */
4118 if (InterlockedCompareExchange (&thread->interruption_requested, FALSE, TRUE)) {
4119 /* this will consume pending APC calls */
4120 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
4121 InterlockedDecrement (&thread_interruption_requested);
4123 /* Clear the interrupted flag of the thread so it can wait again */
4124 wapi_clear_interruption ();
4128 if ((thread->state & ThreadState_AbortRequested) != 0) {
4129 LeaveCriticalSection (thread->synch_cs);
4130 if (thread->abort_exc == NULL) {
4132 * This might be racy, but it has to be called outside the lock
4133 * since it calls managed code.
4135 MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
4137 return thread->abort_exc;
4139 else if ((thread->state & ThreadState_SuspendRequested) != 0) {
4140 self_suspend_internal (thread);
4143 else if ((thread->state & ThreadState_StopRequested) != 0) {
4144 /* FIXME: do this through the JIT? */
4146 LeaveCriticalSection (thread->synch_cs);
4148 mono_thread_exit ();
4150 } else if (thread->pending_exception) {
4153 exc = thread->pending_exception;
4154 thread->pending_exception = NULL;
4156 LeaveCriticalSection (thread->synch_cs);
4158 } else if (thread->thread_interrupt_requested) {
4160 thread->thread_interrupt_requested = FALSE;
4161 LeaveCriticalSection (thread->synch_cs);
4163 return(mono_get_exception_thread_interrupted ());
4166 LeaveCriticalSection (thread->synch_cs);
4172 * mono_thread_request_interruption
4174 * A signal handler can call this method to request the interruption of a
4175 * thread. The result of the interruption will depend on the current state of
4176 * the thread. If the result is an exception that needs to be throw, it is
4177 * provided as return value.
4180 mono_thread_request_interruption (gboolean running_managed)
4182 MonoInternalThread *thread = mono_thread_internal_current ();
4184 /* The thread may already be stopping */
4189 if (thread->interrupt_on_stop &&
4190 thread->state & ThreadState_StopRequested &&
4191 thread->state & ThreadState_Background)
4195 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
4197 InterlockedIncrement (&thread_interruption_requested);
4199 if (!running_managed || is_running_protected_wrapper ()) {
4200 /* Can't stop while in unmanaged code. Increase the global interruption
4201 request count. When exiting the unmanaged method the count will be
4202 checked and the thread will be interrupted. */
4205 if (mono_thread_notify_pending_exc_fn && !running_managed)
4206 /* The JIT will notify the thread about the interruption */
4207 /* This shouldn't take any locks */
4208 mono_thread_notify_pending_exc_fn ();
4210 /* this will awake the thread if it is in WaitForSingleObject
4212 /* Our implementation of this function ignores the func argument */
4213 QueueUserAPC ((PAPCFUNC)dummy_apc, thread->handle, NULL);
4217 return mono_thread_execute_interruption (thread);
4221 /*This function should be called by a thread after it has exited all of
4222 * its handle blocks at interruption time.*/
4224 mono_thread_resume_interruption (void)
4226 MonoInternalThread *thread = mono_thread_internal_current ();
4227 gboolean still_aborting;
4229 /* The thread may already be stopping */
4233 ensure_synch_cs_set (thread);
4234 EnterCriticalSection (thread->synch_cs);
4235 still_aborting = (thread->state & ThreadState_AbortRequested) != 0;
4236 LeaveCriticalSection (thread->synch_cs);
4238 /*This can happen if the protected block called Thread::ResetAbort*/
4239 if (!still_aborting)
4242 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
4244 InterlockedIncrement (&thread_interruption_requested);
4247 wapi_self_interrupt ();
4249 return mono_thread_execute_interruption (thread);
4252 gboolean mono_thread_interruption_requested ()
4254 if (thread_interruption_requested) {
4255 MonoInternalThread *thread = mono_thread_internal_current ();
4256 /* The thread may already be stopping */
4258 return (thread->interruption_requested);
4263 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
4265 MonoInternalThread *thread = mono_thread_internal_current ();
4267 /* The thread may already be stopping */
4271 mono_debugger_check_interruption ();
4273 if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
4274 MonoException* exc = mono_thread_execute_interruption (thread);
4275 if (exc) mono_raise_exception (exc);
4280 * Performs the interruption of the current thread, if one has been requested,
4281 * and the thread is not running a protected wrapper.
4283 void mono_thread_interruption_checkpoint ()
4285 mono_thread_interruption_checkpoint_request (FALSE);
4289 * Performs the interruption of the current thread, if one has been requested.
4291 void mono_thread_force_interruption_checkpoint ()
4293 mono_thread_interruption_checkpoint_request (TRUE);
4297 * mono_thread_get_and_clear_pending_exception:
4299 * Return any pending exceptions for the current thread and clear it as a side effect.
4302 mono_thread_get_and_clear_pending_exception (void)
4304 MonoInternalThread *thread = mono_thread_internal_current ();
4306 /* The thread may already be stopping */
4310 if (thread->interruption_requested && !is_running_protected_wrapper ()) {
4311 return mono_thread_execute_interruption (thread);
4314 if (thread->pending_exception) {
4315 MonoException *exc = thread->pending_exception;
4317 thread->pending_exception = NULL;
4325 * mono_set_pending_exception:
4327 * Set the pending exception of the current thread to EXC.
4328 * The exception will be thrown when execution returns to managed code.
4331 mono_set_pending_exception (MonoException *exc)
4333 MonoInternalThread *thread = mono_thread_internal_current ();
4335 /* The thread may already be stopping */
4339 MONO_OBJECT_SETREF (thread, pending_exception, exc);
4341 mono_thread_request_interruption (FALSE);
4345 * mono_thread_interruption_request_flag:
4347 * Returns the address of a flag that will be non-zero if an interruption has
4348 * been requested for a thread. The thread to interrupt may not be the current
4349 * thread, so an additional call to mono_thread_interruption_requested() or
4350 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
4353 gint32* mono_thread_interruption_request_flag ()
4355 return &thread_interruption_requested;
4359 mono_thread_init_apartment_state (void)
4362 MonoInternalThread* thread = mono_thread_internal_current ();
4364 /* Positive return value indicates success, either
4365 * S_OK if this is first CoInitialize call, or
4366 * S_FALSE if CoInitialize already called, but with same
4367 * threading model. A negative value indicates failure,
4368 * probably due to trying to change the threading model.
4370 if (CoInitializeEx(NULL, (thread->apartment_state == ThreadApartmentState_STA)
4371 ? COINIT_APARTMENTTHREADED
4372 : COINIT_MULTITHREADED) < 0) {
4373 thread->apartment_state = ThreadApartmentState_Unknown;
4379 mono_thread_cleanup_apartment_state (void)
4382 MonoInternalThread* thread = mono_thread_internal_current ();
4384 if (thread && thread->apartment_state != ThreadApartmentState_Unknown) {
4391 mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state)
4393 ensure_synch_cs_set (thread);
4395 EnterCriticalSection (thread->synch_cs);
4396 thread->state |= state;
4397 LeaveCriticalSection (thread->synch_cs);
4401 mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state)
4403 ensure_synch_cs_set (thread);
4405 EnterCriticalSection (thread->synch_cs);
4406 thread->state &= ~state;
4407 LeaveCriticalSection (thread->synch_cs);
4411 mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test)
4413 gboolean ret = FALSE;
4415 ensure_synch_cs_set (thread);
4417 EnterCriticalSection (thread->synch_cs);
4419 if ((thread->state & test) != 0) {
4423 LeaveCriticalSection (thread->synch_cs);
4428 //static MonoClassField *execution_context_field;
4431 get_execution_context_addr (void)
4433 MonoDomain *domain = mono_domain_get ();
4434 guint32 offset = domain->execution_context_field_offset;
4437 MonoClassField *field = mono_class_get_field_from_name (mono_defaults.thread_class, "_ec");
4440 g_assert (mono_class_try_get_vtable (domain, mono_defaults.appdomain_class));
4442 mono_domain_lock (domain);
4443 offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, field));
4444 mono_domain_unlock (domain);
4447 domain->execution_context_field_offset = offset;
4450 return (MonoObject**) mono_get_special_static_data (offset);
4454 mono_thread_get_execution_context (void)
4456 return *get_execution_context_addr ();
4460 mono_thread_set_execution_context (MonoObject *ec)
4462 *get_execution_context_addr () = ec;
4465 static gboolean has_tls_get = FALSE;
4468 mono_runtime_set_has_tls_get (gboolean val)
4474 mono_runtime_has_tls_get (void)
4480 mono_thread_kill (MonoInternalThread *thread, int signal)
4482 #ifdef __native_client__
4483 /* Workaround pthread_kill abort() in NaCl glibc. */
4487 /* Win32 uses QueueUserAPC and callers of this are guarded */
4488 g_assert_not_reached ();
4490 # ifdef PTHREAD_POINTER_ID
4491 return pthread_kill ((gpointer)(gsize)(thread->tid), mono_thread_get_abort_signal ());
4493 # ifdef PLATFORM_ANDROID
4494 if (thread->android_tid != 0) {
4496 int old_errno = errno;
4498 ret = tkill ((pid_t) thread->android_tid, signal);
4507 return pthread_kill (thread->tid, mono_thread_get_abort_signal ());
4509 return pthread_kill (thread->tid, mono_thread_get_abort_signal ());
4516 self_interrupt_thread (void *_unused)
4518 MonoThreadInfo *info = mono_thread_info_current ();
4519 MonoException *exc = mono_thread_execute_interruption (mono_thread_internal_current ());
4520 if (exc) /*We must use _with_context since we didn't trampoline into the runtime*/
4521 mono_raise_exception_with_context (exc, &info->suspend_state.ctx);
4522 g_assert_not_reached (); /*this MUST not happen since we can't resume from an async call*/
4526 mono_jit_info_match (MonoJitInfo *ji, gpointer ip)
4530 return ji->code_start <= ip && (char*)ip < (char*)ji->code_start + ji->code_size;
4534 last_managed (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer data)
4536 MonoJitInfo **dest = data;
4542 mono_thread_info_get_last_managed (MonoThreadInfo *info)
4544 MonoJitInfo *ji = NULL;
4547 mono_get_eh_callbacks ()->mono_walk_stack_with_state (last_managed, &info->suspend_state, MONO_UNWIND_SIGNAL_SAFE, &ji);
4552 abort_thread_internal (MonoInternalThread *thread, gboolean can_raise_exception, gboolean install_async_abort)
4555 MonoThreadInfo *info = NULL;
4556 gboolean protected_wrapper;
4557 gboolean running_managed;
4559 if (!mono_thread_info_new_interrupt_enabled ()) {
4560 signal_thread_state_change (thread);
4565 FIXME this is insanely broken, it doesn't cause interruption to happen
4566 synchronously since passing FALSE to mono_thread_request_interruption makes sure it returns NULL
4568 if (thread == mono_thread_internal_current ()) {
4569 /* Do it synchronously */
4570 MonoException *exc = mono_thread_request_interruption (can_raise_exception);
4572 mono_raise_exception (exc);
4574 wapi_interrupt_thread (thread->handle);
4579 /*FIXME we need to check 2 conditions here, request to interrupt this thread or if the target died*/
4580 if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, TRUE))) {
4584 if (mono_get_eh_callbacks ()->mono_install_handler_block_guard (&info->suspend_state)) {
4585 mono_thread_info_resume (mono_thread_info_get_tid (info));
4589 /*someone is already interrupting it*/
4590 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1) {
4591 mono_thread_info_resume (mono_thread_info_get_tid (info));
4594 InterlockedIncrement (&thread_interruption_requested);
4596 ji = mono_thread_info_get_last_managed (info);
4597 protected_wrapper = ji && mono_threads_is_critical_method (ji->method);
4598 running_managed = mono_jit_info_match (ji, MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
4600 if (!protected_wrapper && running_managed) {
4601 /*We are in managed code*/
4602 /*Set the thread to call */
4603 if (install_async_abort)
4604 mono_thread_info_setup_async_call (info, self_interrupt_thread, NULL);
4605 mono_thread_info_resume (mono_thread_info_get_tid (info));
4607 gpointer interrupt_handle;
4609 * This will cause waits to be broken.
4610 * It will also prevent the thread from entering a wait, so if the thread returns
4611 * from the wait before it receives the abort signal, it will just spin in the wait
4612 * functions in the io-layer until the signal handler calls QueueUserAPC which will
4616 interrupt_handle = wapi_prepare_interrupt_thread (thread->handle);
4618 mono_thread_info_resume (mono_thread_info_get_tid (info));
4620 wapi_finish_interrupt_thread (interrupt_handle);
4623 /*FIXME we need to wait for interruption to complete -- figure out how much into interruption we should wait for here*/
4627 transition_to_suspended (MonoInternalThread *thread)
4629 if ((thread->state & ThreadState_SuspendRequested) == 0) {
4630 g_assert (0); /*FIXME we should not reach this */
4631 /*Make sure we balance the suspend count.*/
4632 mono_thread_info_resume ((MonoNativeThreadId)(gpointer)(gsize)thread->tid);
4634 thread->state &= ~ThreadState_SuspendRequested;
4635 thread->state |= ThreadState_Suspended;
4637 LeaveCriticalSection (thread->synch_cs);
4641 suspend_thread_internal (MonoInternalThread *thread, gboolean interrupt)
4643 if (!mono_thread_info_new_interrupt_enabled ()) {
4644 signal_thread_state_change (thread);
4648 EnterCriticalSection (thread->synch_cs);
4649 if (thread == mono_thread_internal_current ()) {
4650 transition_to_suspended (thread);
4651 mono_thread_info_self_suspend ();
4653 MonoThreadInfo *info;
4655 gboolean protected_wrapper;
4656 gboolean running_managed;
4658 /*A null info usually means the thread is already dead. */
4659 if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, interrupt))) {
4660 LeaveCriticalSection (thread->synch_cs);
4664 ji = mono_thread_info_get_last_managed (info);
4665 protected_wrapper = ji && mono_threads_is_critical_method (ji->method);
4666 running_managed = mono_jit_info_match (ji, MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
4668 if (running_managed && !protected_wrapper) {
4669 transition_to_suspended (thread);
4671 gpointer interrupt_handle;
4673 if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 0)
4674 InterlockedIncrement (&thread_interruption_requested);
4677 interrupt_handle = wapi_prepare_interrupt_thread (thread->handle);
4679 mono_thread_info_resume (mono_thread_info_get_tid (info));
4682 wapi_finish_interrupt_thread (interrupt_handle);
4684 LeaveCriticalSection (thread->synch_cs);
4689 /*This is called with @thread synch_cs held and it must release it*/
4691 self_suspend_internal (MonoInternalThread *thread)
4693 if (!mono_thread_info_new_interrupt_enabled ()) {
4694 thread->state &= ~ThreadState_SuspendRequested;
4695 thread->state |= ThreadState_Suspended;
4696 thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
4697 if (thread->suspend_event == NULL) {
4698 LeaveCriticalSection (thread->synch_cs);
4701 if (thread->suspended_event)
4702 SetEvent (thread->suspended_event);
4704 LeaveCriticalSection (thread->synch_cs);
4706 if (shutting_down) {
4707 /* After we left the lock, the runtime might shut down so everything becomes invalid */
4712 WaitForSingleObject (thread->suspend_event, INFINITE);
4714 EnterCriticalSection (thread->synch_cs);
4716 CloseHandle (thread->suspend_event);
4717 thread->suspend_event = NULL;
4718 thread->state &= ~ThreadState_Suspended;
4720 /* The thread that requested the resume will have replaced this event
4721 * and will be waiting for it
4723 SetEvent (thread->resume_event);
4725 LeaveCriticalSection (thread->synch_cs);
4729 transition_to_suspended (thread);
4730 mono_thread_info_self_suspend ();
4733 /*This is called with @thread synch_cs held and it must release it*/
4735 resume_thread_internal (MonoInternalThread *thread)
4737 if (!mono_thread_info_new_interrupt_enabled ()) {
4738 thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
4739 if (thread->resume_event == NULL) {
4740 LeaveCriticalSection (thread->synch_cs);
4744 /* Awake the thread */
4745 SetEvent (thread->suspend_event);
4747 LeaveCriticalSection (thread->synch_cs);
4749 /* Wait for the thread to awake */
4750 WaitForSingleObject (thread->resume_event, INFINITE);
4751 CloseHandle (thread->resume_event);
4752 thread->resume_event = NULL;
4756 LeaveCriticalSection (thread->synch_cs);
4757 /* Awake the thread */
4758 if (!mono_thread_info_resume ((MonoNativeThreadId)(gpointer)(gsize)thread->tid))
4760 EnterCriticalSection (thread->synch_cs);
4761 thread->state &= ~ThreadState_Suspended;
4762 LeaveCriticalSection (thread->synch_cs);