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