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.
This commit is contained in:
Ryan Lortie 2011-08-31 16:30:04 -04:00
parent a4777122c8
commit 46af418e05
3 changed files with 51 additions and 37 deletions

View File

@ -36,28 +36,8 @@
#include "glib.h" #include "glib.h"
#include "gthreadprivate.h" #include "gthreadprivate.h"
static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
#include G_THREAD_SOURCE #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 void
g_thread_init (GThreadFunctions *init) g_thread_init (GThreadFunctions *init)
{ {
@ -76,12 +56,6 @@ g_thread_init (GThreadFunctions *init)
#endif /* HAVE_G_THREAD_IMPL_INIT */ #endif /* HAVE_G_THREAD_IMPL_INIT */
g_thread_functions_for_glib_use = g_thread_functions_for_glib_use_default; 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 (); g_thread_init_glib ();
} }

View File

@ -97,6 +97,32 @@ static gint priority_normal_value;
# define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY # define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY
# endif /* !__FreeBSD__ */ # endif /* !__FreeBSD__ */
# define PRIORITY_NORMAL_VALUE priority_normal_value # 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 */ #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
static gulong g_thread_min_stack_size = 0; static gulong g_thread_min_stack_size = 0;
@ -288,7 +314,7 @@ g_thread_create_posix_impl (GThreadFunc thread_func,
{ {
struct sched_param sched; struct sched_param sched;
posix_check_cmd (pthread_attr_getschedparam (&attr, &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)); posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
} }
#endif /* HAVE_PRIORITIES */ #endif /* HAVE_PRIORITIES */
@ -336,7 +362,7 @@ g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
int policy; int policy;
posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy, posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
&sched)); &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, posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
&sched)); &sched));
} }

View File

@ -54,11 +54,6 @@
#define G_MUTEX_SIZE (sizeof (gpointer)) #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_thread_self_tls;
static DWORD g_private_tls; static DWORD g_private_tls;
static DWORD g_cond_event_tls; static DWORD g_cond_event_tls;
@ -380,12 +375,31 @@ static void
g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority) g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
{ {
GThreadData *target = *(GThreadData **)thread; GThreadData *target = *(GThreadData **)thread;
gint native_prio;
g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW); switch (priority)
g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT); {
case G_THREAD_PRIORITY_LOW:
native_prio = THREAD_PRIORITY_BELOW_NORMAL;
break;
win32_check_for_error (SetThreadPriority (target->thread, case G_THREAD_PRIORITY_NORMAL:
g_thread_priority_map [priority])); 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, native_prio));
} }
static void static void