> Issue #3176 has been updated by Yusuke Endoh.
>
> Assigned to set to Koichi Sasada
> Target version set to 1.9.x
>
> Hi, Caleb
>
> Great.          > thread waits GVL.     > same to fix another issue (thread starvation on many core environment).
> (The fix have not been committed yet.)
> I had concerned its performance cost, but if ko1 agrees with it, I
> also agree.
>
> However, ko1 seems to still dislike the priority support for some
> reason.        
> this ticket, so please wait for him.
>
>
> Anyway, thank you for your writing a patch.
>
> One comment for the patch: not-static functions (like pqueue_*) should
> prefix "rb_" to avoid conflict with symbols of other projects, even if
> they are just for internal.

Alternative patch is here. I mean we should use os's thread priority
interface if possible.

The test result of caleb's testcase is,

    ./ruby projects/thrprio/thrprio.rb
    315662770 209679 0.9986723818385312 true

Hmm...
Current MRI internal seems to have too many yield() and sleep(). Linux
setpriority()
give a thread to 1.25 times timeslice bonus per a nice. but this test  but now
t1 got about 1500 times bonus against t2. ;-)


diff --git a/thread_pthread.c b/thread_pthread.c
index e6295db..dd66c40 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -535,7 +535,23 @@ native_thread_join(pthread_t th)
 static void
 native_thread_apply_priority(rb_thread_t *th)
 {
-#if defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING > 0)
+#ifdef linux
+    int priority = 0 - th->priority;
+
+    if (priority > 19)
+           priority = 19;
+
+    /*
+     * No privileged can't use <0 priority. But we don't need care it.
+     * Setpriority() naturally ignore such call.
+     */
+    if (priority < -20)
+           priority = -20;
+
+    /* Strangely, Linux's setpriority(PRIO_PROCESS) change per-thread
priority. */
+    setpriority(PRIO_PROCESS, 0, priority);
+    return;
+#elif defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING >)
     struct sched_param sp;
     int policy;
     int priority = 0 - th->priority;