12 #include <boost/thread/condition_variable.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);
106 #ifdef DEBUG_LOCKCONTENTION 107 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
111 template <
typename Mutex>
115 boost::unique_lock<Mutex>
lock;
117 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
119 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()));
120 #ifdef DEBUG_LOCKCONTENTION 121 if (!lock.try_lock()) {
122 PrintLockContention(pszName, pszFile, nLine);
125 #ifdef DEBUG_LOCKCONTENTION 130 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
132 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()),
true);
134 if (!lock.owns_lock())
136 return lock.owns_lock();
143 TryEnter(pszName, pszFile, nLine);
145 Enter(pszName, pszFile, nLine);
150 if (!pmutexIn)
return;
152 lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
154 TryEnter(pszName, pszFile, nLine);
156 Enter(pszName, pszFile, nLine);
161 if (lock.owns_lock())
167 return lock.owns_lock();
173 #define PASTE(x, y) x ## y 174 #define PASTE2(x, y) PASTE(x, y) 176 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__) 177 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__) 178 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true) 180 #define ENTER_CRITICAL_SECTION(cs) \ 182 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \ 186 #define LEAVE_CRITICAL_SECTION(cs) \ 204 boost::unique_lock<boost::mutex> lock(mutex);
206 condition.wait(lock);
213 boost::unique_lock<boost::mutex> lock(mutex);
223 boost::unique_lock<boost::mutex> lock(mutex);
226 condition.notify_one();
284 operator bool()
const 290 #endif // RAVEN_SYNC_H void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
boost::condition_variable CConditionVariable
Just a typedef for boost::condition_variable, can be wrapped later if desired.
CMutexLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
CMutexLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
~CMutexLock() UNLOCK_FUNCTION()
RAII-style semaphore lock.
CMutexLock< CCriticalSection > CCriticalBlock
void lock() EXCLUSIVE_LOCK_FUNCTION()
#define UNLOCK_FUNCTION(...)
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
void Enter(const char *pszName, const char *pszFile, int nLine)
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
AnnotatedMixin< boost::mutex > CWaitableCriticalSection
Wrapped boost mutex: supports waiting but not recursive locking.
Wrapper around boost::unique_lock<Mutex>
Template mixin that adds -Wthread-safety locking annotations to a subset of the mutex API...
boost::condition_variable condition
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
boost::unique_lock< Mutex > lock
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...