Fix copyright lines
[folly.git] / folly / MicroLock.cpp
index d6656dce4c32c2fc374eb596200c508a0215a04b..910e23f353d647b0a0006e6c0a7d04d812a19785 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2016-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
  */
 
 #include <folly/MicroLock.h>
+#include <thread>
+
+#include <folly/portability/Asm.h>
 
 namespace folly {
 
@@ -23,7 +26,7 @@ void MicroLockCore::lockSlowPath(uint32_t oldWord,
                                  uint32_t slotHeldBit,
                                  unsigned maxSpins,
                                  unsigned maxYields) {
-  unsigned newWord;
+  uint32_t newWord;
   unsigned spins = 0;
   uint32_t slotWaitBit = slotHeldBit << 1;
 
@@ -45,7 +48,10 @@ retry:
       }
       (void)wordPtr->futexWait(newWord, slotHeldBit);
     } else if (spins > maxSpins) {
-      sched_yield();
+      // sched_yield(), but more portable
+      std::this_thread::yield();
+    } else {
+      folly::asm_volatile_pause();
     }
     oldWord = wordPtr->load(std::memory_order_relaxed);
     goto retry;
@@ -54,14 +60,9 @@ retry:
   newWord = oldWord | slotHeldBit;
   if (!wordPtr->compare_exchange_weak(oldWord,
                                       newWord,
-                                      std::memory_order_relaxed,
+                                      std::memory_order_acquire,
                                       std::memory_order_relaxed)) {
     goto retry;
   }
-
-  // Locks are traditionally memory barriers, so we emit a full fence
-  // even though we were happy using relaxed atomics for the
-  // lock itself.
-  std::atomic_thread_fence(std::memory_order_seq_cst);
-}
 }
+} // namespace folly