Clean up lockorder data of destroyed mutexes
[bitcoinplatinum.git] / src / sync.h
blob0c58fb6b4e94cbb20081388b67da5b9984c03cba
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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.
6 #ifndef BITCOIN_SYNC_H
7 #define BITCOIN_SYNC_H
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 ////////////////////////////////////////////////
18 // //
19 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
20 // //
21 ////////////////////////////////////////////////
24 CCriticalSection mutex;
25 boost::recursive_mutex mutex;
27 LOCK(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
38 mutex.lock();
40 LEAVE_CRITICAL_SECTION(mutex); // no RAII
41 mutex.unlock();
44 ///////////////////////////////
45 // //
46 // THE ACTUAL IMPLEMENTATION //
47 // //
48 ///////////////////////////////
50 /**
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
57 public:
58 void lock() EXCLUSIVE_LOCK_FUNCTION()
60 PARENT::lock();
63 void unlock() UNLOCK_FUNCTION()
65 PARENT::unlock();
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);
76 void LeaveCritical();
77 std::string LocksHeld();
78 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
79 void DeleteLock(void* cs);
80 #else
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) {}
85 #endif
86 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
88 /**
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>
94 public:
95 ~CCriticalSection() {
96 DeleteLock((void*)this);
100 typedef CCriticalSection CDynamicCriticalSection;
101 /** Wrapped boost mutex: supports waiting but not recursive locking */
102 typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
104 /** Just a typedef for boost::condition_variable, can be wrapped later if desired */
105 typedef boost::condition_variable CConditionVariable;
107 #ifdef DEBUG_LOCKCONTENTION
108 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
109 #endif
111 /** Wrapper around boost::unique_lock<Mutex> */
112 template <typename Mutex>
113 class SCOPED_LOCKABLE CMutexLock
115 private:
116 boost::unique_lock<Mutex> lock;
118 void Enter(const char* pszName, const char* pszFile, int nLine)
120 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
121 #ifdef DEBUG_LOCKCONTENTION
122 if (!lock.try_lock()) {
123 PrintLockContention(pszName, pszFile, nLine);
124 #endif
125 lock.lock();
126 #ifdef DEBUG_LOCKCONTENTION
128 #endif
131 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
133 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
134 lock.try_lock();
135 if (!lock.owns_lock())
136 LeaveCritical();
137 return lock.owns_lock();
140 public:
141 CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
143 if (fTry)
144 TryEnter(pszName, pszFile, nLine);
145 else
146 Enter(pszName, pszFile, nLine);
149 CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
151 if (!pmutexIn) return;
153 lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
154 if (fTry)
155 TryEnter(pszName, pszFile, nLine);
156 else
157 Enter(pszName, pszFile, nLine);
160 ~CMutexLock() UNLOCK_FUNCTION()
162 if (lock.owns_lock())
163 LeaveCritical();
166 operator bool()
168 return lock.owns_lock();
172 typedef CMutexLock<CCriticalSection> CCriticalBlock;
174 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
175 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
176 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
178 #define ENTER_CRITICAL_SECTION(cs) \
180 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
181 (cs).lock(); \
184 #define LEAVE_CRITICAL_SECTION(cs) \
186 (cs).unlock(); \
187 LeaveCritical(); \
190 class CSemaphore
192 private:
193 boost::condition_variable condition;
194 boost::mutex mutex;
195 int value;
197 public:
198 CSemaphore(int init) : value(init) {}
200 void wait()
202 boost::unique_lock<boost::mutex> lock(mutex);
203 while (value < 1) {
204 condition.wait(lock);
206 value--;
209 bool try_wait()
211 boost::unique_lock<boost::mutex> lock(mutex);
212 if (value < 1)
213 return false;
214 value--;
215 return true;
218 void post()
221 boost::unique_lock<boost::mutex> lock(mutex);
222 value++;
224 condition.notify_one();
228 /** RAII-style semaphore lock */
229 class CSemaphoreGrant
231 private:
232 CSemaphore* sem;
233 bool fHaveGrant;
235 public:
236 void Acquire()
238 if (fHaveGrant)
239 return;
240 sem->wait();
241 fHaveGrant = true;
244 void Release()
246 if (!fHaveGrant)
247 return;
248 sem->post();
249 fHaveGrant = false;
252 bool TryAcquire()
254 if (!fHaveGrant && sem->try_wait())
255 fHaveGrant = true;
256 return fHaveGrant;
259 void MoveTo(CSemaphoreGrant& grant)
261 grant.Release();
262 grant.sem = sem;
263 grant.fHaveGrant = fHaveGrant;
264 sem = NULL;
265 fHaveGrant = false;
268 CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
270 CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
272 if (fTry)
273 TryAcquire();
274 else
275 Acquire();
278 ~CSemaphoreGrant()
280 Release();
283 operator bool()
285 return fHaveGrant;
289 #endif // BITCOIN_SYNC_H