Skip to content

Commit 52e71ad

Browse files
authored
Fix test for mutex starvation as well as small fix in thread_sync.c (ruby#15982)
Don't reset `th->running_time_us` when unlocking from `mutex_free` or force unlocking during thread destruction. Follow-up to 994257a.
1 parent 5d76922 commit 52e71ad

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

test/ruby/test_thread.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,22 +1667,24 @@ def test_mn_threads_sub_millisecond_sleep
16671667

16681668
# [Bug #21840]
16691669
def test_mutex_owner_doesnt_starve_waiters
1670-
assert_ruby_status([], "#{<<~"begin;"}\n#{<<~'end;'}")
1670+
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
16711671
begin;
1672+
require "tempfile"
1673+
temp = Tempfile.new("temp")
16721674
m = Mutex.new
16731675
1674-
fib = lambda { |n|
1676+
def fib(n)
16751677
return n if n <= 1
16761678
fib(n - 1) + fib(n - 2)
1677-
}
1679+
end
16781680
16791681
t1_running = false
1680-
t1 = Thread.new do
1682+
Thread.new do
16811683
t1_running = true
16821684
loop do
16831685
fib(20)
16841686
m.synchronize do
1685-
File.open(__FILE__) { } # reset timeslice due to blocking operation
1687+
File.open(temp.path) { } # reset timeslice due to blocking operation
16861688
end
16871689
end
16881690
end

thread_sync.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
466466
struct sync_waiter *cur = 0, *next;
467467

468468

469-
if (mutex->wait_waking) {
469+
if (mutex->wait_waking && ec_serial) {
470470
uint32_t saved = mutex->saved_running_time_us;
471471
if (th->running_time_us < saved) {
472472
th->running_time_us = saved;

0 commit comments

Comments
 (0)