Repository navigation
Conversation
…#1129) The lock counter counts reader threads, while the per-thread count counts acquisitions. Comparing the two granted a write lock to a thread that had re-entered its own read lock while another thread still held one, and the other thread's release never woke the upgrading writer.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ReentrantReadWriteLockcounts reader threads in the shared counter and counts re-entrant acquisitions in the per-thread hold count. The write-lock checks compared those two numbers.A thread that acquired its read lock twice, while one other thread also held a read lock, saw both numbers equal 2 and was granted the write lock.
try_write_lockreturnedtrue. On current master, before this change:acquire_write_lockwaits for the same comparison.release_read_locksignaled a waiting writer only when the reader count hit 0. An upgrading thread still holds its own read lock, so the count stops at 1 and the upgrader is never woken. Later readers and writers then block behind it.Fix
A thread holds every current read lock only when it is the sole reader: the reader count is 1 if this thread holds a read lock, and 0 if it does not. Re-entrant acquisitions no longer count as other readers.
Releasing a read lock also signals a waiting writer when one reader remains, so an upgrading writer is woken once the other reader lets go. A writer that holds no read lock wakes, sees the remaining reader, and waits again; the last release still signals it.
After the change the same script prints
falseboth times, and once the other reader releases the upgrade succeeds. A blocked upgrade now finishes, and a new reader can acquire the lock afterward:spec/concurrent/atomic/reentrant_read_write_lock_spec.rb: 34 examples, 0 failures.Fixes #1129
Fixes #1130