Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-111964: Add _PyRWMutex a "readers-writer" lock#112859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd61525
gh-111964: Add `_PyRWMutex` a "readers-writer" lock
colesbury c6b9d62
Add comments and ASCII diagram based on review.
colesbury e8419d7
Update comments and add assertions.
colesbury 1658506
Fix typo
colesbury 95f27c3
Remove failing assertion
colesbury 86fb625
Add comment describing state with _Py_HAS_PARKED set
colesbury File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -353,3 +353,109 @@ _PyOnceFlag_CallOnceSlow(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg) | ||
| v = _Py_atomic_load_uint8(&flag->v); | ||
| } | ||
| } | ||
| #define _Py_WRITE_LOCKED 1 | ||
| #define _PyRWMutex_READER_SHIFT 2 | ||
| #define _Py_RWMUTEX_MAX_READERS (UINTPTR_MAX >> _PyRWMutex_READER_SHIFT) | ||
| static uintptr_t | ||
| rwmutex_set_parked_and_wait(_PyRWMutex *rwmutex, uintptr_t bits) | ||
| { | ||
| // Set _Py_HAS_PARKED and wait until we are woken up. | ||
| if ((bits & _Py_HAS_PARKED) == 0) { | ||
| uintptr_t newval = bits | _Py_HAS_PARKED; | ||
| if (!_Py_atomic_compare_exchange_uintptr(&rwmutex->bits, | ||
| &bits, newval)) { | ||
| return bits; | ||
| } | ||
| bits = newval; | ||
| } | ||
| _PyParkingLot_Park(&rwmutex->bits, &bits, sizeof(bits), -1, NULL, 1); | ||
| return _Py_atomic_load_uintptr_relaxed(&rwmutex->bits); | ||
| } | ||
| // The number of readers holding the lock | ||
| static uintptr_t | ||
| rwmutex_reader_count(uintptr_t bits) | ||
| { | ||
| return bits >> _PyRWMutex_READER_SHIFT; | ||
| } | ||
| void | ||
| _PyRWMutex_RLock(_PyRWMutex *rwmutex) | ||
| { | ||
| uintptr_t bits = _Py_atomic_load_uintptr_relaxed(&rwmutex->bits); | ||
| for (;;) { | ||
| if ((bits & _Py_WRITE_LOCKED)) { | ||
| // A writer already holds the lock. | ||
| bits = rwmutex_set_parked_and_wait(rwmutex, bits); | ||
| continue; | ||
| } | ||
| else if ((bits & _Py_HAS_PARKED)) { | ||
| // Reader(s) hold the lock (or just gave up the lock), but there is | ||
| // at least one waiting writer. We can't grab the lock because we | ||
| // don't want to starve the writer. Instead, we park ourselves and | ||
| // wait for the writer to eventually wake us up. | ||
| bits = rwmutex_set_parked_and_wait(rwmutex, bits); | ||
| continue; | ||
| } | ||
| else { | ||
| // The lock is unlocked or read-locked. Try to grab it. | ||
| assert(rwmutex_reader_count(bits) < _Py_RWMUTEX_MAX_READERS); | ||
| uintptr_t newval = bits + (1 << _PyRWMutex_READER_SHIFT); | ||
| if (!_Py_atomic_compare_exchange_uintptr(&rwmutex->bits, | ||
| &bits, newval)) { | ||
| continue; | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| void | ||
| _PyRWMutex_RUnlock(_PyRWMutex *rwmutex) | ||
| { | ||
| uintptr_t bits = _Py_atomic_add_uintptr(&rwmutex->bits, -(1 << _PyRWMutex_READER_SHIFT)); | ||
| assert(rwmutex_reader_count(bits) > 0 && "lock was not read-locked"); | ||
| bits -= (1 << _PyRWMutex_READER_SHIFT); | ||
| if (rwmutex_reader_count(bits) == 0 && (bits & _Py_HAS_PARKED)) { | ||
| _PyParkingLot_UnparkAll(&rwmutex->bits); | ||
| return; | ||
| } | ||
| } | ||
| void | ||
| _PyRWMutex_Lock(_PyRWMutex *rwmutex) | ||
| { | ||
| uintptr_t bits = _Py_atomic_load_uintptr_relaxed(&rwmutex->bits); | ||
| for (;;) { | ||
| // If there are no active readers and it's not already write-locked, | ||
| // then we can grab the lock. | ||
| if ((bits & ~_Py_HAS_PARKED) == 0) { | ||
| if (!_Py_atomic_compare_exchange_uintptr(&rwmutex->bits, | ||
| &bits, | ||
| bits | _Py_WRITE_LOCKED)) { | ||
| continue; | ||
| } | ||
| return; | ||
| } | ||
| // Otherwise, we have to wait. | ||
| bits = rwmutex_set_parked_and_wait(rwmutex, bits); | ||
| } | ||
| } | ||
| void | ||
| _PyRWMutex_Unlock(_PyRWMutex *rwmutex) | ||
| { | ||
| uintptr_t old_bits = _Py_atomic_exchange_uintptr(&rwmutex->bits, 0); | ||
| assert((old_bits & _Py_WRITE_LOCKED) && "lock was not write-locked"); | ||
| assert(rwmutex_reader_count(old_bits) == 0 && "lock was read-locked"); | ||
ericsnowcurrently marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if ((old_bits & _Py_HAS_PARKED) != 0) { | ||
| _PyParkingLot_UnparkAll(&rwmutex->bits); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.