-
Notifications
You must be signed in to change notification settings - Fork 48
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
["RFC"] convenient, race-free mutability #41
Comments
This seems similar to the Entry API mentioned in #12. The Java code has a concept called With regard to rewriting the current function implementations, I'd say this is not such a big issue since such case distinctions will have to be added anyways for things like #13. Overall I agree that your proposed solution is preferable over the "easy" one, in particular if we manage to use it for something like an Entry API as well. |
I'm a little strapped for time these days, so won't have the time to give this quite the time it deserves, but my first instinct is that it seems unfortunate to hold the lock for the entire bin just to get mutable access to one value. Though admittedly, that is also what the proposed My second instinct is that it's not clear how we can ever give out |
We won't need to, since every item in the bin is itself a
We can't, so we need to clone the contents into our
I'll look into that |
Currently |
Hmm, you're right, requiring |
Yep. I like the idea of being able to only lock the single bin entry for the Entry API. In order to get the entry API working similar to Java implementation I was going to introduce |
Abstract
As I see it we have no built in support for mutating data in a race-free way, this "RFC" will look into how we could implement this.
The Problem
Say a user wants to mutate some data in the list. Currently they need to use some kind of lock around their data (
HashMap<K, Mutex<V>>
||HashMap<K, RwLock<V>>
). The downside to this it that it incurs overhead, even if no concurrent accesses are made.Now a "naive" solution would be this:
However this basically the definition of a race condition, since while we modify
item
another thread could overwrite the data in the map thus making ourclone
of it stale.The "easy" solution:
The easiest way to implement this would be to expose a
compare_and_set
orcompare_and_swap
function. This would alow users to make sure, the data they got, mutated and that they are now planning to store is still what it was, when they first read it.pro
con
cas
operations.Proposed solution
I propose we add a variant to
BinEntry
:Mutating((std::sync::Condvar, Node<K, V>))
Which is basically the same as
BinEntry::Node
with an additional Condvar, which is notified once the write has finished.This would allow us to have a
get_mut
function which returns aWriteGuard
(we'll have to use guards anyways, see #16).This function finds the BinEntry for the key (if it exists),
clone
s it's contents and replaces it with aBinEntry::Mutating
(containing the originalNode
). All immutable reads can (/will have to be able to) just read the node data, butget_mut
can wait on theCondvar
for the ongoing read to be done and onlyclone
the node data again once it has finished.The
WriteGuard
would have to:Node
/V
, mutably (deref
to it) and allow mutating it.drop
, which does the following:BinEntry::Mutating
aBinEntry::Node
again.notify_all
on theCondvar
pro
Condvar
when writing concurrently to the same entry.con
BinEntry::Node
andBinEntry::Moved
(or onlyBinEntry::Node
as a >1st item of the LL)The text was updated successfully, but these errors were encountered: