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/locks.hpp>
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/recursive_mutex.hpp>
17 ////////////////////////////////////////////////
19 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
21 ////////////////////////////////////////////////
24 CCriticalSection mutex;
25 boost::recursive_mutex mutex;
28 boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
30 LOCK2(mutex1, mutex2);
31 boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
32 boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
34 TRY_LOCK(mutex, name);
35 boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t);
37 ENTER_CRITICAL_SECTION(mutex); // no RAII
40 LEAVE_CRITICAL_SECTION(mutex); // no RAII
44 ///////////////////////////////
46 // THE ACTUAL IMPLEMENTATION //
48 ///////////////////////////////
51 * Template mixin that adds -Wthread-safety locking
52 * annotations to a subset of the mutex API.
54 template <typename PARENT
>
55 class LOCKABLE AnnotatedMixin
: public PARENT
58 void lock() EXCLUSIVE_LOCK_FUNCTION()
63 void unlock() UNLOCK_FUNCTION()
68 bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
70 return PARENT::try_lock();
74 #ifdef DEBUG_LOCKORDER
75 void EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
= false);
77 std::string
LocksHeld();
78 void AssertLockHeldInternal(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 DeleteLock(void* cs
) {}
86 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
89 * Wrapped boost mutex: supports recursive locking, but no waiting
90 * TODO: We should move away from using the recursive lock by default.
92 class CCriticalSection
: public AnnotatedMixin
<boost::recursive_mutex
>
96 DeleteLock((void*)this);
100 /** Wrapped boost mutex: supports waiting but not recursive locking */
101 typedef AnnotatedMixin
<boost::mutex
> CWaitableCriticalSection
;
103 /** Just a typedef for boost::condition_variable, can be wrapped later if desired */
104 typedef boost::condition_variable CConditionVariable
;
106 #ifdef DEBUG_LOCKCONTENTION
107 void PrintLockContention(const char* pszName
, const char* pszFile
, int nLine
);
110 /** Wrapper around boost::unique_lock<Mutex> */
111 template <typename Mutex
>
112 class SCOPED_LOCKABLE CMutexLock
115 boost::unique_lock
<Mutex
> lock
;
117 void Enter(const char* pszName
, const char* pszFile
, int nLine
)
119 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()));
120 #ifdef DEBUG_LOCKCONTENTION
121 if (!lock
.try_lock()) {
122 PrintLockContention(pszName
, pszFile
, nLine
);
125 #ifdef DEBUG_LOCKCONTENTION
130 bool TryEnter(const char* pszName
, const char* pszFile
, int nLine
)
132 EnterCritical(pszName
, pszFile
, nLine
, (void*)(lock
.mutex()), true);
134 if (!lock
.owns_lock())
136 return lock
.owns_lock();
140 CMutexLock(Mutex
& mutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(mutexIn
) : lock(mutexIn
, boost::defer_lock
)
143 TryEnter(pszName
, pszFile
, nLine
);
145 Enter(pszName
, pszFile
, nLine
);
148 CMutexLock(Mutex
* pmutexIn
, const char* pszName
, const char* pszFile
, int nLine
, bool fTry
= false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn
)
150 if (!pmutexIn
) return;
152 lock
= boost::unique_lock
<Mutex
>(*pmutexIn
, boost::defer_lock
);
154 TryEnter(pszName
, pszFile
, nLine
);
156 Enter(pszName
, pszFile
, nLine
);
159 ~CMutexLock() UNLOCK_FUNCTION()
161 if (lock
.owns_lock())
167 return lock
.owns_lock();
171 typedef CMutexLock
<CCriticalSection
> CCriticalBlock
;
173 #define PASTE(x, y) x ## y
174 #define PASTE2(x, y) PASTE(x, y)
176 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
177 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
178 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
180 #define ENTER_CRITICAL_SECTION(cs) \
182 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
186 #define LEAVE_CRITICAL_SECTION(cs) \
195 boost::condition_variable condition
;
200 CSemaphore(int init
) : value(init
) {}
204 boost::unique_lock
<boost::mutex
> lock(mutex
);
206 condition
.wait(lock
);
213 boost::unique_lock
<boost::mutex
> lock(mutex
);
223 boost::unique_lock
<boost::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(NULL
), fHaveGrant(false) {}
271 CSemaphoreGrant(CSemaphore
& sema
, bool fTry
= false) : sem(&sema
), fHaveGrant(false)
290 #endif // BITCOIN_SYNC_H