难道我们使用XCHG时需要MFENCE [英] Do we need mfence when using xchg
问题描述
我有一组并根据测试 XCHG
装配锁。我的问题是:
I have a set and test xchg
based assembly lock. my question is :
我们是否需要使用内存击剑( MFENCE
, SFENCE
或 lfence
使用 XCHG
指令时)?
Do we need to use memory fencing (mfence
, sfence
or lfence
) when using xchg
instruction ?
编辑:
64位平台:英特尔的Nehalem
64 Bit platform : with Intel nehalem
推荐答案
正如在其他的答案说锁preFIX是隐含的,在这里,所以在汇编水平没有问题。当您使用内联汇编问题可以在C(或C ++)水平奠定。在这里,你必须确保编译器不会相对于指令重新排序,你的 XCHG
。如果你正在使用gcc(或表兄弟),你通常会做这样的事情:
As said in the other answers the lock prefix is implicit, here, so there is no problem on the assembler level. The problem may lay on the C (or C++) level when you use that as inline assembler. Here you have to ensure that the compiler doesn't reorder instructions with respect to your xchg
. If you are using gcc (or cousins) you would typically do something like:
__asm__ __volatile__("xchgl %1, %0"
: "=r"(ret)
: "m"(*point), "0"(ret)
: "memory");
这是声明指令挥发性的和的补充记忆撞地。
that is declare the instruction as volatile and add the "memory" clobber.
这篇关于难道我们使用XCHG时需要MFENCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!