-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add RwLockReadGuard::upgrade method to obtain RwLockWriteGuard #7282
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
Comments
That style of API will deadlock if there are there are ever 2 concurrent tasks trying to upgrade. A special "upgradable read" mode can avoid that: https://docs.rs/parking_lot/latest/parking_lot/type.RwLock.html#method.upgradable_read. |
Tokio's rwlock is implemented using a semaphore. I don't think we can implement upgradable reads with a semaphore, and we're not going to completely change the implementation just for this. |
Hi @sfackler. If you want we can discuss about the API in order to avoid deadlock (I wrote it without any special experience in tokio lib). I don't understand what do you want to say with that link. Can you elaborate more? Thanks @Darksonn for your reply. I understood the complexity of this task and your reply highlight a possible blocker for this implementation. Do you any suggestion how I can implement it? (with tokio or other libraries). |
What happens if two threads currently hold a read lock, and both decide that they want to upgrade to a write lock? |
The upgrade should fail imho. |
Making the upgrade fail would also require tracking new information, and I don't think we should make the |
I think you could implement this with a compare exchange. Since the only time you are allowed to aquire the lock is when no one else is holding it that would mean that the number of readers is 1. You then want to do Compare_exchange(cond,1,-1) If this succeeds then you have unique access and of it fails the error can give back your read lock. |
made a rough draft of how this can look like |
Yes, it's possible to implement |
I see ur point, the waiting mechanism itself is not gonna be an easy change. maybe a seprate struct? maybe in another crate? that does this weird neich thing of letting u upgrade a lock. |
Uh oh!
There was an error while loading. Please reload this page.
Is your feature request related to a problem? Please describe.
I have a procedure that can be split into two parts:
read
permissionwrite
permissionThose operations rely on the same locked data. I cannot guarantee the calculated changes are valid before acquiring the write guard because the thread can be yielded in the middle.
Describe the solution you'd like
I would like to have a way to upgrade the read guard to a write guard, so I can write something like:
In this way, I'm sure the
delta
is valid.Describe alternatives you've considered
I haven't found any solutions
Additional context
The text was updated successfully, but these errors were encountered: