2 * threads.c: Thread support internal calls
5 * Dick Porter (dick@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
14 #define _WIN32_WINNT 0x0500
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/threadpool.h>
26 #include <mono/metadata/threads-types.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/environment.h>
29 #include <mono/metadata/monitor.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/metadata/marshal.h>
32 #include <mono/io-layer/io-layer.h>
33 #include <mono/metadata/object-internals.h>
34 #include <mono/metadata/mono-debug-debugger.h>
35 #include <mono/utils/mono-compiler.h>
36 #include <mono/utils/mono-mmap.h>
37 #include <mono/utils/mono-membar.h>
39 #include <mono/os/gc_wrapper.h>
41 /*#define THREAD_DEBUG(a) do { a; } while (0)*/
42 #define THREAD_DEBUG(a)
43 /*#define THREAD_WAIT_DEBUG(a) do { a; } while (0)*/
44 #define THREAD_WAIT_DEBUG(a)
45 /*#define LIBGC_DEBUG(a) do { a; } while (0)*/
46 #define LIBGC_DEBUG(a)
48 /* Provide this for systems with glib < 2.6 */
49 #ifndef G_GSIZE_FORMAT
50 # if GLIB_SIZEOF_LONG == 8
51 # define G_GSIZE_FORMAT "lu"
53 # define G_GSIZE_FORMAT "u"
59 guint32 (*func)(void *);
76 typedef struct _MonoThreadDomainTls MonoThreadDomainTls;
77 struct _MonoThreadDomainTls {
78 MonoThreadDomainTls *next;
86 MonoThreadDomainTls *freelist;
91 MonoHazardousFreeFunc free_func;
94 /* Number of cached culture objects in the MonoThread->cached_culture_info array
95 * (per-type): we use the first NUM entries for CultureInfo and the last for
96 * UICultureInfo. So the size of the array is really NUM_CACHED_CULTURES * 2.
98 #define NUM_CACHED_CULTURES 4
99 #define CULTURES_START_IDX 0
100 #define UICULTURES_START_IDX NUM_CACHED_CULTURES
102 /* Controls access to the 'threads' hash table */
103 #define mono_threads_lock() EnterCriticalSection (&threads_mutex)
104 #define mono_threads_unlock() LeaveCriticalSection (&threads_mutex)
105 static CRITICAL_SECTION threads_mutex;
107 /* Controls access to context static data */
108 #define mono_contexts_lock() EnterCriticalSection (&contexts_mutex)
109 #define mono_contexts_unlock() LeaveCriticalSection (&contexts_mutex)
110 static CRITICAL_SECTION contexts_mutex;
112 /* Holds current status of static data heap */
113 static StaticDataInfo thread_static_info;
114 static StaticDataInfo context_static_info;
116 /* The hash of existing threads (key is thread ID) that need joining
119 static MonoGHashTable *threads=NULL;
121 /* The TLS key that holds the MonoObject assigned to each thread */
122 static guint32 current_object_key = -1;
124 #ifdef HAVE_KW_THREAD
125 /* we need to use both the Tls* functions and __thread because
126 * the gc needs to see all the threads
128 static __thread MonoThread * tls_current_object MONO_TLS_FAST;
129 #define SET_CURRENT_OBJECT(x) do { \
130 tls_current_object = x; \
131 TlsSetValue (current_object_key, x); \
133 #define GET_CURRENT_OBJECT() tls_current_object
135 #define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x);
136 #define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key);
139 /* function called at thread start */
140 static MonoThreadStartCB mono_thread_start_cb = NULL;
142 /* function called at thread attach */
143 static MonoThreadAttachCB mono_thread_attach_cb = NULL;
145 /* function called at thread cleanup */
146 static MonoThreadCleanupFunc mono_thread_cleanup_fn = NULL;
148 /* The default stack size for each thread */
149 static guint32 default_stacksize = 0;
150 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
152 static void thread_adjust_static_data (MonoThread *thread);
153 static void mono_init_static_data_info (StaticDataInfo *static_data);
154 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
155 static gboolean mono_thread_resume (MonoThread* thread);
156 static void mono_thread_start (MonoThread *thread);
157 static void signal_thread_state_change (MonoThread *thread);
159 /* Spin lock for InterlockedXXX 64 bit functions */
160 #define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
161 #define mono_interlocked_unlock() LeaveCriticalSection (&interlocked_mutex)
162 static CRITICAL_SECTION interlocked_mutex;
164 /* global count of thread interruptions requested */
165 static gint32 thread_interruption_requested = 0;
167 /* Event signaled when a thread changes its background mode */
168 static HANDLE background_change_event;
170 /* The table for small ID assignment */
171 static CRITICAL_SECTION small_id_mutex;
172 static int small_id_table_size = 0;
173 static int small_id_next = 0;
174 static int highest_small_id = -1;
175 static MonoThread **small_id_table = NULL;
177 /* The hazard table */
178 #define HAZARD_TABLE_MAX_SIZE 16384 /* There cannot be more threads than this number. */
179 static volatile int hazard_table_size = 0;
180 static MonoThreadHazardPointers * volatile hazard_table = NULL;
182 /* The table where we keep pointers to blocks to be freed but that
183 have to wait because they're guarded by a hazard pointer. */
184 static CRITICAL_SECTION delayed_free_table_mutex;
185 static GArray *delayed_free_table = NULL;
188 mono_thread_get_tls_key (void)
190 return current_object_key;
194 mono_thread_get_tls_offset (void)
197 MONO_THREAD_VAR_OFFSET (tls_current_object,offset);
201 /* handle_store() and handle_remove() manage the array of threads that
202 * still need to be waited for when the main thread exits.
204 static void handle_store(MonoThread *thread)
206 mono_threads_lock ();
208 THREAD_DEBUG (g_message ("%s: thread %p ID %"G_GSIZE_FORMAT, __func__, thread, (gsize)thread->tid));
211 MONO_GC_REGISTER_ROOT (threads);
212 threads=mono_g_hash_table_new(NULL, NULL);
215 /* We don't need to duplicate thread->handle, because it is
216 * only closed when the thread object is finalized by the GC.
218 mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->tid),
221 mono_threads_unlock ();
224 static void handle_remove(gsize tid)
226 THREAD_DEBUG (g_message ("%s: thread ID %"G_GSIZE_FORMAT, __func__, tid));
228 mono_threads_lock ();
232 mono_g_hash_table_remove (threads, (gpointer)tid);
234 mono_threads_unlock ();
236 /* Don't close the handle here, wait for the object finalizer
237 * to do it. Otherwise, the following race condition applies:
239 * 1) Thread exits (and handle_remove() closes the handle)
241 * 2) Some other handle is reassigned the same slot
243 * 3) Another thread tries to join the first thread, and
244 * blocks waiting for the reassigned handle to be signalled
245 * (which might never happen). This is possible, because the
246 * thread calling Join() still has a reference to the first
252 * Allocate a small thread id.
254 * FIXME: The biggest part of this function is very similar to
255 * domain_id_alloc() in domain.c and should be merged.
258 small_id_alloc (MonoThread *thread)
262 EnterCriticalSection (&small_id_mutex);
264 if (!small_id_table) {
265 small_id_table_size = 2;
266 small_id_table = mono_gc_alloc_fixed (small_id_table_size * sizeof (MonoThread*), NULL);
268 for (i = small_id_next; i < small_id_table_size; ++i) {
269 if (!small_id_table [i]) {
275 for (i = 0; i < small_id_next; ++i) {
276 if (!small_id_table [i]) {
283 MonoThread **new_table;
284 int new_size = small_id_table_size * 2;
285 if (new_size >= (1 << 16))
286 g_assert_not_reached ();
287 id = small_id_table_size;
288 new_table = mono_gc_alloc_fixed (new_size * sizeof (MonoThread*), NULL);
289 memcpy (new_table, small_id_table, small_id_table_size * sizeof (void*));
290 mono_gc_free_fixed (small_id_table);
291 small_id_table = new_table;
292 small_id_table_size = new_size;
294 thread->small_id = id;
295 g_assert (small_id_table [id] == NULL);
296 small_id_table [id] = thread;
298 if (small_id_next > small_id_table_size)
301 if (id >= hazard_table_size) {
303 int pagesize = mono_pagesize ();
304 int num_pages = (hazard_table_size * sizeof (MonoThreadHazardPointers) + pagesize - 1) / pagesize;
306 if (hazard_table == NULL) {
307 hazard_table = mono_valloc (NULL,
308 sizeof (MonoThreadHazardPointers) * HAZARD_TABLE_MAX_SIZE,
312 g_assert (hazard_table != NULL);
313 page_addr = (guint8*)hazard_table + num_pages * pagesize;
315 g_assert (id < HAZARD_TABLE_MAX_SIZE);
317 mono_mprotect (page_addr, pagesize, MONO_MMAP_READ | MONO_MMAP_WRITE);
320 hazard_table_size = num_pages * pagesize / sizeof (MonoThreadHazardPointers);
322 g_assert (id < hazard_table_size);
324 hazard_table [id].hazard_pointers [0] = NULL;
325 hazard_table [id].hazard_pointers [1] = NULL;
328 if (id > highest_small_id) {
329 highest_small_id = id;
330 mono_memory_write_barrier ();
333 LeaveCriticalSection (&small_id_mutex);
339 small_id_free (int id)
341 g_assert (id >= 0 && id < small_id_table_size);
342 g_assert (small_id_table [id] != NULL);
344 small_id_table [id] = NULL;
348 is_pointer_hazardous (gpointer p)
351 int highest = highest_small_id;
353 g_assert (highest < hazard_table_size);
355 for (i = 0; i <= highest; ++i) {
356 if (hazard_table [i].hazard_pointers [0] == p
357 || hazard_table [i].hazard_pointers [1] == p)
364 MonoThreadHazardPointers*
365 mono_hazard_pointer_get (void)
367 MonoThread *current_thread = mono_thread_current ();
369 g_assert (current_thread && current_thread->small_id >= 0);
371 return &hazard_table [current_thread->small_id];
375 mono_thread_hazardous_free_or_queue (gpointer p, MonoHazardousFreeFunc free_func)
377 if (is_pointer_hazardous (p)) {
378 DelayedFreeItem item = { p, free_func };
380 ++mono_stats.hazardous_pointer_count;
382 EnterCriticalSection (&delayed_free_table_mutex);
383 g_array_append_val (delayed_free_table, item);
384 LeaveCriticalSection (&delayed_free_table_mutex);
386 DelayedFreeItem item;
390 if (delayed_free_table->len == 0)
394 EnterCriticalSection (&delayed_free_table_mutex);
395 if (delayed_free_table->len > 0) {
396 DelayedFreeItem item = g_array_index (delayed_free_table, DelayedFreeItem, 0);
398 if (!is_pointer_hazardous (item.p))
399 g_array_remove_index_fast (delayed_free_table, 0);
403 LeaveCriticalSection (&delayed_free_table_mutex);
406 item.free_func (item.p);
410 static void thread_cleanup (MonoThread *thread)
412 g_assert (thread != NULL);
414 mono_release_type_locks (thread);
416 if (!mono_monitor_enter (thread->synch_lock))
419 thread->state |= ThreadState_Stopped;
420 thread->state &= ~ThreadState_Background;
421 mono_monitor_exit (thread->synch_lock);
423 mono_profiler_thread_end (thread->tid);
424 handle_remove (thread->tid);
426 mono_thread_pop_appdomain_ref ();
428 if (thread->serialized_culture_info)
429 g_free (thread->serialized_culture_info);
431 thread->cached_culture_info = NULL;
433 if (mono_thread_cleanup_fn)
434 mono_thread_cleanup_fn (thread);
436 small_id_free (thread->small_id);
437 thread->small_id = -2;
440 static guint32 WINAPI start_wrapper(void *data)
442 struct StartInfo *start_info=(struct StartInfo *)data;
443 guint32 (*start_func)(void *);
446 MonoThread *thread=start_info->obj;
447 MonoObject *start_delegate = start_info->delegate;
449 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper", __func__, GetCurrentThreadId ()));
451 /* We can be sure start_info->obj->tid and
452 * start_info->obj->handle have been set, because the thread
453 * was created suspended, and these values were set before the
459 SET_CURRENT_OBJECT (thread);
461 /* Every thread references the appdomain which created it */
462 mono_thread_push_appdomain_ref (start_info->domain);
464 if (!mono_domain_set (start_info->domain, FALSE)) {
465 /* No point in raising an appdomain_unloaded exception here */
466 /* FIXME: Cleanup here */
467 mono_thread_pop_appdomain_ref ();
471 start_func = start_info->func;
472 start_arg = start_info->start_arg;
474 /* This MUST be called before any managed code can be
475 * executed, as it calls the callback function that (for the
476 * jit) sets the lmf marker.
478 mono_thread_new_init (tid, &tid, start_func);
479 thread->stack_ptr = &tid;
481 LIBGC_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT",%d) Setting thread stack to %p", __func__, GetCurrentThreadId (), getpid (), thread->stack_ptr));
483 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
485 mono_profiler_thread_start (tid);
487 /* On 2.0 profile (and higher), set explicitly since state might have been
489 if (mono_get_runtime_info ()->framework_version [0] != '1') {
490 if (thread->apartment_state == ThreadApartmentState_Unknown)
491 thread->apartment_state = ThreadApartmentState_MTA;
494 mono_thread_init_apartment_state ();
496 if(thread->start_notify!=NULL) {
497 /* Let the thread that called Start() know we're
500 ReleaseSemaphore (thread->start_notify, 1, NULL);
505 thread_adjust_static_data (thread);
507 g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
511 /* start_func is set only for unmanaged start functions */
513 start_func (start_arg);
516 g_assert (start_delegate != NULL);
517 args [0] = start_arg;
518 /* we may want to handle the exception here. See comment below on unhandled exceptions */
519 mono_runtime_delegate_invoke (start_delegate, args, NULL);
522 /* If the thread calls ExitThread at all, this remaining code
523 * will not be executed, but the main thread will eventually
524 * call thread_cleanup() on this thread's behalf.
527 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper terminating", __func__, GetCurrentThreadId ()));
529 thread_cleanup (thread);
531 /* Do any cleanup needed for apartment state. This
532 * cannot be done in thread_cleanup since thread_cleanup could be
533 * called for a thread other than the current thread.
534 * mono_thread_cleanup_apartment_state cleans up apartment
535 * for the current thead */
536 mono_thread_cleanup_apartment_state ();
538 /* Remove the reference to the thread object in the TLS data,
539 * so the thread object can be finalized. This won't be
540 * reached if the thread threw an uncaught exception, so those
541 * thread handles will stay referenced :-( (This is due to
542 * missing support for scanning thread-specific data in the
543 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
546 SET_CURRENT_OBJECT (NULL);
551 void mono_thread_new_init (gsize tid, gpointer stack_start, gpointer func)
553 if (mono_thread_start_cb) {
554 mono_thread_start_cb (tid, stack_start, func);
558 void mono_threads_set_default_stacksize (guint32 stacksize)
560 default_stacksize = stacksize;
563 guint32 mono_threads_get_default_stacksize (void)
565 return default_stacksize;
568 void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
571 HANDLE thread_handle;
572 struct StartInfo *start_info;
575 thread=(MonoThread *)mono_object_new (domain,
576 mono_defaults.thread_class);
578 start_info=g_new0 (struct StartInfo, 1);
579 start_info->func = func;
580 start_info->obj = thread;
581 start_info->domain = domain;
582 start_info->start_arg = arg;
584 /* Create suspended, so we can do some housekeeping before the thread
587 thread_handle = CreateThread(NULL, default_stacksize_for_thread (thread), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
588 CREATE_SUSPENDED, &tid);
589 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
590 if (thread_handle == NULL) {
591 /* The thread couldn't be created, so throw an exception */
592 mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
596 thread->handle=thread_handle;
598 thread->apartment_state=ThreadApartmentState_Unknown;
599 small_id_alloc (thread);
601 MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
603 handle_store(thread);
605 ResumeThread (thread_handle);
609 * mono_thread_get_stack_bounds:
611 * Return the address and size of the current threads stack. Return NULL as the stack
612 * address if the stack address cannot be determined.
615 mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
617 #ifndef PLATFORM_WIN32
619 guint8 *current = (guint8*)&attr;
621 pthread_attr_init (&attr);
622 #ifdef HAVE_PTHREAD_GETATTR_NP
623 pthread_getattr_np (pthread_self(), &attr);
625 #ifdef HAVE_PTHREAD_ATTR_GET_NP
626 pthread_attr_get_np (pthread_self(), &attr);
629 pthread_attr_getstacksize (&attr, &stsize);
638 pthread_attr_getstack (&attr, (void**)staddr, stsize);
640 g_assert ((current > *staddr) && (current < *staddr + *stsize));
643 pthread_attr_destroy (&attr);
648 mono_thread_attach (MonoDomain *domain)
651 HANDLE thread_handle;
654 if ((thread = mono_thread_current ())) {
655 if (domain != mono_domain_get ())
656 mono_domain_set (domain, TRUE);
657 /* Already attached */
661 if (!mono_gc_register_thread (&domain)) {
662 g_error ("Thread %"G_GSIZE_FORMAT" calling into managed code is not registered with the GC. On UNIX, this can be fixed by #include-ing <gc.h> before <pthread.h> in the file containing the thread creation code.", GetCurrentThreadId ());
665 thread = (MonoThread *)mono_object_new (domain,
666 mono_defaults.thread_class);
668 thread_handle = GetCurrentThread ();
669 g_assert (thread_handle);
671 tid=GetCurrentThreadId ();
674 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
675 * refer to the thread from other threads for things like aborting.
677 DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
678 THREAD_ALL_ACCESS, TRUE, 0);
680 thread->handle=thread_handle;
682 thread->apartment_state=ThreadApartmentState_Unknown;
683 small_id_alloc (thread);
684 thread->stack_ptr = &tid;
685 MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
687 THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
689 handle_store(thread);
691 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
693 SET_CURRENT_OBJECT (thread);
694 mono_domain_set (domain, TRUE);
696 thread_adjust_static_data (thread);
698 if (mono_thread_attach_cb) {
702 mono_thread_get_stack_bounds (&staddr, &stsize);
705 mono_thread_attach_cb (tid, &tid);
707 mono_thread_attach_cb (tid, staddr + stsize);
714 mono_thread_detach (MonoThread *thread)
716 g_return_if_fail (thread != NULL);
718 THREAD_DEBUG (g_message ("%s: mono_thread_detach for %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
720 thread_cleanup (thread);
722 SET_CURRENT_OBJECT (NULL);
724 /* Don't need to CloseHandle this thread, even though we took a
725 * reference in mono_thread_attach (), because the GC will do it
726 * when the Thread object is finalised.
733 MonoThread *thread = mono_thread_current ();
735 thread_cleanup (thread);
736 SET_CURRENT_OBJECT (NULL);
738 /* we could add a callback here for embedders to use. */
739 if (thread == mono_thread_get_main ())
740 exit (mono_environment_exitcode_get ());
744 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
747 guint32 (*start_func)(void *);
748 struct StartInfo *start_info;
754 THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__, this, start));
756 mono_monitor_enter (this->synch_lock);
758 if ((this->state & ThreadState_Unstarted) == 0) {
759 mono_monitor_exit (this->synch_lock);
760 mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
766 if ((this->state & ThreadState_Aborted) != 0) {
767 mono_monitor_exit (this->synch_lock);
772 /* This is freed in start_wrapper */
773 start_info = g_new0 (struct StartInfo, 1);
774 start_info->func = start_func;
775 start_info->start_arg = this->start_obj; /* FIXME: GC object stored in unmanaged memory */
776 start_info->delegate = start;
777 start_info->obj = this;
778 start_info->domain = mono_domain_get ();
780 this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
781 if(this->start_notify==NULL) {
782 mono_monitor_exit (this->synch_lock);
783 g_warning ("%s: CreateSemaphore error 0x%x", __func__, GetLastError ());
787 thread=CreateThread(NULL, default_stacksize_for_thread (this), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
788 CREATE_SUSPENDED, &tid);
790 mono_monitor_exit (this->synch_lock);
791 g_warning("%s: CreateThread error 0x%x", __func__, GetLastError());
797 small_id_alloc (this);
799 /* Don't call handle_store() here, delay it to Start.
800 * We can't join a thread (trying to will just block
801 * forever) until it actually starts running, so don't
802 * store the handle till then.
805 mono_thread_start (this);
807 this->state &= ~ThreadState_Unstarted;
809 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread));
811 mono_monitor_exit (this->synch_lock);
816 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
821 THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
823 CloseHandle (thread);
826 static void mono_thread_start (MonoThread *thread)
830 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
832 /* Only store the handle when the thread is about to be
833 * launched, to avoid the main thread deadlocking while trying
834 * to clean up a thread that will never be signalled.
836 handle_store (thread);
838 ResumeThread (thread->handle);
840 if(thread->start_notify!=NULL) {
841 /* Wait for the thread to set up its TLS data etc, so
842 * theres no potential race condition if someone tries
843 * to look up the data believing the thread has
847 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for thread %p (%"G_GSIZE_FORMAT") to start", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
849 WaitForSingleObjectEx (thread->start_notify, INFINITE, FALSE);
850 CloseHandle (thread->start_notify);
851 thread->start_notify = NULL;
854 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Done launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
857 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
859 MonoThread *thread = mono_thread_current ();
863 THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
865 mono_thread_current_check_pending_interrupt ();
867 mono_monitor_enter (thread->synch_lock);
868 thread->state |= ThreadState_WaitSleepJoin;
869 mono_monitor_exit (thread->synch_lock);
873 mono_monitor_enter (thread->synch_lock);
874 thread->state &= ~ThreadState_WaitSleepJoin;
875 mono_monitor_exit (thread->synch_lock);
878 void ves_icall_System_Threading_Thread_SpinWait_internal (gint32 iterations)
882 for(i = 0; i < iterations; i++) {
883 /* We're busy waiting, but at least we can tell the
884 * scheduler to let someone else have a go...
891 ves_icall_System_Threading_Thread_GetDomainID (void)
895 return mono_domain_get()->domain_id;
899 ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
902 mono_monitor_enter (this_obj->synch_lock);
907 str = mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
909 mono_monitor_exit (this_obj->synch_lock);
914 ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
916 mono_monitor_enter (this_obj->synch_lock);
918 if (this_obj->name) {
919 mono_monitor_exit (this_obj->synch_lock);
920 mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
924 this_obj->name = g_new (gunichar2, mono_string_length (name));
925 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
926 this_obj->name_len = mono_string_length (name);
929 this_obj->name = NULL;
931 mono_monitor_exit (this_obj->synch_lock);
935 lookup_cached_culture (MonoThread *this, MonoDomain *domain, int start_idx)
940 if (this->cached_culture_info) {
941 domain = mono_domain_get ();
942 for (i = start_idx; i < start_idx + NUM_CACHED_CULTURES; ++i) {
943 res = mono_array_get (this->cached_culture_info, MonoObject*, i);
944 if (res && res->vtable->domain == domain)
953 ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoThread *this)
955 return lookup_cached_culture (this, mono_domain_get (), CULTURES_START_IDX);
959 ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread *this)
963 mono_monitor_enter (this->synch_lock);
964 if (this->serialized_culture_info) {
965 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_culture_info_len);
966 memcpy (mono_array_addr (res, guint8, 0), this->serialized_culture_info, this->serialized_culture_info_len);
970 mono_monitor_exit (this->synch_lock);
976 cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
979 MonoDomain *domain = mono_domain_get ();
982 int same_domain_slot = -1;
984 mono_monitor_enter (this->synch_lock);
985 if (!this->cached_culture_info)
986 this->cached_culture_info = mono_array_new (mono_object_domain (this), mono_defaults.object_class, NUM_CACHED_CULTURES * 2);
988 for (i = start_idx; i < start_idx + NUM_CACHED_CULTURES; ++i) {
989 obj = mono_array_get (this->cached_culture_info, MonoObject*, i);
993 /* we continue, because there may be a slot used with the same domain */
997 if (obj->vtable->domain == domain) {
998 same_domain_slot = i;
1002 if (same_domain_slot >= 0)
1003 mono_array_setref (this->cached_culture_info, same_domain_slot, culture);
1004 else if (free_slot >= 0)
1005 mono_array_setref (this->cached_culture_info, free_slot, culture);
1006 /* we may want to replace an existing entry here, even when no suitable slot is found */
1007 mono_monitor_exit (this->synch_lock);
1011 ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this, MonoObject *culture)
1013 cache_culture (this, culture, CULTURES_START_IDX);
1017 ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread *this, MonoArray *arr)
1019 mono_monitor_enter (this->synch_lock);
1020 if (this->serialized_culture_info)
1021 g_free (this->serialized_culture_info);
1022 this->serialized_culture_info = g_new0 (guint8, mono_array_length (arr));
1023 this->serialized_culture_info_len = mono_array_length (arr);
1024 memcpy (this->serialized_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
1025 mono_monitor_exit (this->synch_lock);
1030 ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoThread *this)
1032 return lookup_cached_culture (this, mono_domain_get (), UICULTURES_START_IDX);
1036 ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread *this)
1040 mono_monitor_enter (this->synch_lock);
1041 if (this->serialized_ui_culture_info) {
1042 res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_ui_culture_info_len);
1043 memcpy (mono_array_addr (res, guint8, 0), this->serialized_ui_culture_info, this->serialized_ui_culture_info_len);
1047 mono_monitor_exit (this->synch_lock);
1053 ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this, MonoObject *culture)
1055 cache_culture (this, culture, UICULTURES_START_IDX);
1059 ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread *this, MonoArray *arr)
1061 mono_monitor_enter (this->synch_lock);
1062 if (this->serialized_ui_culture_info)
1063 g_free (this->serialized_ui_culture_info);
1064 this->serialized_ui_culture_info = g_new0 (guint8, mono_array_length (arr));
1065 this->serialized_ui_culture_info_len = mono_array_length (arr);
1066 memcpy (this->serialized_ui_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
1067 mono_monitor_exit (this->synch_lock);
1070 /* the jit may read the compiled code of this function */
1072 mono_thread_current (void)
1074 THREAD_DEBUG (g_message ("%s: returning %p", __func__, GET_CURRENT_OBJECT ()));
1075 return GET_CURRENT_OBJECT ();
1078 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
1079 int ms, HANDLE thread)
1083 MONO_ARCH_SAVE_REGS;
1085 mono_monitor_enter (this->synch_lock);
1087 if ((this->state & ThreadState_Unstarted) != 0) {
1088 mono_monitor_exit (this->synch_lock);
1089 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
1093 mono_thread_current_check_pending_interrupt ();
1095 this->state |= ThreadState_WaitSleepJoin;
1096 mono_monitor_exit (this->synch_lock);
1101 THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__, thread, ms));
1103 ret=WaitForSingleObjectEx (thread, ms, TRUE);
1105 mono_monitor_enter (this->synch_lock);
1106 this->state &= ~ThreadState_WaitSleepJoin;
1107 mono_monitor_exit (this->synch_lock);
1109 if(ret==WAIT_OBJECT_0) {
1110 THREAD_DEBUG (g_message ("%s: join successful", __func__));
1115 THREAD_DEBUG (g_message ("%s: join failed", __func__));
1120 /* FIXME: exitContext isnt documented */
1121 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
1127 MonoObject *waitHandle;
1128 MonoThread *thread = mono_thread_current ();
1130 MONO_ARCH_SAVE_REGS;
1132 /* Do this WaitSleepJoin check before creating objects */
1133 mono_thread_current_check_pending_interrupt ();
1135 numhandles = mono_array_length(mono_handles);
1136 handles = g_new0(HANDLE, numhandles);
1138 for(i = 0; i < numhandles; i++) {
1139 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
1140 handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
1147 mono_monitor_enter (thread->synch_lock);
1148 thread->state |= ThreadState_WaitSleepJoin;
1149 mono_monitor_exit (thread->synch_lock);
1151 ret=WaitForMultipleObjectsEx(numhandles, handles, TRUE, ms, TRUE);
1153 mono_monitor_enter (thread->synch_lock);
1154 thread->state &= ~ThreadState_WaitSleepJoin;
1155 mono_monitor_exit (thread->synch_lock);
1159 if(ret==WAIT_FAILED) {
1160 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1162 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
1163 /* Do we want to try again if we get
1164 * WAIT_IO_COMPLETION? The documentation for
1165 * WaitHandle doesn't give any clues. (We'd have to
1166 * fiddle with the timeout if we retry.)
1168 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1175 /* FIXME: exitContext isnt documented */
1176 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
1182 MonoObject *waitHandle;
1183 MonoThread *thread = mono_thread_current ();
1185 MONO_ARCH_SAVE_REGS;
1187 /* Do this WaitSleepJoin check before creating objects */
1188 mono_thread_current_check_pending_interrupt ();
1190 numhandles = mono_array_length(mono_handles);
1191 handles = g_new0(HANDLE, numhandles);
1193 for(i = 0; i < numhandles; i++) {
1194 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
1195 handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
1202 mono_monitor_enter (thread->synch_lock);
1203 thread->state |= ThreadState_WaitSleepJoin;
1204 mono_monitor_exit (thread->synch_lock);
1206 ret=WaitForMultipleObjectsEx(numhandles, handles, FALSE, ms, TRUE);
1208 mono_monitor_enter (thread->synch_lock);
1209 thread->state &= ~ThreadState_WaitSleepJoin;
1210 mono_monitor_exit (thread->synch_lock);
1214 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") returning %d", __func__, GetCurrentThreadId (), ret));
1217 * These need to be here. See MSDN dos on WaitForMultipleObjects.
1219 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
1220 return ret - WAIT_OBJECT_0;
1222 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
1223 return ret - WAIT_ABANDONED_0;
1230 /* FIXME: exitContext isnt documented */
1231 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
1234 MonoThread *thread = mono_thread_current ();
1236 MONO_ARCH_SAVE_REGS;
1238 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for %p, %d ms", __func__, GetCurrentThreadId (), handle, ms));
1244 mono_thread_current_check_pending_interrupt ();
1246 mono_monitor_enter (thread->synch_lock);
1247 thread->state |= ThreadState_WaitSleepJoin;
1248 mono_monitor_exit (thread->synch_lock);
1250 ret=WaitForSingleObjectEx (handle, ms, TRUE);
1252 mono_monitor_enter (thread->synch_lock);
1253 thread->state &= ~ThreadState_WaitSleepJoin;
1254 mono_monitor_exit (thread->synch_lock);
1256 if(ret==WAIT_FAILED) {
1257 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
1259 } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
1260 /* Do we want to try again if we get
1261 * WAIT_IO_COMPLETION? The documentation for
1262 * WaitHandle doesn't give any clues. (We'd have to
1263 * fiddle with the timeout if we retry.)
1265 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
1272 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
1276 MONO_ARCH_SAVE_REGS;
1281 mutex = CreateMutex (NULL, owned, NULL);
1283 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
1285 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1293 MonoBoolean ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
1294 MONO_ARCH_SAVE_REGS;
1296 return(ReleaseMutex (handle));
1299 HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name,
1305 MONO_ARCH_SAVE_REGS;
1307 *error = ERROR_SUCCESS;
1309 ret = OpenMutex (rights, FALSE, mono_string_chars (name));
1311 *error = GetLastError ();
1318 HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created)
1322 MONO_ARCH_SAVE_REGS;
1327 sem = CreateSemaphore (NULL, initialCount, maximumCount, NULL);
1329 sem = CreateSemaphore (NULL, initialCount, maximumCount,
1330 mono_string_chars (name));
1332 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1340 gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail)
1344 MONO_ARCH_SAVE_REGS;
1346 *fail = !ReleaseSemaphore (handle, releaseCount, &prevcount);
1351 HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
1355 MONO_ARCH_SAVE_REGS;
1357 *error = ERROR_SUCCESS;
1359 ret = OpenSemaphore (rights, FALSE, mono_string_chars (name));
1361 *error = GetLastError ();
1367 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created)
1371 MONO_ARCH_SAVE_REGS;
1376 event = CreateEvent (NULL, manual, initial, NULL);
1378 event = CreateEvent (NULL, manual, initial,
1379 mono_string_chars (name));
1381 if (GetLastError () == ERROR_ALREADY_EXISTS) {
1389 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
1390 MONO_ARCH_SAVE_REGS;
1392 return (SetEvent(handle));
1395 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
1396 MONO_ARCH_SAVE_REGS;
1398 return (ResetEvent(handle));
1402 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
1403 MONO_ARCH_SAVE_REGS;
1405 CloseHandle (handle);
1408 HANDLE ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name,
1414 MONO_ARCH_SAVE_REGS;
1416 *error = ERROR_SUCCESS;
1418 ret = OpenEvent (rights, FALSE, mono_string_chars (name));
1420 *error = GetLastError ();
1426 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
1428 MONO_ARCH_SAVE_REGS;
1430 return InterlockedIncrement (location);
1433 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
1437 MONO_ARCH_SAVE_REGS;
1439 mono_interlocked_lock ();
1443 mono_interlocked_unlock ();
1449 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
1451 MONO_ARCH_SAVE_REGS;
1453 return InterlockedDecrement(location);
1456 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
1460 MONO_ARCH_SAVE_REGS;
1462 mono_interlocked_lock ();
1466 mono_interlocked_unlock ();
1471 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location, gint32 value)
1473 MONO_ARCH_SAVE_REGS;
1475 return InterlockedExchange(location, value);
1478 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location, MonoObject *value)
1480 MONO_ARCH_SAVE_REGS;
1482 return (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
1485 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location, gfloat value)
1487 IntFloatUnion val, ret;
1489 MONO_ARCH_SAVE_REGS;
1492 ret.ival = InterlockedExchange((gint32 *) location, val.ival);
1498 ves_icall_System_Threading_Interlocked_Exchange_Long (gint64 *location, gint64 value)
1500 #if SIZEOF_VOID_P == 8
1501 return (gint64) InterlockedExchangePointer((gpointer *) location, (gpointer)value);
1506 * According to MSDN, this function is only atomic with regards to the
1507 * other Interlocked functions on 32 bit platforms.
1509 mono_interlocked_lock ();
1512 mono_interlocked_unlock ();
1519 ves_icall_System_Threading_Interlocked_Exchange_Double (gdouble *location, gdouble value)
1521 #if SIZEOF_VOID_P == 8
1522 LongDoubleUnion val, ret;
1525 ret.ival = (gint64)InterlockedExchangePointer((gpointer *) location, (gpointer)val.ival);
1532 * According to MSDN, this function is only atomic with regards to the
1533 * other Interlocked functions on 32 bit platforms.
1535 mono_interlocked_lock ();
1538 mono_interlocked_unlock ();
1544 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand)
1546 MONO_ARCH_SAVE_REGS;
1548 return InterlockedCompareExchange(location, value, comparand);
1551 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location, MonoObject *value, MonoObject *comparand)
1553 MONO_ARCH_SAVE_REGS;
1555 return (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
1558 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location, gfloat value, gfloat comparand)
1560 IntFloatUnion val, ret, cmp;
1562 MONO_ARCH_SAVE_REGS;
1565 cmp.fval = comparand;
1566 ret.ival = InterlockedCompareExchange((gint32 *) location, val.ival, cmp.ival);
1572 ves_icall_System_Threading_Interlocked_CompareExchange_Double (gdouble *location, gdouble value, gdouble comparand)
1574 #if SIZEOF_VOID_P == 8
1575 LongDoubleUnion val, comp, ret;
1578 comp.fval = comparand;
1579 ret.ival = (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)val.ival, (gpointer)comp.ival);
1585 mono_interlocked_lock ();
1587 if (old == comparand)
1589 mono_interlocked_unlock ();
1596 ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64 *location, gint64 value, gint64 comparand)
1598 #if SIZEOF_VOID_P == 8
1599 return (gint64)InterlockedCompareExchangePointer((gpointer *) location, (gpointer)value, (gpointer)comparand);
1603 mono_interlocked_lock ();
1605 if (old == comparand)
1607 mono_interlocked_unlock ();
1614 ves_icall_System_Threading_Interlocked_CompareExchange_T (MonoObject **location, MonoObject *value, MonoObject *comparand)
1616 MONO_ARCH_SAVE_REGS;
1618 return InterlockedCompareExchangePointer ((gpointer *)location, value, comparand);
1622 ves_icall_System_Threading_Interlocked_Exchange_T (MonoObject **location, MonoObject *value)
1624 MONO_ARCH_SAVE_REGS;
1626 return InterlockedExchangePointer ((gpointer *)location, value);
1630 ves_icall_System_Threading_Interlocked_Add_Int (gint32 *location, gint32 value)
1632 #if SIZEOF_VOID_P == 8
1633 /* Should be implemented as a JIT intrinsic */
1634 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1639 mono_interlocked_lock ();
1641 *location = orig + value;
1642 mono_interlocked_unlock ();
1644 return orig + value;
1649 ves_icall_System_Threading_Interlocked_Add_Long (gint64 *location, gint64 value)
1651 #if SIZEOF_VOID_P == 8
1652 /* Should be implemented as a JIT intrinsic */
1653 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1658 mono_interlocked_lock ();
1660 *location = orig + value;
1661 mono_interlocked_unlock ();
1663 return orig + value;
1668 ves_icall_System_Threading_Interlocked_Read_Long (gint64 *location)
1670 #if SIZEOF_VOID_P == 8
1671 /* 64 bit reads are already atomic */
1676 mono_interlocked_lock ();
1678 mono_interlocked_unlock ();
1685 ves_icall_System_Threading_Thread_MemoryBarrier (void)
1687 mono_threads_lock ();
1688 mono_threads_unlock ();
1692 ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
1694 mono_monitor_enter (this->synch_lock);
1695 this->state &= ~state;
1696 if (state & ThreadState_Background) {
1697 /* If the thread changes the background mode, the main thread has to
1698 * be notified, since it has to rebuild the list of threads to
1701 SetEvent (background_change_event);
1703 mono_monitor_exit (this->synch_lock);
1707 ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
1709 mono_monitor_enter (this->synch_lock);
1710 this->state |= state;
1711 if (state & ThreadState_Background) {
1712 /* If the thread changes the background mode, the main thread has to
1713 * be notified, since it has to rebuild the list of threads to
1716 SetEvent (background_change_event);
1718 mono_monitor_exit (this->synch_lock);
1722 ves_icall_System_Threading_Thread_GetState (MonoThread* this)
1725 mono_monitor_enter (this->synch_lock);
1726 state = this->state;
1727 mono_monitor_exit (this->synch_lock);
1731 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this)
1733 gboolean throw = FALSE;
1735 mono_monitor_enter (this->synch_lock);
1737 /* Clear out any previous request */
1738 this->thread_interrupt_requested = FALSE;
1740 if (this->state & ThreadState_WaitSleepJoin) {
1743 this->thread_interrupt_requested = TRUE;
1746 mono_monitor_exit (this->synch_lock);
1749 signal_thread_state_change (this);
1753 void mono_thread_current_check_pending_interrupt ()
1755 MonoThread *thread = mono_thread_current ();
1756 gboolean throw = FALSE;
1758 mono_monitor_enter (thread->synch_lock);
1760 if (thread->thread_interrupt_requested) {
1762 thread->thread_interrupt_requested = FALSE;
1765 mono_monitor_exit (thread->synch_lock);
1768 mono_raise_exception (mono_get_exception_thread_interrupted ());
1773 mono_thread_get_abort_signal (void)
1775 #ifdef PLATFORM_WIN32
1781 static int abort_signum = -1;
1783 if (abort_signum != -1)
1784 return abort_signum;
1785 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
1786 for (i = SIGRTMIN + 1; i < SIGRTMAX; ++i) {
1787 struct sigaction sinfo;
1788 sigaction (i, NULL, &sinfo);
1789 if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
1794 /* fallback to the old way */
1797 #endif /* PLATFORM_WIN32 */
1800 #ifdef PLATFORM_WIN32
1801 static void CALLBACK interruption_request_apc (ULONG_PTR param)
1803 MonoException* exc = mono_thread_request_interruption (FALSE);
1804 if (exc) mono_raise_exception (exc);
1806 #endif /* PLATFORM_WIN32 */
1809 * signal_thread_state_change
1811 * Tells the thread that his state has changed and it has to enter the new
1812 * state as soon as possible.
1814 static void signal_thread_state_change (MonoThread *thread)
1816 if (thread == mono_thread_current ()) {
1817 /* Do it synchronously */
1818 MonoException *exc = mono_thread_request_interruption (FALSE);
1820 mono_raise_exception (exc);
1823 #ifdef PLATFORM_WIN32
1824 QueueUserAPC ((PAPCFUNC)interruption_request_apc, thread->handle, NULL);
1826 /* fixme: store the state somewhere */
1827 #ifdef PTHREAD_POINTER_ID
1828 pthread_kill ((gpointer)(gsize)(thread->tid), mono_thread_get_abort_signal ());
1830 pthread_kill (thread->tid, mono_thread_get_abort_signal ());
1832 #endif /* PLATFORM_WIN32 */
1836 ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
1838 MONO_ARCH_SAVE_REGS;
1840 mono_monitor_enter (thread->synch_lock);
1842 if ((thread->state & ThreadState_AbortRequested) != 0 ||
1843 (thread->state & ThreadState_StopRequested) != 0 ||
1844 (thread->state & ThreadState_Stopped) != 0)
1846 mono_monitor_exit (thread->synch_lock);
1850 if ((thread->state & ThreadState_Unstarted) != 0) {
1851 thread->state |= ThreadState_Aborted;
1852 mono_monitor_exit (thread->synch_lock);
1856 thread->state |= ThreadState_AbortRequested;
1857 MONO_OBJECT_SETREF (thread, abort_state, state);
1858 thread->abort_exc = NULL;
1860 mono_monitor_exit (thread->synch_lock);
1862 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Abort requested for %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
1864 /* Make sure the thread is awake */
1865 mono_thread_resume (thread);
1867 signal_thread_state_change (thread);
1871 ves_icall_System_Threading_Thread_ResetAbort (void)
1873 MonoThread *thread = mono_thread_current ();
1875 MONO_ARCH_SAVE_REGS;
1877 mono_monitor_enter (thread->synch_lock);
1879 thread->state &= ~ThreadState_AbortRequested;
1881 if (!thread->abort_exc) {
1882 const char *msg = "Unable to reset abort because no abort was requested";
1883 mono_monitor_exit (thread->synch_lock);
1884 mono_raise_exception (mono_get_exception_thread_state (msg));
1886 thread->abort_exc = NULL;
1887 thread->abort_state = NULL;
1890 mono_monitor_exit (thread->synch_lock);
1894 mono_thread_suspend (MonoThread *thread)
1896 MONO_ARCH_SAVE_REGS;
1898 mono_monitor_enter (thread->synch_lock);
1900 if ((thread->state & ThreadState_Unstarted) != 0 ||
1901 (thread->state & ThreadState_Aborted) != 0 ||
1902 (thread->state & ThreadState_Stopped) != 0)
1904 mono_monitor_exit (thread->synch_lock);
1908 if ((thread->state & ThreadState_Suspended) != 0 ||
1909 (thread->state & ThreadState_SuspendRequested) != 0 ||
1910 (thread->state & ThreadState_StopRequested) != 0)
1912 mono_monitor_exit (thread->synch_lock);
1916 thread->state |= ThreadState_SuspendRequested;
1917 mono_monitor_exit (thread->synch_lock);
1919 signal_thread_state_change (thread);
1924 ves_icall_System_Threading_Thread_Suspend (MonoThread *thread)
1926 if (!mono_thread_suspend (thread))
1927 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1931 mono_thread_resume (MonoThread *thread)
1933 MONO_ARCH_SAVE_REGS;
1935 mono_monitor_enter (thread->synch_lock);
1937 if ((thread->state & ThreadState_SuspendRequested) != 0) {
1938 thread->state &= ~ThreadState_SuspendRequested;
1939 mono_monitor_exit (thread->synch_lock);
1943 if ((thread->state & ThreadState_Suspended) == 0 ||
1944 (thread->state & ThreadState_Unstarted) != 0 ||
1945 (thread->state & ThreadState_Aborted) != 0 ||
1946 (thread->state & ThreadState_Stopped) != 0)
1948 mono_monitor_exit (thread->synch_lock);
1952 thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1953 if (thread->resume_event == NULL) {
1954 mono_monitor_exit (thread->synch_lock);
1958 /* Awake the thread */
1959 SetEvent (thread->suspend_event);
1961 mono_monitor_exit (thread->synch_lock);
1963 /* Wait for the thread to awake */
1964 WaitForSingleObject (thread->resume_event, INFINITE);
1965 CloseHandle (thread->resume_event);
1966 thread->resume_event = NULL;
1972 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
1974 if (!mono_thread_resume (thread))
1975 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1979 find_wrapper (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
1984 if (m->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1985 m->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
1986 m->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH)
1988 *((gboolean*)data) = TRUE;
1995 is_running_protected_wrapper (void)
1997 gboolean found = FALSE;
1998 mono_stack_walk (find_wrapper, &found);
2002 void mono_thread_stop (MonoThread *thread)
2004 mono_monitor_enter (thread->synch_lock);
2006 if ((thread->state & ThreadState_StopRequested) != 0 ||
2007 (thread->state & ThreadState_Stopped) != 0)
2009 mono_monitor_exit (thread->synch_lock);
2013 /* Make sure the thread is awake */
2014 mono_thread_resume (thread);
2016 thread->state |= ThreadState_StopRequested;
2017 thread->state &= ~ThreadState_AbortRequested;
2019 mono_monitor_exit (thread->synch_lock);
2021 signal_thread_state_change (thread);
2025 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
2027 return *((volatile gint8 *) (ptr));
2031 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
2033 return *((volatile gint16 *) (ptr));
2037 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
2039 return *((volatile gint32 *) (ptr));
2043 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
2045 return *((volatile gint64 *) (ptr));
2049 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
2051 return (void *) *((volatile void **) ptr);
2055 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
2057 *((volatile gint8 *) ptr) = value;
2061 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
2063 *((volatile gint16 *) ptr) = value;
2067 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
2069 *((volatile gint32 *) ptr) = value;
2073 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
2075 *((volatile gint64 *) ptr) = value;
2079 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
2081 *((volatile void **) ptr) = value;
2084 void mono_thread_init (MonoThreadStartCB start_cb,
2085 MonoThreadAttachCB attach_cb)
2087 MONO_GC_REGISTER_ROOT (small_id_table);
2088 InitializeCriticalSection(&threads_mutex);
2089 InitializeCriticalSection(&interlocked_mutex);
2090 InitializeCriticalSection(&contexts_mutex);
2091 InitializeCriticalSection(&delayed_free_table_mutex);
2092 InitializeCriticalSection(&small_id_mutex);
2093 background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2094 g_assert(background_change_event != NULL);
2096 mono_init_static_data_info (&thread_static_info);
2097 mono_init_static_data_info (&context_static_info);
2099 current_object_key=TlsAlloc();
2100 THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__, current_object_key));
2102 mono_thread_start_cb = start_cb;
2103 mono_thread_attach_cb = attach_cb;
2105 delayed_free_table = g_array_new (FALSE, FALSE, sizeof (DelayedFreeItem));
2107 /* Get a pseudo handle to the current process. This is just a
2108 * kludge so that wapi can build a process handle if needed.
2109 * As a pseudo handle is returned, we don't need to clean
2112 GetCurrentProcess ();
2115 void mono_thread_cleanup (void)
2117 #if !defined(PLATFORM_WIN32) && !defined(RUN_IN_SUBTHREAD)
2118 /* The main thread must abandon any held mutexes (particularly
2119 * important for named mutexes as they are shared across
2120 * processes, see bug 74680.) This will happen when the
2121 * thread exits, but if it's not running in a subthread it
2122 * won't exit in time.
2124 /* Using non-w32 API is a nasty kludge, but I couldn't find
2125 * anything in the documentation that would let me do this
2126 * here yet still be safe to call on windows.
2128 _wapi_thread_signal_self (mono_environment_exitcode_get ());
2132 /* This stuff needs more testing, it seems one of these
2133 * critical sections can be locked when mono_thread_cleanup is
2136 DeleteCriticalSection (&threads_mutex);
2137 DeleteCriticalSection (&interlocked_mutex);
2138 DeleteCriticalSection (&contexts_mutex);
2139 DeleteCriticalSection (&delayed_free_table_mutex);
2140 DeleteCriticalSection (&small_id_mutex);
2141 CloseHandle (background_change_event);
2144 g_array_free (delayed_free_table, TRUE);
2146 TlsFree (current_object_key);
2150 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
2152 mono_thread_cleanup_fn = func;
2156 static void print_tids (gpointer key, gpointer value, gpointer user)
2158 /* GPOINTER_TO_UINT breaks horribly if sizeof(void *) >
2159 * sizeof(uint) and a cast to uint would overflow
2161 /* Older versions of glib don't have G_GSIZE_FORMAT, so just
2162 * print this as a pointer.
2164 g_message ("Waiting for: %p", key);
2169 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
2170 MonoThread *threads[MAXIMUM_WAIT_OBJECTS];
2174 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
2178 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
2180 ret=WaitForMultipleObjectsEx(wait->num, wait->handles, TRUE, timeout, FALSE);
2182 if(ret==WAIT_FAILED) {
2183 /* See the comment in build_wait_tids() */
2184 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
2188 for(i=0; i<wait->num; i++)
2189 CloseHandle (wait->handles[i]);
2191 if (ret == WAIT_TIMEOUT)
2194 for(i=0; i<wait->num; i++) {
2195 gsize tid = wait->threads[i]->tid;
2197 if(mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
2198 /* This thread must have been killed, because
2199 * it hasn't cleaned itself up. (It's just
2200 * possible that the thread exited before the
2201 * parent thread had a chance to store the
2202 * handle, and now there is another pointer to
2203 * the already-exited thread stored. In this
2204 * case, we'll just get two
2205 * mono_profiler_thread_end() calls for the
2209 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
2210 thread_cleanup (wait->threads[i]);
2215 static void wait_for_tids_or_state_change (struct wait_data *wait, guint32 timeout)
2217 guint32 i, ret, count;
2219 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
2221 /* Add the thread state change event, so it wakes up if a thread changes
2222 * to background mode.
2225 if (count < MAXIMUM_WAIT_OBJECTS) {
2226 wait->handles [count] = background_change_event;
2230 ret=WaitForMultipleObjectsEx (count, wait->handles, FALSE, timeout, FALSE);
2232 if(ret==WAIT_FAILED) {
2233 /* See the comment in build_wait_tids() */
2234 THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
2238 for(i=0; i<wait->num; i++)
2239 CloseHandle (wait->handles[i]);
2241 if (ret == WAIT_TIMEOUT)
2244 if (ret < wait->num) {
2245 gsize tid = wait->threads[ret]->tid;
2246 mono_threads_lock ();
2247 if (mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
2248 /* See comment in wait_for_tids about thread cleanup */
2249 mono_threads_unlock ();
2250 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
2251 thread_cleanup (wait->threads [ret]);
2253 mono_threads_unlock ();
2257 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
2259 struct wait_data *wait=(struct wait_data *)user;
2261 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
2263 MonoThread *thread=(MonoThread *)value;
2265 /* Ignore background threads, we abort them later */
2266 /* Do not lock here since it is not needed and the caller holds threads_lock */
2267 if (thread->state & ThreadState_Background) {
2268 THREAD_DEBUG (g_message ("%s: ignoring background thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2269 return; /* just leave, ignore */
2272 if (mono_gc_is_finalizer_thread (thread)) {
2273 THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2277 if (thread == mono_thread_current ()) {
2278 THREAD_DEBUG (g_message ("%s: ignoring current thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2282 if (thread == mono_thread_get_main ()) {
2283 THREAD_DEBUG (g_message ("%s: ignoring main thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2287 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2288 if (handle == NULL) {
2289 THREAD_DEBUG (g_message ("%s: ignoring unopenable thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2293 wait->handles[wait->num]=handle;
2294 wait->threads[wait->num]=thread;
2297 THREAD_DEBUG (g_message ("%s: adding thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
2299 /* Just ignore the rest, we can't do anything with
2306 remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
2308 struct wait_data *wait=(struct wait_data *)user;
2309 gsize self = GetCurrentThreadId ();
2310 MonoThread *thread = (MonoThread *) value;
2313 if (wait->num >= MAXIMUM_WAIT_OBJECTS)
2316 /* The finalizer thread is not a background thread */
2317 if (thread->tid != self && (thread->state & ThreadState_Background) != 0) {
2319 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2323 if(thread->state & ThreadState_AbortRequested ||
2324 thread->state & ThreadState_Aborted) {
2325 THREAD_DEBUG (g_message ("%s: Thread id %"G_GSIZE_FORMAT" already aborting", __func__, (gsize)thread->tid));
2329 /* printf ("A: %d\n", wait->num); */
2330 wait->handles[wait->num]=thread->handle;
2331 wait->threads[wait->num]=thread;
2334 THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT"\n", __func__, (gsize)thread->tid));
2335 mono_thread_stop (thread);
2339 return (thread->tid != self && !mono_gc_is_finalizer_thread (thread));
2342 void mono_thread_manage (void)
2344 struct wait_data *wait=g_new0 (struct wait_data, 1);
2346 /* join each thread that's still running */
2347 THREAD_DEBUG (g_message ("%s: Joining each running thread...", __func__));
2349 mono_threads_lock ();
2351 THREAD_DEBUG (g_message("%s: No threads", __func__));
2352 mono_threads_unlock ();
2355 mono_threads_unlock ();
2358 mono_threads_lock ();
2359 THREAD_DEBUG (g_message ("%s: There are %d threads to join", __func__, mono_g_hash_table_size (threads));
2360 mono_g_hash_table_foreach (threads, print_tids, NULL));
2362 ResetEvent (background_change_event);
2364 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
2365 mono_threads_unlock ();
2367 /* Something to wait for */
2368 wait_for_tids_or_state_change (wait, INFINITE);
2370 THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
2371 } while(wait->num>0);
2373 mono_runtime_set_shutting_down ();
2375 THREAD_DEBUG (g_message ("%s: threadpool cleanup", __func__));
2376 mono_thread_pool_cleanup ();
2379 * Remove everything but the finalizer thread and self.
2380 * Also abort all the background threads
2383 mono_threads_lock ();
2386 mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
2388 mono_threads_unlock ();
2390 THREAD_DEBUG (g_message ("%s: wait->num is now %d", __func__, wait->num));
2392 /* Something to wait for */
2393 wait_for_tids (wait, INFINITE);
2395 } while (wait->num > 0);
2398 * give the subthreads a chance to really quit (this is mainly needed
2399 * to get correct user and system times from getrusage/wait/time(1)).
2400 * This could be removed if we avoid pthread_detach() and use pthread_join().
2402 #ifndef PLATFORM_WIN32
2409 static void terminate_thread (gpointer key, gpointer value, gpointer user)
2411 MonoThread *thread=(MonoThread *)value;
2413 if(thread->tid != (gsize)user) {
2414 /*TerminateThread (thread->handle, -1);*/
2418 void mono_thread_abort_all_other_threads (void)
2420 gsize self = GetCurrentThreadId ();
2422 mono_threads_lock ();
2423 THREAD_DEBUG (g_message ("%s: There are %d threads to abort", __func__,
2424 mono_g_hash_table_size (threads));
2425 mono_g_hash_table_foreach (threads, print_tids, NULL));
2427 mono_g_hash_table_foreach (threads, terminate_thread, (gpointer)self);
2429 mono_threads_unlock ();
2433 collect_threads (gpointer key, gpointer value, gpointer user_data)
2435 MonoThread *thread = (MonoThread*)value;
2436 struct wait_data *wait = (struct wait_data*)user_data;
2439 if (wait->num<MAXIMUM_WAIT_OBJECTS) {
2440 handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2444 wait->handles [wait->num] = handle;
2445 wait->threads [wait->num] = thread;
2451 * mono_thread_suspend_all_other_threads:
2453 * Suspend all managed threads except the finalizer thread and this thread.
2455 void mono_thread_suspend_all_other_threads (void)
2457 struct wait_data *wait = g_new0 (struct wait_data, 1);
2459 gsize self = GetCurrentThreadId ();
2461 guint32 eventidx = 0;
2464 * Make a copy of the hashtable since we can't do anything with
2465 * threads while threads_mutex is held.
2467 mono_threads_lock ();
2468 mono_g_hash_table_foreach (threads, collect_threads, wait);
2469 mono_threads_unlock ();
2471 events = g_new0 (gpointer, wait->num);
2473 /* Get the suspended events that we'll be waiting for */
2474 for (i = 0; i < wait->num; ++i) {
2475 MonoThread *thread = wait->threads [i];
2477 if ((thread->tid == self) || mono_gc_is_finalizer_thread (thread)) {
2478 //CloseHandle (wait->handles [i]);
2479 wait->threads [i] = NULL; /* ignore this thread in next loop */
2483 mono_monitor_enter (thread->synch_lock);
2485 if ((thread->state & ThreadState_Suspended) != 0 ||
2486 (thread->state & ThreadState_SuspendRequested) != 0 ||
2487 (thread->state & ThreadState_StopRequested) != 0 ||
2488 (thread->state & ThreadState_Stopped) != 0) {
2489 mono_monitor_exit (thread->synch_lock);
2490 CloseHandle (wait->handles [i]);
2491 wait->threads [i] = NULL; /* ignore this thread in next loop */
2495 /* Convert abort requests into suspend requests */
2496 if ((thread->state & ThreadState_AbortRequested) != 0)
2497 thread->state &= ~ThreadState_AbortRequested;
2499 thread->state |= ThreadState_SuspendRequested;
2501 if (thread->suspended_event == NULL) {
2502 thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
2503 if (thread->suspended_event == NULL) {
2504 /* Forget this one and go on to the next */
2505 mono_monitor_exit (thread->synch_lock);
2510 events [eventidx++] = thread->suspended_event;
2511 mono_monitor_exit (thread->synch_lock);
2513 /* Signal the thread to suspend */
2514 signal_thread_state_change (thread);
2517 WaitForMultipleObjectsEx (eventidx, events, TRUE, INFINITE, FALSE);
2518 for (i = 0; i < wait->num; ++i) {
2519 MonoThread *thread = wait->threads [i];
2524 mono_monitor_enter (thread->synch_lock);
2525 CloseHandle (thread->suspended_event);
2526 thread->suspended_event = NULL;
2527 mono_monitor_exit (thread->synch_lock);
2535 * mono_threads_request_thread_dump:
2537 * Ask all threads except the current to print their stacktrace to stdout.
2540 mono_threads_request_thread_dump (void)
2542 struct wait_data *wait = g_new0 (struct wait_data, 1);
2546 * Make a copy of the hashtable since we can't do anything with
2547 * threads while threads_mutex is held.
2549 mono_threads_lock ();
2550 mono_g_hash_table_foreach (threads, collect_threads, wait);
2551 mono_threads_unlock ();
2553 for (i = 0; i < wait->num; ++i) {
2554 MonoThread *thread = wait->threads [i];
2556 if (!mono_gc_is_finalizer_thread (thread) && (thread != mono_thread_current ()) && !thread->thread_dump_requested) {
2557 thread->thread_dump_requested = TRUE;
2559 signal_thread_state_change (thread);
2562 CloseHandle (wait->handles [i]);
2567 * mono_thread_push_appdomain_ref:
2569 * Register that the current thread may have references to objects in domain
2570 * @domain on its stack. Each call to this function should be paired with a
2571 * call to pop_appdomain_ref.
2574 mono_thread_push_appdomain_ref (MonoDomain *domain)
2576 MonoThread *thread = mono_thread_current ();
2579 /* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
2580 mono_threads_lock ();
2581 thread->appdomain_refs = g_slist_prepend (thread->appdomain_refs, domain);
2582 mono_threads_unlock ();
2587 mono_thread_pop_appdomain_ref (void)
2589 MonoThread *thread = mono_thread_current ();
2592 /* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
2593 mono_threads_lock ();
2594 /* FIXME: How can the list be empty ? */
2595 if (thread->appdomain_refs)
2596 thread->appdomain_refs = g_slist_remove (thread->appdomain_refs, thread->appdomain_refs->data);
2597 mono_threads_unlock ();
2602 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
2605 mono_threads_lock ();
2606 res = g_slist_find (thread->appdomain_refs, domain) != NULL;
2607 mono_threads_unlock ();
2611 typedef struct abort_appdomain_data {
2612 struct wait_data wait;
2614 } abort_appdomain_data;
2617 abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
2619 MonoThread *thread = (MonoThread*)value;
2620 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
2621 MonoDomain *domain = data->domain;
2623 if (mono_thread_has_appdomain_ref (thread, domain)) {
2624 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
2626 ves_icall_System_Threading_Thread_Abort (thread, NULL);
2628 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
2629 HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
2632 data->wait.handles [data->wait.num] = handle;
2633 data->wait.threads [data->wait.num] = thread;
2636 /* Just ignore the rest, we can't do anything with
2644 * mono_threads_abort_appdomain_threads:
2646 * Abort threads which has references to the given appdomain.
2649 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
2651 abort_appdomain_data user_data;
2654 /* printf ("ABORT BEGIN.\n"); */
2656 start_time = GetTickCount ();
2658 mono_threads_lock ();
2660 user_data.domain = domain;
2661 user_data.wait.num = 0;
2662 mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
2663 mono_threads_unlock ();
2665 if (user_data.wait.num > 0)
2667 * We should wait for the threads either to abort, or to leave the
2668 * domain. We can't do the latter, so we wait with a timeout.
2670 wait_for_tids (&user_data.wait, 100);
2672 /* Update remaining time */
2673 timeout -= GetTickCount () - start_time;
2674 start_time = GetTickCount ();
2679 while (user_data.wait.num > 0);
2681 /* printf ("ABORT DONE.\n"); */
2687 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
2689 MonoThread *thread = (MonoThread*)value;
2690 MonoDomain *domain = (MonoDomain*)user_data;
2693 /* No locking needed here */
2694 /* FIXME: why no locking? writes to the cache are protected with synch_lock above */
2696 if (thread->cached_culture_info) {
2697 for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i) {
2698 MonoObject *obj = mono_array_get (thread->cached_culture_info, MonoObject*, i);
2699 if (obj && obj->vtable->domain == domain)
2700 mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
2706 * mono_threads_clear_cached_culture:
2708 * Clear the cached_current_culture from all threads if it is in the
2712 mono_threads_clear_cached_culture (MonoDomain *domain)
2714 mono_threads_lock ();
2715 mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
2716 mono_threads_unlock ();
2720 * mono_thread_get_pending_exception:
2722 * Return an exception which needs to be raised when leaving a catch clause.
2723 * This is used for undeniable exception propagation.
2726 mono_thread_get_pending_exception (void)
2728 MonoThread *thread = mono_thread_current ();
2730 MONO_ARCH_SAVE_REGS;
2732 if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
2734 * FIXME: Clear the abort exception and return an AppDomainUnloaded
2735 * exception if the thread no longer references a dying appdomain.
2737 thread->abort_exc->trace_ips = NULL;
2738 thread->abort_exc->stack_trace = NULL;
2739 return thread->abort_exc;
2745 #define NUM_STATIC_DATA_IDX 8
2746 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
2747 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
2752 * mono_alloc_static_data
2754 * Allocate memory blocks for storing threads or context static data
2757 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset)
2759 guint idx = (offset >> 24) - 1;
2762 gpointer* static_data = *static_data_ptr;
2764 static_data = mono_gc_alloc_fixed (static_data_size [0], NULL);
2765 *static_data_ptr = static_data;
2766 static_data [0] = static_data;
2769 for (i = 1; i <= idx; ++i) {
2770 if (static_data [i])
2772 static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
2777 * mono_init_static_data_info
2779 * Initializes static data counters
2781 static void mono_init_static_data_info (StaticDataInfo *static_data)
2783 static_data->idx = 0;
2784 static_data->offset = 0;
2785 static_data->freelist = NULL;
2789 * mono_alloc_static_data_slot
2791 * Generates an offset for static data. static_data contains the counters
2792 * used to generate it.
2795 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
2799 if (!static_data->idx && !static_data->offset) {
2801 * we use the first chunk of the first allocation also as
2802 * an array for the rest of the data
2804 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
2806 static_data->offset += align - 1;
2807 static_data->offset &= ~(align - 1);
2808 if (static_data->offset + size >= static_data_size [static_data->idx]) {
2809 static_data->idx ++;
2810 g_assert (size <= static_data_size [static_data->idx]);
2811 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
2812 static_data->offset = 0;
2814 offset = static_data->offset | ((static_data->idx + 1) << 24);
2815 static_data->offset += size;
2820 * ensure thread static fields already allocated are valid for thread
2821 * This function is called when a thread is created or on thread attach.
2824 thread_adjust_static_data (MonoThread *thread)
2828 mono_threads_lock ();
2829 if (thread_static_info.offset || thread_static_info.idx > 0) {
2830 /* get the current allocated size */
2831 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
2832 mono_alloc_static_data (&(thread->static_data), offset);
2834 mono_threads_unlock ();
2838 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
2840 MonoThread *thread = value;
2841 guint32 offset = GPOINTER_TO_UINT (user);
2843 mono_alloc_static_data (&(thread->static_data), offset);
2846 static MonoThreadDomainTls*
2847 search_tls_slot_in_freelist (StaticDataInfo *static_data, guint32 size, guint32 align)
2849 MonoThreadDomainTls* prev = NULL;
2850 MonoThreadDomainTls* tmp = static_data->freelist;
2852 if (tmp->size == size) {
2854 prev->next = tmp->next;
2856 static_data->freelist = tmp->next;
2865 * The offset for a special static variable is composed of three parts:
2866 * a bit that indicates the type of static data (0:thread, 1:context),
2867 * an index in the array of chunks of memory for the thread (thread->static_data)
2868 * and an offset in that chunk of mem. This allows allocating less memory in the
2873 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align)
2876 if (static_type == SPECIAL_STATIC_THREAD)
2878 MonoThreadDomainTls *item;
2879 mono_threads_lock ();
2880 item = search_tls_slot_in_freelist (&thread_static_info, size, align);
2881 /*g_print ("TLS alloc: %d in domain %p (total: %d), cached: %p\n", size, mono_domain_get (), thread_static_info.offset, item);*/
2883 offset = item->offset;
2886 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
2888 /* This can be called during startup */
2889 if (threads != NULL)
2890 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
2891 mono_threads_unlock ();
2895 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
2896 mono_contexts_lock ();
2897 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
2898 mono_contexts_unlock ();
2899 offset |= 0x80000000; /* Set the high bit to indicate context static data */
2905 mono_get_special_static_data (guint32 offset)
2907 /* The high bit means either thread (0) or static (1) data. */
2909 guint32 static_type = (offset & 0x80000000);
2912 offset &= 0x7fffffff;
2913 idx = (offset >> 24) - 1;
2915 if (static_type == 0)
2917 MonoThread *thread = mono_thread_current ();
2918 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
2922 /* Allocate static data block under demand, since we don't have a list
2925 MonoAppContext *context = mono_context_get ();
2926 if (!context->static_data || !context->static_data [idx]) {
2927 mono_contexts_lock ();
2928 mono_alloc_static_data (&(context->static_data), offset);
2929 mono_contexts_unlock ();
2931 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
2941 free_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
2943 MonoThread *thread = value;
2944 TlsOffsetSize *data = user;
2945 int idx = (data->offset >> 24) - 1;
2948 if (!thread->static_data [idx])
2950 ptr = ((char*) thread->static_data [idx]) + (data->offset & 0xffffff);
2951 memset (ptr, 0, data->size);
2955 do_free_special (gpointer key, gpointer value, gpointer data)
2957 MonoClassField *field = key;
2958 guint32 offset = GPOINTER_TO_UINT (value);
2959 guint32 static_type = (offset & 0x80000000);
2962 size = mono_type_size (field->type, &align);
2963 /*g_print ("free %s , size: %d, offset: %x\n", field->name, size, offset);*/
2964 if (static_type == 0) {
2966 MonoThreadDomainTls *item = g_new0 (MonoThreadDomainTls, 1);
2967 data.offset = offset & 0x7fffffff;
2969 if (threads != NULL)
2970 mono_g_hash_table_foreach (threads, free_thread_static_data_helper, &data);
2971 item->offset = offset;
2973 item->next = thread_static_info.freelist;
2974 thread_static_info.freelist = item;
2976 /* FIXME: free context static data as well */
2981 mono_alloc_special_static_data_free (GHashTable *special_static_fields)
2983 mono_threads_lock ();
2984 g_hash_table_foreach (special_static_fields, do_free_special, NULL);
2985 mono_threads_unlock ();
2988 static MonoClassField *local_slots = NULL;
2991 /* local tls data to get locals_slot from a thread */
2994 /* index in the locals_slot array */
2999 clear_local_slot (gpointer key, gpointer value, gpointer user_data)
3001 LocalSlotID *sid = user_data;
3002 MonoThread *thread = (MonoThread*)value;
3003 MonoArray *slots_array;
3005 * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
3006 * it is for the right domain, so we need to check if it is allocated an initialized
3007 * for the current thread.
3009 /*g_print ("handling thread %p\n", thread);*/
3010 if (!thread->static_data || !thread->static_data [sid->idx])
3012 slots_array = *(MonoArray **)(((char*) thread->static_data [sid->idx]) + (sid->offset & 0xffffff));
3013 if (!slots_array || sid->slot >= mono_array_length (slots_array))
3015 mono_array_set (slots_array, MonoObject*, sid->slot, NULL);
3019 mono_thread_free_local_slot_values (int slot, MonoBoolean thread_local)
3027 local_slots = mono_class_get_field_from_name (mono_defaults.thread_class, "local_slots");
3029 g_warning ("local_slots field not found in Thread class");
3033 domain = mono_domain_get ();
3034 mono_domain_lock (domain);
3035 if (domain->special_static_fields)
3036 addr = g_hash_table_lookup (domain->special_static_fields, local_slots);
3037 mono_domain_unlock (domain);
3040 /*g_print ("freeing slot %d at %p\n", slot, addr);*/
3041 sid.offset = GPOINTER_TO_UINT (addr);
3042 sid.offset &= 0x7fffffff;
3043 sid.idx = (sid.offset >> 24) - 1;
3044 mono_threads_lock ();
3045 mono_g_hash_table_foreach (threads, clear_local_slot, &sid);
3046 mono_threads_unlock ();
3048 /* FIXME: clear the slot for MonoAppContexts, too */
3052 #ifdef PLATFORM_WIN32
3053 static void CALLBACK dummy_apc (ULONG_PTR param)
3057 static guint32 dummy_apc (gpointer param)
3064 * mono_thread_execute_interruption
3066 * Performs the operation that the requested thread state requires (abort,
3069 static MonoException* mono_thread_execute_interruption (MonoThread *thread)
3071 mono_monitor_enter (thread->synch_lock);
3073 if (thread->interruption_requested) {
3074 /* this will consume pending APC calls */
3075 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
3076 InterlockedDecrement (&thread_interruption_requested);
3077 thread->interruption_requested = FALSE;
3080 if ((thread->state & ThreadState_AbortRequested) != 0) {
3081 if (thread->abort_exc == NULL)
3082 MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
3083 mono_monitor_exit (thread->synch_lock);
3084 return thread->abort_exc;
3086 else if ((thread->state & ThreadState_SuspendRequested) != 0) {
3087 thread->state &= ~ThreadState_SuspendRequested;
3088 thread->state |= ThreadState_Suspended;
3089 thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
3090 if (thread->suspend_event == NULL) {
3091 mono_monitor_exit (thread->synch_lock);
3094 if (thread->suspended_event)
3095 SetEvent (thread->suspended_event);
3096 mono_monitor_exit (thread->synch_lock);
3098 WaitForSingleObject (thread->suspend_event, INFINITE);
3100 mono_monitor_enter (thread->synch_lock);
3101 CloseHandle (thread->suspend_event);
3102 thread->suspend_event = NULL;
3103 thread->state &= ~ThreadState_Suspended;
3105 /* The thread that requested the resume will have replaced this event
3106 * and will be waiting for it
3108 SetEvent (thread->resume_event);
3109 mono_monitor_exit (thread->synch_lock);
3112 else if ((thread->state & ThreadState_StopRequested) != 0) {
3113 /* FIXME: do this through the JIT? */
3114 mono_monitor_exit (thread->synch_lock);
3115 mono_thread_exit ();
3117 } else if (thread->thread_interrupt_requested) {
3118 mono_monitor_exit (thread->synch_lock);
3119 return(mono_get_exception_thread_interrupted ());
3122 mono_monitor_exit (thread->synch_lock);
3127 * mono_thread_request_interruption
3129 * A signal handler can call this method to request the interruption of a
3130 * thread. The result of the interruption will depend on the current state of
3131 * the thread. If the result is an exception that needs to be throw, it is
3132 * provided as return value.
3134 MonoException* mono_thread_request_interruption (gboolean running_managed)
3136 MonoThread *thread = mono_thread_current ();
3138 /* The thread may already be stopping */
3142 mono_monitor_enter (thread->synch_lock);
3144 if (thread->interruption_requested) {
3145 mono_monitor_exit (thread->synch_lock);
3149 if (!running_managed || is_running_protected_wrapper ()) {
3150 /* Can't stop while in unmanaged code. Increase the global interruption
3151 request count. When exiting the unmanaged method the count will be
3152 checked and the thread will be interrupted. */
3154 InterlockedIncrement (&thread_interruption_requested);
3155 thread->interruption_requested = TRUE;
3156 mono_monitor_exit (thread->synch_lock);
3158 /* this will awake the thread if it is in WaitForSingleObject
3160 QueueUserAPC ((PAPCFUNC)dummy_apc, thread->handle, NULL);
3164 mono_monitor_exit (thread->synch_lock);
3165 return mono_thread_execute_interruption (thread);
3169 gboolean mono_thread_interruption_requested ()
3171 if (thread_interruption_requested) {
3172 MonoThread *thread = mono_thread_current ();
3173 /* The thread may already be stopping */
3175 return (thread->interruption_requested);
3180 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
3182 MonoThread *thread = mono_thread_current ();
3184 /* The thread may already be stopping */
3188 if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
3189 MonoException* exc = mono_thread_execute_interruption (thread);
3190 if (exc) mono_raise_exception (exc);
3195 * Performs the interruption of the current thread, if one has been requested,
3196 * and the thread is not running a protected wrapper.
3198 void mono_thread_interruption_checkpoint ()
3200 mono_thread_interruption_checkpoint_request (FALSE);
3204 * Performs the interruption of the current thread, if one has been requested.
3206 void mono_thread_force_interruption_checkpoint ()
3208 mono_thread_interruption_checkpoint_request (TRUE);
3212 * mono_thread_interruption_request_flag:
3214 * Returns the address of a flag that will be non-zero if an interruption has
3215 * been requested for a thread. The thread to interrupt may not be the current
3216 * thread, so an additional call to mono_thread_interruption_requested() or
3217 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
3220 gint32* mono_thread_interruption_request_flag ()
3222 return &thread_interruption_requested;
3226 mono_thread_init_apartment_state (void)
3229 thread = mono_thread_current ();
3231 #ifdef PLATFORM_WIN32
3232 /* Positive return value indicates success, either
3233 * S_OK if this is first CoInitialize call, or
3234 * S_FALSE if CoInitialize already called, but with same
3235 * threading model. A negative value indicates failure,
3236 * probably due to trying to change the threading model.
3238 if (CoInitializeEx(NULL, (thread->apartment_state == ThreadApartmentState_STA)
3239 ? COINIT_APARTMENTTHREADED
3240 : COINIT_MULTITHREADED) < 0) {
3241 thread->apartment_state = ThreadApartmentState_Unknown;
3247 mono_thread_cleanup_apartment_state (void)
3249 #ifdef PLATFORM_WIN32
3251 thread = mono_thread_current ();
3253 if (thread && thread->apartment_state != ThreadApartmentState_Unknown) {