Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / utils / mono-threads-posix.c
index 66746ad3dfa2896823229ab1e97335b19eb32351..6804024e156a0a6a31ff4c9ff3db282344197a02 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * mono-threads-posix.c: Low-level threading, posix version
+/**
+ * \file
+ * Low-level threading, posix version
  *
  * Author:
  *     Rodrigo Kumpera (kumpera@gmail.com)
@@ -21,7 +22,7 @@
 
 #include <errno.h>
 
-#if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
+#if defined(HOST_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
 #define USE_TKILL_ON_ANDROID 1
 #endif
 
 extern int tkill (pid_t tid, int signal);
 #endif
 
-#if defined(_POSIX_VERSION) || defined(__native_client__)
+#if defined(_POSIX_VERSION) && !defined (TARGET_WASM)
 
 #include <pthread.h>
 
 #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 +71,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
@@ -174,22 +142,29 @@ 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));
+
+       int result;
+
 #ifdef USE_TKILL_ON_ANDROID
-       int result, old_errno = errno;
+       int old_errno = errno;
+
        result = tkill (info->native_handle, signum);
+
        if (result < 0) {
                result = errno;
                errno = old_errno;
        }
-       return result;
-#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");
+#elif defined (HAVE_PTHREAD_KILL)
+       result = pthread_kill (mono_thread_info_get_tid (info), signum);
 #else
-       return pthread_kill (mono_thread_info_get_tid (info), signum);
+       result = -1;
+       g_error ("pthread_kill () is not supported by this platform");
 #endif
+
+       if (result && result != ESRCH)
+               g_error ("%s: pthread_kill failed with error %d - potential kernel OOM or signal queue overflow", __func__, result);
+
+       return result;
 }
 
 MonoNativeThreadId
@@ -231,8 +206,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [63];
 
-               strncpy (n, name, 63);
-               n [62] = '\0';
+               strncpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (n);
        }
 #elif defined (__NetBSD__)
@@ -241,8 +216,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';
+               strncpy (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 +226,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [16];
 
-               strncpy (n, name, 16);
-               n [15] = '\0';
+               strncpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (tid, n);
        }
 #endif
@@ -266,7 +241,7 @@ mono_native_thread_join (MonoNativeThreadId tid)
        return !pthread_join (tid, &res);
 }
 
-#endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
+#endif /* defined(_POSIX_VERSION) */
 
 #if defined(USE_POSIX_BACKEND)
 
@@ -317,16 +292,10 @@ mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
        }
 }
 
-gboolean
-mono_threads_suspend_needs_abort_syscall (void)
-{
-       return TRUE;
-}
-
 void
 mono_threads_suspend_register (MonoThreadInfo *info)
 {
-#if defined (PLATFORM_ANDROID)
+#if defined (HOST_ANDROID)
        info->native_handle = gettid ();
 #endif
 }