[maemo-commits] [maemo-commits] r14936 - in projects/haf/trunk/glib: glib gthread

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Mon Dec 10 14:54:05 EET 2007
Author: xan
Date: 2007-12-10 14:53:55 +0200 (Mon, 10 Dec 2007)
New Revision: 14936

Modified:
   projects/haf/trunk/glib/glib/gthread.c
   projects/haf/trunk/glib/glib/gthread.h
   projects/haf/trunk/glib/glib/gthreadpool.c
   projects/haf/trunk/glib/glib/gthreadpool.h
   projects/haf/trunk/glib/gthread/gthread-posix.c
Log:
Realtime scheduling support for GThreadPools.

Part of bug NB#76856


Modified: projects/haf/trunk/glib/glib/gthread.c
===================================================================
--- projects/haf/trunk/glib/glib/gthread.c	2007-12-10 10:30:46 UTC (rev 14935)
+++ projects/haf/trunk/glib/glib/gthread.c	2007-12-10 12:53:55 UTC (rev 14936)
@@ -881,6 +881,15 @@
   g_static_mutex_free (&lock->mutex);
 }
 
+gboolean g_thread_set_scheduler (GThread         *thread,
+                                 GThreadScheduler scheduler,
+                                 gint             priority)
+{
+  GRealThread* real = (GRealThread*) thread;
+
+  return G_THREAD_CF(scheduler_set, 0, (&real->system_thread, scheduler, priority));
+}
+
 /**
  * g_thread_foreach
  * @thread_func: function to call for all GThread structures

Modified: projects/haf/trunk/glib/glib/gthread.h
===================================================================
--- projects/haf/trunk/glib/glib/gthread.h	2007-12-10 10:30:46 UTC (rev 14935)
+++ projects/haf/trunk/glib/glib/gthread.h	2007-12-10 12:53:55 UTC (rev 14936)
@@ -1,6 +1,8 @@
 /* GLIB - Library of useful routines for C programming
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
+ * Copyright (C) 2007 Nokia Corporation
+ *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
@@ -54,6 +56,13 @@
   G_THREAD_PRIORITY_URGENT
 } GThreadPriority;
 
+typedef enum
+{
+  G_THREAD_SCHEDULER_DEFAULT,
+  G_THREAD_SCHEDULER_FIFO,
+  G_THREAD_SCHEDULER_RR
+} GThreadScheduler;
+
 typedef struct _GThread         GThread;
 struct  _GThread
 {
@@ -106,6 +115,11 @@
   void      (*thread_self)        (gpointer              thread);
   gboolean  (*thread_equal)       (gpointer              thread1,
 				   gpointer              thread2);
+  gint      (*priority_min)       (GThreadScheduler      scheduler);
+  gint      (*priority_max)       (GThreadScheduler      scheduler);
+  gboolean  (*scheduler_set)      (gpointer              thread,
+                                   GThreadScheduler      scheduler,
+                                   gint                  priority);
 };
 
 GLIB_VAR GThreadFunctions       g_thread_functions_for_glib_use;
@@ -204,6 +218,13 @@
                                                        (private_key, value))
 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
 
+#define g_thread_scheduler_get_priority_min(sched) G_THREAD_CF (priority_min, \
+                                                            0, \
+                                                            (sched))
+#define g_thread_scheduler_get_priority_max(sched) G_THREAD_CF (priority_max, \
+                                                            0, \
+                                                            (sched))
+
 #define g_thread_create(func, data, joinable, error)			\
   (g_thread_create_full (func, data, 0, joinable, FALSE, 		\
                          G_THREAD_PRIORITY_NORMAL, error))
@@ -221,6 +242,9 @@
 
 void     g_thread_set_priority (GThread               *thread,
                                 GThreadPriority        priority);
+gboolean g_thread_set_scheduler (GThread              *thread,
+                                 GThreadScheduler      scheduler,
+                                 gint                  priority);
 
 /* GStaticMutexes can be statically initialized with the value
  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is

Modified: projects/haf/trunk/glib/glib/gthreadpool.c
===================================================================
--- projects/haf/trunk/glib/glib/gthreadpool.c	2007-12-10 10:30:46 UTC (rev 14935)
+++ projects/haf/trunk/glib/glib/gthreadpool.c	2007-12-10 12:53:55 UTC (rev 14936)
@@ -4,6 +4,8 @@
  * GAsyncQueue: thread pool implementation.
  * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
  *
+ * Copyright (C) 2007 Nokia Corporation
+ *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
@@ -46,6 +48,8 @@
   gboolean waiting;
   GCompareDataFunc sort_func;
   gpointer sort_user_data;
+  GThreadScheduler scheduler;
+  gint priority;
 };
 
 /* The following is just an address to mark the wakeup order for a
@@ -245,6 +249,15 @@
   DEBUG_MSG (("thread %p started for pool %p.", 
 	      g_thread_self (), pool));
 
+  /* thread self-modifies the scheduler and priority
+   */
+  if (!(pool->scheduler == G_THREAD_SCHEDULER_DEFAULT &&
+        pool->priority == 0))
+    {
+      if (!g_thread_set_scheduler(g_thread_self(), pool->scheduler, pool->priority))
+        g_warning("g_thread_set_scheduler() failed for pool %p", pool);
+    }
+
   g_async_queue_lock (pool->queue);
 
   while (TRUE)
@@ -420,6 +433,20 @@
 		   gboolean         exclusive,
 		   GError         **error)
 {
+  return g_thread_pool_new_with_priority(func, user_data, max_threads,
+                                         exclusive, G_THREAD_SCHEDULER_DEFAULT,
+                                         0, error);
+}
+
+GThreadPool* 
+g_thread_pool_new_with_priority (GFunc            func,
+                                 gpointer         user_data,
+                                 gint             max_threads,
+                                 gboolean         exclusive,
+                                 GThreadScheduler scheduler,
+                                 gint             priority,
+                                 GError         **error)
+{
   GRealThreadPool *retval;
   G_LOCK_DEFINE_STATIC (init);
 
@@ -438,6 +465,8 @@
   retval->max_threads = max_threads;
   retval->num_threads = 0;
   retval->running = TRUE;
+  retval->scheduler = scheduler;
+  retval->priority = priority;
   retval->sort_func = NULL;
   retval->sort_user_data = NULL;
 

Modified: projects/haf/trunk/glib/glib/gthreadpool.h
===================================================================
--- projects/haf/trunk/glib/glib/gthreadpool.h	2007-12-10 10:30:46 UTC (rev 14935)
+++ projects/haf/trunk/glib/glib/gthreadpool.h	2007-12-10 12:53:55 UTC (rev 14936)
@@ -1,6 +1,8 @@
 /* GLIB - Library of useful routines for C programming
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
+ * Copyright (C) 2007 Nokia Corporation
+ *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
@@ -56,6 +58,14 @@
                                                gboolean         exclusive,
                                                GError         **error);
 
+GThreadPool*    g_thread_pool_new_with_priority (GFunc            func,
+                                                 gpointer         user_data,
+                                                 gint             max_threads,
+                                                 gboolean         exclusive,
+                                                 GThreadScheduler scheduler,
+                                                 gint             priority,
+                                                 GError         **error);
+
 /* Push new data into the thread pool. This task is assigned to a thread later
  * (when the maximal number of threads is reached for that pool) or now
  * (otherwise). If necessary a new thread will be started. The function

Modified: projects/haf/trunk/glib/gthread/gthread-posix.c
===================================================================
--- projects/haf/trunk/glib/gthread/gthread-posix.c	2007-12-10 10:30:46 UTC (rev 14935)
+++ projects/haf/trunk/glib/gthread/gthread-posix.c	2007-12-10 12:53:55 UTC (rev 14936)
@@ -4,6 +4,8 @@
  * gthread.c: posix thread system implementation
  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
  *
+ * Copyright (C) 2007 Nokia Corporation
+ *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
@@ -441,6 +443,61 @@
   return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
 }
 
+gint g_thread_scheduler_priority_min_posix_impl (GThreadScheduler scheduler)
+{
+  switch (scheduler)
+  {
+    case G_THREAD_SCHEDULER_DEFAULT:
+      return sched_get_priority_min(SCHED_OTHER);
+    case G_THREAD_SCHEDULER_FIFO:
+      return sched_get_priority_min(SCHED_FIFO);
+    case G_THREAD_SCHEDULER_RR:
+      return sched_get_priority_min(SCHED_RR);
+    default:
+      return 0;
+  }
+}
+
+gint g_thread_scheduler_priority_max_posix_impl (GThreadScheduler scheduler)
+{
+  switch (scheduler)
+  {
+    case G_THREAD_SCHEDULER_DEFAULT:
+      return sched_get_priority_max(SCHED_OTHER);
+    case G_THREAD_SCHEDULER_FIFO:
+      return sched_get_priority_max(SCHED_FIFO);
+    case G_THREAD_SCHEDULER_RR:
+      return sched_get_priority_max(SCHED_RR);
+    default:
+      return 0;
+  }
+}
+
+gboolean g_thread_scheduler_set_posix_impl (gpointer         thread,
+		                            GThreadScheduler scheduler,
+                                            gint             priority)
+{
+  int schedpol;
+  struct sched_param schedprio;
+
+  switch (scheduler)
+  {
+    case G_THREAD_SCHEDULER_FIFO:
+      schedpol = SCHED_FIFO;
+      break;
+    case G_THREAD_SCHEDULER_RR:
+      schedpol = SCHED_RR;
+      break;
+    case G_THREAD_SCHEDULER_DEFAULT:
+    default:
+      schedpol = SCHED_OTHER;
+  }
+  schedprio.sched_priority = priority;
+  if (pthread_setschedparam(*(pthread_t *) thread, schedpol, &schedprio))
+    return FALSE;
+  return TRUE;
+}
+
 #ifdef USE_CLOCK_GETTIME 
 static guint64
 gettime (void)
@@ -478,5 +535,8 @@
   g_thread_exit_posix_impl,
   g_thread_set_priority_posix_impl,
   g_thread_self_posix_impl,
-  g_thread_equal_posix_impl
+  g_thread_equal_posix_impl,
+  g_thread_scheduler_priority_min_posix_impl,
+  g_thread_scheduler_priority_max_posix_impl,
+  g_thread_scheduler_set_posix_impl
 };


More information about the maemo-commits mailing list