|
|
|
@ -10,7 +10,9 @@
@@ -10,7 +10,9 @@
|
|
|
|
|
|
|
|
|
|
#include <boost/thread/condition_variable.hpp> |
|
|
|
|
#include <boost/thread/mutex.hpp> |
|
|
|
|
#include <boost/thread/recursive_mutex.hpp> |
|
|
|
|
#include <condition_variable> |
|
|
|
|
#include <thread> |
|
|
|
|
#include <mutex> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
@ -21,17 +23,17 @@
@@ -21,17 +23,17 @@
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
CCriticalSection mutex; |
|
|
|
|
boost::recursive_mutex mutex; |
|
|
|
|
std::recursive_mutex mutex; |
|
|
|
|
|
|
|
|
|
LOCK(mutex); |
|
|
|
|
boost::unique_lock<boost::recursive_mutex> criticalblock(mutex); |
|
|
|
|
std::unique_lock<std::recursive_mutex> criticalblock(mutex); |
|
|
|
|
|
|
|
|
|
LOCK2(mutex1, mutex2); |
|
|
|
|
boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1); |
|
|
|
|
boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2); |
|
|
|
|
std::unique_lock<std::recursive_mutex> criticalblock1(mutex1); |
|
|
|
|
std::unique_lock<std::recursive_mutex> criticalblock2(mutex2); |
|
|
|
|
|
|
|
|
|
TRY_LOCK(mutex, name); |
|
|
|
|
boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t); |
|
|
|
|
std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t); |
|
|
|
|
|
|
|
|
|
ENTER_CRITICAL_SECTION(mutex); // no RAII
|
|
|
|
|
mutex.lock(); |
|
|
|
@ -85,10 +87,10 @@ void static inline DeleteLock(void* cs) {}
@@ -85,10 +87,10 @@ void static inline DeleteLock(void* cs) {}
|
|
|
|
|
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapped boost mutex: supports recursive locking, but no waiting |
|
|
|
|
* Wrapped mutex: supports recursive locking, but no waiting |
|
|
|
|
* TODO: We should move away from using the recursive lock by default. |
|
|
|
|
*/ |
|
|
|
|
class CCriticalSection : public AnnotatedMixin<boost::recursive_mutex> |
|
|
|
|
class CCriticalSection : public AnnotatedMixin<std::recursive_mutex> |
|
|
|
|
{ |
|
|
|
|
public: |
|
|
|
|
~CCriticalSection() { |
|
|
|
@ -96,22 +98,24 @@ public:
@@ -96,22 +98,24 @@ public:
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** Wrapped boost mutex: supports waiting but not recursive locking */ |
|
|
|
|
typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection; |
|
|
|
|
/** Wrapped mutex: supports waiting but not recursive locking */ |
|
|
|
|
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection; |
|
|
|
|
|
|
|
|
|
/** Just a typedef for boost::condition_variable, can be wrapped later if desired */ |
|
|
|
|
typedef boost::condition_variable CConditionVariable; |
|
|
|
|
/** Just a typedef for std::condition_variable, can be wrapped later if desired */ |
|
|
|
|
typedef std::condition_variable CConditionVariable; |
|
|
|
|
|
|
|
|
|
/** Just a typedef for std::unique_lock, can be wrapped later if desired */ |
|
|
|
|
typedef std::unique_lock<std::mutex> WaitableLock; |
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_LOCKCONTENTION |
|
|
|
|
void PrintLockContention(const char* pszName, const char* pszFile, int nLine); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/** Wrapper around boost::unique_lock<Mutex> */ |
|
|
|
|
template <typename Mutex> |
|
|
|
|
class SCOPED_LOCKABLE CMutexLock |
|
|
|
|
/** Wrapper around std::unique_lock<CCriticalSection> */ |
|
|
|
|
class SCOPED_LOCKABLE CCriticalBlock |
|
|
|
|
{ |
|
|
|
|
private: |
|
|
|
|
boost::unique_lock<Mutex> lock; |
|
|
|
|
std::unique_lock<CCriticalSection> lock; |
|
|
|
|
|
|
|
|
|
void Enter(const char* pszName, const char* pszFile, int nLine) |
|
|
|
|
{ |
|
|
|
@ -136,7 +140,7 @@ private:
@@ -136,7 +140,7 @@ private:
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock) |
|
|
|
|
CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock) |
|
|
|
|
{ |
|
|
|
|
if (fTry) |
|
|
|
|
TryEnter(pszName, pszFile, nLine); |
|
|
|
@ -144,18 +148,18 @@ public:
@@ -144,18 +148,18 @@ public:
|
|
|
|
|
Enter(pszName, pszFile, nLine); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn) |
|
|
|
|
CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn) |
|
|
|
|
{ |
|
|
|
|
if (!pmutexIn) return; |
|
|
|
|
|
|
|
|
|
lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock); |
|
|
|
|
lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock); |
|
|
|
|
if (fTry) |
|
|
|
|
TryEnter(pszName, pszFile, nLine); |
|
|
|
|
else |
|
|
|
|
Enter(pszName, pszFile, nLine); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
~CMutexLock() UNLOCK_FUNCTION() |
|
|
|
|
~CCriticalBlock() UNLOCK_FUNCTION() |
|
|
|
|
{ |
|
|
|
|
if (lock.owns_lock()) |
|
|
|
|
LeaveCritical(); |
|
|
|
@ -167,8 +171,6 @@ public:
@@ -167,8 +171,6 @@ public:
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
typedef CMutexLock<CCriticalSection> CCriticalBlock; |
|
|
|
|
|
|
|
|
|
#define PASTE(x, y) x ## y |
|
|
|
|
#define PASTE2(x, y) PASTE(x, y) |
|
|
|
|
|
|
|
|
|