Merge pull request #1840 from ludovic-henry/iolayer-thread-interrupt
[mono.git] / mono / utils / mono-threads-posix.c
index dcb296fdbe9332c94a372808aa4e9e4a2c06e17f..b2e4bd37bcc93a8ec42eb3e9a6f844ecf36e353e 100644 (file)
@@ -20,6 +20,7 @@
 #include <mono/utils/mono-tls.h>
 #include <mono/utils/mono-mmap.h>
 #include <mono/metadata/threads-types.h>
+#include <mono/metadata/gc-internal.h>
 #include <limits.h>
 
 #include <errno.h>
@@ -51,7 +52,7 @@ typedef struct {
 static void*
 inner_start_thread (void *arg)
 {
-       StartInfo *start_info = arg;
+       StartInfo *start_info = (StartInfo *) arg;
        void *t_arg = start_info->arg;
        int res;
        void *(*start_func)(void*) = start_info->start_routine;
@@ -70,6 +71,8 @@ inner_start_thread (void *arg)
        start_info->handle = handle;
 
        info = mono_thread_info_attach (&result);
+       MONO_PREPARE_BLOCKING
+
        info->runtime_thread = TRUE;
        info->handle = handle;
 
@@ -89,25 +92,12 @@ inner_start_thread (void *arg)
                MONO_SEM_DESTROY (&info->create_suspended_sem);
        }
 
+       MONO_FINISH_BLOCKING
        /* Run the actual main function of the thread */
        result = start_func (t_arg);
 
-       /*
-       mono_thread_info_detach ();
-       */
-
-#if defined(__native_client__)
-       nacl_shutdown_gc_thread();
-#endif
-
-       wapi_thread_handle_set_exited (handle, GPOINTER_TO_UINT (result));
-       /* This is needed by mono_threads_core_unregister () which is called later */
-       info->handle = NULL;
-
-       g_assert (mono_threads_get_callbacks ()->thread_exit);
-       mono_threads_get_callbacks ()->thread_exit (NULL);
+       mono_threads_core_exit (GPOINTER_TO_UINT (result));
        g_assert_not_reached ();
-       return result;
 }
 
 HANDLE
@@ -143,13 +133,13 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer
 #endif
 
        memset (&start_info, 0, sizeof (StartInfo));
-       start_info.start_routine = (gpointer)start_routine;
+       start_info.start_routine = (void *(*)(void *)) start_routine;
        start_info.arg = arg;
        start_info.flags = creation_flags;
        MONO_SEM_INIT (&(start_info.registered), 0);
 
        /* Actually start the thread */
-       res = mono_threads_get_callbacks ()->mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
+       res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
        if (res) {
                MONO_SEM_DESTROY (&(start_info.registered));
                return NULL;
@@ -195,8 +185,9 @@ mono_threads_core_exit (int exit_code)
 
        wapi_thread_handle_set_exited (current->handle, exit_code);
 
-       g_assert (mono_threads_get_callbacks ()->thread_exit);
-       mono_threads_get_callbacks ()->thread_exit (NULL);
+       mono_thread_info_detach ();
+
+       pthread_exit (NULL);
 }
 
 void
@@ -245,33 +236,10 @@ mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
        return handle;
 }
 
-gpointer
-mono_threads_core_prepare_interrupt (HANDLE thread_handle)
-{
-       return wapi_prepare_interrupt_thread (thread_handle);
-}
-
-void
-mono_threads_core_finish_interrupt (gpointer wait_handle)
-{
-       wapi_finish_interrupt_thread (wait_handle);
-}
-
-void
-mono_threads_core_self_interrupt (void)
-{
-       wapi_self_interrupt ();
-}
-
-void
-mono_threads_core_clear_interruption (void)
-{
-       wapi_clear_interruption ();
-}
-
 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);
@@ -283,13 +251,54 @@ mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
 #elif defined(__native_client__)
        /* Workaround pthread_kill abort() in NaCl glibc. */
        return 0;
+#elif !defined(HAVE_PTHREAD_KILL)
+       g_error ("pthread_kill() is not supported by this platform");
 #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);
+}
+
+/*
+ * 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, (void *(*)(void *)) func, arg) == 0;
 }
 
-#if defined (USE_POSIX_BACKEND)
+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 +313,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 +320,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;
                }
        }
@@ -362,12 +366,32 @@ mono_thread_get_alt_resume_signal (void)
 #else
        static int resume_signum = -1;
        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)
@@ -394,6 +418,7 @@ suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
        if (current->syscall_break_signal) {
                current->syscall_break_signal = FALSE;
                THREADS_SUSPEND_DEBUG ("\tsyscall break for %p\n", current);
+               mono_threads_notify_initiator_of_abort (current);
                goto done;
        }
 
@@ -489,7 +514,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;
@@ -524,7 +549,8 @@ mono_threads_core_abort_syscall (MonoThreadInfo *info)
        This signal should not be interpreted as a suspend request.
        */
        info->syscall_break_signal = TRUE;
-       mono_threads_pthread_kill (info, abort_signal_num);
+       if (!mono_threads_pthread_kill (info, abort_signal_num))
+               mono_threads_add_to_pending_operation_set (info);
 }
 
 gboolean
@@ -577,43 +603,14 @@ 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)
+void
+mono_threads_core_begin_global_suspend (void)
 {
-       return pthread_create (tid, NULL, func, arg) == 0;
 }
 
 void
-mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
+mono_threads_core_end_global_suspend (void)
 {
-#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)*/