tests: move pwalletMain to wallet test fixture
[bitcoinplatinum.git] / src / sync.h
blobb0889be767546beedc1964e0b7fdc06bdf3929b3
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.
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/mutex.hpp>
13 #include <condition_variable>
14 #include <thread>
15 #include <mutex>
18 ////////////////////////////////////////////////
19 // //
20 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
21 // //
22 ////////////////////////////////////////////////
25 CCriticalSection mutex;
26 std::recursive_mutex mutex;
28 LOCK(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
39 mutex.lock();
41 LEAVE_CRITICAL_SECTION(mutex); // no RAII
42 mutex.unlock();
45 ///////////////////////////////
46 // //
47 // THE ACTUAL IMPLEMENTATION //
48 // //
49 ///////////////////////////////
51 /**
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
58 public:
59 void lock() EXCLUSIVE_LOCK_FUNCTION()
61 PARENT::lock();
64 void unlock() UNLOCK_FUNCTION()
66 PARENT::unlock();
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);
77 void LeaveCritical();
78 std::string LocksHeld();
79 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
80 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
81 void DeleteLock(void* cs);
82 #else
83 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
84 void static inline LeaveCritical() {}
85 void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
86 void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
87 void static inline DeleteLock(void* cs) {}
88 #endif
89 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
90 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
92 /**
93 * Wrapped mutex: supports recursive locking, but no waiting
94 * TODO: We should move away from using the recursive lock by default.
96 class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
98 public:
99 ~CCriticalSection() {
100 DeleteLock((void*)this);
104 /** Wrapped mutex: supports waiting but not recursive locking */
105 typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
107 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
108 typedef std::condition_variable CConditionVariable;
110 /** Just a typedef for std::unique_lock, can be wrapped later if desired */
111 typedef std::unique_lock<std::mutex> WaitableLock;
113 #ifdef DEBUG_LOCKCONTENTION
114 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
115 #endif
117 /** Wrapper around std::unique_lock<CCriticalSection> */
118 class SCOPED_LOCKABLE CCriticalBlock
120 private:
121 std::unique_lock<CCriticalSection> lock;
123 void Enter(const char* pszName, const char* pszFile, int nLine)
125 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
126 #ifdef DEBUG_LOCKCONTENTION
127 if (!lock.try_lock()) {
128 PrintLockContention(pszName, pszFile, nLine);
129 #endif
130 lock.lock();
131 #ifdef DEBUG_LOCKCONTENTION
133 #endif
136 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
138 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
139 lock.try_lock();
140 if (!lock.owns_lock())
141 LeaveCritical();
142 return lock.owns_lock();
145 public:
146 CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
148 if (fTry)
149 TryEnter(pszName, pszFile, nLine);
150 else
151 Enter(pszName, pszFile, nLine);
154 CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
156 if (!pmutexIn) return;
158 lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
159 if (fTry)
160 TryEnter(pszName, pszFile, nLine);
161 else
162 Enter(pszName, pszFile, nLine);
165 ~CCriticalBlock() UNLOCK_FUNCTION()
167 if (lock.owns_lock())
168 LeaveCritical();
171 operator bool()
173 return lock.owns_lock();
177 #define PASTE(x, y) x ## y
178 #define PASTE2(x, y) PASTE(x, y)
180 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
181 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
182 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
184 #define ENTER_CRITICAL_SECTION(cs) \
186 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
187 (cs).lock(); \
190 #define LEAVE_CRITICAL_SECTION(cs) \
192 (cs).unlock(); \
193 LeaveCritical(); \
196 class CSemaphore
198 private:
199 boost::condition_variable condition;
200 boost::mutex mutex;
201 int value;
203 public:
204 explicit CSemaphore(int init) : value(init) {}
206 void wait()
208 boost::unique_lock<boost::mutex> lock(mutex);
209 while (value < 1) {
210 condition.wait(lock);
212 value--;
215 bool try_wait()
217 boost::unique_lock<boost::mutex> lock(mutex);
218 if (value < 1)
219 return false;
220 value--;
221 return true;
224 void post()
227 boost::unique_lock<boost::mutex> lock(mutex);
228 value++;
230 condition.notify_one();
234 /** RAII-style semaphore lock */
235 class CSemaphoreGrant
237 private:
238 CSemaphore* sem;
239 bool fHaveGrant;
241 public:
242 void Acquire()
244 if (fHaveGrant)
245 return;
246 sem->wait();
247 fHaveGrant = true;
250 void Release()
252 if (!fHaveGrant)
253 return;
254 sem->post();
255 fHaveGrant = false;
258 bool TryAcquire()
260 if (!fHaveGrant && sem->try_wait())
261 fHaveGrant = true;
262 return fHaveGrant;
265 void MoveTo(CSemaphoreGrant& grant)
267 grant.Release();
268 grant.sem = sem;
269 grant.fHaveGrant = fHaveGrant;
270 fHaveGrant = false;
273 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
275 explicit CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
277 if (fTry)
278 TryAcquire();
279 else
280 Acquire();
283 ~CSemaphoreGrant()
285 Release();
288 operator bool() const
290 return fHaveGrant;
294 #endif // BITCOIN_SYNC_H