I have a basic struct consisting of only atomics:
#[repr(C)]
struct Chunk {
size: AtomicU32,
flags: AtomicU32,
prev: AtomicPtr<Chunk>
}
At some point I need to create structs like this in raw memory.
Example:
(p as *mut Chunk).write(Chunk {
size: AtomicU32::new((size + size_of::<Chunk>()) as u32),
flags: AtomicU32::new(0),
prev: AtomicPtr::default() });
I have a lock for the memory section so writing is fine but I wondered if the write could end up in the cache and the propagation to the other caches happens then at a “random” time, potentially after releasing the lock. Or is this solved by the struct members being atomic and all loads with Ordering::Acquire
are already syncing the memory?
(The lock is just an atomicbool
; I manually implement locking using compare_exchange
to “acquire the lock”. Editor’s note: and hopefully a release
store to unlock, to make a spinlock.)
What is a potential way to avoid that without the use of a memory fence and without using most libraries (even simple ones / inbuilt ones are mostly of limit, as I have no allocator available so everything which uses the heap just crashes).
Should I create the struct in memory and then initialize each member with an atomic store?
I am open to most suggestions and critics regarding my memory handling.
12