/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#include <folly/MicroLock.h>
+#include <thread>
+
+#include <folly/portability/Asm.h>
namespace folly {
uint32_t slotHeldBit,
unsigned maxSpins,
unsigned maxYields) {
- unsigned newWord;
+ uint32_t newWord;
unsigned spins = 0;
uint32_t slotWaitBit = slotHeldBit << 1;
}
(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;
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