Bitcoin Core  0.18.99
P2P Digital Currency
Classes | Macros | Typedefs | Functions
sync.h File Reference
#include <threadsafety.h>
#include <condition_variable>
#include <thread>
#include <mutex>
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  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 PASTE(x, y)   x ## y
 
#define PASTE2(x, y)   PASTE(x, y)
 
#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::recursive_mutex > CCriticalSection
 
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

static void EnterCritical (const char *pszName, const char *pszFile, int nLine, void *cs, bool fTry=false)
 
static void LeaveCritical ()
 
static void AssertLockHeldInternal (const char *pszName, const char *pszFile, int nLine, void *cs) ASSERT_EXCLUSIVE_LOCK(cs)
 
static void AssertLockNotHeldInternal (const char *pszName, const char *pszFile, int nLine, void *cs)
 
static void DeleteLock (void *cs)
 

Macro Definition Documentation

◆ AssertLockHeld

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

Definition at line 70 of file sync.h.

◆ AssertLockNotHeld

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

Definition at line 71 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 189 of file sync.h.

◆ LEAVE_CRITICAL_SECTION

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

Definition at line 195 of file sync.h.

◆ LOCK

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

Definition at line 182 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:119

Definition at line 183 of file sync.h.

◆ PASTE

#define PASTE (   x,
 
)    x ## y

Definition at line 179 of file sync.h.

◆ PASTE2

#define PASTE2 (   x,
 
)    PASTE(x, y)

Definition at line 180 of file sync.h.

◆ TRY_LOCK

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

Definition at line 186 of file sync.h.

◆ WAIT_LOCK

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

Definition at line 187 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 209 of file sync.h.

Typedef Documentation

◆ CCriticalSection

typedef AnnotatedMixin<std::recursive_mutex> CCriticalSection

Definition at line 108 of file sync.h.

◆ DebugLock

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

Definition at line 177 of file sync.h.

◆ Mutex

typedef AnnotatedMixin<std::mutex> Mutex

Wrapped mutex: supports waiting but not recursive locking.

Definition at line 111 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 107 of file sync.h.

Function Documentation

◆ AssertLockHeldInternal()

static void AssertLockHeldInternal ( const char *  pszName,
const char *  pszFile,
int  nLine,
void *  cs 
)
inlinestatic

Definition at line 66 of file sync.h.

◆ AssertLockNotHeldInternal()

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

Definition at line 67 of file sync.h.

◆ DeleteLock()

static void DeleteLock ( void *  cs)
inlinestatic

Definition at line 68 of file sync.h.

Here is the caller graph for this function:

◆ EnterCritical()

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

Definition at line 64 of file sync.h.

Here is the caller graph for this function:

◆ LeaveCritical()

static void LeaveCritical ( )
inlinestatic

Definition at line 65 of file sync.h.

Here is the caller graph for this function: