Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[mono.git] / mono / utils / mono-threads-posix.c
index 8beb693a51cc2ba4ab6120fef1f7e2c379641dd3..303f2529aeeed6dbda934b097c17647261792444 100644 (file)
@@ -272,6 +272,7 @@ mono_threads_core_clear_interruption (void)
 int
 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
 {
+       THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
 #ifdef USE_TKILL_ON_ANDROID
        int result, old_errno = errno;
        result = tkill (info->native_handle, signum);
@@ -286,10 +287,49 @@ mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
 #else
        return pthread_kill (mono_thread_info_get_tid (info), signum);
 #endif
+}
+
+MonoNativeThreadId
+mono_native_thread_id_get (void)
+{
+       return pthread_self ();
+}
 
+gboolean
+mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
+{
+       return pthread_equal (id1, id2);
 }
 
-#if defined (USE_POSIX_BACKEND)
+/*
+ * mono_native_thread_create:
+ *
+ *   Low level thread creation function without any GC wrappers.
+ */
+gboolean
+mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
+{
+       return pthread_create (tid, NULL, func, arg) == 0;
+}
+
+void
+mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
+{
+#if defined (HAVE_PTHREAD_SETNAME_NP) && !defined (__MACH__)
+       if (!name) {
+               pthread_setname_np (tid, "");
+       } else {
+               char n [16];
+
+               strncpy (n, name, 16);
+               n [15] = '\0';
+               pthread_setname_np (tid, n);
+       }
+#endif
+}
+
+
+#if defined (USE_POSIX_BACKEND) && !defined (USE_COOP_GC)
 
 static int suspend_signal_num;
 static int restart_signal_num;
@@ -304,7 +344,6 @@ static sigset_t suspend_ack_signal_mask;
 #define DEFAULT_SUSPEND_SIGNAL SIGPWR
 #endif
 #define DEFAULT_RESTART_SIGNAL SIGXCPU
-#define DEFAULT_ABORT_SIGNAL SIGWINCH
 
 static int
 mono_thread_search_alt_signal (int min_signal)
@@ -312,16 +351,12 @@ mono_thread_search_alt_signal (int min_signal)
 #if !defined (SIGRTMIN)
        g_error ("signal search only works with RTMIN");
 #else
-       static int abort_signum = -1;
        int i;
-       if (abort_signum != -1)
-               return abort_signum;
        /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
        for (i = MAX (min_signal, SIGRTMIN) + 1; i < SIGRTMAX; ++i) {
                struct sigaction sinfo;
                sigaction (i, NULL, &sinfo);
                if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
-                       abort_signum = i;
                        return i;
                }
        }
@@ -342,7 +377,6 @@ mono_thread_get_alt_suspend_signal (void)
 #endif /* SIGUSR1 */
 #else
        static int suspend_signum = -1;
-       int i;
        if (suspend_signum == -1)
                suspend_signum = mono_thread_search_alt_signal (-1);
        return suspend_signum;
@@ -362,14 +396,33 @@ mono_thread_get_alt_resume_signal (void)
 #endif /* SIGUSR1 */
 #else
        static int resume_signum = -1;
-       int i;
        if (resume_signum == -1)
-               resume_signum = mono_thread_search_alt_signal (mono_thread_get_alt_suspend_signal ());
+               resume_signum = mono_thread_search_alt_signal (mono_thread_get_alt_suspend_signal () + 1);
        return resume_signum;
 #endif /* SIGRTMIN */
 }
 
 
+static int
+mono_threads_get_abort_signal (void)
+{
+#if defined(PLATFORM_ANDROID)
+       return SIGTTIN;
+#elif !defined (SIGRTMIN)
+#ifdef SIGTTIN
+       return SIGTTIN;
+#else
+       return -1;
+#endif /* SIGRTMIN */
+#else
+       static int abort_signum = -1;
+       if (abort_signum == -1)
+               abort_signum = mono_thread_search_alt_signal (mono_thread_get_alt_resume_signal () + 1);
+       return abort_signum;
+#endif /* SIGRTMIN */
+}
+
+
 #if !defined(__native_client__)
 static void
 restart_signal_handler (int _dummy, siginfo_t *_info, void *context)
@@ -406,7 +459,7 @@ suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
                goto done;
        }
 
-       ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&current->suspend_state, context);
+       ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], context);
 
        /* thread_state_init_from_sigctx return FALSE if the current thread is detaching and suspend can't continue. */
        current->suspend_can_continue = ret;
@@ -442,7 +495,7 @@ suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
 
        if (current->async_target) {
 #if MONO_ARCH_HAS_MONO_CONTEXT
-               MonoContext tmp = current->suspend_state.ctx;
+               MonoContext tmp = current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
                mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, current->async_target, current->user_data);
                current->async_target = current->user_data = NULL;
                mono_monoctx_to_sigctx (&tmp, context);
@@ -491,7 +544,7 @@ mono_threads_init_platform (void)
 {
        sigset_t signal_set;
 
-       abort_signal_num = DEFAULT_ABORT_SIGNAL;
+       abort_signal_num = mono_threads_get_abort_signal ();
        if (mono_thread_info_unified_management_enabled ()) {
                suspend_signal_num = DEFAULT_SUSPEND_SIGNAL;
                restart_signal_num = DEFAULT_RESTART_SIGNAL;
@@ -579,45 +632,6 @@ mono_threads_platform_free (MonoThreadInfo *info)
 {
 }
 
-MonoNativeThreadId
-mono_native_thread_id_get (void)
-{
-       return pthread_self ();
-}
-
-gboolean
-mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
-{
-       return pthread_equal (id1, id2);
-}
-
-/*
- * mono_native_thread_create:
- *
- *   Low level thread creation function without any GC wrappers.
- */
-gboolean
-mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
-{
-       return pthread_create (tid, NULL, func, arg) == 0;
-}
-
-void
-mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
-{
-#if defined (HAVE_PTHREAD_SETNAME_NP) && !defined (__MACH__)
-       if (!name) {
-               pthread_setname_np (tid, "");
-       } else {
-               char n [16];
-
-               strncpy (n, name, 16);
-               n [15] = '\0';
-               pthread_setname_np (tid, n);
-       }
-#endif
-}
-
 #endif /*defined (USE_POSIX_BACKEND)*/
 
 #endif