Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic operations on memory mapped files

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?

like image 774
dv1729 Avatar asked Apr 11 '26 13:04

dv1729


1 Answers

It's safe. For example, on amd64, it uses the XCHG instruction.

func StoreUint64

    func StoreUint64(addr *uint64, val uint64)

StoreUint64 atomically stores val into *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 Register

Description

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.

like image 65
peterSO Avatar answered Apr 14 '26 04:04

peterSO