Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Functions
sync.h File Reference
#include <threadsafety.h>
#include <util/macros.h>
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
Include dependency graph for sync.h:

Go to the source code of this file.

Classes

class  AnnotatedMixin< PARENT >
 Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of the mutex API. More...
 
class  GlobalMutex
 Different type to mark Mutex at global scope. More...
 
class  UniqueLock< Mutex, Base >
 Wrapper around std::unique_lock style lock for Mutex. More...
 
class  UniqueLock< Mutex, Base >::reverse_lock
 An RAII-style reverse lock. More...
 
class  CSemaphore
 
class  CSemaphoreGrant
 RAII-style semaphore lock. More...
 

Macros

#define AssertLockHeld(cs)   AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
 
#define AssertLockNotHeld(cs)    AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
 
#define REVERSE_LOCK(g)
 
#define LOCK(cs)
 
#define LOCK2(cs1, cs2)
 
#define TRY_LOCK(cs, name)
 
#define WAIT_LOCK(cs, name)    DebugLock<decltype(cs)> name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
 
#define ENTER_CRITICAL_SECTION(cs)
 
#define LEAVE_CRITICAL_SECTION(cs)
 
#define WITH_LOCK(cs, code)
 Run code while locking a mutex.
 

Typedefs

using RecursiveMutex = AnnotatedMixin< std::recursive_mutex >
 Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the recursive lock by default.
 
using Mutex = AnnotatedMixin< std::mutex >
 Wrapped mutex: supports waiting but not recursive locking.
 
template<typename MutexArg >
using DebugLock = UniqueLock< typename std::remove_reference< typename std::remove_pointer< MutexArg >::type >::type >
 

Functions

template<typename MutexType >
void EnterCritical (const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
 
void LeaveCritical ()
 
void CheckLastCritical (void *cs, std::string &lockname, const char *guardname, const char *file, int line)
 
template<typename MutexType >
void AssertLockHeldInternal (const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
template<typename MutexType >
void AssertLockNotHeldInternal (const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)
 
void DeleteLock (void *cs)
 
bool LockStackEmpty ()
 
void AssertLockNotHeldInline (const char *name, const char *file, int line, Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void AssertLockNotHeldInline (const char *name, const char *file, int line, GlobalMutex *cs) LOCKS_EXCLUDED(cs)
 
MutexMaybeCheckNotHeld (Mutex &cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs)
 
MutexMaybeCheckNotHeld (Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs)
 
GlobalMutexMaybeCheckNotHeld (GlobalMutex &cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs)
 
GlobalMutexMaybeCheckNotHeld (GlobalMutex *cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs)
 

Macro Definition Documentation

◆ AssertLockHeld

#define AssertLockHeld (   cs)    AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)

Definition at line 146 of file sync.h.

◆ AssertLockNotHeld

#define AssertLockNotHeld (   cs)     AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)

Definition at line 163 of file sync.h.

◆ ENTER_CRITICAL_SECTION

#define ENTER_CRITICAL_SECTION (   cs)
Value:
{ \
(cs).lock(); \
}
static void pool cs
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85

Definition at line 320 of file sync.h.

◆ LEAVE_CRITICAL_SECTION

#define LEAVE_CRITICAL_SECTION (   cs)
Value:
{ \
std::string lockname; \
CheckLastCritical((void *)(&cs), lockname, #cs, __FILE__, __LINE__); \
(cs).unlock(); \
}

Definition at line 326 of file sync.h.

◆ LOCK

#define LOCK (   cs)
Value:
Wrapper around std::unique_lock style lock for Mutex.
Definition sync.h:168
#define UNIQUE_LOG_NAME(name)
Definition macros.h:11

Definition at line 306 of file sync.h.

◆ LOCK2

#define LOCK2 (   cs1,
  cs2 
)
Value:
Mutex & MaybeCheckNotHeld(Mutex &cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs)
Definition sync.h:275

Definition at line 309 of file sync.h.

◆ REVERSE_LOCK

#define REVERSE_LOCK (   g)
Value:
typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_LOG_NAME( \

Definition at line 265 of file sync.h.

◆ TRY_LOCK

#define TRY_LOCK (   cs,
  name 
)
Value:
__LINE__, true)
const char * name
Definition rest.cpp:47

Definition at line 314 of file sync.h.

◆ WAIT_LOCK

#define WAIT_LOCK (   cs,
  name 
)     DebugLock<decltype(cs)> name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)

Definition at line 317 of file sync.h.

◆ WITH_LOCK

#define WITH_LOCK (   cs,
  code 
)
Value:
(MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { \
LOCK(cs); \
code; \
}())

Run code while locking a mutex.

Examples:

WITH_LOCK(cs, shared_val = shared_val + 1);

int val = WITH_LOCK(cs, return shared_val);

Note:

Since the return type deduction follows that of decltype(auto), while the deduced type of:

WITH_LOCK(cs, return {int i = 1; return i;});

is int, the deduced type of:

WITH_LOCK(cs, return {int j = 1; return (j);});

is &int, a reference to a local variable

The above is detectable at compile-time with the -Wreturn-local-addr flag in gcc and the -Wreturn-stack-address flag in clang, both enabled by default.

Definition at line 357 of file sync.h.

Typedef Documentation

◆ DebugLock

Definition at line 270 of file sync.h.

◆ Mutex

using Mutex = AnnotatedMixin<std::mutex>

Wrapped mutex: supports waiting but not recursive locking.

Definition at line 132 of file sync.h.

◆ RecursiveMutex

using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>

Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the recursive lock by default.

Definition at line 129 of file sync.h.

Function Documentation

◆ AssertLockHeldInternal()

template<typename MutexType >
void AssertLockHeldInternal ( const char pszName,
const char pszFile,
int  nLine,
MutexType cs 
)
inline

Definition at line 87 of file sync.h.

◆ AssertLockNotHeldInline() [1/2]

void AssertLockNotHeldInline ( const char name,
const char file,
int  line,
GlobalMutex cs 
)
inline

Definition at line 158 of file sync.h.

Here is the call graph for this function:

◆ AssertLockNotHeldInline() [2/2]

void AssertLockNotHeldInline ( const char name,
const char file,
int  line,
Mutex cs 
)
inline

Definition at line 148 of file sync.h.

Here is the call graph for this function:

◆ AssertLockNotHeldInternal()

template<typename MutexType >
void AssertLockNotHeldInternal ( const char pszName,
const char pszFile,
int  nLine,
MutexType cs 
)

Definition at line 91 of file sync.h.

Here is the caller graph for this function:

◆ CheckLastCritical()

void CheckLastCritical ( void cs,
std::string &  lockname,
const char guardname,
const char file,
int  line 
)
inline

Definition at line 83 of file sync.h.

Here is the caller graph for this function:

◆ DeleteLock()

void DeleteLock ( void cs)
inline

Definition at line 93 of file sync.h.

Here is the caller graph for this function:

◆ EnterCritical()

template<typename MutexType >
void EnterCritical ( const char pszName,
const char pszFile,
int  nLine,
MutexType cs,
bool  fTry = false 
)
inline

Definition at line 80 of file sync.h.

Here is the caller graph for this function:

◆ LeaveCritical()

void LeaveCritical ( )
inline

Definition at line 82 of file sync.h.

Here is the caller graph for this function:

◆ LockStackEmpty()

bool LockStackEmpty ( )
inline

Definition at line 94 of file sync.h.

◆ MaybeCheckNotHeld() [1/4]

GlobalMutex & MaybeCheckNotHeld ( GlobalMutex cs)
inline

Definition at line 286 of file sync.h.

◆ MaybeCheckNotHeld() [2/4]

GlobalMutex * MaybeCheckNotHeld ( GlobalMutex cs)
inline

Definition at line 290 of file sync.h.

◆ MaybeCheckNotHeld() [3/4]

Mutex & MaybeCheckNotHeld ( Mutex cs)
inline

Definition at line 275 of file sync.h.

◆ MaybeCheckNotHeld() [4/4]

Mutex * MaybeCheckNotHeld ( Mutex cs)
inline

Definition at line 279 of file sync.h.