Просмотр исходного кода

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

Equivalent to cpu_relax() or the pause instruction on x86
rdb 3 лет назад
Родитель
Сommit
70c49a6416
2 измененных файлов с 15 добавлено и 0 удалено
  1. 14 0
      panda/src/pipeline/thread.I
  2. 1 0
      panda/src/pipeline/thread.h

+ 14 - 0
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

+ 1 - 0
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);