为什么 hint::spin_loop 在 aarch64 上使用 ISB?

Why does hint::spin_loop use ISB on aarch64?

std::hint 中有一个 spin_loop function with the following definition in its documentation:

Emits a machine instruction to signal the processor that it is running in a busy-wait spin-loop (“spin lock”).

Upon receiving the spin-loop signal the processor can optimize its behavior by, for example, saving power or switching hyper-threads.

Depending on the target architecture, this compiles to either:

最后一个让我有点头晕 ()。我认为 ISB 是一个冗长的操作,这意味着,如果在自旋锁中使用,线程在尝试检测锁是否再次打开时会有点滞后,否则几乎没有任何好处。

在 aarch64 的自旋循环中使用 ISB SY 而不是 NOP 有什么优势?

我不得不深入研究 Rust 存储库历史才能得到这个答案:

yield 已替换为 c064b6560b7c 中的 isb:

On arm64 we have seen on several databases that ISB (instruction synchronization barrier) is better to use than yield in a spin loop. The yield instruction is a nop. The isb instruction puts the processor to sleep for some short time. isb is a good equivalent to the pause instruction on x86.

[...]

所以本质上,它使用 ISB 完成暂停处理器所需的时间,从而减少功耗。

Peter Cordes 在他的评论中很好地解释了这一点:

ISB SY doesn't stall for long, just saves a bit of power vs. spamming loads in a tight loop.