内存映射文件上的原子操作

huangapple go评论85阅读模式
英文:

Atomic operations on memory mapped files

问题

我正在使用一个内存映射文件,并且需要在Go语言中使用原子存储操作。如果我在常规分配的内存上工作,我会使用StoreUint64()函数。然而,我不确定原子操作在内存映射文件上是否安全。

在内存映射文件上使用StoreUint64()函数是否安全?

英文:

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?

答案1

得分: 3

这是要翻译的内容:

这是安全的。例如,在amd64上,它使用XCHG指令。

func StoreUint64

func StoreUint64(addr *uint64, val uint64)

StoreUint64原子地将val存储到*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和IA-32体系结构软件开发人员手册

XCHG-交换寄存器/内存与寄存器

描述

交换目标(第一个)和源(第二个)操作数的内容。操作数可以是两个通用寄存器或一个寄存器和一个内存位置。如果引用了一个内存操作数,在交换操作的持续时间内,处理器的锁定协议将自动实现,无论有无LOCK前缀或IOPL的值如何。

英文:

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.

huangapple
  • 本文由 发表于 2015年4月25日 06:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/29858774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定