Skip to content
Snippets Groups Projects
Commit 14ba0c19 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Change LLMutex internal mutex type from std::mutex to absl::Mutex

parent 2cafc3a6
No related branches found
No related tags found
No related merge requests found
...@@ -45,7 +45,7 @@ void LLMutex::lock() ...@@ -45,7 +45,7 @@ void LLMutex::lock()
return; return;
} }
mMutex.lock(); mMutex.Lock();
#if MUTEX_DEBUG #if MUTEX_DEBUG
// Have to have the lock before we can access the debug info // Have to have the lock before we can access the debug info
...@@ -75,18 +75,18 @@ void LLMutex::unlock() ...@@ -75,18 +75,18 @@ void LLMutex::unlock()
#endif #endif
mLockingThread = LLThread::id_t(); mLockingThread = LLThread::id_t();
mMutex.unlock(); mMutex.Unlock();
} }
bool LLMutex::isLocked() bool LLMutex::isLocked()
{ {
if (!mMutex.try_lock()) if (!mMutex.TryLock())
{ {
return true; return true;
} }
else else
{ {
mMutex.unlock(); mMutex.Unlock();
return false; return false;
} }
} }
...@@ -109,7 +109,7 @@ bool LLMutex::trylock() ...@@ -109,7 +109,7 @@ bool LLMutex::trylock()
return true; return true;
} }
if (!mMutex.try_lock()) if (!mMutex.TryLock())
{ {
return false; return false;
} }
...@@ -136,50 +136,19 @@ LLCondition::LLCondition() : ...@@ -136,50 +136,19 @@ LLCondition::LLCondition() :
void LLCondition::wait() void LLCondition::wait()
{ {
std::unique_lock< std::mutex > lock(mMutex); mMutex.Lock();
mCond.wait(lock); mCond.Wait(&mMutex);
mMutex.Unlock();
} }
void LLCondition::signal() void LLCondition::signal()
{ {
mCond.notify_one(); mCond.Signal();
} }
void LLCondition::broadcast() void LLCondition::broadcast()
{ {
mCond.notify_all(); mCond.SignalAll();
}
LLMutexTrylock::LLMutexTrylock(LLMutex* mutex)
: mMutex(mutex),
mLocked(false)
{
if (mMutex)
mLocked = mMutex->trylock();
}
LLMutexTrylock::LLMutexTrylock(LLMutex* mutex, U32 aTries, U32 delay_ms)
: mMutex(mutex),
mLocked(false)
{
if (!mMutex)
return;
for (U32 i = 0; i < aTries; ++i)
{
mLocked = mMutex->trylock();
if (mLocked)
break;
ms_sleep(delay_ms);
}
}
LLMutexTrylock::~LLMutexTrylock()
{
if (mMutex && mLocked)
mMutex->unlock();
} }
//============================================================================ //============================================================================
...@@ -32,10 +32,6 @@ ...@@ -32,10 +32,6 @@
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include <mutex>
#include <condition_variable>
//============================================================================ //============================================================================
#define MUTEX_DEBUG (LL_DEBUG || LL_RELEASE_WITH_DEBUG_INFO) #define MUTEX_DEBUG (LL_DEBUG || LL_RELEASE_WITH_DEBUG_INFO)
...@@ -50,7 +46,6 @@ class LL_COMMON_API LLMutex ...@@ -50,7 +46,6 @@ class LL_COMMON_API LLMutex
{ {
public: public:
LLMutex(); LLMutex();
virtual ~LLMutex() = default;
void lock(); // blocks void lock(); // blocks
bool trylock(); // non-blocking, returns true if lock held. bool trylock(); // non-blocking, returns true if lock held.
...@@ -60,7 +55,7 @@ class LL_COMMON_API LLMutex ...@@ -60,7 +55,7 @@ class LL_COMMON_API LLMutex
LLThread::id_t lockingThread() const; //get ID of locking thread LLThread::id_t lockingThread() const; //get ID of locking thread
protected: protected:
std::mutex mMutex; absl::Mutex mMutex;
mutable U32 mCount; mutable U32 mCount;
mutable LLThread::id_t mLockingThread; mutable LLThread::id_t mLockingThread;
...@@ -70,18 +65,17 @@ class LL_COMMON_API LLMutex ...@@ -70,18 +65,17 @@ class LL_COMMON_API LLMutex
}; };
// Actually a condition/mutex pair (since each condition needs to be associated with a mutex). // Actually a condition/mutex pair (since each condition needs to be associated with a mutex).
class LL_COMMON_API LLCondition : public LLMutex class LL_COMMON_API LLCondition final : public LLMutex
{ {
public: public:
LLCondition(); LLCondition();
~LLCondition() = default;
void wait(); // blocks void wait(); // blocks
void signal(); void signal();
void broadcast(); void broadcast();
protected: protected:
std::condition_variable mCond; absl::CondVar mCond;
}; };
class LLMutexLock class LLMutexLock
...@@ -120,9 +114,35 @@ class LLMutexLock ...@@ -120,9 +114,35 @@ class LLMutexLock
class LLMutexTrylock class LLMutexTrylock
{ {
public: public:
LLMutexTrylock(LLMutex* mutex); LLMutexTrylock(LLMutex* mutex)
LLMutexTrylock(LLMutex* mutex, U32 aTries, U32 delay_ms = 10); : mMutex(mutex),
~LLMutexTrylock(); mLocked(false)
{
if (mMutex)
mLocked = mMutex->trylock();
}
LLMutexTrylock(LLMutex* mutex, U32 aTries, U32 delay_ms)
: mMutex(mutex),
mLocked(false)
{
if (!mMutex)
return;
for (U32 i = 0; i < aTries; ++i)
{
mLocked = mMutex->trylock();
if (mLocked)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
}
~LLMutexTrylock()
{
if (mMutex && mLocked)
mMutex->unlock();
}
bool isLocked() const bool isLocked() const
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment