[test] Replace check_output with low level version
[bitcoinplatinum.git] / src / sync.h
blob0871c5fb4d8518a9d174fee4e3d57c9f81812574
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 <boost/thread/recursive_mutex.hpp>
16 ////////////////////////////////////////////////
17 // //
18 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
19 // //
20 ////////////////////////////////////////////////
23 CCriticalSection mutex;
24 boost::recursive_mutex mutex;
26 LOCK(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
37 mutex.lock();
39 LEAVE_CRITICAL_SECTION(mutex); // no RAII
40 mutex.unlock();
43 ///////////////////////////////
44 // //
45 // THE ACTUAL IMPLEMENTATION //
46 // //
47 ///////////////////////////////
49 /**
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
56 public:
57 void lock() EXCLUSIVE_LOCK_FUNCTION()
59 PARENT::lock();
62 void unlock() UNLOCK_FUNCTION()
64 PARENT::unlock();
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);
75 void LeaveCritical();
76 std::string LocksHeld();
77 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
78 void DeleteLock(void* cs);
79 #else
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) {}
84 #endif
85 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
87 /**
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>
93 public:
94 ~CCriticalSection() {
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);
107 #endif
109 /** Wrapper around boost::unique_lock<Mutex> */
110 template <typename Mutex>
111 class SCOPED_LOCKABLE CMutexLock
113 private:
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);
122 #endif
123 lock.lock();
124 #ifdef DEBUG_LOCKCONTENTION
126 #endif
129 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
131 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
132 lock.try_lock();
133 if (!lock.owns_lock())
134 LeaveCritical();
135 return lock.owns_lock();
138 public:
139 CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
141 if (fTry)
142 TryEnter(pszName, pszFile, nLine);
143 else
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);
152 if (fTry)
153 TryEnter(pszName, pszFile, nLine);
154 else
155 Enter(pszName, pszFile, nLine);
158 ~CMutexLock() UNLOCK_FUNCTION()
160 if (lock.owns_lock())
161 LeaveCritical();
164 operator bool()
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)); \
182 (cs).lock(); \
185 #define LEAVE_CRITICAL_SECTION(cs) \
187 (cs).unlock(); \
188 LeaveCritical(); \
191 class CSemaphore
193 private:
194 boost::condition_variable condition;
195 boost::mutex mutex;
196 int value;
198 public:
199 explicit CSemaphore(int init) : value(init) {}
201 void wait()
203 boost::unique_lock<boost::mutex> lock(mutex);
204 while (value < 1) {
205 condition.wait(lock);
207 value--;
210 bool try_wait()
212 boost::unique_lock<boost::mutex> lock(mutex);
213 if (value < 1)
214 return false;
215 value--;
216 return true;
219 void post()
222 boost::unique_lock<boost::mutex> lock(mutex);
223 value++;
225 condition.notify_one();
229 /** RAII-style semaphore lock */
230 class CSemaphoreGrant
232 private:
233 CSemaphore* sem;
234 bool fHaveGrant;
236 public:
237 void Acquire()
239 if (fHaveGrant)
240 return;
241 sem->wait();
242 fHaveGrant = true;
245 void Release()
247 if (!fHaveGrant)
248 return;
249 sem->post();
250 fHaveGrant = false;
253 bool TryAcquire()
255 if (!fHaveGrant && sem->try_wait())
256 fHaveGrant = true;
257 return fHaveGrant;
260 void MoveTo(CSemaphoreGrant& grant)
262 grant.Release();
263 grant.sem = sem;
264 grant.fHaveGrant = fHaveGrant;
265 fHaveGrant = false;
268 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
270 explicit 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() const
285 return fHaveGrant;
289 #endif // BITCOIN_SYNC_H