Given something like this on an ARMv8 CPU (though this may apply to many others as well):
class abcxzy
{
// Pragma align to cacheline to ensure they exist on same line.
unit32_t atomic_data;
uint32_t data;
void foo()
{
volatile asm (
" ldr w0, [address of data]\n"
"# Do stuff with data in w0..."
" str w0, [address of data]\n"
"1: ldaxr w0, [address of atomic_data]\n"
" add w1, w0, #0x1\n"
" stxr w2,w1, [address of atomic_data]\n"
" cbnz w2, 1b\n"
);
}
}
With proper clobbers and such set on the Asm inline so that C and Asm can coexist happily in a world of rainbow ponies and sunshine.
In a multiple CPU situation, all running this code at the same time, will the stores to data
cause the atomic load/store to atomic_data
to fail? From what I've read, the ARM atomic stuff works on a cache line basis, but it is not clear if the non-atomic store will affect the atomic. I hope that it it doesn't (and assume that it does...), but I am looking to see if anyone else can confirm this.