![]() |
Bitcoin Core 30.99.0
P2P Digital Currency
|
#include <threadsafety.h>#include <util/macros.h>#include <cassert>#include <condition_variable>#include <mutex>#include <string>#include <thread>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< MutexType > |
| Wrapper around std::unique_lock style lock for MutexType. More... | |
| class | UniqueLock< MutexType >::reverse_lock |
| An RAII-style reverse lock. More... | |
Macros | |
| #define | AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
| #define | AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs) |
| #define | REVERSE_LOCK(g, cs) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, cs, #cs, __FILE__, __LINE__) |
| #define | LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) |
| #define | LOCK2(cs1, cs2) |
| #define | LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__ |
| #define | TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true) |
| #define | WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs)) |
| #define | WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { 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... | |
| using | Mutex = AnnotatedMixin< std::mutex > |
| Wrapped mutex: supports waiting but not recursive locking. More... | |
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) |
| Mutex & | MaybeCheckNotHeld (Mutex &cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) |
| Mutex * | MaybeCheckNotHeld (Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) |
| template<typename MutexType > | |
| MutexType & | MaybeCheckNotHeld (MutexType &m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) |
| template<typename MutexType > | |
| MutexType * | MaybeCheckNotHeld (MutexType *m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) |
| #define AssertLockHeld | ( | cs | ) | AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
| #define AssertLockNotHeld | ( | cs | ) | AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs) |
| #define LOCK | ( | cs | ) | UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) |
| #define LOCK2 | ( | cs1, | |
| cs2 | |||
| ) |
| #define LOCK_ARGS | ( | cs | ) | MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__ |
| #define REVERSE_LOCK | ( | g, | |
| cs | |||
| ) | typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, cs, #cs, __FILE__, __LINE__) |
| #define WITH_LOCK | ( | cs, | |
| code | |||
| ) | (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.
| using Mutex = AnnotatedMixin<std::mutex> |
| using RecursiveMutex = AnnotatedMixin<std::recursive_mutex> |
|
inline |
|
inline |
|
inline |
| void AssertLockNotHeldInternal | ( | const char * | pszName, |
| const char * | pszFile, | ||
| int | nLine, | ||
| MutexType * | cs | ||
| ) |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |