Atomic operations on memory-mapped files

I am using a memory mapped file and I need to use an atomic store in Go. I would use StoreUint64 () if I was working on distributed memory. However, I don't know how atomic operations work on memory files.

Can StoreUint64 () be used on memory mapped files?

+3


source to share


1 answer


It's safe. For example, in amd64

he uses an instruction XCHG

.

func StoreUint64

    func StoreUint64(addr *uint64, val uint64)

      

StoreUint64

atomically stores val

in *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 Software Developer's Guide

XCHG

-Exchange Register / Memory with Register

Description

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.

+3


source







All Articles