1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
normal 2018-08-27 17:17:08 +00:00
parent cda13d8c71
commit 943bf37b10

View file

@ -188,15 +188,15 @@ do_gvl_timer(rb_vm_t *vm, rb_thread_t *th)
static rb_hrtime_t abs; static rb_hrtime_t abs;
native_thread_data_t *nd = &th->native_thread_data; native_thread_data_t *nd = &th->native_thread_data;
vm->gvl.timer = th;
/* take over wakeups from UBF_TIMER */ /* take over wakeups from UBF_TIMER */
ubf_timer_disarm(); ubf_timer_disarm();
if (vm->gvl.timer_err == ETIMEDOUT) { if (vm->gvl.timer_err == ETIMEDOUT) {
abs = native_cond_timeout(&nd->cond.gvlq, TIME_QUANTUM_NSEC); 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_err = native_cond_timedwait(&nd->cond.gvlq, &vm->gvl.lock, &abs);
vm->gvl.timer = 0;
ubf_wakeup_all_threads(); ubf_wakeup_all_threads();
ruby_sigchld_handler(vm); 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); RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
} }
else { 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); 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: * thread is contending for GVL:
*/ */
if (vm->gvl.acquired) timer_thread_function(); if (vm->gvl.acquired) timer_thread_function();
vm->gvl.timer = 0;
} }
static void static void
@ -1329,16 +1326,17 @@ ubf_select(void *ptr)
/* /*
* ubf_wakeup_thread() doesn't guarantee to wake up a target thread. * ubf_wakeup_thread() doesn't guarantee to wake up a target thread.
* Therefore, we repeatedly call ubf_wakeup_thread() until 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 * exit from ubf function. We must have a timer to perform this operation.
* this operation. * We use double-checked locking here because this function may be called
* while vm->gvl.lock is held in do_gvl_timer
*/ */
if (vm->gvl.timer != ruby_thread_from_native()) {
rb_native_mutex_lock(&vm->gvl.lock); rb_native_mutex_lock(&vm->gvl.lock);
if (!vm->gvl.timer) { if (!vm->gvl.timer) {
if (!designate_timer_thread(vm)) {
rb_thread_wakeup_timer_thread(-1); rb_thread_wakeup_timer_thread(-1);
} }
}
rb_native_mutex_unlock(&vm->gvl.lock); rb_native_mutex_unlock(&vm->gvl.lock);
}
ubf_wakeup_thread(th); ubf_wakeup_thread(th);
} }