proxygen
Synchronized.h File Reference
#include <folly/Function.h>
#include <folly/Likely.h>
#include <folly/LockTraits.h>
#include <folly/Preprocessor.h>
#include <folly/SharedMutex.h>
#include <folly/Traits.h>
#include <folly/Utility.h>
#include <folly/container/Foreach.h>
#include <folly/functional/ApplyTuple.h>
#include <glog/logging.h>
#include <array>
#include <mutex>
#include <tuple>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Classes

class  folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >
 
class  folly::LockedPtr< SynchronizedType, LockPolicy >
 
class  folly::SynchronizedBase< Subclass, level >
 
class  folly::SynchronizedBase< Subclass, detail::MutexLevel::SHARED >
 
class  folly::SynchronizedBase< Subclass, detail::MutexLevel::UPGRADE >
 
class  folly::SynchronizedBase< Subclass, detail::MutexLevel::UNIQUE >
 
struct  folly::Synchronized< T, Mutex >
 
class  folly::ScopedUnlocker< SynchronizedType, LockPolicy >
 
class  folly::detail::SynchronizedLocker< Synchronized, LockFunc, TryLockFunc, Args >
 
class  folly::LockedPtrBase< SynchronizedType, Mutex, LockPolicy >
 
class  folly::LockedPtrBase< SynchronizedType, std::mutex, LockPolicy >
 
class  folly::ScopedUnlocker< SynchronizedType, LockPolicy >
 
class  folly::LockedPtr< SynchronizedType, LockPolicy >
 

Namespaces

 folly
 —— Concurrent Priority Queue Implementation ——
 
 folly::detail
 

Macros

#define SYNCHRONIZED_VAR(var)   FB_CONCATENATE(SYNCHRONIZED_##var##_, __LINE__)
 
#define SYNCHRONIZED(...)
 
#define TIMED_SYNCHRONIZED(timeout, ...)
 
#define SYNCHRONIZED_CONST(...)
 
#define TIMED_SYNCHRONIZED_CONST(timeout, ...)
 
#define SYNCHRONIZED_DUAL(n1, e1, n2, e2)
 

Typedefs

template<class Mutex >
using folly::MutexLevelValue = detail::MutexLevelValueImpl< true, LockTraits< Mutex >::is_shared, LockTraits< Mutex >::is_upgrade >
 
template<class SynchronizedType >
using folly::detail::SynchronizedDataType = typename std::conditional< std::is_const< SynchronizedType >::value, typename SynchronizedType::DataType const, typename SynchronizedType::DataType >::type
 
template<class SynchronizedType >
using folly::detail::LockedPtrType = typename std::conditional< std::is_const< SynchronizedType >::value, typename SynchronizedType::ConstLockedPtr, typename SynchronizedType::LockedPtr >::type
 

Functions

template<typename Synchronized , typename LockFunc , typename TryLockFunc , typename... Args>
auto folly::detail::makeSynchronizedLocker (Synchronized &synchronized, LockFunc &&lockFunc, TryLockFunc &&tryLockFunc, Args &&...args)
 
template<typename... SynchronizedLocker>
auto folly::detail::lock (SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
 
template<typename Synchronized , typename... Args>
auto folly::detail::wlock (Synchronized &synchronized, Args &&...args)
 
template<typename Synchronized , typename... Args>
auto folly::detail::rlock (Synchronized &synchronized, Args &&...args)
 
template<typename Synchronized , typename... Args>
auto folly::detail::ulock (Synchronized &synchronized, Args &&...args)
 
template<typename Synchronized , typename... Args>
auto folly::detail::lock (Synchronized &synchronized, Args &&...args)
 
template<typename D , typename M , typename... Args>
auto folly::wlock (Synchronized< D, M > &synchronized, Args &&...args)
 
template<typename Data , typename Mutex , typename... Args>
auto folly::rlock (const Synchronized< Data, Mutex > &synchronized, Args &&...args)
 
template<typename D , typename M , typename... Args>
auto folly::ulock (Synchronized< D, M > &synchronized, Args &&...args)
 
template<typename D , typename M , typename... Args>
auto folly::lock (Synchronized< D, M > &synchronized, Args &&...args)
 
template<typename D , typename M , typename... Args>
auto folly::lock (const Synchronized< D, M > &synchronized, Args &&...args)
 
template<typename Func , typename... SynchronizedLockers>
decltype(auto) folly::synchronized (Func &&func, SynchronizedLockers &&...lockers)
 
template<typename LockableOne , typename LockableTwo , typename... Lockables>
void folly::lock (LockableOne &one, LockableTwo &two, Lockables &...lockables)
 
template<class Sync1 , class Sync2 >
std::tuple< detail::LockedPtrType< Sync1 >, detail::LockedPtrType< Sync2 > > folly::acquireLocked (Sync1 &l1, Sync2 &l2)
 
template<class Sync1 , class Sync2 >
std::pair< detail::LockedPtrType< Sync1 >, detail::LockedPtrType< Sync2 > > folly::acquireLockedPair (Sync1 &l1, Sync2 &l2)
 
template<class T , class M >
void folly::swap (Synchronized< T, M > &lhs, Synchronized< T, M > &rhs)
 

Macro Definition Documentation

#define SYNCHRONIZED (   ...)
Value:
FOLLY_MSVC_DISABLE_WARNING(4189) /* initialized but unreferenced */ \
FOLLY_MSVC_DISABLE_WARNING(4456) /* declaration hides local */ \
FOLLY_MSVC_DISABLE_WARNING(4457) /* declaration hides parameter */ \
FOLLY_MSVC_DISABLE_WARNING(4458) /* declaration hides member */ \
FOLLY_MSVC_DISABLE_WARNING(4459) /* declaration hides global */ \
if (bool SYNCHRONIZED_VAR(state) = false) { \
} else \
for (auto SYNCHRONIZED_VAR(lockedPtr) = \
(FB_VA_GLUE(FB_ARG_2_OR_1, (__VA_ARGS__))).operator->(); \
for (auto& FB_VA_GLUE(FB_ARG_1, (__VA_ARGS__)) = \
*SYNCHRONIZED_VAR(lockedPtr).operator->(); \
#define FOLLY_GNU_DISABLE_WARNING(warningName)
Definition: Portability.h:180
#define FOLLY_POP_WARNING
Definition: Portability.h:179
#define FB_ARG_2_OR_1(...)
Definition: Preprocessor.h:54
#define FB_VA_GLUE(a, b)
Definition: Preprocessor.h:27
#define SYNCHRONIZED_VAR(var)
#define FB_ARG_1(a,...)
Definition: Preprocessor.h:41
#define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
Definition: Portability.h:183
if(FOLLY_USE_SYMBOLIZER) add_library(folly_exception_tracer_base ExceptionTracer.cpp StackTrace.cpp) apply_folly_compile_options_to_target(folly_exception_tracer_base) target_link_libraries(folly_exception_tracer_base PUBLIC folly) add_library(folly_exception_tracer ExceptionStackTraceLib.cpp ExceptionTracerLib.cpp) apply_folly_compile_options_to_target(folly_exception_tracer) target_link_libraries(folly_exception_tracer PUBLIC folly_exception_tracer_base) add_library(folly_exception_counter ExceptionCounterLib.cpp) apply_folly_compile_options_to_target(folly_exception_counter) target_link_libraries(folly_exception_counter PUBLIC folly_exception_tracer) install(FILES ExceptionAbi.h ExceptionCounterLib.h ExceptionTracer.h ExceptionTracerLib.h StackTrace.h DESTINATION $
Definition: CMakeLists.txt:1
state
Definition: http_parser.c:272

SYNCHRONIZED is the main facility that makes Synchronized<T> helpful. It is a pseudo-statement that introduces a scope where the object is locked. Inside that scope you get to access the unadorned datum.

Example:

Synchronized<vector<int>> svector; ... SYNCHRONIZED (svector) { ... use svector as a vector<int> ... } or SYNCHRONIZED (v, svector) { ... use v as a vector<int> ... }

Refer to folly/docs/Synchronized.md for a detailed explanation and more examples.

Definition at line 1686 of file Synchronized.h.

Referenced by folly::sync_tests::testDeprecated().

#define SYNCHRONIZED_CONST (   ...)
Value:
FB_VA_GLUE(FB_ARG_1, (__VA_ARGS__)), \
as_const(FB_VA_GLUE(FB_ARG_2_OR_1, (__VA_ARGS__))))
#define FB_ARG_2_OR_1(...)
Definition: Preprocessor.h:54
#define FB_VA_GLUE(a, b)
Definition: Preprocessor.h:27
#define FB_ARG_1(a,...)
Definition: Preprocessor.h:41
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::as_const_fn as_const
#define SYNCHRONIZED(...)

Similar to SYNCHRONIZED, but only uses a read lock.

Definition at line 1724 of file Synchronized.h.

Referenced by folly::sync_tests::testDeprecated().

#define SYNCHRONIZED_DUAL (   n1,
  e1,
  n2,
  e2 
)
Value:
if (bool SYNCHRONIZED_VAR(state) = false) { \
} else \
for (auto SYNCHRONIZED_VAR(ptrs) = acquireLockedPair(e1, e2); \
for (auto& n1 = *SYNCHRONIZED_VAR(ptrs).first; !SYNCHRONIZED_VAR(state); \
for (auto& n2 = *SYNCHRONIZED_VAR(ptrs).second; \
#define SYNCHRONIZED_VAR(var)
std::pair< detail::LockedPtrType< Sync1 >, detail::LockedPtrType< Sync2 > > acquireLockedPair(Sync1 &l1, Sync2 &l2)
state
Definition: http_parser.c:272

Synchronizes two Synchronized objects (they may encapsulate different data). Synchronization is done in increasing address of object order, so there is no deadlock risk.

Definition at line 1743 of file Synchronized.h.

Referenced by folly::sync_tests::testDualLocking(), and folly::sync_tests::testDualLockingWithConst().

#define SYNCHRONIZED_VAR (   var)    FB_CONCATENATE(SYNCHRONIZED_##var##_, __LINE__)

Disambiguate the name var by concatenating the line number of the original point of expansion. This avoids shadowing warnings for nested SYNCHRONIZEDs. The name is consistent if used multiple times within another macro. Only for internal use.

Definition at line 1667 of file Synchronized.h.

#define TIMED_SYNCHRONIZED (   timeout,
  ... 
)
Value:
if (bool SYNCHRONIZED_VAR(state) = false) { \
} else \
for (auto SYNCHRONIZED_VAR(lockedPtr) = \
(FB_VA_GLUE(FB_ARG_2_OR_1, (__VA_ARGS__))).timedAcquire(timeout); \
for (auto FB_VA_GLUE(FB_ARG_1, (__VA_ARGS__)) = \
(!SYNCHRONIZED_VAR(lockedPtr) \
? nullptr \
: SYNCHRONIZED_VAR(lockedPtr).operator->()); \
#define FB_ARG_2_OR_1(...)
Definition: Preprocessor.h:54
#define FB_VA_GLUE(a, b)
Definition: Preprocessor.h:27
#define SYNCHRONIZED_VAR(var)
#define FB_ARG_1(a,...)
Definition: Preprocessor.h:41
state
Definition: http_parser.c:272

Definition at line 1707 of file Synchronized.h.

Referenced by folly::sync_tests::testTimedSynchronized().

#define TIMED_SYNCHRONIZED_CONST (   timeout,
  ... 
)
Value:
timeout, \
FB_VA_GLUE(FB_ARG_1, (__VA_ARGS__)), \
as_const(FB_VA_GLUE(FB_ARG_2_OR_1, (__VA_ARGS__))))
#define FB_ARG_2_OR_1(...)
Definition: Preprocessor.h:54
#define FB_VA_GLUE(a, b)
Definition: Preprocessor.h:27
#define FB_ARG_1(a,...)
Definition: Preprocessor.h:41
#define TIMED_SYNCHRONIZED(timeout,...)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::as_const_fn as_const

Similar to TIMED_SYNCHRONIZED, but only uses a read lock.

Definition at line 1732 of file Synchronized.h.

Referenced by folly::sync_tests::testTimedSynchronizedWithConst().