proxygen
folly::MicroLockBase< MaxSpins, MaxYields > Class Template Reference

#include <MicroLock.h>

Inheritance diagram for folly::MicroLockBase< MaxSpins, MaxYields >:
folly::MicroLockCore

Public Member Functions

void lock (unsigned slot)
 
void lock ()
 
bool try_lock (unsigned slot)
 
bool try_lock ()
 
- Public Member Functions inherited from folly::MicroLockCore
void unlock (unsigned slot)
 
void unlock ()
 
void init ()
 

Additional Inherited Members

- Protected Member Functions inherited from folly::MicroLockCore
detail::Futexword () const
 
uint32_t baseShift (unsigned slot) const
 
uint32_t heldBit (unsigned slot) const
 
uint32_t waitBit (unsigned slot) const
 
- Static Protected Member Functions inherited from folly::MicroLockCore
static void lockSlowPath (uint32_t oldWord, detail::Futex<> *wordPtr, uint32_t slotHeldBit, unsigned maxSpins, unsigned maxYields)
 
- Protected Attributes inherited from folly::MicroLockCore
uint8_t lock_
 

Detailed Description

template<unsigned MaxSpins = 1000, unsigned MaxYields = 0>
class folly::MicroLockBase< MaxSpins, MaxYields >

Definition at line 170 of file MicroLock.h.

Member Function Documentation

template<unsigned MaxSpins, unsigned MaxYields>
void folly::MicroLockBase< MaxSpins, MaxYields >::lock ( unsigned  slot)
inline

Definition at line 210 of file MicroLock.h.

References folly::MicroLockCore::heldBit(), folly::MicroLockCore::lockSlowPath(), uint32_t, folly::MicroLockCore::waitBit(), and folly::MicroLockCore::word().

210  {
211  static_assert(MaxSpins + MaxYields < (unsigned)-1, "overflow");
212 
213  detail::Futex<>* wordPtr = word();
214  uint32_t oldWord;
215  oldWord = wordPtr->load(std::memory_order_relaxed);
216  if ((oldWord & heldBit(slot)) == 0 &&
217  wordPtr->compare_exchange_weak(
218  oldWord,
219  oldWord | heldBit(slot),
220  std::memory_order_acquire,
221  std::memory_order_relaxed)) {
222  // Fast uncontended case: memory_order_acquire above is our barrier
223  } else {
224  // lockSlowPath doesn't have any slot-dependent computation; it
225  // just shifts the input bit. Make sure its shifting produces the
226  // same result a call to waitBit for our slot would.
227  assert(heldBit(slot) << 1 == waitBit(slot));
228  // lockSlowPath emits its own memory barrier
229  lockSlowPath(oldWord, wordPtr, heldBit(slot), MaxSpins, MaxYields);
230  }
231 }
uint32_t heldBit(unsigned slot) const
Definition: MicroLock.h:144
static void lockSlowPath(uint32_t oldWord, detail::Futex<> *wordPtr, uint32_t slotHeldBit, unsigned maxSpins, unsigned maxYields)
Definition: MicroLock.cpp:24
detail::Futex * word() const
Definition: MicroLock.h:129
uint32_t waitBit(unsigned slot) const
Definition: MicroLock.h:148
template<unsigned MaxSpins = 1000, unsigned MaxYields = 0>
void folly::MicroLockBase< MaxSpins, MaxYields >::lock ( )
inline

Definition at line 173 of file MicroLock.h.

References folly::lock(), and NO_SANITIZE_ADDRESS.

173  {
174  lock(0);
175  }
template<unsigned MaxSpins, unsigned MaxYields>
bool folly::MicroLockBase< MaxSpins, MaxYields >::try_lock ( unsigned  slot)
inline

Definition at line 183 of file MicroLock.h.

References folly::MicroLockCore::heldBit(), uint32_t, and folly::MicroLockCore::word().

183  {
184  // N.B. You might think that try_lock is just the fast path of lock,
185  // but you'd be wrong. Keep in mind that other parts of our host
186  // word might be changing while we take the lock! We're not allowed
187  // to fail spuriously if the lock is in fact not held, even if other
188  // people are concurrently modifying other parts of the word.
189  //
190  // We need to loop until we either see firm evidence that somebody
191  // else has the lock (by looking at heldBit) or see our CAS succeed.
192  // A failed CAS by itself does not indicate lock-acquire failure.
193 
194  detail::Futex<>* wordPtr = word();
195  uint32_t oldWord = wordPtr->load(std::memory_order_relaxed);
196  do {
197  if (oldWord & heldBit(slot)) {
198  return false;
199  }
200  } while (!wordPtr->compare_exchange_weak(
201  oldWord,
202  oldWord | heldBit(slot),
203  std::memory_order_acquire,
204  std::memory_order_relaxed));
205 
206  return true;
207 }
uint32_t heldBit(unsigned slot) const
Definition: MicroLock.h:144
detail::Futex * word() const
Definition: MicroLock.h:129
template<unsigned MaxSpins = 1000, unsigned MaxYields = 0>
bool folly::MicroLockBase< MaxSpins, MaxYields >::try_lock ( )
inline

Definition at line 177 of file MicroLock.h.

177  {
178  return try_lock(0);
179  }

The documentation for this class was generated from the following file: