ruby-changes:51031
From: normal <ko1@a...>
Date: Sun, 22 Apr 2018 21:09:13 +0900 (JST)
Subject: [ruby-changes:51031] normal:r63238 (trunk): thread*: all condvars are monotonic
normal 2018-04-22 21:09:07 +0900 (Sun, 22 Apr 2018) New Revision: 63238 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63238 Log: thread*: all condvars are monotonic There's no reason to use CLOCK_REALTIME for any condvars in Ruby. Indeed, we initialized all condvars with RB_CONDATTR_CLOCK_MONOTONIC anyway; so simplify our code and reduce ifdefs. [ruby-core:85639] [Misc #14497] Modified files: trunk/mjit.c trunk/thread_pthread.c trunk/thread_pthread.h trunk/thread_win32.c Index: mjit.c =================================================================== --- mjit.c (revision 63237) +++ mjit.c (revision 63238) @@ -101,7 +101,7 @@ extern void rb_native_mutex_unlock(rb_na https://github.com/ruby/ruby/blob/trunk/mjit.c#L101 extern void rb_native_mutex_initialize(rb_nativethread_lock_t *lock); extern void rb_native_mutex_destroy(rb_nativethread_lock_t *lock); -extern void rb_native_cond_initialize(rb_nativethread_cond_t *cond, int flags); +extern void rb_native_cond_initialize(rb_nativethread_cond_t *cond); extern void rb_native_cond_destroy(rb_nativethread_cond_t *cond); extern void rb_native_cond_signal(rb_nativethread_cond_t *cond); extern void rb_native_cond_broadcast(rb_nativethread_cond_t *cond); @@ -1370,10 +1370,10 @@ mjit_init(struct mjit_options *opts) https://github.com/ruby/ruby/blob/trunk/mjit.c#L1370 /* Initialize mutex */ rb_native_mutex_initialize(&mjit_engine_mutex); - rb_native_cond_initialize(&mjit_pch_wakeup, RB_CONDATTR_CLOCK_MONOTONIC); - rb_native_cond_initialize(&mjit_client_wakeup, RB_CONDATTR_CLOCK_MONOTONIC); - rb_native_cond_initialize(&mjit_worker_wakeup, RB_CONDATTR_CLOCK_MONOTONIC); - rb_native_cond_initialize(&mjit_gc_wakeup, RB_CONDATTR_CLOCK_MONOTONIC); + rb_native_cond_initialize(&mjit_pch_wakeup); + rb_native_cond_initialize(&mjit_client_wakeup); + rb_native_cond_initialize(&mjit_worker_wakeup); + rb_native_cond_initialize(&mjit_gc_wakeup); /* Initialize class_serials cache for compilation */ valid_class_serials = rb_hash_new(); Index: thread_pthread.c =================================================================== --- thread_pthread.c (revision 63237) +++ thread_pthread.c (revision 63238) @@ -43,7 +43,7 @@ void rb_native_mutex_destroy(rb_nativeth https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L43 void rb_native_cond_signal(rb_nativethread_cond_t *cond); void rb_native_cond_broadcast(rb_nativethread_cond_t *cond); void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex); -void rb_native_cond_initialize(rb_nativethread_cond_t *cond, int flags); +void rb_native_cond_initialize(rb_nativethread_cond_t *cond); void rb_native_cond_destroy(rb_nativethread_cond_t *cond); static void rb_thread_wakeup_timer_thread_low(void); static struct { @@ -52,14 +52,13 @@ static struct { https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L52 } timer_thread; #define TIMER_THREAD_CREATED_P() (timer_thread.created != 0) -#define RB_CONDATTR_CLOCK_MONOTONIC 1 - -#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCKID_T) && \ +#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && \ defined(CLOCK_REALTIME) && defined(CLOCK_MONOTONIC) && \ defined(HAVE_CLOCK_GETTIME) -#define USE_MONOTONIC_COND 1 +static pthread_condattr_t condattr_mono; +static pthread_condattr_t *const condattr_monotonic = &condattr_mono; #else -#define USE_MONOTONIC_COND 0 +static const void *condattr_monotonic; #endif #if defined(HAVE_POLL) && defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) @@ -161,9 +160,9 @@ static void https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L160 gvl_init(rb_vm_t *vm) { rb_native_mutex_initialize(&vm->gvl.lock); - rb_native_cond_initialize(&vm->gvl.cond, RB_CONDATTR_CLOCK_MONOTONIC); - rb_native_cond_initialize(&vm->gvl.switch_cond, RB_CONDATTR_CLOCK_MONOTONIC); - rb_native_cond_initialize(&vm->gvl.switch_wait_cond, RB_CONDATTR_CLOCK_MONOTONIC); + rb_native_cond_initialize(&vm->gvl.cond); + rb_native_cond_initialize(&vm->gvl.switch_cond); + rb_native_cond_initialize(&vm->gvl.switch_wait_cond); vm->gvl.acquired = 0; vm->gvl.waiting = 0; vm->gvl.need_yield = 0; @@ -262,38 +261,18 @@ rb_native_mutex_destroy(pthread_mutex_t https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L261 } void -rb_native_cond_initialize(rb_nativethread_cond_t *cond, int flags) +rb_native_cond_initialize(rb_nativethread_cond_t *cond) { - int r; -# if USE_MONOTONIC_COND - pthread_condattr_t attr; - - pthread_condattr_init(&attr); - - cond->clockid = CLOCK_REALTIME; - if (flags & RB_CONDATTR_CLOCK_MONOTONIC) { - r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); - if (r == 0) { - cond->clockid = CLOCK_MONOTONIC; - } - } - - r = pthread_cond_init(&cond->cond, &attr); - pthread_condattr_destroy(&attr); -# else - r = pthread_cond_init(&cond->cond, NULL); -# endif + int r = pthread_cond_init(cond, condattr_monotonic); if (r != 0) { rb_bug_errno("pthread_cond_init", r); } - - return; } void rb_native_cond_destroy(rb_nativethread_cond_t *cond) { - int r = pthread_cond_destroy(&cond->cond); + int r = pthread_cond_destroy(cond); if (r != 0) { rb_bug_errno("pthread_cond_destroy", r); } @@ -314,7 +293,7 @@ rb_native_cond_signal(rb_nativethread_co https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L293 { int r; do { - r = pthread_cond_signal(&cond->cond); + r = pthread_cond_signal(cond); } while (r == EAGAIN); if (r != 0) { rb_bug_errno("pthread_cond_signal", r); @@ -326,7 +305,7 @@ rb_native_cond_broadcast(rb_nativethread https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L305 { int r; do { - r = pthread_cond_broadcast(&cond->cond); + r = pthread_cond_broadcast(cond); } while (r == EAGAIN); if (r != 0) { rb_bug_errno("rb_native_cond_broadcast", r); @@ -336,7 +315,7 @@ rb_native_cond_broadcast(rb_nativethread https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L315 void rb_native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex) { - int r = pthread_cond_wait(&cond->cond, mutex); + int r = pthread_cond_wait(cond, mutex); if (r != 0) { rb_bug_errno("pthread_cond_wait", r); } @@ -354,7 +333,7 @@ native_cond_timedwait(rb_nativethread_co https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L333 * Let's hide it from arch generic code. */ do { - r = pthread_cond_timedwait(&cond->cond, mutex, ts); + r = pthread_cond_timedwait(cond, mutex, ts); } while (r == EINTR); if (r != 0 && r != ETIMEDOUT) { @@ -369,20 +348,12 @@ native_cond_timeout(rb_nativethread_cond https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L348 { struct timespec abs; -#if USE_MONOTONIC_COND - if (cond->clockid == CLOCK_MONOTONIC) { - getclockofday(&abs); - goto out; + if (condattr_monotonic) { + getclockofday(&abs); + } + else { + rb_timespec_now(&abs); } - - if (cond->clockid != CLOCK_REALTIME) - rb_bug("unsupported clockid %"PRIdVALUE, (SIGNED_VALUE)cond->clockid); -#endif - rb_timespec_now(&abs); - -#if USE_MONOTONIC_COND - out: -#endif timespec_add(&abs, &timeout_rel); return abs; @@ -426,6 +397,12 @@ static void native_thread_init(rb_thread https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L397 void Init_native_thread(rb_thread_t *th) { +#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) + if (condattr_monotonic) { + int r = pthread_condattr_setclock(condattr_monotonic, CLOCK_MONOTONIC); + if (r) rb_bug_errno("pthread_condattr_setclock", r); + } +#endif pthread_key_create(&ruby_native_thread_key, NULL); th->thread_id = pthread_self(); fill_thread_id_str(th); @@ -444,7 +421,7 @@ native_thread_init(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L421 #ifdef USE_UBF_LIST list_node_init(&nd->ubf_list); #endif - rb_native_cond_initialize(&nd->sleep_cond, RB_CONDATTR_CLOCK_MONOTONIC); + rb_native_cond_initialize(&nd->sleep_cond); ruby_thread_set_native(th); } @@ -899,7 +876,7 @@ register_cached_thread_and_wait(rb_nativ https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L876 struct timespec end = { 60, 0 }; struct cached_thread_entry entry; - rb_native_cond_initialize(&entry.cond, RB_CONDATTR_CLOCK_MONOTONIC); + rb_native_cond_initialize(&entry.cond); entry.th = NULL; entry.thread_id = thread_self_id; end = native_cond_timeout(&entry.cond, end); @@ -1504,7 +1481,7 @@ thread_timer(void *p) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1481 #if !USE_SLEEPY_TIMER_THREAD rb_native_mutex_initialize(&timer_thread_lock); - rb_native_cond_initialize(&timer_thread_cond, RB_CONDATTR_CLOCK_MONOTONIC); + rb_native_cond_initialize(&timer_thread_cond); rb_native_mutex_lock(&timer_thread_lock); #endif while (system_working > 0) { Index: thread_pthread.h =================================================================== --- thread_pthread.h (revision 63237) +++ thread_pthread.h (revision 63238) @@ -16,14 +16,9 @@ https://github.com/ruby/ruby/blob/trunk/thread_pthread.h#L16 #endif #define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER -#define RB_NATIVETHREAD_COND_INIT { PTHREAD_COND_INITIALIZER, } +#define RB_NATIVETHREAD_COND_INIT PTHREAD_COND_INITIALIZER -typedef struct rb_thread_cond_struct { - pthread_cond_t cond; -#ifdef HAVE_CLOCKID_T - clockid_t clockid; -#endif -} rb_nativethread_cond_t; +typedef pthread_cond_t rb_nativethread_cond_t; typedef struct native_thread_data_struct { struct list_node ubf_list; Index: thread_win32.c =================================================================== --- thread_win32.c (revision 63237) +++ thread_win32.c (revision 63238) @@ -508,7 +508,7 @@ native_cond_timeout(rb_nativethread_cond https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L508 #endif void -rb_native_cond_initialize(rb_nativethread_cond_t *cond, int flags) +rb_native_cond_initialize(rb_nativethread_cond_t *cond) { cond->next = (struct cond_event_entry *)cond; cond->prev = (struct cond_event_entry *)cond; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/