Skip to content
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

Write guard to read guard downgrading for sync::RwLock #2733

Merged
merged 3 commits into from
Sep 11, 2020

Conversation

max-heller
Copy link
Contributor

Motivation

The asynchronous sync::RwLock currently provides no way to atomically downgrade a write guard into a read guard, forcing a pattern of dropping the write guard and reacquiring a read guard to achieve a downgrade. In addition to being non-atomic, this makes it easy to accidentally induce a deadlock by forgetting to drop an existing read guard before attempting to acquire a write guard--this is particularly tricky and non-obvious when shadowing the name of the old read guard.

Solution

This implements an atomic downgrade operation by taking ownership of a write guard, releasing all but one of the permits it holds, and returning a read guard holding the remaining permit.

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync labels Jul 31, 2020
@Darksonn Darksonn requested a review from hawkw July 31, 2020 20:09
Comment on lines +107 to +349
/// Atomically downgrades a write lock into a read lock without allowing
/// any writers to take exclusive access of the lock in the meantime.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, because Tokio's locks are fairly queued, if the next queued waiter is a writer, no additional waiters will be allowed access. I'm not sure if that behavior is actually an issue, or not, but it's worth noting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same behavior as if the holder of the (to be downgraded) write guard had initially acquired a read guard instead. It might not always be ideal, but it's the behavior required to avoid starving writers. I'll add a note.

tokio/src/sync/rwlock.rs Outdated Show resolved Hide resolved
@max-heller max-heller requested a review from hawkw August 3, 2020 21:15
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me — @carllerche may want to sign off, since this adds a new public API function.

@Darksonn
Copy link
Contributor

Darksonn commented Sep 9, 2020

This has conflicted with #2615, #2445. Sorry about that. I will try to get Carl's attention.

@carllerche
Copy link
Member

@hawkw I'm OK w/ this 👍 merge whenever it is ready.

@max-heller
Copy link
Contributor Author

@hawkw rebased onto master and fixed things up

@Darksonn Darksonn merged commit c5a9ede into tokio-rs:master Sep 11, 2020
@max-heller max-heller deleted the rwlock-downgrade branch November 16, 2020 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants