This is the mail archive of the cygwin-cvs@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[newlib-cygwin] Fix thread priority handling


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=4b51e4c142445c133d6075d2a68ce98e8ec6058b

commit 4b51e4c142445c133d6075d2a68ce98e8ec6058b
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Fri May 20 17:45:24 2016 +0200

    Fix thread priority handling
    
    So far pthread::postcreate() only sets the thread priority at all, only
    if the inherit-scheduler attribute is PTHREAD_EXPLICIT_SCHED.  This
    completely ignores the PTHREAD_INHERIT_SCHED case, since in contrast
    to POSIX, a thread does not inherit its priority from the creating
    thread, but always starts with THREAD_PRIORITY_NORMAL.
    
    pthread_getschedparam() only returns what's stored in the thread attributes,
    not the actual thread priority.
    
    This patch fixes both problems.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/thread.cc | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 350879a..1feee5b 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -37,6 +37,7 @@ details. */
 extern "C" void __fp_lock_all ();
 extern "C" void __fp_unlock_all ();
 extern "C" bool valid_sched_parameters(const struct sched_param *);
+extern "C" int sched_get_thread_priority(HANDLE thread);
 extern "C" int sched_set_thread_priority(HANDLE thread, int priority);
 static inline verifyable_object_state
   verifyable_object_isvalid (void const * objectptr, thread_magic_t magic,
@@ -531,12 +532,15 @@ pthread::postcreate ()
   valid = true;
 
   InterlockedIncrement (&MT_INTERFACE->threadcount);
-  /* FIXME: set the priority appropriately for system contention scope */
-  if (attr.inheritsched == PTHREAD_EXPLICIT_SCHED)
-    {
-      /* FIXME: set the scheduling settings for the new thread */
-      /* sched_thread_setparam (win32_obj_id, attr.schedparam); */
-    }
+
+  /* Per POSIX the new thread inherits the sched priority from its caller
+     thread if PTHREAD_INHERIT_SCHED is set.
+     FIXME: set the priority appropriately for system contention scope */
+  if (attr.inheritsched == PTHREAD_INHERIT_SCHED)
+    attr.schedparam.sched_priority
+	= sched_get_thread_priority (GetCurrentThread ());
+  if (attr.schedparam.sched_priority)
+    sched_set_thread_priority (win32_obj_id, attr.schedparam.sched_priority);
 }
 
 void
@@ -2601,9 +2605,7 @@ pthread_getschedparam (pthread_t thread, int *policy,
   if (!pthread::is_good_object (&thread))
     return ESRCH;
   *policy = SCHED_FIFO;
-  /* we don't return the current effective priority, we return the current
-     requested priority */
-  *param = thread->attr.schedparam;
+  param->sched_priority = sched_get_thread_priority (thread->win32_obj_id);
   return 0;
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]