Merge pull request #3388 from ntherning/fix-thread-priority-on-windows
authorNiklas Therning <niklas@therning.org>
Thu, 11 Aug 2016 07:20:54 +0000 (09:20 +0200)
committerGitHub <noreply@github.com>
Thu, 11 Aug 2016 07:20:54 +0000 (09:20 +0200)
Fix getting/setting thread priority on Windows

mono/tests/priority.cs
mono/utils/mono-threads-windows.c

index 67591ddcc06bb784a0a36e3d4bb8ffe61b9d36a9..7956f23a4cba228035accae07c9673698e0a89b4 100644 (file)
@@ -5,8 +5,11 @@ using System.Text;
 
 public class Tests
 {
+       private static int mainThreadId;
+
        public static int Main ()
        {
+               mainThreadId = Thread.CurrentThread.ManagedThreadId;
                return TestDriver.RunTests (typeof (Tests));
        }
 
@@ -21,6 +24,46 @@ public class Tests
                        Thread.CurrentThread.Priority.ToString());
        }
        
+       public static int test_0_main_thread_priority ()
+       {
+               Console.WriteLine("Testing main thread's priority");
+               if (Thread.CurrentThread.ManagedThreadId != mainThreadId)
+               {
+                       Console.WriteLine("test_0_main_thread_priority() must be run on the main thread");
+                       return 1;
+               }
+
+               var before = Thread.CurrentThread.Priority;
+               Console.WriteLine("Priority: {0}", before);
+               if (before != ThreadPriority.Normal)
+                       return 2;
+
+               Console.WriteLine("Setting main thread's priority to AboveNormal");
+               Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
+               var after = Thread.CurrentThread.Priority;
+               Console.WriteLine("Priority: {0} {1}", before, after);
+               if (after != ThreadPriority.AboveNormal)
+                       return 3;
+
+               before = after;
+               Console.WriteLine("Setting main thread's priority to BelowNormal");
+               Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
+               after = Thread.CurrentThread.Priority;
+               Console.WriteLine("Priority: {0} {1}", before, after);
+               if (after != ThreadPriority.BelowNormal)
+                       return 4;
+
+               before = after;
+               Console.WriteLine("Setting main thread's priority to Normal");
+               Thread.CurrentThread.Priority = ThreadPriority.Normal;
+               after = Thread.CurrentThread.Priority;
+               Console.WriteLine("Priority: {0} {1}", before, after);
+               if (after != ThreadPriority.Normal)
+                       return 5;
+
+               return 0;
+       }
+
        public static int test_0_thread_priority () 
        {
                int res = 0;
index 034200d3173aad0a237c32cacca1de5785d616f7..b926791f0621f7dd5e0a57dba29569ae6b10815a 100644 (file)
@@ -121,6 +121,8 @@ mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
 void
 mono_threads_suspend_register (MonoThreadInfo *info)
 {
+       g_assert (!info->handle);
+       info->handle = mono_threads_platform_open_handle();
 }
 
 void
@@ -135,6 +137,7 @@ mono_threads_suspend_free (MonoThreadInfo *info)
 typedef struct {
        LPTHREAD_START_ROUTINE start_routine;
        void *arg;
+       gint32 priority;
        MonoCoopSem registered;
        gboolean suspend;
        HANDLE suspend_event;
@@ -156,6 +159,8 @@ inner_start_thread (LPVOID arg)
        info->runtime_thread = TRUE;
        info->create_suspended = suspend;
 
+       mono_threads_platform_set_priority(info, start_info->priority);
+
        mono_coop_sem_post (&(start_info->registered));
 
        if (suspend) {
@@ -184,6 +189,7 @@ mono_threads_platform_create_thread (MonoThreadStart start_routine, gpointer arg
                return NULL;
        mono_coop_sem_init (&(start_info->registered), 0);
        start_info->arg = arg;
+       start_info->priority = tp->priority;
        start_info->start_routine = start_routine;
        start_info->suspend = creation_flags & CREATE_SUSPENDED;
        creation_flags &= ~CREATE_SUSPENDED;
@@ -388,12 +394,14 @@ mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
 MonoThreadPriority
 mono_threads_platform_get_priority (MonoThreadInfo *info)
 {
+       g_assert (info->handle);
        return GetThreadPriority (info->handle) + 2;
 }
 
 gboolean
 mono_threads_platform_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
 {
+       g_assert (info->handle);
        return SetThreadPriority (info->handle, priority - 2);
 }