[threads] Fix crash on unknown policy (#4264)
[mono.git] / mono / utils / mono-threads-posix.c
index be440711fb3bfcbdf3be365bd25876045dfe8bf5..430738591c6884de507fc93103a6a150f2edcd5b 100644 (file)
@@ -17,7 +17,6 @@
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-coop-semaphore.h>
 #include <mono/metadata/gc-internals.h>
-#include <mono/utils/w32handle.h>
 #include <mono/utils/mono-threads-debug.h>
 
 #include <errno.h>
@@ -36,21 +35,52 @@ extern int tkill (pid_t tid, int signal);
 
 #include <sys/resource.h>
 
-#if defined(__native_client__)
-void nacl_shutdown_gc_thread(void);
-#endif
-
-void
-mono_threads_platform_register (MonoThreadInfo *info)
+static void
+reset_priority (pthread_attr_t *attr)
 {
-       gpointer thread_handle;
+       struct sched_param param;
+       gint res;
+       gint policy;
+
+       memset (&param, 0, sizeof (param));
+
+       res = pthread_attr_getschedpolicy (attr, &policy);
+       if (res != 0)
+               g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+       gint max, min;
+
+       /* Necessary to get valid priority range */
 
-       thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
-       if (thread_handle == INVALID_HANDLE_VALUE)
-               g_error ("%s: failed to create handle", __func__);
+       min = sched_get_priority_min (policy);
+       max = sched_get_priority_max (policy);
 
-       g_assert (!info->handle);
-       info->handle = thread_handle;
+       if (max > 0 && min >= 0 && max > min)
+               param.sched_priority = (max - min) / 2 + min;
+       else
+#endif
+       {
+               switch (policy) {
+               case SCHED_FIFO:
+               case SCHED_RR:
+                       param.sched_priority = 50;
+                       break;
+#ifdef SCHED_BATCH
+               case SCHED_BATCH:
+#endif
+               case SCHED_OTHER:
+                       param.sched_priority = 0;
+                       break;
+               default:
+                       g_warning ("%s: unknown policy %d", __func__, policy);
+                       return;
+               }
+       }
+
+       res = pthread_attr_setschedparam (attr, &param);
+       if (res != 0)
+               g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
 }
 
 int
@@ -58,11 +88,9 @@ mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_
 {
        pthread_attr_t attr;
        pthread_t thread;
-       int policy;
-       struct sched_param param;
        gint res;
        gsize set_stack_size;
-       size_t min_size;
+       gsize min_stack_size;
 
        res = pthread_attr_init (&attr);
        g_assert (!res);
@@ -93,51 +121,14 @@ mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_
        g_assert (!res);
 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
 
-       memset (&param, 0, sizeof (param));
-
-       res = pthread_attr_getschedpolicy (&attr, &policy);
-       if (res != 0)
-               g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
-
-#ifdef _POSIX_PRIORITY_SCHEDULING
-       int max, min;
-
-       /* Necessary to get valid priority range */
-
-       min = sched_get_priority_min (policy);
-       max = sched_get_priority_max (policy);
-
-       if (max > 0 && min >= 0 && max > min)
-               param.sched_priority = (max - min) / 2 + min;
-       else
-#endif
-       {
-               switch (policy) {
-               case SCHED_FIFO:
-               case SCHED_RR:
-                       param.sched_priority = 50;
-                       break;
-#ifdef SCHED_BATCH
-               case SCHED_BATCH:
-#endif
-               case SCHED_OTHER:
-                       param.sched_priority = 0;
-                       break;
-               default:
-                       g_error ("%s: unknown policy %d", __func__, policy);
-               }
-       }
-
-       res = pthread_attr_setschedparam (&attr, &param);
-       if (res != 0)
-               g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
+       reset_priority (&attr);
 
        if (stack_size) {
-               res = pthread_attr_getstacksize (&attr, &min_size);
+               res = pthread_attr_getstacksize (&attr, &min_stack_size);
                if (res != 0)
                        g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", g_strerror (res), res);
-               else
-                       *stack_size = min_size;
+
+               *stack_size = min_stack_size;
        }
 
        /* Actually start the thread */
@@ -151,32 +142,27 @@ mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_
        return 0;
 }
 
-gboolean
-mono_threads_platform_yield (void)
+void
+mono_threads_platform_init (void)
 {
-       return sched_yield () == 0;
 }
 
-void
-mono_threads_platform_exit (int exit_code)
+gboolean
+mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
 {
-#if defined(__native_client__)
-       nacl_shutdown_gc_thread();
-#endif
-
-       mono_thread_info_detach ();
+       return FALSE;
+}
 
-       pthread_exit (NULL);
+gboolean
+mono_threads_platform_yield (void)
+{
+       return sched_yield () == 0;
 }
 
 void
-mono_threads_platform_unregister (MonoThreadInfo *info)
+mono_threads_platform_exit (gsize exit_code)
 {
-       g_assert (info->handle);
-
-       /* The thread is no longer active, so unref it */
-       mono_w32handle_unref (info->handle);
-       info->handle = NULL;
+       pthread_exit ((gpointer) exit_code);
 }
 
 int
@@ -193,28 +179,6 @@ mono_threads_get_max_stack_size (void)
        return (int)lim.rlim_max;
 }
 
-gpointer
-mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
-{
-       g_assert (info->handle);
-       mono_w32handle_ref (info->handle);
-       return info->handle;
-}
-
-HANDLE
-mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
-{
-       mono_w32handle_ref (handle);
-
-       return handle;
-}
-
-void
-mono_threads_platform_close_thread_handle (HANDLE handle)
-{
-       mono_w32handle_unref (handle);
-}
-
 int
 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
 {
@@ -311,56 +275,6 @@ mono_native_thread_join (MonoNativeThreadId tid)
        return !pthread_join (tid, &res);
 }
 
-void
-mono_threads_platform_set_exited (gpointer handle)
-{
-       int thr_ret;
-
-       g_assert (handle);
-       if (mono_w32handle_issignalled (handle))
-               g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, handle, mono_native_thread_id_get ());
-       if (mono_w32handle_get_type (handle) == MONO_W32HANDLE_UNUSED)
-               g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, handle, mono_native_thread_id_get ());
-
-       thr_ret = mono_w32handle_lock_handle (handle);
-       g_assert (thr_ret == 0);
-
-       mono_w32handle_set_signal_state (handle, TRUE, TRUE);
-
-       thr_ret = mono_w32handle_unlock_handle (handle);
-       g_assert (thr_ret == 0);
-}
-
-static const gchar* thread_typename (void)
-{
-       return "Thread";
-}
-
-static gsize thread_typesize (void)
-{
-       return 0;
-}
-
-static MonoW32HandleOps thread_ops = {
-       NULL,                           /* close */
-       NULL,                           /* signal */
-       NULL,                           /* own */
-       NULL,                           /* is_owned */
-       NULL,                           /* special_wait */
-       NULL,                           /* prewait */
-       NULL,                           /* details */
-       thread_typename,        /* typename */
-       thread_typesize,        /* typesize */
-};
-
-void
-mono_threads_platform_init (void)
-{
-       mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
-
-       mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
-}
-
 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
 
 #if defined(USE_POSIX_BACKEND)
@@ -392,8 +306,13 @@ This begins async resume. This function must do the following:
 gboolean
 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
 {
-       mono_threads_add_to_pending_operation_set (info);
-       return mono_threads_pthread_kill (info, mono_threads_suspend_get_restart_signal ()) == 0;
+       int sig = mono_threads_suspend_get_restart_signal ();
+
+       if (!mono_threads_pthread_kill (info, sig)) {
+               mono_threads_add_to_pending_operation_set (info);
+               return TRUE;
+       }
+       return FALSE;
 }
 
 void