-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
tokio-sync: Add async mutual exclusion primitive #964
Conversation
This PR introduces `Lock`: A concurrency primitive built on top of `Semaphore` that provides a `Mutex`-like primitive that interacts nicely with futures. Specifically, `LockGuard` (in contrast to `MutexGuard`) does _not_ borrow the `Lock`, and can thus be passed into a future where it will later be unlocked.
@hawkw Thanks! Should both be addressed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm thanks.
Given what it takes to make the feature flags work, do you think it is worth forwarding the read / write impls? They are usable via I believe @seanmonstar is wondering if the forwards are worth it, but he is not a strong -1 on it, so it is up to you. |
It would make the example a little nicer I think, but I don't know how common the pattern is in practice. I'd be okay getting rid of them. |
@jonhoo Ok, just in the spirit of getting something out, I will opt for the conservative option and remove the impls + feature flag junk. If it turns out to be annoying, we can always bring it back later. |
@jonhoo let me know if this looks good to you. |
@carllerche oh, I think the |
@jonhoo do you have a “real” example where they are needed? I’m most hesitant because the std version does not forward. We also can always add it later once there is a clear example in support. |
Forwarding |
Should someone really use the combinators on a guard? That would mean keeping the lock even though the combinator isn't ready yet. |
@seanmonstar I think that's totally reasonable. I'm specifically imagining a case like sharing a single-threaded connection-like object to send a single object. You take the lock, use the send combinator, and then release the lock. |
This PR introduces
Lock
: A concurrency primitive built on top ofSemaphore
that provides aMutex
-like primitive that interacts nicely with futures. Specifically,LockGuard
(in contrast toMutexGuard
) does not borrow theLock
, and can thus be passed into a future where it will later be unlocked.This replaces #958, which attempted to introduce a less generic version. The primitive proposed there will instead live in
async-lease
.