[glib] Move thread priority translation into the backends



commit 46af418e05de82cfd6174575ef3f376aecfe8795
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Aug 31 16:30:04 2011 -0400

    Move thread priority translation into the backends
    
    The translation of GLib priorities into the thread priorities of
    different operating systems belongs in the implementation -- not
    half-way in the front end.

 gthread/gthread-impl.c  |   26 --------------------------
 gthread/gthread-posix.c |   30 ++++++++++++++++++++++++++++--
 gthread/gthread-win32.c |   32 +++++++++++++++++++++++---------
 3 files changed, 51 insertions(+), 37 deletions(-)
---
diff --git a/gthread/gthread-impl.c b/gthread/gthread-impl.c
index f7d5860..8b531dc 100644
--- a/gthread/gthread-impl.c
+++ b/gthread/gthread-impl.c
@@ -36,28 +36,8 @@
 #include "glib.h"
 #include "gthreadprivate.h"
 
-static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
-
 #include G_THREAD_SOURCE
 
-#ifndef PRIORITY_LOW_VALUE
-# define PRIORITY_LOW_VALUE 0
-#endif
-
-#ifndef PRIORITY_URGENT_VALUE
-# define PRIORITY_URGENT_VALUE 0
-#endif
-
-#ifndef PRIORITY_NORMAL_VALUE
-# define PRIORITY_NORMAL_VALUE						\
-  ((PRIORITY_LOW_VALUE * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
-#endif /* PRIORITY_NORMAL_VALUE */
-
-#ifndef PRIORITY_HIGH_VALUE
-# define PRIORITY_HIGH_VALUE						\
-  ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
-#endif /* PRIORITY_HIGH_VALUE */
-
 void
 g_thread_init (GThreadFunctions *init)
 {
@@ -76,12 +56,6 @@ g_thread_init (GThreadFunctions *init)
 #endif /* HAVE_G_THREAD_IMPL_INIT */
 
   g_thread_functions_for_glib_use = g_thread_functions_for_glib_use_default;
-
-  g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
-  g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
-  g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
-  g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
-
   g_thread_init_glib ();
 }
 
diff --git a/gthread/gthread-posix.c b/gthread/gthread-posix.c
index 937c73e..d4467c0 100644
--- a/gthread/gthread-posix.c
+++ b/gthread/gthread-posix.c
@@ -97,6 +97,32 @@ static gint priority_normal_value;
 #  define PRIORITY_URGENT_VALUE   POSIX_MAX_PRIORITY
 # endif /* !__FreeBSD__ */
 # define PRIORITY_NORMAL_VALUE    priority_normal_value
+
+# define PRIORITY_HIGH_VALUE \
+    ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
+
+static gint
+g_thread_priority_map (GThreadPriority priority)
+{
+  switch (priority)
+    {
+    case G_THREAD_PRIORITY_LOW:
+      return PRIORITY_LOW_VALUE;
+
+    case G_THREAD_PRIORITY_NORMAL:
+      return PRIORITY_NORMAL_VALUE;
+
+    case G_THREAD_PRIORITY_HIGH:
+      return PRIORITY_HIGH_VALUE;
+
+    case G_THREAD_PRIORITY_URGENT:
+      return PRIORITY_URGENT_VALUE;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
 #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
 
 static gulong g_thread_min_stack_size = 0;
@@ -288,7 +314,7 @@ g_thread_create_posix_impl (GThreadFunc thread_func,
   {
     struct sched_param sched;
     posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
-    sched.sched_priority = g_thread_priority_map [priority];
+    sched.sched_priority = g_thread_priority_map (priority);
     posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
   }
 #endif /* HAVE_PRIORITIES */
@@ -336,7 +362,7 @@ g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
     int policy;
     posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
 					    &sched));
-    sched.sched_priority = g_thread_priority_map [priority];
+    sched.sched_priority = g_thread_priority_map (priority);
     posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
 						 &sched));
   }
diff --git a/gthread/gthread-win32.c b/gthread/gthread-win32.c
index c72d061..df16233 100644
--- a/gthread/gthread-win32.c
+++ b/gthread/gthread-win32.c
@@ -54,11 +54,6 @@
 
 #define G_MUTEX_SIZE (sizeof (gpointer))
 
-#define PRIORITY_LOW_VALUE    THREAD_PRIORITY_BELOW_NORMAL
-#define PRIORITY_NORMAL_VALUE THREAD_PRIORITY_NORMAL
-#define PRIORITY_HIGH_VALUE   THREAD_PRIORITY_ABOVE_NORMAL
-#define PRIORITY_URGENT_VALUE THREAD_PRIORITY_HIGHEST
-
 static DWORD g_thread_self_tls;
 static DWORD g_private_tls;
 static DWORD g_cond_event_tls;
@@ -380,12 +375,31 @@ static void
 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
 {
   GThreadData *target = *(GThreadData **)thread;
+  gint native_prio;
 
-  g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
-  g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
+  switch (priority)
+    {
+    case G_THREAD_PRIORITY_LOW:
+      native_prio = THREAD_PRIORITY_BELOW_NORMAL;
+      break;
+
+    case G_THREAD_PRIORITY_NORMAL:
+      native_prio = THREAD_PRIORITY_NORMAL;
+      break;
+
+    case G_THREAD_PRIORITY_HIGH:
+      native_prio = THREAD_PRIORITY_ABOVE_NORMAL;
+      break;
+
+    case G_THREAD_PRIORITY_URGENT:
+      native_prio = THREAD_PRIORITY_HIGHEST;
+      break;
+
+    default:
+      g_return_if_reached ();
+    }
 
-  win32_check_for_error (SetThreadPriority (target->thread,
-					    g_thread_priority_map [priority]));
+  win32_check_for_error (SetThreadPriority (target->thread, native_prio));
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]