summaryrefslogtreecommitdiff
path: root/thread_sync.c
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2020-09-20 13:29:24 +0200
committerBenoit Daloze <eregontp@gmail.com>2020-09-20 13:35:39 +0200
commit6987c8997e6cd8f45bbd7ece6582c0024be0cc0f (patch)
treee4c29386daeb1e4179c8b5f180782cd511898b65 /thread_sync.c
parent73a626c0789161911aa5753859c4a81ed2dfd999 (diff)
Remove from waiter in Mutex#lock with ensure when calling rb_scheduler_block()
* Previously this could lead to an invalid waiter entry and then trying to wake up that waiter would result in various issues in rb_mutex_unlock_th().
Diffstat (limited to 'thread_sync.c')
-rw-r--r--thread_sync.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/thread_sync.c b/thread_sync.c
index 94e4d35395..148e6091e6 100644
--- a/thread_sync.c
+++ b/thread_sync.c
@@ -214,18 +214,17 @@ VALUE
rb_mutex_trylock(VALUE self)
{
rb_mutex_t *mutex = mutex_ptr(self);
- VALUE locked = Qfalse;
if (mutex->fiber == 0) {
rb_fiber_t *fiber = GET_EC()->fiber_ptr;
rb_thread_t *th = GET_THREAD();
mutex->fiber = fiber;
- locked = Qtrue;
mutex_locked(th, self);
+ return Qtrue;
}
- return locked;
+ return Qfalse;
}
/*
@@ -246,6 +245,16 @@ mutex_owned_p(rb_fiber_t *fiber, rb_mutex_t *mutex)
}
}
+static VALUE call_rb_scheduler_block(VALUE mutex) {
+ return rb_scheduler_block(rb_thread_current_scheduler(), mutex);
+}
+
+static VALUE remove_from_mutex_lock_waiters(VALUE arg) {
+ struct list_node *node = (struct list_node*)arg;
+ list_del(node);
+ return Qnil;
+}
+
static VALUE
do_mutex_lock(VALUE self, int interruptible_p)
{
@@ -276,9 +285,7 @@ do_mutex_lock(VALUE self, int interruptible_p)
if (scheduler != Qnil) {
list_add_tail(&mutex->waitq, &w.node);
- rb_scheduler_block(scheduler, self);
-
- list_del(&w.node);
+ rb_ensure(call_rb_scheduler_block, self, remove_from_mutex_lock_waiters, (VALUE)&w.node);
if (!mutex->fiber) {
mutex->fiber = fiber;