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 DeleteLock(void* cs
);
82 void static inline EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false) {}
83 void static inline LeaveCritical() {}
84 void static inline AssertLockHeldInternal(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)
90 * Wrapped mutex: supports recursive locking, but no waiting
91 * TODO: We should move away from using the recursive lock by default.
93 class CCriticalSection
: public AnnotatedMixin
<std::recursive_mutex
>
97 DeleteLock((void*)this);
101 /** Wrapped mutex: supports waiting but not recursive locking */
102 typedef AnnotatedMixin
<std::mutex
> CWaitableCriticalSection
;
104 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
105 typedef std::condition_variable CConditionVariable
;
107 /** Just a typedef for std::unique_lock, can be wrapped later if desired */
108 typedef std::unique_lock
<std::mutex
> WaitableLock
;
110 #ifdef DEBUG_LOCKCONTENTION
111 void PrintLockContention(const char* pszName
, const char* pszFile
, int nLine
);
114 /** Wrapper around std::unique_lock<CCriticalSection> */
115 class SCOPED_LOCKABLE CCriticalBlock
118 std::unique_lock
<CCriticalSection
> lock
;
120 void Enter(const char* pszName
, const char* pszFile
, int nLine
)
122 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()));
123 #ifdef DEBUG_LOCKCONTENTION
124 if (!lock
.try_lock()) {
125 PrintLockContention(pszName
, pszFile
, nLine
);
128 #ifdef DEBUG_LOCKCONTENTION
133 bool TryEnter(const char* pszName
, const char* pszFile
, int nLine
)
135 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()), true);
137 if (!lock
.owns_lock())
139 return lock
.owns_lock();
143 CCriticalBlock(CCriticalSection
& mutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(mutexIn
) : lock(mutexIn
, std::defer_lock
)
146 TryEnter(pszName
, pszFile
, nLine
);
148 Enter(pszName
, pszFile
, nLine
);
151 CCriticalBlock(CCriticalSection
* pmutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn
)
153 if (!pmutexIn
) return;
155 lock
= std::unique_lock
<CCriticalSection
>(*pmutexIn
, std::defer_lock
);
157 TryEnter(pszName
, pszFile
, nLine
);
159 Enter(pszName
, pszFile
, nLine
);
162 ~CCriticalBlock() UNLOCK_FUNCTION()
164 if (lock
.owns_lock())
170 return lock
.owns_lock();
174 #define PASTE(x, y) x ## y
175 #define PASTE2(x, y) PASTE(x, y)
177 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
178 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
179 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
181 #define ENTER_CRITICAL_SECTION(cs) \
183 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
187 #define LEAVE_CRITICAL_SECTION(cs) \
196 boost::condition_variable condition
;
201 explicit CSemaphore(int init
) : value(init
) {}
205 boost::unique_lock
<boost::mutex
> lock(mutex
);
207 condition
.wait(lock
);
214 boost::unique_lock
<boost::mutex
> lock(mutex
);
224 boost::unique_lock
<boost::mutex
> lock(mutex
);
227 condition
.notify_one();
231 /** RAII-style semaphore lock */
232 class CSemaphoreGrant
257 if (!fHaveGrant
&& sem
->try_wait())
262 void MoveTo(CSemaphoreGrant
& grant
)
266 grant
.fHaveGrant
= fHaveGrant
;
270 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
272 explicit CSemaphoreGrant(CSemaphore
& sema
, bool fTry
= false) : sem(&sema
), fHaveGrant(false)
285 operator bool() const
291 #endif // BITCOIN_SYNC_H