1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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();
75 * Wrapped boost mutex: supports recursive locking, but no waiting
76 * TODO: We should move away from using the recursive lock by default.
78 typedef AnnotatedMixin<boost::recursive_mutex> CCriticalSection;
80 /** Wrapped boost mutex: supports waiting but not recursive locking */
81 typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
83 /** Just a typedef for boost::condition_variable, can be wrapped later if desired */
84 typedef boost::condition_variable CConditionVariable;
86 #ifdef DEBUG_LOCKORDER
87 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
89 std::string LocksHeld();
90 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
92 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
93 void static inline LeaveCritical() {}
94 void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
96 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
98 #ifdef DEBUG_LOCKCONTENTION
99 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
102 /** Wrapper around boost::unique_lock<Mutex> */
103 template <typename Mutex>
104 class SCOPED_LOCKABLE CMutexLock
107 boost::unique_lock<Mutex> lock;
109 void Enter(const char* pszName, const char* pszFile, int nLine)
111 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
112 #ifdef DEBUG_LOCKCONTENTION
113 if (!lock.try_lock()) {
114 PrintLockContention(pszName, pszFile, nLine);
117 #ifdef DEBUG_LOCKCONTENTION
122 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
124 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
126 if (!lock.owns_lock())
128 return lock.owns_lock();
132 CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
135 TryEnter(pszName, pszFile, nLine);
137 Enter(pszName, pszFile, nLine);
140 CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
142 if (!pmutexIn) return;
144 lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
146 TryEnter(pszName, pszFile, nLine);
148 Enter(pszName, pszFile, nLine);
151 ~CMutexLock() UNLOCK_FUNCTION()
153 if (lock.owns_lock())
159 return lock.owns_lock();
163 typedef CMutexLock<CCriticalSection> CCriticalBlock;
165 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
166 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
167 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
169 #define ENTER_CRITICAL_SECTION(cs) \
171 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
175 #define LEAVE_CRITICAL_SECTION(cs) \
184 boost::condition_variable condition;
189 CSemaphore(int init) : value(init) {}
193 boost::unique_lock<boost::mutex> lock(mutex);
195 condition.wait(lock);
202 boost::unique_lock<boost::mutex> lock(mutex);
212 boost::unique_lock<boost::mutex> lock(mutex);
215 condition.notify_one();
219 /** RAII-style semaphore lock */
220 class CSemaphoreGrant
245 if (!fHaveGrant && sem->try_wait())
250 void MoveTo(CSemaphoreGrant& grant)
254 grant.fHaveGrant = fHaveGrant;
259 CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
261 CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
280 #endif // BITCOIN_SYNC_H