Merge pull request #1840 from ludovic-henry/iolayer-thread-interrupt
[mono.git] / mono / utils / mono-threads-posix.c
index 303f2529aeeed6dbda934b097c17647261792444..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,30 +236,6 @@ 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)
 {
@@ -284,6 +251,8 @@ 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
@@ -309,7 +278,7 @@ mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
 gboolean
 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
 {
-       return pthread_create (tid, NULL, func, arg) == 0;
+       return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
 }
 
 void
@@ -449,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;
        }
 
@@ -579,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
@@ -632,6 +603,16 @@ mono_threads_platform_free (MonoThreadInfo *info)
 {
 }
 
+void
+mono_threads_core_begin_global_suspend (void)
+{
+}
+
+void
+mono_threads_core_end_global_suspend (void)
+{
+}
+
 #endif /*defined (USE_POSIX_BACKEND)*/
 
 #endif