@echo quarry sorry for ping but I'm pretty sure you're the one to confirm this with.
Is setting IEnableableComponent state thread safe?
Looking at the source it appears to be specifically written to be but I just want to confirm if this is supported behaviour before I depend on it.
Specifically I'm looking at this method where I noticed the old bit state is validated to not have changed.
internal void SetComponentEnabled(Chunk* chunk, int indexInChunk, int typeIndexInArchetype, bool value)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
// the bit array size is padded up to 64 bits, so we validate we're not indexing outside the valid data.
if (Hint.Unlikely(indexInChunk < 0 || indexInChunk >= chunk->Capacity))
throw new ArgumentException($"indexInChunk {indexInChunk} is outside the valid range [0..{chunk->Capacity-1}]");
#endif
var bits = ChunkDataUtility.GetEnabledRefRW(chunk, typeIndexInArchetype, out var ptrChunkDisabledCount);
var numStridesIntoBits = (indexInChunk / 64);
var pBits = bits.Ptr + numStridesIntoBits;
var indexInPBits = indexInChunk - (numStridesIntoBits * 64);
var mask = 1L << indexInPBits;
var oldBits = (long)*pBits;
var newBits = 0L;
var expectedOldBits = 0L;
do
{
newBits = math.select(oldBits & ~mask, oldBits | mask, value);
expectedOldBits = oldBits;
oldBits = Interlocked.CompareExchange(ref UnsafeUtility.AsRef<long>(pBits), newBits, expectedOldBits);
} while (expectedOldBits != oldBits);
if (oldBits == newBits)
return;
// do we need increment or decrement?
var adjustment = math.select(1, -1, value);
Interlocked.Add(ref UnsafeUtility.AsRef<int>(ptrChunkDisabledCount), adjustment);
}```