mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-10-11 12:03:04 +02:00
sync: simplify MaybeCheckNotHeld() definitions by using a template
Reduce 4 of the `MaybeCheckNotHeld()` definitions to 2 by using a template. This also makes the function usable for other [BasicLockable](https://en.cppreference.com/w/cpp/named_req/BasicLockable) types.
This commit is contained in:
14
src/sync.h
14
src/sync.h
@@ -249,14 +249,12 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove
|
|||||||
inline Mutex& MaybeCheckNotHeld(Mutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
|
inline Mutex& MaybeCheckNotHeld(Mutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
|
||||||
inline Mutex* MaybeCheckNotHeld(Mutex* cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
|
inline Mutex* MaybeCheckNotHeld(Mutex* cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
|
||||||
|
|
||||||
// When locking a GlobalMutex, just check it is not locked in the surrounding scope
|
// When locking a GlobalMutex or RecursiveMutex, just check it is not
|
||||||
inline GlobalMutex& MaybeCheckNotHeld(GlobalMutex& cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
|
// locked in the surrounding scope.
|
||||||
inline GlobalMutex* MaybeCheckNotHeld(GlobalMutex* cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
|
template <typename MutexType>
|
||||||
|
inline MutexType& MaybeCheckNotHeld(MutexType& m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }
|
||||||
// When locking a RecursiveMutex, it's okay to already hold the lock
|
template <typename MutexType>
|
||||||
// but check that it is not known to be locked in the surrounding scope anyway
|
inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }
|
||||||
inline RecursiveMutex& MaybeCheckNotHeld(RecursiveMutex& cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
|
|
||||||
inline RecursiveMutex* MaybeCheckNotHeld(RecursiveMutex* cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
|
|
||||||
|
|
||||||
#define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
|
#define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
|
||||||
#define LOCK2(cs1, cs2) \
|
#define LOCK2(cs1, cs2) \
|
||||||
|
Reference in New Issue
Block a user