proxygen
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy > Class Template Reference

#include <Synchronized.h>

Public Types

using MutexType = Mutex
 

Public Member Functions

 ~LockedPtrBase ()
 
void unlock ()
 

Protected Types

using UnlockerData = SynchronizedType *
 

Protected Member Functions

 LockedPtrBase ()
 
 LockedPtrBase (SynchronizedType *parent)
 
template<class Rep , class Period >
 LockedPtrBase (SynchronizedType *parent, const std::chrono::duration< Rep, Period > &timeout)
 
 LockedPtrBase (LockedPtrBase &&rhs) noexcept
 
LockedPtrBaseoperator= (LockedPtrBase &&rhs) noexcept
 
template<typename LockPolicyType >
 LockedPtrBase (LockedPtrBase< SynchronizedType, Mutex, LockPolicyType > &&rhs) noexcept
 
template<typename LockPolicyType >
LockedPtrBaseoperator= (LockedPtrBase< SynchronizedType, Mutex, LockPolicyType > &&rhs) noexcept
 
template<typename LockPolicyLhs , typename LockPolicyRhs >
void assignImpl (LockedPtrBase< SynchronizedType, Mutex, LockPolicyLhs > &lhs, LockedPtrBase< SynchronizedType, Mutex, LockPolicyRhs > &rhs) noexcept
 
UnlockerData releaseLock ()
 
void reacquireLock (UnlockerData &&data)
 

Static Protected Member Functions

static SynchronizedType * getSynchronized (UnlockerData data)
 

Protected Attributes

SynchronizedType * parent_ = nullptr
 

Friends

class folly::ScopedUnlocker< SynchronizedType, LockPolicy >
 
template<typename S , typename L >
class folly::LockedPtr
 
template<typename S , typename M , typename L >
class LockedPtrBase
 

Detailed Description

template<class SynchronizedType, class Mutex, class LockPolicy>
class folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >

A helper base class for implementing LockedPtr.

The main reason for having this as a separate class is so we can specialize it for std::mutex, so we can expose a std::unique_lock to the caller when std::mutex is being used. This allows callers to use a std::condition_variable with the mutex from a Synchronized<T, std::mutex>.

We don't use std::unique_lock with other Mutex types since it makes the LockedPtr class slightly larger, and it makes the logic to support ScopedUnlocker slightly more complicated. std::mutex is the only one that really seems to benefit from the unique_lock. std::condition_variable itself only supports std::unique_lock<std::mutex>, so there doesn't seem to be any real benefit to exposing the unique_lock with other mutex types.

Note that the SynchronizedType template parameter may or may not be const qualified.

Definition at line 47 of file Synchronized.h.

Member Typedef Documentation

template<class SynchronizedType, class Mutex, class LockPolicy>
using folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::MutexType = Mutex

Definition at line 988 of file Synchronized.h.

template<class SynchronizedType, class Mutex, class LockPolicy>
using folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::UnlockerData = SynchronizedType*
protected

Definition at line 1082 of file Synchronized.h.

Constructor & Destructor Documentation

template<class SynchronizedType, class Mutex, class LockPolicy>
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::~LockedPtrBase ( )
inline

Destructor releases.

Definition at line 1002 of file Synchronized.h.

1002  {
1003  if (parent_) {
1004  LockPolicy::unlock(parent_->mutex_);
1005  }
1006  }
SynchronizedType * parent_
template<class SynchronizedType, class Mutex, class LockPolicy>
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::LockedPtrBase ( )
inlineprotected

Definition at line 1023 of file Synchronized.h.

1023 {}
template<class SynchronizedType, class Mutex, class LockPolicy>
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::LockedPtrBase ( SynchronizedType *  parent)
inlineexplicitprotected

Definition at line 1024 of file Synchronized.h.

1024  : parent_(parent) {
1025  DCHECK(parent);
1026  if (!LockPolicy::lock(parent_->mutex_)) {
1027  parent_ = nullptr;
1028  }
1029  }
void lock(LockableOne &one, LockableTwo &two, Lockables &...lockables)
SynchronizedType * parent_
folly::Function< void()> parent
Definition: AtFork.cpp:34
template<class SynchronizedType, class Mutex, class LockPolicy>
template<class Rep , class Period >
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::LockedPtrBase ( SynchronizedType *  parent,
const std::chrono::duration< Rep, Period > &  timeout 
)
inlineprotected

Definition at line 1031 of file Synchronized.h.

1033  {
1034  if (LockPolicy::try_lock_for(parent->mutex_, timeout)) {
1035  this->parent_ = parent;
1036  }
1037  }
SynchronizedType * parent_
folly::Function< void()> parent
Definition: AtFork.cpp:34
template<class SynchronizedType, class Mutex, class LockPolicy>
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::LockedPtrBase ( LockedPtrBase< SynchronizedType, Mutex, LockPolicy > &&  rhs)
inlineprotectednoexcept

Definition at line 1038 of file Synchronized.h.

1039  : parent_{exchange(rhs.parent_, nullptr)} {}
SynchronizedType * parent_
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
template<class SynchronizedType, class Mutex, class LockPolicy>
template<typename LockPolicyType >
folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::LockedPtrBase ( LockedPtrBase< SynchronizedType, Mutex, LockPolicyType > &&  rhs)
inlineprotectednoexcept

Templated move construct and assignment operators

These allow converting LockedPtr types that have the same unlocking policy to each other. This allows us to write code like

auto wlock = sync.wlock(); wlock.unlock();

auto ulock = sync.ulock(); wlock = ulock.moveFromUpgradeToWrite();

Definition at line 1058 of file Synchronized.h.

1060  : parent_{exchange(rhs.parent_, nullptr)} {}
SynchronizedType * parent_
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120

Member Function Documentation

template<class SynchronizedType, class Mutex, class LockPolicy>
template<typename LockPolicyLhs , typename LockPolicyRhs >
void folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::assignImpl ( LockedPtrBase< SynchronizedType, Mutex, LockPolicyLhs > &  lhs,
LockedPtrBase< SynchronizedType, Mutex, LockPolicyRhs > &  rhs 
)
inlineprotectednoexcept

Implementation for the assignment operator

Definition at line 1072 of file Synchronized.h.

1074  {
1075  if (lhs.parent_) {
1076  LockPolicy::unlock(lhs.parent_->mutex_);
1077  }
1078 
1079  lhs.parent_ = exchange(rhs.parent_, nullptr);
1080  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
template<class SynchronizedType, class Mutex, class LockPolicy>
static SynchronizedType* folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::getSynchronized ( UnlockerData  data)
inlinestaticprotected

Get a pointer to the Synchronized object from the UnlockerData.

In the generic case UnlockerData is just the Synchronized pointer, so we return it as is. (This function is more interesting in the std::mutex specialization below.)

Definition at line 1091 of file Synchronized.h.

1091  {
1092  return data;
1093  }
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
template<class SynchronizedType, class Mutex, class LockPolicy>
LockedPtrBase& folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::operator= ( LockedPtrBase< SynchronizedType, Mutex, LockPolicy > &&  rhs)
inlineprotectednoexcept

Definition at line 1040 of file Synchronized.h.

1040  {
1041  assignImpl(*this, rhs);
1042  return *this;
1043  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
void assignImpl(LockedPtrBase< SynchronizedType, Mutex, LockPolicyLhs > &lhs, LockedPtrBase< SynchronizedType, Mutex, LockPolicyRhs > &rhs) noexcept
template<class SynchronizedType, class Mutex, class LockPolicy>
template<typename LockPolicyType >
LockedPtrBase& folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::operator= ( LockedPtrBase< SynchronizedType, Mutex, LockPolicyType > &&  rhs)
inlineprotectednoexcept

Definition at line 1062 of file Synchronized.h.

1063  {
1064  assignImpl(*this, rhs);
1065  return *this;
1066  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
void assignImpl(LockedPtrBase< SynchronizedType, Mutex, LockPolicyLhs > &lhs, LockedPtrBase< SynchronizedType, Mutex, LockPolicyRhs > &rhs) noexcept
template<class SynchronizedType, class Mutex, class LockPolicy>
void folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::reacquireLock ( UnlockerData &&  data)
inlineprotected

Definition at line 1102 of file Synchronized.h.

1102  {
1103  DCHECK(parent_ == nullptr);
1104  parent_ = data;
1105  LockPolicy::lock(parent_->mutex_);
1106  }
void lock(LockableOne &one, LockableTwo &two, Lockables &...lockables)
SynchronizedType * parent_
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
template<class SynchronizedType, class Mutex, class LockPolicy>
UnlockerData folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::releaseLock ( )
inlineprotected

Definition at line 1095 of file Synchronized.h.

1095  {
1096  DCHECK(parent_ != nullptr);
1097  auto current = parent_;
1098  parent_ = nullptr;
1099  LockPolicy::unlock(current->mutex_);
1100  return current;
1101  }
SynchronizedType * parent_
int current
template<class SynchronizedType, class Mutex, class LockPolicy>
void folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::unlock ( )
inline

Unlock the synchronized data.

The LockedPtr can no longer be dereferenced after unlock() has been called. isValid() will return false on an unlocked LockedPtr.

unlock() can only be called on a LockedPtr that is valid.

Definition at line 1016 of file Synchronized.h.

1016  {
1017  DCHECK(parent_ != nullptr);
1018  LockPolicy::unlock(parent_->mutex_);
1019  parent_ = nullptr;
1020  }
SynchronizedType * parent_

Friends And Related Function Documentation

template<class SynchronizedType, class Mutex, class LockPolicy>
template<typename S , typename L >
friend class folly::LockedPtr
friend

Friend all instantiations of LockedPtr and LockedPtrBase

Definition at line 995 of file Synchronized.h.

template<class SynchronizedType, class Mutex, class LockPolicy>
friend class folly::ScopedUnlocker< SynchronizedType, LockPolicy >
friend

Definition at line 989 of file Synchronized.h.

template<class SynchronizedType, class Mutex, class LockPolicy>
template<typename S , typename M , typename L >
friend class LockedPtrBase
friend

Definition at line 997 of file Synchronized.h.

Member Data Documentation

template<class SynchronizedType, class Mutex, class LockPolicy>
SynchronizedType* folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >::parent_ = nullptr
protected

Definition at line 1108 of file Synchronized.h.


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