-
-
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
Added map associated function to MutexGuard that uses new MappedMutexGuard type #2472
Conversation
What is the status on this? |
Thanks @carllerche! I have addressed @hawkw's comment and merged This is ready for review again. 🎉 |
Heyo. So in case we want to support a similar API as For a quick comparison:
It would be nice to make sure that if we want to bring these methods into Tokio's lock API, we don't encounter similar issues, which is a consideration I've raised now in #2445 as well. |
@udoprog could you elaborate on why the APIs differ between the two? |
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.
This looks correct to me, and not having a separate type for MappedMutexGuard
looks nice. However, if @udoprog has additional context around the rationale behind having a separate type in parking_lot
, I'd like to wait to hear about that before we merge this PR in its current state.
CI failures appear to be network timeouts while updating dependencies, not test failures. I restarted them. |
Yeah. I just haven't had time to do it yet. If someone else beats me to reviewing this part of |
Well. So far the issue seems to be that Maybe @Amanieu could provide some insight into what the potential soundness issue is if they have the time? |
The main soundness issue is that a This can happen in several cases, which parking_lot supports only for
|
@Amanieu That makes sense. Thank you! With that in mind, I think it would be prudent to keep the separate guards so that we can mimic those APIs in the future without a major break. @carllerche What do you say? |
We should also consider whether or not it's possible and makes sense for Tokio's async mutex to provide all the same methods as |
What are the use cases for mapping a mutex guard? I don't think I've ever used it. |
The main use case is for getting a subfield of a value guarded by a mutex. See the documentation of |
@sunjay i understand this, but when would one want it guarded by the mutex vs. |
Mapped guards are generally used when you are returning a guard as part of your public API, but you only want to expose a subset of your internal state to the user. |
Or, conversely, I imagine, when a function takes a |
Is this waiting for #2445 to merge, or what is the status? |
This PR doesn't depend on any of the changes from #2445, so it can be merged as soon as a decision is made about whether we want a separate type for the mapped mutex guard. I'm guessing that we will choose not to have one based on #2445 (comment), but I defer that to the team members who know more about all of this than I do. :) I'll rebase the PR shortly. |
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.
Overall, this looks good to me, modulo a couple of very minor nits.
I still want to get @carllerche's final opinions on the overall direction of MappedXGuard
changes.
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.
Apologies for the spam. I'm away from my computer so I'm fixing the build via GitHub suggestions from my phone.
I would like to remind people that this exists. |
@sunjay Are you interested in getting this PR finished up? |
Sure, though I believe it is still waiting on an opinion from @carllerche |
I will do a full review later today, and try to get Carl to look as well. (but that probably wont happen until monday I'm guessing) |
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.
This seems ok to me. We already have these methods for RwLock
, and it follows the same design here.
There's a rustfmt failure, and you should probably merge in master to get the up-to-date CI config. |
Looking more closely at this, I think we should have separate non-mapped and mapped guards, since if we don't, we can never implement an async |
The first commit on my PR has separate mapped guards. Should I go back to that version? Happy to do whatever you think is correct here. |
Yeah, I think that having separate guard types is safer in the light of being able to add new features in the future. |
Were you going to go back to the other version? |
Yes, sorry. Haven't had a chance to do so yet. |
@Darksonn sorry for the delay. I've gone back to the version with a separate |
…roduced `MappedMutexGuard` type
The history seems like you added back the commit that merged them, but the diff doesn't have it..? I'm very confused. Force pushes always make the history so confusing. |
Instead of reverting back to the first commit and losing the history, I just added a commit on top of everything else with the separate mapped muted guard type. So all the commits there right now are correct. |
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.
Thanks.
…exGuard MappedOwnedMutexGuard had an (as of yet unused) undefined behaviour case in its implementation of map_mut. This has been superceded by tokio's tokio-rs/tokio#2472 which used a better implementation without undefined behaviour (using UnsafeCell instead).
Part of #2471, extends the work of #2445. Both this PR and #2445 together close #2471.
This PR introduces the
MappedMutexGuard
type and adds amap
associated function toMutexGuard
. The work here is largely based on #2445, so thanks @udoprog for the great PR. This was very easy to implement using your work as a reference.MappedMutexGuard
works almost exactly the same asparking_lot::MappedMutexGuard
, but adapted to the internals oftokio::sync::Mutex
. TheMappedMutexGuard
type stores a reference to the semaphore from the originalMutex
as well as a raw pointer*mut T
that is the result of calling the function passed tomap
. TheMutex
does not hold a mutable reference to its data (it uses anUnsafeCell
), so I do not believe that it is possible to accidentally run into any aliasing issues.I added documentation based on the work in #2445 and the documentation in
parking_lot
/lock_api
. There are doctests in bothmap
andtry_map
that are almost exactly the same as the ones in #2445 forRwLockWriteGuard
. I generated the docs withcargo doc
and everything looks great. 🎉