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

tokio-sync: Add async mutual exclusion primitive #964

Merged
merged 21 commits into from
Apr 18, 2019

Conversation

jonhoo
Copy link
Contributor

@jonhoo jonhoo commented Mar 8, 2019

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.

This replaces #958, which attempted to introduce a less generic version. The primitive proposed there will instead live in async-lease.

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.
tokio-sync/src/lock.rs Outdated Show resolved Hide resolved
tokio-sync/src/lock.rs Outdated Show resolved Hide resolved
@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 8, 2019

@hawkw Thanks! Should both be addressed now.

tokio-sync/src/lock.rs Outdated Show resolved Hide resolved
tokio-sync/src/lock.rs Outdated Show resolved Hide resolved
tokio-sync/src/lock.rs Show resolved Hide resolved
tokio-sync/src/lock.rs Outdated Show resolved Hide resolved
@jonhoo jonhoo requested a review from carllerche March 15, 2019 20:25
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Lgtm thanks.

@carllerche
Copy link
Member

@jonhoo

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 Deref and DerefMut. The main issue would be passing a LockGuard<TcpStream> to something that requires T: AsyncRead + AsyncWrite.

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.

@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 16, 2019

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.

@carllerche
Copy link
Member

@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.

@carllerche
Copy link
Member

@jonhoo let me know if this looks good to you.

@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 21, 2019

@carllerche oh, I think the Stream, Sink, and Future impls should still stick around!

@carllerche
Copy link
Member

@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.

@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 22, 2019

Forwarding Sink, Stream, and Future means that you can now use the guard with combinators that consume self, which is pretty much all of them. You can't do that if the guard doesn't forward (i.e., just through Deref).

@seanmonstar
Copy link
Member

Should someone really use the combinators on a guard? That would mean keeping the lock even though the combinator isn't ready yet.

@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 22, 2019

@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.

@LucioFranco LucioFranco merged commit cf06621 into tokio-rs:master Apr 18, 2019
@jonhoo jonhoo deleted the tokio-sync-lock branch September 19, 2019 17:10
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.

5 participants