-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add Mutex::unlock #81873
Add Mutex::unlock #81873
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Is this really worth it? An explicit call to drop would leave the small footgun that if there's some other guard acquired after the lock that you want to drop inside the critical section then you have to explicitly call drop() before the explicit unlock. So it's not that much of an ergonomic improvement. If we want a no-worries convenience method wouldn't a scope-based one be more appropriate? Something like |
@the8472 The point of the PR is not ergonomics. Rather it is readability and performance -- readability because FWIW, I agree with you about some sort of
I feel like the value of explicitly demarcating the critical section is being underestimated, but I don't really have any new arguments to make... |
Would it be a bit nicer as a method on |
@tavianator That's how I originally implemented it, but the problem is that MutexGuard implements Deref so that you can transparently call methods of the underlying T. If we add an unlock methed to MutexGuard, it breaks all code where T has a method named unlock. |
The main problem with such a |
@m-ou-se I don't wish to derail this thread, so feel free to point me elsewhere, but I'm curious what is wrong with: impl<T> Mutex<T> {
pub fn with<R, F: Fn(&mut T) -> R>(&self, f: F) -> Result<R, PoisonErr> {
self.lock().map_ok(f)
}
}
/// ...
foo.with(|locked| locked.bar()).unwrap(); This seems roughly like what I actually want to do with a lock usually... |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The problem is that the lock would still be locked after that function returns in the case of a poisoned lock, as the |
|
But the lock should unlock at the end of the with closure anyway. So as the function returns the lock will always be unlocked. The only difference is whether the body executed or was skipped due to poison, which will be handled by the |
A |
Not quite. It would still ensure that other drop types made in the critical section get dropped before the lock is released. And it would also keep the lock lifetime shorter than the outer scope in the happy path. Only in the poison case it could live longer than expected. We could also return an error that drops the guard. That's not exactly ignoring the problem (there's still an error) but the poison will be foisted on next user of the lock. |
Feel free to write an RFC if you believe there's a good solution. But there were too many unresolved questions/problems like this to add that as a regular PR, since it wasn't clear at all how any solution compares to the alternatives. |
For the scope of this PR it would make sense to add some wording that using this method (or |
@m-ou-se Squashed. I had forgotten that the |
Thanks! @bors r+ rollup
Questions like those are exactly why I don't think we can add a function like that in a PR directly, without a good design discussion or RFC first. |
📌 Commit e92e5fd has been approved by |
Rollup of 10 pull requests Successful merges: - rust-lang#79747 (Add explanations and suggestions to `irrefutable_let_patterns` lint) - rust-lang#81496 (name async generators something more human friendly in type error diagnostic) - rust-lang#81873 (Add Mutex::unlock) - rust-lang#82093 (Add tests for Atomic*::fetch_{min,max}) - rust-lang#82238 (ast: Keep expansion status for out-of-line module items) - rust-lang#82245 (Do not ICE when evaluating locals' types of invalid `yield`) - rust-lang#82259 (Fix popping singleton paths in when generating E0433) - rust-lang#82261 (rustdoc: Support argument files) - rust-lang#82274 (libtest: Fix unwrap panic on duplicate TestDesc) - rust-lang#82275 (Update cargo) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Tracking issue: #81872
Discussion: #79434 (comment)
r? @m-ou-se