From 70c49a6416b34c002711fb753e5ac61c4d5e3cd7 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 23 Feb 2022 21:41:53 +0100 Subject: [PATCH] pipeline: Add Thread::relax() for more efficient busy waiting Equivalent to cpu_relax() or the pause instruction on x86 --- panda/src/pipeline/thread.I | 14 ++++++++++++++ panda/src/pipeline/thread.h | 1 + 2 files changed, 15 insertions(+) diff --git a/panda/src/pipeline/thread.I b/panda/src/pipeline/thread.I index 7fce66e6d5..c262808675 100644 --- a/panda/src/pipeline/thread.I +++ b/panda/src/pipeline/thread.I @@ -222,6 +222,20 @@ consider_yield() { ThreadImpl::consider_yield(); } +/** + * Equivalent to the pause instruction on x86 or the yield instruction on ARM, + * to be called in spin loops. + */ +INLINE void Thread:: +relax() { +#ifdef _MSC_VER + YieldProcessor(); +#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64)) + __asm__ __volatile__("pause"); +#elif defined(__arm__) || defined(__aarch64__) + __asm__ __volatile__ ("yield" ::: "memory"); +#endif +} /** * Returns thread statistics. The first number is the total number of context diff --git a/panda/src/pipeline/thread.h b/panda/src/pipeline/thread.h index 55ea0cbb09..ec7ada4971 100644 --- a/panda/src/pipeline/thread.h +++ b/panda/src/pipeline/thread.h @@ -80,6 +80,7 @@ PUBLISHED: BLOCKING INLINE static void force_yield(); BLOCKING INLINE static void consider_yield(); + BLOCKING INLINE static void relax(); INLINE static bool get_context_switches(size_t &total, size_t &involuntary);