proxygen
|
#include <Synchronized.h>
Public Types | |
using | LockedPtr = typename Base::LockedPtr |
using | ConstLockedPtr = typename Base::ConstLockedPtr |
using | DataType = T |
using | MutexType = Mutex |
Public Member Functions | |
Synchronized ()=default | |
Synchronized (typename std::conditional< std::is_copy_constructible< T >::value, const Synchronized &, NonImplementedType >::type rhs) | |
Synchronized (Synchronized &&rhs) noexcept(nxMoveCtor) | |
Synchronized (const T &rhs) noexcept(nxCopyCtor) | |
Synchronized (T &&rhs) noexcept(nxMoveCtor) | |
template<typename... Args> | |
Synchronized (in_place_t, Args &&...args) | |
template<typename... DatumArgs, typename... MutexArgs> | |
Synchronized (std::piecewise_construct_t, std::tuple< DatumArgs... > datumArgs, std::tuple< MutexArgs... > mutexArgs) | |
Synchronized & | operator= (typename std::conditional< std::is_copy_constructible< T >::value &&std::is_move_assignable< T >::value, const Synchronized &, NonImplementedType >::type rhs) |
Synchronized & | operator= (Synchronized &&rhs) |
Synchronized & | operator= (const T &rhs) |
Synchronized & | operator= (T &&rhs) |
LockedPtr | contextualLock () |
ConstLockedPtr | contextualLock () const |
template<class Rep , class Period > | |
LockedPtr | contextualLock (const std::chrono::duration< Rep, Period > &timeout) |
template<class Rep , class Period > | |
ConstLockedPtr | contextualLock (const std::chrono::duration< Rep, Period > &timeout) const |
ConstLockedPtr | contextualRLock () const |
template<class Rep , class Period > | |
ConstLockedPtr | contextualRLock (const std::chrono::duration< Rep, Period > &timeout) const |
LockedPtr | operator-> () |
ConstLockedPtr | operator-> () const |
LockedPtr | timedAcquire (unsigned int milliseconds) |
ConstLockedPtr | timedAcquire (unsigned int milliseconds) const |
void | swap (Synchronized &rhs) |
void | swap (T &rhs) |
T | exchange (T &&rhs) |
void | copy (T *target) const |
T | copy () const |
Private Types | |
using | Base = SynchronizedBase< Synchronized< T, Mutex >, MutexLevelValue< Mutex >::value > |
Private Member Functions | |
Synchronized (const Synchronized &rhs, const ConstLockedPtr &) noexcept(nxCopyCtor) | |
Synchronized (Synchronized &&rhs, const LockedPtr &) noexcept(nxMoveCtor) | |
template<typename... DatumArgs, typename... MutexArgs, std::size_t... IndicesOne, std::size_t... IndicesTwo> | |
Synchronized (std::piecewise_construct_t, std::tuple< DatumArgs... > datumArgs, std::tuple< MutexArgs... > mutexArgs, index_sequence< IndicesOne... >, index_sequence< IndicesTwo... >) | |
Private Attributes | |
T | datum_ |
Mutex | mutex_ |
Static Private Attributes | |
static constexpr bool | nxCopyCtor |
static constexpr bool | nxMoveCtor |
Friends | |
template<class LockedType , class MutexType , class LockPolicy > | |
class | folly::LockedPtrBase |
template<class LockedType , class LockPolicy > | |
class | folly::LockedPtr |
Synchronized<T> encapsulates an object of type T (a "datum") paired with a mutex. The only way to access the datum is while the mutex is locked, and Synchronized makes it virtually impossible to do otherwise. The code that would access the datum in unsafe ways would look odd and convoluted, thus readily alerting the human reviewer. In contrast, the code that uses Synchronized<T> correctly looks simple and intuitive.
The second parameter must be a mutex type. Any mutex type supported by LockTraits<Mutex> can be used. By default any class with lock() and unlock() methods will work automatically. LockTraits can be specialized to teach Synchronized how to use other custom mutex types. See the documentation in LockTraits.h for additional details.
Supported mutexes that work by default include std::mutex, std::recursive_mutex, std::timed_mutex, std::recursive_timed_mutex, folly::SharedMutex, folly::RWSpinLock, and folly::SpinLock. Include LockTraitsBoost.h to get additional LockTraits specializations to support the following boost mutex types: boost::mutex, boost::recursive_mutex, boost::shared_mutex, boost::timed_mutex, and boost::recursive_timed_mutex.
Definition at line 438 of file Synchronized.h.
|
private |
Definition at line 443 of file Synchronized.h.
using folly::Synchronized< T, Mutex >::ConstLockedPtr = typename Base::ConstLockedPtr |
Definition at line 454 of file Synchronized.h.
using folly::Synchronized< T, Mutex >::DataType = T |
Definition at line 455 of file Synchronized.h.
using folly::Synchronized< T, Mutex >::LockedPtr = typename Base::LockedPtr |
Definition at line 453 of file Synchronized.h.
using folly::Synchronized< T, Mutex >::MutexType = Mutex |
Definition at line 456 of file Synchronized.h.
|
default |
Default constructor leaves both members call their own default constructor.
|
inline |
Copy constructor; deprecated
Enabled only when the data type is copy-constructible.
Takes a shared-or-exclusive lock on the source mutex while performing the copy-construction of the destination data from the source data. No lock is taken on the destination mutex.
May throw even when the data type is is nothrow-copy-constructible because acquiring a lock may throw.
Definition at line 477 of file Synchronized.h.
|
inlinenoexcept |
Move constructor; deprecated
Move-constructs from the source data without locking either the source or the destination mutex.
Semantically, assumes that the source object is a true rvalue and therefore that no synchronization is required for accessing it.
Definition at line 492 of file Synchronized.h.
|
inlineexplicitnoexcept |
Constructor taking a datum as argument copies it. There is no need to lock the constructing object.
Definition at line 499 of file Synchronized.h.
|
inlineexplicitnoexcept |
Constructor taking a datum rvalue as argument moves it. Again, there is no need to lock the constructing object.
Definition at line 505 of file Synchronized.h.
|
inlineexplicit |
Lets you construct non-movable types in-place. Use the constexpr instance in_place
as the first argument.
Definition at line 513 of file Synchronized.h.
|
inline |
Lets you construct the synchronized object and also pass construction parameters to the underlying mutex if desired
Definition at line 521 of file Synchronized.h.
|
inlineprivatenoexcept |
Helper constructors to enable Synchronized for non-default constructible types T. Guards are created in actual public constructors and are alive for the time required to construct the object
Definition at line 746 of file Synchronized.h.
|
inlineprivatenoexcept |
Definition at line 751 of file Synchronized.h.
|
inlineprivate |
Definition at line 760 of file Synchronized.h.
|
inline |
Acquire an appropriate lock based on the context.
If the mutex is a shared mutex, and the Synchronized instance is const, this acquires a shared lock. Otherwise this acquires an exclusive lock.
In general, prefer using the explicit rlock() and wlock() methods for read-write locks, and lock() for purely exclusive locks.
contextualLock() is primarily intended for use in other template functions that do not necessarily know the lock type.
Definition at line 601 of file Synchronized.h.
Referenced by folly::sync_tests::testBasicImpl(), folly::sync_tests::testConcurrency(), folly::sync_tests::testTimed(), folly::sync_tests::testTimedShared(), folly::sync_tests::testTimedSynchronized(), and folly::sync_tests::testTimedSynchronizedWithConst().
|
inline |
Definition at line 604 of file Synchronized.h.
|
inline |
Definition at line 608 of file Synchronized.h.
|
inline |
Definition at line 612 of file Synchronized.h.
|
inline |
contextualRLock() acquires a read lock if the mutex type is shared, or a regular exclusive lock for non-shared mutex types.
contextualRLock() when you know that you prefer a read lock (if available), even if the Synchronized<T> object itself is non-const.
Definition at line 623 of file Synchronized.h.
Referenced by folly::sync_tests::testTimed(), folly::sync_tests::testTimedShared(), folly::sync_tests::testTimedSynchronized(), and folly::sync_tests::testTimedSynchronizedWithConst().
|
inline |
Definition at line 627 of file Synchronized.h.
|
inline |
Copies datum to a given target.
Definition at line 721 of file Synchronized.h.
Referenced by folly::observer::detail::ObserverCreatorContext< Observable, Traits >::get(), folly::Synchronized< std::vector< detail::folly::detail::TypeDescriptor >, folly::SharedMutexImpl >::operator=(), folly::observer_detail::Core::refresh(), folly::sync_tests::testConstCopy(), folly::sync_tests::testExchange(), and folly::observer::detail::ObserverCreatorContext< Observable, Traits >::~ObserverCreatorContext().
|
inline |
Returns a fresh copy of the datum.
Definition at line 729 of file Synchronized.h.
|
inline |
Assign another datum and return the original value. Recommended because it keeps the mutex held only briefly.
Definition at line 713 of file Synchronized.h.
Referenced by folly::sync_tests::testExchange().
|
inline |
This accessor offers a LockedPtr. In turn, LockedPtr offers operator-> returning a pointer to T. The operator-> keeps expanding until it reaches a pointer, so syncobj->foo() will lock the object and call foo() against it.
NOTE: This API is planned to be deprecated in an upcoming diff. Prefer using lock(), wlock(), or rlock() instead.
Definition at line 641 of file Synchronized.h.
|
inline |
Obtain a ConstLockedPtr.
NOTE: This API is planned to be deprecated in an upcoming diff. Prefer using lock(), wlock(), or rlock() instead.
Definition at line 651 of file Synchronized.h.
|
inline |
Copy assignment operator; deprecated
Enabled only when the data type is copy-constructible and move-assignable.
Move-assigns from a copy of the source data.
Takes a shared-or-exclusive lock on the source mutex while copying the source data to a temporary. Takes an exclusive lock on the destination mutex while move-assigning from the temporary.
This technique consts an extra temporary but avoids the need to take locks on both mutexes together.
Definition at line 545 of file Synchronized.h.
|
inline |
Move assignment operator; deprecated
Takes an exclusive lock on the destination mutex while move-assigning the destination data from the source data. The source mutex is not locked or otherwise accessed.
Semantically, assumes that the source object is a true rvalue and therefore that no synchronization is required for accessing it.
Definition at line 563 of file Synchronized.h.
|
inline |
|
inline |
|
inline |
Swaps with another Synchronized. Protected against self-swap. Only data is swapped. Locks are acquired in increasing address order.
Definition at line 684 of file Synchronized.h.
Referenced by folly::VirtualEventBase::destroyImpl(), folly::LoggerDB::finishConfigUpdate(), folly::Synchronized< std::vector< detail::folly::detail::TypeDescriptor >, folly::SharedMutexImpl >::swap(), folly::swap(), folly::sync_tests::testAcquireLocked(), folly::sync_tests::testAcquireLockedWithConst(), folly::sync_tests::testConcurrency(), folly::sync_tests::testDualLocking(), folly::sync_tests::testDualLockingWithConst(), folly::sync_tests::testTimed(), folly::sync_tests::testTimedShared(), folly::sync_tests::testTimedSynchronized(), folly::sync_tests::testTimedSynchronizedWithConst(), and folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateValue().
|
inline |
Swap with another datum. Recommended because it keeps the mutex held only briefly.
Definition at line 702 of file Synchronized.h.
|
inline |
Attempts to acquire for a given number of milliseconds. If acquisition is unsuccessful, the returned LockedPtr is nullptr.
NOTE: This API is deprecated. Use lock(), wlock(), or rlock() instead. In the future it will be marked with a deprecation attribute to emit build-time warnings, and then it will be removed entirely.
Definition at line 663 of file Synchronized.h.
|
inline |
Attempts to acquire for a given number of milliseconds. If acquisition is unsuccessful, the returned ConstLockedPtr is nullptr.
NOTE: This API is deprecated. Use lock(), wlock(), or rlock() instead. In the future it will be marked with a deprecation attribute to emit build-time warnings, and then it will be removed entirely.
Definition at line 675 of file Synchronized.h.
|
friend |
Definition at line 738 of file Synchronized.h.
|
friend |
Definition at line 736 of file Synchronized.h.
|
private |
Definition at line 770 of file Synchronized.h.
Referenced by folly::Synchronized< std::vector< detail::folly::detail::TypeDescriptor >, folly::SharedMutexImpl >::swap().
|
mutableprivate |
Definition at line 771 of file Synchronized.h.
|
staticprivate |
Definition at line 444 of file Synchronized.h.
|
staticprivate |
Definition at line 446 of file Synchronized.h.