最近,我一直在浏览Go的源代码,特别是386.s。下面是一个internal.atomic.Store64函数的实现。
// void runtime∕internal∕atomic·Store64(uint64 volatile* addr, uint64 v);
TEXT runtime∕internal∕atomic·Store64(SB), NOSPLIT, $0-12
MOVL ptr+0(FP), AX
TESTL $7, AX
JZ 2(PC)
MOVL 0, AX // crash with nil ptr deref
// MOVQ and EMMS were introduced on the Pentium MMX.
MOVQ val+4(FP), M0
MOVQ M0, (AX)
EMMS
// This is essentially a no-op, but it provides required memory fencing.
// It can be replaced with MFENCE, but MFENCE was introduced only on the Pentium4 (SSE2).
XORL AX, AX
LOCK
XADDL AX, (SP)
RET这个问题与函数中的第二个注释有关。有人能解释一下XORL AX, AX作为内存围栏是如何工作的吗?
我想这与紧随其后的LOCK有关,但它是如何工作的呢?
谢谢。
发布于 2019-10-07 13:55:21
不是XOR起着内存围栏的作用。正是锁定的XADD指令才能做到这一点。EAX指令清除XOR,这样XADD指令就不会真正改变内存的内容。
这个问题的答案:一个x86 CPU有多少内存屏障指令?描述了锁定指令的内存排序行为。
https://stackoverflow.com/questions/58270779
复制相似问题