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 6.4KB

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