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 <boost/thread/recursive_mutex.hpp>
16 ////////////////////////////////////////////////
18 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
20 ////////////////////////////////////////////////
23 CCriticalSection mutex;
24 boost::recursive_mutex mutex;
27 boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
29 LOCK2(mutex1, mutex2);
30 boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
31 boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
33 TRY_LOCK(mutex, name);
34 boost::unique_lock<boost::recursive_mutex> name(mutex, boost::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 DeleteLock(void* cs
);
80 void static inline EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false) {}
81 void static inline LeaveCritical() {}
82 void static inline AssertLockHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
) {}
83 void static inline DeleteLock(void* cs
) {}
85 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
88 * Wrapped boost mutex: supports recursive locking, but no waiting
89 * TODO: We should move away from using the recursive lock by default.
91 class CCriticalSection
: public AnnotatedMixin
<boost::recursive_mutex
>
95 DeleteLock((void*)this);
99 /** Wrapped boost mutex: supports waiting but not recursive locking */
100 typedef AnnotatedMixin
<boost::mutex
> CWaitableCriticalSection
;
102 /** Just a typedef for boost::condition_variable, can be wrapped later if desired */
103 typedef boost::condition_variable CConditionVariable
;
105 #ifdef DEBUG_LOCKCONTENTION
106 void PrintLockContention(const char* pszName
, const char* pszFile
, int nLine
);
109 /** Wrapper around boost::unique_lock<Mutex> */
110 template <typename Mutex
>
111 class SCOPED_LOCKABLE CMutexLock
114 boost::unique_lock
<Mutex
> lock
;
116 void Enter(const char* pszName
, const char* pszFile
, int nLine
)
118 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()));
119 #ifdef DEBUG_LOCKCONTENTION
120 if (!lock
.try_lock()) {
121 PrintLockContention(pszName
, pszFile
, nLine
);
124 #ifdef DEBUG_LOCKCONTENTION
129 bool TryEnter(const char* pszName
, const char* pszFile
, int nLine
)
131 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()), true);
133 if (!lock
.owns_lock())
135 return lock
.owns_lock();
139 CMutexLock(Mutex
& mutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(mutexIn
) : lock(mutexIn
, boost::defer_lock
)
142 TryEnter(pszName
, pszFile
, nLine
);
144 Enter(pszName
, pszFile
, nLine
);
147 CMutexLock(Mutex
* pmutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn
)
149 if (!pmutexIn
) return;
151 lock
= boost::unique_lock
<Mutex
>(*pmutexIn
, boost::defer_lock
);
153 TryEnter(pszName
, pszFile
, nLine
);
155 Enter(pszName
, pszFile
, nLine
);
158 ~CMutexLock() UNLOCK_FUNCTION()
160 if (lock
.owns_lock())
166 return lock
.owns_lock();
170 typedef CMutexLock
<CCriticalSection
> CCriticalBlock
;
172 #define PASTE(x, y) x ## y
173 #define PASTE2(x, y) PASTE(x, y)
175 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
176 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
177 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
179 #define ENTER_CRITICAL_SECTION(cs) \
181 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
185 #define LEAVE_CRITICAL_SECTION(cs) \
194 boost::condition_variable condition
;
199 explicit CSemaphore(int init
) : value(init
) {}
203 boost::unique_lock
<boost::mutex
> lock(mutex
);
205 condition
.wait(lock
);
212 boost::unique_lock
<boost::mutex
> lock(mutex
);
222 boost::unique_lock
<boost::mutex
> lock(mutex
);
225 condition
.notify_one();
229 /** RAII-style semaphore lock */
230 class CSemaphoreGrant
255 if (!fHaveGrant
&& sem
->try_wait())
260 void MoveTo(CSemaphoreGrant
& grant
)
264 grant
.fHaveGrant
= fHaveGrant
;
268 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
270 explicit CSemaphoreGrant(CSemaphore
& sema
, bool fTry
= false) : sem(&sema
), fHaveGrant(false)
283 operator bool() const
289 #endif // BITCOIN_SYNC_H