proxygen
folly::RWSpinLock Class Reference

#include <RWSpinLock.h>

Classes

class  ReadHolder
 
class  UpgradedHolder
 
class  WriteHolder
 

Public Member Functions

constexpr RWSpinLock ()
 
 RWSpinLock (RWSpinLock const &)=delete
 
RWSpinLockoperator= (RWSpinLock const &)=delete
 
void lock ()
 
void unlock ()
 
void lock_shared ()
 
void unlock_shared ()
 
void unlock_and_lock_shared ()
 
void lock_upgrade ()
 
void unlock_upgrade ()
 
void unlock_upgrade_and_lock ()
 
void unlock_upgrade_and_lock_shared ()
 
void unlock_and_lock_upgrade ()
 
bool try_lock ()
 
bool try_lock_shared ()
 
bool try_unlock_upgrade_and_lock ()
 
bool try_lock_upgrade ()
 
int32_t bits () const
 

Private Types

enum  : int32_t { READER = 4, UPGRADED = 2, WRITER = 1 }
 

Private Attributes

std::atomic< int32_tbits_
 

Detailed Description

Definition at line 181 of file RWSpinLock.h.

Member Enumeration Documentation

anonymous enum : int32_t
private
Enumerator
READER 
UPGRADED 
WRITER 

Definition at line 182 of file RWSpinLock.h.

Constructor & Destructor Documentation

constexpr folly::RWSpinLock::RWSpinLock ( )
inline

Definition at line 185 of file RWSpinLock.h.

References operator=().

185 : bits_(0) {}
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
folly::RWSpinLock::RWSpinLock ( RWSpinLock const &  )
delete

Member Function Documentation

int32_t folly::RWSpinLock::bits ( ) const
inline

Definition at line 306 of file RWSpinLock.h.

References bits_.

306  {
307  return bits_.load(std::memory_order_acquire);
308  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void folly::RWSpinLock::lock ( )
inline

Definition at line 191 of file RWSpinLock.h.

References count, LIKELY, try_lock(), and folly::fibers::yield().

Referenced by folly::RWSpinLock::ReadHolder::reset(), folly::RWSpinLock::UpgradedHolder::reset(), folly::RWSpinLock::WriteHolder::reset(), and folly::RWSpinLock::WriteHolder::WriteHolder().

191  {
192  uint_fast32_t count = 0;
193  while (!LIKELY(try_lock())) {
194  if (++count > 1000) {
196  }
197  }
198  }
#define LIKELY(x)
Definition: Likely.h:47
int * count
void folly::RWSpinLock::lock_shared ( )
inline

Definition at line 207 of file RWSpinLock.h.

References count, LIKELY, try_lock_shared(), and folly::fibers::yield().

Referenced by folly::RWSpinLock::ReadHolder::ReadHolder(), and folly::RWSpinLock::ReadHolder::reset().

207  {
208  uint_fast32_t count = 0;
209  while (!LIKELY(try_lock_shared())) {
210  if (++count > 1000) {
212  }
213  }
214  }
#define LIKELY(x)
Definition: Likely.h:47
bool try_lock_shared()
Definition: RWSpinLock.h:276
int * count
void folly::RWSpinLock::lock_upgrade ( )
inline

Definition at line 227 of file RWSpinLock.h.

References count, try_lock_upgrade(), and folly::fibers::yield().

Referenced by folly::RWSpinLock::UpgradedHolder::reset(), and folly::RWSpinLock::UpgradedHolder::UpgradedHolder().

227  {
228  uint_fast32_t count = 0;
229  while (!try_lock_upgrade()) {
230  if (++count > 1000) {
232  }
233  }
234  }
int * count
bool try_lock_upgrade()
Definition: RWSpinLock.h:295
RWSpinLock& folly::RWSpinLock::operator= ( RWSpinLock const &  )
delete

Referenced by RWSpinLock().

bool folly::RWSpinLock::try_lock ( )
inline

Definition at line 264 of file RWSpinLock.h.

References bits_, folly::symbolizer::test::expect(), int32_t, and WRITER.

Referenced by lock().

264  {
265  int32_t expect = 0;
266  return bits_.compare_exchange_strong(
267  expect, WRITER, std::memory_order_acq_rel);
268  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void expect(LineReader &lr, const char *expected)
bool folly::RWSpinLock::try_lock_shared ( )
inline

Definition at line 276 of file RWSpinLock.h.

References bits_, int32_t, READER, UNLIKELY, UPGRADED, folly::value(), and WRITER.

Referenced by lock_shared().

276  {
277  // fetch_add is considerably (100%) faster than compare_exchange,
278  // so here we are optimizing for the common (lock success) case.
279  int32_t value = bits_.fetch_add(READER, std::memory_order_acquire);
280  if (UNLIKELY(value & (WRITER | UPGRADED))) {
281  bits_.fetch_add(-READER, std::memory_order_release);
282  return false;
283  }
284  return true;
285  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
#define UNLIKELY(x)
Definition: Likely.h:48
bool folly::RWSpinLock::try_lock_upgrade ( )
inline

Definition at line 295 of file RWSpinLock.h.

References bits_, int32_t, UPGRADED, folly::value(), and WRITER.

Referenced by lock_upgrade().

295  {
296  int32_t value = bits_.fetch_or(UPGRADED, std::memory_order_acquire);
297 
298  // Note: when failed, we cannot flip the UPGRADED bit back,
299  // as in this case there is either another upgrade lock or a write lock.
300  // If it's a write lock, the bit will get cleared up when that lock's done
301  // with unlock().
302  return ((value & (UPGRADED | WRITER)) == 0);
303  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
bool folly::RWSpinLock::try_unlock_upgrade_and_lock ( )
inline

Definition at line 288 of file RWSpinLock.h.

References bits_, folly::symbolizer::test::expect(), int32_t, UPGRADED, and WRITER.

Referenced by unlock_upgrade_and_lock().

288  {
290  return bits_.compare_exchange_strong(
291  expect, WRITER, std::memory_order_acq_rel);
292  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void expect(LineReader &lr, const char *expected)
void folly::RWSpinLock::unlock ( )
inline

Definition at line 201 of file RWSpinLock.h.

References bits_, READER, UPGRADED, and WRITER.

Referenced by folly::RWSpinLock::WriteHolder::reset(), unlock_and_lock_shared(), and folly::RWSpinLock::WriteHolder::~WriteHolder().

201  {
202  static_assert(READER > WRITER + UPGRADED, "wrong bits!");
203  bits_.fetch_and(~(WRITER | UPGRADED), std::memory_order_release);
204  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void folly::RWSpinLock::unlock_and_lock_shared ( )
inline

Definition at line 221 of file RWSpinLock.h.

References bits_, READER, and unlock().

Referenced by folly::RWSpinLock::ReadHolder::ReadHolder().

221  {
222  bits_.fetch_add(READER, std::memory_order_acquire);
223  unlock();
224  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void folly::RWSpinLock::unlock_and_lock_upgrade ( )
inline

Definition at line 256 of file RWSpinLock.h.

References bits_, UPGRADED, and WRITER.

Referenced by folly::RWSpinLock::UpgradedHolder::UpgradedHolder().

256  {
257  // need to do it in two steps here -- as the UPGRADED bit might be OR-ed at
258  // the same time when other threads are trying do try_lock_upgrade().
259  bits_.fetch_or(UPGRADED, std::memory_order_acquire);
260  bits_.fetch_add(-WRITER, std::memory_order_release);
261  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void folly::RWSpinLock::unlock_shared ( )
inline

Definition at line 216 of file RWSpinLock.h.

References bits_, and READER.

Referenced by folly::RWSpinLock::ReadHolder::reset(), and folly::RWSpinLock::ReadHolder::~ReadHolder().

216  {
217  bits_.fetch_add(-READER, std::memory_order_release);
218  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void folly::RWSpinLock::unlock_upgrade ( )
inline

Definition at line 236 of file RWSpinLock.h.

References bits_, and UPGRADED.

Referenced by folly::RWSpinLock::UpgradedHolder::reset(), and folly::RWSpinLock::UpgradedHolder::~UpgradedHolder().

236  {
237  bits_.fetch_add(-UPGRADED, std::memory_order_acq_rel);
238  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511
void folly::RWSpinLock::unlock_upgrade_and_lock ( )
inline

Definition at line 241 of file RWSpinLock.h.

References count, int64_t, try_unlock_upgrade_and_lock(), and folly::fibers::yield().

Referenced by folly::RWSpinLock::WriteHolder::WriteHolder().

241  {
242  int64_t count = 0;
243  while (!try_unlock_upgrade_and_lock()) {
244  if (++count > 1000) {
246  }
247  }
248  }
int * count
bool try_unlock_upgrade_and_lock()
Definition: RWSpinLock.h:288
void folly::RWSpinLock::unlock_upgrade_and_lock_shared ( )
inline

Definition at line 251 of file RWSpinLock.h.

References bits_, READER, and UPGRADED.

Referenced by folly::RWSpinLock::ReadHolder::ReadHolder().

251  {
252  bits_.fetch_add(READER - UPGRADED, std::memory_order_acq_rel);
253  }
std::atomic< int32_t > bits_
Definition: RWSpinLock.h:511

Member Data Documentation


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