Merge pull request #2530 from lambdageek/monoerror-mono_string_new
[mono.git] / mono / utils / mono-threads-coop.c
index 03e0aba0bb160e2afbbb4847d79f044076cb9823..3b11148aafe3af5d8c7ff2d93368bb3033da1e10 100644 (file)
@@ -15,7 +15,6 @@
 #endif
 
 #include <mono/utils/mono-compiler.h>
-#include <mono/utils/mono-semaphore.h>
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-tls.h>
 #include <mono/utils/hazard-pointer.h>
@@ -25,6 +24,7 @@
 #include <mono/utils/mono-time.h>
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/mono-threads-coop.h>
+#include <mono/utils/mono-threads-api.h>
 
 #ifdef TARGET_OSX
 #include <mono/utils/mach-support.h>
@@ -83,7 +83,7 @@ mono_threads_state_poll (void)
 static void *
 return_stack_ptr ()
 {
-       int i;
+       gpointer i;
        return &i;
 }
 
@@ -99,6 +99,12 @@ copy_stack_data (MonoThreadInfo *info, void* stackdata_begin)
        state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
 
        stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
+
+       if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
+               g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
+       if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
+               g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
+
        if (stackdata_size <= 0)
                g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
 
@@ -152,7 +158,7 @@ mono_threads_finish_blocking (void *cookie, void* stackdata)
        if (!mono_threads_is_coop_enabled ())
                return;
 
-       info = cookie;
+       info = (MonoThreadInfo *)cookie;
        if (!info)
                return;
 
@@ -222,7 +228,7 @@ mono_threads_reset_blocking_end (void *cookie, void* stackdata)
        if (!mono_threads_is_coop_enabled ())
                return;
 
-       info = cookie;
+       info = (MonoThreadInfo *)cookie;
        if (!info)
                return;
 
@@ -230,49 +236,6 @@ mono_threads_reset_blocking_end (void *cookie, void* stackdata)
        mono_threads_prepare_blocking (stackdata);
 }
 
-void*
-mono_threads_try_prepare_blocking (void* stackdata)
-{
-       MonoThreadInfo *info;
-
-       if (!mono_threads_is_coop_enabled ())
-               return NULL;
-
-       ++coop_try_blocking_count;
-
-       info = mono_thread_info_current_unchecked ();
-       /* If the thread is not attached, it doesn't make sense prepare for suspend. */
-       if (!info || !mono_thread_info_is_live (info) || mono_thread_info_current_state (info) == STATE_BLOCKING) {
-               THREADS_SUSPEND_DEBUG ("PREPARE-TRY-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
-               return NULL;
-       }
-
-       copy_stack_data (info, stackdata);
-
-retry:
-       ++coop_save_count;
-       mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
-
-       switch (mono_threads_transition_do_blocking (info)) {
-       case DoBlockingContinue:
-               break;
-       case DoBlockingPollAndRetry:
-               mono_threads_state_poll ();
-               goto retry;
-       }
-
-       return info;
-}
-
-void
-mono_threads_finish_try_blocking (void* cookie, void* stackdata)
-{
-       if (!mono_threads_is_coop_enabled ())
-               return;
-
-       mono_threads_finish_blocking (cookie, stackdata);
-}
-
 void
 mono_threads_init_coop (void)
 {
@@ -301,8 +264,8 @@ mono_threads_coop_end_global_suspend (void)
                mono_polling_required = 0;
 }
 
-void*
-mono_threads_enter_gc_unsafe_region (void* stackdata)
+gpointer
+mono_threads_enter_gc_unsafe_region (gpointer* stackdata)
 {
        if (!mono_threads_is_coop_enabled ())
                return NULL;
@@ -311,10 +274,40 @@ mono_threads_enter_gc_unsafe_region (void* stackdata)
 }
 
 void
-mono_threads_exit_gc_unsafe_region (void *regions_cookie, void* stackdata)
+mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer* stackdata)
 {
        if (!mono_threads_is_coop_enabled ())
                return;
 
-       mono_threads_reset_blocking_end (regions_cookie, stackdata);
+       mono_threads_reset_blocking_end (cookie, stackdata);
+}
+
+void
+mono_threads_assert_gc_unsafe_region (void)
+{
+       MONO_REQ_GC_UNSAFE_MODE;
+}
+
+gpointer
+mono_threads_enter_gc_safe_region (gpointer *stackdata)
+{
+       if (!mono_threads_is_coop_enabled ())
+               return NULL;
+
+       return mono_threads_prepare_blocking (stackdata);
+}
+
+void
+mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
+{
+       if (!mono_threads_is_coop_enabled ())
+               return;
+
+       mono_threads_finish_blocking (cookie, stackdata);
+}
+
+void
+mono_threads_assert_gc_safe_region (void)
+{
+       MONO_REQ_GC_SAFE_MODE;
 }