英文:
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(addr *uint64, val uint64)
StoreUint64
原子地将val
存储到*addr
。
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
.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论