[metal] Revise NodeManager's implementation due to weak memory order #2008
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It's turned out that #2000's approach didn't really work, due to Metal's weak memory order guarantee...
Problem
Previously, each
pointer
cell stores aNodeManager::ElemIndex::raw_
. This is basically an index in thedata_list
. The index then gets mapped to a chunk in the list, then a slot within that chunk.This design has involved two places that would require atomic operations:
data_list
.If
allocate()
is done in thread A, such thatallocate()
in another thread B sees thatcell
is already valid, due to the relaxed memory order, B'sget()
could still observe invalidaddr
...Solution
Just store the allocated pointer offset (32-bit,
ListManagerData::ReservedElemPtrOffs
) intocell
directly. This avoids the second lookup intodata_list
. Note that to reduce code change,NodeManager::ElemIndex
is just an alias forListManagerData::ReservedElemPtrOffs
now.Related issue = #1740, #1174
[Click here for the format server]