[mono-threads] Move platform unregister after detach (#3604)
authorLudovic Henry <ludovic@xamarin.com>
Thu, 22 Sep 2016 20:09:29 +0000 (22:09 +0200)
committerGitHub <noreply@github.com>
Thu, 22 Sep 2016 20:09:29 +0000 (22:09 +0200)
This is important as platform unregister is where we signal the thread handle, and that should be done as late as possible, but still before we switch the thread state to DETACHED and remove it from the thread list.

The fact that we do under the suspend lock shouldn't be an issue as we shouldn't call in any other code than w32handle code.

mono/utils/mono-threads.c

index ccb9121d0b6245a5558c099246ae535bbafe3eea..3eb88171257a0f68687870c9655144791ae779d9 100644 (file)
@@ -260,8 +260,6 @@ mono_threads_wait_pending_operations (void)
 
 //Thread initialization code
 
-static void mono_threads_unregister_current_thread (MonoThreadInfo *info);
-
 static inline void
 mono_hazard_pointer_clear_all (MonoThreadHazardPointers *hp, int retain)
 {
@@ -400,6 +398,7 @@ unregister_thread (void *arg)
        gpointer gc_unsafe_stackdata;
        MonoThreadInfo *info;
        int small_id;
+       gboolean result;
 
        info = (MonoThreadInfo *) arg;
        g_assert (info);
@@ -416,8 +415,6 @@ unregister_thread (void *arg)
 
        mono_native_tls_set_value (thread_exited_key, GUINT_TO_POINTER (1));
 
-       mono_threads_platform_unregister (info);
-
        /*
         * TLS destruction order is not reliable so small_id might be cleaned up
         * before us.
@@ -444,7 +441,10 @@ unregister_thread (void *arg)
        */
        if (threads_callbacks.thread_unregister)
                threads_callbacks.thread_unregister (info);
-       mono_threads_unregister_current_thread (info);
+
+       mono_threads_platform_unregister (info);
+       result = mono_thread_info_remove (info);
+       g_assert (result);
        mono_threads_transition_detach (info);
 
        mono_thread_info_suspend_unlock ();
@@ -480,20 +480,6 @@ thread_exited_dtor (void *arg)
 #endif
 }
 
-/**
- * Removes the current thread from the thread list.
- * This must be called from the thread unregister callback and nowhere else.
- * The current thread must be passed as TLS might have already been cleaned up.
-*/
-static void
-mono_threads_unregister_current_thread (MonoThreadInfo *info)
-{
-       gboolean result;
-       g_assert (mono_thread_info_get_tid (info) == mono_native_thread_id_get ());
-       result = mono_thread_info_remove (info);
-       g_assert (result);
-}
-
 MonoThreadInfo*
 mono_thread_info_current_unchecked (void)
 {