From 09feaac844d67a94ffe8c98a201e1e7f2f84be9a Mon Sep 17 00:00:00 2001
From: Xiaohong Bao <bao@lindenlab.com>
Date: Mon, 5 Dec 2011 13:23:05 -0700
Subject: [PATCH] fix for sh-2738: Texture fetching freezes due to LLcurl

---
 indra/llcommon/llthread.h  | 11 ++++++--
 indra/llmessage/llcurl.cpp | 54 ++++++++++++++++++--------------------
 2 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h
index 40291a25693..b0a1c9e12b7 100644
--- a/indra/llcommon/llthread.h
+++ b/indra/llcommon/llthread.h
@@ -187,11 +187,18 @@ class LLMutexLock
 	LLMutexLock(LLMutex* mutex)
 	{
 		mMutex = mutex;
-		mMutex->lock();
+
+		if(mMutex)
+		{
+			mMutex->lock();
+		}
 	}
 	~LLMutexLock()
 	{
-		mMutex->unlock();
+		if(mMutex)
+		{
+			mMutex->unlock();
+		}
 	}
 private:
 	LLMutex* mMutex;
diff --git a/indra/llmessage/llcurl.cpp b/indra/llmessage/llcurl.cpp
index 7ca25d07fce..228f0391327 100644
--- a/indra/llmessage/llcurl.cpp
+++ b/indra/llmessage/llcurl.cpp
@@ -585,37 +585,30 @@ void LLCurl::Multi::unlock()
 
 void LLCurl::Multi::markDead()
 {
-	if(mDeletionMutexp)
-	{
-		mDeletionMutexp->lock() ;
-	}
-
+	LLMutexLock lock(mDeletionMutexp) ;
+	
 	mDead = TRUE ;
-
-	if(mDeletionMutexp)
-	{
-		mDeletionMutexp->unlock() ;
-	}
 }
 
 void LLCurl::Multi::setState(LLCurl::Multi::ePerformState state)
 {
-	lock() ;
+	LLMutexLock lock(mMutexp) ;
+
 	mState = state ;
 	if(mState == STATE_READY)
 	{
 		LLCurl::getCurlThread()->setPriority(mHandle, LLQueuedThread::PRIORITY_NORMAL) ;
 	}
-	unlock() ;
 }
 
 LLCurl::Multi::ePerformState LLCurl::Multi::getState()
 {
 	ePerformState state ;
 
-	lock() ;
-	state = mState ;
-	unlock() ;
+	{
+		LLMutexLock lock(mMutexp) ;
+		state = mState ;
+	}
 
 	return state ;
 }
@@ -635,13 +628,15 @@ bool LLCurl::Multi::waitToComplete()
 
 	bool completed ;
 
-	lock() ;
-	completed = (STATE_COMPLETED == mState) ;
-	if(!completed)
 	{
-		LLCurl::getCurlThread()->setPriority(mHandle, LLQueuedThread::PRIORITY_URGENT) ;
+		LLMutexLock lock(mMutexp) ;
+
+		completed = (STATE_COMPLETED == mState) ;
+		if(!completed)
+		{
+			LLCurl::getCurlThread()->setPriority(mHandle, LLQueuedThread::PRIORITY_URGENT) ;
+		}
 	}
-	unlock() ;
 
 	return completed;
 }
@@ -655,10 +650,8 @@ CURLMsg* LLCurl::Multi::info_read(S32* msgs_in_queue)
 //return true if dead
 bool LLCurl::Multi::doPerform()
 {
-	if(mDeletionMutexp)
-	{
-		mDeletionMutexp->lock() ;
-	}
+	LLMutexLock lock(mDeletionMutexp) ;
+	
 	bool dead = mDead ;
 
 	if(mDead)
@@ -675,6 +668,8 @@ bool LLCurl::Multi::doPerform()
 				call_count < MULTI_PERFORM_CALL_REPEAT;
 				call_count++)
 		{
+			LLMutexLock lock(mMutexp) ;
+
 			CURLMcode code = curl_multi_perform(mCurlMultiHandle, &q);
 			if (CURLM_CALL_MULTI_PERFORM != code || q == 0)
 			{
@@ -688,11 +683,6 @@ bool LLCurl::Multi::doPerform()
 		setState(STATE_COMPLETED) ;
 	}
 
-	if(mDeletionMutexp)
-	{
-		mDeletionMutexp->unlock() ;
-	}
-
 	return dead ;
 }
 
@@ -764,6 +754,8 @@ LLCurl::Easy* LLCurl::Multi::allocEasy()
 
 bool LLCurl::Multi::addEasy(Easy* easy)
 {
+	LLMutexLock lock(mMutexp) ;
+
 	CURLMcode mcode = curl_multi_add_handle(mCurlMultiHandle, easy->getCurlHandle());
 	check_curl_multi_code(mcode);
 	//if (mcode != CURLM_OK)
@@ -776,6 +768,8 @@ bool LLCurl::Multi::addEasy(Easy* easy)
 
 void LLCurl::Multi::easyFree(Easy* easy)
 {
+	LLMutexLock lock(mMutexp) ;
+
 	mEasyActiveList.erase(easy);
 	mEasyActiveMap.erase(easy->getCurlHandle());
 	if (mEasyFreeList.size() < EASY_HANDLE_POOL_SIZE)
@@ -791,6 +785,8 @@ void LLCurl::Multi::easyFree(Easy* easy)
 
 void LLCurl::Multi::removeEasy(Easy* easy)
 {
+	LLMutexLock lock(mMutexp) ;
+
 	check_curl_multi_code(curl_multi_remove_handle(mCurlMultiHandle, easy->getCurlHandle()));
 	easyFree(easy);
 }
-- 
GitLab