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.
9 #include "threadsafety.h"
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>
17 ////////////////////////////////////////////////
19 // THE SIMPLE DEFINITON, EXCLUDING DEBUG CODE //
21 ////////////////////////////////////////////////
27 CCriticalSection mutex;
28 boost::recursive_mutex mutex;
31 boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
33 LOCK2(mutex1, mutex2);
34 boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
35 boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
37 TRY_LOCK(mutex, name);
38 boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t);
40 ENTER_CRITICAL_SECTION(mutex); // no RAII
43 LEAVE_CRITICAL_SECTION(mutex); // no RAII
52 ///////////////////////////////
54 // THE ACTUAL IMPLEMENTATION //
56 ///////////////////////////////
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
64 void lock() EXCLUSIVE_LOCK_FUNCTION()
69 void unlock() UNLOCK_FUNCTION()
74 bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
76 return PARENT::try_lock();
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;
84 /** Wrapped boost mutex: supports waiting but not recursive locking */
85 typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
87 #ifdef DEBUG_LOCKORDER
88 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
90 std::string LocksHeld();
91 void AssertLockHeld(std::string strName);
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 AssertLockHeld(std::string) {}
98 #ifdef DEBUG_LOCKCONTENTION
99 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
102 /** Wrapper around boost::unique_lock<Mutex> */
103 template<typename Mutex>
107 boost::unique_lock<Mutex> lock;
109 void Enter(const char* pszName, const char* pszFile, int nLine)
111 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
112 #ifdef DEBUG_LOCKCONTENTION
113 if (!lock.try_lock())
115 PrintLockContention(pszName, pszFile, nLine);
118 #ifdef DEBUG_LOCKCONTENTION
123 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
125 EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
127 if (!lock.owns_lock())
129 return lock.owns_lock();
133 CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
136 TryEnter(pszName, pszFile, nLine);
138 Enter(pszName, pszFile, nLine);
143 if (lock.owns_lock())
149 return lock.owns_lock();
153 typedef CMutexLock<CCriticalSection> CCriticalBlock;
155 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
156 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
157 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
159 #define ENTER_CRITICAL_SECTION(cs) \
161 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
165 #define LEAVE_CRITICAL_SECTION(cs) \
174 boost::condition_variable condition;
179 CSemaphore(int init) : value(init) {}
182 boost::unique_lock<boost::mutex> lock(mutex);
184 condition.wait(lock);
190 boost::unique_lock<boost::mutex> lock(mutex);
199 boost::unique_lock<boost::mutex> lock(mutex);
202 condition.notify_one();
206 /** RAII-style semaphore lock */
207 class CSemaphoreGrant
229 if (!fHaveGrant && sem->try_wait())
234 void MoveTo(CSemaphoreGrant &grant) {
237 grant.fHaveGrant = fHaveGrant;
242 CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
244 CSemaphoreGrant(CSemaphore &sema, bool fTry = false) : sem(&sema), fHaveGrant(false) {