Merge #11747: Fix: Open files read only if requested
[bitcoinplatinum.git] / src / sync.h
blob3af27c65d0d4dc799646a6ac474f44b647dcaf16
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 <condition_variable>
12 #include <thread>
13 #include <mutex>
16 ////////////////////////////////////////////////
17 // //
18 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
19 // //
20 ////////////////////////////////////////////////
23 CCriticalSection mutex;
24 std::recursive_mutex mutex;
26 LOCK(mutex);
27 std::unique_lock<std::recursive_mutex> criticalblock(mutex);
29 LOCK2(mutex1, mutex2);
30 std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
31 std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
33 TRY_LOCK(mutex, name);
34 std::unique_lock<std::recursive_mutex> name(mutex, std::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 AssertLockNotHeldInternal(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 AssertLockNotHeldInternal(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)
88 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
90 /**
91 * Wrapped mutex: supports recursive locking, but no waiting
92 * TODO: We should move away from using the recursive lock by default.
94 class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
96 public:
97 ~CCriticalSection() {
98 DeleteLock((void*)this);
102 /** Wrapped mutex: supports waiting but not recursive locking */
103 typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
105 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
106 typedef std::condition_variable CConditionVariable;
108 /** Just a typedef for std::unique_lock, can be wrapped later if desired */
109 typedef std::unique_lock<std::mutex> WaitableLock;
111 #ifdef DEBUG_LOCKCONTENTION
112 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
113 #endif
115 /** Wrapper around std::unique_lock<CCriticalSection> */
116 class SCOPED_LOCKABLE CCriticalBlock
118 private:
119 std::unique_lock<CCriticalSection> lock;
121 void Enter(const char* pszName, const char* pszFile, int nLine)
123 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
124 #ifdef DEBUG_LOCKCONTENTION
125 if (!lock.try_lock()) {
126 PrintLockContention(pszName, pszFile, nLine);
127 #endif
128 lock.lock();
129 #ifdef DEBUG_LOCKCONTENTION
131 #endif
134 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
136 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
137 lock.try_lock();
138 if (!lock.owns_lock())
139 LeaveCritical();
140 return lock.owns_lock();
143 public:
144 CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
146 if (fTry)
147 TryEnter(pszName, pszFile, nLine);
148 else
149 Enter(pszName, pszFile, nLine);
152 CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
154 if (!pmutexIn) return;
156 lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
157 if (fTry)
158 TryEnter(pszName, pszFile, nLine);
159 else
160 Enter(pszName, pszFile, nLine);
163 ~CCriticalBlock() UNLOCK_FUNCTION()
165 if (lock.owns_lock())
166 LeaveCritical();
169 operator bool()
171 return lock.owns_lock();
175 #define PASTE(x, y) x ## y
176 #define PASTE2(x, y) PASTE(x, y)
178 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
179 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
180 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
182 #define ENTER_CRITICAL_SECTION(cs) \
184 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
185 (cs).lock(); \
188 #define LEAVE_CRITICAL_SECTION(cs) \
190 (cs).unlock(); \
191 LeaveCritical(); \
194 class CSemaphore
196 private:
197 std::condition_variable condition;
198 std::mutex mutex;
199 int value;
201 public:
202 explicit CSemaphore(int init) : value(init) {}
204 void wait()
206 std::unique_lock<std::mutex> lock(mutex);
207 condition.wait(lock, [&]() { return value >= 1; });
208 value--;
211 bool try_wait()
213 std::lock_guard<std::mutex> lock(mutex);
214 if (value < 1)
215 return false;
216 value--;
217 return true;
220 void post()
223 std::lock_guard<std::mutex> lock(mutex);
224 value++;
226 condition.notify_one();
230 /** RAII-style semaphore lock */
231 class CSemaphoreGrant
233 private:
234 CSemaphore* sem;
235 bool fHaveGrant;
237 public:
238 void Acquire()
240 if (fHaveGrant)
241 return;
242 sem->wait();
243 fHaveGrant = true;
246 void Release()
248 if (!fHaveGrant)
249 return;
250 sem->post();
251 fHaveGrant = false;
254 bool TryAcquire()
256 if (!fHaveGrant && sem->try_wait())
257 fHaveGrant = true;
258 return fHaveGrant;
261 void MoveTo(CSemaphoreGrant& grant)
263 grant.Release();
264 grant.sem = sem;
265 grant.fHaveGrant = fHaveGrant;
266 fHaveGrant = false;
269 CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
271 explicit CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
273 if (fTry)
274 TryAcquire();
275 else
276 Acquire();
279 ~CSemaphoreGrant()
281 Release();
284 operator bool() const
286 return fHaveGrant;
290 #endif // BITCOIN_SYNC_H