New thread priority mechanism on linux.
- From: Sebastian Wilhelmi <wilhelmi ira uka de>
- To: Gtk Development List <gtk-devel-list gnome org>, timj gtk org
- Subject: New thread priority mechanism on linux.
- Date: Fri, 10 Nov 2000 14:16:43 +0100
Hi,
Tim recently mentioned, that thread priorities don't work on linux. And in
fact on linux (2.2.14) the following holds:
sched_get_priority_min(SCHED_OTHER) == sched_get_priority_max(SCHED_OTHER)
I.e. all threads have the same priority (for whatever reason they decided to
do that). But on linux there is another peculiarity. Threads all have their
own PID. So I added the following thing to GLib. On all systems with different
PIDs for different threads _and_ minimal == maximal thread priority the
process niceness is used to simulate priorities.
This actually works pretty good on linux. Here's a little test-program:
--------------------------------------------------------------------------------------
#include <glib.h>
G_LOCK_DEFINE (thread_start);
void
print_out_func (gpointer data)
{
gchar *name = data;
int count = 0;
int i;
int j = GPOINTER_TO_INT (name); /* to avoid optimizing the loop away ;-) */
G_LOCK (thread_start);
G_UNLOCK (thread_start);
while (TRUE)
{
for (i = 0; i <10000000; i++)
j *= i;
g_print ("%8s: %3d\n", name, count);
count++;
}
}
int
main()
{
GThread *low, *normal, *high, *urgent;
g_thread_init (NULL);
G_LOCK (thread_start);
low = g_thread_create (print_out_func, "low", 0, TRUE, TRUE,
G_THREAD_PRIORITY_LOW, NULL);
normal = g_thread_create (print_out_func, "normal", 0, TRUE, TRUE,
G_THREAD_PRIORITY_NORMAL, NULL);
high = g_thread_create (print_out_func, "high", 0, TRUE, TRUE,
G_THREAD_PRIORITY_HIGH, NULL);
urgent = g_thread_create (print_out_func, "urgent", 0, TRUE, TRUE,
G_THREAD_PRIORITY_URGENT, NULL);
g_usleep (G_USEC_PER_SEC);
G_UNLOCK (thread_start);
g_usleep (10 * G_USEC_PER_SEC);
return 0;
}
--------------------------------------------------------------------------------------
It also demonstrates, that only root can achieve certain levels of niceness.
In general only low and normal priorities are available to non-super-users.
High and urgent priorities can only be used by root. We can still change those
values though.
The patch is attached. If noone objects, I'm going to commit this next week.
Bye,
Sebastian
--
Sebastian Wilhelmi
mailto:wilhelmi ira uka de
http://goethe.ira.uka.de/~wilhelmi
Index: configure.in
===================================================================
RCS file: /cvs/gnome/glib/configure.in,v
retrieving revision 1.156
diff -u -b -B -r1.156 configure.in
--- configure.in 2000/10/30 21:55:21 1.156
+++ configure.in 2000/11/10 12:54:55
@@ -1033,6 +1033,28 @@
fi
AC_DEFINE_UNQUOTED(POSIX_YIELD_FUNC,$posix_yield_func,[The POSIX RT yield function])
CPPFLAGS="$glib_save_CPPFLAGS"
+
+ AC_MSG_CHECKING(whether to use PID surrogate for thread priorities)
+ AC_TRY_RUN([#include <pthread.h>
+ #include <sys/types.h>
+ #include <unistd.h>
+ pid_t other_pid = 0;
+
+ void* func(void* data) {other_pid = getpid();}
+ main()
+ { pthread_t t;
+ void *ret;
+ pthread_create (&t, $defattr, func, NULL);
+ pthread_join (t, &ret);
+ exit (getpid()==other_pid ||
+ $posix_priority_min != $posix_priority_max);
+ }],
+ [AC_MSG_RESULT(yes),
+ AC_DEFINE(USE_PID_SURROGATE, 1,
+ [whether to use the PID surrogate for thread priorities])
+ g_system_thread_sizeof=`expr $g_system_thread_sizeof + 4`],
+ [AC_MSG_RESULT(no)])
+
else # solaris threads
GLIB_SIZEOF([#include <thread.h>],
thread_t,
Index: gthread/gthread-impl.c
===================================================================
RCS file: /cvs/gnome/glib/gthread/gthread-impl.c,v
retrieving revision 1.5
diff -u -b -B -r1.5 gthread-impl.c
--- gthread/gthread-impl.c 2000/09/29 13:10:41 1.5
+++ gthread/gthread-impl.c 2000/11/10 12:54:56
@@ -38,12 +38,20 @@
#include <glib.h>
static gboolean thread_system_already_initialized = FALSE;
-static gint g_thread_map_priority (GThreadPriority priority);
-static gint g_thread_min_priority = 0;
-static gint g_thread_max_priority = 0;
+static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT];
#include G_THREAD_SOURCE
+#ifndef PRIORITY_NORMAL_VALUE
+#define PRIORITY_NORMAL_VALUE \
+ PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 40 / 100
+#endif /* PRIORITY_NORMAL_VALUE */
+
+#ifndef PRIORITY_HIGH_VALUE
+#define PRIORITY_HIGH_VALUE \
+ PRIORITY_LOW_VALUE + (PRIORITY_URGENT_VALUE - PRIORITY_LOW_VALUE) * 80 / 100
+#endif /* PRIORITY_HIGH_VALUE */
+
void g_mutex_init (void);
void g_mem_init (void);
void g_messages_init (void);
@@ -253,6 +261,11 @@
* but only if called with a NULL argument, of course. Otherwise
* it's up to the user to do so. */
+ 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;
+
#ifdef HAVE_G_THREAD_IMPL_INIT
if (g_thread_use_default_impl)
g_thread_impl_init();
@@ -272,19 +285,4 @@
/* we want the main thread to run with normal priority */
g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
-}
-
-static gint
-g_thread_map_priority (GThreadPriority priority)
-{
- guint procent;
- switch (priority)
- {
- case G_THREAD_PRIORITY_LOW: procent = 0; break;
- default: case G_THREAD_PRIORITY_NORMAL: procent = 40; break;
- case G_THREAD_PRIORITY_HIGH: procent = 80; break;
- case G_THREAD_PRIORITY_URGENT: procent = 100; break;
- }
- return g_thread_min_priority +
- (g_thread_max_priority - g_thread_min_priority) * procent / 100;
}
Index: gthread/gthread-posix.c
===================================================================
RCS file: /cvs/gnome/glib/gthread/gthread-posix.c,v
retrieving revision 1.22
diff -u -b -B -r1.22 gthread-posix.c
--- gthread/gthread-posix.c 2000/10/25 10:58:46 1.22
+++ gthread/gthread-posix.c 2000/11/10 12:54:56
@@ -35,16 +35,25 @@
#include <errno.h>
#include <stdlib.h>
#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
+# include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+# include <unistd.h>
#endif
+#ifdef USE_PID_SURROGATE
+# include <sys/resource.h>
+# define PID_IN_THREAD(thread) (*(pid_t*)(((gchar*)thread)+sizeof(pthread_t)))
+# define SET_PRIO(pid, prio) \
+ posix_check_cmd_prio ((setpriority (PRIO_PROCESS, (pid), \
+ g_thread_priority_map [prio]) == -1) ? \
+ (errno == EACCES ? EPERM : errno ): 0)
+#endif /* USE_PID_SURROGATE */
+
#define posix_check_err(err, name) G_STMT_START{ \
int error = (err); \
if (error) \
- g_error ("file %s: line %d (%s): error %s during %s", \
+ g_error ("file %s: line %d (%s): error '%s' during '%s'", \
__FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
g_strerror (error), name); \
}G_STMT_END
@@ -60,7 +69,8 @@
if (!posix_check_cmd_prio_warned) \
{ \
posix_check_cmd_prio_warned = TRUE; \
- g_warning ("Priorities can only be changed by root."); \
+ g_warning ("Priorities can only be changed " \
+ "(resp. increased) by root."); \
} \
} \
else \
@@ -90,9 +100,16 @@
# error This should not happen. Contact the GLib team.
#endif
-#if defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
+#ifdef USE_PID_SURROGATE
+# define PRIORITY_LOW_VALUE 15
+# define PRIORITY_NORMAL_VALUE 0
+# define PRIORITY_HIGH_VALUE -15
+# define PRIORITY_URGENT_VALUE -20
+#elif defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
# define HAVE_PRIORITIES 1
-#endif
+# define PRIORITY_LOW_VALUE POSIX_MIN_PRIORITY
+# define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY
+#endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
gulong g_thread_min_stack_size = 0;
@@ -102,10 +119,6 @@
static void
g_thread_impl_init()
{
-#ifdef HAVE_PRIORITIES
- g_thread_min_priority = POSIX_MIN_PRIORITY;
- g_thread_max_priority = POSIX_MAX_PRIORITY;
-#endif /* HAVE_PRIORITIES */
#ifdef _SC_THREAD_STACK_MIN
g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
#endif /* _SC_THREAD_STACK_MIN */
@@ -248,7 +261,28 @@
#endif
}
+#ifdef USE_PID_SURROGATE
+struct proxy_data
+{
+ GThreadFunc thread_func;
+ gpointer arg;
+ gpointer thread;
+ GThreadPriority priority;
+};
+
static void
+g_thread_create_posix_impl_proxy (struct proxy_data *data)
+{
+ GThreadFunc thread_func = data->thread_func;
+ GThreadFunc arg = data->arg;
+ PID_IN_THREAD (data->thread) = getpid();
+ SET_PRIO (PID_IN_THREAD (data->thread), data->priority);
+ g_free (data);
+ thread_func (arg);
+}
+#endif /* USE_PID_SURROGATE */
+
+static void
g_thread_create_posix_impl (GThreadFunc thread_func,
gpointer arg,
gulong stack_size,
@@ -262,6 +296,8 @@
gint ret;
g_return_if_fail (thread_func);
+ g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
+ g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
posix_check_cmd (pthread_attr_init (&attr));
@@ -285,22 +321,35 @@
joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
#endif /* G_THREADS_IMPL_POSIX */
-#ifdef HAVE_PRIORITIES
+#ifdef USE_PID_SURROGATE
+ {
+ struct proxy_data *data = g_new (struct proxy_data, 1);
+ data->thread_func = thread_func;
+ data->arg = arg;
+ data->thread = thread;
+ data->priority = priority;
+ PID_IN_THREAD (thread) = 0;
+ ret = posix_error (pthread_create (thread, &attr, (void* (*)(void*))
+ g_thread_create_posix_impl_proxy,
+ data));
+ }
+#else /* USE_PID_SURROGATE */
+# ifdef HAVE_PRIORITIES
# ifdef G_THREADS_IMPL_POSIX
{
struct sched_param sched;
posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
- sched.sched_priority = g_thread_map_priority (priority);
+ sched.sched_priority = g_thread_priority_map [priority];
posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
}
# else /* G_THREADS_IMPL_DCE */
posix_check_cmd_prio
- (pthread_attr_setprio (&attr, g_thread_map_priority (priority)));
+ (pthread_attr_setprio (&attr, g_thread_priority_map [priority]));
# endif /* G_THREADS_IMPL_DCE */
-#endif /* HAVE_PRIORITIES */
-
+# endif /* HAVE_PRIORITIES */
ret = posix_error (pthread_create (thread, &attr,
(void* (*)(void*))thread_func, arg));
+#endif /* !USE_PID_SURROGATE */
posix_check_cmd (pthread_attr_destroy (&attr));
@@ -341,26 +390,34 @@
static void
g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
{
+ g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
+ g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
#ifdef HAVE_PRIORITIES
# ifdef G_THREADS_IMPL_POSIX
struct sched_param sched;
int policy;
posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
&sched));
- sched.sched_priority = g_thread_map_priority (priority);
+ sched.sched_priority = g_thread_priority_map [priority];
posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
&sched));
# else /* G_THREADS_IMPL_DCE */
posix_check_cmd_prio (pthread_setprio (*(pthread_t*)thread,
- g_thread_map_priority (priority)));
+ g_thread_priority_map [priority]));
# endif
-#endif /* HAVE_PRIORITIES */
+#elif defined (USE_PID_SURROGATE)
+ /* If the addressed thread hasn't yet been able to provide it's pid,
+ * we ignore the request. Should be more than rare */
+ if (PID_IN_THREAD (thread) != 0)
+ SET_PRIO (PID_IN_THREAD (thread), priority);
+#endif /* USE_PID_SURROGATE */
}
static void
g_thread_self_posix_impl (gpointer thread)
{
*(pthread_t*)thread = pthread_self();
+ PID_IN_THREAD (thread) = getpid();
}
static GThreadFunctions g_thread_functions_for_glib_use_default =
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]