From: KOSAKI Motohiro Date: 2010-05-16T01:50:45+09:00 Subject: [ruby-core:30256] Re: [Feature #3176] Thread#priority= should actually do something > Issue #3176 has been updated by Yusuke Endoh. > > Assigned to set to Koichi Sasada > Target version set to 1.9.x > > Hi, Caleb > > Great. �I glanced over your patch. �I think the biggest change is how > thread waits GVL. �It is quite funny because ko1 recently does the > 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. �I don't know the precise reason. �He said he would answer to > 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 > 0) struct sched_param sp; int policy; int priority = 0 - th->priority;