Use MakeUnique<Db>(...)
[bitcoinplatinum.git] / src / sync.h
blob20556af89071c5237d17e99cc292c545f9796665
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 DeleteLock(void* cs);
81 #else
82 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
83 void static inline LeaveCritical() {}
84 void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
85 void static inline DeleteLock(void* cs) {}
86 #endif
87 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
89 /**
90 * Wrapped mutex: supports recursive locking, but no waiting
91 * TODO: We should move away from using the recursive lock by default.
93 class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
95 public:
96 ~CCriticalSection() {
97 DeleteLock((void*)this);
101 /** Wrapped mutex: supports waiting but not recursive locking */
102 typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
104 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
105 typedef std::condition_variable CConditionVariable;
107 /** Just a typedef for std::unique_lock, can be wrapped later if desired */
108 typedef std::unique_lock<std::mutex> WaitableLock;
110 #ifdef DEBUG_LOCKCONTENTION
111 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
112 #endif
114 /** Wrapper around std::unique_lock<CCriticalSection> */
115 class SCOPED_LOCKABLE CCriticalBlock
117 private:
118 std::unique_lock<CCriticalSection> lock;
120 void Enter(const char* pszName, const char* pszFile, int nLine)
122 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
123 #ifdef DEBUG_LOCKCONTENTION
124 if (!lock.try_lock()) {
125 PrintLockContention(pszName, pszFile, nLine);
126 #endif
127 lock.lock();
128 #ifdef DEBUG_LOCKCONTENTION
130 #endif
133 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
135 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
136 lock.try_lock();
137 if (!lock.owns_lock())
138 LeaveCritical();
139 return lock.owns_lock();
142 public:
143 CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
145 if (fTry)
146 TryEnter(pszName, pszFile, nLine);
147 else
148 Enter(pszName, pszFile, nLine);
151 CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
153 if (!pmutexIn) return;
155 lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
156 if (fTry)
157 TryEnter(pszName, pszFile, nLine);
158 else
159 Enter(pszName, pszFile, nLine);
162 ~CCriticalBlock() UNLOCK_FUNCTION()
164 if (lock.owns_lock())
165 LeaveCritical();
168 operator bool()
170 return lock.owns_lock();
174 #define PASTE(x, y) x ## y
175 #define PASTE2(x, y) PASTE(x, y)
177 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
178 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
179 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
181 #define ENTER_CRITICAL_SECTION(cs) \
183 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
184 (cs).lock(); \
187 #define LEAVE_CRITICAL_SECTION(cs) \
189 (cs).unlock(); \
190 LeaveCritical(); \
193 class CSemaphore
195 private:
196 boost::condition_variable condition;
197 boost::mutex mutex;
198 int value;
200 public:
201 explicit CSemaphore(int init) : value(init) {}
203 void wait()
205 boost::unique_lock<boost::mutex> lock(mutex);
206 while (value < 1) {
207 condition.wait(lock);
209 value--;
212 bool try_wait()
214 boost::unique_lock<boost::mutex> lock(mutex);
215 if (value < 1)
216 return false;
217 value--;
218 return true;
221 void post()
224 boost::unique_lock<boost::mutex> lock(mutex);
225 value++;
227 condition.notify_one();
231 /** RAII-style semaphore lock */
232 class CSemaphoreGrant
234 private:
235 CSemaphore* sem;
236 bool fHaveGrant;
238 public:
239 void Acquire()
241 if (fHaveGrant)
242 return;
243 sem->wait();
244 fHaveGrant = true;
247 void Release()
249 if (!fHaveGrant)
250 return;
251 sem->post();
252 fHaveGrant = false;
255 bool TryAcquire()
257 if (!fHaveGrant && sem->try_wait())
258 fHaveGrant = true;
259 return fHaveGrant;
262 void MoveTo(CSemaphoreGrant& grant)
264 grant.Release();
265 grant.sem = sem;
266 grant.fHaveGrant = fHaveGrant;
267 fHaveGrant = false;
270 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
272 explicit CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
274 if (fTry)
275 TryAcquire();
276 else
277 Acquire();
280 ~CSemaphoreGrant()
282 Release();
285 operator bool() const
287 return fHaveGrant;
291 #endif // BITCOIN_SYNC_H