在多核 x86 上,是否需要将 LOCK 作为 XCHG 的前缀?

发布于 2024-09-07 08:41:16 字数 177 浏览 1 评论 0原文

如果mem是共享内存位置,我是否需要:

XCHG EAX,mem

或:

LOCK XCHG EAX,mem

以原子方式进行交换?

谷歌搜索会得到“是”和“否”的答案。有谁明确知道这一点吗?

If mem is a shared memory location, do I need:

XCHG EAX,mem

or:

LOCK XCHG EAX,mem

to do the exchange atomically?

Googling this yields both yes and no answers. Does anyone know this definitively?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拔了角的鹿 2024-09-14 08:41:16

英特尔的文档似乎很清楚它是多余的。

IA-32 英特尔® 架构
软件开发人员手册
第 3A 卷:
系统编程指南,第 1 部分

7.1.2.1 说:

处理器自动遵循 LOCK 语义的操作如下
如下:

  • 执行引用内存的 XCHG 指令时。

同样,

英特尔® 64 和 IA-32 架构
软件开发人员手册
第 2B 卷:
指令集参考,NZ

XCHG:

如果引用内存操作数,处理器的锁定协议会自动
在交换操作期间实现,无论是否存在 LOCK 前缀或 IOPL 值。

请注意,这实际上并不意味着无论是否使用 LOCK 前缀,都会断言 LOCK# 信号,7.1.4 描述了如果内存位置被缓存,则在后续处理器上如何在没有 LOCK# 的情况下保留锁定语义。聪明,绝对超出了我的想象。

Intel's documentation seems pretty clear that it is redundant.

IA-32 Intel® Architecture
Software Developer’s Manual
Volume 3A:
System Programming Guide, Part 1

7.1.2.1 says:

The operations on which the processor automatically follows the LOCK semantics are as
follows:

  • When executing an XCHG instruction that references memory.

Similarly,

Intel® 64 and IA-32 Architectures
Software Developer’s Manual
Volume 2B:
Instruction Set Reference, N-Z

XCHG:

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.

Note that this doesn't actually meant that the LOCK# signal is asserted whether or not the LOCK prefix is used, 7.1.4 describes how on later processors locking semantics are preserved without a LOCK# if the memory location is cached. Clever, and definitely over my head.

天气好吗我好吗 2024-09-14 08:41:16

自 386 天以来,无论您是否在其上添加锁定前缀,xchg 都会断言锁定信号。 Intel 文档 在 IA-32 指令集参考 NZ 中非常清楚地介绍了这一点。

Since 386 days, xchg will assert the Lock signal whether or not you put the lock prefix on it. Intel's documentation covers this quite clearly in IA-32 instruction set reference N-Z.

靑春怀旧 2024-09-14 08:41:16

根据 80386 说明手册BUS LOCK 在交换期间被置位。对于此操作,LOCK 前缀没有优先级,I/O 权限级别

我的建议是,由于文档指出无论是否存在 LOCK 前缀,都会断言 BUS LOCK ,否则 LOCK XCHG EAX, mem 会被断言安全的。如有疑问,请添加LOCK

As per the 80386 Instruction Manual, BUS LOCK is asserted for the duration of the exchange. The LOCK prefix has no precedence for this operation and neither does the value of the I/O Privilege Level.

My suggestion is that since the documentation states that BUS LOCK is asserted regardless of the presence of the LOCK prefix, LOCK XCHG EAX, mem is otherwise safe. When in doubt, add a LOCK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文