winapi - 原子 x86 指令与 MS 的 InterlockedCompareExchange 文档的对齐要求?

标签 winapi x86 atomic memory-alignment interlocked

Microsoft 提供 InterlockedCompareExchange用于执行原子比较和交换操作的函数。还有一个_InterlockedCompareExchange 内在

在 x86 上,这些是使用 lock cmpxchg 指令实现的。

但是,通读这三种方法的文档后,他们似乎在对齐要求上并不一致。

英特尔的reference manual没有提及对齐(除了如果启用了对齐检查并且进行了未对齐的内存引用,则会生成异常)

我还查找了 lock 前缀,其中明确指出

The integrity of the LOCK prefix is not affected by the alignment of the memory field.

(强调我的)

所以英特尔似乎说对齐是无关紧要的。无论如何,该操作都是原子的。

_InterlockedCompareExchange 内部文档也没有提及对齐,但是 InterlockedCompareExchange 函数 指出

The parameters for this function must be aligned on a 32-bit boundary; otherwise, the function will behave unpredictably on multiprocessor x86 systems and any non-x86 systems.

那么什么给出了呢? InterlockedCompareExchange 的对齐要求是否只是为了确保该函数即使在 cmpxchg 指令不可用的 486 之前的 CPU 上也能正常工作? 根据上述信息,这似乎是可能的,但我想在依赖它之前确定一下。 :)

或者 ISA 是否需要对齐来保证原子性,而我只是在英特尔引用手册中查找了错误的位置?

最佳答案

x86 要求将lock cmpxchg 指令对齐为原子指令。然而,为了获得良好的性能,对齐是必要的。

这应该不足为奇,向后兼容性意味着 14 年前使用手册编写的软件仍然可以在今天的处理器上运行。现代 CPU 甚至有一个专门用于分割锁检测的性能计数器,因为它非常昂贵。 (核心不能在操作期间仅保留对单个缓存行的独占访问;它必须执行类似于传统总线锁定的操作)。

微软究竟为何记录对齐要求尚不清楚。这对于支持 RISC 架构当然是必要的,但多处理器 x86 上的不可预测行为的具体声明甚至可能是无效的。 (除非它们意味着不可预测的性能,而不是正确性问题。)

您猜测仅适用于没有 lock cmpxchg 的 486 之前的系统可能是正确的;那里需要一种不同的机制,这可能需要某种围绕纯加载或纯存储的锁定。 (另请注意,486 cmpxchg 具有与 currently-undocumented opcode 不同的 modern cmpxchg (0f b1) (0f a7),后者是 586 Pentium 中新增的;Windows 可能只使用 cmpxchg 在 P5 Pentium 及更高版本上,我不知道。)这也许可以解释某些 x86 上的奇怪现象,但并不意味着现代 x86 上的奇怪现象。

Intel® 64 and IA-32 Architectures Software Developer’s Manual
Volume 3 (3A): System Programming Guide
January 2013

8.1.2.2 Software Controlled Bus Locking

To explicitly force the LOCK semantics, software can use the LOCK prefix with the following instructions when they are used to modify a memory location. [...]

• The exchange instructions (XADD, CMPXCHG, and CMPXCHG8B).
• The LOCK prefix is automatically assumed for XCHG instruction.
• [...]

[...] The integrity of a bus lock is not affected by the alignment of the memory field. The LOCK semantics are followed for as many bus cycles as necessary to update the entire operand. However, it is recommend that locked accesses be aligned on their natural boundaries for better system performance:

• Any boundary for an 8-bit access (locked or otherwise).
• 16-bit boundary for locked word accesses.
• 32-bit boundary for locked doubleword accesses.
• 64-bit boundary for locked quadword accesses.

<小时/>

有趣的事实:cmpxchg without a lock prefix is still atomic wrt. context switches ,因此可用于单核系统上的多线程。

即使未对齐,它仍然是原子的。中断(完全之前或完全之后),并且只有其他设备(例如 DMA)的内存读取才能看到撕裂。但此类访问也可以看到加载和存储之间的分离,因此即使旧 Windows 确实在单核系统上使用它来实现更高效的 InterlockedCompareExchange,它仍然不需要正确性对齐,只需要性能对齐。如果这可以用于硬件访问,Windows 可能不会这样做。

如果库函数需要执行与锁定 cmpxchg 分开的纯加载,这可能有意义,但不需要这样做。 (如果不内联,32 位版本必须从堆栈加载其参数,但这是私有(private)的,无法访问共享变量。)

关于winapi - 原子 x86 指令与 MS 的 InterlockedCompareExchange 文档的对齐要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1415256/

相关文章:

mongodb - 如何使用golang在mongodb中findAndModify一条记录?

delphi - DocumentProperties 在 XE6 中失败;在 Delphi 7 中工作

winapi - 如何读取调用堆栈?

winapi - 将字符串复制到剪贴板,粘贴时只写入一个字符

c - x86 linux - 如何创建带有地址提示的自定义 malloc

assembly - 如何在 x86 上的寄存器中存储地址

c++ - 获取常见文件类型的图标

memory - 如何将 8086 的内存空间扩展到 1 GB?

cuda - 如何实现涉及多个变量的自定义原子函数?

c++ - 使 std::atomic<> 对象具有限定符 - volatile 是否有意义?