[mono-threads] Fix win32 to posix priority conversion
authorLudovic Henry <ludovic@xamarin.com>
Wed, 10 Aug 2016 14:49:45 +0000 (16:49 +0200)
committerLudovic Henry <ludovic@xamarin.com>
Tue, 6 Sep 2016 15:01:26 +0000 (17:01 +0200)
mono/utils/mono-threads-posix.c

index ac4fc138eaf5f2742e687aa40deebace1a02daf7..b201a838fa465d47af9a62c0ac1662202c2cb216 100644 (file)
@@ -45,6 +45,7 @@ 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
@@ -53,9 +54,14 @@ win32_priority_to_posix_priority (MonoThreadPriority priority, int policy)
        min = sched_get_priority_min (policy);
        max = sched_get_priority_max (policy);
 
-       /* Partition priority range linearly (cross-multiply) */
-       if (max > 0 && min >= 0 && max > min)
-               return (int)((double) priority * (max - min) / (MONO_THREAD_PRIORITY_HIGHEST - MONO_THREAD_PRIORITY_LOWEST));
+       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) {