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>
35 #include <mono/os/gc_wrapper.h>
38 #undef THREAD_WAIT_DEBUG
42 guint32 (*func)(void *);
59 /* Number of cached culture objects in the MonoThread->culture_info array */
60 #define NUM_CACHED_CULTURES 4
63 * The "os_handle" field of the WaitHandle class.
65 static MonoClassField *wait_handle_os_handle_field = NULL;
67 /* Controls access to the 'threads' hash table */
68 static CRITICAL_SECTION threads_mutex;
70 /* Controls access to context static data */
71 static CRITICAL_SECTION contexts_mutex;
73 /* Holds current status of static data heap */
74 static StaticDataInfo thread_static_info;
75 static StaticDataInfo context_static_info;
77 /* The hash of existing threads (key is thread ID) that need joining
80 static MonoGHashTable *threads=NULL;
82 /* The TLS key that holds the MonoObject assigned to each thread */
83 static guint32 current_object_key = -1;
86 /* we need to use both the Tls* functions and __thread because
87 * the gc needs to see all the threads
89 static __thread MonoThread * tls_current_object;
90 #define SET_CURRENT_OBJECT(x) do { \
91 tls_current_object = x; \
92 TlsSetValue (current_object_key, x); \
94 #define GET_CURRENT_OBJECT() tls_current_object
96 #define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x);
97 #define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key);
100 /* function called at thread start */
101 static MonoThreadStartCB mono_thread_start_cb = NULL;
103 /* function called at thread attach */
104 static MonoThreadAttachCB mono_thread_attach_cb = NULL;
106 /* function called at thread cleanup */
107 static MonoThreadCleanupFunc mono_thread_cleanup = NULL;
109 /* function called when a new thread has been created */
110 static MonoThreadCallbacks *mono_thread_callbacks = NULL;
112 /* The TLS key that holds the LocalDataStoreSlot hash in each thread */
113 static guint32 slothash_key = -1;
115 /* The default stack size for each thread */
116 static guint32 default_stacksize = 0;
117 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
119 static void thread_adjust_static_data (MonoThread *thread);
120 static void mono_init_static_data_info (StaticDataInfo *static_data);
121 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
123 /* Spin lock for InterlockedXXX 64 bit functions */
124 static CRITICAL_SECTION interlocked_mutex;
126 /* Controls access to interruption flag */
127 static CRITICAL_SECTION interruption_mutex;
129 /* global count of thread interruptions requested */
130 static gint32 thread_interruption_requested = 0;
133 mono_thread_get_tls_key (void)
135 return current_object_key;
138 /* handle_store() and handle_remove() manage the array of threads that
139 * still need to be waited for when the main thread exits.
141 static void handle_store(MonoThread *thread)
143 EnterCriticalSection(&threads_mutex);
146 g_message(G_GNUC_PRETTY_FUNCTION ": thread %p ID %d", thread,
151 MONO_GC_REGISTER_ROOT (threads);
152 threads=mono_g_hash_table_new(NULL, NULL);
155 /* We don't need to duplicate thread->handle, because it is
156 * only closed when the thread object is finalized by the GC.
158 mono_g_hash_table_insert(threads, GUINT_TO_POINTER(thread->tid), thread);
159 LeaveCriticalSection(&threads_mutex);
162 static void handle_remove(guint32 tid)
165 g_message(G_GNUC_PRETTY_FUNCTION ": thread ID %d", tid);
168 EnterCriticalSection(&threads_mutex);
171 mono_g_hash_table_remove (threads, GUINT_TO_POINTER(tid));
173 LeaveCriticalSection(&threads_mutex);
175 /* Don't close the handle here, wait for the object finalizer
176 * to do it. Otherwise, the following race condition applies:
178 * 1) Thread exits (and handle_remove() closes the handle)
180 * 2) Some other handle is reassigned the same slot
182 * 3) Another thread tries to join the first thread, and
183 * blocks waiting for the reassigned handle to be signalled
184 * (which might never happen). This is possible, because the
185 * thread calling Join() still has a reference to the first
190 static void thread_cleanup (MonoThread *thread)
192 mono_release_type_locks (thread);
194 if (!mono_monitor_enter (thread->synch_lock))
197 thread->state |= ThreadState_Stopped;
198 mono_monitor_exit (thread->synch_lock);
200 mono_profiler_thread_end (thread->tid);
201 handle_remove (thread->tid);
203 mono_thread_pop_appdomain_ref ();
205 if (thread->serialized_culture_info)
206 g_free (thread->serialized_culture_info);
208 mono_gc_free_fixed (thread->culture_info);
209 mono_gc_free_fixed (thread->ui_culture_info);
211 if (mono_thread_cleanup)
212 mono_thread_cleanup (thread);
215 static guint32 start_wrapper(void *data)
217 struct StartInfo *start_info=(struct StartInfo *)data;
218 guint32 (*start_func)(void *);
221 MonoThread *thread=start_info->obj;
222 MonoObject *start_delegate = start_info->delegate;
225 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Start wrapper",
226 GetCurrentThreadId ());
229 /* We can be sure start_info->obj->tid and
230 * start_info->obj->handle have been set, because the thread
231 * was created suspended, and these values were set before the
237 SET_CURRENT_OBJECT (thread);
239 if (!mono_domain_set (start_info->domain, FALSE)) {
240 /* No point in raising an appdomain_unloaded exception here */
241 /* FIXME: Cleanup here */
245 start_func = start_info->func;
246 start_arg = start_info->start_arg;
248 /* This MUST be called before any managed code can be
249 * executed, as it calls the callback function that (for the
250 * jit) sets the lmf marker.
252 mono_thread_new_init (tid, &tid, start_func);
253 thread->stack_ptr = &tid;
256 g_message (G_GNUC_PRETTY_FUNCTION
257 ": (%d,%d) Setting thread stack to %p",
258 GetCurrentThreadId (), getpid (), thread->stack_ptr);
262 g_message (G_GNUC_PRETTY_FUNCTION
263 ": (%d) Setting current_object_key to %p",
264 GetCurrentThreadId (), thread);
267 mono_profiler_thread_start (tid);
269 if(thread->start_notify!=NULL) {
270 /* Let the thread that called Start() know we're
273 ReleaseSemaphore (thread->start_notify, 1, NULL);
278 /* Every thread references the appdomain which created it */
279 mono_thread_push_appdomain_ref (mono_domain_get ());
281 thread_adjust_static_data (thread);
283 g_message (G_GNUC_PRETTY_FUNCTION "start_wrapper for %d\n", thread->tid);
286 /* start_func is set only for unamanged start functions */
288 start_func (start_arg);
291 g_assert (start_delegate != NULL);
292 args [0] = start_arg;
293 /* we may want to handle the exception here. See comment below on unhandled exceptions */
294 mono_runtime_delegate_invoke (start_delegate, args, NULL);
297 /* If the thread calls ExitThread at all, this remaining code
298 * will not be executed, but the main thread will eventually
299 * call thread_cleanup() on this thread's behalf.
303 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Start wrapper terminating",
304 GetCurrentThreadId ());
307 /* Remove the reference to the thread object in the TLS data,
308 * so the thread object can be finalized. This won't be
309 * reached if the thread threw an uncaught exception, so those
310 * thread handles will stay referenced :-( (This is due to
311 * missing support for scanning thread-specific data in the
312 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
315 SET_CURRENT_OBJECT (NULL);
317 thread_cleanup (thread);
322 void mono_thread_new_init (guint32 tid, gpointer stack_start, gpointer func)
324 if (mono_thread_start_cb) {
325 mono_thread_start_cb (tid, stack_start, func);
328 if (mono_thread_callbacks)
329 (* mono_thread_callbacks->thread_created) (tid, stack_start, func);
332 void mono_threads_set_default_stacksize (guint32 stacksize)
334 default_stacksize = stacksize;
337 guint32 mono_threads_get_default_stacksize (void)
339 return default_stacksize;
342 void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
345 HANDLE thread_handle;
346 struct StartInfo *start_info;
349 thread=(MonoThread *)mono_object_new (domain,
350 mono_defaults.thread_class);
352 start_info=g_new0 (struct StartInfo, 1);
353 start_info->func = func;
354 start_info->obj = thread;
355 start_info->domain = domain;
356 start_info->start_arg = arg;
358 /* Create suspended, so we can do some housekeeping before the thread
361 thread_handle = CreateThread(NULL, default_stacksize_for_thread (thread), start_wrapper, start_info,
362 CREATE_SUSPENDED, &tid);
364 g_message(G_GNUC_PRETTY_FUNCTION ": Started thread ID %d (handle %p)",
367 g_assert (thread_handle);
369 thread->handle=thread_handle;
372 thread->synch_lock=mono_object_new (domain, mono_defaults.object_class);
374 handle_store(thread);
376 ResumeThread (thread_handle);
380 mono_thread_attach (MonoDomain *domain)
383 HANDLE thread_handle;
386 if ((thread = mono_thread_current ())) {
387 /* Already attached */
391 thread = (MonoThread *)mono_object_new (domain,
392 mono_defaults.thread_class);
394 thread_handle = GetCurrentThread ();
395 g_assert (thread_handle);
397 tid=GetCurrentThreadId ();
399 #ifdef PLATFORM_WIN32
401 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
402 * refer to the thread from other threads for things like aborting.
404 DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
405 THREAD_ALL_ACCESS, TRUE, 0);
408 thread->handle=thread_handle;
410 thread->synch_lock=mono_object_new (domain, mono_defaults.object_class);
413 g_message(G_GNUC_PRETTY_FUNCTION ": Attached thread ID %d (handle %p)",
417 handle_store(thread);
420 g_message (G_GNUC_PRETTY_FUNCTION
421 ": (%d) Setting current_object_key to %p",
422 GetCurrentThreadId (), thread);
425 SET_CURRENT_OBJECT (thread);
426 mono_domain_set (domain, TRUE);
428 thread_adjust_static_data (thread);
430 if (mono_thread_attach_cb) {
431 mono_thread_attach_cb (tid, &tid);
438 mono_thread_detach (MonoThread *thread)
440 g_return_if_fail (thread != NULL);
443 g_message (G_GNUC_PRETTY_FUNCTION "mono_thread_detach for %d\n", thread->tid);
445 SET_CURRENT_OBJECT (NULL);
447 thread_cleanup (thread);
453 MonoThread *thread = mono_thread_current ();
455 SET_CURRENT_OBJECT (NULL);
456 thread_cleanup (thread);
458 /* we could add a callback here for embedders to use. */
459 if (thread == mono_thread_get_main ())
460 exit (mono_environment_exitcode_get ());
464 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
467 guint32 (*start_func)(void *);
468 struct StartInfo *start_info;
476 g_message(G_GNUC_PRETTY_FUNCTION
477 ": Trying to start a new thread: this (%p) start (%p)",
481 /* FIXME: remove the code inside BROKEN_THREAD_START once martin gets rid of the
482 * thread_start_compile_func stuff.
484 #define BROKEN_THREAD_START
485 #ifdef BROKEN_THREAD_START
486 im = mono_get_delegate_invoke (start->vtable->klass);
487 im = mono_marshal_get_delegate_invoke (im);
488 if (mono_thread_callbacks)
489 start_func = (* mono_thread_callbacks->thread_start_compile_func) (im);
491 start_func = mono_compile_method (im);
493 if(start_func==NULL) {
494 g_warning(G_GNUC_PRETTY_FUNCTION
495 ": Can't locate start method!");
502 /* This is freed in start_wrapper */
503 start_info = g_new0 (struct StartInfo, 1);
504 start_info->func = start_func;
505 #ifdef BROKEN_THREAD_START
506 start_info->start_arg = start;
508 start_info->start_arg = this->start_obj;
510 start_info->delegate = start;
511 start_info->obj = this;
512 start_info->domain = mono_domain_get ();
514 this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
515 if(this->start_notify==NULL) {
516 g_warning (G_GNUC_PRETTY_FUNCTION ": CreateSemaphore error 0x%x", GetLastError ());
520 thread=CreateThread(NULL, default_stacksize_for_thread (this), start_wrapper, start_info,
521 CREATE_SUSPENDED, &tid);
523 g_warning(G_GNUC_PRETTY_FUNCTION
524 ": CreateThread error 0x%x", GetLastError());
531 /* Don't call handle_store() here, delay it to Start.
532 * We can't join a thread (trying to will just block
533 * forever) until it actually starts running, so don't
534 * store the handle till then.
538 g_message(G_GNUC_PRETTY_FUNCTION
539 ": Started thread ID %d (handle %p)", tid, thread);
546 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
552 g_message (G_GNUC_PRETTY_FUNCTION ": Closing thread %p, handle %p",
556 CloseHandle (thread);
559 void ves_icall_System_Threading_Thread_Start_internal(MonoThread *this,
565 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Launching thread %p (%d)",
566 GetCurrentThreadId (), this, this->tid);
569 /* Only store the handle when the thread is about to be
570 * launched, to avoid the main thread deadlocking while trying
571 * to clean up a thread that will never be signalled.
575 if (mono_thread_callbacks)
576 (* mono_thread_callbacks->start_resume) (this->tid);
578 ResumeThread(thread);
580 if (mono_thread_callbacks)
581 (* mono_thread_callbacks->end_resume) (this->tid);
583 if(this->start_notify!=NULL) {
584 /* Wait for the thread to set up its TLS data etc, so
585 * theres no potential race condition if someone tries
586 * to look up the data believing the thread has
591 g_message(G_GNUC_PRETTY_FUNCTION
592 ": (%d) waiting for thread %p (%d) to start",
593 GetCurrentThreadId (), this, this->tid);
596 WaitForSingleObjectEx (this->start_notify, INFINITE, FALSE);
597 CloseHandle (this->start_notify);
598 this->start_notify=NULL;
602 g_message(G_GNUC_PRETTY_FUNCTION
603 ": (%d) Done launching thread %p (%d)",
604 GetCurrentThreadId (), this, this->tid);
608 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
610 MonoThread *thread = mono_thread_current ();
615 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", 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)
643 return mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
647 ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
650 g_free (this_obj->name);
652 this_obj->name = g_new (gunichar2, mono_string_length (name));
653 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
654 this_obj->name_len = mono_string_length (name);
657 this_obj->name = NULL;
661 ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoThread *this)
667 /* No need to lock here */
668 if (this->culture_info) {
669 domain = mono_domain_get ();
670 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
671 res = this->culture_info [i];
672 if (res && res->vtable->domain == domain)
681 ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread *this)
685 mono_monitor_enter (this->synch_lock);
686 if (this->serialized_culture_info) {
687 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_culture_info_len);
688 memcpy (mono_array_addr (res, guint8, 0), this->serialized_culture_info, this->serialized_culture_info_len);
692 mono_monitor_exit (this->synch_lock);
698 ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this, MonoObject *culture)
701 MonoDomain *domain = mono_domain_get ();
703 mono_monitor_enter (this->synch_lock);
704 if (!this->culture_info) {
705 this->culture_info = mono_gc_alloc_fixed (sizeof (MonoObject*) * NUM_CACHED_CULTURES, NULL);
708 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
709 if (this->culture_info [i]) {
710 if (this->culture_info [i]->vtable->domain == domain)
718 if (i < NUM_CACHED_CULTURES)
719 this->culture_info [i] = culture;
720 mono_monitor_exit (this->synch_lock);
724 ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread *this, MonoArray *arr)
726 mono_monitor_enter (this->synch_lock);
727 if (this->serialized_culture_info)
728 g_free (this->serialized_culture_info);
729 this->serialized_culture_info = g_new0 (guint8, mono_array_length (arr));
730 this->serialized_culture_info_len = mono_array_length (arr);
731 memcpy (this->serialized_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
732 mono_monitor_exit (this->synch_lock);
737 ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoThread *this)
743 /* No need to lock here */
744 if (this->ui_culture_info) {
745 domain = mono_domain_get ();
746 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
747 res = this->ui_culture_info [i];
748 if (res && res->vtable->domain == domain)
757 ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread *this)
761 mono_monitor_enter (this->synch_lock);
762 if (this->serialized_ui_culture_info) {
763 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_ui_culture_info_len);
764 memcpy (mono_array_addr (res, guint8, 0), this->serialized_ui_culture_info, this->serialized_ui_culture_info_len);
768 mono_monitor_exit (this->synch_lock);
774 ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this, MonoObject *culture)
777 MonoDomain *domain = mono_domain_get ();
779 mono_monitor_enter (this->synch_lock);
780 if (!this->ui_culture_info) {
781 this->ui_culture_info = mono_gc_alloc_fixed (sizeof (MonoObject*) * NUM_CACHED_CULTURES, NULL);
784 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
785 if (this->ui_culture_info [i]) {
786 if (this->ui_culture_info [i]->vtable->domain == domain)
794 if (i < NUM_CACHED_CULTURES)
795 this->ui_culture_info [i] = culture;
796 mono_monitor_exit (this->synch_lock);
800 ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread *this, MonoArray *arr)
802 mono_monitor_enter (this->synch_lock);
803 if (this->serialized_ui_culture_info)
804 g_free (this->serialized_ui_culture_info);
805 this->serialized_ui_culture_info = g_new0 (guint8, mono_array_length (arr));
806 this->serialized_ui_culture_info_len = mono_array_length (arr);
807 memcpy (this->serialized_ui_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
808 mono_monitor_exit (this->synch_lock);
811 /* the jit may read the compiled code of this function */
813 mono_thread_current (void)
818 thread = GET_CURRENT_OBJECT ();
819 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", thread);
823 return GET_CURRENT_OBJECT ();
827 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
828 int ms, HANDLE thread)
834 mono_monitor_enter (this->synch_lock);
835 this->state |= ThreadState_WaitSleepJoin;
836 mono_monitor_exit (this->synch_lock);
842 g_message (G_GNUC_PRETTY_FUNCTION ": joining thread handle %p, %d ms",
846 ret=WaitForSingleObjectEx (thread, ms, TRUE);
848 mono_monitor_enter (this->synch_lock);
849 this->state &= ~ThreadState_WaitSleepJoin;
850 mono_monitor_exit (this->synch_lock);
852 if(ret==WAIT_OBJECT_0) {
854 g_message (G_GNUC_PRETTY_FUNCTION ": join successful");
861 g_message (G_GNUC_PRETTY_FUNCTION ": join failed");
867 void ves_icall_System_Threading_Thread_SlotHash_store(MonoObject *data)
872 g_message(G_GNUC_PRETTY_FUNCTION ": Storing key %p", data);
875 /* Object location stored here */
876 TlsSetValue(slothash_key, data);
879 MonoObject *ves_icall_System_Threading_Thread_SlotHash_lookup(void)
885 data=TlsGetValue(slothash_key);
888 g_message(G_GNUC_PRETTY_FUNCTION ": Retrieved key %p", data);
894 /* FIXME: exitContext isnt documented */
895 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
901 MonoObject *waitHandle;
903 MonoThread *thread = mono_thread_current ();
907 numhandles = mono_array_length(mono_handles);
908 handles = g_new0(HANDLE, numhandles);
910 if (wait_handle_os_handle_field == 0) {
911 /* Get the field os_handle which will contain the actual handle */
912 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
913 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
916 for(i = 0; i < numhandles; i++) {
917 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
918 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
925 mono_monitor_enter (thread->synch_lock);
926 thread->state |= ThreadState_WaitSleepJoin;
927 mono_monitor_exit (thread->synch_lock);
929 ret=WaitForMultipleObjectsEx(numhandles, handles, TRUE, ms, TRUE);
931 mono_monitor_enter (thread->synch_lock);
932 thread->state &= ~ThreadState_WaitSleepJoin;
933 mono_monitor_exit (thread->synch_lock);
937 if(ret==WAIT_FAILED) {
938 #ifdef THREAD_WAIT_DEBUG
939 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
940 GetCurrentThreadId ());
943 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
944 /* Do we want to try again if we get
945 * WAIT_IO_COMPLETION? The documentation for
946 * WaitHandle doesn't give any clues. (We'd have to
947 * fiddle with the timeout if we retry.)
949 #ifdef THREAD_WAIT_DEBUG
950 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait timed out",
951 GetCurrentThreadId ());
959 /* FIXME: exitContext isnt documented */
960 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
966 MonoObject *waitHandle;
968 MonoThread *thread = mono_thread_current ();
972 numhandles = mono_array_length(mono_handles);
973 handles = g_new0(HANDLE, numhandles);
975 if (wait_handle_os_handle_field == 0) {
976 /* Get the field os_handle which will contain the actual handle */
977 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
978 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
981 for(i = 0; i < numhandles; i++) {
982 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
983 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
990 mono_monitor_enter (thread->synch_lock);
991 thread->state |= ThreadState_WaitSleepJoin;
992 mono_monitor_exit (thread->synch_lock);
994 ret=WaitForMultipleObjectsEx(numhandles, handles, FALSE, ms, TRUE);
996 mono_monitor_enter (thread->synch_lock);
997 thread->state &= ~ThreadState_WaitSleepJoin;
998 mono_monitor_exit (thread->synch_lock);
1002 #ifdef THREAD_WAIT_DEBUG
1003 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) returning %d",
1004 GetCurrentThreadId (), ret);
1008 * These need to be here. See MSDN dos on WaitForMultipleObjects.
1010 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
1011 return ret - WAIT_OBJECT_0;
1013 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
1014 return ret - WAIT_ABANDONED_0;
1021 /* FIXME: exitContext isnt documented */
1022 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
1025 MonoThread *thread = mono_thread_current ();
1027 MONO_ARCH_SAVE_REGS;
1029 #ifdef THREAD_WAIT_DEBUG
1030 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) waiting for %p, %d ms",
1031 GetCurrentThreadId (), handle, ms);
1038 mono_monitor_enter (thread->synch_lock);
1039 thread->state |= ThreadState_WaitSleepJoin;
1040 mono_monitor_exit (thread->synch_lock);
1042 ret=WaitForSingleObjectEx (handle, ms, TRUE);
1044 mono_monitor_enter (thread->synch_lock);
1045 thread->state &= ~ThreadState_WaitSleepJoin;
1046 mono_monitor_exit (thread->synch_lock);
1048 if(ret==WAIT_FAILED) {
1049 #ifdef THREAD_WAIT_DEBUG
1050 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
1051 GetCurrentThreadId ());
1054 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
1055 /* Do we want to try again if we get
1056 * WAIT_IO_COMPLETION? The documentation for
1057 * WaitHandle doesn't give any clues. (We'd have to
1058 * fiddle with the timeout if we retry.)
1060 #ifdef THREAD_WAIT_DEBUG
1061 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait timed out",
1062 GetCurrentThreadId ());
1070 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
1074 MONO_ARCH_SAVE_REGS;
1079 mutex = CreateMutex (NULL, owned, NULL);
1081 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
1083 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1091 void ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
1092 MONO_ARCH_SAVE_REGS;
1094 ReleaseMutex(handle);
1097 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name) {
1098 MONO_ARCH_SAVE_REGS;
1100 return(CreateEvent (NULL, manual, initial,
1101 name==NULL?NULL:mono_string_chars (name)));
1104 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
1105 MONO_ARCH_SAVE_REGS;
1107 return (SetEvent(handle));
1110 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
1111 MONO_ARCH_SAVE_REGS;
1113 return (ResetEvent(handle));
1117 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
1118 MONO_ARCH_SAVE_REGS;
1120 CloseHandle (handle);
1123 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
1125 MONO_ARCH_SAVE_REGS;
1127 return InterlockedIncrement (location);
1130 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
1134 MONO_ARCH_SAVE_REGS;
1136 EnterCriticalSection(&interlocked_mutex);
1140 LeaveCriticalSection(&interlocked_mutex);
1146 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
1148 MONO_ARCH_SAVE_REGS;
1150 return InterlockedDecrement(location);
1153 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
1157 MONO_ARCH_SAVE_REGS;
1159 EnterCriticalSection(&interlocked_mutex);
1163 LeaveCriticalSection(&interlocked_mutex);
1168 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location1, gint32 value)
1170 MONO_ARCH_SAVE_REGS;
1172 return InterlockedExchange(location1, value);
1175 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location1, MonoObject *value)
1177 MONO_ARCH_SAVE_REGS;
1179 return (MonoObject *) InterlockedExchangePointer((gpointer *) location1, value);
1182 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location1, gfloat value)
1184 IntFloatUnion val, ret;
1186 MONO_ARCH_SAVE_REGS;
1189 ret.ival = InterlockedExchange((gint32 *) location1, val.ival);
1194 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location1, gint32 value, gint32 comparand)
1196 MONO_ARCH_SAVE_REGS;
1198 return InterlockedCompareExchange(location1, value, comparand);
1201 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location1, MonoObject *value, MonoObject *comparand)
1203 MONO_ARCH_SAVE_REGS;
1205 return (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location1, value, comparand);
1208 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location1, gfloat value, gfloat comparand)
1210 IntFloatUnion val, ret, cmp;
1212 MONO_ARCH_SAVE_REGS;
1215 cmp.fval = comparand;
1216 ret.ival = InterlockedCompareExchange((gint32 *) location1, val.ival, cmp.ival);
1222 mono_thread_get_abort_signal (void)
1232 #endif /* __MINGW32__ */
1236 static CALLBACK void interruption_request_apc (ULONG_PTR param)
1238 MonoException* exc = mono_thread_request_interruption (FALSE);
1239 if (exc) mono_raise_exception (exc);
1242 #endif /* __MINGW32__ */
1245 * signal_thread_state_change
1247 * Tells the thread that his state has changed and it has to enter the new
1248 * state as soon as possible.
1250 static void signal_thread_state_change (MonoThread *thread)
1252 if (thread == mono_thread_current ()) {
1253 /* Do it synchronously */
1254 MonoException *exc = mono_thread_request_interruption (FALSE);
1256 mono_raise_exception (exc);
1260 QueueUserAPC (interruption_request_apc, thread->handle, NULL);
1262 /* fixme: store the state somewhere */
1263 #ifdef PTHREAD_POINTER_ID
1264 pthread_kill (GUINT_TO_POINTER(thread->tid), mono_thread_get_abort_signal ());
1266 pthread_kill (thread->tid, mono_thread_get_abort_signal ());
1268 #endif /* __MINGW32__ */
1272 ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
1274 MONO_ARCH_SAVE_REGS;
1276 mono_monitor_enter (thread->synch_lock);
1278 if ((thread->state & ThreadState_AbortRequested) != 0 ||
1279 (thread->state & ThreadState_StopRequested) != 0)
1281 mono_monitor_exit (thread->synch_lock);
1285 thread->state |= ThreadState_AbortRequested;
1286 thread->abort_state = state;
1287 thread->abort_exc = NULL;
1289 mono_monitor_exit (thread->synch_lock);
1292 g_message (G_GNUC_PRETTY_FUNCTION
1293 ": (%d) Abort requested for %p (%d)", GetCurrentThreadId (),
1294 thread, thread->tid);
1297 /* Make sure the thread is awake */
1298 ves_icall_System_Threading_Thread_Resume (thread);
1300 signal_thread_state_change (thread);
1304 ves_icall_System_Threading_Thread_ResetAbort (void)
1306 MonoThread *thread = mono_thread_current ();
1308 MONO_ARCH_SAVE_REGS;
1310 mono_monitor_enter (thread->synch_lock);
1312 thread->state &= ~ThreadState_AbortRequested;
1314 if (!thread->abort_exc) {
1315 const char *msg = "Unable to reset abort because no abort was requested";
1316 mono_monitor_exit (thread->synch_lock);
1317 mono_raise_exception (mono_get_exception_thread_state (msg));
1319 thread->abort_exc = NULL;
1320 thread->abort_state = NULL;
1323 mono_monitor_exit (thread->synch_lock);
1327 ves_icall_System_Threading_Thread_Suspend (MonoThread *thread)
1329 MONO_ARCH_SAVE_REGS;
1331 mono_monitor_enter (thread->synch_lock);
1333 if ((thread->state & ThreadState_Suspended) != 0 ||
1334 (thread->state & ThreadState_SuspendRequested) != 0 ||
1335 (thread->state & ThreadState_StopRequested) != 0)
1337 mono_monitor_exit (thread->synch_lock);
1341 thread->state |= ThreadState_SuspendRequested;
1342 mono_monitor_exit (thread->synch_lock);
1344 signal_thread_state_change (thread);
1348 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
1350 MONO_ARCH_SAVE_REGS;
1352 mono_monitor_enter (thread->synch_lock);
1354 if ((thread->state & ThreadState_SuspendRequested) != 0) {
1355 thread->state &= ~ThreadState_SuspendRequested;
1356 mono_monitor_exit (thread->synch_lock);
1360 if ((thread->state & ThreadState_Suspended) == 0)
1362 mono_monitor_exit (thread->synch_lock);
1366 thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1368 /* Awake the thread */
1369 SetEvent (thread->suspend_event);
1371 mono_monitor_exit (thread->synch_lock);
1373 /* Wait for the thread to awake */
1374 WaitForSingleObject (thread->resume_event, INFINITE);
1375 CloseHandle (thread->resume_event);
1376 thread->resume_event = NULL;
1380 find_wrapper (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1385 if (m->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1386 m->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
1387 m->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH)
1389 *((gboolean*)data) = TRUE;
1396 is_running_protected_wrapper (void)
1398 gboolean found = FALSE;
1399 mono_stack_walk (find_wrapper, &found);
1403 void mono_thread_stop (MonoThread *thread)
1405 mono_monitor_enter (thread->synch_lock);
1407 if ((thread->state & ThreadState_StopRequested) != 0 ||
1408 (thread->state & ThreadState_Stopped) != 0)
1410 mono_monitor_exit (thread->synch_lock);
1414 /* Make sure the thread is awake */
1415 ves_icall_System_Threading_Thread_Resume (thread);
1417 thread->state |= ThreadState_StopRequested;
1418 thread->state &= ~ThreadState_AbortRequested;
1420 mono_monitor_exit (thread->synch_lock);
1422 signal_thread_state_change (thread);
1426 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
1428 return *((volatile gint8 *) (ptr));
1432 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
1434 return *((volatile gint16 *) (ptr));
1438 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
1440 return *((volatile gint32 *) (ptr));
1444 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
1446 return *((volatile gint64 *) (ptr));
1450 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
1452 return (void *) *((volatile void **) ptr);
1456 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
1458 *((volatile gint8 *) ptr) = value;
1462 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
1464 *((volatile gint16 *) ptr) = value;
1468 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
1470 *((volatile gint32 *) ptr) = value;
1474 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
1476 *((volatile gint64 *) ptr) = value;
1480 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
1482 *((volatile void **) ptr) = value;
1485 void mono_thread_init (MonoThreadStartCB start_cb,
1486 MonoThreadAttachCB attach_cb)
1488 InitializeCriticalSection(&threads_mutex);
1489 InitializeCriticalSection(&interlocked_mutex);
1490 InitializeCriticalSection(&contexts_mutex);
1491 InitializeCriticalSection(&interruption_mutex);
1493 mono_init_static_data_info (&thread_static_info);
1494 mono_init_static_data_info (&context_static_info);
1496 current_object_key=TlsAlloc();
1498 g_message (G_GNUC_PRETTY_FUNCTION ": Allocated current_object_key %d",
1499 current_object_key);
1502 mono_thread_start_cb = start_cb;
1503 mono_thread_attach_cb = attach_cb;
1505 slothash_key=TlsAlloc();
1507 /* Get a pseudo handle to the current process. This is just a
1508 * kludge so that wapi can build a process handle if needed.
1509 * As a pseudo handle is returned, we don't need to clean
1512 GetCurrentProcess ();
1516 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
1518 mono_thread_cleanup = func;
1521 void mono_install_thread_callbacks (MonoThreadCallbacks *callbacks)
1523 mono_thread_callbacks = callbacks;
1527 static void print_tids (gpointer key, gpointer value, gpointer user)
1529 g_message ("Waiting for: %d", GPOINTER_TO_UINT(key));
1535 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1536 MonoThread *threads[MAXIMUM_WAIT_OBJECTS];
1540 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
1545 g_message(G_GNUC_PRETTY_FUNCTION
1546 ": %d threads to wait for in this batch", wait->num);
1549 ret=WaitForMultipleObjectsEx(wait->num, wait->handles, TRUE, timeout, FALSE);
1551 if(ret==WAIT_FAILED) {
1552 /* See the comment in build_wait_tids() */
1554 g_message (G_GNUC_PRETTY_FUNCTION ": Wait failed");
1559 for(i=0; i<wait->num; i++)
1560 CloseHandle (wait->handles[i]);
1562 if (ret == WAIT_TIMEOUT)
1565 for(i=0; i<wait->num; i++) {
1566 guint32 tid=wait->threads[i]->tid;
1568 if(mono_g_hash_table_lookup (threads, GUINT_TO_POINTER(tid))!=NULL) {
1569 /* This thread must have been killed, because
1570 * it hasn't cleaned itself up. (It's just
1571 * possible that the thread exited before the
1572 * parent thread had a chance to store the
1573 * handle, and now there is another pointer to
1574 * the already-exited thread stored. In this
1575 * case, we'll just get two
1576 * mono_profiler_thread_end() calls for the
1581 g_message (G_GNUC_PRETTY_FUNCTION
1582 ": cleaning up after thread %d", tid);
1584 thread_cleanup (wait->threads[i]);
1589 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
1591 struct wait_data *wait=(struct wait_data *)user;
1593 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
1595 MonoThread *thread=(MonoThread *)value;
1597 /* Ignore background threads, we abort them later */
1598 if (thread->state & ThreadState_Background)
1599 return; /* just leave, ignore */
1601 if (mono_gc_is_finalizer_thread (thread))
1604 if (thread == mono_thread_current ())
1607 if (thread == mono_thread_get_main ())
1610 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1614 wait->handles[wait->num]=handle;
1615 wait->threads[wait->num]=thread;
1618 /* Just ignore the rest, we can't do anything with
1625 remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
1627 struct wait_data *wait=(struct wait_data *)user;
1628 guint32 self = GetCurrentThreadId ();
1629 MonoThread *thread = (MonoThread *) value;
1632 /* The finalizer thread is not a background thread */
1633 if (thread->tid != self && thread->state & ThreadState_Background) {
1635 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1639 wait->handles[wait->num]=thread->handle;
1640 wait->threads[wait->num]=thread;
1643 if(thread->state & ThreadState_AbortRequested ||
1644 thread->state & ThreadState_Aborted) {
1646 g_message (G_GNUC_PRETTY_FUNCTION ": Thread id %d already aborting", thread->tid);
1652 g_print (G_GNUC_PRETTY_FUNCTION ": Aborting id: %d\n", thread->tid);
1654 mono_thread_stop (thread);
1658 return (thread->tid != self && !mono_gc_is_finalizer_thread (thread));
1661 void mono_thread_manage (void)
1663 struct wait_data *wait=g_new0 (struct wait_data, 1);
1665 /* join each thread that's still running */
1667 g_message(G_GNUC_PRETTY_FUNCTION ": Joining each running thread...");
1670 EnterCriticalSection (&threads_mutex);
1673 g_message(G_GNUC_PRETTY_FUNCTION ": No threads");
1675 LeaveCriticalSection (&threads_mutex);
1678 LeaveCriticalSection (&threads_mutex);
1681 EnterCriticalSection (&threads_mutex);
1683 g_message(G_GNUC_PRETTY_FUNCTION
1684 ":There are %d threads to join",
1685 mono_g_hash_table_size (threads));
1686 mono_g_hash_table_foreach (threads, print_tids, NULL);
1690 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
1691 LeaveCriticalSection (&threads_mutex);
1693 /* Something to wait for */
1694 wait_for_tids (wait, INFINITE);
1696 } while(wait->num>0);
1698 mono_thread_pool_cleanup ();
1700 EnterCriticalSection(&threads_mutex);
1703 * Remove everything but the finalizer thread and self.
1704 * Also abort all the background threads
1707 mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
1709 LeaveCriticalSection(&threads_mutex);
1712 /* Something to wait for */
1713 wait_for_tids (wait, INFINITE);
1716 * give the subthreads a chance to really quit (this is mainly needed
1717 * to get correct user and system times from getrusage/wait/time(1)).
1718 * This could be removed if we avoid pthread_detach() and use pthread_join().
1720 #ifndef PLATFORM_WIN32
1727 static void terminate_thread (gpointer key, gpointer value, gpointer user)
1729 MonoThread *thread=(MonoThread *)value;
1730 guint32 self=GPOINTER_TO_UINT (user);
1732 if(thread->tid!=self) {
1733 /*TerminateThread (thread->handle, -1);*/
1737 void mono_thread_abort_all_other_threads (void)
1739 guint32 self=GetCurrentThreadId ();
1741 EnterCriticalSection (&threads_mutex);
1743 g_message(G_GNUC_PRETTY_FUNCTION ":There are %d threads to abort",
1744 mono_g_hash_table_size (threads));
1745 mono_g_hash_table_foreach (threads, print_tids, NULL);
1748 mono_g_hash_table_foreach (threads, terminate_thread,
1749 GUINT_TO_POINTER (self));
1751 LeaveCriticalSection (&threads_mutex);
1754 static void suspend_thread (gpointer key, gpointer value, gpointer user)
1756 MonoThread *thread=(MonoThread *)value;
1757 guint32 self=GPOINTER_TO_UINT (user);
1759 if ((thread->tid!=self) && !mono_gc_is_finalizer_thread (thread))
1760 SuspendThread (thread->handle);
1764 * mono_thread_suspend_all_other_threads:
1766 * Suspend all managed threads except the finalizer thread and this thread.
1768 void mono_thread_suspend_all_other_threads (void)
1770 guint32 self=GetCurrentThreadId ();
1772 EnterCriticalSection (&threads_mutex);
1773 mono_g_hash_table_foreach (threads, suspend_thread,
1774 GUINT_TO_POINTER (self));
1775 LeaveCriticalSection (&threads_mutex);
1779 * mono_thread_push_appdomain_ref:
1781 * Register that the current thread may have references to objects in domain
1782 * @domain on its stack. Each call to this function should be paired with a
1783 * call to pop_appdomain_ref.
1786 mono_thread_push_appdomain_ref (MonoDomain *domain)
1788 MonoThread *thread = mono_thread_current ();
1791 /* printf ("PUSH REF: %x -> %s.\n", thread->tid, domain->friendly_name); */
1792 EnterCriticalSection (&threads_mutex);
1793 thread->appdomain_refs = g_slist_prepend (thread->appdomain_refs, domain);
1794 LeaveCriticalSection (&threads_mutex);
1799 mono_thread_pop_appdomain_ref (void)
1801 MonoThread *thread = mono_thread_current ();
1804 /* printf ("POP REF: %x -> %s.\n", thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
1805 EnterCriticalSection (&threads_mutex);
1806 /* FIXME: How can the list be empty ? */
1807 if (thread->appdomain_refs)
1808 thread->appdomain_refs = g_slist_remove (thread->appdomain_refs, thread->appdomain_refs->data);
1809 LeaveCriticalSection (&threads_mutex);
1814 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
1817 EnterCriticalSection (&threads_mutex);
1818 res = g_slist_find (thread->appdomain_refs, domain) != NULL;
1819 LeaveCriticalSection (&threads_mutex);
1823 typedef struct abort_appdomain_data {
1824 struct wait_data wait;
1826 } abort_appdomain_data;
1829 abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
1831 MonoThread *thread = (MonoThread*)value;
1832 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
1833 MonoDomain *domain = data->domain;
1835 if (mono_thread_has_appdomain_ref (thread, domain)) {
1836 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
1837 HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
1841 ves_icall_System_Threading_Thread_Abort (thread, NULL);
1843 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
1844 data->wait.handles [data->wait.num] = handle;
1845 data->wait.threads [data->wait.num] = thread;
1848 /* Just ignore the rest, we can't do anything with
1856 * mono_threads_abort_appdomain_threads:
1858 * Abort threads which has references to the given appdomain.
1861 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
1863 abort_appdomain_data user_data;
1866 /* printf ("ABORT BEGIN.\n"); */
1868 start_time = GetTickCount ();
1870 EnterCriticalSection (&threads_mutex);
1872 user_data.domain = domain;
1873 user_data.wait.num = 0;
1874 mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
1875 LeaveCriticalSection (&threads_mutex);
1877 if (user_data.wait.num > 0)
1878 wait_for_tids (&user_data.wait, timeout);
1880 /* Update remaining time */
1881 timeout -= GetTickCount () - start_time;
1882 start_time = GetTickCount ();
1887 while (user_data.wait.num > 0);
1889 /* printf ("ABORT DONE.\n"); */
1895 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
1897 MonoThread *thread = (MonoThread*)value;
1898 MonoDomain *domain = (MonoDomain*)user_data;
1901 /* No locking needed here */
1903 if (thread->culture_info) {
1904 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
1905 if (thread->culture_info [i] && thread->culture_info [i]->vtable->domain == domain)
1906 thread->culture_info [i] = NULL;
1909 if (thread->ui_culture_info) {
1910 for (i = 0; i < NUM_CACHED_CULTURES; ++i) {
1911 if (thread->ui_culture_info [i] && thread->ui_culture_info [i]->vtable->domain == domain)
1912 thread->ui_culture_info [i] = NULL;
1918 * mono_threads_clear_cached_culture:
1920 * Clear the cached_current_culture from all threads if it is in the
1924 mono_threads_clear_cached_culture (MonoDomain *domain)
1926 EnterCriticalSection (&threads_mutex);
1927 mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
1928 LeaveCriticalSection (&threads_mutex);
1932 * mono_thread_get_pending_exception:
1934 * Return an exception which needs to be raised when leaving a catch clause.
1935 * This is used for undeniable exception propagation.
1938 mono_thread_get_pending_exception (void)
1940 MonoThread *thread = mono_thread_current ();
1942 MONO_ARCH_SAVE_REGS;
1944 if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
1946 * FIXME: Clear the abort exception and return an AppDomainUnloaded
1947 * exception if the thread no longer references a dying appdomain.
1949 thread->abort_exc->trace_ips = NULL;
1950 thread->abort_exc->stack_trace = NULL;
1951 return thread->abort_exc;
1957 #define NUM_STATIC_DATA_IDX 8
1958 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
1959 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
1964 * mono_alloc_static_data
1966 * Allocate memory blocks for storing threads or context static data
1969 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset)
1971 guint idx = (offset >> 24) - 1;
1974 gpointer* static_data = *static_data_ptr;
1976 static_data = mono_gc_alloc_fixed (static_data_size [0], NULL);
1977 *static_data_ptr = static_data;
1978 static_data [0] = static_data;
1981 for (i = 1; i <= idx; ++i) {
1982 if (static_data [i])
1984 static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
1989 * mono_init_static_data_info
1991 * Initializes static data counters
1993 static void mono_init_static_data_info (StaticDataInfo *static_data)
1995 static_data->idx = 0;
1996 static_data->offset = 0;
2000 * mono_alloc_static_data_slot
2002 * Generates an offset for static data. static_data contains the counters
2003 * used to generate it.
2006 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
2010 if (!static_data->idx && !static_data->offset) {
2012 * we use the first chunk of the first allocation also as
2013 * an array for the rest of the data
2015 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
2017 static_data->offset += align - 1;
2018 static_data->offset &= ~(align - 1);
2019 if (static_data->offset + size >= static_data_size [static_data->idx]) {
2020 static_data->idx ++;
2021 g_assert (size <= static_data_size [static_data->idx]);
2023 * massive unloading and reloading of domains with thread-static
2024 * data may eventually exceed the allocated storage...
2025 * Need to check what the MS runtime does in that case.
2026 * Note that for each appdomain, we need to allocate a separate
2027 * thread data slot for security reasons. We could keep track
2028 * of the slots per-domain and when the domain is unloaded
2029 * out the slots on a sort of free list.
2031 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
2032 static_data->offset = 0;
2034 offset = static_data->offset | ((static_data->idx + 1) << 24);
2035 static_data->offset += size;
2040 * ensure thread static fields already allocated are valid for thread
2041 * This function is called when a thread is created or on thread attach.
2044 thread_adjust_static_data (MonoThread *thread)
2048 EnterCriticalSection (&threads_mutex);
2049 if (thread_static_info.offset || thread_static_info.idx > 0) {
2050 /* get the current allocated size */
2051 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
2052 mono_alloc_static_data (&(thread->static_data), offset);
2054 LeaveCriticalSection (&threads_mutex);
2058 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
2060 MonoThread *thread = value;
2061 guint32 offset = GPOINTER_TO_UINT (user);
2063 mono_alloc_static_data (&(thread->static_data), offset);
2067 * The offset for a special static variable is composed of three parts:
2068 * a bit that indicates the type of static data (0:thread, 1:context),
2069 * an index in the array of chunks of memory for the thread (thread->static_data)
2070 * and an offset in that chunk of mem. This allows allocating less memory in the
2075 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align)
2078 if (static_type == SPECIAL_STATIC_THREAD)
2080 EnterCriticalSection (&threads_mutex);
2081 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
2082 /* This can be called during startup */
2083 if (threads != NULL)
2084 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
2085 LeaveCriticalSection (&threads_mutex);
2089 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
2090 EnterCriticalSection (&contexts_mutex);
2091 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
2092 LeaveCriticalSection (&contexts_mutex);
2093 offset |= 0x80000000; /* Set the high bit to indicate context static data */
2099 mono_get_special_static_data (guint32 offset)
2101 /* The high bit means either thread (0) or static (1) data. */
2103 guint32 static_type = (offset & 0x80000000);
2106 offset &= 0x7fffffff;
2107 idx = (offset >> 24) - 1;
2109 if (static_type == 0)
2111 MonoThread *thread = mono_thread_current ();
2112 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
2116 /* Allocate static data block under demand, since we don't have a list
2119 MonoAppContext *context = mono_context_get ();
2120 if (!context->static_data || !context->static_data [idx]) {
2121 EnterCriticalSection (&contexts_mutex);
2122 mono_alloc_static_data (&(context->static_data), offset);
2123 LeaveCriticalSection (&contexts_mutex);
2125 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
2129 static void gc_stop_world (gpointer key, gpointer value, gpointer user)
2131 MonoThread *thread=(MonoThread *)value;
2132 guint32 self=GPOINTER_TO_UINT (user);
2135 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d", self, thread->tid);
2138 if(thread->tid==self)
2141 SuspendThread (thread->handle);
2144 void mono_gc_stop_world (void)
2146 guint32 self=GetCurrentThreadId ();
2149 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads);
2152 EnterCriticalSection (&threads_mutex);
2154 if (threads != NULL)
2155 mono_g_hash_table_foreach (threads, gc_stop_world, GUINT_TO_POINTER (self));
2157 LeaveCriticalSection (&threads_mutex);
2160 static void gc_start_world (gpointer key, gpointer value, gpointer user)
2162 MonoThread *thread=(MonoThread *)value;
2163 guint32 self=GPOINTER_TO_UINT (user);
2166 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d", self, thread->tid);
2169 if(thread->tid==self)
2172 ResumeThread (thread->handle);
2175 void mono_gc_start_world (void)
2177 guint32 self=GetCurrentThreadId ();
2180 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads);
2183 EnterCriticalSection (&threads_mutex);
2185 if (threads != NULL)
2186 mono_g_hash_table_foreach (threads, gc_start_world, GUINT_TO_POINTER (self));
2188 LeaveCriticalSection (&threads_mutex);
2192 static CALLBACK void dummy_apc (ULONG_PTR param)
2196 static guint32 dummy_apc (gpointer param)
2203 * mono_thread_execute_interruption
2205 * Performs the operation that the requested thread state requires (abort,
2208 static MonoException* mono_thread_execute_interruption (MonoThread *thread)
2210 mono_monitor_enter (thread->synch_lock);
2212 if (thread->interruption_requested) {
2213 /* this will consume pending APC calls */
2214 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
2215 EnterCriticalSection (&interruption_mutex);
2216 thread_interruption_requested--;
2217 LeaveCriticalSection (&interruption_mutex);
2218 thread->interruption_requested = FALSE;
2221 if ((thread->state & ThreadState_AbortRequested) != 0) {
2222 if (thread->abort_exc == NULL)
2223 thread->abort_exc = mono_get_exception_thread_abort ();
2224 mono_monitor_exit (thread->synch_lock);
2225 return thread->abort_exc;
2227 else if ((thread->state & ThreadState_SuspendRequested) != 0) {
2228 thread->state &= ~ThreadState_SuspendRequested;
2229 thread->state |= ThreadState_Suspended;
2230 thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2231 mono_monitor_exit (thread->synch_lock);
2233 WaitForSingleObject (thread->suspend_event, INFINITE);
2235 mono_monitor_enter (thread->synch_lock);
2236 CloseHandle (thread->suspend_event);
2237 thread->suspend_event = NULL;
2238 thread->state &= ~ThreadState_Suspended;
2240 /* The thread that requested the resume will have replaced this event
2241 * and will be waiting for it
2243 SetEvent (thread->resume_event);
2244 mono_monitor_exit (thread->synch_lock);
2247 else if ((thread->state & ThreadState_StopRequested) != 0) {
2248 /* FIXME: do this through the JIT? */
2249 mono_monitor_exit (thread->synch_lock);
2250 mono_thread_exit ();
2254 mono_monitor_exit (thread->synch_lock);
2259 * mono_thread_request_interruption
2261 * A signal handler can call this method to request the interruption of a
2262 * thread. The result of the interruption will depend on the current state of
2263 * the thread. If the result is an exception that needs to be throw, it is
2264 * provided as return value.
2266 MonoException* mono_thread_request_interruption (gboolean running_managed)
2268 MonoThread *thread = mono_thread_current ();
2270 /* The thread may already be stopping */
2274 mono_monitor_enter (thread->synch_lock);
2276 if (thread->interruption_requested) {
2277 mono_monitor_exit (thread->synch_lock);
2281 if (!running_managed || is_running_protected_wrapper ()) {
2282 /* Can't stop while in unmanaged code. Increase the global interruption
2283 request count. When exiting the unmanaged method the count will be
2284 checked and the thread will be interrupted. */
2286 EnterCriticalSection (&interruption_mutex);
2287 thread_interruption_requested++;
2288 LeaveCriticalSection (&interruption_mutex);
2290 thread->interruption_requested = TRUE;
2291 mono_monitor_exit (thread->synch_lock);
2293 /* this will awake the thread if it is in WaitForSingleObject
2295 QueueUserAPC (dummy_apc, thread->handle, NULL);
2300 mono_monitor_exit (thread->synch_lock);
2301 return mono_thread_execute_interruption (thread);
2305 gboolean mono_thread_interruption_requested ()
2307 if (thread_interruption_requested) {
2308 MonoThread *thread = mono_thread_current ();
2309 /* The thread may already be stopping */
2311 return (thread->interruption_requested);
2316 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
2318 MonoThread *thread = mono_thread_current ();
2320 /* The thread may already be stopping */
2324 if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
2325 MonoException* exc = mono_thread_execute_interruption (thread);
2326 if (exc) mono_raise_exception (exc);
2331 * Performs the interruption of the current thread, if one has been requested,
2332 * and the thread is not running a protected wrapper.
2334 void mono_thread_interruption_checkpoint ()
2336 mono_thread_interruption_checkpoint_request (FALSE);
2340 * Performs the interruption of the current thread, if one has been requested.
2342 void mono_thread_force_interruption_checkpoint ()
2344 mono_thread_interruption_checkpoint_request (TRUE);
2348 * mono_thread_interruption_request_flag:
2350 * Returns the address of a flag that will be non-zero if an interruption has
2351 * been requested for a thread. The thread to interrupt may not be the current
2352 * thread, so an additional call to mono_thread_interruption_requested() or
2353 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
2356 gint32* mono_thread_interruption_request_flag ()
2358 return &thread_interruption_requested;
2361 #ifdef WITH_INCLUDED_LIBGC
2363 static void gc_push_all_stacks (gpointer key, gpointer value, gpointer user)
2365 MonoThread *thread=(MonoThread *)value;
2366 guint32 *selfp=(guint32 *)user, self = *selfp;
2369 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d - %p", self, thread->tid, thread->stack_ptr);
2372 if(thread->tid==self) {
2374 g_message (G_GNUC_PRETTY_FUNCTION ": %p - %p", selfp, thread->stack_ptr);
2376 GC_push_all_stack (selfp, thread->stack_ptr);
2380 #ifdef PLATFORM_WIN32
2381 GC_win32_push_thread_stack (thread->handle, thread->stack_ptr);
2383 mono_wapi_push_thread_stack (thread->handle, thread->stack_ptr);
2387 void mono_gc_push_all_stacks (void)
2389 guint32 self=GetCurrentThreadId ();
2392 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads);
2395 EnterCriticalSection (&threads_mutex);
2397 if (threads != NULL)
2398 mono_g_hash_table_foreach (threads, gc_push_all_stacks, &self);
2400 LeaveCriticalSection (&threads_mutex);
2403 #endif /* WITH_INCLUDED_LIBGC */