1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include <threadsafety.h>
11 #include <condition_variable>
16 ////////////////////////////////////////////////
18 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
20 ////////////////////////////////////////////////
23 CCriticalSection mutex;
24 std::recursive_mutex mutex;
27 std::unique_lock<std::recursive_mutex> criticalblock(mutex);
29 LOCK2(mutex1, mutex2);
30 std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
31 std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
33 TRY_LOCK(mutex, name);
34 std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
36 ENTER_CRITICAL_SECTION(mutex); // no RAII
39 LEAVE_CRITICAL_SECTION(mutex); // no RAII
43 ///////////////////////////////
45 // THE ACTUAL IMPLEMENTATION //
47 ///////////////////////////////
50 * Template mixin that adds -Wthread-safety locking
51 * annotations to a subset of the mutex API.
53 template <typename PARENT
>
54 class LOCKABLE AnnotatedMixin
: public PARENT
57 void lock() EXCLUSIVE_LOCK_FUNCTION()
62 void unlock() UNLOCK_FUNCTION()
67 bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
69 return PARENT::try_lock();
73 #ifdef DEBUG_LOCKORDER
74 void EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false);
76 std::string
LocksHeld();
77 void AssertLockHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
);
78 void AssertLockNotHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
);
79 void DeleteLock(void* cs
);
81 void static inline EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false) {}
82 void static inline LeaveCritical() {}
83 void static inline AssertLockHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
) {}
84 void static inline AssertLockNotHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
) {}
85 void static inline DeleteLock(void* cs
) {}
87 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
88 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
91 * Wrapped mutex: supports recursive locking, but no waiting
92 * TODO: We should move away from using the recursive lock by default.
94 class CCriticalSection
: public AnnotatedMixin
<std::recursive_mutex
>
98 DeleteLock((void*)this);
102 /** Wrapped mutex: supports waiting but not recursive locking */
103 typedef AnnotatedMixin
<std::mutex
> CWaitableCriticalSection
;
105 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
106 typedef std::condition_variable CConditionVariable
;
108 /** Just a typedef for std::unique_lock, can be wrapped later if desired */
109 typedef std::unique_lock
<std::mutex
> WaitableLock
;
111 #ifdef DEBUG_LOCKCONTENTION
112 void PrintLockContention(const char* pszName
, const char* pszFile
, int nLine
);
115 /** Wrapper around std::unique_lock<CCriticalSection> */
116 class SCOPED_LOCKABLE CCriticalBlock
119 std::unique_lock
<CCriticalSection
> lock
;
121 void Enter(const char* pszName
, const char* pszFile
, int nLine
)
123 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()));
124 #ifdef DEBUG_LOCKCONTENTION
125 if (!lock
.try_lock()) {
126 PrintLockContention(pszName
, pszFile
, nLine
);
129 #ifdef DEBUG_LOCKCONTENTION
134 bool TryEnter(const char* pszName
, const char* pszFile
, int nLine
)
136 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()), true);
138 if (!lock
.owns_lock())
140 return lock
.owns_lock();
144 CCriticalBlock(CCriticalSection
& mutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(mutexIn
) : lock(mutexIn
, std::defer_lock
)
147 TryEnter(pszName
, pszFile
, nLine
);
149 Enter(pszName
, pszFile
, nLine
);
152 CCriticalBlock(CCriticalSection
* pmutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn
)
154 if (!pmutexIn
) return;
156 lock
= std::unique_lock
<CCriticalSection
>(*pmutexIn
, std::defer_lock
);
158 TryEnter(pszName
, pszFile
, nLine
);
160 Enter(pszName
, pszFile
, nLine
);
163 ~CCriticalBlock() UNLOCK_FUNCTION()
165 if (lock
.owns_lock())
171 return lock
.owns_lock();
175 #define PASTE(x, y) x ## y
176 #define PASTE2(x, y) PASTE(x, y)
178 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
179 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
180 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
182 #define ENTER_CRITICAL_SECTION(cs) \
184 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
188 #define LEAVE_CRITICAL_SECTION(cs) \
197 std::condition_variable condition
;
202 explicit CSemaphore(int init
) : value(init
) {}
206 std::unique_lock
<std::mutex
> lock(mutex
);
207 condition
.wait(lock
, [&]() { return value
>= 1; });
213 std::lock_guard
<std::mutex
> lock(mutex
);
223 std::lock_guard
<std::mutex
> lock(mutex
);
226 condition
.notify_one();
230 /** RAII-style semaphore lock */
231 class CSemaphoreGrant
256 if (!fHaveGrant
&& sem
->try_wait())
261 void MoveTo(CSemaphoreGrant
& grant
)
265 grant
.fHaveGrant
= fHaveGrant
;
269 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
271 explicit CSemaphoreGrant(CSemaphore
& sema
, bool fTry
= false) : sem(&sema
), fHaveGrant(false)
284 operator bool() const
290 #endif // BITCOIN_SYNC_H