Atomic operations on memory-mapped files
It's safe. For example, in amd64
he uses an instruction XCHG
.
func StoreUint64(addr *uint64, val uint64)
StoreUint64
atomically storesval
in*addr
.
TEXT ·StoreUint64(SB),NOSPLIT,$0-16
MOVQ addr+0(FP), BP
MOVQ val+8(FP), AX
XCHGQ AX, 0(BP)
RET
Intel 64 and IA-32 Software Developer's Guide
XCHG
-Exchange Register / Memory with RegisterDescription
Swaps the contents of the destination (first) and the original (second) operands. The operands can be two general purpose registers or a register and memory location. If the memory operand is referenced, the processor lock protocol is automatically executed for the duration of the exchange operation, regardless of the presence or absence of the LOCK prefix or the IOPL value.
source to share