Split flags
in struct MVMCollectable to avoid data races when setting MVM_CF_HAS_OBJECT_ID
and MVM_CF_REF_FROM_GEN2
#1336
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.
"paranoia" checks added as part of work on the new hash tables reveal that there is a nasty data race when setting flag bits in
MVMCollectable
Specifically, if one thread calls
MVM_gc_object_id
for an object that does not yet have an ID, and another thread callsMVM_gc_write_barrier_hit_by
for the same object, then each function needs to perform a read, modify, write on the object's flags. If the data race is hit, then the setting of one of the flags bits is lost. IfMVM_CF_HAS_OBJECT_ID
fails to be set, then we simply leak a little memory in the gen2 pools. IfMVM_CF_REF_FROM_GEN2
fails to be set, then the GC doesn't function properly.A solution to this is to split the flags from a single
MVMuint16
into twoMVMuint8
s and partition the flags bits across the two, so that these two hot flags are not in the same byte. This way, each can be updated without causing a data race with the other. The proposed split turns out to be quite easily memorable - other thanMVM_CF_HAS_OBJECT_ID
, all "gen2", "nursery" and GC related flags go intoflags2
, all others intoflags1
.Unfortunately rakudo's extension ops have an unhealthy amount of tight coupling with MoarVM, including (ab)using a free bit in the flags for
FIRST
handling. Hence rakudo needs to be changed before it can build with these changes. The proposed patch to rakudo makes it build with both MoarVMmaster
and this branchSuggested actions
(intersperse with testing as necessary)(as expected, the automatic tests here fail currently, because Rakudo has not been patched)
Problem found by @dogbert17, cause diagnosed by @niner