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>
16 template <
typename PARENT>
32 return PARENT::try_lock();
43 #ifdef DEBUG_LOCKORDER
44 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
47 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
48 void static inline LeaveCritical() {}
51 #ifdef DEBUG_LOCKCONTENTION
52 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
56 template<
typename Mutex>
60 boost::unique_lock<Mutex>
lock;
62 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
64 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()));
65 #ifdef DEBUG_LOCKCONTENTION
68 PrintLockContention(pszName, pszFile, nLine);
71 #ifdef DEBUG_LOCKCONTENTION
76 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
78 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()),
true);
80 if (!lock.owns_lock())
82 return lock.owns_lock();
86 CMutexLock(Mutex& mutexIn,
const char* pszName,
const char* pszFile,
int nLine,
bool fTry =
false) : lock(mutexIn,
boost::defer_lock)
91 Enter(pszName, pszFile, nLine);
102 return lock.owns_lock();
108 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
109 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
110 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
112 #define ENTER_CRITICAL_SECTION(cs) \
114 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
118 #define LEAVE_CRITICAL_SECTION(cs) \
135 boost::unique_lock<boost::mutex> lock(mutex);
137 condition.wait(lock);
143 boost::unique_lock<boost::mutex> lock(mutex);
152 boost::unique_lock<boost::mutex> lock(mutex);
155 condition.notify_one();
void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
RAII-style semaphore lock.
CMutexLock< CCriticalSection > CCriticalBlock
void lock() EXCLUSIVE_LOCK_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
#define UNLOCK_FUNCTION(...)
boost::condition_variable condition
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
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