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>
54 template <
typename PARENT>
70 return PARENT::try_lock();
74 #ifdef DEBUG_LOCKORDER
75 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
77 std::string LocksHeld();
78 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs);
79 void DeleteLock(
void* cs);
81 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
82 void static inline LeaveCritical() {}
83 void static inline AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs) {}
84 void static inline DeleteLock(
void* cs) {}
86 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
96 DeleteLock((
void*)
this);
107 #ifdef DEBUG_LOCKCONTENTION
108 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
112 template <
typename Mutex>
116 boost::unique_lock<Mutex>
lock;
118 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
120 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()));
121 #ifdef DEBUG_LOCKCONTENTION
122 if (!lock.try_lock()) {
123 PrintLockContention(pszName, pszFile, nLine);
126 #ifdef DEBUG_LOCKCONTENTION
131 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
133 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()),
true);
135 if (!lock.owns_lock())
137 return lock.owns_lock();
144 TryEnter(pszName, pszFile, nLine);
146 Enter(pszName, pszFile, nLine);
151 if (!pmutexIn)
return;
153 lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
155 TryEnter(pszName, pszFile, nLine);
157 Enter(pszName, pszFile, nLine);
162 if (lock.owns_lock())
168 return lock.owns_lock();
174 #define PASTE(x, y) x ## y
175 #define PASTE2(x, y) PASTE(x, y)
177 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
178 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
179 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
181 #define ENTER_CRITICAL_SECTION(cs) \
183 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
187 #define LEAVE_CRITICAL_SECTION(cs) \
205 boost::unique_lock<boost::mutex> lock(
mutex);
214 boost::unique_lock<boost::mutex> lock(
mutex);
224 boost::unique_lock<boost::mutex> lock(
mutex);
Template mixin that adds -Wthread-safety locking annotations to a subset of the mutex API.
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
void unlock() UNLOCK_FUNCTION()
void lock() EXCLUSIVE_LOCK_FUNCTION()
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Wrapper around boost::unique_lock<Mutex>
boost::unique_lock< Mutex > lock
CMutexLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
CMutexLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
void Enter(const char *pszName, const char *pszFile, int nLine)
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
~CMutexLock() UNLOCK_FUNCTION()
RAII-style semaphore lock.
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
void MoveTo(CSemaphoreGrant &grant)
boost::condition_variable condition
AnnotatedMixin< boost::mutex > CWaitableCriticalSection
Wrapped boost mutex: supports waiting but not recursive locking.
CMutexLock< CCriticalSection > CCriticalBlock
boost::condition_variable CConditionVariable
Just a typedef for boost::condition_variable, can be wrapped later if desired.
CCriticalSection CDynamicCriticalSection
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define UNLOCK_FUNCTION(...)