Merge pull request #505 from roji/shutdown_flow
[mono.git] / mono / metadata / threads.c
index c8151e24ad7af099e5aded0bb994afb0804f2217..56a1ea2646be71194ebe78c192d034ce8c60dd2f 100644 (file)
@@ -33,6 +33,7 @@
 #include <mono/metadata/monitor.h>
 #include <mono/metadata/gc-internal.h>
 #include <mono/metadata/marshal.h>
+#include <mono/metadata/runtime.h>
 #include <mono/io-layer/io-layer.h>
 #ifndef HOST_WIN32
 #include <mono/io-layer/threads.h>
@@ -410,7 +411,11 @@ static void thread_cleanup (MonoInternalThread *thread)
        if (mono_thread_cleanup_fn)
                mono_thread_cleanup_fn (thread);
 
-       MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
+       if (mono_gc_is_moving ()) {
+               MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
+               thread->thread_pinning_ref = NULL;
+       }
+               
 }
 
 static gpointer
@@ -763,9 +768,11 @@ mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gb
        internal->handle=thread_handle;
        internal->tid=tid;
        internal->apartment_state=ThreadApartmentState_Unknown;
-       internal->thread_pinning_ref = internal;
        internal->managed_id = get_next_managed_thread_id ();
-       MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+       if (mono_gc_is_moving ()) {
+               internal->thread_pinning_ref = internal;
+               MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+       }
 
        internal->synch_cs = g_new0 (CRITICAL_SECTION, 1);
        InitializeCriticalSection (internal->synch_cs);
@@ -894,9 +901,11 @@ mono_thread_attach (MonoDomain *domain)
        thread->android_tid = (gpointer) gettid ();
 #endif
        thread->apartment_state=ThreadApartmentState_Unknown;
-       thread->thread_pinning_ref = thread;
        thread->managed_id = get_next_managed_thread_id ();
-       MONO_GC_REGISTER_ROOT_PINNING (thread->thread_pinning_ref);
+       if (mono_gc_is_moving ()) {
+               thread->thread_pinning_ref = thread;
+               MONO_GC_REGISTER_ROOT_PINNING (thread->thread_pinning_ref);
+       }
 
        thread->stack_ptr = &tid;
 
@@ -1063,8 +1072,10 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
                
                internal->handle=thread;
                internal->tid=tid;
-               internal->thread_pinning_ref = internal;
-               MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+               if (mono_gc_is_moving ()) {
+                       internal->thread_pinning_ref = internal;
+                       MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+               }
                
 
                /* Don't call handle_store() here, delay it to Start.
@@ -2456,6 +2467,12 @@ ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr)
        return *((volatile float *) (ptr));
 }
 
+MonoObject*
+ves_icall_System_Threading_Volatile_Read_T (void *ptr)
+{
+       return (MonoObject*)*((volatile MonoObject**)ptr);
+}
+
 void
 ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
 {
@@ -2504,6 +2521,13 @@ ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float value)
        *((volatile float *) ptr) = value;
 }
 
+void
+ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value)
+{
+       *((volatile MonoObject **) ptr) = value;
+       mono_gc_wbarrier_generic_nostore (ptr);
+}
+
 void mono_thread_init (MonoThreadStartCB start_cb,
                       MonoThreadAttachCB attach_cb)
 {
@@ -2883,6 +2907,10 @@ void mono_thread_manage (void)
                THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
        } while(wait->num>0);
 
+#ifndef MONO_CROSS_COMPILE
+       mono_runtime_shutdown ();
+#endif
+
        mono_threads_set_shutting_down ();
 
        /* No new threads will be created after this point */
@@ -4593,8 +4621,10 @@ suspend_thread_internal (MonoInternalThread *thread, gboolean interrupt)
                gboolean running_managed;
 
                /*A null info usually means the thread is already dead. */
-               if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, interrupt)))
+               if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, interrupt))) {
+                       LeaveCriticalSection (thread->synch_cs);
                        return;
+               }
 
                ji = mono_thread_info_get_last_managed (info);
                protected_wrapper = ji && mono_threads_is_critical_method (ji->method);