Add some convenience methods for locks. by mark-i-m · Pull Request #79434 · rust-lang/rust (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation14 Commits2 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

mark-i-m

This PR adds three new methods (under two new features) for Mutex and MutexGuard:

This is my first PR adding a std feature, so please let me know if something is missing.

@mark-i-m

@rust-highfive

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@mark-i-m

bugadani

@m-ou-se

The return types of with and try_with feel a bit odd to me. In case the mutex was poisoned, the guard is not dropped right after the call, but returned inside the PoisonError instead, which is very different than the non-poison behaviour where it drops it right after calling the closure.

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?

m-ou-se

Comment on lines +518 to +521

#[unstable(feature = "guard_unlock", issue = "none")]
pub fn unlock(self) {
drop(self);
}

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?

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

which seems about as self-documenting as it gets...

@mark-i-m

The return types of with and try_with feel a bit odd to me. In case the mutex was poisoned, the guard is not dropped right after the call, but returned inside the PoisonError instead, which is very different than the non-poison behavior where it drops it right after calling the closure.

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 PoisonError just result in a panic somewhere, and I think those who care would likely be more careful anyway. But I think the second option could also be reasonable.

@mark-i-m

@m-ou-se

@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, with and try_with would have a more straightforward interface.

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.

@mark-i-m

@m-ou-se Any updates? I saw the survey on the blog, and I assumed that was related.

@m-ou-se

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 Mutex::unlock. It'd be good to add a note about about RwLock::unlock() (which would be a bit more complicated) to the unresovled questions on that issue.

@m-ou-se m-ou-se added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Jan 24, 2021

This was referenced

Feb 8, 2021

@mark-i-m

Sorry! I totally didn't see your response. I opened #81872 and #81873

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request

Feb 19, 2021

@Dylan-DPC

Labels

A-concurrency

Area: Concurrency

S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

T-libs-api

Relevant to the library API team, which will review and decide on the PR/issue.