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 * (C) 2001 Ximian, Inc.
14 #define _WIN32_WINNT 0x0500
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/threadpool.h>
26 #include <mono/metadata/threads-types.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/environment.h>
29 #include <mono/metadata/monitor.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/metadata/marshal.h>
32 #include <mono/io-layer/io-layer.h>
33 #include <mono/metadata/object-internals.h>
34 #include <mono/metadata/mono-debug-debugger.h>
35 #include <mono/utils/mono-compiler.h>
37 #include <mono/os/gc_wrapper.h>
39 /*#define THREAD_DEBUG(a) do { a; } while (0)*/
40 #define THREAD_DEBUG(a)
41 /*#define THREAD_WAIT_DEBUG(a) do { a; } while (0)*/
42 #define THREAD_WAIT_DEBUG(a)
43 /*#define LIBGC_DEBUG(a) do { a; } while (0)*/
44 #define LIBGC_DEBUG(a)
48 guint32 (*func)(void *);
70 /* Number of cached culture objects in the MonoThread->cached_culture_info array
71 * (per-type): we use the first NUM entries for CultureInfo and the last for
72 * UICultureInfo. So the size of the array is really NUM_CACHED_CULTURES * 2.
74 #define NUM_CACHED_CULTURES 4
75 #define CULTURES_START_IDX 0
76 #define UICULTURES_START_IDX NUM_CACHED_CULTURES
79 * The "os_handle" field of the WaitHandle class.
81 static MonoClassField *wait_handle_os_handle_field = NULL;
83 /* Controls access to the 'threads' hash table */
84 #define mono_threads_lock() EnterCriticalSection (&threads_mutex)
85 #define mono_threads_unlock() LeaveCriticalSection (&threads_mutex)
86 static CRITICAL_SECTION threads_mutex;
88 /* Controls access to context static data */
89 #define mono_contexts_lock() EnterCriticalSection (&contexts_mutex)
90 #define mono_contexts_unlock() LeaveCriticalSection (&contexts_mutex)
91 static CRITICAL_SECTION contexts_mutex;
93 /* Holds current status of static data heap */
94 static StaticDataInfo thread_static_info;
95 static StaticDataInfo context_static_info;
97 /* The hash of existing threads (key is thread ID) that need joining
100 static MonoGHashTable *threads=NULL;
102 /* The TLS key that holds the MonoObject assigned to each thread */
103 static guint32 current_object_key = -1;
105 #ifdef HAVE_KW_THREAD
106 /* we need to use both the Tls* functions and __thread because
107 * the gc needs to see all the threads
109 static __thread MonoThread * tls_current_object MONO_TLS_FAST;
110 #define SET_CURRENT_OBJECT(x) do { \
111 tls_current_object = x; \
112 TlsSetValue (current_object_key, x); \
114 #define GET_CURRENT_OBJECT() tls_current_object
116 #define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x);
117 #define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key);
120 /* function called at thread start */
121 static MonoThreadStartCB mono_thread_start_cb = NULL;
123 /* function called at thread attach */
124 static MonoThreadAttachCB mono_thread_attach_cb = NULL;
126 /* function called at thread cleanup */
127 static MonoThreadCleanupFunc mono_thread_cleanup = NULL;
129 /* The default stack size for each thread */
130 static guint32 default_stacksize = 0;
131 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
133 static void thread_adjust_static_data (MonoThread *thread);
134 static void mono_init_static_data_info (StaticDataInfo *static_data);
135 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
136 static gboolean mono_thread_resume (MonoThread* thread);
137 static void mono_thread_start (MonoThread *thread);
139 /* Spin lock for InterlockedXXX 64 bit functions */
140 #define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
141 #define mono_interlocked_unlock() LeaveCriticalSection (&interlocked_mutex)
142 static CRITICAL_SECTION interlocked_mutex;
144 /* global count of thread interruptions requested */
145 static gint32 thread_interruption_requested = 0;
147 /* Event signaled when a thread changes its background mode */
148 static HANDLE background_change_event;
151 mono_thread_get_tls_key (void)
153 return current_object_key;
157 mono_thread_get_tls_offset (void)
160 MONO_THREAD_VAR_OFFSET (tls_current_object,offset);
164 /* handle_store() and handle_remove() manage the array of threads that
165 * still need to be waited for when the main thread exits.
167 static void handle_store(MonoThread *thread)
169 mono_threads_lock ();
171 THREAD_DEBUG (g_message ("%s: thread %p ID %"G_GSIZE_FORMAT, __func__, thread, (gsize)thread->tid));
174 MONO_GC_REGISTER_ROOT (threads);
175 threads=mono_g_hash_table_new(NULL, NULL);
178 /* We don't need to duplicate thread->handle, because it is
179 * only closed when the thread object is finalized by the GC.
181 mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->tid),
184 mono_threads_unlock ();
187 static void handle_remove(gsize tid)
189 THREAD_DEBUG (g_message ("%s: thread ID %"G_GSIZE_FORMAT, __func__, tid));
191 mono_threads_lock ();
195 mono_g_hash_table_remove (threads, (gpointer)tid);
197 mono_threads_unlock ();
199 /* Don't close the handle here, wait for the object finalizer
200 * to do it. Otherwise, the following race condition applies:
202 * 1) Thread exits (and handle_remove() closes the handle)
204 * 2) Some other handle is reassigned the same slot
206 * 3) Another thread tries to join the first thread, and
207 * blocks waiting for the reassigned handle to be signalled
208 * (which might never happen). This is possible, because the
209 * thread calling Join() still has a reference to the first
215 * Tell the Mono Debugger about a newly created thread.
216 * mono_debugger_event() is a no-op if we're not running inside the debugger.
218 static void debugger_thread_created (MonoThread *thread)
220 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
221 (guint64) (gsize) &thread->end_stack, thread->tid);
225 * Tell the Mono Debugger that a thrad is about to exit.
226 * mono_debugger_event() is a no-op if we're not running inside the debugger.
228 static void debugger_thread_exited (MonoThread *thread)
230 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_EXITED,
231 (guint64) (gsize) &thread->end_stack, thread->tid);
234 static void thread_cleanup (MonoThread *thread)
236 g_assert (thread != NULL);
238 mono_release_type_locks (thread);
240 if (!mono_monitor_enter (thread->synch_lock))
243 thread->state |= ThreadState_Stopped;
244 mono_monitor_exit (thread->synch_lock);
246 mono_profiler_thread_end (thread->tid);
247 handle_remove (thread->tid);
249 mono_thread_pop_appdomain_ref ();
251 if (thread->serialized_culture_info)
252 g_free (thread->serialized_culture_info);
254 thread->cached_culture_info = NULL;
256 debugger_thread_exited (thread);
258 if (mono_thread_cleanup)
259 mono_thread_cleanup (thread);
262 static guint32 WINAPI start_wrapper(void *data)
264 struct StartInfo *start_info=(struct StartInfo *)data;
265 guint32 (*start_func)(void *);
268 MonoThread *thread=start_info->obj;
269 MonoObject *start_delegate = start_info->delegate;
271 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper", __func__, GetCurrentThreadId ()));
273 /* We can be sure start_info->obj->tid and
274 * start_info->obj->handle have been set, because the thread
275 * was created suspended, and these values were set before the
281 SET_CURRENT_OBJECT (thread);
283 if (!mono_domain_set (start_info->domain, FALSE)) {
284 /* No point in raising an appdomain_unloaded exception here */
285 /* FIXME: Cleanup here */
289 start_func = start_info->func;
290 start_arg = start_info->start_arg;
292 /* This MUST be called before any managed code can be
293 * executed, as it calls the callback function that (for the
294 * jit) sets the lmf marker.
296 mono_thread_new_init (tid, &tid, start_func);
297 thread->stack_ptr = &tid;
299 debugger_thread_created (thread);
301 LIBGC_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT",%d) Setting thread stack to %p", __func__, GetCurrentThreadId (), getpid (), thread->stack_ptr));
303 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
305 mono_profiler_thread_start (tid);
307 if(thread->start_notify!=NULL) {
308 /* Let the thread that called Start() know we're
311 ReleaseSemaphore (thread->start_notify, 1, NULL);
316 /* Every thread references the appdomain which created it */
317 mono_thread_push_appdomain_ref (mono_domain_get ());
319 thread_adjust_static_data (thread);
321 g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
325 /* start_func is set only for unamanged start functions */
327 start_func (start_arg);
330 g_assert (start_delegate != NULL);
331 args [0] = start_arg;
332 /* we may want to handle the exception here. See comment below on unhandled exceptions */
333 mono_runtime_delegate_invoke (start_delegate, args, NULL);
336 /* If the thread calls ExitThread at all, this remaining code
337 * will not be executed, but the main thread will eventually
338 * call thread_cleanup() on this thread's behalf.
341 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper terminating", __func__, GetCurrentThreadId ()));
343 /* Remove the reference to the thread object in the TLS data,
344 * so the thread object can be finalized. This won't be
345 * reached if the thread threw an uncaught exception, so those
346 * thread handles will stay referenced :-( (This is due to
347 * missing support for scanning thread-specific data in the
348 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
351 SET_CURRENT_OBJECT (NULL);
353 thread_cleanup (thread);
358 void mono_thread_new_init (gsize tid, gpointer stack_start, gpointer func)
360 if (mono_thread_start_cb) {
361 mono_thread_start_cb (tid, stack_start, func);
365 void mono_threads_set_default_stacksize (guint32 stacksize)
367 default_stacksize = stacksize;
370 guint32 mono_threads_get_default_stacksize (void)
372 return default_stacksize;
375 void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
378 HANDLE thread_handle;
379 struct StartInfo *start_info;
382 thread=(MonoThread *)mono_object_new (domain,
383 mono_defaults.thread_class);
385 start_info=g_new0 (struct StartInfo, 1);
386 start_info->func = func;
387 start_info->obj = thread;
388 start_info->domain = domain;
389 start_info->start_arg = arg;
391 /* Create suspended, so we can do some housekeeping before the thread
394 thread_handle = CreateThread(NULL, default_stacksize_for_thread (thread), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
395 CREATE_SUSPENDED, &tid);
396 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
397 if (thread_handle == NULL) {
398 /* The thread couldn't be created, so throw an exception */
399 mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
403 thread->handle=thread_handle;
406 MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
408 handle_store(thread);
410 ResumeThread (thread_handle);
414 mono_thread_attach (MonoDomain *domain)
417 HANDLE thread_handle;
420 if ((thread = mono_thread_current ())) {
421 if (domain != mono_domain_get ())
422 mono_domain_set (domain, TRUE);
423 /* Already attached */
427 if (!mono_gc_register_thread (&domain)) {
428 g_error ("Thread %p 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.", GetCurrentThread ());
431 thread = (MonoThread *)mono_object_new (domain,
432 mono_defaults.thread_class);
434 thread_handle = GetCurrentThread ();
435 g_assert (thread_handle);
437 tid=GetCurrentThreadId ();
439 #ifdef PLATFORM_WIN32
441 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
442 * refer to the thread from other threads for things like aborting.
444 DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
445 THREAD_ALL_ACCESS, TRUE, 0);
448 thread->handle=thread_handle;
450 MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
452 THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
454 handle_store(thread);
456 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
458 SET_CURRENT_OBJECT (thread);
459 mono_domain_set (domain, TRUE);
461 thread_adjust_static_data (thread);
463 if (mono_thread_attach_cb) {
464 mono_thread_attach_cb (tid, &tid);
471 mono_thread_detach (MonoThread *thread)
473 g_return_if_fail (thread != NULL);
475 THREAD_DEBUG (g_message ("%s: mono_thread_detach for %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
476 SET_CURRENT_OBJECT (NULL);
478 thread_cleanup (thread);
480 /* Need to CloseHandle this thread, because we took a reference in
481 * mono_thread_attach ()
483 CloseHandle(thread->handle);
489 MonoThread *thread = mono_thread_current ();
491 SET_CURRENT_OBJECT (NULL);
492 thread_cleanup (thread);
494 /* we could add a callback here for embedders to use. */
495 if (thread == mono_thread_get_main ())
496 exit (mono_environment_exitcode_get ());
500 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
503 guint32 (*start_func)(void *);
504 struct StartInfo *start_info;
510 THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__, this, start));
512 mono_monitor_enter (this->synch_lock);
514 if ((this->state & ThreadState_Unstarted) == 0) {
515 mono_monitor_exit (this->synch_lock);
516 mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
520 if ((this->state & ThreadState_Aborted) != 0) {
521 mono_monitor_exit (this->synch_lock);
526 /* This is freed in start_wrapper */
527 start_info = g_new0 (struct StartInfo, 1);
528 start_info->func = start_func;
529 start_info->start_arg = this->start_obj; /* FIXME: GC object stored in unmanaged memory */
530 start_info->delegate = start;
531 start_info->obj = this;
532 start_info->domain = mono_domain_get ();
534 this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
535 if(this->start_notify==NULL) {
536 mono_monitor_exit (this->synch_lock);
537 g_warning ("%s: CreateSemaphore error 0x%x", __func__, GetLastError ());
541 thread=CreateThread(NULL, default_stacksize_for_thread (this), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
542 CREATE_SUSPENDED, &tid);
544 mono_monitor_exit (this->synch_lock);
545 g_warning("%s: CreateThread error 0x%x", __func__, GetLastError());
552 /* Don't call handle_store() here, delay it to Start.
553 * We can't join a thread (trying to will just block
554 * forever) until it actually starts running, so don't
555 * store the handle till then.
558 mono_thread_start (this);
560 this->state &= ~ThreadState_Unstarted;
562 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread));
564 mono_monitor_exit (this->synch_lock);
569 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
574 THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
576 CloseHandle (thread);
579 static void mono_thread_start (MonoThread *thread)
583 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
585 /* Only store the handle when the thread is about to be
586 * launched, to avoid the main thread deadlocking while trying
587 * to clean up a thread that will never be signalled.
589 handle_store (thread);
591 ResumeThread (thread->handle);
593 if(thread->start_notify!=NULL) {
594 /* Wait for the thread to set up its TLS data etc, so
595 * theres no potential race condition if someone tries
596 * to look up the data believing the thread has
600 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for thread %p (%"G_GSIZE_FORMAT") to start", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
602 WaitForSingleObjectEx (thread->start_notify, INFINITE, FALSE);
603 CloseHandle (thread->start_notify);
604 thread->start_notify = NULL;
607 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Done launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
610 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
612 MonoThread *thread = mono_thread_current ();
616 THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
618 mono_monitor_enter (thread->synch_lock);
619 thread->state |= ThreadState_WaitSleepJoin;
620 mono_monitor_exit (thread->synch_lock);
624 mono_monitor_enter (thread->synch_lock);
625 thread->state &= ~ThreadState_WaitSleepJoin;
626 mono_monitor_exit (thread->synch_lock);
630 ves_icall_System_Threading_Thread_GetDomainID (void)
634 return mono_domain_get()->domain_id;
638 ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
641 mono_monitor_enter (this_obj->synch_lock);
646 str = mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
648 mono_monitor_exit (this_obj->synch_lock);
653 ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
655 mono_monitor_enter (this_obj->synch_lock);
657 if (this_obj->name) {
658 mono_monitor_exit (this_obj->synch_lock);
659 mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
663 this_obj->name = g_new (gunichar2, mono_string_length (name));
664 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
665 this_obj->name_len = mono_string_length (name);
668 this_obj->name = NULL;
670 mono_monitor_exit (this_obj->synch_lock);
674 lookup_cached_culture (MonoThread *this, MonoDomain *domain, int start_idx)
679 if (this->cached_culture_info) {
680 domain = mono_domain_get ();
681 for (i = start_idx; i < start_idx + NUM_CACHED_CULTURES; ++i) {
682 res = mono_array_get (this->cached_culture_info, MonoObject*, i);
683 if (res && res->vtable->domain == domain)
692 ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoThread *this)
694 return lookup_cached_culture (this, mono_domain_get (), CULTURES_START_IDX);
698 ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread *this)
702 mono_monitor_enter (this->synch_lock);
703 if (this->serialized_culture_info) {
704 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_culture_info_len);
705 memcpy (mono_array_addr (res, guint8, 0), this->serialized_culture_info, this->serialized_culture_info_len);
709 mono_monitor_exit (this->synch_lock);
715 cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
718 MonoDomain *domain = mono_domain_get ();
721 int same_domain_slot = -1;
723 mono_monitor_enter (this->synch_lock);
724 if (!this->cached_culture_info)
725 this->cached_culture_info = mono_array_new (mono_object_domain (this), mono_defaults.object_class, NUM_CACHED_CULTURES * 2);
727 for (i = start_idx; i < start_idx + NUM_CACHED_CULTURES; ++i) {
728 obj = mono_array_get (this->cached_culture_info, MonoObject*, i);
732 /* we continue, because there may be a slot used with the same domain */
736 if (obj->vtable->domain == domain) {
737 same_domain_slot = i;
741 if (same_domain_slot >= 0)
742 mono_array_setref (this->cached_culture_info, same_domain_slot, culture);
743 else if (free_slot >= 0)
744 mono_array_setref (this->cached_culture_info, free_slot, culture);
745 /* we may want to replace an existing entry here, even when no suitable slot is found */
746 mono_monitor_exit (this->synch_lock);
750 ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this, MonoObject *culture)
752 cache_culture (this, culture, CULTURES_START_IDX);
756 ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread *this, MonoArray *arr)
758 mono_monitor_enter (this->synch_lock);
759 if (this->serialized_culture_info)
760 g_free (this->serialized_culture_info);
761 this->serialized_culture_info = g_new0 (guint8, mono_array_length (arr));
762 this->serialized_culture_info_len = mono_array_length (arr);
763 memcpy (this->serialized_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
764 mono_monitor_exit (this->synch_lock);
769 ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoThread *this)
771 return lookup_cached_culture (this, mono_domain_get (), UICULTURES_START_IDX);
775 ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread *this)
779 mono_monitor_enter (this->synch_lock);
780 if (this->serialized_ui_culture_info) {
781 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_ui_culture_info_len);
782 memcpy (mono_array_addr (res, guint8, 0), this->serialized_ui_culture_info, this->serialized_ui_culture_info_len);
786 mono_monitor_exit (this->synch_lock);
792 ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this, MonoObject *culture)
794 cache_culture (this, culture, UICULTURES_START_IDX);
798 ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread *this, MonoArray *arr)
800 mono_monitor_enter (this->synch_lock);
801 if (this->serialized_ui_culture_info)
802 g_free (this->serialized_ui_culture_info);
803 this->serialized_ui_culture_info = g_new0 (guint8, mono_array_length (arr));
804 this->serialized_ui_culture_info_len = mono_array_length (arr);
805 memcpy (this->serialized_ui_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
806 mono_monitor_exit (this->synch_lock);
809 /* the jit may read the compiled code of this function */
811 mono_thread_current (void)
813 THREAD_DEBUG (g_message ("%s: returning %p", __func__, GET_CURRENT_OBJECT ()));
814 return GET_CURRENT_OBJECT ();
817 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
818 int ms, HANDLE thread)
824 mono_monitor_enter (this->synch_lock);
826 if ((this->state & ThreadState_Unstarted) != 0) {
827 mono_monitor_exit (this->synch_lock);
828 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
832 this->state |= ThreadState_WaitSleepJoin;
833 mono_monitor_exit (this->synch_lock);
838 THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__, thread, ms));
840 ret=WaitForSingleObjectEx (thread, ms, TRUE);
842 mono_monitor_enter (this->synch_lock);
843 this->state &= ~ThreadState_WaitSleepJoin;
844 mono_monitor_exit (this->synch_lock);
846 if(ret==WAIT_OBJECT_0) {
847 THREAD_DEBUG (g_message ("%s: join successful", __func__));
852 THREAD_DEBUG (g_message ("%s: join failed", __func__));
857 /* FIXME: exitContext isnt documented */
858 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
864 MonoObject *waitHandle;
866 MonoThread *thread = mono_thread_current ();
870 numhandles = mono_array_length(mono_handles);
871 handles = g_new0(HANDLE, numhandles);
873 if (wait_handle_os_handle_field == 0) {
874 /* Get the field os_handle which will contain the actual handle */
875 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
876 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
879 for(i = 0; i < numhandles; i++) {
880 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
881 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
888 mono_monitor_enter (thread->synch_lock);
889 thread->state |= ThreadState_WaitSleepJoin;
890 mono_monitor_exit (thread->synch_lock);
892 ret=WaitForMultipleObjectsEx(numhandles, handles, TRUE, ms, TRUE);
894 mono_monitor_enter (thread->synch_lock);
895 thread->state &= ~ThreadState_WaitSleepJoin;
896 mono_monitor_exit (thread->synch_lock);
900 if(ret==WAIT_FAILED) {
901 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
903 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
904 /* Do we want to try again if we get
905 * WAIT_IO_COMPLETION? The documentation for
906 * WaitHandle doesn't give any clues. (We'd have to
907 * fiddle with the timeout if we retry.)
909 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
916 /* FIXME: exitContext isnt documented */
917 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
923 MonoObject *waitHandle;
925 MonoThread *thread = mono_thread_current ();
929 numhandles = mono_array_length(mono_handles);
930 handles = g_new0(HANDLE, numhandles);
932 if (wait_handle_os_handle_field == 0) {
933 /* Get the field os_handle which will contain the actual handle */
934 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
935 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
938 for(i = 0; i < numhandles; i++) {
939 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
940 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
947 mono_monitor_enter (thread->synch_lock);
948 thread->state |= ThreadState_WaitSleepJoin;
949 mono_monitor_exit (thread->synch_lock);
951 ret=WaitForMultipleObjectsEx(numhandles, handles, FALSE, ms, TRUE);
953 mono_monitor_enter (thread->synch_lock);
954 thread->state &= ~ThreadState_WaitSleepJoin;
955 mono_monitor_exit (thread->synch_lock);
959 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") returning %d", __func__, GetCurrentThreadId (), ret));
962 * These need to be here. See MSDN dos on WaitForMultipleObjects.
964 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
965 return ret - WAIT_OBJECT_0;
967 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
968 return ret - WAIT_ABANDONED_0;
975 /* FIXME: exitContext isnt documented */
976 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
979 MonoThread *thread = mono_thread_current ();
983 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for %p, %d ms", __func__, GetCurrentThreadId (), handle, ms));
989 mono_monitor_enter (thread->synch_lock);
990 thread->state |= ThreadState_WaitSleepJoin;
991 mono_monitor_exit (thread->synch_lock);
993 ret=WaitForSingleObjectEx (handle, ms, TRUE);
995 mono_monitor_enter (thread->synch_lock);
996 thread->state &= ~ThreadState_WaitSleepJoin;
997 mono_monitor_exit (thread->synch_lock);
999 if(ret==WAIT_FAILED) {
1000 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1002 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
1003 /* Do we want to try again if we get
1004 * WAIT_IO_COMPLETION? The documentation for
1005 * WaitHandle doesn't give any clues. (We'd have to
1006 * fiddle with the timeout if we retry.)
1008 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1015 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
1019 MONO_ARCH_SAVE_REGS;
1024 mutex = CreateMutex (NULL, owned, NULL);
1026 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
1028 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1036 void ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
1037 MONO_ARCH_SAVE_REGS;
1039 ReleaseMutex(handle);
1042 HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name,
1048 MONO_ARCH_SAVE_REGS;
1050 *error = ERROR_SUCCESS;
1052 ret = OpenMutex (rights, FALSE, mono_string_chars (name));
1054 *error = GetLastError ();
1061 HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created)
1065 MONO_ARCH_SAVE_REGS;
1070 sem = CreateSemaphore (NULL, initialCount, maximumCount, NULL);
1072 sem = CreateSemaphore (NULL, initialCount, maximumCount,
1073 mono_string_chars (name));
1075 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1083 gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail)
1087 MONO_ARCH_SAVE_REGS;
1089 *fail = !ReleaseSemaphore (handle, releaseCount, &prevcount);
1094 HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
1098 MONO_ARCH_SAVE_REGS;
1100 *error = ERROR_SUCCESS;
1102 ret = OpenSemaphore (rights, FALSE, mono_string_chars (name));
1104 *error = GetLastError ();
1110 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created)
1114 MONO_ARCH_SAVE_REGS;
1119 event = CreateEvent (NULL, manual, initial, NULL);
1121 event = CreateEvent (NULL, manual, initial,
1122 mono_string_chars (name));
1124 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1132 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
1133 MONO_ARCH_SAVE_REGS;
1135 return (SetEvent(handle));
1138 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
1139 MONO_ARCH_SAVE_REGS;
1141 return (ResetEvent(handle));
1145 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
1146 MONO_ARCH_SAVE_REGS;
1148 CloseHandle (handle);
1151 HANDLE ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name,
1157 MONO_ARCH_SAVE_REGS;
1159 *error = ERROR_SUCCESS;
1161 ret = OpenEvent (rights, FALSE, mono_string_chars (name));
1163 *error = GetLastError ();
1169 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
1171 MONO_ARCH_SAVE_REGS;
1173 return InterlockedIncrement (location);
1176 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
1180 MONO_ARCH_SAVE_REGS;
1182 mono_interlocked_lock ();
1186 mono_interlocked_unlock ();
1192 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
1194 MONO_ARCH_SAVE_REGS;
1196 return InterlockedDecrement(location);
1199 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
1203 MONO_ARCH_SAVE_REGS;
1205 mono_interlocked_lock ();
1209 mono_interlocked_unlock ();
1214 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location, gint32 value)
1216 MONO_ARCH_SAVE_REGS;
1218 return InterlockedExchange(location, value);
1221 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location, MonoObject *value)
1223 MONO_ARCH_SAVE_REGS;
1225 return (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
1228 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location, gfloat value)
1230 IntFloatUnion val, ret;
1232 MONO_ARCH_SAVE_REGS;
1235 ret.ival = InterlockedExchange((gint32 *) location, val.ival);
1241 ves_icall_System_Threading_Interlocked_Exchange_Long (gint64 *location, gint64 value)
1243 #if SIZEOF_VOID_P == 8
1244 return (gint64) InterlockedExchangePointer((gpointer *) location, (gpointer)value);
1249 * According to MSDN, this function is only atomic with regards to the
1250 * other Interlocked functions on 32 bit platforms.
1252 mono_interlocked_lock ();
1255 mono_interlocked_unlock ();
1262 ves_icall_System_Threading_Interlocked_Exchange_Double (gdouble *location, gdouble value)
1264 #if SIZEOF_VOID_P == 8
1265 LongDoubleUnion val, ret;
1268 ret.ival = (gint64)InterlockedExchangePointer((gpointer *) location, (gpointer)val.ival);
1275 * According to MSDN, this function is only atomic with regards to the
1276 * other Interlocked functions on 32 bit platforms.
1278 mono_interlocked_lock ();
1281 mono_interlocked_unlock ();
1287 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand)
1289 MONO_ARCH_SAVE_REGS;
1291 return InterlockedCompareExchange(location, value, comparand);
1294 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location, MonoObject *value, MonoObject *comparand)
1296 MONO_ARCH_SAVE_REGS;
1298 return (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
1301 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location, gfloat value, gfloat comparand)
1303 IntFloatUnion val, ret, cmp;
1305 MONO_ARCH_SAVE_REGS;
1308 cmp.fval = comparand;
1309 ret.ival = InterlockedCompareExchange((gint32 *) location, val.ival, cmp.ival);
1315 ves_icall_System_Threading_Interlocked_CompareExchange_Double (gdouble *location, gdouble value, gdouble comparand)
1317 #if SIZEOF_VOID_P == 8
1318 LongDoubleUnion val, comp, ret;
1321 comp.fval = comparand;
1322 ret.ival = (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)val.ival, (gpointer)comp.ival);
1328 mono_interlocked_lock ();
1330 if (old == comparand)
1332 mono_interlocked_unlock ();
1339 ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64 *location, gint64 value, gint64 comparand)
1341 #if SIZEOF_VOID_P == 8
1342 return (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)value, (gpointer)comparand);
1346 mono_interlocked_lock ();
1348 if (old == comparand)
1350 mono_interlocked_unlock ();
1357 ves_icall_System_Threading_Interlocked_CompareExchange_T (MonoObject **location, MonoObject *value, MonoObject *comparand)
1359 MONO_ARCH_SAVE_REGS;
1361 return InterlockedCompareExchangePointer ((gpointer *)location, value, comparand);
1365 ves_icall_System_Threading_Interlocked_Exchange_T (MonoObject **location, MonoObject *value)
1367 MONO_ARCH_SAVE_REGS;
1369 return InterlockedExchangePointer ((gpointer *)location, value);
1373 ves_icall_System_Threading_Interlocked_Add_Int (gint32 *location, gint32 value)
1375 #if SIZEOF_VOID_P == 8
1376 /* Should be implemented as a JIT intrinsic */
1377 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1382 mono_interlocked_lock ();
1384 *location = orig + value;
1385 mono_interlocked_unlock ();
1392 ves_icall_System_Threading_Interlocked_Add_Long (gint64 *location, gint64 value)
1394 #if SIZEOF_VOID_P == 8
1395 /* Should be implemented as a JIT intrinsic */
1396 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1401 mono_interlocked_lock ();
1403 *location = orig + value;
1404 mono_interlocked_unlock ();
1411 ves_icall_System_Threading_Interlocked_Read_Long (gint64 *location)
1413 #if SIZEOF_VOID_P == 8
1414 /* 64 bit reads are already atomic */
1419 mono_interlocked_lock ();
1421 mono_interlocked_unlock ();
1428 ves_icall_System_Threading_Thread_MemoryBarrier (void)
1430 /* Should be implemented as a JIT intrinsic */
1431 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1435 ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
1437 mono_monitor_enter (this->synch_lock);
1438 this->state &= ~state;
1439 if (state & ThreadState_Background) {
1440 /* If the thread changes the background mode, the main thread has to
1441 * be notified, since it has to rebuild the list of threads to
1444 SetEvent (background_change_event);
1446 mono_monitor_exit (this->synch_lock);
1450 ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
1452 mono_monitor_enter (this->synch_lock);
1453 this->state |= state;
1454 if (state & ThreadState_Background) {
1455 /* If the thread changes the background mode, the main thread has to
1456 * be notified, since it has to rebuild the list of threads to
1459 SetEvent (background_change_event);
1461 mono_monitor_exit (this->synch_lock);
1465 ves_icall_System_Threading_Thread_GetState (MonoThread* this)
1468 mono_monitor_enter (this->synch_lock);
1469 state = this->state;
1470 mono_monitor_exit (this->synch_lock);
1475 mono_thread_get_abort_signal (void)
1477 #if defined (__MINGW32__) || defined (_MSC_VER)
1483 static int abort_signum = -1;
1485 if (abort_signum != -1)
1486 return abort_signum;
1487 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
1488 for (i = SIGRTMIN + 1; i < SIGRTMAX; ++i) {
1489 struct sigaction sinfo;
1490 sigaction (i, NULL, &sinfo);
1491 if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
1496 /* fallback to the old way */
1499 #endif /*defined (__MINGW32__) || defined (_MSC_VER) */
1502 #if defined (__MINGW32__) || defined (_MSC_VER)
1503 static void CALLBACK interruption_request_apc (ULONG_PTR param)
1505 MonoException* exc = mono_thread_request_interruption (FALSE);
1506 if (exc) mono_raise_exception (exc);
1508 #endif /* defined (__MINGW32__) || defined (_MSC_VER) */
1511 * signal_thread_state_change
1513 * Tells the thread that his state has changed and it has to enter the new
1514 * state as soon as possible.
1516 static void signal_thread_state_change (MonoThread *thread)
1518 if (thread == mono_thread_current ()) {
1519 /* Do it synchronously */
1520 MonoException *exc = mono_thread_request_interruption (FALSE);
1522 mono_raise_exception (exc);
1525 #if defined (__MINGW32__) || defined (_MSC_VER)
1526 QueueUserAPC ((PAPCFUNC)interruption_request_apc, thread->handle, NULL);
1528 /* fixme: store the state somewhere */
1529 #ifdef PTHREAD_POINTER_ID
1530 pthread_kill ((gpointer)(gsize)(thread->tid), mono_thread_get_abort_signal ());
1532 pthread_kill (thread->tid, mono_thread_get_abort_signal ());
1534 #endif /* defined (__MINGW32__) || defined (__MSC_VER) */
1538 ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
1540 MONO_ARCH_SAVE_REGS;
1542 mono_monitor_enter (thread->synch_lock);
1544 if ((thread->state & ThreadState_AbortRequested) != 0 ||
1545 (thread->state & ThreadState_StopRequested) != 0 ||
1546 (thread->state & ThreadState_Stopped) != 0)
1548 mono_monitor_exit (thread->synch_lock);
1552 if ((thread->state & ThreadState_Unstarted) != 0) {
1553 thread->state |= ThreadState_Aborted;
1554 mono_monitor_exit (thread->synch_lock);
1558 thread->state |= ThreadState_AbortRequested;
1559 MONO_OBJECT_SETREF (thread, abort_state, state);
1560 thread->abort_exc = NULL;
1562 mono_monitor_exit (thread->synch_lock);
1564 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Abort requested for %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
1566 /* Make sure the thread is awake */
1567 mono_thread_resume (thread);
1569 signal_thread_state_change (thread);
1573 ves_icall_System_Threading_Thread_ResetAbort (void)
1575 MonoThread *thread = mono_thread_current ();
1577 MONO_ARCH_SAVE_REGS;
1579 mono_monitor_enter (thread->synch_lock);
1581 thread->state &= ~ThreadState_AbortRequested;
1583 if (!thread->abort_exc) {
1584 const char *msg = "Unable to reset abort because no abort was requested";
1585 mono_monitor_exit (thread->synch_lock);
1586 mono_raise_exception (mono_get_exception_thread_state (msg));
1588 thread->abort_exc = NULL;
1589 thread->abort_state = NULL;
1592 mono_monitor_exit (thread->synch_lock);
1596 mono_thread_suspend (MonoThread *thread)
1598 MONO_ARCH_SAVE_REGS;
1600 mono_monitor_enter (thread->synch_lock);
1602 if ((thread->state & ThreadState_Unstarted) != 0 ||
1603 (thread->state & ThreadState_Aborted) != 0 ||
1604 (thread->state & ThreadState_Stopped) != 0)
1606 mono_monitor_exit (thread->synch_lock);
1610 if ((thread->state & ThreadState_Suspended) != 0 ||
1611 (thread->state & ThreadState_SuspendRequested) != 0 ||
1612 (thread->state & ThreadState_StopRequested) != 0)
1614 mono_monitor_exit (thread->synch_lock);
1618 thread->state |= ThreadState_SuspendRequested;
1619 mono_monitor_exit (thread->synch_lock);
1621 signal_thread_state_change (thread);
1626 ves_icall_System_Threading_Thread_Suspend (MonoThread *thread)
1628 if (!mono_thread_suspend (thread))
1629 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1633 mono_thread_resume (MonoThread *thread)
1635 MONO_ARCH_SAVE_REGS;
1637 mono_monitor_enter (thread->synch_lock);
1639 if ((thread->state & ThreadState_SuspendRequested) != 0) {
1640 thread->state &= ~ThreadState_SuspendRequested;
1641 mono_monitor_exit (thread->synch_lock);
1645 if ((thread->state & ThreadState_Suspended) == 0 ||
1646 (thread->state & ThreadState_Unstarted) != 0 ||
1647 (thread->state & ThreadState_Aborted) != 0 ||
1648 (thread->state & ThreadState_Stopped) != 0)
1650 mono_monitor_exit (thread->synch_lock);
1654 thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1656 /* Awake the thread */
1657 SetEvent (thread->suspend_event);
1659 mono_monitor_exit (thread->synch_lock);
1661 /* Wait for the thread to awake */
1662 WaitForSingleObject (thread->resume_event, INFINITE);
1663 CloseHandle (thread->resume_event);
1664 thread->resume_event = NULL;
1670 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
1672 if (!mono_thread_resume (thread))
1673 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1677 find_wrapper (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1682 if (m->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1683 m->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
1684 m->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH)
1686 *((gboolean*)data) = TRUE;
1693 is_running_protected_wrapper (void)
1695 gboolean found = FALSE;
1696 mono_stack_walk (find_wrapper, &found);
1700 void mono_thread_stop (MonoThread *thread)
1702 mono_monitor_enter (thread->synch_lock);
1704 if ((thread->state & ThreadState_StopRequested) != 0 ||
1705 (thread->state & ThreadState_Stopped) != 0)
1707 mono_monitor_exit (thread->synch_lock);
1711 /* Make sure the thread is awake */
1712 mono_thread_resume (thread);
1714 thread->state |= ThreadState_StopRequested;
1715 thread->state &= ~ThreadState_AbortRequested;
1717 mono_monitor_exit (thread->synch_lock);
1719 signal_thread_state_change (thread);
1723 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
1725 return *((volatile gint8 *) (ptr));
1729 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
1731 return *((volatile gint16 *) (ptr));
1735 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
1737 return *((volatile gint32 *) (ptr));
1741 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
1743 return *((volatile gint64 *) (ptr));
1747 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
1749 return (void *) *((volatile void **) ptr);
1753 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
1755 *((volatile gint8 *) ptr) = value;
1759 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
1761 *((volatile gint16 *) ptr) = value;
1765 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
1767 *((volatile gint32 *) ptr) = value;
1771 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
1773 *((volatile gint64 *) ptr) = value;
1777 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
1779 *((volatile void **) ptr) = value;
1782 void mono_thread_init (MonoThreadStartCB start_cb,
1783 MonoThreadAttachCB attach_cb)
1785 InitializeCriticalSection(&threads_mutex);
1786 InitializeCriticalSection(&interlocked_mutex);
1787 InitializeCriticalSection(&contexts_mutex);
1788 background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1790 mono_init_static_data_info (&thread_static_info);
1791 mono_init_static_data_info (&context_static_info);
1793 current_object_key=TlsAlloc();
1794 THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__, current_object_key));
1796 mono_thread_start_cb = start_cb;
1797 mono_thread_attach_cb = attach_cb;
1799 /* Get a pseudo handle to the current process. This is just a
1800 * kludge so that wapi can build a process handle if needed.
1801 * As a pseudo handle is returned, we don't need to clean
1804 GetCurrentProcess ();
1808 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
1810 mono_thread_cleanup = func;
1814 static void print_tids (gpointer key, gpointer value, gpointer user)
1816 /* GPOINTER_TO_UINT breaks horribly if sizeof(void *) >
1817 * sizeof(uint) and a cast to uint would overflow
1819 /* Older versions of glib don't have G_GSIZE_FORMAT, so just
1820 * print this as a pointer.
1822 g_message ("Waiting for: %p", key);
1827 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1828 MonoThread *threads[MAXIMUM_WAIT_OBJECTS];
1832 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
1836 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
1838 ret=WaitForMultipleObjectsEx(wait->num, wait->handles, TRUE, timeout, FALSE);
1840 if(ret==WAIT_FAILED) {
1841 /* See the comment in build_wait_tids() */
1842 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
1846 for(i=0; i<wait->num; i++)
1847 CloseHandle (wait->handles[i]);
1849 if (ret == WAIT_TIMEOUT)
1852 for(i=0; i<wait->num; i++) {
1853 gsize tid = wait->threads[i]->tid;
1855 if(mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
1856 /* This thread must have been killed, because
1857 * it hasn't cleaned itself up. (It's just
1858 * possible that the thread exited before the
1859 * parent thread had a chance to store the
1860 * handle, and now there is another pointer to
1861 * the already-exited thread stored. In this
1862 * case, we'll just get two
1863 * mono_profiler_thread_end() calls for the
1867 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
1868 thread_cleanup (wait->threads[i]);
1873 static void wait_for_tids_or_state_change (struct wait_data *wait, guint32 timeout)
1875 guint32 i, ret, count;
1877 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
1879 /* Add the thread state change event, so it wakes up if a thread changes
1880 * to background mode.
1883 if (count < MAXIMUM_WAIT_OBJECTS) {
1884 wait->handles [count] = background_change_event;
1888 ret=WaitForMultipleObjectsEx (count, wait->handles, FALSE, timeout, FALSE);
1890 if(ret==WAIT_FAILED) {
1891 /* See the comment in build_wait_tids() */
1892 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
1896 for(i=0; i<wait->num; i++)
1897 CloseHandle (wait->handles[i]);
1899 if (ret == WAIT_TIMEOUT)
1902 if (ret < wait->num) {
1903 gsize tid = wait->threads[ret]->tid;
1904 mono_threads_lock ();
1905 if (mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
1906 /* See comment in wait_for_tids about thread cleanup */
1907 mono_threads_unlock ();
1908 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
1909 thread_cleanup (wait->threads [ret]);
1911 mono_threads_unlock ();
1915 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
1917 struct wait_data *wait=(struct wait_data *)user;
1919 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
1921 MonoThread *thread=(MonoThread *)value;
1923 /* Ignore background threads, we abort them later */
1924 mono_monitor_enter (thread->synch_lock);
1925 if (thread->state & ThreadState_Background) {
1926 mono_monitor_exit (thread->synch_lock);
1927 return; /* just leave, ignore */
1929 mono_monitor_exit (thread->synch_lock);
1931 if (mono_gc_is_finalizer_thread (thread))
1934 if (thread == mono_thread_current ())
1937 if (thread == mono_thread_get_main ())
1940 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1944 wait->handles[wait->num]=handle;
1945 wait->threads[wait->num]=thread;
1948 /* Just ignore the rest, we can't do anything with
1955 remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
1957 struct wait_data *wait=(struct wait_data *)user;
1958 gsize self = GetCurrentThreadId ();
1959 MonoThread *thread = (MonoThread *) value;
1962 if (wait->num >= MAXIMUM_WAIT_OBJECTS)
1965 /* The finalizer thread is not a background thread */
1966 if (thread->tid != self && (thread->state & ThreadState_Background) != 0) {
1968 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1972 if(thread->state & ThreadState_AbortRequested ||
1973 thread->state & ThreadState_Aborted) {
1974 THREAD_DEBUG (g_message ("%s: Thread id %"G_GSIZE_FORMAT" already aborting", __func__, (gsize)thread->tid));
1978 /* printf ("A: %d\n", wait->num); */
1979 wait->handles[wait->num]=thread->handle;
1980 wait->threads[wait->num]=thread;
1983 THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT"\n", __func__, (gsize)thread->tid));
1984 mono_thread_stop (thread);
1988 return (thread->tid != self && !mono_gc_is_finalizer_thread (thread));
1991 void mono_thread_manage (void)
1993 struct wait_data *wait=g_new0 (struct wait_data, 1);
1995 /* join each thread that's still running */
1996 THREAD_DEBUG (g_message ("%s: Joining each running thread...", __func__));
1998 mono_threads_lock ();
2000 THREAD_DEBUG (g_message("%s: No threads", __func__));
2001 mono_threads_unlock ();
2004 mono_threads_unlock ();
2007 mono_threads_lock ();
2008 THREAD_DEBUG (g_message ("%s: There are %d threads to join", __func__, mono_g_hash_table_size (threads));
2009 mono_g_hash_table_foreach (threads, print_tids, NULL));
2011 ResetEvent (background_change_event);
2013 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
2014 mono_threads_unlock ();
2016 /* Something to wait for */
2017 wait_for_tids_or_state_change (wait, INFINITE);
2019 THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
2020 } while(wait->num>0);
2022 mono_runtime_set_shutting_down ();
2024 THREAD_DEBUG (g_message ("%s: threadpool cleanup", __func__));
2025 mono_thread_pool_cleanup ();
2028 * Remove everything but the finalizer thread and self.
2029 * Also abort all the background threads
2032 mono_threads_lock ();
2035 mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
2037 mono_threads_unlock ();
2039 THREAD_DEBUG (g_message ("%s: wait->num is now %d", __func__, wait->num));
2041 /* Something to wait for */
2042 wait_for_tids (wait, INFINITE);
2044 } while (wait->num > 0);
2046 #if !defined(PLATFORM_WIN32) && !defined(RUN_IN_SUBTHREAD)
2047 /* The main thread must abandon any held mutexes (particularly
2048 * important for named mutexes as they are shared across
2049 * processes, see bug 74680.) This will happen when the
2050 * thread exits, but if it's not running in a subthread it
2051 * won't exit in time.
2053 /* Using non-w32 API is a nasty kludge, but I couldn't find
2054 * anything in the documentation that would let me do this
2055 * here yet still be safe to call on windows.
2057 _wapi_thread_abandon_mutexes (GetCurrentThread ());
2061 * give the subthreads a chance to really quit (this is mainly needed
2062 * to get correct user and system times from getrusage/wait/time(1)).
2063 * This could be removed if we avoid pthread_detach() and use pthread_join().
2065 #ifndef PLATFORM_WIN32
2072 static void terminate_thread (gpointer key, gpointer value, gpointer user)
2074 MonoThread *thread=(MonoThread *)value;
2076 if(thread->tid != (gsize)user) {
2077 /*TerminateThread (thread->handle, -1);*/
2081 void mono_thread_abort_all_other_threads (void)
2083 gsize self = GetCurrentThreadId ();
2085 mono_threads_lock ();
2086 THREAD_DEBUG (g_message ("%s: There are %d threads to abort", __func__,
2087 mono_g_hash_table_size (threads));
2088 mono_g_hash_table_foreach (threads, print_tids, NULL));
2090 mono_g_hash_table_foreach (threads, terminate_thread, (gpointer)self);
2092 mono_threads_unlock ();
2096 collect_threads (gpointer key, gpointer value, gpointer user_data)
2098 MonoThread *thread = (MonoThread*)value;
2099 struct wait_data *wait = (struct wait_data*)user_data;
2102 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
2103 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2107 wait->handles [wait->num] = handle;
2108 wait->threads [wait->num] = thread;
2114 * mono_thread_suspend_all_other_threads:
2116 * Suspend all managed threads except the finalizer thread and this thread.
2118 void mono_thread_suspend_all_other_threads (void)
2120 struct wait_data *wait = g_new0 (struct wait_data, 1);
2122 gsize self = GetCurrentThreadId ();
2124 guint32 eventidx = 0;
2127 * Make a copy of the hashtable since we can't do anything with
2128 * threads while threads_mutex is held.
2130 mono_threads_lock ();
2131 mono_g_hash_table_foreach (threads, collect_threads, wait);
2132 mono_threads_unlock ();
2134 events = g_new0 (gpointer, wait->num);
2136 /* Get the suspended events that we'll be waiting for */
2137 for (i = 0; i < wait->num; ++i) {
2138 MonoThread *thread = wait->threads [i];
2140 if ((thread->tid == self) || mono_gc_is_finalizer_thread (thread)) {
2141 //CloseHandle (wait->handles [i]);
2142 wait->threads [i] = NULL; /* ignore this thread in next loop */
2146 mono_monitor_enter (thread->synch_lock);
2148 if ((thread->state & ThreadState_Suspended) != 0 ||
2149 (thread->state & ThreadState_SuspendRequested) != 0 ||
2150 (thread->state & ThreadState_StopRequested) != 0 ||
2151 (thread->state & ThreadState_Stopped) != 0) {
2152 mono_monitor_exit (thread->synch_lock);
2153 CloseHandle (wait->handles [i]);
2154 wait->threads [i] = NULL; /* ignore this thread in next loop */
2158 /* Convert abort requests into suspend requests */
2159 if ((thread->state & ThreadState_AbortRequested) != 0)
2160 thread->state &= ~ThreadState_AbortRequested;
2162 thread->state |= ThreadState_SuspendRequested;
2164 if (thread->suspended_event == NULL)
2165 thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2167 events [eventidx++] = thread->suspended_event;
2168 mono_monitor_exit (thread->synch_lock);
2170 /* Signal the thread to suspend */
2171 signal_thread_state_change (thread);
2174 WaitForMultipleObjectsEx (eventidx, events, TRUE, INFINITE, FALSE);
2175 for (i = 0; i < wait->num; ++i) {
2176 MonoThread *thread = wait->threads [i];
2181 mono_monitor_enter (thread->synch_lock);
2182 CloseHandle (thread->suspended_event);
2183 thread->suspended_event = NULL;
2184 mono_monitor_exit (thread->synch_lock);
2192 * mono_threads_request_thread_dump:
2194 * Ask all threads except the current to print their stacktrace to stdout.
2197 mono_threads_request_thread_dump (void)
2199 struct wait_data *wait = g_new0 (struct wait_data, 1);
2203 * Make a copy of the hashtable since we can't do anything with
2204 * threads while threads_mutex is held.
2206 mono_threads_lock ();
2207 mono_g_hash_table_foreach (threads, collect_threads, wait);
2208 mono_threads_unlock ();
2210 for (i = 0; i < wait->num; ++i) {
2211 MonoThread *thread = wait->threads [i];
2213 if (!mono_gc_is_finalizer_thread (thread) && (thread != mono_thread_current ()) && !thread->thread_dump_requested) {
2214 thread->thread_dump_requested = TRUE;
2216 signal_thread_state_change (thread);
2219 CloseHandle (wait->handles [i]);
2224 * mono_thread_push_appdomain_ref:
2226 * Register that the current thread may have references to objects in domain
2227 * @domain on its stack. Each call to this function should be paired with a
2228 * call to pop_appdomain_ref.
2231 mono_thread_push_appdomain_ref (MonoDomain *domain)
2233 MonoThread *thread = mono_thread_current ();
2236 /* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
2237 mono_threads_lock ();
2238 thread->appdomain_refs = g_slist_prepend (thread->appdomain_refs, domain);
2239 mono_threads_unlock ();
2244 mono_thread_pop_appdomain_ref (void)
2246 MonoThread *thread = mono_thread_current ();
2249 /* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
2250 mono_threads_lock ();
2251 /* FIXME: How can the list be empty ? */
2252 if (thread->appdomain_refs)
2253 thread->appdomain_refs = g_slist_remove (thread->appdomain_refs, thread->appdomain_refs->data);
2254 mono_threads_unlock ();
2259 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
2262 mono_threads_lock ();
2263 res = g_slist_find (thread->appdomain_refs, domain) != NULL;
2264 mono_threads_unlock ();
2268 typedef struct abort_appdomain_data {
2269 struct wait_data wait;
2271 } abort_appdomain_data;
2274 abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
2276 MonoThread *thread = (MonoThread*)value;
2277 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
2278 MonoDomain *domain = data->domain;
2280 if (mono_thread_has_appdomain_ref (thread, domain)) {
2281 HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2285 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
2287 ves_icall_System_Threading_Thread_Abort (thread, NULL);
2289 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
2290 data->wait.handles [data->wait.num] = handle;
2291 data->wait.threads [data->wait.num] = thread;
2294 /* Just ignore the rest, we can't do anything with
2302 * mono_threads_abort_appdomain_threads:
2304 * Abort threads which has references to the given appdomain.
2307 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
2309 abort_appdomain_data user_data;
2312 /* printf ("ABORT BEGIN.\n"); */
2314 start_time = GetTickCount ();
2316 mono_threads_lock ();
2318 user_data.domain = domain;
2319 user_data.wait.num = 0;
2320 mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
2321 mono_threads_unlock ();
2323 if (user_data.wait.num > 0)
2325 * We should wait for the threads either to abort, or to leave the
2326 * domain. We can't do the latter, so we wait with a timeout.
2328 wait_for_tids (&user_data.wait, 100);
2330 /* Update remaining time */
2331 timeout -= GetTickCount () - start_time;
2332 start_time = GetTickCount ();
2337 while (user_data.wait.num > 0);
2339 /* printf ("ABORT DONE.\n"); */
2345 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
2347 MonoThread *thread = (MonoThread*)value;
2348 MonoDomain *domain = (MonoDomain*)user_data;
2351 /* No locking needed here */
2352 /* FIXME: why no locking? writes to the cache are protected with synch_lock above */
2354 if (thread->cached_culture_info) {
2355 for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i) {
2356 MonoObject *obj = mono_array_get (thread->cached_culture_info, MonoObject*, i);
2357 if (obj && obj->vtable->domain == domain)
2358 mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
2364 * mono_threads_clear_cached_culture:
2366 * Clear the cached_current_culture from all threads if it is in the
2370 mono_threads_clear_cached_culture (MonoDomain *domain)
2372 mono_threads_lock ();
2373 mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
2374 mono_threads_unlock ();
2378 * mono_thread_get_pending_exception:
2380 * Return an exception which needs to be raised when leaving a catch clause.
2381 * This is used for undeniable exception propagation.
2384 mono_thread_get_pending_exception (void)
2386 MonoThread *thread = mono_thread_current ();
2388 MONO_ARCH_SAVE_REGS;
2390 if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
2392 * FIXME: Clear the abort exception and return an AppDomainUnloaded
2393 * exception if the thread no longer references a dying appdomain.
2395 thread->abort_exc->trace_ips = NULL;
2396 thread->abort_exc->stack_trace = NULL;
2397 return thread->abort_exc;
2403 #define NUM_STATIC_DATA_IDX 8
2404 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
2405 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
2410 * mono_alloc_static_data
2412 * Allocate memory blocks for storing threads or context static data
2415 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset)
2417 guint idx = (offset >> 24) - 1;
2420 gpointer* static_data = *static_data_ptr;
2422 static_data = mono_gc_alloc_fixed (static_data_size [0], NULL);
2423 *static_data_ptr = static_data;
2424 static_data [0] = static_data;
2427 for (i = 1; i <= idx; ++i) {
2428 if (static_data [i])
2430 static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
2435 * mono_init_static_data_info
2437 * Initializes static data counters
2439 static void mono_init_static_data_info (StaticDataInfo *static_data)
2441 static_data->idx = 0;
2442 static_data->offset = 0;
2446 * mono_alloc_static_data_slot
2448 * Generates an offset for static data. static_data contains the counters
2449 * used to generate it.
2452 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
2456 if (!static_data->idx && !static_data->offset) {
2458 * we use the first chunk of the first allocation also as
2459 * an array for the rest of the data
2461 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
2463 static_data->offset += align - 1;
2464 static_data->offset &= ~(align - 1);
2465 if (static_data->offset + size >= static_data_size [static_data->idx]) {
2466 static_data->idx ++;
2467 g_assert (size <= static_data_size [static_data->idx]);
2469 * massive unloading and reloading of domains with thread-static
2470 * data may eventually exceed the allocated storage...
2471 * Need to check what the MS runtime does in that case.
2472 * Note that for each appdomain, we need to allocate a separate
2473 * thread data slot for security reasons. We could keep track
2474 * of the slots per-domain and when the domain is unloaded
2475 * out the slots on a sort of free list.
2477 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
2478 static_data->offset = 0;
2480 offset = static_data->offset | ((static_data->idx + 1) << 24);
2481 static_data->offset += size;
2486 * ensure thread static fields already allocated are valid for thread
2487 * This function is called when a thread is created or on thread attach.
2490 thread_adjust_static_data (MonoThread *thread)
2494 mono_threads_lock ();
2495 if (thread_static_info.offset || thread_static_info.idx > 0) {
2496 /* get the current allocated size */
2497 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
2498 mono_alloc_static_data (&(thread->static_data), offset);
2500 mono_threads_unlock ();
2504 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
2506 MonoThread *thread = value;
2507 guint32 offset = GPOINTER_TO_UINT (user);
2509 mono_alloc_static_data (&(thread->static_data), offset);
2513 * The offset for a special static variable is composed of three parts:
2514 * a bit that indicates the type of static data (0:thread, 1:context),
2515 * an index in the array of chunks of memory for the thread (thread->static_data)
2516 * and an offset in that chunk of mem. This allows allocating less memory in the
2521 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align)
2524 if (static_type == SPECIAL_STATIC_THREAD)
2526 mono_threads_lock ();
2527 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
2528 /* This can be called during startup */
2529 if (threads != NULL)
2530 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
2531 mono_threads_unlock ();
2535 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
2536 mono_contexts_lock ();
2537 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
2538 mono_contexts_unlock ();
2539 offset |= 0x80000000; /* Set the high bit to indicate context static data */
2545 mono_get_special_static_data (guint32 offset)
2547 /* The high bit means either thread (0) or static (1) data. */
2549 guint32 static_type = (offset & 0x80000000);
2552 offset &= 0x7fffffff;
2553 idx = (offset >> 24) - 1;
2555 if (static_type == 0)
2557 MonoThread *thread = mono_thread_current ();
2558 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
2562 /* Allocate static data block under demand, since we don't have a list
2565 MonoAppContext *context = mono_context_get ();
2566 if (!context->static_data || !context->static_data [idx]) {
2567 mono_contexts_lock ();
2568 mono_alloc_static_data (&(context->static_data), offset);
2569 mono_contexts_unlock ();
2571 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
2575 static MonoClassField *local_slots = NULL;
2578 /* local tls data to get locals_slot from a thread */
2581 /* index in the locals_slot array */
2586 clear_local_slot (gpointer key, gpointer value, gpointer user_data)
2588 LocalSlotID *sid = user_data;
2589 MonoThread *thread = (MonoThread*)value;
2590 MonoArray *slots_array;
2592 * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
2593 * it is for the right domain, so we need to check if it is allocated an initialized
2594 * for the current thread.
2596 /*g_print ("handling thread %p\n", thread);*/
2597 if (!thread->static_data || !thread->static_data [sid->idx])
2599 slots_array = *(MonoArray **)(((char*) thread->static_data [sid->idx]) + (sid->offset & 0xffffff));
2600 if (!slots_array || sid->slot >= mono_array_length (slots_array))
2602 mono_array_set (slots_array, MonoObject*, sid->slot, NULL);
2606 mono_thread_free_local_slot_values (int slot, MonoBoolean thread_local)
2614 local_slots = mono_class_get_field_from_name (mono_defaults.thread_class, "local_slots");
2616 g_warning ("local_slots field not found in Thread class");
2620 domain = mono_domain_get ();
2621 mono_domain_lock (domain);
2622 if (domain->special_static_fields)
2623 addr = g_hash_table_lookup (domain->special_static_fields, local_slots);
2624 mono_domain_unlock (domain);
2627 /*g_print ("freeing slot %d at %p\n", slot, addr);*/
2628 sid.offset = GPOINTER_TO_UINT (addr);
2629 sid.offset &= 0x7fffffff;
2630 sid.idx = (sid.offset >> 24) - 1;
2631 mono_threads_lock ();
2632 mono_g_hash_table_foreach (threads, clear_local_slot, &sid);
2633 mono_threads_unlock ();
2635 /* FIXME: clear the slot for MonoAppContexts, too */
2640 static CALLBACK void dummy_apc (ULONG_PTR param)
2644 static guint32 dummy_apc (gpointer param)
2651 * mono_thread_execute_interruption
2653 * Performs the operation that the requested thread state requires (abort,
2656 static MonoException* mono_thread_execute_interruption (MonoThread *thread)
2658 mono_monitor_enter (thread->synch_lock);
2660 if (thread->interruption_requested) {
2661 /* this will consume pending APC calls */
2662 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
2663 InterlockedDecrement (&thread_interruption_requested);
2664 thread->interruption_requested = FALSE;
2667 if ((thread->state & ThreadState_AbortRequested) != 0) {
2668 if (thread->abort_exc == NULL)
2669 MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
2670 mono_monitor_exit (thread->synch_lock);
2671 return thread->abort_exc;
2673 else if ((thread->state & ThreadState_SuspendRequested) != 0) {
2674 thread->state &= ~ThreadState_SuspendRequested;
2675 thread->state |= ThreadState_Suspended;
2676 thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2677 if (thread->suspended_event)
2678 SetEvent (thread->suspended_event);
2679 mono_monitor_exit (thread->synch_lock);
2681 WaitForSingleObject (thread->suspend_event, INFINITE);
2683 mono_monitor_enter (thread->synch_lock);
2684 CloseHandle (thread->suspend_event);
2685 thread->suspend_event = NULL;
2686 thread->state &= ~ThreadState_Suspended;
2688 /* The thread that requested the resume will have replaced this event
2689 * and will be waiting for it
2691 SetEvent (thread->resume_event);
2692 mono_monitor_exit (thread->synch_lock);
2695 else if ((thread->state & ThreadState_StopRequested) != 0) {
2696 /* FIXME: do this through the JIT? */
2697 mono_monitor_exit (thread->synch_lock);
2698 mono_thread_exit ();
2702 mono_monitor_exit (thread->synch_lock);
2707 * mono_thread_request_interruption
2709 * A signal handler can call this method to request the interruption of a
2710 * thread. The result of the interruption will depend on the current state of
2711 * the thread. If the result is an exception that needs to be throw, it is
2712 * provided as return value.
2714 MonoException* mono_thread_request_interruption (gboolean running_managed)
2716 MonoThread *thread = mono_thread_current ();
2718 /* The thread may already be stopping */
2722 mono_monitor_enter (thread->synch_lock);
2724 if (thread->interruption_requested) {
2725 mono_monitor_exit (thread->synch_lock);
2729 if (!running_managed || is_running_protected_wrapper ()) {
2730 /* Can't stop while in unmanaged code. Increase the global interruption
2731 request count. When exiting the unmanaged method the count will be
2732 checked and the thread will be interrupted. */
2734 InterlockedIncrement (&thread_interruption_requested);
2735 thread->interruption_requested = TRUE;
2736 mono_monitor_exit (thread->synch_lock);
2738 /* this will awake the thread if it is in WaitForSingleObject
2740 QueueUserAPC ((PAPCFUNC)dummy_apc, thread->handle, NULL);
2744 mono_monitor_exit (thread->synch_lock);
2745 return mono_thread_execute_interruption (thread);
2749 gboolean mono_thread_interruption_requested ()
2751 if (thread_interruption_requested) {
2752 MonoThread *thread = mono_thread_current ();
2753 /* The thread may already be stopping */
2755 return (thread->interruption_requested);
2760 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
2762 MonoThread *thread = mono_thread_current ();
2764 /* The thread may already be stopping */
2768 if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
2769 MonoException* exc = mono_thread_execute_interruption (thread);
2770 if (exc) mono_raise_exception (exc);
2775 * Performs the interruption of the current thread, if one has been requested,
2776 * and the thread is not running a protected wrapper.
2778 void mono_thread_interruption_checkpoint ()
2780 mono_thread_interruption_checkpoint_request (FALSE);
2784 * Performs the interruption of the current thread, if one has been requested.
2786 void mono_thread_force_interruption_checkpoint ()
2788 mono_thread_interruption_checkpoint_request (TRUE);
2792 * mono_thread_interruption_request_flag:
2794 * Returns the address of a flag that will be non-zero if an interruption has
2795 * been requested for a thread. The thread to interrupt may not be the current
2796 * thread, so an additional call to mono_thread_interruption_requested() or
2797 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
2800 gint32* mono_thread_interruption_request_flag ()
2802 return &thread_interruption_requested;
2805 static void debugger_create_all_threads (gpointer key, gpointer value, gpointer user)
2807 debugger_thread_created ((MonoThread *) value);
2811 mono_debugger_create_all_threads (void)
2813 mono_threads_lock ();
2814 mono_g_hash_table_foreach (threads, debugger_create_all_threads, NULL);
2815 mono_threads_unlock ();
2818 #if MONO_DEBUGGER_SUPPORTED
2820 static guint64 debugger_main_thread_id = -1;
2821 static gpointer debugger_main_thread_stack_ptr = NULL;
2822 static gpointer debugger_main_thread_end_stack = NULL;
2824 extern void GC_push_all_stack (gpointer b, gpointer t);
2827 debugger_gc_push_stack (gpointer key, gpointer value, gpointer user)
2829 MonoThread *thread = (MonoThread*)value;
2833 * The debugger stops all other threads for us in debugger_gc_stop_world() and
2834 * then sets `thread->end_stack' for each of them.
2837 end_stack = (thread->tid == GetCurrentThreadId ()) ? &key : thread->end_stack;
2839 if (!end_stack || !thread->stack_ptr)
2842 GC_push_all_stack (end_stack, thread->stack_ptr);
2846 * We're called with the thread lock.
2849 debugger_gc_push_all_stacks (void)
2851 gpointer end_stack = (debugger_main_thread_id == GetCurrentThreadId ()) ?
2852 &end_stack : debugger_main_thread_end_stack;
2854 GC_push_all_stack (end_stack, debugger_main_thread_stack_ptr);
2856 if (threads != NULL)
2857 mono_g_hash_table_foreach (threads, debugger_gc_push_stack, NULL);
2861 debugger_gc_stop_world (void)
2864 * Acquire the thread lock and tell the debugger to stop all other threads.
2866 mono_threads_lock ();
2867 mono_debugger_event (
2868 MONO_DEBUGGER_EVENT_ACQUIRE_GLOBAL_THREAD_LOCK, 0, 0);
2872 debugger_gc_start_world (void)
2875 * Tell the debugger to resume all other threads and release the lock.
2877 mono_debugger_event (
2878 MONO_DEBUGGER_EVENT_RELEASE_GLOBAL_THREAD_LOCK, 0, 0);
2879 mono_threads_unlock ();
2883 debugger_gc_init (void)
2886 static GCThreadFunctions debugger_thread_vtable = {
2889 debugger_gc_stop_world,
2890 debugger_gc_push_all_stacks,
2891 debugger_gc_start_world
2895 * mono_debugger_init_threads:
2897 * This is used when running inside the Mono Debugger.
2900 mono_debugger_init_threads (gpointer main_thread_stack)
2902 gc_thread_vtable = &debugger_thread_vtable;
2904 debugger_main_thread_id = GetCurrentThreadId ();
2905 debugger_main_thread_stack_ptr = main_thread_stack;
2907 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
2908 (guint64) (gsize) &debugger_main_thread_end_stack,
2909 debugger_main_thread_id);
2912 #else /* WITH_INCLUDED_LIBGC */
2915 mono_debugger_init_threads (gpointer main_thread_stack)
2917 g_assert_not_reached ();