Revert "[runtime] Get rid of ResumeThread(), use mono_thread_info_resume () instead."
[mono.git] / mono / utils / mono-threads-posix.c
index be3837cd9c7ac86fc5735a3cf308cec09685c149..7abb7a4045ed805dfa32978dca0194d772b4d723 100644 (file)
@@ -13,6 +13,7 @@
 #include <mono/utils/mono-semaphore.h>
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-tls.h>
+#include <mono/utils/gc_wrapper.h>
 #include <mono/metadata/threads-types.h>
 
 #include <errno.h>
@@ -24,57 +25,129 @@ extern int tkill (pid_t tid, int signal);
 #if defined(_POSIX_VERSION) || defined(__native_client__)
 #include <signal.h>
 
+#if defined(__native_client__)
+void nacl_shutdown_gc_thread(void);
+#endif
+
 typedef struct {
        void *(*start_routine)(void*);
        void *arg;
        int flags;
        MonoSemType registered;
-} ThreadStartInfo;
-
+       HANDLE handle;
+} StartInfo;
 
 static void*
 inner_start_thread (void *arg)
 {
-       ThreadStartInfo *start_info = arg;
+       StartInfo *start_info = arg;
        void *t_arg = start_info->arg;
-       int post_result;
+       int res;
        void *(*start_func)(void*) = start_info->start_routine;
+       guint32 flags = start_info->flags;
        void *result;
+       HANDLE handle;
+       MonoThreadInfo *info;
+
+       /* Register the thread with the io-layer */
+       handle = wapi_create_thread_handle ();
+       if (!handle) {
+               res = MONO_SEM_POST (&(start_info->registered));
+               g_assert (!res);
+               return NULL;
+       }
+       start_info->handle = handle;
 
-       mono_thread_info_attach (&result);
+       if (!(flags & CREATE_NO_DETACH)) {
+               res = mono_gc_pthread_detach (pthread_self ());
+               g_assert (!res);
+       }
+
+       info = mono_thread_info_attach (&result);
+       info->runtime_thread = TRUE;
 
-       post_result = MONO_SEM_POST (&(start_info->registered));
-       g_assert (!post_result);
+       /* start_info is not valid after this */
+       res = MONO_SEM_POST (&(start_info->registered));
+       g_assert (!res);
+       start_info = NULL;
 
+       if (flags & CREATE_SUSPENDED)
+               wapi_thread_suspend (handle);
+
+       /* Run the actual main function of the thread */
        result = start_func (t_arg);
+
        g_assert (!mono_domain_get ());
+       mono_thread_info_dettach ();
 
+#if defined(__native_client__)
+       nacl_shutdown_gc_thread();
+#endif
+
+       wapi_thread_set_exit_code (GPOINTER_TO_UINT (result), handle);
 
+       // FIXME: Why is this needed ?
+       mono_gc_pthread_exit (NULL);
+
+       g_assert_not_reached ();
        return result;
 }
 
-int
-mono_threads_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
-{
-       ThreadStartInfo *start_info;
-       int result;
-
-       start_info = g_malloc0 (sizeof (ThreadStartInfo));
-       if (!start_info)
-               return ENOMEM;
-       MONO_SEM_INIT (&(start_info->registered), 0);
-       start_info->arg = arg;
-       start_info->start_routine = start_routine;
-
-       result = mono_threads_get_callbacks ()->mono_gc_pthread_create (new_thread, attr, inner_start_thread, start_info);
-       if (result == 0) {
-               while (MONO_SEM_WAIT (&(start_info->registered)) != 0) {
-                       /*if (EINTR != errno) ABORT("sem_wait failed"); */
-               }
+HANDLE
+mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
+{
+       pthread_attr_t attr;
+       int res;
+       pthread_t thread;
+       StartInfo start_info;
+
+       res = pthread_attr_init (&attr);
+       g_assert (!res);
+
+       if (stack_size == 0) {
+#if HAVE_VALGRIND_MEMCHECK_H
+               if (RUNNING_ON_VALGRIND)
+                       stack_size = 1 << 20;
+               else
+                       stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
+#else
+               stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
+#endif
        }
-       MONO_SEM_DESTROY (&(start_info->registered));
-       g_free (start_info);
-       return result;
+
+#ifdef PTHREAD_STACK_MIN
+       if (stack_size < PTHREAD_STACK_MIN)
+               stack_size = PTHREAD_STACK_MIN;
+#endif
+
+#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
+       res = pthread_attr_setstacksize (&attr, stack_size);
+       g_assert (!res);
+#endif
+
+       memset (&start_info, 0, sizeof (StartInfo));
+       start_info.start_routine = (gpointer)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);
+       if (res) {
+               // FIXME:
+               g_assert_not_reached ();
+       }
+
+       /* Wait until the thread register itself in various places */
+       while (MONO_SEM_WAIT (&(start_info.registered)) != 0) {
+               /*if (EINTR != errno) ABORT("sem_wait failed"); */
+       }
+       MONO_SEM_DESTROY (&(start_info.registered));
+
+       if (out_tid)
+               *out_tid = thread;
+
+       return start_info.handle;
 }
 
 #if !defined (__MACH__)
@@ -93,10 +166,15 @@ suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
 
        ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&current->suspend_state, context);
 
-       g_assert (ret);
+       /* thread_state_init_from_sigctx return FALSE if the current thread is detaching and suspend can't continue. */
+       current->suspend_can_continue = ret;
+
+       MONO_SEM_POST (&current->begin_suspend_semaphore);
+
+       /* This thread is doomed, all we can do is give up and let the suspender recover. */
+       if (!ret)
+               return;
 
-       MONO_SEM_POST (&current->suspend_semaphore);
-               
        while (MONO_SEM_WAIT (&current->resume_semaphore) != 0) {
                /*if (EINTR != errno) ABORT("sem_wait failed"); */
        }
@@ -164,6 +242,9 @@ mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
                errno = old_errno;
        }
        return result;
+#elif defined(__native_client__)
+       /* Workaround pthread_kill abort() in NaCl glibc. */
+       return 0;
 #else
        return pthread_kill (mono_thread_info_get_tid (info), signum);
 #endif
@@ -192,10 +273,10 @@ mono_threads_core_suspend (MonoThreadInfo *info)
 {
        /*FIXME, check return value*/
        mono_threads_pthread_kill (info, mono_thread_get_abort_signal ());
-       while (MONO_SEM_WAIT (&info->suspend_semaphore) != 0) {
+       while (MONO_SEM_WAIT (&info->begin_suspend_semaphore) != 0) {
                /* g_assert (errno == EINTR); */
        }
-       return TRUE;
+       return info->suspend_can_continue;
 }
 
 gboolean
@@ -212,7 +293,7 @@ mono_threads_core_resume (MonoThreadInfo *info)
 void
 mono_threads_platform_register (MonoThreadInfo *info)
 {
-       MONO_SEM_INIT (&info->suspend_semaphore, 0);
+       MONO_SEM_INIT (&info->begin_suspend_semaphore, 0);
 
 #if defined (PLATFORM_ANDROID)
        info->native_handle = (gpointer) gettid ();
@@ -222,7 +303,7 @@ mono_threads_platform_register (MonoThreadInfo *info)
 void
 mono_threads_platform_free (MonoThreadInfo *info)
 {
-       MONO_SEM_DESTROY (&info->suspend_semaphore);
+       MONO_SEM_DESTROY (&info->begin_suspend_semaphore);
 }
 
 MonoNativeThreadId