GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid thread status race in read-write lock spec (#1127) · ruby-concurrency/concurrent-ruby@aa56313

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • spec/concurrent/atomic
    • reentrant_read_write_lock_spec.rb
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎spec/concurrent/atomic/reentrant_read_write_lock_spec.rb‎

Copy file name to clipboardExpand all lines: spec/concurrent/atomic/reentrant_read_write_lock_spec.rb
+14-6Lines 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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.