Skip to content

Commit aa56313

Browse files
authored
Avoid thread status race in read-write lock spec (#1127)
1 parent 19d4ece commit aa56313

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

‎spec/concurrent/atomic/reentrant_read_write_lock_spec.rb‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,26 @@ def wait_up_to(secs, &condition)
249249
in_thread { lock.acquire_read_lock; latch1.count_down; latch2.wait; lock.release_read_lock },
250250
in_thread { lock.acquire_read_lock; latch1.count_down; latch2.wait; lock.release_read_lock },
251251
in_thread { lock.acquire_read_lock; latch1.count_down; latch2.wait; lock.release_read_lock },
252-
in_thread { latch1.wait; lock.acquire_write_lock; good.value = true }
252+
in_thread { latch1.wait; lock.with_write_lock { good.value = true } }
253253
]
254-
wait_up_to(0.5) { threads[3].status == 'sleep' }
255-
# The last thread should be waiting to acquire a write lock now...
256-
expect(threads[3].status).to eql "sleep"
257-
expect(threads[3]).not_to hold(lock).for_write
254+
# A failed try can race with the writer updating the counter.
255+
new_readers_blocked = lambda do
256+
2.times.all? do
257+
acquired = lock.try_read_lock
258+
lock.release_read_lock if acquired
259+
Thread.pass
260+
!acquired
261+
end
262+
end
263+
wait_up_to(0.5, &new_readers_blocked)
264+
# New readers should be blocked once a writer is queued...
265+
expect(new_readers_blocked.call).to be true
258266
expect(good.value).to be false
259267
# Throw latch2 and the 3 readers will wake up and all release their read locks...
260268
latch2.count_down
261269
wait_up_to(0.5) { good.value }
262-
expect(threads[3]).to hold(lock).for_write
263270
expect(good.value).to be true
271+
threads.each(&:join)
264272
end
265273
end
266274

0 commit comments

Comments
 (0)