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 <boost/thread/condition_variable.hpp>
12 #include <boost/thread/mutex.hpp>
13 #include <condition_variable>
18 ////////////////////////////////////////////////
20 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
22 ////////////////////////////////////////////////
25 CCriticalSection mutex;
26 std::recursive_mutex mutex;
29 std::unique_lock<std::recursive_mutex> criticalblock(mutex);
31 LOCK2(mutex1, mutex2);
32 std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
33 std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
35 TRY_LOCK(mutex, name);
36 std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
38 ENTER_CRITICAL_SECTION(mutex); // no RAII
41 LEAVE_CRITICAL_SECTION(mutex); // no RAII
45 ///////////////////////////////
47 // THE ACTUAL IMPLEMENTATION //
49 ///////////////////////////////
52 * Template mixin that adds -Wthread-safety locking
53 * annotations to a subset of the mutex API.
55 template <typename PARENT
>
56 class LOCKABLE AnnotatedMixin
: public PARENT
59 void lock() EXCLUSIVE_LOCK_FUNCTION()
64 void unlock() UNLOCK_FUNCTION()
69 bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
71 return PARENT::try_lock();
75 #ifdef DEBUG_LOCKORDER
76 void EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false);
78 std::string
LocksHeld();
79 void AssertLockHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
);
80 void AssertLockNotHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
);
81 void DeleteLock(void* cs
);
83 void static inline EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false) {}
84 void static inline LeaveCritical() {}
85 void static inline AssertLockHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
) {}
86 void static inline AssertLockNotHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
) {}
87 void static inline DeleteLock(void* cs
) {}
89 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
90 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
93 * Wrapped mutex: supports recursive locking, but no waiting
94 * TODO: We should move away from using the recursive lock by default.
96 class CCriticalSection
: public AnnotatedMixin
<std::recursive_mutex
>
100 DeleteLock((void*)this);
104 /** Wrapped mutex: supports waiting but not recursive locking */
105 typedef AnnotatedMixin
<std::mutex
> CWaitableCriticalSection
;
107 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
108 typedef std::condition_variable CConditionVariable
;
110 /** Just a typedef for std::unique_lock, can be wrapped later if desired */
111 typedef std::unique_lock
<std::mutex
> WaitableLock
;
113 #ifdef DEBUG_LOCKCONTENTION
114 void PrintLockContention(const char* pszName
, const char* pszFile
, int nLine
);
117 /** Wrapper around std::unique_lock<CCriticalSection> */
118 class SCOPED_LOCKABLE CCriticalBlock
121 std::unique_lock
<CCriticalSection
> lock
;
123 void Enter(const char* pszName
, const char* pszFile
, int nLine
)
125 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()));
126 #ifdef DEBUG_LOCKCONTENTION
127 if (!lock
.try_lock()) {
128 PrintLockContention(pszName
, pszFile
, nLine
);
131 #ifdef DEBUG_LOCKCONTENTION
136 bool TryEnter(const char* pszName
, const char* pszFile
, int nLine
)
138 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()), true);
140 if (!lock
.owns_lock())
142 return lock
.owns_lock();
146 CCriticalBlock(CCriticalSection
& mutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(mutexIn
) : lock(mutexIn
, std::defer_lock
)
149 TryEnter(pszName
, pszFile
, nLine
);
151 Enter(pszName
, pszFile
, nLine
);
154 CCriticalBlock(CCriticalSection
* pmutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn
)
156 if (!pmutexIn
) return;
158 lock
= std::unique_lock
<CCriticalSection
>(*pmutexIn
, std::defer_lock
);
160 TryEnter(pszName
, pszFile
, nLine
);
162 Enter(pszName
, pszFile
, nLine
);
165 ~CCriticalBlock() UNLOCK_FUNCTION()
167 if (lock
.owns_lock())
173 return lock
.owns_lock();
177 #define PASTE(x, y) x ## y
178 #define PASTE2(x, y) PASTE(x, y)
180 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
181 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
182 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
184 #define ENTER_CRITICAL_SECTION(cs) \
186 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
190 #define LEAVE_CRITICAL_SECTION(cs) \
199 boost::condition_variable condition
;
204 explicit CSemaphore(int init
) : value(init
) {}
208 boost::unique_lock
<boost::mutex
> lock(mutex
);
210 condition
.wait(lock
);
217 boost::unique_lock
<boost::mutex
> lock(mutex
);
227 boost::unique_lock
<boost::mutex
> lock(mutex
);
230 condition
.notify_one();
234 /** RAII-style semaphore lock */
235 class CSemaphoreGrant
260 if (!fHaveGrant
&& sem
->try_wait())
265 void MoveTo(CSemaphoreGrant
& grant
)
269 grant
.fHaveGrant
= fHaveGrant
;
273 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
275 explicit CSemaphoreGrant(CSemaphore
& sema
, bool fTry
= false) : sem(&sema
), fHaveGrant(false)
288 operator bool() const
294 #endif // BITCOIN_SYNC_H