Fix Mutex#unlock with a scheduler and thread contention

* It would hit "[BUG] unexpected THREAD_STOPPED" before.
This commit is contained in:
Benoit Daloze 2020-09-17 15:15:43 +02:00
parent deffb63021
commit 264889ec3d
2 changed files with 36 additions and 13 deletions

View File

@ -47,6 +47,29 @@ class TestFiberMutex < Test::Unit::TestCase
thread.join
end
def test_mutex_thread
mutex = Mutex.new
mutex.lock
thread = Thread.new do
scheduler = Scheduler.new
Thread.current.scheduler = scheduler
Fiber.schedule do
mutex.lock
sleep 0.1
mutex.unlock
end
scheduler.run
end
sleep 0.1
mutex.unlock
thread.join
end
def test_condition_variable
mutex = Mutex.new
condition = ConditionVariable.new

View File

@ -402,8 +402,7 @@ rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_fiber_t *fiber)
if (cur->th->scheduler != Qnil) {
rb_scheduler_unblock(cur->th->scheduler, cur->self, rb_fiberptr_self(cur->fiber));
}
} else {
switch (cur->th->status) {
case THREAD_RUNNABLE: /* from someone else calling Thread#run */
case THREAD_STOPPED_FOREVER: /* likely (rb_mutex_lock) */
@ -417,6 +416,7 @@ rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_fiber_t *fiber)
continue;
}
}
}
found:
while (*th_mutex != mutex) {
th_mutex = &(*th_mutex)->next_mutex;