]> Git Repo - VerusCoin.git/blob - src/sync.h
Use mutex pointer instead of name for AssertLockHeld
[VerusCoin.git] / src / sync.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef BITCOIN_SYNC_H
7 #define BITCOIN_SYNC_H
8
9 #include "threadsafety.h"
10
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>
15
16
17 ////////////////////////////////////////////////
18 //                                            //
19 // THE SIMPLE DEFINITON, EXCLUDING DEBUG CODE //
20 //                                            //
21 ////////////////////////////////////////////////
22
23 /*
24  
25  
26  
27 CCriticalSection mutex;
28     boost::recursive_mutex mutex;
29
30 LOCK(mutex);
31     boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
32
33 LOCK2(mutex1, mutex2);
34     boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
35     boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
36
37 TRY_LOCK(mutex, name);
38     boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t);
39
40 ENTER_CRITICAL_SECTION(mutex); // no RAII
41     mutex.lock();
42
43 LEAVE_CRITICAL_SECTION(mutex); // no RAII
44     mutex.unlock();
45  
46  
47  
48  */
49
50
51
52 ///////////////////////////////
53 //                           //
54 // THE ACTUAL IMPLEMENTATION //
55 //                           //
56 ///////////////////////////////
57
58 // Template mixin that adds -Wthread-safety locking annotations to a
59 // subset of the mutex API.
60 template <typename PARENT>
61 class LOCKABLE AnnotatedMixin : public PARENT
62 {
63 public:
64     void lock() EXCLUSIVE_LOCK_FUNCTION()
65     {
66       PARENT::lock();
67     }
68
69     void unlock() UNLOCK_FUNCTION()
70     {
71       PARENT::unlock();
72     }
73
74     bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
75     {
76       return PARENT::try_lock();
77     }
78 };
79
80 /** Wrapped boost mutex: supports recursive locking, but no waiting  */
81 // TODO: We should move away from using the recursive lock by default.
82 typedef AnnotatedMixin<boost::recursive_mutex> CCriticalSection;
83
84 /** Wrapped boost mutex: supports waiting but not recursive locking */
85 typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
86
87 #ifdef DEBUG_LOCKORDER
88 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
89 void LeaveCritical();
90 std::string LocksHeld();
91 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void *cs);
92 #else
93 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
94 void static inline LeaveCritical() {}
95 void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void *cs) {}
96 #endif
97 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
98
99 #ifdef DEBUG_LOCKCONTENTION
100 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
101 #endif
102
103 /** Wrapper around boost::unique_lock<Mutex> */
104 template<typename Mutex>
105 class CMutexLock
106 {
107 private:
108     boost::unique_lock<Mutex> lock;
109
110     void Enter(const char* pszName, const char* pszFile, int nLine)
111     {
112         EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
113 #ifdef DEBUG_LOCKCONTENTION
114         if (!lock.try_lock())
115         {
116             PrintLockContention(pszName, pszFile, nLine);
117 #endif
118         lock.lock();
119 #ifdef DEBUG_LOCKCONTENTION
120         }
121 #endif
122     }
123
124     bool TryEnter(const char* pszName, const char* pszFile, int nLine)
125     {
126         EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
127         lock.try_lock();
128         if (!lock.owns_lock())
129             LeaveCritical();
130         return lock.owns_lock();
131     }
132
133 public:
134     CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
135     {
136         if (fTry)
137             TryEnter(pszName, pszFile, nLine);
138         else
139             Enter(pszName, pszFile, nLine);
140     }
141
142     ~CMutexLock()
143     {
144         if (lock.owns_lock())
145             LeaveCritical();
146     }
147
148     operator bool()
149     {
150         return lock.owns_lock();
151     }
152 };
153
154 typedef CMutexLock<CCriticalSection> CCriticalBlock;
155
156 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
157 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
158 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
159
160 #define ENTER_CRITICAL_SECTION(cs) \
161     { \
162         EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
163         (cs).lock(); \
164     }
165
166 #define LEAVE_CRITICAL_SECTION(cs) \
167     { \
168         (cs).unlock(); \
169         LeaveCritical(); \
170     }
171
172 class CSemaphore
173 {
174 private:
175     boost::condition_variable condition;
176     boost::mutex mutex;
177     int value;
178
179 public:
180     CSemaphore(int init) : value(init) {}
181
182     void wait() {
183         boost::unique_lock<boost::mutex> lock(mutex);
184         while (value < 1) {
185             condition.wait(lock);
186         }
187         value--;
188     }
189
190     bool try_wait() {
191         boost::unique_lock<boost::mutex> lock(mutex);
192         if (value < 1)
193             return false;
194         value--;
195         return true;
196     }
197
198     void post() {
199         {
200             boost::unique_lock<boost::mutex> lock(mutex);
201             value++;
202         }
203         condition.notify_one();
204     }
205 };
206
207 /** RAII-style semaphore lock */
208 class CSemaphoreGrant
209 {
210 private:
211     CSemaphore *sem;
212     bool fHaveGrant;
213
214 public:
215     void Acquire() {
216         if (fHaveGrant)
217             return;
218         sem->wait();
219         fHaveGrant = true;
220     }
221
222     void Release() {
223         if (!fHaveGrant)
224             return;
225         sem->post();
226         fHaveGrant = false;
227     }
228
229     bool TryAcquire() {
230         if (!fHaveGrant && sem->try_wait())
231             fHaveGrant = true;
232         return fHaveGrant;
233     }
234
235     void MoveTo(CSemaphoreGrant &grant) {
236         grant.Release();
237         grant.sem = sem;
238         grant.fHaveGrant = fHaveGrant;
239         sem = NULL;
240         fHaveGrant = false;
241     }
242
243     CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
244
245     CSemaphoreGrant(CSemaphore &sema, bool fTry = false) : sem(&sema), fHaveGrant(false) {
246         if (fTry)
247             TryAcquire();
248         else
249             Acquire();
250     }
251
252     ~CSemaphoreGrant() {
253         Release();
254     }
255
256     operator bool() {
257         return fHaveGrant;
258     }
259 };
260 #endif
261
This page took 0.038314 seconds and 4 git commands to generate.