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/utils/mono-compiler.h>
36 #include <mono/os/gc_wrapper.h>
38 /*#define THREAD_DEBUG(a) do { a; } while (0)*/
39 #define THREAD_DEBUG(a)
40 /*#define THREAD_WAIT_DEBUG(a) do { a; } while (0)*/
41 #define THREAD_WAIT_DEBUG(a)
42 /*#define LIBGC_DEBUG(a) do { a; } while (0)*/
43 #define LIBGC_DEBUG(a)
47 guint32 (*func)(void *);
69 /* Number of cached culture objects in the MonoThread->culture_info array */
70 #define NUM_CACHED_CULTURES 4
73 * The "os_handle" field of the WaitHandle class.
75 static MonoClassField *wait_handle_os_handle_field = NULL;
77 /* Controls access to the 'threads' hash table */
78 static CRITICAL_SECTION threads_mutex;
80 /* Controls access to context static data */
81 static CRITICAL_SECTION contexts_mutex;
83 /* Holds current status of static data heap */
84 static StaticDataInfo thread_static_info;
85 static StaticDataInfo context_static_info;
87 /* The hash of existing threads (key is thread ID) that need joining
90 static MonoGHashTable *threads=NULL;
92 /* The TLS key that holds the MonoObject assigned to each thread */
93 static guint32 current_object_key = -1;
96 /* we need to use both the Tls* functions and __thread because
97 * the gc needs to see all the threads
99 static __thread MonoThread * tls_current_object MONO_TLS_FAST;
100 #define SET_CURRENT_OBJECT(x) do { \
101 tls_current_object = x; \
102 TlsSetValue (current_object_key, x); \
104 #define GET_CURRENT_OBJECT() tls_current_object
106 #define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x);
107 #define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key);
110 /* function called at thread start */
111 static MonoThreadStartCB mono_thread_start_cb = NULL;
113 /* function called at thread attach */
114 static MonoThreadAttachCB mono_thread_attach_cb = NULL;
116 /* function called at thread cleanup */
117 static MonoThreadCleanupFunc mono_thread_cleanup = NULL;
119 /* function called when a new thread has been created */
120 static MonoThreadCallbacks *mono_thread_callbacks = NULL;
122 /* The default stack size for each thread */
123 static guint32 default_stacksize = 0;
124 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
126 static void thread_adjust_static_data (MonoThread *thread);
127 static void mono_init_static_data_info (StaticDataInfo *static_data);
128 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
129 static gboolean mono_thread_resume (MonoThread* thread);
130 static void mono_thread_start (MonoThread *thread);
132 /* Spin lock for InterlockedXXX 64 bit functions */
133 static CRITICAL_SECTION interlocked_mutex;
135 /* global count of thread interruptions requested */
136 static gint32 thread_interruption_requested = 0;
138 /* Event signaled when a thread changes its background mode */
139 static HANDLE background_change_event;
142 mono_thread_get_tls_key (void)
144 return current_object_key;
148 mono_thread_get_tls_offset (void)
151 MONO_THREAD_VAR_OFFSET (tls_current_object,offset);
155 /* handle_store() and handle_remove() manage the array of threads that
156 * still need to be waited for when the main thread exits.
158 static void handle_store(MonoThread *thread)
160 EnterCriticalSection(&threads_mutex);
162 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": thread %p ID %d", thread, thread->tid));
165 MONO_GC_REGISTER_ROOT (threads);
166 threads=mono_g_hash_table_new(NULL, NULL);
169 /* We don't need to duplicate thread->handle, because it is
170 * only closed when the thread object is finalized by the GC.
172 mono_g_hash_table_insert(threads, GUINT_TO_POINTER(thread->tid), thread);
173 LeaveCriticalSection(&threads_mutex);
176 static void handle_remove(guint32 tid)
178 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": thread ID %d", tid));
180 EnterCriticalSection(&threads_mutex);
183 mono_g_hash_table_remove (threads, GUINT_TO_POINTER(tid));
185 LeaveCriticalSection(&threads_mutex);
187 /* Don't close the handle here, wait for the object finalizer
188 * to do it. Otherwise, the following race condition applies:
190 * 1) Thread exits (and handle_remove() closes the handle)
192 * 2) Some other handle is reassigned the same slot
194 * 3) Another thread tries to join the first thread, and
195 * blocks waiting for the reassigned handle to be signalled
196 * (which might never happen). This is possible, because the
197 * thread calling Join() still has a reference to the first
202 static void thread_cleanup (MonoThread *thread)
204 g_assert (thread != NULL);
206 mono_release_type_locks (thread);
208 if (!mono_monitor_enter (thread->synch_lock))
211 thread->state |= ThreadState_Stopped;
212 mono_monitor_exit (thread->synch_lock);
214 mono_profiler_thread_end (thread->tid);
215 handle_remove (thread->tid);
217 mono_thread_pop_appdomain_ref ();
219 if (thread->serialized_culture_info)
220 g_free (thread->serialized_culture_info);
222 mono_gc_free_fixed (thread->culture_info);
223 mono_gc_free_fixed (thread->ui_culture_info);
225 if (mono_thread_cleanup)
226 mono_thread_cleanup (thread);
229 static guint32 WINAPI start_wrapper(void *data)
231 struct StartInfo *start_info=(struct StartInfo *)data;
232 guint32 (*start_func)(void *);
235 MonoThread *thread=start_info->obj;
236 MonoObject *start_delegate = start_info->delegate;
238 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Start wrapper", GetCurrentThreadId ()));
240 /* We can be sure start_info->obj->tid and
241 * start_info->obj->handle have been set, because the thread
242 * was created suspended, and these values were set before the
248 SET_CURRENT_OBJECT (thread);
250 if (!mono_domain_set (start_info->domain, FALSE)) {
251 /* No point in raising an appdomain_unloaded exception here */
252 /* FIXME: Cleanup here */
256 start_func = start_info->func;
257 start_arg = start_info->start_arg;
259 /* This MUST be called before any managed code can be
260 * executed, as it calls the callback function that (for the
261 * jit) sets the lmf marker.
263 mono_thread_new_init (tid, &tid, start_func);
264 thread->stack_ptr = &tid;
266 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
267 ": (%d,%d) Setting thread stack to %p",
268 GetCurrentThreadId (), getpid (), thread->stack_ptr));
270 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
271 ": (%d) Setting current_object_key to %p",
272 GetCurrentThreadId (), thread));
274 mono_profiler_thread_start (tid);
276 if(thread->start_notify!=NULL) {
277 /* Let the thread that called Start() know we're
280 ReleaseSemaphore (thread->start_notify, 1, NULL);
285 /* Every thread references the appdomain which created it */
286 mono_thread_push_appdomain_ref (mono_domain_get ());
288 thread_adjust_static_data (thread);
290 g_message (G_GNUC_PRETTY_FUNCTION "start_wrapper for %d\n", thread->tid);
293 /* start_func is set only for unamanged start functions */
295 start_func (start_arg);
298 g_assert (start_delegate != NULL);
299 args [0] = start_arg;
300 /* we may want to handle the exception here. See comment below on unhandled exceptions */
301 mono_runtime_delegate_invoke (start_delegate, args, NULL);
304 /* If the thread calls ExitThread at all, this remaining code
305 * will not be executed, but the main thread will eventually
306 * call thread_cleanup() on this thread's behalf.
309 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Start wrapper terminating",
310 GetCurrentThreadId ()));
312 /* Remove the reference to the thread object in the TLS data,
313 * so the thread object can be finalized. This won't be
314 * reached if the thread threw an uncaught exception, so those
315 * thread handles will stay referenced :-( (This is due to
316 * missing support for scanning thread-specific data in the
317 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
320 SET_CURRENT_OBJECT (NULL);
322 thread_cleanup (thread);
327 void mono_thread_new_init (guint32 tid, gpointer stack_start, gpointer func)
329 if (mono_thread_start_cb) {
330 mono_thread_start_cb (tid, stack_start, func);
333 if (mono_thread_callbacks)
334 (* mono_thread_callbacks->thread_created) (tid, stack_start, func);
337 void mono_threads_set_default_stacksize (guint32 stacksize)
339 default_stacksize = stacksize;
342 guint32 mono_threads_get_default_stacksize (void)
344 return default_stacksize;
347 void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
350 HANDLE thread_handle;
351 struct StartInfo *start_info;
354 thread=(MonoThread *)mono_object_new (domain,
355 mono_defaults.thread_class);
357 start_info=g_new0 (struct StartInfo, 1);
358 start_info->func = func;
359 start_info->obj = thread;
360 start_info->domain = domain;
361 start_info->start_arg = arg;
363 /* Create suspended, so we can do some housekeeping before the thread
366 thread_handle = CreateThread(NULL, default_stacksize_for_thread (thread), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
367 CREATE_SUSPENDED, &tid);
368 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Started thread ID %d (handle %p)",
369 tid, thread_handle));
370 if (thread_handle == NULL) {
371 /* The thread couldn't be created, so throw an exception */
372 mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
376 thread->handle=thread_handle;
379 thread->synch_lock=mono_object_new (domain, mono_defaults.object_class);
381 handle_store(thread);
383 ResumeThread (thread_handle);
387 mono_thread_attach (MonoDomain *domain)
390 HANDLE thread_handle;
393 if ((thread = mono_thread_current ())) {
394 /* Already attached */
398 if (!mono_gc_is_gc_thread ()) {
399 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 ());
402 thread = (MonoThread *)mono_object_new (domain,
403 mono_defaults.thread_class);
405 thread_handle = GetCurrentThread ();
406 g_assert (thread_handle);
408 tid=GetCurrentThreadId ();
410 #ifdef PLATFORM_WIN32
412 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
413 * refer to the thread from other threads for things like aborting.
415 DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
416 THREAD_ALL_ACCESS, TRUE, 0);
419 thread->handle=thread_handle;
421 thread->synch_lock=mono_object_new (domain, mono_defaults.object_class);
423 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Attached thread ID %d (handle %p)",
424 tid, thread_handle));
426 handle_store(thread);
428 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Setting current_object_key to %p",
429 GetCurrentThreadId (), thread));
431 SET_CURRENT_OBJECT (thread);
432 mono_domain_set (domain, TRUE);
434 thread_adjust_static_data (thread);
436 if (mono_thread_attach_cb) {
437 mono_thread_attach_cb (tid, &tid);
444 mono_thread_detach (MonoThread *thread)
446 g_return_if_fail (thread != NULL);
448 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION "mono_thread_detach for %d\n", thread->tid));
449 SET_CURRENT_OBJECT (NULL);
451 thread_cleanup (thread);
457 MonoThread *thread = mono_thread_current ();
459 SET_CURRENT_OBJECT (NULL);
460 thread_cleanup (thread);
462 /* we could add a callback here for embedders to use. */
463 if (thread == mono_thread_get_main ())
464 exit (mono_environment_exitcode_get ());
468 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
471 guint32 (*start_func)(void *);
472 struct StartInfo *start_info;
479 THREAD_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION
480 ": Trying to start a new thread: this (%p) start (%p)", this, start));
482 mono_monitor_enter (this->synch_lock);
484 if ((this->state & ThreadState_Unstarted) == 0) {
485 mono_monitor_exit (this->synch_lock);
486 mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
490 if ((this->state & ThreadState_Aborted) != 0) {
491 mono_monitor_exit (this->synch_lock);
494 /* FIXME: remove the code inside BROKEN_THREAD_START once martin gets rid of the
495 * thread_start_compile_func stuff.
497 #define BROKEN_THREAD_START
498 #ifdef BROKEN_THREAD_START
499 im = mono_get_delegate_invoke (start->vtable->klass);
500 im = mono_marshal_get_delegate_invoke (im);
501 if (mono_thread_callbacks)
502 start_func = (* mono_thread_callbacks->thread_start_compile_func) (im);
504 start_func = mono_compile_method (im);
506 if(start_func==NULL) {
507 mono_monitor_exit (this->synch_lock);
508 g_warning(G_GNUC_PRETTY_FUNCTION
509 ": Can't locate start method!");
516 /* This is freed in start_wrapper */
517 start_info = g_new0 (struct StartInfo, 1);
518 start_info->func = start_func;
519 #ifdef BROKEN_THREAD_START
520 start_info->start_arg = start;
522 start_info->start_arg = this->start_obj;
524 start_info->delegate = start;
525 start_info->obj = this;
526 start_info->domain = mono_domain_get ();
528 this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
529 if(this->start_notify==NULL) {
530 mono_monitor_exit (this->synch_lock);
531 g_warning (G_GNUC_PRETTY_FUNCTION ": CreateSemaphore error 0x%x", GetLastError ());
535 thread=CreateThread(NULL, default_stacksize_for_thread (this), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
536 CREATE_SUSPENDED, &tid);
538 mono_monitor_exit (this->synch_lock);
539 g_warning(G_GNUC_PRETTY_FUNCTION
540 ": CreateThread error 0x%x", GetLastError());
547 /* Don't call handle_store() here, delay it to Start.
548 * We can't join a thread (trying to will just block
549 * forever) until it actually starts running, so don't
550 * store the handle till then.
553 mono_thread_start (this);
555 this->state &= ~ThreadState_Unstarted;
557 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
558 ": Started thread ID %d (handle %p)", tid, thread));
560 mono_monitor_exit (this->synch_lock);
565 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
570 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Closing thread %p, handle %p",
573 CloseHandle (thread);
576 static void mono_thread_start (MonoThread *thread)
580 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Launching thread %p (%d)",
581 GetCurrentThreadId (), thread, thread->tid));
583 /* Only store the handle when the thread is about to be
584 * launched, to avoid the main thread deadlocking while trying
585 * to clean up a thread that will never be signalled.
587 handle_store (thread);
589 if (mono_thread_callbacks)
590 (* mono_thread_callbacks->start_resume) (thread->tid);
592 ResumeThread (thread->handle);
594 if (mono_thread_callbacks)
595 (* mono_thread_callbacks->end_resume) (thread->tid);
597 if(thread->start_notify!=NULL) {
598 /* Wait for the thread to set up its TLS data etc, so
599 * theres no potential race condition if someone tries
600 * to look up the data believing the thread has
604 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
605 ": (%d) waiting for thread %p (%d) to start",
606 GetCurrentThreadId (), thread, thread->tid));
608 WaitForSingleObjectEx (thread->start_notify, INFINITE, FALSE);
609 CloseHandle (thread->start_notify);
610 thread->start_notify = NULL;
613 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
614 ": (%d) Done launching thread %p (%d)",
615 GetCurrentThreadId (), thread, thread->tid));
618 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
620 MonoThread *thread = mono_thread_current ();
624 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms));
626 mono_monitor_enter (thread->synch_lock);
627 thread->state |= ThreadState_WaitSleepJoin;
628 mono_monitor_exit (thread->synch_lock);
632 mono_monitor_enter (thread->synch_lock);
633 thread->state &= ~ThreadState_WaitSleepJoin;
634 mono_monitor_exit (thread->synch_lock);
638 ves_icall_System_Threading_Thread_GetDomainID (void)
642 return mono_domain_get()->domain_id;
646 ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
649 mono_monitor_enter (this_obj->synch_lock);
654 str = mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
656 mono_monitor_exit (this_obj->synch_lock);
661 ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
663 mono_monitor_enter (this_obj->synch_lock);
665 if (this_obj->name) {
666 mono_monitor_exit (this_obj->synch_lock);
667 mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
671 this_obj->name = g_new (gunichar2, mono_string_length (name));
672 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
673 this_obj->name_len = mono_string_length (name);
676 this_obj->name = NULL;
678 mono_monitor_exit (this_obj->synch_lock);
682 ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoThread *this)
688 /* No need to lock here */
689 if (this->culture_info) {
690 domain = mono_domain_get ();
691 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
692 res = this->culture_info [i];
693 if (res && res->vtable->domain == domain)
702 ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread *this)
706 mono_monitor_enter (this->synch_lock);
707 if (this->serialized_culture_info) {
708 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_culture_info_len);
709 memcpy (mono_array_addr (res, guint8, 0), this->serialized_culture_info, this->serialized_culture_info_len);
713 mono_monitor_exit (this->synch_lock);
719 ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this, MonoObject *culture)
722 MonoDomain *domain = mono_domain_get ();
724 mono_monitor_enter (this->synch_lock);
725 if (!this->culture_info) {
726 this->culture_info = mono_gc_alloc_fixed (sizeof (MonoObject*) * NUM_CACHED_CULTURES, NULL);
729 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
730 if (this->culture_info [i]) {
731 if (this->culture_info [i]->vtable->domain == domain)
739 if (i < NUM_CACHED_CULTURES)
740 this->culture_info [i] = culture;
741 mono_monitor_exit (this->synch_lock);
745 ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread *this, MonoArray *arr)
747 mono_monitor_enter (this->synch_lock);
748 if (this->serialized_culture_info)
749 g_free (this->serialized_culture_info);
750 this->serialized_culture_info = g_new0 (guint8, mono_array_length (arr));
751 this->serialized_culture_info_len = mono_array_length (arr);
752 memcpy (this->serialized_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
753 mono_monitor_exit (this->synch_lock);
758 ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoThread *this)
764 /* No need to lock here */
765 if (this->ui_culture_info) {
766 domain = mono_domain_get ();
767 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
768 res = this->ui_culture_info [i];
769 if (res && res->vtable->domain == domain)
778 ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread *this)
782 mono_monitor_enter (this->synch_lock);
783 if (this->serialized_ui_culture_info) {
784 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_ui_culture_info_len);
785 memcpy (mono_array_addr (res, guint8, 0), this->serialized_ui_culture_info, this->serialized_ui_culture_info_len);
789 mono_monitor_exit (this->synch_lock);
795 ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this, MonoObject *culture)
798 MonoDomain *domain = mono_domain_get ();
800 mono_monitor_enter (this->synch_lock);
801 if (!this->ui_culture_info) {
802 this->ui_culture_info = mono_gc_alloc_fixed (sizeof (MonoObject*) * NUM_CACHED_CULTURES, NULL);
805 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
806 if (this->ui_culture_info [i]) {
807 if (this->ui_culture_info [i]->vtable->domain == domain)
815 if (i < NUM_CACHED_CULTURES)
816 this->ui_culture_info [i] = culture;
817 mono_monitor_exit (this->synch_lock);
821 ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread *this, MonoArray *arr)
823 mono_monitor_enter (this->synch_lock);
824 if (this->serialized_ui_culture_info)
825 g_free (this->serialized_ui_culture_info);
826 this->serialized_ui_culture_info = g_new0 (guint8, mono_array_length (arr));
827 this->serialized_ui_culture_info_len = mono_array_length (arr);
828 memcpy (this->serialized_ui_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
829 mono_monitor_exit (this->synch_lock);
832 /* the jit may read the compiled code of this function */
834 mono_thread_current (void)
836 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", GET_CURRENT_OBJECT ()));
837 return GET_CURRENT_OBJECT ();
840 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
841 int ms, HANDLE thread)
847 mono_monitor_enter (this->synch_lock);
849 if ((this->state & ThreadState_Unstarted) != 0) {
850 mono_monitor_exit (this->synch_lock);
851 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
855 this->state |= ThreadState_WaitSleepJoin;
856 mono_monitor_exit (this->synch_lock);
861 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": joining thread handle %p, %d ms",
864 ret=WaitForSingleObjectEx (thread, ms, TRUE);
866 mono_monitor_enter (this->synch_lock);
867 this->state &= ~ThreadState_WaitSleepJoin;
868 mono_monitor_exit (this->synch_lock);
870 if(ret==WAIT_OBJECT_0) {
871 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": join successful"));
876 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": join failed"));
881 /* FIXME: exitContext isnt documented */
882 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
888 MonoObject *waitHandle;
890 MonoThread *thread = mono_thread_current ();
894 numhandles = mono_array_length(mono_handles);
895 handles = g_new0(HANDLE, numhandles);
897 if (wait_handle_os_handle_field == 0) {
898 /* Get the field os_handle which will contain the actual handle */
899 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
900 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
903 for(i = 0; i < numhandles; i++) {
904 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
905 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
912 mono_monitor_enter (thread->synch_lock);
913 thread->state |= ThreadState_WaitSleepJoin;
914 mono_monitor_exit (thread->synch_lock);
916 ret=WaitForMultipleObjectsEx(numhandles, handles, TRUE, ms, TRUE);
918 mono_monitor_enter (thread->synch_lock);
919 thread->state &= ~ThreadState_WaitSleepJoin;
920 mono_monitor_exit (thread->synch_lock);
924 if(ret==WAIT_FAILED) {
925 THREAD_WAIT_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
926 GetCurrentThreadId ()));
928 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
929 /* Do we want to try again if we get
930 * WAIT_IO_COMPLETION? The documentation for
931 * WaitHandle doesn't give any clues. (We'd have to
932 * fiddle with the timeout if we retry.)
934 THREAD_WAIT_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Wait timed out",
935 GetCurrentThreadId ()));
942 /* FIXME: exitContext isnt documented */
943 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
949 MonoObject *waitHandle;
951 MonoThread *thread = mono_thread_current ();
955 numhandles = mono_array_length(mono_handles);
956 handles = g_new0(HANDLE, numhandles);
958 if (wait_handle_os_handle_field == 0) {
959 /* Get the field os_handle which will contain the actual handle */
960 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
961 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
964 for(i = 0; i < numhandles; i++) {
965 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
966 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
973 mono_monitor_enter (thread->synch_lock);
974 thread->state |= ThreadState_WaitSleepJoin;
975 mono_monitor_exit (thread->synch_lock);
977 ret=WaitForMultipleObjectsEx(numhandles, handles, FALSE, ms, TRUE);
979 mono_monitor_enter (thread->synch_lock);
980 thread->state &= ~ThreadState_WaitSleepJoin;
981 mono_monitor_exit (thread->synch_lock);
985 THREAD_WAIT_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) returning %d",
986 GetCurrentThreadId (), ret));
989 * These need to be here. See MSDN dos on WaitForMultipleObjects.
991 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
992 return ret - WAIT_OBJECT_0;
994 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
995 return ret - WAIT_ABANDONED_0;
1002 /* FIXME: exitContext isnt documented */
1003 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
1006 MonoThread *thread = mono_thread_current ();
1008 MONO_ARCH_SAVE_REGS;
1010 THREAD_WAIT_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) waiting for %p, %d ms",
1011 GetCurrentThreadId (), handle, ms));
1017 mono_monitor_enter (thread->synch_lock);
1018 thread->state |= ThreadState_WaitSleepJoin;
1019 mono_monitor_exit (thread->synch_lock);
1021 ret=WaitForSingleObjectEx (handle, ms, TRUE);
1023 mono_monitor_enter (thread->synch_lock);
1024 thread->state &= ~ThreadState_WaitSleepJoin;
1025 mono_monitor_exit (thread->synch_lock);
1027 if(ret==WAIT_FAILED) {
1028 THREAD_WAIT_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
1029 GetCurrentThreadId ()));
1031 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
1032 /* Do we want to try again if we get
1033 * WAIT_IO_COMPLETION? The documentation for
1034 * WaitHandle doesn't give any clues. (We'd have to
1035 * fiddle with the timeout if we retry.)
1037 THREAD_WAIT_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Wait timed out",
1038 GetCurrentThreadId ()));
1045 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
1049 MONO_ARCH_SAVE_REGS;
1054 mutex = CreateMutex (NULL, owned, NULL);
1056 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
1058 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1066 void ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
1067 MONO_ARCH_SAVE_REGS;
1069 ReleaseMutex(handle);
1072 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name) {
1073 MONO_ARCH_SAVE_REGS;
1075 return(CreateEvent (NULL, manual, initial,
1076 name==NULL?NULL:mono_string_chars (name)));
1079 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
1080 MONO_ARCH_SAVE_REGS;
1082 return (SetEvent(handle));
1085 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
1086 MONO_ARCH_SAVE_REGS;
1088 return (ResetEvent(handle));
1092 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
1093 MONO_ARCH_SAVE_REGS;
1095 CloseHandle (handle);
1098 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
1100 MONO_ARCH_SAVE_REGS;
1102 return InterlockedIncrement (location);
1105 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
1109 MONO_ARCH_SAVE_REGS;
1111 EnterCriticalSection(&interlocked_mutex);
1115 LeaveCriticalSection(&interlocked_mutex);
1121 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
1123 MONO_ARCH_SAVE_REGS;
1125 return InterlockedDecrement(location);
1128 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
1132 MONO_ARCH_SAVE_REGS;
1134 EnterCriticalSection(&interlocked_mutex);
1138 LeaveCriticalSection(&interlocked_mutex);
1143 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location, gint32 value)
1145 MONO_ARCH_SAVE_REGS;
1147 return InterlockedExchange(location, value);
1150 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location, MonoObject *value)
1152 MONO_ARCH_SAVE_REGS;
1154 return (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
1157 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location, gfloat value)
1159 IntFloatUnion val, ret;
1161 MONO_ARCH_SAVE_REGS;
1164 ret.ival = InterlockedExchange((gint32 *) location, val.ival);
1170 ves_icall_System_Threading_Interlocked_Exchange_Long (gint64 *location, gint64 value)
1172 #if SIZEOF_VOID_P == 8
1173 return (gint64) InterlockedExchangePointer((gpointer *) location, (gpointer)value);
1178 * According to MSDN, this function is only atomic with regards to the
1179 * other Interlocked functions on 32 bit platforms.
1181 EnterCriticalSection(&interlocked_mutex);
1184 LeaveCriticalSection(&interlocked_mutex);
1191 ves_icall_System_Threading_Interlocked_Exchange_Double (gdouble *location, gdouble value)
1193 #if SIZEOF_VOID_P == 8
1194 LongDoubleUnion val, ret;
1197 ret.ival = (gint64)InterlockedExchangePointer((gpointer *) location, (gpointer)val.ival);
1204 * According to MSDN, this function is only atomic with regards to the
1205 * other Interlocked functions on 32 bit platforms.
1207 EnterCriticalSection(&interlocked_mutex);
1210 LeaveCriticalSection(&interlocked_mutex);
1216 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand)
1218 MONO_ARCH_SAVE_REGS;
1220 return InterlockedCompareExchange(location, value, comparand);
1223 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location, MonoObject *value, MonoObject *comparand)
1225 MONO_ARCH_SAVE_REGS;
1227 return (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
1230 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location, gfloat value, gfloat comparand)
1232 IntFloatUnion val, ret, cmp;
1234 MONO_ARCH_SAVE_REGS;
1237 cmp.fval = comparand;
1238 ret.ival = InterlockedCompareExchange((gint32 *) location, val.ival, cmp.ival);
1244 ves_icall_System_Threading_Interlocked_CompareExchange_Double (gdouble *location, gdouble value, gdouble comparand)
1246 #if SIZEOF_VOID_P == 8
1247 LongDoubleUnion val, comp, ret;
1250 comp.fval = comparand;
1251 ret.ival = (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)val.ival, (gpointer)comp.ival);
1257 EnterCriticalSection(&interlocked_mutex);
1259 if (old == comparand)
1261 LeaveCriticalSection(&interlocked_mutex);
1268 ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64 *location, gint64 value, gint64 comparand)
1270 #if SIZEOF_VOID_P == 8
1271 return (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)value, (gpointer)comparand);
1275 EnterCriticalSection(&interlocked_mutex);
1277 if (old == comparand)
1279 LeaveCriticalSection(&interlocked_mutex);
1286 ves_icall_System_Threading_Interlocked_Add_Int (gint32 *location, gint32 value)
1288 #if SIZEOF_VOID_P == 8
1289 /* Should be implemented as a JIT intrinsic */
1290 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1295 EnterCriticalSection(&interlocked_mutex);
1297 *location = orig + value;
1298 LeaveCriticalSection(&interlocked_mutex);
1305 ves_icall_System_Threading_Interlocked_Add_Long (gint64 *location, gint64 value)
1307 #if SIZEOF_VOID_P == 8
1308 /* Should be implemented as a JIT intrinsic */
1309 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1314 EnterCriticalSection(&interlocked_mutex);
1316 *location = orig + value;
1317 LeaveCriticalSection(&interlocked_mutex);
1324 ves_icall_System_Threading_Interlocked_Read_Long (gint64 *location)
1326 #if SIZEOF_VOID_P == 8
1327 /* 64 bit reads are already atomic */
1332 EnterCriticalSection(&interlocked_mutex);
1334 LeaveCriticalSection(&interlocked_mutex);
1341 ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
1343 mono_monitor_enter (this->synch_lock);
1344 this->state &= ~state;
1345 if (state & ThreadState_Background) {
1346 /* If the thread changes the background mode, the main thread has to
1347 * be notified, since it has to rebuild the list of threads to
1350 SetEvent (background_change_event);
1352 mono_monitor_exit (this->synch_lock);
1356 ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
1358 mono_monitor_enter (this->synch_lock);
1359 this->state |= state;
1360 if (state & ThreadState_Background) {
1361 /* If the thread changes the background mode, the main thread has to
1362 * be notified, since it has to rebuild the list of threads to
1365 SetEvent (background_change_event);
1367 mono_monitor_exit (this->synch_lock);
1371 ves_icall_System_Threading_Thread_GetState (MonoThread* this)
1374 mono_monitor_enter (this->synch_lock);
1375 state = this->state;
1376 mono_monitor_exit (this->synch_lock);
1381 mono_thread_get_abort_signal (void)
1383 #if defined (__MINGW32__) || defined (_MSC_VER)
1391 #endif /*defined (__MINGW32__) || defined (_MSC_VER) */
1394 #if defined (__MINGW32__) || defined (_MSC_VER)
1395 static void CALLBACK interruption_request_apc (ULONG_PTR param)
1397 MonoException* exc = mono_thread_request_interruption (FALSE);
1398 if (exc) mono_raise_exception (exc);
1400 #endif /* defined (__MINGW32__) || defined (_MSC_VER) */
1403 * signal_thread_state_change
1405 * Tells the thread that his state has changed and it has to enter the new
1406 * state as soon as possible.
1408 static void signal_thread_state_change (MonoThread *thread)
1410 if (thread == mono_thread_current ()) {
1411 /* Do it synchronously */
1412 MonoException *exc = mono_thread_request_interruption (FALSE);
1414 mono_raise_exception (exc);
1417 #if defined (__MINGW32__) || defined (_MSC_VER)
1418 QueueUserAPC ((PAPCFUNC)interruption_request_apc, thread->handle, NULL);
1420 /* fixme: store the state somewhere */
1421 #ifdef PTHREAD_POINTER_ID
1422 pthread_kill (GUINT_TO_POINTER(thread->tid), mono_thread_get_abort_signal ());
1424 pthread_kill (thread->tid, mono_thread_get_abort_signal ());
1426 #endif /* defined (__MINGW32__) || defined (__MSC_VER) */
1430 ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
1432 MONO_ARCH_SAVE_REGS;
1434 mono_monitor_enter (thread->synch_lock);
1436 if ((thread->state & ThreadState_AbortRequested) != 0 ||
1437 (thread->state & ThreadState_StopRequested) != 0)
1439 mono_monitor_exit (thread->synch_lock);
1443 if ((thread->state & ThreadState_Unstarted) != 0) {
1444 thread->state |= ThreadState_Aborted;
1445 mono_monitor_exit (thread->synch_lock);
1449 thread->state |= ThreadState_AbortRequested;
1450 thread->abort_state = state;
1451 thread->abort_exc = NULL;
1453 mono_monitor_exit (thread->synch_lock);
1455 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
1456 ": (%d) Abort requested for %p (%d)", GetCurrentThreadId (),
1457 thread, thread->tid));
1459 /* Make sure the thread is awake */
1460 mono_thread_resume (thread);
1462 signal_thread_state_change (thread);
1466 ves_icall_System_Threading_Thread_ResetAbort (void)
1468 MonoThread *thread = mono_thread_current ();
1470 MONO_ARCH_SAVE_REGS;
1472 mono_monitor_enter (thread->synch_lock);
1474 thread->state &= ~ThreadState_AbortRequested;
1476 if (!thread->abort_exc) {
1477 const char *msg = "Unable to reset abort because no abort was requested";
1478 mono_monitor_exit (thread->synch_lock);
1479 mono_raise_exception (mono_get_exception_thread_state (msg));
1481 thread->abort_exc = NULL;
1482 thread->abort_state = NULL;
1485 mono_monitor_exit (thread->synch_lock);
1489 mono_thread_suspend (MonoThread *thread)
1491 MONO_ARCH_SAVE_REGS;
1493 mono_monitor_enter (thread->synch_lock);
1495 if ((thread->state & ThreadState_Unstarted) != 0 ||
1496 (thread->state & ThreadState_Aborted) != 0 ||
1497 (thread->state & ThreadState_Stopped) != 0)
1499 mono_monitor_exit (thread->synch_lock);
1503 if ((thread->state & ThreadState_Suspended) != 0 ||
1504 (thread->state & ThreadState_SuspendRequested) != 0 ||
1505 (thread->state & ThreadState_StopRequested) != 0)
1507 mono_monitor_exit (thread->synch_lock);
1511 thread->state |= ThreadState_SuspendRequested;
1512 mono_monitor_exit (thread->synch_lock);
1514 signal_thread_state_change (thread);
1519 ves_icall_System_Threading_Thread_Suspend (MonoThread *thread)
1521 if (!mono_thread_suspend (thread))
1522 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1526 mono_thread_resume (MonoThread *thread)
1528 MONO_ARCH_SAVE_REGS;
1530 mono_monitor_enter (thread->synch_lock);
1532 if ((thread->state & ThreadState_SuspendRequested) != 0) {
1533 thread->state &= ~ThreadState_SuspendRequested;
1534 mono_monitor_exit (thread->synch_lock);
1538 if ((thread->state & ThreadState_Suspended) == 0 ||
1539 (thread->state & ThreadState_Unstarted) != 0 ||
1540 (thread->state & ThreadState_Aborted) != 0 ||
1541 (thread->state & ThreadState_Stopped) != 0)
1543 mono_monitor_exit (thread->synch_lock);
1547 thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1549 /* Awake the thread */
1550 SetEvent (thread->suspend_event);
1552 mono_monitor_exit (thread->synch_lock);
1554 /* Wait for the thread to awake */
1555 WaitForSingleObject (thread->resume_event, INFINITE);
1556 CloseHandle (thread->resume_event);
1557 thread->resume_event = NULL;
1563 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
1565 if (!mono_thread_resume (thread))
1566 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1570 find_wrapper (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1575 if (m->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1576 m->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
1577 m->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH)
1579 *((gboolean*)data) = TRUE;
1586 is_running_protected_wrapper (void)
1588 gboolean found = FALSE;
1589 mono_stack_walk (find_wrapper, &found);
1593 void mono_thread_stop (MonoThread *thread)
1595 mono_monitor_enter (thread->synch_lock);
1597 if ((thread->state & ThreadState_StopRequested) != 0 ||
1598 (thread->state & ThreadState_Stopped) != 0)
1600 mono_monitor_exit (thread->synch_lock);
1604 /* Make sure the thread is awake */
1605 mono_thread_resume (thread);
1607 thread->state |= ThreadState_StopRequested;
1608 thread->state &= ~ThreadState_AbortRequested;
1610 mono_monitor_exit (thread->synch_lock);
1612 signal_thread_state_change (thread);
1616 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
1618 return *((volatile gint8 *) (ptr));
1622 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
1624 return *((volatile gint16 *) (ptr));
1628 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
1630 return *((volatile gint32 *) (ptr));
1634 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
1636 return *((volatile gint64 *) (ptr));
1640 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
1642 return (void *) *((volatile void **) ptr);
1646 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
1648 *((volatile gint8 *) ptr) = value;
1652 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
1654 *((volatile gint16 *) ptr) = value;
1658 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
1660 *((volatile gint32 *) ptr) = value;
1664 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
1666 *((volatile gint64 *) ptr) = value;
1670 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
1672 *((volatile void **) ptr) = value;
1675 void mono_thread_init (MonoThreadStartCB start_cb,
1676 MonoThreadAttachCB attach_cb)
1678 InitializeCriticalSection(&threads_mutex);
1679 InitializeCriticalSection(&interlocked_mutex);
1680 InitializeCriticalSection(&contexts_mutex);
1681 background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1683 mono_init_static_data_info (&thread_static_info);
1684 mono_init_static_data_info (&context_static_info);
1686 current_object_key=TlsAlloc();
1687 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Allocated current_object_key %d",
1688 current_object_key));
1690 mono_thread_start_cb = start_cb;
1691 mono_thread_attach_cb = attach_cb;
1693 /* Get a pseudo handle to the current process. This is just a
1694 * kludge so that wapi can build a process handle if needed.
1695 * As a pseudo handle is returned, we don't need to clean
1698 GetCurrentProcess ();
1702 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
1704 mono_thread_cleanup = func;
1707 void mono_install_thread_callbacks (MonoThreadCallbacks *callbacks)
1709 mono_thread_callbacks = callbacks;
1713 static void print_tids (gpointer key, gpointer value, gpointer user)
1715 g_message ("Waiting for: %d", GPOINTER_TO_UINT(key));
1720 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1721 MonoThread *threads[MAXIMUM_WAIT_OBJECTS];
1725 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
1729 THREAD_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION
1730 ": %d threads to wait for in this batch", wait->num));
1732 ret=WaitForMultipleObjectsEx(wait->num, wait->handles, TRUE, timeout, FALSE);
1734 if(ret==WAIT_FAILED) {
1735 /* See the comment in build_wait_tids() */
1736 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Wait failed"));
1740 for(i=0; i<wait->num; i++)
1741 CloseHandle (wait->handles[i]);
1743 if (ret == WAIT_TIMEOUT)
1746 for(i=0; i<wait->num; i++) {
1747 guint32 tid=wait->threads[i]->tid;
1749 if(mono_g_hash_table_lookup (threads, GUINT_TO_POINTER(tid))!=NULL) {
1750 /* This thread must have been killed, because
1751 * it hasn't cleaned itself up. (It's just
1752 * possible that the thread exited before the
1753 * parent thread had a chance to store the
1754 * handle, and now there is another pointer to
1755 * the already-exited thread stored. In this
1756 * case, we'll just get two
1757 * mono_profiler_thread_end() calls for the
1761 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
1762 ": cleaning up after thread %d", tid));
1763 thread_cleanup (wait->threads[i]);
1768 static void wait_for_tids_or_state_change (struct wait_data *wait, guint32 timeout)
1770 guint32 i, ret, count;
1772 THREAD_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION
1773 ": %d threads to wait for in this batch", wait->num));
1775 /* Add the thread state change event, so it wakes up if a thread changes
1776 * to background mode.
1779 if (count < MAXIMUM_WAIT_OBJECTS) {
1780 wait->handles [count] = background_change_event;
1784 ret=WaitForMultipleObjectsEx (count, wait->handles, FALSE, timeout, FALSE);
1786 if(ret==WAIT_FAILED) {
1787 /* See the comment in build_wait_tids() */
1788 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Wait failed"));
1792 for(i=0; i<wait->num; i++)
1793 CloseHandle (wait->handles[i]);
1795 if (ret == WAIT_TIMEOUT)
1798 if (ret < wait->num) {
1799 guint32 tid=wait->threads[ret]->tid;
1800 EnterCriticalSection (&threads_mutex);
1801 if (mono_g_hash_table_lookup (threads, GUINT_TO_POINTER(tid))!=NULL) {
1802 /* See comment in wait_for_tids about thread cleanup */
1803 LeaveCriticalSection (&threads_mutex);
1804 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
1805 ": cleaning up after thread %d", tid));
1806 thread_cleanup (wait->threads[i]);
1809 LeaveCriticalSection (&threads_mutex);
1813 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
1815 struct wait_data *wait=(struct wait_data *)user;
1817 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
1819 MonoThread *thread=(MonoThread *)value;
1821 /* Ignore background threads, we abort them later */
1822 mono_monitor_enter (thread->synch_lock);
1823 if (thread->state & ThreadState_Background) {
1824 mono_monitor_exit (thread->synch_lock);
1825 return; /* just leave, ignore */
1827 mono_monitor_exit (thread->synch_lock);
1829 if (mono_gc_is_finalizer_thread (thread))
1832 if (thread == mono_thread_current ())
1835 if (thread == mono_thread_get_main ())
1838 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1842 wait->handles[wait->num]=handle;
1843 wait->threads[wait->num]=thread;
1846 /* Just ignore the rest, we can't do anything with
1853 remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
1855 struct wait_data *wait=(struct wait_data *)user;
1856 guint32 self = GetCurrentThreadId ();
1857 MonoThread *thread = (MonoThread *) value;
1860 /* The finalizer thread is not a background thread */
1861 if (thread->tid != self && (thread->state & ThreadState_Background) != 0) {
1863 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1867 if(thread->state & ThreadState_AbortRequested ||
1868 thread->state & ThreadState_Aborted) {
1869 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Thread id %d already aborting", thread->tid));
1873 wait->handles[wait->num]=thread->handle;
1874 wait->threads[wait->num]=thread;
1877 THREAD_DEBUG (g_print (G_GNUC_PRETTY_FUNCTION ": Aborting id: %d\n", thread->tid));
1878 mono_thread_stop (thread);
1882 return (thread->tid != self && !mono_gc_is_finalizer_thread (thread));
1885 void mono_thread_manage (void)
1887 struct wait_data *wait=g_new0 (struct wait_data, 1);
1889 /* join each thread that's still running */
1890 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Joining each running thread..."));
1892 EnterCriticalSection (&threads_mutex);
1894 THREAD_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION ": No threads"));
1895 LeaveCriticalSection (&threads_mutex);
1898 LeaveCriticalSection (&threads_mutex);
1901 EnterCriticalSection (&threads_mutex);
1902 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
1903 ":There are %d threads to join", mono_g_hash_table_size (threads));
1904 mono_g_hash_table_foreach (threads, print_tids, NULL));
1906 ResetEvent (background_change_event);
1908 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
1909 LeaveCriticalSection (&threads_mutex);
1911 /* Something to wait for */
1912 wait_for_tids_or_state_change (wait, INFINITE);
1914 THREAD_DEBUG (g_message ("I have %d threads after waiting.", wait->num));
1915 } while(wait->num>0);
1917 mono_runtime_set_shutting_down ();
1919 THREAD_DEBUG (g_message ("threadpool cleanup"));
1920 mono_thread_pool_cleanup ();
1922 EnterCriticalSection(&threads_mutex);
1925 * Remove everything but the finalizer thread and self.
1926 * Also abort all the background threads
1929 mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
1931 LeaveCriticalSection(&threads_mutex);
1933 THREAD_DEBUG (g_message ("wait->num is now %d", wait->num));
1935 /* Something to wait for */
1936 wait_for_tids (wait, INFINITE);
1939 * give the subthreads a chance to really quit (this is mainly needed
1940 * to get correct user and system times from getrusage/wait/time(1)).
1941 * This could be removed if we avoid pthread_detach() and use pthread_join().
1943 #ifndef PLATFORM_WIN32
1950 static void terminate_thread (gpointer key, gpointer value, gpointer user)
1952 MonoThread *thread=(MonoThread *)value;
1953 guint32 self=GPOINTER_TO_UINT (user);
1955 if(thread->tid!=self) {
1956 /*TerminateThread (thread->handle, -1);*/
1960 void mono_thread_abort_all_other_threads (void)
1962 guint32 self=GetCurrentThreadId ();
1964 EnterCriticalSection (&threads_mutex);
1965 THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ":There are %d threads to abort",
1966 mono_g_hash_table_size (threads));
1967 mono_g_hash_table_foreach (threads, print_tids, NULL));
1969 mono_g_hash_table_foreach (threads, terminate_thread,
1970 GUINT_TO_POINTER (self));
1972 LeaveCriticalSection (&threads_mutex);
1976 collect_threads (gpointer key, gpointer value, gpointer user_data)
1978 MonoThread *thread = (MonoThread*)value;
1979 struct wait_data *wait = (struct wait_data*)user_data;
1982 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
1983 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1987 wait->handles [wait->num] = handle;
1988 wait->threads [wait->num] = thread;
1994 * mono_thread_suspend_all_other_threads:
1996 * Suspend all managed threads except the finalizer thread and this thread.
1998 void mono_thread_suspend_all_other_threads (void)
2000 struct wait_data *wait = g_new0 (struct wait_data, 1);
2002 guint32 self = GetCurrentThreadId ();
2004 guint32 eventidx = 0;
2007 * Make a copy of the hashtable since we can't do anything with
2008 * threads while threads_mutex is held.
2010 EnterCriticalSection (&threads_mutex);
2011 mono_g_hash_table_foreach (threads, collect_threads, wait);
2012 LeaveCriticalSection (&threads_mutex);
2014 events = g_new0 (gpointer, wait->num);
2016 /* Get the suspended events that we'll be waiting for */
2017 for (i = 0; i < wait->num; ++i) {
2018 MonoThread *thread = wait->threads [i];
2020 if ((thread->tid == self) || mono_gc_is_finalizer_thread (thread)) {
2021 //CloseHandle (wait->handles [i]);
2022 wait->threads [i] = NULL; /* ignore this thread in next loop */
2026 mono_monitor_enter (thread->synch_lock);
2028 if ((thread->state & ThreadState_Suspended) != 0 ||
2029 (thread->state & ThreadState_SuspendRequested) != 0 ||
2030 (thread->state & ThreadState_StopRequested) != 0 ||
2031 (thread->state & ThreadState_Stopped) != 0) {
2032 mono_monitor_exit (thread->synch_lock);
2033 CloseHandle (wait->handles [i]);
2034 wait->threads [i] = NULL; /* ignore this thread in next loop */
2038 /* Convert abort requests into suspend requests */
2039 if ((thread->state & ThreadState_AbortRequested) != 0)
2040 thread->state &= ~ThreadState_AbortRequested;
2042 thread->state |= ThreadState_SuspendRequested;
2044 if (thread->suspended_event == NULL)
2045 thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2047 events [eventidx++] = thread->suspended_event;
2048 mono_monitor_exit (thread->synch_lock);
2050 /* Signal the thread to suspend */
2051 signal_thread_state_change (thread);
2054 WaitForMultipleObjectsEx (eventidx, events, TRUE, INFINITE, FALSE);
2055 for (i = 0; i < wait->num; ++i) {
2056 MonoThread *thread = wait->threads [i];
2061 mono_monitor_enter (thread->synch_lock);
2062 CloseHandle (thread->suspended_event);
2063 thread->suspended_event = NULL;
2064 mono_monitor_exit (thread->synch_lock);
2072 * mono_thread_push_appdomain_ref:
2074 * Register that the current thread may have references to objects in domain
2075 * @domain on its stack. Each call to this function should be paired with a
2076 * call to pop_appdomain_ref.
2079 mono_thread_push_appdomain_ref (MonoDomain *domain)
2081 MonoThread *thread = mono_thread_current ();
2084 /* printf ("PUSH REF: %x -> %s.\n", thread->tid, domain->friendly_name); */
2085 EnterCriticalSection (&threads_mutex);
2086 thread->appdomain_refs = g_slist_prepend (thread->appdomain_refs, domain);
2087 LeaveCriticalSection (&threads_mutex);
2092 mono_thread_pop_appdomain_ref (void)
2094 MonoThread *thread = mono_thread_current ();
2097 /* printf ("POP REF: %x -> %s.\n", thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
2098 EnterCriticalSection (&threads_mutex);
2099 /* FIXME: How can the list be empty ? */
2100 if (thread->appdomain_refs)
2101 thread->appdomain_refs = g_slist_remove (thread->appdomain_refs, thread->appdomain_refs->data);
2102 LeaveCriticalSection (&threads_mutex);
2107 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
2110 EnterCriticalSection (&threads_mutex);
2111 res = g_slist_find (thread->appdomain_refs, domain) != NULL;
2112 LeaveCriticalSection (&threads_mutex);
2116 typedef struct abort_appdomain_data {
2117 struct wait_data wait;
2119 } abort_appdomain_data;
2122 abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
2124 MonoThread *thread = (MonoThread*)value;
2125 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
2126 MonoDomain *domain = data->domain;
2128 if (mono_thread_has_appdomain_ref (thread, domain)) {
2129 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
2130 HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2134 ves_icall_System_Threading_Thread_Abort (thread, NULL);
2136 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
2137 data->wait.handles [data->wait.num] = handle;
2138 data->wait.threads [data->wait.num] = thread;
2141 /* Just ignore the rest, we can't do anything with
2149 * mono_threads_abort_appdomain_threads:
2151 * Abort threads which has references to the given appdomain.
2154 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
2156 abort_appdomain_data user_data;
2159 /* printf ("ABORT BEGIN.\n"); */
2161 start_time = GetTickCount ();
2163 EnterCriticalSection (&threads_mutex);
2165 user_data.domain = domain;
2166 user_data.wait.num = 0;
2167 mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
2168 LeaveCriticalSection (&threads_mutex);
2170 if (user_data.wait.num > 0)
2171 wait_for_tids (&user_data.wait, timeout);
2173 /* Update remaining time */
2174 timeout -= GetTickCount () - start_time;
2175 start_time = GetTickCount ();
2180 while (user_data.wait.num > 0);
2182 /* printf ("ABORT DONE.\n"); */
2188 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
2190 MonoThread *thread = (MonoThread*)value;
2191 MonoDomain *domain = (MonoDomain*)user_data;
2194 /* No locking needed here */
2196 if (thread->culture_info) {
2197 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
2198 if (thread->culture_info [i] && thread->culture_info [i]->vtable->domain == domain)
2199 thread->culture_info [i] = NULL;
2202 if (thread->ui_culture_info) {
2203 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
2204 if (thread->ui_culture_info [i] && thread->ui_culture_info [i]->vtable->domain == domain)
2205 thread->ui_culture_info [i] = NULL;
2211 * mono_threads_clear_cached_culture:
2213 * Clear the cached_current_culture from all threads if it is in the
2217 mono_threads_clear_cached_culture (MonoDomain *domain)
2219 EnterCriticalSection (&threads_mutex);
2220 mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
2221 LeaveCriticalSection (&threads_mutex);
2225 * mono_thread_get_pending_exception:
2227 * Return an exception which needs to be raised when leaving a catch clause.
2228 * This is used for undeniable exception propagation.
2231 mono_thread_get_pending_exception (void)
2233 MonoThread *thread = mono_thread_current ();
2235 MONO_ARCH_SAVE_REGS;
2237 if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
2239 * FIXME: Clear the abort exception and return an AppDomainUnloaded
2240 * exception if the thread no longer references a dying appdomain.
2242 thread->abort_exc->trace_ips = NULL;
2243 thread->abort_exc->stack_trace = NULL;
2244 return thread->abort_exc;
2250 #define NUM_STATIC_DATA_IDX 8
2251 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
2252 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
2257 * mono_alloc_static_data
2259 * Allocate memory blocks for storing threads or context static data
2262 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset)
2264 guint idx = (offset >> 24) - 1;
2267 gpointer* static_data = *static_data_ptr;
2269 static_data = mono_gc_alloc_fixed (static_data_size [0], NULL);
2270 *static_data_ptr = static_data;
2271 static_data [0] = static_data;
2274 for (i = 1; i <= idx; ++i) {
2275 if (static_data [i])
2277 static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
2282 * mono_init_static_data_info
2284 * Initializes static data counters
2286 static void mono_init_static_data_info (StaticDataInfo *static_data)
2288 static_data->idx = 0;
2289 static_data->offset = 0;
2293 * mono_alloc_static_data_slot
2295 * Generates an offset for static data. static_data contains the counters
2296 * used to generate it.
2299 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
2303 if (!static_data->idx && !static_data->offset) {
2305 * we use the first chunk of the first allocation also as
2306 * an array for the rest of the data
2308 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
2310 static_data->offset += align - 1;
2311 static_data->offset &= ~(align - 1);
2312 if (static_data->offset + size >= static_data_size [static_data->idx]) {
2313 static_data->idx ++;
2314 g_assert (size <= static_data_size [static_data->idx]);
2316 * massive unloading and reloading of domains with thread-static
2317 * data may eventually exceed the allocated storage...
2318 * Need to check what the MS runtime does in that case.
2319 * Note that for each appdomain, we need to allocate a separate
2320 * thread data slot for security reasons. We could keep track
2321 * of the slots per-domain and when the domain is unloaded
2322 * out the slots on a sort of free list.
2324 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
2325 static_data->offset = 0;
2327 offset = static_data->offset | ((static_data->idx + 1) << 24);
2328 static_data->offset += size;
2333 * ensure thread static fields already allocated are valid for thread
2334 * This function is called when a thread is created or on thread attach.
2337 thread_adjust_static_data (MonoThread *thread)
2341 EnterCriticalSection (&threads_mutex);
2342 if (thread_static_info.offset || thread_static_info.idx > 0) {
2343 /* get the current allocated size */
2344 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
2345 mono_alloc_static_data (&(thread->static_data), offset);
2347 LeaveCriticalSection (&threads_mutex);
2351 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
2353 MonoThread *thread = value;
2354 guint32 offset = GPOINTER_TO_UINT (user);
2356 mono_alloc_static_data (&(thread->static_data), offset);
2360 * The offset for a special static variable is composed of three parts:
2361 * a bit that indicates the type of static data (0:thread, 1:context),
2362 * an index in the array of chunks of memory for the thread (thread->static_data)
2363 * and an offset in that chunk of mem. This allows allocating less memory in the
2368 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align)
2371 if (static_type == SPECIAL_STATIC_THREAD)
2373 EnterCriticalSection (&threads_mutex);
2374 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
2375 /* This can be called during startup */
2376 if (threads != NULL)
2377 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
2378 LeaveCriticalSection (&threads_mutex);
2382 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
2383 EnterCriticalSection (&contexts_mutex);
2384 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
2385 LeaveCriticalSection (&contexts_mutex);
2386 offset |= 0x80000000; /* Set the high bit to indicate context static data */
2392 mono_get_special_static_data (guint32 offset)
2394 /* The high bit means either thread (0) or static (1) data. */
2396 guint32 static_type = (offset & 0x80000000);
2399 offset &= 0x7fffffff;
2400 idx = (offset >> 24) - 1;
2402 if (static_type == 0)
2404 MonoThread *thread = mono_thread_current ();
2405 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
2409 /* Allocate static data block under demand, since we don't have a list
2412 MonoAppContext *context = mono_context_get ();
2413 if (!context->static_data || !context->static_data [idx]) {
2414 EnterCriticalSection (&contexts_mutex);
2415 mono_alloc_static_data (&(context->static_data), offset);
2416 LeaveCriticalSection (&contexts_mutex);
2418 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
2422 static void gc_stop_world (gpointer key, gpointer value, gpointer user)
2424 MonoThread *thread=(MonoThread *)value;
2425 guint32 self=GPOINTER_TO_UINT (user);
2427 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d", self, thread->tid));
2429 if(thread->tid==self)
2432 SuspendThread (thread->handle);
2435 void mono_gc_stop_world (void)
2437 guint32 self=GetCurrentThreadId ();
2439 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads));
2441 EnterCriticalSection (&threads_mutex);
2443 if (threads != NULL)
2444 mono_g_hash_table_foreach (threads, gc_stop_world, GUINT_TO_POINTER (self));
2446 LeaveCriticalSection (&threads_mutex);
2449 static void gc_start_world (gpointer key, gpointer value, gpointer user)
2451 MonoThread *thread=(MonoThread *)value;
2452 guint32 self=GPOINTER_TO_UINT (user);
2454 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d", self, thread->tid));
2456 if(thread->tid==self)
2459 ResumeThread (thread->handle);
2462 void mono_gc_start_world (void)
2464 guint32 self=GetCurrentThreadId ();
2466 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads));
2468 EnterCriticalSection (&threads_mutex);
2470 if (threads != NULL)
2471 mono_g_hash_table_foreach (threads, gc_start_world, GUINT_TO_POINTER (self));
2473 LeaveCriticalSection (&threads_mutex);
2477 static CALLBACK void dummy_apc (ULONG_PTR param)
2481 static guint32 dummy_apc (gpointer param)
2488 * mono_thread_execute_interruption
2490 * Performs the operation that the requested thread state requires (abort,
2493 static MonoException* mono_thread_execute_interruption (MonoThread *thread)
2495 mono_monitor_enter (thread->synch_lock);
2497 if (thread->interruption_requested) {
2498 /* this will consume pending APC calls */
2499 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
2500 InterlockedDecrement (&thread_interruption_requested);
2501 thread->interruption_requested = FALSE;
2504 if ((thread->state & ThreadState_AbortRequested) != 0) {
2505 if (thread->abort_exc == NULL)
2506 thread->abort_exc = mono_get_exception_thread_abort ();
2507 mono_monitor_exit (thread->synch_lock);
2508 return thread->abort_exc;
2510 else if ((thread->state & ThreadState_SuspendRequested) != 0) {
2511 thread->state &= ~ThreadState_SuspendRequested;
2512 thread->state |= ThreadState_Suspended;
2513 thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2514 if (thread->suspended_event)
2515 SetEvent (thread->suspended_event);
2516 mono_monitor_exit (thread->synch_lock);
2518 WaitForSingleObject (thread->suspend_event, INFINITE);
2520 mono_monitor_enter (thread->synch_lock);
2521 CloseHandle (thread->suspend_event);
2522 thread->suspend_event = NULL;
2523 thread->state &= ~ThreadState_Suspended;
2525 /* The thread that requested the resume will have replaced this event
2526 * and will be waiting for it
2528 SetEvent (thread->resume_event);
2529 mono_monitor_exit (thread->synch_lock);
2532 else if ((thread->state & ThreadState_StopRequested) != 0) {
2533 /* FIXME: do this through the JIT? */
2534 mono_monitor_exit (thread->synch_lock);
2535 mono_thread_exit ();
2539 mono_monitor_exit (thread->synch_lock);
2544 * mono_thread_request_interruption
2546 * A signal handler can call this method to request the interruption of a
2547 * thread. The result of the interruption will depend on the current state of
2548 * the thread. If the result is an exception that needs to be throw, it is
2549 * provided as return value.
2551 MonoException* mono_thread_request_interruption (gboolean running_managed)
2553 MonoThread *thread = mono_thread_current ();
2555 /* The thread may already be stopping */
2559 mono_monitor_enter (thread->synch_lock);
2561 if (thread->interruption_requested) {
2562 mono_monitor_exit (thread->synch_lock);
2566 if (!running_managed || is_running_protected_wrapper ()) {
2567 /* Can't stop while in unmanaged code. Increase the global interruption
2568 request count. When exiting the unmanaged method the count will be
2569 checked and the thread will be interrupted. */
2571 InterlockedIncrement (&thread_interruption_requested);
2572 thread->interruption_requested = TRUE;
2573 mono_monitor_exit (thread->synch_lock);
2575 /* this will awake the thread if it is in WaitForSingleObject
2577 QueueUserAPC ((PAPCFUNC)dummy_apc, thread->handle, NULL);
2581 mono_monitor_exit (thread->synch_lock);
2582 return mono_thread_execute_interruption (thread);
2586 gboolean mono_thread_interruption_requested ()
2588 if (thread_interruption_requested) {
2589 MonoThread *thread = mono_thread_current ();
2590 /* The thread may already be stopping */
2592 return (thread->interruption_requested);
2597 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
2599 MonoThread *thread = mono_thread_current ();
2601 /* The thread may already be stopping */
2605 if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
2606 MonoException* exc = mono_thread_execute_interruption (thread);
2607 if (exc) mono_raise_exception (exc);
2612 * Performs the interruption of the current thread, if one has been requested,
2613 * and the thread is not running a protected wrapper.
2615 void mono_thread_interruption_checkpoint ()
2617 mono_thread_interruption_checkpoint_request (FALSE);
2621 * Performs the interruption of the current thread, if one has been requested.
2623 void mono_thread_force_interruption_checkpoint ()
2625 mono_thread_interruption_checkpoint_request (TRUE);
2629 * mono_thread_interruption_request_flag:
2631 * Returns the address of a flag that will be non-zero if an interruption has
2632 * been requested for a thread. The thread to interrupt may not be the current
2633 * thread, so an additional call to mono_thread_interruption_requested() or
2634 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
2637 gint32* mono_thread_interruption_request_flag ()
2639 return &thread_interruption_requested;
2642 #ifdef WITH_INCLUDED_LIBGC
2644 static void gc_push_all_stacks (gpointer key, gpointer value, gpointer user)
2646 MonoThread *thread=(MonoThread *)value;
2647 guint32 *selfp=(guint32 *)user, self = *selfp;
2649 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d - %p", self, thread->tid, thread->stack_ptr));
2651 if(thread->tid==self) {
2652 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %p - %p", selfp, thread->stack_ptr));
2653 GC_push_all_stack (selfp, thread->stack_ptr);
2657 #ifdef PLATFORM_WIN32
2658 GC_win32_push_thread_stack (thread->handle, thread->stack_ptr);
2660 mono_wapi_push_thread_stack (thread->handle, thread->stack_ptr);
2664 void mono_gc_push_all_stacks (void)
2666 guint32 self=GetCurrentThreadId ();
2668 LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads));
2670 EnterCriticalSection (&threads_mutex);
2672 if (threads != NULL)
2673 mono_g_hash_table_foreach (threads, gc_push_all_stacks, &self);
2675 LeaveCriticalSection (&threads_mutex);
2678 #endif /* WITH_INCLUDED_LIBGC */