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>
60 template <
typename PARENT>
76 return PARENT::try_lock();
87 #ifdef DEBUG_LOCKORDER
88 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
90 std::string LocksHeld();
93 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
97 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
99 #ifdef DEBUG_LOCKCONTENTION
100 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
104 template<
typename Mutex>
108 boost::unique_lock<Mutex>
lock;
110 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
112 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()));
113 #ifdef DEBUG_LOCKCONTENTION
114 if (!lock.try_lock())
116 PrintLockContention(pszName, pszFile, nLine);
119 #ifdef DEBUG_LOCKCONTENTION
124 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
126 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()),
true);
128 if (!lock.owns_lock())
130 return lock.owns_lock();
134 CMutexLock(Mutex& mutexIn,
const char* pszName,
const char* pszFile,
int nLine,
bool fTry =
false) : lock(mutexIn,
boost::defer_lock)
139 Enter(pszName, pszFile, nLine);
144 if (lock.owns_lock())
150 return lock.owns_lock();
156 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
157 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
158 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
160 #define ENTER_CRITICAL_SECTION(cs) \
162 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
166 #define LEAVE_CRITICAL_SECTION(cs) \
183 boost::unique_lock<boost::mutex> lock(mutex);
185 condition.wait(lock);
191 boost::unique_lock<boost::mutex> lock(mutex);
200 boost::unique_lock<boost::mutex> lock(mutex);
203 condition.notify_one();
void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_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.
static void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
static void EnterCritical(const char *pszName, const char *pszFile, int nLine, void *cs, bool fTry=false)
Wrapper around boost::unique_lock
static void LeaveCritical()
boost::condition_variable condition
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
AnnotatedMixin< boost::recursive_mutex > CCriticalSection
Wrapped boost mutex: supports recursive locking, but no waiting.
CMutexLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false)
boost::unique_lock< Mutex > lock