[threads] Enable blocking transition with MONO_ENABLE_BLOCKING_TRANSITION env variabl...
[mono.git] / mono / utils / mono-threads-coop.c
index ddc3c6ebbe231b49371e86760e198272c3d08191..440eff85125121729d9c330de899bf5d74e43d0f 100644 (file)
@@ -1,5 +1,6 @@
- /*
- * mono-threads.c: Coop threading
+/**
+ * \file
+ * Coop threading
  *
  * Author:
  *     Rodrigo Kumpera (kumpera@gmail.com)
@@ -27,6 +28,7 @@
 #include <mono/utils/mono-threads-coop.h>
 #include <mono/utils/mono-threads-api.h>
 #include <mono/utils/checked-build.h>
+#include <mono/utils/mono-threads-debug.h>
 
 #ifdef TARGET_OSX
 #include <mono/utils/mach-support.h>
@@ -70,7 +72,7 @@ coop_tls_pop (gpointer received_cookie)
 
        stack = mono_native_tls_get_value (coop_reset_count_stack_key);
        if (!stack || 0 == stack->len)
-               mono_fatal_with_history ("Received cookie %p but found no stack at all, %x\n", received_cookie);
+               mono_fatal_with_history ("Received cookie %p but found no stack at all\n", received_cookie);
 
        expected_cookie = g_array_index (stack, gpointer, stack->len - 1);
        stack->len --;
@@ -115,7 +117,7 @@ mono_threads_state_poll (void)
 static void
 mono_threads_state_poll_with_info (MonoThreadInfo *info)
 {
-       g_assert (mono_threads_is_coop_enabled ());
+       g_assert (mono_threads_is_blocking_transition_enabled ());
 
        ++coop_do_polling_count;
 
@@ -134,7 +136,7 @@ mono_threads_state_poll_with_info (MonoThreadInfo *info)
        /* commit the saved state and notify others if needed */
        switch (mono_threads_transition_state_poll (info)) {
        case SelfSuspendResumed:
-               return;
+               break;
        case SelfSuspendWait:
                mono_thread_info_wait_for_resume (info);
                break;
@@ -143,13 +145,22 @@ mono_threads_state_poll_with_info (MonoThreadInfo *info)
                mono_thread_info_wait_for_resume (info);
                break;
        }
+
+       if (info->async_target) {
+               info->async_target (info->user_data);
+               info->async_target = NULL;
+               info->user_data = NULL;
+       }
 }
 
-static void *
-return_stack_ptr ()
+static volatile gpointer* dummy_global;
+
+static MONO_NEVER_INLINE
+void*
+return_stack_ptr (gpointer *i)
 {
-       gpointer i;
-       return &i;
+       dummy_global = i;
+       return i;
 }
 
 static void
@@ -157,7 +168,8 @@ copy_stack_data (MonoThreadInfo *info, gpointer *stackdata_begin)
 {
        MonoThreadUnwindState *state;
        int stackdata_size;
-       void* stackdata_end = return_stack_ptr ();
+       gpointer dummy;
+       void* stackdata_end = return_stack_ptr (&dummy);
 
        SAVE_REGS_ON_STACK;
 
@@ -194,7 +206,7 @@ mono_threads_enter_gc_safe_region_with_info (MonoThreadInfo *info, gpointer *sta
 {
        gpointer cookie;
 
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return NULL;
 
        cookie = mono_threads_enter_gc_safe_region_unbalanced_with_info (info, stackdata);
@@ -216,7 +228,7 @@ mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata)
 static gpointer
 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
 {
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return NULL;
 
        ++coop_do_blocking_count;
@@ -243,7 +255,7 @@ retry:
 void
 mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
 {
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return;
 
 #ifdef ENABLE_CHECKED_BUILD_GC
@@ -259,7 +271,7 @@ mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdat
 {
        MonoThreadInfo *info;
 
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return;
 
        info = (MonoThreadInfo *)cookie;
@@ -277,6 +289,12 @@ mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdat
        default:
                g_error ("Unknown thread state");
        }
+
+       if (info->async_target) {
+               info->async_target (info->user_data);
+               info->async_target = NULL;
+               info->user_data = NULL;
+       }
 }
 
 void
@@ -296,7 +314,7 @@ mono_threads_enter_gc_unsafe_region_with_info (THREAD_INFO_TYPE *info, gpointer
 {
        gpointer cookie;
 
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return NULL;
 
        cookie = mono_threads_enter_gc_unsafe_region_unbalanced_with_info (info, stackdata);
@@ -318,7 +336,7 @@ mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata)
 gpointer
 mono_threads_enter_gc_unsafe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
 {
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return NULL;
 
        ++coop_reset_blocking_count;
@@ -344,6 +362,12 @@ mono_threads_enter_gc_unsafe_region_unbalanced_with_info (MonoThreadInfo *info,
                g_error ("Unknown thread state");
        }
 
+       if (info->async_target) {
+               info->async_target (info->user_data);
+               info->async_target = NULL;
+               info->user_data = NULL;
+       }
+
        return info;
 }
 
@@ -352,7 +376,7 @@ mono_threads_enter_gc_unsafe_region_cookie (void)
 {
        MonoThreadInfo *info;
 
-       g_assert (mono_threads_is_coop_enabled ());
+       g_assert (mono_threads_is_blocking_transition_enabled ());
 
        info = mono_thread_info_current_unchecked ();
 
@@ -369,7 +393,7 @@ mono_threads_enter_gc_unsafe_region_cookie (void)
 void
 mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer *stackdata)
 {
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return;
 
 #ifdef ENABLE_CHECKED_BUILD_GC
@@ -383,7 +407,7 @@ mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer *stackdata)
 void
 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata)
 {
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_blocking_transition_enabled ())
                return;
 
        if (!cookie)
@@ -406,16 +430,29 @@ mono_threads_is_coop_enabled (void)
 #else
        static int is_coop_enabled = -1;
        if (G_UNLIKELY (is_coop_enabled == -1))
-               is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? 1 : 0;
+               is_coop_enabled = g_hasenv ("MONO_ENABLE_COOP") ? 1 : 0;
        return is_coop_enabled == 1;
 #endif
 }
 
+gboolean
+mono_threads_is_blocking_transition_enabled (void)
+{
+#if defined(USE_COOP_GC)
+       return TRUE;
+#else
+       static int is_blocking_transition_enabled = -1;
+       if (G_UNLIKELY (is_blocking_transition_enabled == -1))
+               is_blocking_transition_enabled = (g_hasenv ("MONO_ENABLE_COOP") || g_hasenv ("MONO_ENABLE_BLOCKING_TRANSITION")) ? 1 : 0;
+       return is_blocking_transition_enabled == 1;
+#endif
+}
+
 
 void
-mono_threads_init_coop (void)
+mono_threads_coop_init (void)
 {
-       if (!mono_threads_is_coop_enabled ())
+       if (!mono_threads_is_coop_enabled () && !mono_threads_is_blocking_transition_enabled ())
                return;
 
        mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);