summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-05-15 11:08:00 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-05-15 12:39:02 -0400
commitc964524f18fc5c3b7baf5a3d0eac0980f17d3cf0 (patch)
treeb3975cd85b4375add88ff005538bd578d9dc16f3
parent41c2f7a68bf41b0114a640a039bd8bf411988d23 (diff)
downloadbfs-c964524f18fc5c3b7baf5a3d0eac0980f17d3cf0.tar.xz
atomic: Add a spin_loop() hint
-rw-r--r--src/atomic.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/atomic.h b/src/atomic.h
index 7d32e73..360de20 100644
--- a/src/atomic.h
+++ b/src/atomic.h
@@ -8,6 +8,7 @@
#ifndef BFS_ATOMIC_H
#define BFS_ATOMIC_H
+#include "prelude.h"
#include "sanity.h"
#include <stdatomic.h>
@@ -101,4 +102,17 @@
#define signal_fence(order) \
atomic_signal_fence(memory_order_##order)
+/**
+ * A hint to the CPU to relax while it spins.
+ */
+#if __has_builtin(__builtin_ia32_pause)
+# define spin_loop() __builtin_ia32_pause()
+#elif __has_builtin(__builtin_arm_yield)
+# define spin_loop() __builtin_arm_yield()
+#elif __has_builtin(__builtin_riscv_pause)
+# define spin_loop() __builtin_riscv_pause()
+#else
+# define spin_loop() ((void)0)
+#endif
+
#endif // BFS_ATOMIC_H