From 14ba0c1930627de6c47dc2a699fd4712c92ecc5f Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Wed, 21 Oct 2020 01:03:55 -0400
Subject: [PATCH] Change LLMutex internal mutex type from std::mutex to
 absl::Mutex

---
 indra/llcommon/llmutex.cpp | 51 ++++++++------------------------------
 indra/llcommon/llmutex.h   | 44 +++++++++++++++++++++++---------
 2 files changed, 42 insertions(+), 53 deletions(-)

diff --git a/indra/llcommon/llmutex.cpp b/indra/llcommon/llmutex.cpp
index 70655a43337..e9adfda99e8 100644
--- a/indra/llcommon/llmutex.cpp
+++ b/indra/llcommon/llmutex.cpp
@@ -45,7 +45,7 @@ void LLMutex::lock()
 		return;
 	}
 	
-	mMutex.lock();
+	mMutex.Lock();
 	
 #if MUTEX_DEBUG
 	// Have to have the lock before we can access the debug info
@@ -75,18 +75,18 @@ void LLMutex::unlock()
 #endif
 
 	mLockingThread = LLThread::id_t();
-	mMutex.unlock();
+	mMutex.Unlock();
 }
 
 bool LLMutex::isLocked()
 {
-	if (!mMutex.try_lock())
+	if (!mMutex.TryLock())
 	{
 		return true;
 	}
 	else
 	{
-		mMutex.unlock();
+		mMutex.Unlock();
 		return false;
 	}
 }
@@ -109,7 +109,7 @@ bool LLMutex::trylock()
 		return true;
 	}
 	
-	if (!mMutex.try_lock())
+	if (!mMutex.TryLock())
 	{
 		return false;
 	}
@@ -136,50 +136,19 @@ LLCondition::LLCondition() :
 
 void LLCondition::wait()
 {
-	std::unique_lock< std::mutex > lock(mMutex);
-	mCond.wait(lock);
+	mMutex.Lock();
+	mCond.Wait(&mMutex);
+	mMutex.Unlock();
 }
 
 void LLCondition::signal()
 {
-	mCond.notify_one();
+	mCond.Signal();
 }
 
 void LLCondition::broadcast()
 {
-	mCond.notify_all();
-}
-
-
-
-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();
+	mCond.SignalAll();
 }
 
 //============================================================================
diff --git a/indra/llcommon/llmutex.h b/indra/llcommon/llmutex.h
index ec2166d3686..a7bce83f5e8 100644
--- a/indra/llcommon/llmutex.h
+++ b/indra/llcommon/llmutex.h
@@ -32,10 +32,6 @@
 
 #include "absl/synchronization/mutex.h"
 
-#include <mutex>
-#include <condition_variable>
-
-
 //============================================================================
 
 #define MUTEX_DEBUG (LL_DEBUG || LL_RELEASE_WITH_DEBUG_INFO)
@@ -50,7 +46,6 @@ class LL_COMMON_API LLMutex
 {
 public:
 	LLMutex();
-	virtual ~LLMutex() = default;
 	
 	void lock();		// blocks
 	bool trylock();		// non-blocking, returns true if lock held.
@@ -60,7 +55,7 @@ class LL_COMMON_API LLMutex
 	LLThread::id_t lockingThread() const; //get ID of locking thread
 
 protected:
-	std::mutex			mMutex;
+	absl::Mutex			mMutex;
 	mutable U32			mCount;
 	mutable LLThread::id_t	mLockingThread;
 	
@@ -70,18 +65,17 @@ class LL_COMMON_API LLMutex
 };
 
 // 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:
 	LLCondition();
-	~LLCondition() = default;
 	
 	void wait();		// blocks
 	void signal();
 	void broadcast();
 	
 protected:
-	std::condition_variable mCond;
+	absl::CondVar mCond;
 };
 
 class LLMutexLock
@@ -120,9 +114,35 @@ class LLMutexLock
 class LLMutexTrylock
 {
 public:
-	LLMutexTrylock(LLMutex* mutex);
-	LLMutexTrylock(LLMutex* mutex, U32 aTries, U32 delay_ms = 10);
-	~LLMutexTrylock();
+	LLMutexTrylock(LLMutex* mutex)
+		: mMutex(mutex),
+		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
 	{
-- 
GitLab