I'm using a memory mapped file and I need to use an atomic store on Go. I would use StoreUint64() if I were working on regularly allocated memory. However, I'm not sure how atomic operations work on memory mapped files.
Is it safe to use StoreUint64() on memory mapped files?
It's safe. For example, on amd64, it uses the XCHG instruction.
func StoreUint64func StoreUint64(addr *uint64, val uint64)
StoreUint64atomically storesvalinto*addr.
src/sync/atomic/asm_amd64.s;
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 Architectures Software Developer's Manual
XCHG—Exchange Register/Memory with RegisterDescription
Exchanges the contents of the destination (first) and source (second) operands. The operands can be two general-purpose registers or a register and a memory location. If a memory operand is referenced, the processor’s locking protocol is automatically implemented for the duration of the exchange operation, regardless of the presence or absence of the LOCK prefix or of the value of the IOPL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With