Bitcoin Core  0.20.99
P2P Digital Currency
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:
This graph shows which files directly or indirectly include this file:

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  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...
 
struct  LockAssertion
 

Macros

#define AssertLockHeld(cs)   AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
 
#define AssertLockNotHeld(cs)   AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
 
#define REVERSE_LOCK(g)   typename std::decay<decltype(g)>::type::reverse_lock PASTE2(revlock, __COUNTER__)(g, #g, __FILE__, __LINE__)
 
#define LOCK(cs)   DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
 
#define LOCK2(cs1, cs2)
 
#define TRY_LOCK(cs, name)   DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
 
#define WAIT_LOCK(cs, name)   DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
 
#define ENTER_CRITICAL_SECTION(cs)
 
#define LEAVE_CRITICAL_SECTION(cs)
 
#define WITH_LOCK(cs, code)   [&] { LOCK(cs); code; }()
 Run code while locking a mutex. More...
 

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. More...
 
typedef AnnotatedMixin< std::mutex > Mutex
 Wrapped mutex: supports waiting but not recursive locking. More...
 
template<typename MutexArg >
using DebugLock = UniqueLock< typename std::remove_reference< typename std::remove_pointer< MutexArg >::type >::type >
 

Functions

void EnterCritical (const char *pszName, const char *pszFile, int nLine, void *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) ASSERT_EXCLUSIVE_LOCK(cs)
 
void AssertLockNotHeldInternal (const char *pszName, const char *pszFile, int nLine, void *cs)
 
void DeleteLock (void *cs)
 
bool LockStackEmpty ()
 

Macro Definition Documentation

◆ AssertLockHeld

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

Definition at line 77 of file sync.h.

◆ AssertLockNotHeld

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

Definition at line 78 of file sync.h.

◆ ENTER_CRITICAL_SECTION

#define ENTER_CRITICAL_SECTION (   cs)
Value:
{ \
EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
(cs).lock(); \
}
static void pool cs

Definition at line 235 of file sync.h.

◆ LEAVE_CRITICAL_SECTION

#define LEAVE_CRITICAL_SECTION (   cs)
Value:
{ \
(cs).unlock(); \
LeaveCritical(); \
}
static void pool cs

Definition at line 241 of file sync.h.

◆ LOCK

#define LOCK (   cs)    DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)

Definition at line 228 of file sync.h.

◆ LOCK2

#define LOCK2 (   cs1,
  cs2 
)
Value:
DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
Wrapper around std::unique_lock style lock for Mutex.
Definition: sync.h:131

Definition at line 229 of file sync.h.

◆ REVERSE_LOCK

#define REVERSE_LOCK (   g)    typename std::decay<decltype(g)>::type::reverse_lock PASTE2(revlock, __COUNTER__)(g, #g, __FILE__, __LINE__)

Definition at line 223 of file sync.h.

◆ TRY_LOCK

#define TRY_LOCK (   cs,
  name 
)    DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)

Definition at line 232 of file sync.h.

◆ WAIT_LOCK

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

Definition at line 233 of file sync.h.

◆ WITH_LOCK

#define WITH_LOCK (   cs,
  code 
)    [&] { 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);

Definition at line 255 of file sync.h.

Typedef Documentation

◆ DebugLock

template<typename MutexArg >
using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>

Definition at line 226 of file sync.h.

◆ Mutex

typedef AnnotatedMixin<std::mutex> Mutex

Wrapped mutex: supports waiting but not recursive locking.

Definition at line 123 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 120 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 72 of file sync.h.

◆ AssertLockNotHeldInternal()

void AssertLockNotHeldInternal ( const char *  pszName,
const char *  pszFile,
int  nLine,
void *  cs 
)
inline

Definition at line 73 of file sync.h.

◆ CheckLastCritical()

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

Definition at line 70 of file sync.h.

Here is the caller graph for this function:

◆ DeleteLock()

void DeleteLock ( void *  cs)
inline

Definition at line 74 of file sync.h.

Here is the caller graph for this function:

◆ EnterCritical()

void EnterCritical ( const char *  pszName,
const char *  pszFile,
int  nLine,
void *  cs,
bool  fTry = false 
)
inline

Definition at line 68 of file sync.h.

Here is the caller graph for this function:

◆ LeaveCritical()

void LeaveCritical ( )
inline

Definition at line 69 of file sync.h.

Here is the caller graph for this function:

◆ LockStackEmpty()

bool LockStackEmpty ( )
inline

Definition at line 75 of file sync.h.