2 * threads.c: Thread support internal calls
5 * Dick Porter (dick@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
14 #define _WIN32_WINNT 0x0500
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/threadpool.h>
26 #include <mono/metadata/threads-types.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/environment.h>
29 #include <mono/metadata/monitor.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/metadata/marshal.h>
32 #include <mono/io-layer/io-layer.h>
33 #include <mono/metadata/object-internals.h>
34 #include <mono/metadata/mono-debug-debugger.h>
35 #include <mono/utils/mono-compiler.h>
37 #include <mono/os/gc_wrapper.h>
39 /*#define THREAD_DEBUG(a) do { a; } while (0)*/
40 #define THREAD_DEBUG(a)
41 /*#define THREAD_WAIT_DEBUG(a) do { a; } while (0)*/
42 #define THREAD_WAIT_DEBUG(a)
43 /*#define LIBGC_DEBUG(a) do { a; } while (0)*/
44 #define LIBGC_DEBUG(a)
46 /* Provide this for systems with glib < 2.6 */
47 #ifndef G_GSIZE_FORMAT
48 # if GLIB_SIZEOF_LONG == 8
49 # define G_GSIZE_FORMAT "lu"
51 # define G_GSIZE_FORMAT "u"
57 guint32 (*func
)(void *);
79 /* Number of cached culture objects in the MonoThread->cached_culture_info array
80 * (per-type): we use the first NUM entries for CultureInfo and the last for
81 * UICultureInfo. So the size of the array is really NUM_CACHED_CULTURES * 2.
83 #define NUM_CACHED_CULTURES 4
84 #define CULTURES_START_IDX 0
85 #define UICULTURES_START_IDX NUM_CACHED_CULTURES
88 * The "os_handle" field of the WaitHandle class.
90 static MonoClassField
*wait_handle_os_handle_field
= NULL
;
92 /* Controls access to the 'threads' hash table */
93 #define mono_threads_lock() EnterCriticalSection (&threads_mutex)
94 #define mono_threads_unlock() LeaveCriticalSection (&threads_mutex)
95 static CRITICAL_SECTION threads_mutex
;
97 /* Controls access to context static data */
98 #define mono_contexts_lock() EnterCriticalSection (&contexts_mutex)
99 #define mono_contexts_unlock() LeaveCriticalSection (&contexts_mutex)
100 static CRITICAL_SECTION contexts_mutex
;
102 /* Holds current status of static data heap */
103 static StaticDataInfo thread_static_info
;
104 static StaticDataInfo context_static_info
;
106 /* The hash of existing threads (key is thread ID) that need joining
109 static MonoGHashTable
*threads
=NULL
;
111 /* The TLS key that holds the MonoObject assigned to each thread */
112 static guint32 current_object_key
= -1;
114 #ifdef HAVE_KW_THREAD
115 /* we need to use both the Tls* functions and __thread because
116 * the gc needs to see all the threads
118 static __thread MonoThread
* tls_current_object MONO_TLS_FAST
;
119 #define SET_CURRENT_OBJECT(x) do { \
120 tls_current_object = x; \
121 TlsSetValue (current_object_key, x); \
123 #define GET_CURRENT_OBJECT() tls_current_object
125 #define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x);
126 #define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key);
129 /* function called at thread start */
130 static MonoThreadStartCB mono_thread_start_cb
= NULL
;
132 /* function called at thread attach */
133 static MonoThreadAttachCB mono_thread_attach_cb
= NULL
;
135 /* function called at thread cleanup */
136 static MonoThreadCleanupFunc mono_thread_cleanup_fn
= NULL
;
138 /* The default stack size for each thread */
139 static guint32 default_stacksize
= 0;
140 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
142 static void thread_adjust_static_data (MonoThread
*thread
);
143 static void mono_init_static_data_info (StaticDataInfo
*static_data
);
144 static guint32
mono_alloc_static_data_slot (StaticDataInfo
*static_data
, guint32 size
, guint32 align
);
145 static gboolean
mono_thread_resume (MonoThread
* thread
);
146 static void mono_thread_start (MonoThread
*thread
);
147 static void signal_thread_state_change (MonoThread
*thread
);
149 /* Spin lock for InterlockedXXX 64 bit functions */
150 #define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
151 #define mono_interlocked_unlock() LeaveCriticalSection (&interlocked_mutex)
152 static CRITICAL_SECTION interlocked_mutex
;
154 /* global count of thread interruptions requested */
155 static gint32 thread_interruption_requested
= 0;
157 /* Event signaled when a thread changes its background mode */
158 static HANDLE background_change_event
;
161 mono_thread_get_tls_key (void)
163 return current_object_key
;
167 mono_thread_get_tls_offset (void)
170 MONO_THREAD_VAR_OFFSET (tls_current_object
,offset
);
174 /* handle_store() and handle_remove() manage the array of threads that
175 * still need to be waited for when the main thread exits.
177 static void handle_store(MonoThread
*thread
)
179 mono_threads_lock ();
181 THREAD_DEBUG (g_message ("%s: thread %p ID %"G_GSIZE_FORMAT
, __func__
, thread
, (gsize
)thread
->tid
));
184 MONO_GC_REGISTER_ROOT (threads
);
185 threads
=mono_g_hash_table_new(NULL
, NULL
);
188 /* We don't need to duplicate thread->handle, because it is
189 * only closed when the thread object is finalized by the GC.
191 mono_g_hash_table_insert(threads
, (gpointer
)(gsize
)(thread
->tid
),
194 mono_threads_unlock ();
197 static void handle_remove(gsize tid
)
199 THREAD_DEBUG (g_message ("%s: thread ID %"G_GSIZE_FORMAT
, __func__
, tid
));
201 mono_threads_lock ();
205 mono_g_hash_table_remove (threads
, (gpointer
)tid
);
207 mono_threads_unlock ();
209 /* Don't close the handle here, wait for the object finalizer
210 * to do it. Otherwise, the following race condition applies:
212 * 1) Thread exits (and handle_remove() closes the handle)
214 * 2) Some other handle is reassigned the same slot
216 * 3) Another thread tries to join the first thread, and
217 * blocks waiting for the reassigned handle to be signalled
218 * (which might never happen). This is possible, because the
219 * thread calling Join() still has a reference to the first
224 static void thread_cleanup (MonoThread
*thread
)
226 g_assert (thread
!= NULL
);
228 mono_release_type_locks (thread
);
230 if (!mono_monitor_enter (thread
->synch_lock
))
233 thread
->state
|= ThreadState_Stopped
;
234 mono_monitor_exit (thread
->synch_lock
);
236 mono_profiler_thread_end (thread
->tid
);
237 handle_remove (thread
->tid
);
239 mono_thread_pop_appdomain_ref ();
241 if (thread
->serialized_culture_info
)
242 g_free (thread
->serialized_culture_info
);
244 thread
->cached_culture_info
= NULL
;
246 if (mono_thread_cleanup_fn
)
247 mono_thread_cleanup_fn (thread
);
250 static guint32 WINAPI
start_wrapper(void *data
)
252 struct StartInfo
*start_info
=(struct StartInfo
*)data
;
253 guint32 (*start_func
)(void *);
256 MonoThread
*thread
=start_info
->obj
;
257 MonoObject
*start_delegate
= start_info
->delegate
;
259 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Start wrapper", __func__
, GetCurrentThreadId ()));
261 /* We can be sure start_info->obj->tid and
262 * start_info->obj->handle have been set, because the thread
263 * was created suspended, and these values were set before the
269 SET_CURRENT_OBJECT (thread
);
271 if (!mono_domain_set (start_info
->domain
, FALSE
)) {
272 /* No point in raising an appdomain_unloaded exception here */
273 /* FIXME: Cleanup here */
277 start_func
= start_info
->func
;
278 start_arg
= start_info
->start_arg
;
280 /* This MUST be called before any managed code can be
281 * executed, as it calls the callback function that (for the
282 * jit) sets the lmf marker.
284 mono_thread_new_init (tid
, &tid
, start_func
);
285 thread
->stack_ptr
= &tid
;
287 LIBGC_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
",%d) Setting thread stack to %p", __func__
, GetCurrentThreadId (), getpid (), thread
->stack_ptr
));
289 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Setting current_object_key to %p", __func__
, GetCurrentThreadId (), thread
));
291 mono_profiler_thread_start (tid
);
293 if(thread
->start_notify
!=NULL
) {
294 /* Let the thread that called Start() know we're
297 ReleaseSemaphore (thread
->start_notify
, 1, NULL
);
302 /* Every thread references the appdomain which created it */
303 mono_thread_push_appdomain_ref (mono_domain_get ());
305 thread_adjust_static_data (thread
);
307 g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT
, __func__
,
311 /* start_func is set only for unmanaged start functions */
313 start_func (start_arg
);
316 g_assert (start_delegate
!= NULL
);
317 args
[0] = start_arg
;
318 /* we may want to handle the exception here. See comment below on unhandled exceptions */
319 mono_runtime_delegate_invoke (start_delegate
, args
, NULL
);
322 /* If the thread calls ExitThread at all, this remaining code
323 * will not be executed, but the main thread will eventually
324 * call thread_cleanup() on this thread's behalf.
327 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Start wrapper terminating", __func__
, GetCurrentThreadId ()));
329 thread_cleanup (thread
);
331 /* Remove the reference to the thread object in the TLS data,
332 * so the thread object can be finalized. This won't be
333 * reached if the thread threw an uncaught exception, so those
334 * thread handles will stay referenced :-( (This is due to
335 * missing support for scanning thread-specific data in the
336 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
339 SET_CURRENT_OBJECT (NULL
);
344 void mono_thread_new_init (gsize tid
, gpointer stack_start
, gpointer func
)
346 if (mono_thread_start_cb
) {
347 mono_thread_start_cb (tid
, stack_start
, func
);
351 void mono_threads_set_default_stacksize (guint32 stacksize
)
353 default_stacksize
= stacksize
;
356 guint32
mono_threads_get_default_stacksize (void)
358 return default_stacksize
;
361 void mono_thread_create (MonoDomain
*domain
, gpointer func
, gpointer arg
)
364 HANDLE thread_handle
;
365 struct StartInfo
*start_info
;
368 thread
=(MonoThread
*)mono_object_new (domain
,
369 mono_defaults
.thread_class
);
371 start_info
=g_new0 (struct StartInfo
, 1);
372 start_info
->func
= func
;
373 start_info
->obj
= thread
;
374 start_info
->domain
= domain
;
375 start_info
->start_arg
= arg
;
377 /* Create suspended, so we can do some housekeeping before the thread
380 thread_handle
= CreateThread(NULL
, default_stacksize_for_thread (thread
), (LPTHREAD_START_ROUTINE
)start_wrapper
, start_info
,
381 CREATE_SUSPENDED
, &tid
);
382 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT
" (handle %p)", __func__
, tid
, thread_handle
));
383 if (thread_handle
== NULL
) {
384 /* The thread couldn't be created, so throw an exception */
385 mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
389 thread
->handle
=thread_handle
;
392 MONO_OBJECT_SETREF (thread
, synch_lock
, mono_object_new (domain
, mono_defaults
.object_class
));
394 handle_store(thread
);
396 ResumeThread (thread_handle
);
400 mono_thread_attach (MonoDomain
*domain
)
403 HANDLE thread_handle
;
406 if ((thread
= mono_thread_current ())) {
407 if (domain
!= mono_domain_get ())
408 mono_domain_set (domain
, TRUE
);
409 /* Already attached */
413 if (!mono_gc_register_thread (&domain
)) {
414 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 ());
417 thread
= (MonoThread
*)mono_object_new (domain
,
418 mono_defaults
.thread_class
);
420 thread_handle
= GetCurrentThread ();
421 g_assert (thread_handle
);
423 tid
=GetCurrentThreadId ();
426 * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
427 * refer to the thread from other threads for things like aborting.
429 DuplicateHandle (GetCurrentProcess (), thread_handle
, GetCurrentProcess (), &thread_handle
,
430 THREAD_ALL_ACCESS
, TRUE
, 0);
432 thread
->handle
=thread_handle
;
434 thread
->stack_ptr
= &tid
;
435 MONO_OBJECT_SETREF (thread
, synch_lock
, mono_object_new (domain
, mono_defaults
.object_class
));
437 THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT
" (handle %p)", __func__
, tid
, thread_handle
));
439 handle_store(thread
);
441 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Setting current_object_key to %p", __func__
, GetCurrentThreadId (), thread
));
443 SET_CURRENT_OBJECT (thread
);
444 mono_domain_set (domain
, TRUE
);
446 thread_adjust_static_data (thread
);
448 if (mono_thread_attach_cb
) {
449 mono_thread_attach_cb (tid
, &tid
);
456 mono_thread_detach (MonoThread
*thread
)
458 g_return_if_fail (thread
!= NULL
);
460 THREAD_DEBUG (g_message ("%s: mono_thread_detach for %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
462 thread_cleanup (thread
);
464 SET_CURRENT_OBJECT (NULL
);
466 /* Don't need to CloseHandle this thread, even though we took a
467 * reference in mono_thread_attach (), because the GC will do it
468 * when the Thread object is finalised.
475 MonoThread
*thread
= mono_thread_current ();
477 thread_cleanup (thread
);
478 SET_CURRENT_OBJECT (NULL
);
480 /* we could add a callback here for embedders to use. */
481 if (thread
== mono_thread_get_main ())
482 exit (mono_environment_exitcode_get ());
486 HANDLE
ves_icall_System_Threading_Thread_Thread_internal(MonoThread
*this,
489 guint32 (*start_func
)(void *);
490 struct StartInfo
*start_info
;
496 THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__
, this, start
));
498 mono_monitor_enter (this->synch_lock
);
500 if ((this->state
& ThreadState_Unstarted
) == 0) {
501 mono_monitor_exit (this->synch_lock
);
502 mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
506 if ((this->state
& ThreadState_Aborted
) != 0) {
507 mono_monitor_exit (this->synch_lock
);
512 /* This is freed in start_wrapper */
513 start_info
= g_new0 (struct StartInfo
, 1);
514 start_info
->func
= start_func
;
515 start_info
->start_arg
= this->start_obj
; /* FIXME: GC object stored in unmanaged memory */
516 start_info
->delegate
= start
;
517 start_info
->obj
= this;
518 start_info
->domain
= mono_domain_get ();
520 this->start_notify
=CreateSemaphore (NULL
, 0, 0x7fffffff, NULL
);
521 if(this->start_notify
==NULL
) {
522 mono_monitor_exit (this->synch_lock
);
523 g_warning ("%s: CreateSemaphore error 0x%x", __func__
, GetLastError ());
527 thread
=CreateThread(NULL
, default_stacksize_for_thread (this), (LPTHREAD_START_ROUTINE
)start_wrapper
, start_info
,
528 CREATE_SUSPENDED
, &tid
);
530 mono_monitor_exit (this->synch_lock
);
531 g_warning("%s: CreateThread error 0x%x", __func__
, GetLastError());
538 /* Don't call handle_store() here, delay it to Start.
539 * We can't join a thread (trying to will just block
540 * forever) until it actually starts running, so don't
541 * store the handle till then.
544 mono_thread_start (this);
546 this->state
&= ~ThreadState_Unstarted
;
548 THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT
" (handle %p)", __func__
, tid
, thread
));
550 mono_monitor_exit (this->synch_lock
);
555 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread
*this,
560 THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__
, this, thread
));
562 CloseHandle (thread
);
565 static void mono_thread_start (MonoThread
*thread
)
569 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Launching thread %p (%"G_GSIZE_FORMAT
")", __func__
, GetCurrentThreadId (), thread
, (gsize
)thread
->tid
));
571 /* Only store the handle when the thread is about to be
572 * launched, to avoid the main thread deadlocking while trying
573 * to clean up a thread that will never be signalled.
575 handle_store (thread
);
577 ResumeThread (thread
->handle
);
579 if(thread
->start_notify
!=NULL
) {
580 /* Wait for the thread to set up its TLS data etc, so
581 * theres no potential race condition if someone tries
582 * to look up the data believing the thread has
586 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") waiting for thread %p (%"G_GSIZE_FORMAT
") to start", __func__
, GetCurrentThreadId (), thread
, (gsize
)thread
->tid
));
588 WaitForSingleObjectEx (thread
->start_notify
, INFINITE
, FALSE
);
589 CloseHandle (thread
->start_notify
);
590 thread
->start_notify
= NULL
;
593 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Done launching thread %p (%"G_GSIZE_FORMAT
")", __func__
, GetCurrentThreadId (), thread
, (gsize
)thread
->tid
));
596 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms
)
598 MonoThread
*thread
= mono_thread_current ();
602 THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__
, ms
));
604 mono_thread_current_check_pending_interrupt ();
606 mono_monitor_enter (thread
->synch_lock
);
607 thread
->state
|= ThreadState_WaitSleepJoin
;
608 mono_monitor_exit (thread
->synch_lock
);
612 mono_monitor_enter (thread
->synch_lock
);
613 thread
->state
&= ~ThreadState_WaitSleepJoin
;
614 mono_monitor_exit (thread
->synch_lock
);
617 void ves_icall_System_Threading_Thread_SpinWait_internal (gint32 iterations
)
621 for(i
= 0; i
< iterations
; i
++) {
622 /* We're busy waiting, but at least we can tell the
623 * scheduler to let someone else have a go...
630 ves_icall_System_Threading_Thread_GetDomainID (void)
634 return mono_domain_get()->domain_id
;
638 ves_icall_System_Threading_Thread_GetName_internal (MonoThread
*this_obj
)
641 mono_monitor_enter (this_obj
->synch_lock
);
646 str
= mono_string_new_utf16 (mono_domain_get (), this_obj
->name
, this_obj
->name_len
);
648 mono_monitor_exit (this_obj
->synch_lock
);
653 ves_icall_System_Threading_Thread_SetName_internal (MonoThread
*this_obj
, MonoString
*name
)
655 mono_monitor_enter (this_obj
->synch_lock
);
657 if (this_obj
->name
) {
658 mono_monitor_exit (this_obj
->synch_lock
);
659 mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
663 this_obj
->name
= g_new (gunichar2
, mono_string_length (name
));
664 memcpy (this_obj
->name
, mono_string_chars (name
), mono_string_length (name
) * 2);
665 this_obj
->name_len
= mono_string_length (name
);
668 this_obj
->name
= NULL
;
670 mono_monitor_exit (this_obj
->synch_lock
);
674 lookup_cached_culture (MonoThread
*this, MonoDomain
*domain
, int start_idx
)
679 if (this->cached_culture_info
) {
680 domain
= mono_domain_get ();
681 for (i
= start_idx
; i
< start_idx
+ NUM_CACHED_CULTURES
; ++i
) {
682 res
= mono_array_get (this->cached_culture_info
, MonoObject
*, i
);
683 if (res
&& res
->vtable
->domain
== domain
)
692 ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoThread
*this)
694 return lookup_cached_culture (this, mono_domain_get (), CULTURES_START_IDX
);
698 ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread
*this)
702 mono_monitor_enter (this->synch_lock
);
703 if (this->serialized_culture_info
) {
704 res
= mono_array_new (mono_domain_get (), mono_defaults
.byte_class
, this->serialized_culture_info_len
);
705 memcpy (mono_array_addr (res
, guint8
, 0), this->serialized_culture_info
, this->serialized_culture_info_len
);
709 mono_monitor_exit (this->synch_lock
);
715 cache_culture (MonoThread
*this, MonoObject
*culture
, int start_idx
)
718 MonoDomain
*domain
= mono_domain_get ();
721 int same_domain_slot
= -1;
723 mono_monitor_enter (this->synch_lock
);
724 if (!this->cached_culture_info
)
725 this->cached_culture_info
= mono_array_new (mono_object_domain (this), mono_defaults
.object_class
, NUM_CACHED_CULTURES
* 2);
727 for (i
= start_idx
; i
< start_idx
+ NUM_CACHED_CULTURES
; ++i
) {
728 obj
= mono_array_get (this->cached_culture_info
, MonoObject
*, i
);
732 /* we continue, because there may be a slot used with the same domain */
736 if (obj
->vtable
->domain
== domain
) {
737 same_domain_slot
= i
;
741 if (same_domain_slot
>= 0)
742 mono_array_setref (this->cached_culture_info
, same_domain_slot
, culture
);
743 else if (free_slot
>= 0)
744 mono_array_setref (this->cached_culture_info
, free_slot
, culture
);
745 /* we may want to replace an existing entry here, even when no suitable slot is found */
746 mono_monitor_exit (this->synch_lock
);
750 ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread
*this, MonoObject
*culture
)
752 cache_culture (this, culture
, CULTURES_START_IDX
);
756 ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread
*this, MonoArray
*arr
)
758 mono_monitor_enter (this->synch_lock
);
759 if (this->serialized_culture_info
)
760 g_free (this->serialized_culture_info
);
761 this->serialized_culture_info
= g_new0 (guint8
, mono_array_length (arr
));
762 this->serialized_culture_info_len
= mono_array_length (arr
);
763 memcpy (this->serialized_culture_info
, mono_array_addr (arr
, guint8
, 0), mono_array_length (arr
));
764 mono_monitor_exit (this->synch_lock
);
769 ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoThread
*this)
771 return lookup_cached_culture (this, mono_domain_get (), UICULTURES_START_IDX
);
775 ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread
*this)
779 mono_monitor_enter (this->synch_lock
);
780 if (this->serialized_ui_culture_info
) {
781 res
= mono_array_new (mono_domain_get (), mono_defaults
.byte_class
, this->serialized_ui_culture_info_len
);
782 memcpy (mono_array_addr (res
, guint8
, 0), this->serialized_ui_culture_info
, this->serialized_ui_culture_info_len
);
786 mono_monitor_exit (this->synch_lock
);
792 ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread
*this, MonoObject
*culture
)
794 cache_culture (this, culture
, UICULTURES_START_IDX
);
798 ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread
*this, MonoArray
*arr
)
800 mono_monitor_enter (this->synch_lock
);
801 if (this->serialized_ui_culture_info
)
802 g_free (this->serialized_ui_culture_info
);
803 this->serialized_ui_culture_info
= g_new0 (guint8
, mono_array_length (arr
));
804 this->serialized_ui_culture_info_len
= mono_array_length (arr
);
805 memcpy (this->serialized_ui_culture_info
, mono_array_addr (arr
, guint8
, 0), mono_array_length (arr
));
806 mono_monitor_exit (this->synch_lock
);
809 /* the jit may read the compiled code of this function */
811 mono_thread_current (void)
813 THREAD_DEBUG (g_message ("%s: returning %p", __func__
, GET_CURRENT_OBJECT ()));
814 return GET_CURRENT_OBJECT ();
817 gboolean
ves_icall_System_Threading_Thread_Join_internal(MonoThread
*this,
818 int ms
, HANDLE thread
)
824 mono_monitor_enter (this->synch_lock
);
826 if ((this->state
& ThreadState_Unstarted
) != 0) {
827 mono_monitor_exit (this->synch_lock
);
828 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
832 mono_thread_current_check_pending_interrupt ();
834 this->state
|= ThreadState_WaitSleepJoin
;
835 mono_monitor_exit (this->synch_lock
);
840 THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__
, thread
, ms
));
842 ret
=WaitForSingleObjectEx (thread
, ms
, TRUE
);
844 mono_monitor_enter (this->synch_lock
);
845 this->state
&= ~ThreadState_WaitSleepJoin
;
846 mono_monitor_exit (this->synch_lock
);
848 if(ret
==WAIT_OBJECT_0
) {
849 THREAD_DEBUG (g_message ("%s: join successful", __func__
));
854 THREAD_DEBUG (g_message ("%s: join failed", __func__
));
859 /* FIXME: exitContext isnt documented */
860 gboolean
ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray
*mono_handles
, gint32 ms
, gboolean exitContext
)
866 MonoObject
*waitHandle
;
868 MonoThread
*thread
= mono_thread_current ();
872 /* Do this WaitSleepJoin check before creating objects */
873 mono_thread_current_check_pending_interrupt ();
875 numhandles
= mono_array_length(mono_handles
);
876 handles
= g_new0(HANDLE
, numhandles
);
878 if (wait_handle_os_handle_field
== 0) {
879 /* Get the field os_handle which will contain the actual handle */
880 klass
= mono_class_from_name(mono_defaults
.corlib
, "System.Threading", "WaitHandle");
881 wait_handle_os_handle_field
= mono_class_get_field_from_name(klass
, "os_handle");
884 for(i
= 0; i
< numhandles
; i
++) {
885 waitHandle
= mono_array_get(mono_handles
, MonoObject
*, i
);
886 mono_field_get_value(waitHandle
, wait_handle_os_handle_field
, &handles
[i
]);
893 mono_monitor_enter (thread
->synch_lock
);
894 thread
->state
|= ThreadState_WaitSleepJoin
;
895 mono_monitor_exit (thread
->synch_lock
);
897 ret
=WaitForMultipleObjectsEx(numhandles
, handles
, TRUE
, ms
, TRUE
);
899 mono_monitor_enter (thread
->synch_lock
);
900 thread
->state
&= ~ThreadState_WaitSleepJoin
;
901 mono_monitor_exit (thread
->synch_lock
);
905 if(ret
==WAIT_FAILED
) {
906 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Wait failed", __func__
, GetCurrentThreadId ()));
908 } else if(ret
==WAIT_TIMEOUT
|| ret
== WAIT_IO_COMPLETION
) {
909 /* Do we want to try again if we get
910 * WAIT_IO_COMPLETION? The documentation for
911 * WaitHandle doesn't give any clues. (We'd have to
912 * fiddle with the timeout if we retry.)
914 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Wait timed out", __func__
, GetCurrentThreadId ()));
921 /* FIXME: exitContext isnt documented */
922 gint32
ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray
*mono_handles
, gint32 ms
, gboolean exitContext
)
928 MonoObject
*waitHandle
;
930 MonoThread
*thread
= mono_thread_current ();
934 /* Do this WaitSleepJoin check before creating objects */
935 mono_thread_current_check_pending_interrupt ();
937 numhandles
= mono_array_length(mono_handles
);
938 handles
= g_new0(HANDLE
, numhandles
);
940 if (wait_handle_os_handle_field
== 0) {
941 /* Get the field os_handle which will contain the actual handle */
942 klass
= mono_class_from_name(mono_defaults
.corlib
, "System.Threading", "WaitHandle");
943 wait_handle_os_handle_field
= mono_class_get_field_from_name(klass
, "os_handle");
946 for(i
= 0; i
< numhandles
; i
++) {
947 waitHandle
= mono_array_get(mono_handles
, MonoObject
*, i
);
948 mono_field_get_value(waitHandle
, wait_handle_os_handle_field
, &handles
[i
]);
955 mono_monitor_enter (thread
->synch_lock
);
956 thread
->state
|= ThreadState_WaitSleepJoin
;
957 mono_monitor_exit (thread
->synch_lock
);
959 ret
=WaitForMultipleObjectsEx(numhandles
, handles
, FALSE
, ms
, TRUE
);
961 mono_monitor_enter (thread
->synch_lock
);
962 thread
->state
&= ~ThreadState_WaitSleepJoin
;
963 mono_monitor_exit (thread
->synch_lock
);
967 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") returning %d", __func__
, GetCurrentThreadId (), ret
));
970 * These need to be here. See MSDN dos on WaitForMultipleObjects.
972 if (ret
>= WAIT_OBJECT_0
&& ret
<= WAIT_OBJECT_0
+ numhandles
- 1) {
973 return ret
- WAIT_OBJECT_0
;
975 else if (ret
>= WAIT_ABANDONED_0
&& ret
<= WAIT_ABANDONED_0
+ numhandles
- 1) {
976 return ret
- WAIT_ABANDONED_0
;
983 /* FIXME: exitContext isnt documented */
984 gboolean
ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject
*this, HANDLE handle
, gint32 ms
, gboolean exitContext
)
987 MonoThread
*thread
= mono_thread_current ();
991 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") waiting for %p, %d ms", __func__
, GetCurrentThreadId (), handle
, ms
));
997 mono_thread_current_check_pending_interrupt ();
999 mono_monitor_enter (thread
->synch_lock
);
1000 thread
->state
|= ThreadState_WaitSleepJoin
;
1001 mono_monitor_exit (thread
->synch_lock
);
1003 ret
=WaitForSingleObjectEx (handle
, ms
, TRUE
);
1005 mono_monitor_enter (thread
->synch_lock
);
1006 thread
->state
&= ~ThreadState_WaitSleepJoin
;
1007 mono_monitor_exit (thread
->synch_lock
);
1009 if(ret
==WAIT_FAILED
) {
1010 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Wait failed", __func__
, GetCurrentThreadId ()));
1012 } else if(ret
==WAIT_TIMEOUT
|| ret
== WAIT_IO_COMPLETION
) {
1013 /* Do we want to try again if we get
1014 * WAIT_IO_COMPLETION? The documentation for
1015 * WaitHandle doesn't give any clues. (We'd have to
1016 * fiddle with the timeout if we retry.)
1018 THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Wait timed out", __func__
, GetCurrentThreadId ()));
1025 HANDLE
ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned
, MonoString
*name
, MonoBoolean
*created
)
1029 MONO_ARCH_SAVE_REGS
;
1034 mutex
= CreateMutex (NULL
, owned
, NULL
);
1036 mutex
= CreateMutex (NULL
, owned
, mono_string_chars (name
));
1038 if (GetLastError () == ERROR_ALREADY_EXISTS
) {
1046 void ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle
) {
1047 MONO_ARCH_SAVE_REGS
;
1049 ReleaseMutex(handle
);
1052 HANDLE
ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString
*name
,
1058 MONO_ARCH_SAVE_REGS
;
1060 *error
= ERROR_SUCCESS
;
1062 ret
= OpenMutex (rights
, FALSE
, mono_string_chars (name
));
1064 *error
= GetLastError ();
1071 HANDLE
ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount
, gint32 maximumCount
, MonoString
*name
, MonoBoolean
*created
)
1075 MONO_ARCH_SAVE_REGS
;
1080 sem
= CreateSemaphore (NULL
, initialCount
, maximumCount
, NULL
);
1082 sem
= CreateSemaphore (NULL
, initialCount
, maximumCount
,
1083 mono_string_chars (name
));
1085 if (GetLastError () == ERROR_ALREADY_EXISTS
) {
1093 gint32
ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle
, gint32 releaseCount
, MonoBoolean
*fail
)
1097 MONO_ARCH_SAVE_REGS
;
1099 *fail
= !ReleaseSemaphore (handle
, releaseCount
, &prevcount
);
1104 HANDLE
ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString
*name
, gint32 rights
, gint32
*error
)
1108 MONO_ARCH_SAVE_REGS
;
1110 *error
= ERROR_SUCCESS
;
1112 ret
= OpenSemaphore (rights
, FALSE
, mono_string_chars (name
));
1114 *error
= GetLastError ();
1120 HANDLE
ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual
, MonoBoolean initial
, MonoString
*name
, MonoBoolean
*created
)
1124 MONO_ARCH_SAVE_REGS
;
1129 event
= CreateEvent (NULL
, manual
, initial
, NULL
);
1131 event
= CreateEvent (NULL
, manual
, initial
,
1132 mono_string_chars (name
));
1134 if (GetLastError () == ERROR_ALREADY_EXISTS
) {
1142 gboolean
ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle
) {
1143 MONO_ARCH_SAVE_REGS
;
1145 return (SetEvent(handle
));
1148 gboolean
ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle
) {
1149 MONO_ARCH_SAVE_REGS
;
1151 return (ResetEvent(handle
));
1155 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle
) {
1156 MONO_ARCH_SAVE_REGS
;
1158 CloseHandle (handle
);
1161 HANDLE
ves_icall_System_Threading_Events_OpenEvent_internal (MonoString
*name
,
1167 MONO_ARCH_SAVE_REGS
;
1169 *error
= ERROR_SUCCESS
;
1171 ret
= OpenEvent (rights
, FALSE
, mono_string_chars (name
));
1173 *error
= GetLastError ();
1179 gint32
ves_icall_System_Threading_Interlocked_Increment_Int (gint32
*location
)
1181 MONO_ARCH_SAVE_REGS
;
1183 return InterlockedIncrement (location
);
1186 gint64
ves_icall_System_Threading_Interlocked_Increment_Long (gint64
*location
)
1190 MONO_ARCH_SAVE_REGS
;
1192 mono_interlocked_lock ();
1196 mono_interlocked_unlock ();
1202 gint32
ves_icall_System_Threading_Interlocked_Decrement_Int (gint32
*location
)
1204 MONO_ARCH_SAVE_REGS
;
1206 return InterlockedDecrement(location
);
1209 gint64
ves_icall_System_Threading_Interlocked_Decrement_Long (gint64
* location
)
1213 MONO_ARCH_SAVE_REGS
;
1215 mono_interlocked_lock ();
1219 mono_interlocked_unlock ();
1224 gint32
ves_icall_System_Threading_Interlocked_Exchange_Int (gint32
*location
, gint32 value
)
1226 MONO_ARCH_SAVE_REGS
;
1228 return InterlockedExchange(location
, value
);
1231 MonoObject
* ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject
**location
, MonoObject
*value
)
1233 MONO_ARCH_SAVE_REGS
;
1235 return (MonoObject
*) InterlockedExchangePointer((gpointer
*) location
, value
);
1238 gfloat
ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat
*location
, gfloat value
)
1240 IntFloatUnion val
, ret
;
1242 MONO_ARCH_SAVE_REGS
;
1245 ret
.ival
= InterlockedExchange((gint32
*) location
, val
.ival
);
1251 ves_icall_System_Threading_Interlocked_Exchange_Long (gint64
*location
, gint64 value
)
1253 #if SIZEOF_VOID_P == 8
1254 return (gint64
) InterlockedExchangePointer((gpointer
*) location
, (gpointer
)value
);
1259 * According to MSDN, this function is only atomic with regards to the
1260 * other Interlocked functions on 32 bit platforms.
1262 mono_interlocked_lock ();
1265 mono_interlocked_unlock ();
1272 ves_icall_System_Threading_Interlocked_Exchange_Double (gdouble
*location
, gdouble value
)
1274 #if SIZEOF_VOID_P == 8
1275 LongDoubleUnion val
, ret
;
1278 ret
.ival
= (gint64
)InterlockedExchangePointer((gpointer
*) location
, (gpointer
)val
.ival
);
1285 * According to MSDN, this function is only atomic with regards to the
1286 * other Interlocked functions on 32 bit platforms.
1288 mono_interlocked_lock ();
1291 mono_interlocked_unlock ();
1297 gint32
ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32
*location
, gint32 value
, gint32 comparand
)
1299 MONO_ARCH_SAVE_REGS
;
1301 return InterlockedCompareExchange(location
, value
, comparand
);
1304 MonoObject
* ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject
**location
, MonoObject
*value
, MonoObject
*comparand
)
1306 MONO_ARCH_SAVE_REGS
;
1308 return (MonoObject
*) InterlockedCompareExchangePointer((gpointer
*) location
, value
, comparand
);
1311 gfloat
ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat
*location
, gfloat value
, gfloat comparand
)
1313 IntFloatUnion val
, ret
, cmp
;
1315 MONO_ARCH_SAVE_REGS
;
1318 cmp
.fval
= comparand
;
1319 ret
.ival
= InterlockedCompareExchange((gint32
*) location
, val
.ival
, cmp
.ival
);
1325 ves_icall_System_Threading_Interlocked_CompareExchange_Double (gdouble
*location
, gdouble value
, gdouble comparand
)
1327 #if SIZEOF_VOID_P == 8
1328 LongDoubleUnion val
, comp
, ret
;
1331 comp
.fval
= comparand
;
1332 ret
.ival
= (gint64
)InterlockedCompareExchangePointer((gpointer
*) location
, (gpointer
)val
.ival
, (gpointer
)comp
.ival
);
1338 mono_interlocked_lock ();
1340 if (old
== comparand
)
1342 mono_interlocked_unlock ();
1349 ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64
*location
, gint64 value
, gint64 comparand
)
1351 #if SIZEOF_VOID_P == 8
1352 return (gint64
)InterlockedCompareExchangePointer((gpointer
*) location
, (gpointer
)value
, (gpointer
)comparand
);
1356 mono_interlocked_lock ();
1358 if (old
== comparand
)
1360 mono_interlocked_unlock ();
1367 ves_icall_System_Threading_Interlocked_CompareExchange_T (MonoObject
**location
, MonoObject
*value
, MonoObject
*comparand
)
1369 MONO_ARCH_SAVE_REGS
;
1371 return InterlockedCompareExchangePointer ((gpointer
*)location
, value
, comparand
);
1375 ves_icall_System_Threading_Interlocked_Exchange_T (MonoObject
**location
, MonoObject
*value
)
1377 MONO_ARCH_SAVE_REGS
;
1379 return InterlockedExchangePointer ((gpointer
*)location
, value
);
1383 ves_icall_System_Threading_Interlocked_Add_Int (gint32
*location
, gint32 value
)
1385 #if SIZEOF_VOID_P == 8
1386 /* Should be implemented as a JIT intrinsic */
1387 mono_raise_exception (mono_get_exception_not_implemented (NULL
));
1392 mono_interlocked_lock ();
1394 *location
= orig
+ value
;
1395 mono_interlocked_unlock ();
1397 return orig
+ value
;
1402 ves_icall_System_Threading_Interlocked_Add_Long (gint64
*location
, gint64 value
)
1404 #if SIZEOF_VOID_P == 8
1405 /* Should be implemented as a JIT intrinsic */
1406 mono_raise_exception (mono_get_exception_not_implemented (NULL
));
1411 mono_interlocked_lock ();
1413 *location
= orig
+ value
;
1414 mono_interlocked_unlock ();
1416 return orig
+ value
;
1421 ves_icall_System_Threading_Interlocked_Read_Long (gint64
*location
)
1423 #if SIZEOF_VOID_P == 8
1424 /* 64 bit reads are already atomic */
1429 mono_interlocked_lock ();
1431 mono_interlocked_unlock ();
1438 ves_icall_System_Threading_Thread_MemoryBarrier (void)
1440 /* Should be implemented as a JIT intrinsic */
1441 mono_raise_exception (mono_get_exception_not_implemented (NULL
));
1445 ves_icall_System_Threading_Thread_ClrState (MonoThread
* this, guint32 state
)
1447 mono_monitor_enter (this->synch_lock
);
1448 this->state
&= ~state
;
1449 if (state
& ThreadState_Background
) {
1450 /* If the thread changes the background mode, the main thread has to
1451 * be notified, since it has to rebuild the list of threads to
1454 SetEvent (background_change_event
);
1456 mono_monitor_exit (this->synch_lock
);
1460 ves_icall_System_Threading_Thread_SetState (MonoThread
* this, guint32 state
)
1462 mono_monitor_enter (this->synch_lock
);
1463 this->state
|= state
;
1464 if (state
& ThreadState_Background
) {
1465 /* If the thread changes the background mode, the main thread has to
1466 * be notified, since it has to rebuild the list of threads to
1469 SetEvent (background_change_event
);
1471 mono_monitor_exit (this->synch_lock
);
1475 ves_icall_System_Threading_Thread_GetState (MonoThread
* this)
1478 mono_monitor_enter (this->synch_lock
);
1479 state
= this->state
;
1480 mono_monitor_exit (this->synch_lock
);
1484 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread
*this)
1486 gboolean
throw = FALSE
;
1488 mono_monitor_enter (this->synch_lock
);
1490 /* Clear out any previous request */
1491 this->thread_interrupt_requested
= FALSE
;
1493 if (this->state
& ThreadState_WaitSleepJoin
) {
1496 this->thread_interrupt_requested
= TRUE
;
1499 mono_monitor_exit (this->synch_lock
);
1502 signal_thread_state_change (this);
1506 void mono_thread_current_check_pending_interrupt ()
1508 MonoThread
*thread
= mono_thread_current ();
1509 gboolean
throw = FALSE
;
1511 mono_monitor_enter (thread
->synch_lock
);
1513 if (thread
->thread_interrupt_requested
) {
1515 thread
->thread_interrupt_requested
= FALSE
;
1518 mono_monitor_exit (thread
->synch_lock
);
1521 mono_raise_exception (mono_get_exception_thread_interrupted ());
1526 mono_thread_get_abort_signal (void)
1528 #if defined (__MINGW32__) || defined (_MSC_VER)
1534 static int abort_signum
= -1;
1536 if (abort_signum
!= -1)
1537 return abort_signum
;
1538 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
1539 for (i
= SIGRTMIN
+ 1; i
< SIGRTMAX
; ++i
) {
1540 struct sigaction sinfo
;
1541 sigaction (i
, NULL
, &sinfo
);
1542 if (sinfo
.sa_handler
== SIG_DFL
&& (void*)sinfo
.sa_sigaction
== (void*)SIG_DFL
) {
1547 /* fallback to the old way */
1550 #endif /*defined (__MINGW32__) || defined (_MSC_VER) */
1553 #if defined (__MINGW32__) || defined (_MSC_VER)
1554 static void CALLBACK
interruption_request_apc (ULONG_PTR param
)
1556 MonoException
* exc
= mono_thread_request_interruption (FALSE
);
1557 if (exc
) mono_raise_exception (exc
);
1559 #endif /* defined (__MINGW32__) || defined (_MSC_VER) */
1562 * signal_thread_state_change
1564 * Tells the thread that his state has changed and it has to enter the new
1565 * state as soon as possible.
1567 static void signal_thread_state_change (MonoThread
*thread
)
1569 if (thread
== mono_thread_current ()) {
1570 /* Do it synchronously */
1571 MonoException
*exc
= mono_thread_request_interruption (FALSE
);
1573 mono_raise_exception (exc
);
1576 #if defined (__MINGW32__) || defined (_MSC_VER)
1577 QueueUserAPC ((PAPCFUNC
)interruption_request_apc
, thread
->handle
, NULL
);
1579 /* fixme: store the state somewhere */
1580 #ifdef PTHREAD_POINTER_ID
1581 pthread_kill ((gpointer
)(gsize
)(thread
->tid
), mono_thread_get_abort_signal ());
1583 pthread_kill (thread
->tid
, mono_thread_get_abort_signal ());
1585 #endif /* defined (__MINGW32__) || defined (__MSC_VER) */
1589 ves_icall_System_Threading_Thread_Abort (MonoThread
*thread
, MonoObject
*state
)
1591 MONO_ARCH_SAVE_REGS
;
1593 mono_monitor_enter (thread
->synch_lock
);
1595 if ((thread
->state
& ThreadState_AbortRequested
) != 0 ||
1596 (thread
->state
& ThreadState_StopRequested
) != 0 ||
1597 (thread
->state
& ThreadState_Stopped
) != 0)
1599 mono_monitor_exit (thread
->synch_lock
);
1603 if ((thread
->state
& ThreadState_Unstarted
) != 0) {
1604 thread
->state
|= ThreadState_Aborted
;
1605 mono_monitor_exit (thread
->synch_lock
);
1609 thread
->state
|= ThreadState_AbortRequested
;
1610 MONO_OBJECT_SETREF (thread
, abort_state
, state
);
1611 thread
->abort_exc
= NULL
;
1613 mono_monitor_exit (thread
->synch_lock
);
1615 THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT
") Abort requested for %p (%"G_GSIZE_FORMAT
")", __func__
, GetCurrentThreadId (), thread
, (gsize
)thread
->tid
));
1617 /* Make sure the thread is awake */
1618 mono_thread_resume (thread
);
1620 signal_thread_state_change (thread
);
1624 ves_icall_System_Threading_Thread_ResetAbort (void)
1626 MonoThread
*thread
= mono_thread_current ();
1628 MONO_ARCH_SAVE_REGS
;
1630 mono_monitor_enter (thread
->synch_lock
);
1632 thread
->state
&= ~ThreadState_AbortRequested
;
1634 if (!thread
->abort_exc
) {
1635 const char *msg
= "Unable to reset abort because no abort was requested";
1636 mono_monitor_exit (thread
->synch_lock
);
1637 mono_raise_exception (mono_get_exception_thread_state (msg
));
1639 thread
->abort_exc
= NULL
;
1640 thread
->abort_state
= NULL
;
1643 mono_monitor_exit (thread
->synch_lock
);
1647 mono_thread_suspend (MonoThread
*thread
)
1649 MONO_ARCH_SAVE_REGS
;
1651 mono_monitor_enter (thread
->synch_lock
);
1653 if ((thread
->state
& ThreadState_Unstarted
) != 0 ||
1654 (thread
->state
& ThreadState_Aborted
) != 0 ||
1655 (thread
->state
& ThreadState_Stopped
) != 0)
1657 mono_monitor_exit (thread
->synch_lock
);
1661 if ((thread
->state
& ThreadState_Suspended
) != 0 ||
1662 (thread
->state
& ThreadState_SuspendRequested
) != 0 ||
1663 (thread
->state
& ThreadState_StopRequested
) != 0)
1665 mono_monitor_exit (thread
->synch_lock
);
1669 thread
->state
|= ThreadState_SuspendRequested
;
1670 mono_monitor_exit (thread
->synch_lock
);
1672 signal_thread_state_change (thread
);
1677 ves_icall_System_Threading_Thread_Suspend (MonoThread
*thread
)
1679 if (!mono_thread_suspend (thread
))
1680 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1684 mono_thread_resume (MonoThread
*thread
)
1686 MONO_ARCH_SAVE_REGS
;
1688 mono_monitor_enter (thread
->synch_lock
);
1690 if ((thread
->state
& ThreadState_SuspendRequested
) != 0) {
1691 thread
->state
&= ~ThreadState_SuspendRequested
;
1692 mono_monitor_exit (thread
->synch_lock
);
1696 if ((thread
->state
& ThreadState_Suspended
) == 0 ||
1697 (thread
->state
& ThreadState_Unstarted
) != 0 ||
1698 (thread
->state
& ThreadState_Aborted
) != 0 ||
1699 (thread
->state
& ThreadState_Stopped
) != 0)
1701 mono_monitor_exit (thread
->synch_lock
);
1705 thread
->resume_event
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
1706 if (thread
->resume_event
== NULL
) {
1707 mono_monitor_exit (thread
->synch_lock
);
1711 /* Awake the thread */
1712 SetEvent (thread
->suspend_event
);
1714 mono_monitor_exit (thread
->synch_lock
);
1716 /* Wait for the thread to awake */
1717 WaitForSingleObject (thread
->resume_event
, INFINITE
);
1718 CloseHandle (thread
->resume_event
);
1719 thread
->resume_event
= NULL
;
1725 ves_icall_System_Threading_Thread_Resume (MonoThread
*thread
)
1727 if (!mono_thread_resume (thread
))
1728 mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
1732 find_wrapper (MonoMethod
*m
, gint no
, gint ilo
, gboolean managed
, gpointer data
)
1737 if (m
->wrapper_type
== MONO_WRAPPER_RUNTIME_INVOKE
||
1738 m
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
||
1739 m
->wrapper_type
== MONO_WRAPPER_XDOMAIN_DISPATCH
)
1741 *((gboolean
*)data
) = TRUE
;
1748 is_running_protected_wrapper (void)
1750 gboolean found
= FALSE
;
1751 mono_stack_walk (find_wrapper
, &found
);
1755 void mono_thread_stop (MonoThread
*thread
)
1757 mono_monitor_enter (thread
->synch_lock
);
1759 if ((thread
->state
& ThreadState_StopRequested
) != 0 ||
1760 (thread
->state
& ThreadState_Stopped
) != 0)
1762 mono_monitor_exit (thread
->synch_lock
);
1766 /* Make sure the thread is awake */
1767 mono_thread_resume (thread
);
1769 thread
->state
|= ThreadState_StopRequested
;
1770 thread
->state
&= ~ThreadState_AbortRequested
;
1772 mono_monitor_exit (thread
->synch_lock
);
1774 signal_thread_state_change (thread
);
1778 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr
)
1780 return *((volatile gint8
*) (ptr
));
1784 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr
)
1786 return *((volatile gint16
*) (ptr
));
1790 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr
)
1792 return *((volatile gint32
*) (ptr
));
1796 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr
)
1798 return *((volatile gint64
*) (ptr
));
1802 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr
)
1804 return (void *) *((volatile void **) ptr
);
1808 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr
, gint8 value
)
1810 *((volatile gint8
*) ptr
) = value
;
1814 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr
, gint16 value
)
1816 *((volatile gint16
*) ptr
) = value
;
1820 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr
, gint32 value
)
1822 *((volatile gint32
*) ptr
) = value
;
1826 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr
, gint64 value
)
1828 *((volatile gint64
*) ptr
) = value
;
1832 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr
, void *value
)
1834 *((volatile void **) ptr
) = value
;
1837 void mono_thread_init (MonoThreadStartCB start_cb
,
1838 MonoThreadAttachCB attach_cb
)
1840 InitializeCriticalSection(&threads_mutex
);
1841 InitializeCriticalSection(&interlocked_mutex
);
1842 InitializeCriticalSection(&contexts_mutex
);
1843 background_change_event
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
1844 g_assert(background_change_event
!= NULL
);
1846 mono_init_static_data_info (&thread_static_info
);
1847 mono_init_static_data_info (&context_static_info
);
1849 current_object_key
=TlsAlloc();
1850 THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__
, current_object_key
));
1852 mono_thread_start_cb
= start_cb
;
1853 mono_thread_attach_cb
= attach_cb
;
1855 /* Get a pseudo handle to the current process. This is just a
1856 * kludge so that wapi can build a process handle if needed.
1857 * As a pseudo handle is returned, we don't need to clean
1860 GetCurrentProcess ();
1863 void mono_thread_cleanup (void)
1865 #if !defined(PLATFORM_WIN32) && !defined(RUN_IN_SUBTHREAD)
1866 /* The main thread must abandon any held mutexes (particularly
1867 * important for named mutexes as they are shared across
1868 * processes, see bug 74680.) This will happen when the
1869 * thread exits, but if it's not running in a subthread it
1870 * won't exit in time.
1872 /* Using non-w32 API is a nasty kludge, but I couldn't find
1873 * anything in the documentation that would let me do this
1874 * here yet still be safe to call on windows.
1876 _wapi_thread_signal_self (mono_environment_exitcode_get ());
1880 /* This stuff needs more testing, it seems one of these
1881 * critical sections can be locked when mono_thread_cleanup is
1884 DeleteCriticalSection (&threads_mutex
);
1885 DeleteCriticalSection (&interlocked_mutex
);
1886 DeleteCriticalSection (&contexts_mutex
);
1887 CloseHandle (background_change_event
);
1890 TlsFree (current_object_key
);
1894 mono_threads_install_cleanup (MonoThreadCleanupFunc func
)
1896 mono_thread_cleanup_fn
= func
;
1900 static void print_tids (gpointer key
, gpointer value
, gpointer user
)
1902 /* GPOINTER_TO_UINT breaks horribly if sizeof(void *) >
1903 * sizeof(uint) and a cast to uint would overflow
1905 /* Older versions of glib don't have G_GSIZE_FORMAT, so just
1906 * print this as a pointer.
1908 g_message ("Waiting for: %p", key
);
1913 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
1914 MonoThread
*threads
[MAXIMUM_WAIT_OBJECTS
];
1918 static void wait_for_tids (struct wait_data
*wait
, guint32 timeout
)
1922 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__
, wait
->num
));
1924 ret
=WaitForMultipleObjectsEx(wait
->num
, wait
->handles
, TRUE
, timeout
, FALSE
);
1926 if(ret
==WAIT_FAILED
) {
1927 /* See the comment in build_wait_tids() */
1928 THREAD_DEBUG (g_message ("%s: Wait failed", __func__
));
1932 for(i
=0; i
<wait
->num
; i
++)
1933 CloseHandle (wait
->handles
[i
]);
1935 if (ret
== WAIT_TIMEOUT
)
1938 for(i
=0; i
<wait
->num
; i
++) {
1939 gsize tid
= wait
->threads
[i
]->tid
;
1941 if(mono_g_hash_table_lookup (threads
, (gpointer
)tid
)!=NULL
) {
1942 /* This thread must have been killed, because
1943 * it hasn't cleaned itself up. (It's just
1944 * possible that the thread exited before the
1945 * parent thread had a chance to store the
1946 * handle, and now there is another pointer to
1947 * the already-exited thread stored. In this
1948 * case, we'll just get two
1949 * mono_profiler_thread_end() calls for the
1953 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT
, __func__
, tid
));
1954 thread_cleanup (wait
->threads
[i
]);
1959 static void wait_for_tids_or_state_change (struct wait_data
*wait
, guint32 timeout
)
1961 guint32 i
, ret
, count
;
1963 THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__
, wait
->num
));
1965 /* Add the thread state change event, so it wakes up if a thread changes
1966 * to background mode.
1969 if (count
< MAXIMUM_WAIT_OBJECTS
) {
1970 wait
->handles
[count
] = background_change_event
;
1974 ret
=WaitForMultipleObjectsEx (count
, wait
->handles
, FALSE
, timeout
, FALSE
);
1976 if(ret
==WAIT_FAILED
) {
1977 /* See the comment in build_wait_tids() */
1978 THREAD_DEBUG (g_message ("%s: Wait failed", __func__
));
1982 for(i
=0; i
<wait
->num
; i
++)
1983 CloseHandle (wait
->handles
[i
]);
1985 if (ret
== WAIT_TIMEOUT
)
1988 if (ret
< wait
->num
) {
1989 gsize tid
= wait
->threads
[ret
]->tid
;
1990 mono_threads_lock ();
1991 if (mono_g_hash_table_lookup (threads
, (gpointer
)tid
)!=NULL
) {
1992 /* See comment in wait_for_tids about thread cleanup */
1993 mono_threads_unlock ();
1994 THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT
, __func__
, tid
));
1995 thread_cleanup (wait
->threads
[ret
]);
1997 mono_threads_unlock ();
2001 static void build_wait_tids (gpointer key
, gpointer value
, gpointer user
)
2003 struct wait_data
*wait
=(struct wait_data
*)user
;
2005 if(wait
->num
<MAXIMUM_WAIT_OBJECTS
) {
2007 MonoThread
*thread
=(MonoThread
*)value
;
2009 /* Ignore background threads, we abort them later */
2010 mono_monitor_enter (thread
->synch_lock
);
2011 if (thread
->state
& ThreadState_Background
) {
2012 THREAD_DEBUG (g_message ("%s: ignoring background thread %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
2013 mono_monitor_exit (thread
->synch_lock
);
2014 return; /* just leave, ignore */
2016 mono_monitor_exit (thread
->synch_lock
);
2018 if (mono_gc_is_finalizer_thread (thread
)) {
2019 THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
2023 if (thread
== mono_thread_current ()) {
2024 THREAD_DEBUG (g_message ("%s: ignoring current thread %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
2028 if (thread
== mono_thread_get_main ()) {
2029 THREAD_DEBUG (g_message ("%s: ignoring main thread %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
2033 handle
= OpenThread (THREAD_ALL_ACCESS
, TRUE
, thread
->tid
);
2034 if (handle
== NULL
) {
2035 THREAD_DEBUG (g_message ("%s: ignoring unopenable thread %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
2039 wait
->handles
[wait
->num
]=handle
;
2040 wait
->threads
[wait
->num
]=thread
;
2043 THREAD_DEBUG (g_message ("%s: adding thread %"G_GSIZE_FORMAT
, __func__
, (gsize
)thread
->tid
));
2045 /* Just ignore the rest, we can't do anything with
2052 remove_and_abort_threads (gpointer key
, gpointer value
, gpointer user
)
2054 struct wait_data
*wait
=(struct wait_data
*)user
;
2055 gsize self
= GetCurrentThreadId ();
2056 MonoThread
*thread
= (MonoThread
*) value
;
2059 if (wait
->num
>= MAXIMUM_WAIT_OBJECTS
)
2062 /* The finalizer thread is not a background thread */
2063 if (thread
->tid
!= self
&& (thread
->state
& ThreadState_Background
) != 0) {
2065 handle
= OpenThread (THREAD_ALL_ACCESS
, TRUE
, thread
->tid
);
2069 if(thread
->state
& ThreadState_AbortRequested
||
2070 thread
->state
& ThreadState_Aborted
) {
2071 THREAD_DEBUG (g_message ("%s: Thread id %"G_GSIZE_FORMAT
" already aborting", __func__
, (gsize
)thread
->tid
));
2075 /* printf ("A: %d\n", wait->num); */
2076 wait
->handles
[wait
->num
]=thread
->handle
;
2077 wait
->threads
[wait
->num
]=thread
;
2080 THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT
"\n", __func__
, (gsize
)thread
->tid
));
2081 mono_thread_stop (thread
);
2085 return (thread
->tid
!= self
&& !mono_gc_is_finalizer_thread (thread
));
2088 void mono_thread_manage (void)
2090 struct wait_data
*wait
=g_new0 (struct wait_data
, 1);
2092 /* join each thread that's still running */
2093 THREAD_DEBUG (g_message ("%s: Joining each running thread...", __func__
));
2095 mono_threads_lock ();
2097 THREAD_DEBUG (g_message("%s: No threads", __func__
));
2098 mono_threads_unlock ();
2101 mono_threads_unlock ();
2104 mono_threads_lock ();
2105 THREAD_DEBUG (g_message ("%s: There are %d threads to join", __func__
, mono_g_hash_table_size (threads
));
2106 mono_g_hash_table_foreach (threads
, print_tids
, NULL
));
2108 ResetEvent (background_change_event
);
2110 mono_g_hash_table_foreach (threads
, build_wait_tids
, wait
);
2111 mono_threads_unlock ();
2113 /* Something to wait for */
2114 wait_for_tids_or_state_change (wait
, INFINITE
);
2116 THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__
, wait
->num
));
2117 } while(wait
->num
>0);
2119 mono_runtime_set_shutting_down ();
2121 THREAD_DEBUG (g_message ("%s: threadpool cleanup", __func__
));
2122 mono_thread_pool_cleanup ();
2125 * Remove everything but the finalizer thread and self.
2126 * Also abort all the background threads
2129 mono_threads_lock ();
2132 mono_g_hash_table_foreach_remove (threads
, remove_and_abort_threads
, wait
);
2134 mono_threads_unlock ();
2136 THREAD_DEBUG (g_message ("%s: wait->num is now %d", __func__
, wait
->num
));
2138 /* Something to wait for */
2139 wait_for_tids (wait
, INFINITE
);
2141 } while (wait
->num
> 0);
2144 * give the subthreads a chance to really quit (this is mainly needed
2145 * to get correct user and system times from getrusage/wait/time(1)).
2146 * This could be removed if we avoid pthread_detach() and use pthread_join().
2148 #ifndef PLATFORM_WIN32
2155 static void terminate_thread (gpointer key
, gpointer value
, gpointer user
)
2157 MonoThread
*thread
=(MonoThread
*)value
;
2159 if(thread
->tid
!= (gsize
)user
) {
2160 /*TerminateThread (thread->handle, -1);*/
2164 void mono_thread_abort_all_other_threads (void)
2166 gsize self
= GetCurrentThreadId ();
2168 mono_threads_lock ();
2169 THREAD_DEBUG (g_message ("%s: There are %d threads to abort", __func__
,
2170 mono_g_hash_table_size (threads
));
2171 mono_g_hash_table_foreach (threads
, print_tids
, NULL
));
2173 mono_g_hash_table_foreach (threads
, terminate_thread
, (gpointer
)self
);
2175 mono_threads_unlock ();
2179 collect_threads (gpointer key
, gpointer value
, gpointer user_data
)
2181 MonoThread
*thread
= (MonoThread
*)value
;
2182 struct wait_data
*wait
= (struct wait_data
*)user_data
;
2185 if (wait
->num
<MAXIMUM_WAIT_OBJECTS
) {
2186 handle
= OpenThread (THREAD_ALL_ACCESS
, TRUE
, thread
->tid
);
2190 wait
->handles
[wait
->num
] = handle
;
2191 wait
->threads
[wait
->num
] = thread
;
2197 * mono_thread_suspend_all_other_threads:
2199 * Suspend all managed threads except the finalizer thread and this thread.
2201 void mono_thread_suspend_all_other_threads (void)
2203 struct wait_data
*wait
= g_new0 (struct wait_data
, 1);
2205 gsize self
= GetCurrentThreadId ();
2207 guint32 eventidx
= 0;
2210 * Make a copy of the hashtable since we can't do anything with
2211 * threads while threads_mutex is held.
2213 mono_threads_lock ();
2214 mono_g_hash_table_foreach (threads
, collect_threads
, wait
);
2215 mono_threads_unlock ();
2217 events
= g_new0 (gpointer
, wait
->num
);
2219 /* Get the suspended events that we'll be waiting for */
2220 for (i
= 0; i
< wait
->num
; ++i
) {
2221 MonoThread
*thread
= wait
->threads
[i
];
2223 if ((thread
->tid
== self
) || mono_gc_is_finalizer_thread (thread
)) {
2224 //CloseHandle (wait->handles [i]);
2225 wait
->threads
[i
] = NULL
; /* ignore this thread in next loop */
2229 mono_monitor_enter (thread
->synch_lock
);
2231 if ((thread
->state
& ThreadState_Suspended
) != 0 ||
2232 (thread
->state
& ThreadState_SuspendRequested
) != 0 ||
2233 (thread
->state
& ThreadState_StopRequested
) != 0 ||
2234 (thread
->state
& ThreadState_Stopped
) != 0) {
2235 mono_monitor_exit (thread
->synch_lock
);
2236 CloseHandle (wait
->handles
[i
]);
2237 wait
->threads
[i
] = NULL
; /* ignore this thread in next loop */
2241 /* Convert abort requests into suspend requests */
2242 if ((thread
->state
& ThreadState_AbortRequested
) != 0)
2243 thread
->state
&= ~ThreadState_AbortRequested
;
2245 thread
->state
|= ThreadState_SuspendRequested
;
2247 if (thread
->suspended_event
== NULL
) {
2248 thread
->suspended_event
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
2249 if (thread
->suspended_event
== NULL
) {
2250 /* Forget this one and go on to the next */
2251 mono_monitor_exit (thread
->synch_lock
);
2256 events
[eventidx
++] = thread
->suspended_event
;
2257 mono_monitor_exit (thread
->synch_lock
);
2259 /* Signal the thread to suspend */
2260 signal_thread_state_change (thread
);
2263 WaitForMultipleObjectsEx (eventidx
, events
, TRUE
, INFINITE
, FALSE
);
2264 for (i
= 0; i
< wait
->num
; ++i
) {
2265 MonoThread
*thread
= wait
->threads
[i
];
2270 mono_monitor_enter (thread
->synch_lock
);
2271 CloseHandle (thread
->suspended_event
);
2272 thread
->suspended_event
= NULL
;
2273 mono_monitor_exit (thread
->synch_lock
);
2281 * mono_threads_request_thread_dump:
2283 * Ask all threads except the current to print their stacktrace to stdout.
2286 mono_threads_request_thread_dump (void)
2288 struct wait_data
*wait
= g_new0 (struct wait_data
, 1);
2292 * Make a copy of the hashtable since we can't do anything with
2293 * threads while threads_mutex is held.
2295 mono_threads_lock ();
2296 mono_g_hash_table_foreach (threads
, collect_threads
, wait
);
2297 mono_threads_unlock ();
2299 for (i
= 0; i
< wait
->num
; ++i
) {
2300 MonoThread
*thread
= wait
->threads
[i
];
2302 if (!mono_gc_is_finalizer_thread (thread
) && (thread
!= mono_thread_current ()) && !thread
->thread_dump_requested
) {
2303 thread
->thread_dump_requested
= TRUE
;
2305 signal_thread_state_change (thread
);
2308 CloseHandle (wait
->handles
[i
]);
2313 * mono_thread_push_appdomain_ref:
2315 * Register that the current thread may have references to objects in domain
2316 * @domain on its stack. Each call to this function should be paired with a
2317 * call to pop_appdomain_ref.
2320 mono_thread_push_appdomain_ref (MonoDomain
*domain
)
2322 MonoThread
*thread
= mono_thread_current ();
2325 /* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
2326 mono_threads_lock ();
2327 thread
->appdomain_refs
= g_slist_prepend (thread
->appdomain_refs
, domain
);
2328 mono_threads_unlock ();
2333 mono_thread_pop_appdomain_ref (void)
2335 MonoThread
*thread
= mono_thread_current ();
2338 /* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
2339 mono_threads_lock ();
2340 /* FIXME: How can the list be empty ? */
2341 if (thread
->appdomain_refs
)
2342 thread
->appdomain_refs
= g_slist_remove (thread
->appdomain_refs
, thread
->appdomain_refs
->data
);
2343 mono_threads_unlock ();
2348 mono_thread_has_appdomain_ref (MonoThread
*thread
, MonoDomain
*domain
)
2351 mono_threads_lock ();
2352 res
= g_slist_find (thread
->appdomain_refs
, domain
) != NULL
;
2353 mono_threads_unlock ();
2357 typedef struct abort_appdomain_data
{
2358 struct wait_data wait
;
2360 } abort_appdomain_data
;
2363 abort_appdomain_thread (gpointer key
, gpointer value
, gpointer user_data
)
2365 MonoThread
*thread
= (MonoThread
*)value
;
2366 abort_appdomain_data
*data
= (abort_appdomain_data
*)user_data
;
2367 MonoDomain
*domain
= data
->domain
;
2369 if (mono_thread_has_appdomain_ref (thread
, domain
)) {
2370 HANDLE handle
= OpenThread (THREAD_ALL_ACCESS
, TRUE
, thread
->tid
);
2374 /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
2376 ves_icall_System_Threading_Thread_Abort (thread
, NULL
);
2378 if(data
->wait
.num
<MAXIMUM_WAIT_OBJECTS
) {
2379 data
->wait
.handles
[data
->wait
.num
] = handle
;
2380 data
->wait
.threads
[data
->wait
.num
] = thread
;
2383 /* Just ignore the rest, we can't do anything with
2391 * mono_threads_abort_appdomain_threads:
2393 * Abort threads which has references to the given appdomain.
2396 mono_threads_abort_appdomain_threads (MonoDomain
*domain
, int timeout
)
2398 abort_appdomain_data user_data
;
2401 /* printf ("ABORT BEGIN.\n"); */
2403 start_time
= GetTickCount ();
2405 mono_threads_lock ();
2407 user_data
.domain
= domain
;
2408 user_data
.wait
.num
= 0;
2409 mono_g_hash_table_foreach (threads
, abort_appdomain_thread
, &user_data
);
2410 mono_threads_unlock ();
2412 if (user_data
.wait
.num
> 0)
2414 * We should wait for the threads either to abort, or to leave the
2415 * domain. We can't do the latter, so we wait with a timeout.
2417 wait_for_tids (&user_data
.wait
, 100);
2419 /* Update remaining time */
2420 timeout
-= GetTickCount () - start_time
;
2421 start_time
= GetTickCount ();
2426 while (user_data
.wait
.num
> 0);
2428 /* printf ("ABORT DONE.\n"); */
2434 clear_cached_culture (gpointer key
, gpointer value
, gpointer user_data
)
2436 MonoThread
*thread
= (MonoThread
*)value
;
2437 MonoDomain
*domain
= (MonoDomain
*)user_data
;
2440 /* No locking needed here */
2441 /* FIXME: why no locking? writes to the cache are protected with synch_lock above */
2443 if (thread
->cached_culture_info
) {
2444 for (i
= 0; i
< NUM_CACHED_CULTURES
* 2; ++i
) {
2445 MonoObject
*obj
= mono_array_get (thread
->cached_culture_info
, MonoObject
*, i
);
2446 if (obj
&& obj
->vtable
->domain
== domain
)
2447 mono_array_set (thread
->cached_culture_info
, MonoObject
*, i
, NULL
);
2453 * mono_threads_clear_cached_culture:
2455 * Clear the cached_current_culture from all threads if it is in the
2459 mono_threads_clear_cached_culture (MonoDomain
*domain
)
2461 mono_threads_lock ();
2462 mono_g_hash_table_foreach (threads
, clear_cached_culture
, domain
);
2463 mono_threads_unlock ();
2467 * mono_thread_get_pending_exception:
2469 * Return an exception which needs to be raised when leaving a catch clause.
2470 * This is used for undeniable exception propagation.
2473 mono_thread_get_pending_exception (void)
2475 MonoThread
*thread
= mono_thread_current ();
2477 MONO_ARCH_SAVE_REGS
;
2479 if (thread
&& thread
->abort_exc
&& !is_running_protected_wrapper ()) {
2481 * FIXME: Clear the abort exception and return an AppDomainUnloaded
2482 * exception if the thread no longer references a dying appdomain.
2484 thread
->abort_exc
->trace_ips
= NULL
;
2485 thread
->abort_exc
->stack_trace
= NULL
;
2486 return thread
->abort_exc
;
2492 #define NUM_STATIC_DATA_IDX 8
2493 static const int static_data_size
[NUM_STATIC_DATA_IDX
] = {
2494 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
2499 * mono_alloc_static_data
2501 * Allocate memory blocks for storing threads or context static data
2504 mono_alloc_static_data (gpointer
**static_data_ptr
, guint32 offset
)
2506 guint idx
= (offset
>> 24) - 1;
2509 gpointer
* static_data
= *static_data_ptr
;
2511 static_data
= mono_gc_alloc_fixed (static_data_size
[0], NULL
);
2512 *static_data_ptr
= static_data
;
2513 static_data
[0] = static_data
;
2516 for (i
= 1; i
<= idx
; ++i
) {
2517 if (static_data
[i
])
2519 static_data
[i
] = mono_gc_alloc_fixed (static_data_size
[i
], NULL
);
2524 * mono_init_static_data_info
2526 * Initializes static data counters
2528 static void mono_init_static_data_info (StaticDataInfo
*static_data
)
2530 static_data
->idx
= 0;
2531 static_data
->offset
= 0;
2535 * mono_alloc_static_data_slot
2537 * Generates an offset for static data. static_data contains the counters
2538 * used to generate it.
2541 mono_alloc_static_data_slot (StaticDataInfo
*static_data
, guint32 size
, guint32 align
)
2545 if (!static_data
->idx
&& !static_data
->offset
) {
2547 * we use the first chunk of the first allocation also as
2548 * an array for the rest of the data
2550 static_data
->offset
= sizeof (gpointer
) * NUM_STATIC_DATA_IDX
;
2552 static_data
->offset
+= align
- 1;
2553 static_data
->offset
&= ~(align
- 1);
2554 if (static_data
->offset
+ size
>= static_data_size
[static_data
->idx
]) {
2555 static_data
->idx
++;
2556 g_assert (size
<= static_data_size
[static_data
->idx
]);
2558 * massive unloading and reloading of domains with thread-static
2559 * data may eventually exceed the allocated storage...
2560 * Need to check what the MS runtime does in that case.
2561 * Note that for each appdomain, we need to allocate a separate
2562 * thread data slot for security reasons. We could keep track
2563 * of the slots per-domain and when the domain is unloaded
2564 * out the slots on a sort of free list.
2566 g_assert (static_data
->idx
< NUM_STATIC_DATA_IDX
);
2567 static_data
->offset
= 0;
2569 offset
= static_data
->offset
| ((static_data
->idx
+ 1) << 24);
2570 static_data
->offset
+= size
;
2575 * ensure thread static fields already allocated are valid for thread
2576 * This function is called when a thread is created or on thread attach.
2579 thread_adjust_static_data (MonoThread
*thread
)
2583 mono_threads_lock ();
2584 if (thread_static_info
.offset
|| thread_static_info
.idx
> 0) {
2585 /* get the current allocated size */
2586 offset
= thread_static_info
.offset
| ((thread_static_info
.idx
+ 1) << 24);
2587 mono_alloc_static_data (&(thread
->static_data
), offset
);
2589 mono_threads_unlock ();
2593 alloc_thread_static_data_helper (gpointer key
, gpointer value
, gpointer user
)
2595 MonoThread
*thread
= value
;
2596 guint32 offset
= GPOINTER_TO_UINT (user
);
2598 mono_alloc_static_data (&(thread
->static_data
), offset
);
2602 * The offset for a special static variable is composed of three parts:
2603 * a bit that indicates the type of static data (0:thread, 1:context),
2604 * an index in the array of chunks of memory for the thread (thread->static_data)
2605 * and an offset in that chunk of mem. This allows allocating less memory in the
2610 mono_alloc_special_static_data (guint32 static_type
, guint32 size
, guint32 align
)
2613 if (static_type
== SPECIAL_STATIC_THREAD
)
2615 mono_threads_lock ();
2616 offset
= mono_alloc_static_data_slot (&thread_static_info
, size
, align
);
2617 /* This can be called during startup */
2618 if (threads
!= NULL
)
2619 mono_g_hash_table_foreach (threads
, alloc_thread_static_data_helper
, GUINT_TO_POINTER (offset
));
2620 mono_threads_unlock ();
2624 g_assert (static_type
== SPECIAL_STATIC_CONTEXT
);
2625 mono_contexts_lock ();
2626 offset
= mono_alloc_static_data_slot (&context_static_info
, size
, align
);
2627 mono_contexts_unlock ();
2628 offset
|= 0x80000000; /* Set the high bit to indicate context static data */
2634 mono_get_special_static_data (guint32 offset
)
2636 /* The high bit means either thread (0) or static (1) data. */
2638 guint32 static_type
= (offset
& 0x80000000);
2641 offset
&= 0x7fffffff;
2642 idx
= (offset
>> 24) - 1;
2644 if (static_type
== 0)
2646 MonoThread
*thread
= mono_thread_current ();
2647 return ((char*) thread
->static_data
[idx
]) + (offset
& 0xffffff);
2651 /* Allocate static data block under demand, since we don't have a list
2654 MonoAppContext
*context
= mono_context_get ();
2655 if (!context
->static_data
|| !context
->static_data
[idx
]) {
2656 mono_contexts_lock ();
2657 mono_alloc_static_data (&(context
->static_data
), offset
);
2658 mono_contexts_unlock ();
2660 return ((char*) context
->static_data
[idx
]) + (offset
& 0xffffff);
2664 static MonoClassField
*local_slots
= NULL
;
2667 /* local tls data to get locals_slot from a thread */
2670 /* index in the locals_slot array */
2675 clear_local_slot (gpointer key
, gpointer value
, gpointer user_data
)
2677 LocalSlotID
*sid
= user_data
;
2678 MonoThread
*thread
= (MonoThread
*)value
;
2679 MonoArray
*slots_array
;
2681 * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
2682 * it is for the right domain, so we need to check if it is allocated an initialized
2683 * for the current thread.
2685 /*g_print ("handling thread %p\n", thread);*/
2686 if (!thread
->static_data
|| !thread
->static_data
[sid
->idx
])
2688 slots_array
= *(MonoArray
**)(((char*) thread
->static_data
[sid
->idx
]) + (sid
->offset
& 0xffffff));
2689 if (!slots_array
|| sid
->slot
>= mono_array_length (slots_array
))
2691 mono_array_set (slots_array
, MonoObject
*, sid
->slot
, NULL
);
2695 mono_thread_free_local_slot_values (int slot
, MonoBoolean thread_local
)
2703 local_slots
= mono_class_get_field_from_name (mono_defaults
.thread_class
, "local_slots");
2705 g_warning ("local_slots field not found in Thread class");
2709 domain
= mono_domain_get ();
2710 mono_domain_lock (domain
);
2711 if (domain
->special_static_fields
)
2712 addr
= g_hash_table_lookup (domain
->special_static_fields
, local_slots
);
2713 mono_domain_unlock (domain
);
2716 /*g_print ("freeing slot %d at %p\n", slot, addr);*/
2717 sid
.offset
= GPOINTER_TO_UINT (addr
);
2718 sid
.offset
&= 0x7fffffff;
2719 sid
.idx
= (sid
.offset
>> 24) - 1;
2720 mono_threads_lock ();
2721 mono_g_hash_table_foreach (threads
, clear_local_slot
, &sid
);
2722 mono_threads_unlock ();
2724 /* FIXME: clear the slot for MonoAppContexts, too */
2729 static CALLBACK
void dummy_apc (ULONG_PTR param
)
2733 static guint32
dummy_apc (gpointer param
)
2740 * mono_thread_execute_interruption
2742 * Performs the operation that the requested thread state requires (abort,
2745 static MonoException
* mono_thread_execute_interruption (MonoThread
*thread
)
2747 mono_monitor_enter (thread
->synch_lock
);
2749 if (thread
->interruption_requested
) {
2750 /* this will consume pending APC calls */
2751 WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE
);
2752 InterlockedDecrement (&thread_interruption_requested
);
2753 thread
->interruption_requested
= FALSE
;
2756 if ((thread
->state
& ThreadState_AbortRequested
) != 0) {
2757 if (thread
->abort_exc
== NULL
)
2758 MONO_OBJECT_SETREF (thread
, abort_exc
, mono_get_exception_thread_abort ());
2759 mono_monitor_exit (thread
->synch_lock
);
2760 return thread
->abort_exc
;
2762 else if ((thread
->state
& ThreadState_SuspendRequested
) != 0) {
2763 thread
->state
&= ~ThreadState_SuspendRequested
;
2764 thread
->state
|= ThreadState_Suspended
;
2765 thread
->suspend_event
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
2766 if (thread
->suspend_event
== NULL
) {
2767 mono_monitor_exit (thread
->synch_lock
);
2770 if (thread
->suspended_event
)
2771 SetEvent (thread
->suspended_event
);
2772 mono_monitor_exit (thread
->synch_lock
);
2774 WaitForSingleObject (thread
->suspend_event
, INFINITE
);
2776 mono_monitor_enter (thread
->synch_lock
);
2777 CloseHandle (thread
->suspend_event
);
2778 thread
->suspend_event
= NULL
;
2779 thread
->state
&= ~ThreadState_Suspended
;
2781 /* The thread that requested the resume will have replaced this event
2782 * and will be waiting for it
2784 SetEvent (thread
->resume_event
);
2785 mono_monitor_exit (thread
->synch_lock
);
2788 else if ((thread
->state
& ThreadState_StopRequested
) != 0) {
2789 /* FIXME: do this through the JIT? */
2790 mono_monitor_exit (thread
->synch_lock
);
2791 mono_thread_exit ();
2793 } else if (thread
->thread_interrupt_requested
) {
2794 mono_monitor_exit (thread
->synch_lock
);
2795 return(mono_get_exception_thread_interrupted ());
2798 mono_monitor_exit (thread
->synch_lock
);
2803 * mono_thread_request_interruption
2805 * A signal handler can call this method to request the interruption of a
2806 * thread. The result of the interruption will depend on the current state of
2807 * the thread. If the result is an exception that needs to be throw, it is
2808 * provided as return value.
2810 MonoException
* mono_thread_request_interruption (gboolean running_managed
)
2812 MonoThread
*thread
= mono_thread_current ();
2814 /* The thread may already be stopping */
2818 mono_monitor_enter (thread
->synch_lock
);
2820 if (thread
->interruption_requested
) {
2821 mono_monitor_exit (thread
->synch_lock
);
2825 if (!running_managed
|| is_running_protected_wrapper ()) {
2826 /* Can't stop while in unmanaged code. Increase the global interruption
2827 request count. When exiting the unmanaged method the count will be
2828 checked and the thread will be interrupted. */
2830 InterlockedIncrement (&thread_interruption_requested
);
2831 thread
->interruption_requested
= TRUE
;
2832 mono_monitor_exit (thread
->synch_lock
);
2834 /* this will awake the thread if it is in WaitForSingleObject
2836 QueueUserAPC ((PAPCFUNC
)dummy_apc
, thread
->handle
, NULL
);
2840 mono_monitor_exit (thread
->synch_lock
);
2841 return mono_thread_execute_interruption (thread
);
2845 gboolean
mono_thread_interruption_requested ()
2847 if (thread_interruption_requested
) {
2848 MonoThread
*thread
= mono_thread_current ();
2849 /* The thread may already be stopping */
2851 return (thread
->interruption_requested
);
2856 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection
)
2858 MonoThread
*thread
= mono_thread_current ();
2860 /* The thread may already be stopping */
2864 if (thread
->interruption_requested
&& (bypass_abort_protection
|| !is_running_protected_wrapper ())) {
2865 MonoException
* exc
= mono_thread_execute_interruption (thread
);
2866 if (exc
) mono_raise_exception (exc
);
2871 * Performs the interruption of the current thread, if one has been requested,
2872 * and the thread is not running a protected wrapper.
2874 void mono_thread_interruption_checkpoint ()
2876 mono_thread_interruption_checkpoint_request (FALSE
);
2880 * Performs the interruption of the current thread, if one has been requested.
2882 void mono_thread_force_interruption_checkpoint ()
2884 mono_thread_interruption_checkpoint_request (TRUE
);
2888 * mono_thread_interruption_request_flag:
2890 * Returns the address of a flag that will be non-zero if an interruption has
2891 * been requested for a thread. The thread to interrupt may not be the current
2892 * thread, so an additional call to mono_thread_interruption_requested() or
2893 * mono_thread_interruption_checkpoint() is allways needed if the flag is not
2896 gint32
* mono_thread_interruption_request_flag ()
2898 return &thread_interruption_requested
;