Skip to content

Add BatchSemaphore::upgrade and parking_lot::RwLock::upgrade#258

Merged
sarsko merged 2 commits intomainfrom
upgrade
Apr 11, 2026
Merged

Add BatchSemaphore::upgrade and parking_lot::RwLock::upgrade#258
sarsko merged 2 commits intomainfrom
upgrade

Conversation

@sarsko
Copy link
Copy Markdown
Contributor

@sarsko sarsko commented Mar 27, 2026

Adds upgradeable reads to parking_lot::RwLock


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@sarsko
Copy link
Copy Markdown
Contributor Author

sarsko commented Mar 31, 2026

Code is wrong, ref https://docs.rs/parking_lot/latest/parking_lot/type.RwLock.html#method.upgradable_read:

Locks this rwlock with upgradable read access, blocking the current thread until it can be acquired.

The calling thread will be blocked until there are no more writers **or other upgradable reads** which hold the lock. There may be other readers currently inside the lock when this method returns.

Returns an RAII guard which will release this thread's shared access once it is dropped.

I put **s around the important part, which is the other upgradeable reads part. Credit to this Reddit post: https://www.reddit.com/r/rust/comments/nbrh9v/upgradable_parking_lotrwlock_might_not_do_what/ and this article: https://morestina.net/1739/upgradable-parking_lotrwlock-might-not-be-what-you-expect for pointing this out.

@sarsko
Copy link
Copy Markdown
Contributor Author

sarsko commented Apr 2, 2026

Made it match the semantics of the one from parking_lot (sans the fairness discussed in #259)

Playground I used to check behavior of the parking_lot RwLock:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=cffda8f89a8d2fd2dfe0f2f17fc5012b

@sarsko
Copy link
Copy Markdown
Contributor Author

sarsko commented Apr 2, 2026

There is an alternative design which implements this with only one semaphore by making write map to upgradable_read + upgrade. That causes extra scheduling points for regular write acquires, and also makes it not atomic.

…s. Implement downgrade for RwLockUpgradableReadGuard
@sarsko sarsko merged commit 4b13552 into main Apr 11, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants