-
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 some convenience methods for locks. #79434
Conversation
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
The return types of I'm not sure what a good alternative would be, though. Sending the PoisonError into the closure is also not great. Any thoughts about this? |
#[unstable(feature = "guard_unlock", issue = "none")] | ||
pub fn unlock(self) { | ||
drop(self); | ||
} |
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.
MutexGuard
doesn't have any methods right now, because it implements Deref. If the T
already has an .unlock()
, this will make it harder to call that .unlock()
through this MutexGuard. Any existing code calling such an .unlock()
would change meaning with this change.
Other types like Box
solve this by not taking self
in their methods (e.g. Box::into_raw
), and must be called with the more verbose Box::into_raw(mybox)
syntax.
But I think MutexGuard::unlock(guard);
wouldn't be a big improvement over drop(guard);
. :(
What do you think?
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.
Hmm... I had not thought of that. I guess the primary difference in my mind was to make the code a bit self-documenting. It is not obvious to me when I see drop(foo)
that a lock might be unlocked. I'm ok with the more verbose syntax, personally, but if we are going to do that perhaps we could do it like this instead:
impl Mutex {
pub fn unlock(guard: MutexGuard) { drop(guard); }
}
Then, one would use it like
Mutex::unlock(guard);
which seems about as self-documenting as it gets...
I can think of 3 options:
On balance, I think I still prefer the current API with an explicit note in the documentation. This is mostly based on my own experience that often |
@m-ou-se Any thoughts? |
@mark-i-m Thanks for your detailed comment. I'm not sure which of these I'd prefer. All of them feel like they do not fit the current mutex api very well. :( This problem wouldn't exist if mutexes didn't get poisoned. There's been some discussion about adding new locks in std that don't have the concept of poisoning, just like those in parking_lot. For those, I'm not sure what the status of that is, but I've put it on the agenda for the libs team meeting in two days. |
@m-ou-se Any updates? I saw the survey on the blog, and I assumed that was related. |
We've discussed this PR in the libs team meeting a few days ago. In this meeting we agreed:
Feel free to open a tracking issue for |
Add Mutex::unlock Tracking issue: rust-lang#81872 Discussion: rust-lang#79434 (comment) r? `@m-ou-se`
This PR adds three new methods (under two new features) for
Mutex
andMutexGuard
:We add
MutexGuard::unlock
, which is equivalent todrop(guard)
but more self-documenting. This method is useful for explicitly cutting short the duration for which a lock is held. It is often helpful to do this to shorten critical sections or avoid deadlocks.We add
Mutex::with
andMutex::try_with
, which accept closures and run the closures with the lock held, dropping the lock immediately after the closure returns. These methods are useful for running critical sections in a syntactically clear way and avoiding holding the lock for longer than needed.This is my first PR adding a std feature, so please let me know if something is missing.