feat: Implement write lock downgrading - #65
Merged
tisonkun merged 2 commits intoAug 30, 2025
Merged
Conversation
orthur2
marked this pull request as draft
August 10, 2025 17:27
orthur2
force-pushed
the
feat/add_downgrade_to_write_guard_in_rwlock
branch
from
August 21, 2025 17:01
c18218b to
b3ab854
Compare
Member
orthur2
force-pushed
the
feat/add_downgrade_to_write_guard_in_rwlock
branch
from
August 22, 2025 16:20
3355ceb to
947cb1d
Compare
orthur2
marked this pull request as ready for review
August 22, 2025 16:22
Contributor
Author
tisonkun
approved these changes
Aug 30, 2025
tisonkun
left a comment
Member
There was a problem hiding this comment.
LGTM. I made some code style changes and this is ready for merging.
Contributor
Author
Thanks for your help. |
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.
Description
This PR introduces a
downgrade()method toRwLockWriteGuardand its variants, addressing Issue #62.Currently, a task holding a write lock must drop it and re-acquire a read lock to allow concurrent reads. This is inefficient and creates a small window for race conditions. The
downgrade()method provides a way to atomically and efficiently convert an exclusive write lock into a shared read lock, improving performance and ensuring safety.Implementation
The
downgrade()method has been added to all fourRwLockWriteGuardvariants:RwLockWriteGuardMappedRwLockWriteGuardOwnedRwLockWriteGuardOwnedMappedRwLockWriteGuardThe implementation atomically converts the lock by releasing
max_readers - 1permits from the underlying semaphore, leaving one permit for the new read guard.ManuallyDropis used to prevent the original write guard'sDropimplementation from running, ensuring panic safety and correct ownership transfer of the lock.To implement
RwLockWriteGuard::downgrade, I'll need to create a newRwLockReadGuard. However, thelockfield ofRwLockReadGuardis private. To avoid changing the access permissions of thelockfield directly (given that changing it topub(super)would grant excessive access, and Rust doesn't offer more fine-grained access control, which is a trade-off point tison and I discussed in PR #61), So, my current plan is to add a dedicated constructor forRwLockReadGuard. I will name itfrom_write_downgrade()instead of genericnew()and constrain its API by requiring theWriteGuardto be passed in. This makes its purpose clear and prevents misuse.