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.
8 #include <boost/thread/mutex.hpp>
9 #include <boost/thread/recursive_mutex.hpp>
10 #include <boost/thread/locks.hpp>
11 #include <boost/thread/condition_variable.hpp>
12 #include "threadsafety.h"
15 ////////////////////////////////////////////////
17 // THE SIMPLE DEFINITON, EXCLUDING DEBUG CODE //
19 ////////////////////////////////////////////////
25 CCriticalSection mutex;
26 boost::recursive_mutex mutex;
29 boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
31 LOCK2(mutex1, mutex2);
32 boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
33 boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
35 TRY_LOCK(mutex, name);
36 boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t);
38 ENTER_CRITICAL_SECTION(mutex); // no RAII
41 LEAVE_CRITICAL_SECTION(mutex); // no RAII
50 ///////////////////////////////
52 // THE ACTUAL IMPLEMENTATION //
54 ///////////////////////////////
56 // Template mixin that adds -Wthread-safety locking annotations to a
57 // subset of the mutex API.
58 template <typename PARENT>
59 class LOCKABLE AnnotatedMixin : public PARENT
62 void lock() EXCLUSIVE_LOCK_FUNCTION()
67 void unlock() UNLOCK_FUNCTION()
72 bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
74 return PARENT::try_lock();
78 /** Wrapped boost mutex: supports recursive locking, but no waiting */
79 // TODO: We should move away from using the recursive lock by default.
80 typedef AnnotatedMixin<boost::recursive_mutex> CCriticalSection;
82 /** Wrapped boost mutex: supports waiting but not recursive locking */
83 typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
85 #ifdef DEBUG_LOCKORDER
86 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
89 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
90 void static inline LeaveCritical() {}
93 #ifdef DEBUG_LOCKCONTENTION
94 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
97 /** Wrapper around boost::unique_lock<Mutex> */
98 template<typename Mutex>
102 boost::unique_lock<Mutex> lock;
104 void Enter(const char* pszName, const char* pszFile, int nLine)
106 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
107 #ifdef DEBUG_LOCKCONTENTION
108 if (!lock.try_lock())
110 PrintLockContention(pszName, pszFile, nLine);
113 #ifdef DEBUG_LOCKCONTENTION
118 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
120 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
122 if (!lock.owns_lock())
124 return lock.owns_lock();
128 CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
131 TryEnter(pszName, pszFile, nLine);
133 Enter(pszName, pszFile, nLine);
138 if (lock.owns_lock())
144 return lock.owns_lock();
148 typedef CMutexLock<CCriticalSection> CCriticalBlock;
150 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
151 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
152 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
154 #define ENTER_CRITICAL_SECTION(cs) \
156 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
160 #define LEAVE_CRITICAL_SECTION(cs) \
169 boost::condition_variable condition;
174 CSemaphore(int init) : value(init) {}
177 boost::unique_lock<boost::mutex> lock(mutex);
179 condition.wait(lock);
185 boost::unique_lock<boost::mutex> lock(mutex);
194 boost::unique_lock<boost::mutex> lock(mutex);
197 condition.notify_one();
201 /** RAII-style semaphore lock */
202 class CSemaphoreGrant
224 if (!fHaveGrant && sem->try_wait())
229 void MoveTo(CSemaphoreGrant &grant) {
232 grant.fHaveGrant = fHaveGrant;
237 CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
239 CSemaphoreGrant(CSemaphore &sema, bool fTry = false) : sem(&sema), fHaveGrant(false) {