[threads] Remove mono_threads_create_thread (#4411)
[mono.git] / mono / utils / mono-threads-posix.c
index 66746ad3dfa2896823229ab1e97335b19eb32351..d978c538ae9b2d934a946088a145d8c47e379bc8 100644 (file)
@@ -35,19 +35,17 @@ extern int tkill (pid_t tid, int signal);
 
 #include <sys/resource.h>
 
-int
-mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *out_tid)
+gboolean
+mono_thread_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *tid)
 {
        pthread_attr_t attr;
        pthread_t thread;
-       int policy;
-       struct sched_param param;
        gint res;
        gsize set_stack_size;
-       size_t min_size;
 
        res = pthread_attr_init (&attr);
-       g_assert (!res);
+       if (res != 0)
+               g_error ("%s: pthread_attr_init failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
 
        if (stack_size)
                set_stack_size = *stack_size;
@@ -72,65 +70,34 @@ mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_
 #endif
 
        res = pthread_attr_setstacksize (&attr, set_stack_size);
-       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 */
+               g_error ("%s: pthread_attr_setstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
+#endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
 
-       min = sched_get_priority_min (policy);
-       max = sched_get_priority_max (policy);
+       /* Actually start the thread */
+       res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
+       if (res) {
+               res = pthread_attr_destroy (&attr);
+               if (res != 0)
+                       g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
 
-       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);
-               }
+               return FALSE;
        }
 
-       res = pthread_attr_setschedparam (&attr, &param);
-       if (res != 0)
-               g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
+       if (tid)
+               *tid = thread;
 
        if (stack_size) {
-               res = pthread_attr_getstacksize (&attr, &min_size);
+               res = pthread_attr_getstacksize (&attr, stack_size);
                if (res != 0)
-                       g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", g_strerror (res), res);
-               else
-                       *stack_size = min_size;
+                       g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
        }
 
-       /* Actually start the thread */
-       res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
-       if (res)
-               return -1;
-
-       if (out_tid)
-               *out_tid = thread;
+       res = pthread_attr_destroy (&attr);
+       if (res != 0)
+               g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
 
-       return 0;
+       return TRUE;
 }
 
 void
@@ -231,8 +198,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [63];
 
-               strncpy (n, name, 63);
-               n [62] = '\0';
+               memcpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (n);
        }
 #elif defined (__NetBSD__)
@@ -241,8 +208,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [PTHREAD_MAX_NAMELEN_NP];
 
-               strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
-               n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
+               memcpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (tid, "%s", (void*)n);
        }
 #elif defined (HAVE_PTHREAD_SETNAME_NP)
@@ -251,8 +218,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [16];
 
-               strncpy (n, name, 16);
-               n [15] = '\0';
+               memcpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (tid, n);
        }
 #endif
@@ -317,12 +284,6 @@ mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
        }
 }
 
-gboolean
-mono_threads_suspend_needs_abort_syscall (void)
-{
-       return TRUE;
-}
-
 void
 mono_threads_suspend_register (MonoThreadInfo *info)
 {