X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FMicroLock.cpp;h=06e8de08768bb01bbb69636dffed07b198738108;hb=ab9a4d71366857cf1facc1642d7ef2fd04004cca;hp=d6656dce4c32c2fc374eb596200c508a0215a04b;hpb=ed14d607d70bc342818ea2f6875316654dbc1002;p=folly.git diff --git a/folly/MicroLock.cpp b/folly/MicroLock.cpp index d6656dce..06e8de08 100644 --- a/folly/MicroLock.cpp +++ b/folly/MicroLock.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -15,6 +15,9 @@ */ #include +#include + +#include 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_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); } }