You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sync.h 7.3KB

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