Generalize FOLLY_SPIN_WAIT to use Portability methods
[folly.git] / folly / detail / AtomicHashUtils.h
index a896ba3a5812ec6133f6e87b6e44733d8df10b62..ebc96562de037f8f8b9c9c7241dd8e5585973935 100644 (file)
  * limitations under the License.
  */
 
+#ifndef incl_FOLLY_ATOMIC_HASH_UTILS_H
+#define incl_FOLLY_ATOMIC_HASH_UTILS_H
+
+#include <folly/Portability.h>
+#include <thread>
+
 // Some utilities used by AtomicHashArray and AtomicHashMap
 //
-// Note: no include guard; different -inl.h files include this and
-// undef it more than once in a translation unit.
-// override-include-guard
 
-#if !(defined(__x86__) || defined(__i386__) || FOLLY_X64)
-#define FOLLY_SPIN_WAIT(condition)                \
-   for (int counter = 0; condition; ++counter) {  \
-     if (counter < 10000) continue;               \
-     pthread_yield();                             \
-   }
-#else
-#define FOLLY_SPIN_WAIT(condition)              \
-  for (int counter = 0; condition; ++counter) { \
-    if (counter < 10000) {                      \
-      asm volatile("pause");                    \
-      continue;                                 \
-    }                                           \
-    pthread_yield();                            \
+namespace folly { namespace detail {
+
+template <typename Cond>
+void atomic_hash_spin_wait(Cond condition) {
+  constexpr size_t kPauseLimit = 10000;
+  for (size_t i = 0; condition(); ++i) {
+    if (i < kPauseLimit) {
+      folly::asm_pause();
+    } else {
+      std::this_thread::yield();
+    }
   }
+}
+
+}} // namespace folly::detail
+
 #endif