GCC implements __sync_val_compare_and_swap
on PowerPC[64] as:
sync
1: lwarx 9,0,3
cmpw 0,9,4
bne 0,2f
stwcx. 5,0,3
bne 0,1b
2: isync
GCC documents for the __sync_*
builtins:
In most cases, these builtins are considered a full barrier. That is, no memory operand will be moved across the operation, either forward or backward. Further, instructions will be issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation.
However the use of isync
rather than sync
at the end is bothering me. Is this actually a full barrier? Or:
Could loads performed after the
__sync_val_compare_and_swap
fail to see stores performed before the store that produced the value__sync_val_compare_and_swap
loaded?Could stores performed after the
__sync_val_compare_and_swap
be seen by other threads before they see the value stored by the__sync_val_compare_and_swap
?
__atomic_*
builtins are preferred as they lets you choose C11/C++11 memory model (consume, acquire, release, both or sequentially consistent) – Gastrostomy__sync
one where I want the full-barrier property that's stronger than the C11 memory model and I'm not clear that GCC is actually providing it. – Hectorhecuba__ATOMIC_SEQ_CST
does provide the full barrier property you want. Besides, I'm also looking for the answer to this question since I'm curious about this one too. – Gastrostomy__sync
version. – Hectorhecuba__ATOMIC_SEQ_CST
produces the same asm, then presumably there's some reason. I think seq-cst requires that later loads/stores can't become visible before the store part of the CAS. – Seinestwcx.
is doing some magic that makes it work. – Hectorhecuba__sync
to provide a full barrier. Yes, it is the same as the__sync
* version (and also kinda acts as a fallback). *edit – Gastrostomyisync
prevents speculative execution from accessing earlier operations (acquire),lwsync
is used for 'release' guarantees and is replaced bysync
in case of a seq/cst operation. – Polyvinyl__atomic_store
with seq_cst for ppc64 and it's totally wrong -- it's only a release barrier (sync;stw
). – Hectorhecuba__atomic_store
with__ATOMIC_SEQ_CST
and__ATOMIC_RELEASE
seems to usesync
andlwsync
, respectively. Sequentially consistent ordering only guarantees total order of memory operations wrt other__ATOMIC_SEQ_CST
operations, not relaxed ones. I think the GCC's documentation for__sync
is indeed a bit misleading. So was I, who probably do need to have a cup of coffee after skipping over a night :/ – Gastrostomyisync
being purely an instruction reordering barrier, not a memory barrier, is that it has no influence on synchronization of memory between cores. – Hectorhecuba