pipeline: Add Thread::relax() for more efficient busy waiting

Equivalent to cpu_relax() or the pause instruction on x86
This commit is contained in:
rdb 2022-02-23 21:41:53 +01:00
parent c3ce8164bc
commit 70c49a6416
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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);