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.
16 #include <mono/metadata/object.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/profiler-private.h>
19 #include <mono/metadata/threads.h>
20 #include <mono/metadata/threadpool.h>
21 #include <mono/metadata/threads-types.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/environment.h>
24 #include <mono/metadata/monitor.h>
25 #include <mono/metadata/gc-internal.h>
26 #include <mono/io-layer/io-layer.h>
28 #include <mono/os/gc_wrapper.h>
31 #undef THREAD_WAIT_DEBUG
35 guint32 (*func)(void *);
52 * The "os_handle" field of the WaitHandle class.
54 static MonoClassField *wait_handle_os_handle_field = NULL;
56 /* Controls access to the 'threads' hash table */
57 static CRITICAL_SECTION threads_mutex;
59 /* Controls access to context static data */
60 static CRITICAL_SECTION contexts_mutex;
62 /* Holds current status of static data heap */
63 static StaticDataInfo thread_static_info;
64 static StaticDataInfo context_static_info;
66 /* The hash of existing threads (key is thread ID) that need joining
69 static MonoGHashTable *threads=NULL;
71 /* The TLS key that holds the MonoObject assigned to each thread */
72 static guint32 current_object_key = -1;
74 /* function called at thread start */
75 static MonoThreadStartCB mono_thread_start_cb = NULL;
77 /* function called at thread attach */
78 static MonoThreadAttachCB mono_thread_attach_cb = NULL;
80 /* function called at thread cleanup */
81 static MonoThreadCleanupFunc mono_thread_cleanup = NULL;
83 /* function called when a new thread has been created */
84 static MonoThreadCallbacks *mono_thread_callbacks = NULL;
86 /* The TLS key that holds the LocalDataStoreSlot hash in each thread */
87 static guint32 slothash_key = -1;
89 /* The default stack size for each thread */
90 static guint32 default_stacksize = 0;
92 static void thread_adjust_static_data (MonoThread *thread);
93 static void mono_init_static_data_info (StaticDataInfo *static_data);
94 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
96 /* Spin lock for InterlockedXXX 64 bit functions */
97 static CRITICAL_SECTION interlocked_mutex;
99 /* handle_store() and handle_remove() manage the array of threads that
100 * still need to be waited for when the main thread exits.
102 static void handle_store(MonoThread *thread)
104 EnterCriticalSection(&threads_mutex);
107 g_message(G_GNUC_PRETTY_FUNCTION ": thread %p ID %d", thread,
112 threads=mono_g_hash_table_new(NULL, NULL);
115 /* We don't need to duplicate thread->handle, because it is
116 * only closed when the thread object is finalized by the GC.
118 mono_g_hash_table_insert(threads, GUINT_TO_POINTER(thread->tid), thread);
119 LeaveCriticalSection(&threads_mutex);
122 static void handle_remove(guint32 tid)
125 g_message(G_GNUC_PRETTY_FUNCTION ": thread ID %d", tid);
128 EnterCriticalSection(&threads_mutex);
131 mono_g_hash_table_remove (threads, GUINT_TO_POINTER(tid));
133 LeaveCriticalSection(&threads_mutex);
135 /* Don't close the handle here, wait for the object finalizer
136 * to do it. Otherwise, the following race condition applies:
138 * 1) Thread exits (and handle_remove() closes the handle)
140 * 2) Some other handle is reassigned the same slot
142 * 3) Another thread tries to join the first thread, and
143 * blocks waiting for the reassigned handle to be signalled
144 * (which might never happen). This is possible, because the
145 * thread calling Join() still has a reference to the first
150 static void thread_cleanup (MonoThread *thread)
152 mono_monitor_try_enter ((MonoObject *)thread, INFINITE);
153 thread->state |= ThreadState_Stopped;
154 mono_monitor_exit ((MonoObject *)thread);
156 mono_profiler_thread_end (thread->tid);
157 handle_remove (thread->tid);
159 mono_thread_pop_appdomain_ref ();
161 if (mono_thread_cleanup)
162 mono_thread_cleanup (thread);
165 static guint32 start_wrapper(void *data)
167 struct StartInfo *start_info=(struct StartInfo *)data;
168 guint32 (*start_func)(void *);
171 MonoThread *thread=start_info->obj;
174 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Start wrapper",
175 GetCurrentThreadId ());
178 /* We can be sure start_info->obj->tid and
179 * start_info->obj->handle have been set, because the thread
180 * was created suspended, and these values were set before the
186 TlsSetValue (current_object_key, thread);
188 if (!mono_domain_set (start_info->domain, FALSE)) {
189 /* No point in raising an appdomain_unloaded exception here */
190 /* FIXME: Cleanup here */
194 start_func = start_info->func;
195 this = start_info->this;
197 /* This MUST be called before any managed code can be
198 * executed, as it calls the callback function that (for the
199 * jit) sets the lmf marker.
201 mono_thread_new_init (tid, &tid, start_func);
202 thread->stack_ptr = &tid;
205 g_message (G_GNUC_PRETTY_FUNCTION
206 ": (%d,%d) Setting thread stack to %p",
207 GetCurrentThreadId (), getpid (), thread->stack_ptr);
211 g_message (G_GNUC_PRETTY_FUNCTION
212 ": (%d) Setting current_object_key to %p",
213 GetCurrentThreadId (), thread);
216 mono_profiler_thread_start (tid);
218 if(thread->start_notify!=NULL) {
219 /* Let the thread that called Start() know we're
222 ReleaseSemaphore (thread->start_notify, 1, NULL);
227 /* Every thread references the appdomain which created it */
228 mono_thread_push_appdomain_ref (mono_domain_get ());
230 thread_adjust_static_data (thread);
234 /* If the thread calls ExitThread at all, this remaining code
235 * will not be executed, but the main thread will eventually
236 * call thread_cleanup() on this thread's behalf.
240 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Start wrapper terminating",
241 GetCurrentThreadId ());
244 /* Remove the reference to the thread object in the TLS data,
245 * so the thread object can be finalized. This won't be
246 * reached if the thread threw an uncaught exception, so those
247 * thread handles will stay referenced :-( (This is due to
248 * missing support for scanning thread-specific data in the
249 * Boehm GC - the io-layer keeps a GC-visible hash of pointers
252 TlsSetValue (current_object_key, NULL);
254 thread_cleanup (thread);
259 void mono_thread_new_init (guint32 tid, gpointer stack_start, gpointer func)
261 if (mono_thread_start_cb) {
262 mono_thread_start_cb (tid, stack_start, func);
265 if (mono_thread_callbacks)
266 (* mono_thread_callbacks->thread_created) (tid, stack_start, func);
269 void mono_threads_set_default_stacksize (guint32 stacksize)
271 default_stacksize = stacksize;
274 guint32 mono_threads_get_default_stacksize (void)
276 return default_stacksize;
279 void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
282 HANDLE thread_handle;
283 struct StartInfo *start_info;
286 thread=(MonoThread *)mono_object_new (domain,
287 mono_defaults.thread_class);
289 start_info=g_new0 (struct StartInfo, 1);
290 start_info->func = func;
291 start_info->obj = thread;
292 start_info->domain = domain;
293 start_info->this = arg;
295 /* Create suspended, so we can do some housekeeping before the thread
298 thread_handle = CreateThread(NULL, default_stacksize, start_wrapper, start_info,
299 CREATE_SUSPENDED, &tid);
301 g_message(G_GNUC_PRETTY_FUNCTION ": Started thread ID %d (handle %p)",
304 g_assert (thread_handle);
306 thread->handle=thread_handle;
309 handle_store(thread);
311 ResumeThread (thread_handle);
315 mono_thread_attach (MonoDomain *domain)
318 HANDLE thread_handle;
321 if ((thread = mono_thread_current ())) {
322 g_warning ("mono_thread_attach called for an already attached thread");
323 if (mono_thread_attach_cb) {
324 mono_thread_attach_cb (tid, &tid);
329 thread = (MonoThread *)mono_object_new (domain,
330 mono_defaults.thread_class);
332 thread_handle = GetCurrentThread ();
333 g_assert (thread_handle);
335 tid=GetCurrentThreadId ();
337 thread->handle=thread_handle;
341 g_message(G_GNUC_PRETTY_FUNCTION ": Attached thread ID %d (handle %p)",
345 handle_store(thread);
348 g_message (G_GNUC_PRETTY_FUNCTION
349 ": (%d) Setting current_object_key to %p",
350 GetCurrentThreadId (), thread);
353 TlsSetValue (current_object_key, thread);
354 mono_domain_set (domain, TRUE);
356 thread_adjust_static_data (thread);
358 if (mono_thread_attach_cb) {
359 mono_thread_attach_cb (tid, &tid);
366 ves_icall_System_Threading_ThreadPool_GetAvailableThreads (int *workerThreads, int *completionPortThreads)
372 busy = (int) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
373 *workerThreads = mono_max_worker_threads - busy;
374 *completionPortThreads = 0;
378 ves_icall_System_Threading_ThreadPool_GetMaxThreads (int *workerThreads, int *completionPortThreads)
383 *workerThreads = mono_max_worker_threads;
384 *completionPortThreads = 0;
387 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
390 MonoMulticastDelegate *delegate = (MonoMulticastDelegate*)start;
391 guint32 (*start_func)(void *);
392 struct StartInfo *start_info;
400 g_message(G_GNUC_PRETTY_FUNCTION
401 ": Trying to start a new thread: this (%p) start (%p)",
405 im = mono_get_delegate_invoke (start->vtable->klass);
406 if (mono_thread_callbacks)
407 start_func = (* mono_thread_callbacks->thread_start_compile_func) (im);
409 start_func = mono_compile_method (im);
411 if(start_func==NULL) {
412 g_warning(G_GNUC_PRETTY_FUNCTION
413 ": Can't locate start method!");
416 /* This is freed in start_wrapper */
417 start_info = g_new0 (struct StartInfo, 1);
418 start_info->func = start_func;
419 start_info->this = delegate;
420 start_info->obj = this;
421 start_info->domain = mono_domain_get ();
423 this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
424 if(this->start_notify==NULL) {
425 g_warning (G_GNUC_PRETTY_FUNCTION ": CreateSemaphore error 0x%x", GetLastError ());
429 thread=CreateThread(NULL, default_stacksize, start_wrapper, start_info,
430 CREATE_SUSPENDED, &tid);
432 g_warning(G_GNUC_PRETTY_FUNCTION
433 ": CreateThread error 0x%x", GetLastError());
440 /* Don't call handle_store() here, delay it to Start.
441 * We can't join a thread (trying to will just block
442 * forever) until it actually starts running, so don't
443 * store the handle till then.
447 g_message(G_GNUC_PRETTY_FUNCTION
448 ": Started thread ID %d (handle %p)", tid, thread);
455 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
461 g_message (G_GNUC_PRETTY_FUNCTION ": Closing thread %p, handle %p",
465 CloseHandle (thread);
468 void ves_icall_System_Threading_Thread_Start_internal(MonoThread *this,
474 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Launching thread %p (%d)",
475 GetCurrentThreadId (), this, this->tid);
478 /* Only store the handle when the thread is about to be
479 * launched, to avoid the main thread deadlocking while trying
480 * to clean up a thread that will never be signalled.
484 if (mono_thread_callbacks)
485 (* mono_thread_callbacks->start_resume) (this->tid);
487 ResumeThread(thread);
489 if (mono_thread_callbacks)
490 (* mono_thread_callbacks->end_resume) (this->tid);
492 if(this->start_notify!=NULL) {
493 /* Wait for the thread to set up its TLS data etc, so
494 * theres no potential race condition if someone tries
495 * to look up the data believing the thread has
500 g_message(G_GNUC_PRETTY_FUNCTION
501 ": (%d) waiting for thread %p (%d) to start",
502 GetCurrentThreadId (), this, this->tid);
505 WaitForSingleObject (this->start_notify, INFINITE);
506 CloseHandle (this->start_notify);
507 this->start_notify=NULL;
511 g_message(G_GNUC_PRETTY_FUNCTION
512 ": (%d) Done launching thread %p (%d)",
513 GetCurrentThreadId (), this, this->tid);
517 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
522 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
529 ves_icall_System_Threading_Thread_GetDomainID (void)
533 return mono_domain_get()->domain_id;
537 ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
542 return mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
546 ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
549 g_free (this_obj->name);
551 this_obj->name = g_new (gunichar2, mono_string_length (name));
552 memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
553 this_obj->name_len = mono_string_length (name);
556 this_obj->name = NULL;
560 mono_thread_current (void)
566 /* Find the current thread object */
567 thread=TlsGetValue (current_object_key);
570 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", thread);
576 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
577 int ms, HANDLE thread)
587 g_message (G_GNUC_PRETTY_FUNCTION ": joining thread handle %p, %d ms",
591 ret=WaitForSingleObject(thread, ms);
592 if(ret==WAIT_OBJECT_0) {
594 g_message (G_GNUC_PRETTY_FUNCTION ": join successful");
601 g_message (G_GNUC_PRETTY_FUNCTION ": join failed");
607 void ves_icall_System_Threading_Thread_SlotHash_store(MonoObject *data)
612 g_message(G_GNUC_PRETTY_FUNCTION ": Storing key %p", data);
615 /* Object location stored here */
616 TlsSetValue(slothash_key, data);
619 MonoObject *ves_icall_System_Threading_Thread_SlotHash_lookup(void)
625 data=TlsGetValue(slothash_key);
628 g_message(G_GNUC_PRETTY_FUNCTION ": Retrieved key %p", data);
634 /* FIXME: exitContext isnt documented */
635 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
641 MonoObject *waitHandle;
646 numhandles = mono_array_length(mono_handles);
647 handles = g_new0(HANDLE, numhandles);
649 if (wait_handle_os_handle_field == 0) {
650 /* Get the field os_handle which will contain the actual handle */
651 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
652 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
655 for(i = 0; i < numhandles; i++) {
656 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
657 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
664 ret=WaitForMultipleObjects(numhandles, handles, TRUE, ms);
668 if(ret==WAIT_FAILED) {
669 #ifdef THREAD_WAIT_DEBUG
670 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
671 GetCurrentThreadId ());
674 } else if(ret==WAIT_TIMEOUT) {
675 #ifdef THREAD_WAIT_DEBUG
676 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait timed out",
677 GetCurrentThreadId ());
685 /* FIXME: exitContext isnt documented */
686 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
692 MonoObject *waitHandle;
697 numhandles = mono_array_length(mono_handles);
698 handles = g_new0(HANDLE, numhandles);
700 if (wait_handle_os_handle_field == 0) {
701 /* Get the field os_handle which will contain the actual handle */
702 klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");
703 wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
706 for(i = 0; i < numhandles; i++) {
707 waitHandle = mono_array_get(mono_handles, MonoObject*, i);
708 mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
715 ret=WaitForMultipleObjects(numhandles, handles, FALSE, ms);
719 #ifdef THREAD_WAIT_DEBUG
720 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) returning %d",
721 GetCurrentThreadId (), ret);
725 * These need to be here. See MSDN dos on WaitForMultipleObjects.
727 if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
728 return ret - WAIT_OBJECT_0;
730 else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
731 return ret - WAIT_ABANDONED_0;
738 /* FIXME: exitContext isnt documented */
739 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
745 #ifdef THREAD_WAIT_DEBUG
746 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) waiting for %p, %d ms",
747 GetCurrentThreadId (), handle, ms);
754 ret=WaitForSingleObject(handle, ms);
755 if(ret==WAIT_FAILED) {
756 #ifdef THREAD_WAIT_DEBUG
757 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
758 GetCurrentThreadId ());
761 } else if(ret==WAIT_TIMEOUT) {
762 #ifdef THREAD_WAIT_DEBUG
763 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait timed out",
764 GetCurrentThreadId ());
772 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name)
776 return(CreateMutex (NULL, owned,
777 name==NULL?NULL:mono_string_chars (name)));
780 void ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) {
783 ReleaseMutex(handle);
786 HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name) {
789 return(CreateEvent (NULL, manual, initial,
790 name==NULL?NULL:mono_string_chars (name)));
793 gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle) {
796 return (SetEvent(handle));
799 gboolean ves_icall_System_Threading_Events_ResetEvent_internal (HANDLE handle) {
802 return (ResetEvent(handle));
806 ves_icall_System_Threading_Events_CloseEvent_internal (HANDLE handle) {
809 CloseHandle (handle);
812 gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
816 return InterlockedIncrement (location);
819 gint64 ves_icall_System_Threading_Interlocked_Increment_Long (gint64 *location)
826 EnterCriticalSection(&interlocked_mutex);
828 lowret = InterlockedIncrement((gint32 *) location);
830 highret = InterlockedIncrement((gint32 *) location + 1);
832 highret = *((gint32 *) location + 1);
834 LeaveCriticalSection(&interlocked_mutex);
836 return (gint64) highret << 32 | (gint64) lowret;
839 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int (gint32 *location)
843 return InterlockedDecrement(location);
846 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long (gint64 * location)
853 EnterCriticalSection(&interlocked_mutex);
855 lowret = InterlockedDecrement((gint32 *) location);
857 highret = InterlockedDecrement((gint32 *) location + 1);
859 highret = *((gint32 *) location + 1);
861 LeaveCriticalSection(&interlocked_mutex);
863 return (gint64) highret << 32 | (gint64) lowret;
866 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location1, gint32 value)
870 return InterlockedExchange(location1, value);
873 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location1, MonoObject *value)
877 return (MonoObject *) InterlockedExchangePointer((gpointer *) location1, value);
880 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location1, gfloat value)
882 IntFloatUnion val, ret;
887 ret.ival = InterlockedExchange((gint32 *) location1, val.ival);
892 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location1, gint32 value, gint32 comparand)
896 return InterlockedCompareExchange(location1, value, comparand);
899 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location1, MonoObject *value, MonoObject *comparand)
903 return (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location1, value, comparand);
906 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location1, gfloat value, gfloat comparand)
908 IntFloatUnion val, ret, cmp;
913 cmp.fval = comparand;
914 ret.ival = InterlockedCompareExchange((gint32 *) location1, val.ival, cmp.ival);
920 mono_thread_get_abort_signal (void)
930 #endif /* __MINGW32__ */
934 ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
938 thread->abort_state = state;
939 thread->abort_exc = NULL;
942 g_message (G_GNUC_PRETTY_FUNCTION
943 ": (%d) Abort requested for %p (%d)", GetCurrentThreadId (),
944 thread, thread->tid);
948 g_assert_not_reached ();
950 /* fixme: store the state somewhere */
951 #ifdef PTHREAD_POINTER_ID
952 pthread_kill (GUINT_TO_POINTER(thread->tid), mono_thread_get_abort_signal ());
954 pthread_kill (thread->tid, mono_thread_get_abort_signal ());
956 #endif /* __MINGW32__ */
960 ves_icall_System_Threading_Thread_ResetAbort (void)
962 MonoThread *thread = mono_thread_current ();
966 if (!thread->abort_exc) {
967 const char *msg = "Unable to reset abort because no abort was requested";
968 mono_raise_exception (mono_get_exception_thread_state (msg));
970 thread->abort_exc = NULL;
971 thread->abort_state = NULL;
976 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
978 return *((volatile gint8 *) (ptr));
982 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr)
984 return *((volatile gint16 *) (ptr));
988 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr)
990 return *((volatile gint32 *) (ptr));
994 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr)
996 return *((volatile gint64 *) (ptr));
1000 ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr)
1002 return (void *) *((volatile void **) ptr);
1006 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
1008 *((volatile gint8 *) ptr) = value;
1012 ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value)
1014 *((volatile gint16 *) ptr) = value;
1018 ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value)
1020 *((volatile gint32 *) ptr) = value;
1024 ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value)
1026 *((volatile gint64 *) ptr) = value;
1030 ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
1032 *((volatile void **) ptr) = value;
1035 void mono_thread_init (MonoThreadStartCB start_cb,
1036 MonoThreadAttachCB attach_cb)
1038 InitializeCriticalSection(&threads_mutex);
1039 InitializeCriticalSection(&interlocked_mutex);
1040 InitializeCriticalSection(&contexts_mutex);
1042 mono_init_static_data_info (&thread_static_info);
1043 mono_init_static_data_info (&context_static_info);
1045 current_object_key=TlsAlloc();
1047 g_message (G_GNUC_PRETTY_FUNCTION ": Allocated current_object_key %d",
1048 current_object_key);
1051 mono_thread_start_cb = start_cb;
1052 mono_thread_attach_cb = attach_cb;
1054 slothash_key=TlsAlloc();
1056 /* Get a pseudo handle to the current process. This is just a
1057 * kludge so that wapi can build a process handle if needed.
1058 * As a pseudo handle is returned, we don't need to clean
1061 GetCurrentProcess ();
1065 mono_threads_install_cleanup (MonoThreadCleanupFunc func)
1067 mono_thread_cleanup = func;
1070 void mono_install_thread_callbacks (MonoThreadCallbacks *callbacks)
1072 mono_thread_callbacks = callbacks;
1076 static void print_tids (gpointer key, gpointer value, gpointer user)
1078 g_message ("Waiting for: %d", GPOINTER_TO_UINT(key));
1084 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1085 MonoThread *threads[MAXIMUM_WAIT_OBJECTS];
1089 static void wait_for_tids (struct wait_data *wait, guint32 timeout)
1094 g_message(G_GNUC_PRETTY_FUNCTION
1095 ": %d threads to wait for in this batch", wait->num);
1098 ret=WaitForMultipleObjects(wait->num, wait->handles, TRUE, timeout);
1099 if(ret==WAIT_FAILED) {
1100 /* See the comment in build_wait_tids() */
1102 g_message (G_GNUC_PRETTY_FUNCTION ": Wait failed");
1108 for(i=0; i<wait->num; i++) {
1109 guint32 tid=wait->threads[i]->tid;
1111 if(mono_g_hash_table_lookup (threads, GUINT_TO_POINTER(tid))!=NULL) {
1112 /* This thread must have been killed, because
1113 * it hasn't cleaned itself up. (It's just
1114 * possible that the thread exited before the
1115 * parent thread had a chance to store the
1116 * handle, and now there is another pointer to
1117 * the already-exited thread stored. In this
1118 * case, we'll just get two
1119 * mono_profiler_thread_end() calls for the
1124 g_message (G_GNUC_PRETTY_FUNCTION
1125 ": cleaning up after thread %d", tid);
1127 thread_cleanup (wait->threads[i]);
1132 static void build_wait_tids (gpointer key, gpointer value, gpointer user)
1134 struct wait_data *wait=(struct wait_data *)user;
1136 if(wait->num<MAXIMUM_WAIT_OBJECTS) {
1137 MonoThread *thread=(MonoThread *)value;
1139 /* BUG: For now we just ignore background threads, we should abort them
1141 if (thread->state & ThreadState_Background)
1142 return; /* just leave, ignore */
1144 if (mono_gc_is_finalizer_thread (thread))
1147 wait->handles[wait->num]=thread->handle;
1148 wait->threads[wait->num]=thread;
1151 /* Just ignore the rest, we can't do anything with
1157 void mono_thread_manage (void)
1159 struct wait_data *wait=g_new0 (struct wait_data, 1);
1161 /* join each thread that's still running */
1163 g_message(G_GNUC_PRETTY_FUNCTION ": Joining each running thread...");
1168 g_message(G_GNUC_PRETTY_FUNCTION ": No threads");
1174 EnterCriticalSection (&threads_mutex);
1176 g_message(G_GNUC_PRETTY_FUNCTION
1177 ":There are %d threads to join",
1178 mono_g_hash_table_size (threads));
1179 mono_g_hash_table_foreach (threads, print_tids, NULL);
1183 mono_g_hash_table_foreach (threads, build_wait_tids, wait);
1185 LeaveCriticalSection (&threads_mutex);
1187 /* Something to wait for */
1188 wait_for_tids (wait, INFINITE);
1190 } while(wait->num>0);
1194 EnterCriticalSection(&threads_mutex);
1196 mono_g_hash_table_destroy(threads);
1199 LeaveCriticalSection(&threads_mutex);
1202 static void terminate_thread (gpointer key, gpointer value, gpointer user)
1204 MonoThread *thread=(MonoThread *)value;
1205 guint32 self=GPOINTER_TO_UINT (user);
1207 if(thread->tid!=self) {
1208 /*TerminateThread (thread->handle, -1);*/
1212 void mono_thread_abort_all_other_threads (void)
1214 guint32 self=GetCurrentThreadId ();
1216 EnterCriticalSection (&threads_mutex);
1218 g_message(G_GNUC_PRETTY_FUNCTION ":There are %d threads to abort",
1219 mono_g_hash_table_size (threads));
1220 mono_g_hash_table_foreach (threads, print_tids, NULL);
1223 mono_g_hash_table_foreach (threads, terminate_thread,
1224 GUINT_TO_POINTER (self));
1226 LeaveCriticalSection (&threads_mutex);
1230 * mono_thread_push_appdomain_ref:
1232 * Register that the current thread may have references to objects in domain
1233 * @domain on its stack. Each call to this function should be paired with a
1234 * call to pop_appdomain_ref.
1237 mono_thread_push_appdomain_ref (MonoDomain *domain)
1239 MonoThread *thread = mono_thread_current ();
1242 //printf ("PUSH REF: %p -> %s.\n", thread, domain->friendly_name);
1243 EnterCriticalSection (&threads_mutex);
1244 thread->appdomain_refs = g_slist_prepend (thread->appdomain_refs, domain);
1245 LeaveCriticalSection (&threads_mutex);
1250 mono_thread_pop_appdomain_ref (void)
1252 MonoThread *thread = mono_thread_current ();
1255 //printf ("POP REF: %p -> %s.\n", thread, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name);
1256 EnterCriticalSection (&threads_mutex);
1257 thread->appdomain_refs = g_slist_remove (thread->appdomain_refs, thread->appdomain_refs->data);
1258 LeaveCriticalSection (&threads_mutex);
1263 mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
1266 EnterCriticalSection (&threads_mutex);
1267 res = g_slist_find (thread->appdomain_refs, domain) != NULL;
1268 LeaveCriticalSection (&threads_mutex);
1272 typedef struct abort_appdomain_data {
1273 struct wait_data wait;
1275 } abort_appdomain_data;
1278 abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
1280 MonoThread *thread = (MonoThread*)value;
1281 abort_appdomain_data *data = (abort_appdomain_data*)user_data;
1282 MonoDomain *domain = data->domain;
1284 if (mono_thread_has_appdomain_ref (thread, domain)) {
1285 //printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread, domain->friendly_name);
1286 ves_icall_System_Threading_Thread_Abort (thread, (MonoObject*)domain->domain);
1288 if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
1289 data->wait.handles [data->wait.num] = thread->handle;
1290 data->wait.threads [data->wait.num] = thread;
1293 /* Just ignore the rest, we can't do anything with
1301 * mono_threads_abort_appdomain_threads:
1303 * Abort threads which has references to the given appdomain.
1306 mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
1308 abort_appdomain_data user_data;
1311 //printf ("ABORT BEGIN.\n");
1313 start_time = GetTickCount ();
1315 EnterCriticalSection (&threads_mutex);
1317 user_data.domain = domain;
1318 user_data.wait.num = 0;
1319 mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
1320 LeaveCriticalSection (&threads_mutex);
1322 if (user_data.wait.num > 0)
1323 wait_for_tids (&user_data.wait, timeout);
1325 /* Update remaining time */
1326 timeout -= GetTickCount () - start_time;
1327 start_time = GetTickCount ();
1332 while (user_data.wait.num > 0);
1334 //printf ("ABORT DONE.\n");
1340 * mono_thread_get_pending_exception:
1342 * Return an exception which needs to be raised when leaving a catch clause.
1343 * This is used for undeniable exception propagation.
1346 mono_thread_get_pending_exception (void)
1348 MonoThread *thread = mono_thread_current ();
1350 MONO_ARCH_SAVE_REGS;
1352 if (thread && thread->abort_exc) {
1354 * FIXME: Clear the abort exception and return an AppDomainUnloaded
1355 * exception if the thread no longer references a dying appdomain.
1357 thread->abort_exc->trace_ips = NULL;
1358 thread->abort_exc->stack_trace = NULL;
1359 return thread->abort_exc;
1365 #define NUM_STATIC_DATA_IDX 8
1366 static const int static_data_size [NUM_STATIC_DATA_IDX] = {
1367 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216
1372 * mono_alloc_static_data
1374 * Allocate memory blocks for storing threads or context static data
1377 mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset)
1379 guint idx = (offset >> 24) - 1;
1382 gpointer* static_data = *static_data_ptr;
1385 static_data = GC_MALLOC (static_data_size [0]);
1387 static_data = g_malloc0 (static_data_size [0]);
1389 *static_data_ptr = static_data;
1390 static_data [0] = static_data;
1393 for (i = 1; i < idx; ++i) {
1394 if (static_data [i])
1397 static_data [i] = GC_MALLOC (static_data_size [i]);
1399 static_data [i] = g_malloc0 (static_data_size [i]);
1405 * mono_init_static_data_info
1407 * Initializes static data counters
1409 static void mono_init_static_data_info (StaticDataInfo *static_data)
1411 static_data->idx = 0;
1412 static_data->offset = 0;
1416 * mono_alloc_static_data_slot
1418 * Generates an offset for static data. static_data contains the counters
1419 * used to generate it.
1422 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align)
1426 if (!static_data->idx && !static_data->offset) {
1428 * we use the first chunk of the first allocation also as
1429 * an array for the rest of the data
1431 static_data->offset = sizeof (gpointer) * NUM_STATIC_DATA_IDX;
1433 static_data->offset += align - 1;
1434 static_data->offset &= ~(align - 1);
1435 if (static_data->offset + size >= static_data_size [static_data->idx]) {
1436 static_data->idx ++;
1437 g_assert (size <= static_data_size [static_data->idx]);
1439 * massive unloading and reloading of domains with thread-static
1440 * data may eventually exceed the allocated storage...
1441 * Need to check what the MS runtime does in that case.
1442 * Note that for each appdomain, we need to allocate a separate
1443 * thread data slot for security reasons. We could keep track
1444 * of the slots per-domain and when the domain is unloaded
1445 * out the slots on a sort of free list.
1447 g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
1448 static_data->offset = 0;
1450 offset = static_data->offset | ((static_data->idx + 1) << 24);
1451 static_data->offset += size;
1456 * ensure thread static fields already allocated are valid for thread
1457 * This function is called when a thread is created or on thread attach.
1460 thread_adjust_static_data (MonoThread *thread)
1464 EnterCriticalSection (&threads_mutex);
1465 if (thread_static_info.offset || thread_static_info.idx > 0) {
1466 /* get the current allocated size */
1467 offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
1468 mono_alloc_static_data (&(thread->static_data), offset);
1470 LeaveCriticalSection (&threads_mutex);
1474 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
1476 MonoThread *thread = value;
1477 guint32 offset = GPOINTER_TO_UINT (user);
1479 mono_alloc_static_data (&(thread->static_data), offset);
1483 * The offset for a special static variable is composed of three parts:
1484 * a bit that indicates the type of static data (0:thread, 1:context),
1485 * an index in the array of chunks of memory for the thread (thread->static_data)
1486 * and an offset in that chunk of mem. This allows allocating less memory in the
1491 mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align)
1494 if (static_type == SPECIAL_STATIC_THREAD)
1496 EnterCriticalSection (&threads_mutex);
1497 offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
1498 mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
1499 LeaveCriticalSection (&threads_mutex);
1503 g_assert (static_type == SPECIAL_STATIC_CONTEXT);
1504 EnterCriticalSection (&contexts_mutex);
1505 offset = mono_alloc_static_data_slot (&context_static_info, size, align);
1506 LeaveCriticalSection (&contexts_mutex);
1507 offset |= 0x80000000; // Set the high bit to indicate context static data
1513 mono_get_special_static_data (guint32 offset)
1515 // The high bit means either thread (0) or static (1) data.
1517 guint32 static_type = (offset & 0x80000000);
1520 offset &= 0x7fffffff;
1521 idx = (offset >> 24) - 1;
1523 if (static_type == 0)
1525 MonoThread *thread = mono_thread_current ();
1526 return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
1530 // Allocate static data block under demand, since we don't have a list
1532 MonoAppContext *context = mono_context_get ();
1533 if (!context->static_data || !context->static_data [idx]) {
1534 EnterCriticalSection (&contexts_mutex);
1535 mono_alloc_static_data (&(context->static_data), offset);
1536 LeaveCriticalSection (&contexts_mutex);
1538 return ((char*) context->static_data [idx]) + (offset & 0xffffff);
1542 static void gc_stop_world (gpointer key, gpointer value, gpointer user)
1544 MonoThread *thread=(MonoThread *)value;
1545 guint32 self=GPOINTER_TO_UINT (user);
1548 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d", self, thread->tid);
1551 if(thread->tid==self)
1554 SuspendThread (thread->handle);
1557 void mono_gc_stop_world (void)
1559 guint32 self=GetCurrentThreadId ();
1562 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads);
1565 EnterCriticalSection (&threads_mutex);
1567 if (threads != NULL)
1568 mono_g_hash_table_foreach (threads, gc_stop_world, GUINT_TO_POINTER (self));
1570 LeaveCriticalSection (&threads_mutex);
1573 static void gc_start_world (gpointer key, gpointer value, gpointer user)
1575 MonoThread *thread=(MonoThread *)value;
1576 guint32 self=GPOINTER_TO_UINT (user);
1579 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d", self, thread->tid);
1582 if(thread->tid==self)
1585 ResumeThread (thread->handle);
1588 void mono_gc_start_world (void)
1590 guint32 self=GetCurrentThreadId ();
1593 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads);
1596 EnterCriticalSection (&threads_mutex);
1598 if (threads != NULL)
1599 mono_g_hash_table_foreach (threads, gc_start_world, GUINT_TO_POINTER (self));
1601 LeaveCriticalSection (&threads_mutex);
1604 #ifdef WITH_INCLUDED_LIBGC
1606 static void gc_push_all_stacks (gpointer key, gpointer value, gpointer user)
1608 MonoThread *thread=(MonoThread *)value;
1609 guint32 *selfp=(guint32 *)user, self = *selfp;
1612 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %d - %p", self, thread->tid, thread->stack_ptr);
1615 if(thread->tid==self) {
1617 g_message (G_GNUC_PRETTY_FUNCTION ": %p - %p", selfp, thread->stack_ptr);
1619 GC_push_all_stack (selfp, thread->stack_ptr);
1623 #ifdef PLATFORM_WIN32
1624 GC_win32_push_thread_stack (thread->handle, thread->stack_ptr);
1626 mono_wapi_push_thread_stack (thread->handle, thread->stack_ptr);
1630 void mono_gc_push_all_stacks (void)
1632 guint32 self=GetCurrentThreadId ();
1635 g_message (G_GNUC_PRETTY_FUNCTION ": %d - %p", self, threads);
1638 EnterCriticalSection (&threads_mutex);
1640 if (threads != NULL)
1641 mono_g_hash_table_foreach (threads, gc_push_all_stacks, &self);
1643 LeaveCriticalSection (&threads_mutex);
1646 #endif /* WITH_INCLUDED_LIBGC */