[mono-threads] Have error in case we cannot set thread priority
authorLudovic Henry <ludovic@xamarin.com>
Mon, 5 Sep 2016 13:41:56 +0000 (15:41 +0200)
committerLudovic Henry <ludovic@xamarin.com>
Tue, 6 Sep 2016 15:01:27 +0000 (17:01 +0200)
mono/utils/mono-threads-posix.c
mono/utils/mono-threads-windows.c
mono/utils/mono-threads.c
mono/utils/mono-threads.h

index b201a838fa465d47af9a62c0ac1662202c2cb216..67b470467b96339e10e560a26f99a1f2ee644b07 100644 (file)
@@ -40,44 +40,6 @@ extern int tkill (pid_t tid, int signal);
 void nacl_shutdown_gc_thread(void);
 #endif
 
-static int
-win32_priority_to_posix_priority (MonoThreadPriority priority, int policy)
-{
-       g_assert (priority >= MONO_THREAD_PRIORITY_LOWEST);
-       g_assert (priority <= MONO_THREAD_PRIORITY_HIGHEST);
-       g_assert (MONO_THREAD_PRIORITY_LOWEST < MONO_THREAD_PRIORITY_HIGHEST);
-
-/* Necessary to get valid priority range */
-#ifdef _POSIX_PRIORITY_SCHEDULING
-       int max, min;
-
-       min = sched_get_priority_min (policy);
-       max = sched_get_priority_max (policy);
-
-       if (max > 0 && min >= 0 && max > min) {
-               double srange, drange, sposition, dposition;
-               srange = MONO_THREAD_PRIORITY_HIGHEST - MONO_THREAD_PRIORITY_LOWEST;
-               drange = max - min;
-               sposition = priority - MONO_THREAD_PRIORITY_LOWEST;
-               dposition = (sposition / srange) * drange;
-               return (int)(dposition + min);
-       }
-#endif
-
-       switch (policy) {
-       case SCHED_FIFO:
-       case SCHED_RR:
-               return 50;
-#ifdef SCHED_BATCH
-       case SCHED_BATCH:
-#endif
-       case SCHED_OTHER:
-               return 0;
-       default:
-               return -1;
-       }
-}
-
 void
 mono_threads_platform_register (MonoThreadInfo *info)
 {
@@ -358,49 +320,68 @@ mono_threads_platform_get_priority (MonoThreadInfo *info)
        return info->priority;
 }
 
-gboolean
+void
 mono_threads_platform_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
 {
-       int policy, posix_priority;
+       int policy;
        struct sched_param param;
        pthread_t tid;
+       gint res;
+
+       g_assert (priority >= MONO_THREAD_PRIORITY_LOWEST);
+       g_assert (priority <= MONO_THREAD_PRIORITY_HIGHEST);
+       g_assert (MONO_THREAD_PRIORITY_LOWEST < MONO_THREAD_PRIORITY_HIGHEST);
 
        tid = mono_thread_info_get_tid (info);
 
-       switch (pthread_getschedparam (tid, &policy, &param)) {
-       case 0:
-               break;
-       case ESRCH:
-               g_warning ("pthread_getschedparam: error looking up thread id %x", (gsize)tid);
-               return FALSE;
-       default:
-               return FALSE;
+       res = pthread_getschedparam (tid, &policy, &param);
+       if (res != 0)
+               g_error ("%s: pthread_getschedparam 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) {
+               double srange, drange, sposition, dposition;
+               srange = MONO_THREAD_PRIORITY_HIGHEST - MONO_THREAD_PRIORITY_LOWEST;
+               drange = max - min;
+               sposition = priority - MONO_THREAD_PRIORITY_LOWEST;
+               dposition = (sposition / srange) * drange;
+               param.sched_priority = (int)(dposition + 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);
+               }
        }
 
-       posix_priority =  win32_priority_to_posix_priority (priority, policy);
-       if (posix_priority < 0)
-               return FALSE;
-
-       param.sched_priority = posix_priority;
-       switch (pthread_setschedparam (tid, policy, &param)) {
-       case 0:
-               break;
-       case ESRCH:
-               g_warning ("%s: pthread_setschedprio: error looking up thread id %x", __func__, (gsize)tid);
-               return FALSE;
-       case ENOTSUP:
-               g_warning ("%s: priority %d not supported", __func__, priority);
-               return FALSE;
-       case EPERM:
-               g_warning ("%s: permission denied", __func__);
-               return FALSE;
-       default:
-               return FALSE;
+       res = pthread_setschedparam (tid, policy, &param);
+       if (res != 0) {
+               if (res == EPERM) {
+                       g_warning ("%s: pthread_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
+                       return;
+               }
+               g_error ("%s: pthread_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
        }
 
        info->priority = priority;
-       return TRUE;
-
 }
 
 static const gchar* thread_typename (void)
index bb8d300a58b59306e44268214dd1ed46a932a1ef..4f8495054334310bb2f7289e8778e657915e59f2 100644 (file)
@@ -331,11 +331,16 @@ mono_threads_platform_get_priority (MonoThreadInfo *info)
        return GetThreadPriority (info->handle) + 2;
 }
 
-gboolean
+void
 mono_threads_platform_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
 {
+       BOOL res;
+
        g_assert (info->handle);
-       return SetThreadPriority (info->handle, priority - 2);
+
+       res = SetThreadPriority (info->handle, priority - 2);
+       if (!res)
+               g_error ("%s: SetThreadPriority failed, error %d", __func__, GetLastError ());
 }
 
 void
index 0611e739d3772bbedc9b1e606c2bac84585a696c..a75adc4599f523313fd5ce23bfb4eb966654e562 100644 (file)
@@ -1719,8 +1719,8 @@ mono_thread_info_get_priority (MonoThreadInfo *info)
        return mono_threads_platform_get_priority (info);
 }
 
-gboolean
+void
 mono_thread_info_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
 {
-       return mono_threads_platform_set_priority (info, priority);
+       mono_threads_platform_set_priority (info, priority);
 }
index 9ecc4bf41cdd28f0efab619b5f18d3b2f07bf56d..c81f3c6ba3f4e8c4f97fb6528ec103c86b9ee120 100644 (file)
@@ -555,7 +555,7 @@ void mono_threads_platform_describe (THREAD_INFO_TYPE *info, GString *text);
 void mono_threads_platform_own_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
 void mono_threads_platform_disown_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
 MonoThreadPriority mono_threads_platform_get_priority (THREAD_INFO_TYPE *info);
-gboolean mono_threads_platform_set_priority (THREAD_INFO_TYPE *info, MonoThreadPriority priority);
+void mono_threads_platform_set_priority (THREAD_INFO_TYPE *info, MonoThreadPriority priority);
 
 void mono_threads_coop_begin_global_suspend (void);
 void mono_threads_coop_end_global_suspend (void);
@@ -685,7 +685,7 @@ mono_thread_info_disown_mutex (THREAD_INFO_TYPE *info, gpointer mutex_handle);
 MonoThreadPriority
 mono_thread_info_get_priority (THREAD_INFO_TYPE *info);
 
-gboolean
+void
 mono_thread_info_set_priority (THREAD_INFO_TYPE *info, MonoThreadPriority priority);
 
 #endif /* __MONO_THREADS_H__ */