Repository navigation
feat(rwlock): add an upgradable readers-writer lock - #366
Conversation
orthur2
left a comment
There was a problem hiding this comment.
Thanks for taking this on. The reservation protocol and the cancellation paths look sound to me.
On the borrowed guards question, I think they’re fine for a first version. The existing owned guards hold an Arc<RwLock>, which we can’t get by projecting an Arc<UpgradableRwLock>, so owned support couldn’t reuse them as they are.
Please also add UpgradableRwLock to the API map in lib.rs and README.
e675886 to
4c117c6
Compare
orthur2
left a comment
There was a problem hiding this comment.
Thanks, looks good to me now.
|
Reviewed One small test issue: the compile-fail example here is missing: use asyncband::rwlock::RwLockUpgradableReadGuard;It currently fails with Local validation:
I also ran benchmarks. These are uncontended release-build results on x86_64 Linux with Rust 1.99.1, pinned to one CPU: medians across 11 rounds, including guard acquisition and release.
Ordinary reads stay about the same, while writes have a clear extra cost. I found no consistent regression in the existing Please fix the doctest and resolve the changelog conflict before merging. Adding benchmarks for the new type would also help track its performance. |
|
@orthur2 i have addressed the maintainer review, can u review the changes |
|
Thanks @Kaileshwar16, the latest changes look good to me, and thanks @PragmaTwice for catching the doctest. I'm merging this now. If anything else comes up, feel free to leave a post-merge review and we can follow up in a separate PR. |
Closes #169.
Add
UpgradableRwLockwith ordinary readers, exclusive writers, and one upgradablereader. Support asynchronous upgrade, guard-preserving
try_upgrade(), and downgrade toan ordinary reader.
Cancelling a pending upgrade releases the consumed guard and its permits. Include rustdoc
examples and deterministic tests for ordering, cancellation, reader limits, cross-thread
visibility, and guard traits.
Design Notes
Reuse the existing RwLock and semaphore implementation. Writers and upgradable readers
acquire a FIFO reservation before requesting data permits, preventing an upgrade from
queuing behind a writer waiting for the upgrader’s read permit.
Ordering is FIFO within each queue, rather than globally: ordinary readers can pass
writers waiting for the reservation. Existing RwLock behavior remains unchanged.
Validation:
cargo x lintandcargo x checkpassed.cargo x test: 625 passed.cargo x miri: 194 passed, 10 existing skips.This is an initial implementation intended to help settle the API design in #169. I'd especially appreciate feedback on the two-queue fairness model, cancellation semantics for a pending upgrade, and whether borrowed guards are sufficient for the first version. I'm happy to adjust the design based on maintainer feedback.