From 81c7f6193ce66480b2a29a5b2c66a7356e743308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20M=C3=BCllner?= Date: Thu, 18 Apr 2024 23:58:58 +0200 Subject: [PATCH] RISC-V: Execute a PAUSE hint in spin loops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The atomic_spin_nop() macro can be used to run arch-specific code in the body of a spin loop to potentially improve efficiency. RISC-V's Zihintpause extension includes a PAUSE instruction for this use-case, which is encoded as a HINT, which means that it behaves like a NOP on systems that don't implement Zihintpause. Binutils supports Zihintpause since 2.36, so this patch uses the ".insn" directive to keep the code compatible with older toolchains. Signed-off-by: Christoph Müllner Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt --- sysdeps/unix/sysv/linux/riscv/atomic-machine.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h index c1c9d949a0..90283d9746 100644 --- a/sysdeps/unix/sysv/linux/riscv/atomic-machine.h +++ b/sysdeps/unix/sysv/linux/riscv/atomic-machine.h @@ -178,4 +178,7 @@ # error "ISAs that do not subsume the A extension are not supported" #endif /* !__riscv_atomic */ +/* Execute a PAUSE hint when spinning. */ +#define atomic_spin_nop() __asm(".insn i 0x0f, 0, x0, x0, 0x010") + #endif /* bits/atomic.h */