aboutsummaryrefslogtreecommitdiffstats
path: root/thread_pthread.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-27 17:17:08 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-27 17:17:08 +0000
commit943bf37b10599d585f368c1f8d2e738b6ed8e98c (patch)
treed6a7cd494762fb41cf8531dcfea8d92736aec4bd /thread_pthread.c
parentcda13d8c71100dc50e1084daa7859e5430f8726a (diff)
downloadruby-943bf37b10599d585f368c1f8d2e738b6ed8e98c.tar.gz
thread_pthread.c: avoid lock ping-pong in do_gvl_timer & ubf_select
This simplifies the locking logic somewhat. While we're at it, designate_timer_thread is worthless in ubf_select because gvl_acquire_common already guarantees there is a gvl.timer if gvl->waitq is populated. In the future (for auto-fiber), this will allow using th->unblock.func for rb_waitpid callers (via rb_sigchld_handler). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/thread_pthread.c b/thread_pthread.c
index a12619f92a..ef3cecb0e2 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -188,15 +188,15 @@ do_gvl_timer(rb_vm_t *vm, rb_thread_t *th)
static rb_hrtime_t abs;
native_thread_data_t *nd = &th->native_thread_data;
+ vm->gvl.timer = th;
+
/* take over wakeups from UBF_TIMER */
ubf_timer_disarm();
if (vm->gvl.timer_err == ETIMEDOUT) {
abs = native_cond_timeout(&nd->cond.gvlq, TIME_QUANTUM_NSEC);
}
- vm->gvl.timer = th;
vm->gvl.timer_err = native_cond_timedwait(&nd->cond.gvlq, &vm->gvl.lock, &abs);
- vm->gvl.timer = 0;
ubf_wakeup_all_threads();
ruby_sigchld_handler(vm);
@@ -205,11 +205,7 @@ do_gvl_timer(rb_vm_t *vm, rb_thread_t *th)
RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
}
else {
- /* unlock is needed because threadptr_trap_interrupt may
- * call ubf_select which also acquires vm->gvl.lock */
- rb_native_mutex_unlock(&vm->gvl.lock);
threadptr_trap_interrupt(vm->main_thread);
- rb_native_mutex_lock(&vm->gvl.lock);
}
}
@@ -218,6 +214,7 @@ do_gvl_timer(rb_vm_t *vm, rb_thread_t *th)
* thread is contending for GVL:
*/
if (vm->gvl.acquired) timer_thread_function();
+ vm->gvl.timer = 0;
}
static void
@@ -1329,16 +1326,17 @@ ubf_select(void *ptr)
/*
* ubf_wakeup_thread() doesn't guarantee to wake up a target thread.
* Therefore, we repeatedly call ubf_wakeup_thread() until a target thread
- * exit from ubf function. We must designate a timer-thread to perform
- * this operation.
+ * exit from ubf function. We must have a timer to perform this operation.
+ * We use double-checked locking here because this function may be called
+ * while vm->gvl.lock is held in do_gvl_timer
*/
- rb_native_mutex_lock(&vm->gvl.lock);
- if (!vm->gvl.timer) {
- if (!designate_timer_thread(vm)) {
+ if (vm->gvl.timer != ruby_thread_from_native()) {
+ rb_native_mutex_lock(&vm->gvl.lock);
+ if (!vm->gvl.timer) {
rb_thread_wakeup_timer_thread(-1);
}
+ rb_native_mutex_unlock(&vm->gvl.lock);
}
- rb_native_mutex_unlock(&vm->gvl.lock);
ubf_wakeup_thread(th);
}