From 4924f0c99b021869967f4587df703084d2bdc8ed Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Fri, 14 Oct 2011 12:38:48 -0500
Subject: b782a75c99e6 backout cleanup

---
 indra/llcommon/llmemory.cpp             |   2 +-
 indra/llcommon/llthread.cpp             | 100 +++++++++++++++++---------------
 indra/llcommon/llthread.h               |  14 ++++-
 indra/llimage/llimagedxt.cpp            |   1 +
 indra/llimage/llimagej2c.cpp            |   1 +
 indra/llmessage/llcurl.cpp              |   6 +-
 indra/llmessage/lliosocket.h            |   6 --
 indra/llmessage/llproxy.cpp             |   4 +-
 indra/newview/llfloatermodelpreview.cpp |   4 +-
 indra/newview/llmeshrepository.cpp      |  14 ++---
 indra/newview/lltexturecache.cpp        |   1 +
 indra/newview/llviewermenufile.cpp      |   2 +-
 12 files changed, 84 insertions(+), 71 deletions(-)

diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp
index 3b27a1639a..3c5c20d0bf 100644
--- a/indra/llcommon/llmemory.cpp
+++ b/indra/llcommon/llmemory.cpp
@@ -1325,7 +1325,7 @@ LLPrivateMemoryPool::LLPrivateMemoryPool(S32 type, U32 max_pool_size) :
 {
 	if(type == STATIC_THREADED || type == VOLATILE_THREADED)
 	{
-		mMutexp = new LLMutex ;
+		mMutexp = new LLMutex(NULL) ;
 	}
 
 	for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
diff --git a/indra/llcommon/llthread.cpp b/indra/llcommon/llthread.cpp
index ed4f9eb376..4063cc730b 100644
--- a/indra/llcommon/llthread.cpp
+++ b/indra/llcommon/llthread.cpp
@@ -36,12 +36,6 @@
 #include <sched.h>
 #endif
 
-#if !LL_DARWIN
-U32 ll_thread_local local_thread_ID = 0;
-#endif 
-
-U32 LLThread::sIDIter = 0;
-
 //----------------------------------------------------------------------------
 // Usage:
 // void run_func(LLThread* thread)
@@ -62,6 +56,12 @@ U32 LLThread::sIDIter = 0;
 // 
 //----------------------------------------------------------------------------
 
+#if !LL_DARWIN
+U32 ll_thread_local sThreadID = 0;
+#endif 
+
+U32 LLThread::sIDIter = 0;
+
 LL_COMMON_API void assert_main_thread()
 {
 	static U32 s_thread_id = LLThread::currentID();
@@ -79,7 +79,7 @@ void *APR_THREAD_FUNC LLThread::staticRun(apr_thread_t *apr_threadp, void *datap
 	LLThread *threadp = (LLThread *)datap;
 
 #if !LL_DARWIN
-	local_thread_ID = threadp->mID;
+	sThreadID = threadp->mID;
 #endif
 
 	// Run the user supplied function
@@ -100,6 +100,8 @@ LLThread::LLThread(const std::string& name, apr_pool_t *poolp) :
 	mAPRThreadp(NULL),
 	mStatus(STOPPED)
 {
+	mID = ++sIDIter;
+
 	// Thread creation probably CAN be paranoid about APR being initialized, if necessary
 	if (poolp)
 	{
@@ -162,7 +164,7 @@ void LLThread::shutdown()
 		if (!isStopped())
 		{
 			// This thread just wouldn't stop, even though we gave it time
-			//llwarns << "LLThread::shutdown() exiting thread before clean exit!" << llendl;
+			//llwarns << "LLThread::~LLThread() exiting thread before clean exit!" << llendl;
 			// Put a stake in its heart.
 			apr_thread_exit(mAPRThreadp, -1);
 			return;
@@ -302,7 +304,7 @@ void LLThread::wakeLocked()
 //============================================================================
 
 LLMutex::LLMutex(apr_pool_t *poolp) :
-	mAPRMutexp(NULL)
+	mAPRMutexp(NULL), mCount(0), mLockingThread(NO_THREAD)
 {
 	//if (poolp)
 	//{
@@ -321,7 +323,8 @@ LLMutex::LLMutex(apr_pool_t *poolp) :
 LLMutex::~LLMutex()
 {
 #if MUTEX_DEBUG
-	llassert_always(!isLocked()); // better not be locked!
+	//bad assertion, the subclass LLSignal might be "locked", and that's OK
+	//llassert_always(!isLocked()); // better not be locked!
 #endif
 	apr_thread_mutex_destroy(mAPRMutexp);
 	mAPRMutexp = NULL;
@@ -334,7 +337,18 @@ LLMutex::~LLMutex()
 
 void LLMutex::lock()
 {
+#if LL_DARWIN
+	if (mLockingThread == LLThread::currentID())
+#else
+	if (mLockingThread == sThreadID)
+#endif
+	{ //redundant lock
+		mCount++;
+		return;
+	}
+	
 	apr_thread_mutex_lock(mAPRMutexp);
+	
 #if MUTEX_DEBUG
 	// Have to have the lock before we can access the debug info
 	U32 id = LLThread::currentID();
@@ -342,10 +356,22 @@ void LLMutex::lock()
 		llerrs << "Already locked in Thread: " << id << llendl;
 	mIsLocked[id] = TRUE;
 #endif
+
+#if LL_DARWIN
+	mLockingThread = LLThread::currentID();
+#else
+	mLockingThread = sThreadID;
+#endif
 }
 
 void LLMutex::unlock()
 {
+	if (mCount > 0)
+	{ //not the root unlock
+		mCount--;
+		return;
+	}
+	
 #if MUTEX_DEBUG
 	// Access the debug info while we have the lock
 	U32 id = LLThread::currentID();
@@ -353,6 +379,8 @@ void LLMutex::unlock()
 		llerrs << "Not locked in Thread: " << id << llendl;	
 	mIsLocked[id] = FALSE;
 #endif
+
+	mLockingThread = NO_THREAD;
 	apr_thread_mutex_unlock(mAPRMutexp);
 }
 
@@ -370,6 +398,11 @@ bool LLMutex::isLocked()
 	}
 }
 
+U32 LLMutex::lockingThread() const
+{
+	return mLockingThread;
+}
+
 //============================================================================
 
 LLCondition::LLCondition(apr_pool_t *poolp) :
@@ -390,6 +423,15 @@ LLCondition::~LLCondition()
 
 void LLCondition::wait()
 {
+	if (!isLocked())
+	{ //mAPRMutexp MUST be locked before calling apr_thread_cond_wait
+		apr_thread_mutex_lock(mAPRMutexp);
+#if MUTEX_DEBUG
+		// avoid asserts on destruction in non-release builds
+		U32 id = LLThread::currentID();
+		mIsLocked[id] = TRUE;
+#endif
+	}
 	apr_thread_cond_wait(mAPRCondp, mAPRMutexp);
 }
 
@@ -404,44 +446,6 @@ void LLCondition::broadcast()
 }
 
 //============================================================================
-LLMutexBase::LLMutexBase() :
-	mLockingThread(NO_THREAD),
-	mCount(0)
-{
-}
-
-void LLMutexBase::lock() 
-{ 
-#if LL_DARWIN
-	if (mLockingThread == LLThread::currentID())
-#else
-	if (mLockingThread == local_thread_ID)
-#endif
-	{ //redundant lock
-		mCount++;
-		return;
-	}
-
-	apr_thread_mutex_lock(mAPRMutexp); 
-
-#if LL_DARWIN
-	mLockingThread = LLThread::currentID();
-#else
-	mLockingThread = local_thread_ID;
-#endif
-}
-
-void LLMutexBase::unlock() 
-{ 
-	if (mCount > 0)
-	{ //not the root unlock
-		mCount--;
-		return;
-	}
-	mLockingThread = NO_THREAD;
-
-	apr_thread_mutex_unlock(mAPRMutexp); 
-}
 
 //----------------------------------------------------------------------------
 
diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h
index ad4a6523a1..40291a2569 100644
--- a/indra/llcommon/llthread.h
+++ b/indra/llcommon/llthread.h
@@ -86,6 +86,8 @@ public:
 	apr_pool_t *getAPRPool() { return mAPRPoolp; }
 	LLVolatileAPRPool* getLocalAPRFilePool() { return mLocalAPRFilePoolp ; }
 
+	U32 getID() const { return mID; }
+
 private:
 	BOOL				mPaused;
 	
@@ -101,7 +103,7 @@ protected:
 	BOOL				mIsLocalPool;
 	EThreadStatus		mStatus;
 	U32					mID;
-	
+
 	//a local apr_pool for APRFile operations in this thread. If it exists, LLAPRFile::sAPRFilePoolp should not be used.
 	//Note: this pool is used by APRFile ONLY, do NOT use it for any other purposes.
 	//      otherwise it will cause severe memory leaking!!! --bao
@@ -138,17 +140,27 @@ protected:
 class LL_COMMON_API LLMutex
 {
 public:
+	typedef enum
+	{
+		NO_THREAD = 0xFFFFFFFF
+	} e_locking_thread;
+
 	LLMutex(apr_pool_t *apr_poolp); // NULL pool constructs a new pool for the mutex
 	virtual ~LLMutex();
 	
 	void lock();		// blocks
 	void unlock();
 	bool isLocked(); 	// non-blocking, but does do a lock/unlock so not free
+	U32 lockingThread() const; //get ID of locking thread
 	
 protected:
 	apr_thread_mutex_t *mAPRMutexp;
+	mutable U32			mCount;
+	mutable U32			mLockingThread;
+	
 	apr_pool_t			*mAPRPoolp;
 	BOOL				mIsLocalPool;
+	
 #if MUTEX_DEBUG
 	std::map<U32, BOOL> mIsLocked;
 #endif
diff --git a/indra/llimage/llimagedxt.cpp b/indra/llimage/llimagedxt.cpp
index 2867f5e6f0..34c6793522 100644
--- a/indra/llimage/llimagedxt.cpp
+++ b/indra/llimage/llimagedxt.cpp
@@ -26,6 +26,7 @@
 #include "linden_common.h"
 
 #include "llimagedxt.h"
+#include "llmemory.h"
 
 //static
 void LLImageDXT::checkMinWidthHeight(EFileFormat format, S32& width, S32& height)
diff --git a/indra/llimage/llimagej2c.cpp b/indra/llimage/llimagej2c.cpp
index c44a66f552..cc8cb66d73 100644
--- a/indra/llimage/llimagej2c.cpp
+++ b/indra/llimage/llimagej2c.cpp
@@ -29,6 +29,7 @@
 #include "llmemtype.h"
 #include "lltimer.h"
 #include "llmath.h"
+#include "llmemory.h"
 
 typedef LLImageJ2CImpl* (*CreateLLImageJ2CFunction)();
 typedef void (*DestroyLLImageJ2CFunction)(LLImageJ2CImpl*);
diff --git a/indra/llmessage/llcurl.cpp b/indra/llmessage/llcurl.cpp
index 6e063818e2..330028c926 100644
--- a/indra/llmessage/llcurl.cpp
+++ b/indra/llmessage/llcurl.cpp
@@ -531,7 +531,7 @@ LLCurl::Multi::Multi()
 	mThreaded = LLCurl::sMultiThreaded && LLThread::currentID() == sMainThreadID;
 	if (mThreaded)
 	{
-		mSignal = new LLCondition();
+		mSignal = new LLCondition(NULL);
 	}
 	else
 	{
@@ -1189,8 +1189,8 @@ void LLCurl::initClass(bool multi_threaded)
 
 	check_curl_code(code);
 	
-	Easy::sHandleMutex = new LLMutex();
-	Easy::sMultiMutex = new LLMutex();
+	Easy::sHandleMutex = new LLMutex(NULL);
+	Easy::sMultiMutex = new LLMutex(NULL);
 
 #if SAFE_SSL
 	S32 mutex_count = CRYPTO_num_locks();
diff --git a/indra/llmessage/lliosocket.h b/indra/llmessage/lliosocket.h
index d8ee4e9f98..be0f7dfcc6 100644
--- a/indra/llmessage/lliosocket.h
+++ b/indra/llmessage/lliosocket.h
@@ -145,12 +145,6 @@ public:
 	 */
 	apr_socket_t* getSocket() const { return mSocket; }
 
-	/** 
-	 * @brief Protected constructor since should only make sockets
-	 * with one of the two <code>create()</code> calls.
-	 */
-	LLSocket(apr_socket_t* socket, apr_pool_t* pool);
-
 	/** 
 	 * @brief Set default socket options, with SO_NONBLOCK = 0 and a timeout in us.
 	 * @param timeout Number of microseconds to wait on this socket. Any
diff --git a/indra/llmessage/llproxy.cpp b/indra/llmessage/llproxy.cpp
index 4a7d326c0e..9988fcd9c0 100644
--- a/indra/llmessage/llproxy.cpp
+++ b/indra/llmessage/llproxy.cpp
@@ -49,7 +49,7 @@ static void tcp_close_channel(LLSocket::ptr_t* handle_ptr); // Close an open TCP
 
 LLProxy::LLProxy():
 		mHTTPProxyEnabled(false),
-		mProxyMutex(),
+		mProxyMutex(NULL),
 		mUDPProxy(),
 		mTCPProxy(),
 		mHTTPProxy(),
@@ -524,7 +524,7 @@ static apr_status_t tcp_blocking_handshake(LLSocket::ptr_t handle, char * dataou
  */
 static LLSocket::ptr_t tcp_open_channel(LLHost host)
 {
-	LLSocket::ptr_t socket = LLSocket::create(LLSocket::STREAM_TCP);
+	LLSocket::ptr_t socket = LLSocket::create(NULL, LLSocket::STREAM_TCP);
 	bool connected = socket->blockingConnect(host);
 	if (!connected)
 	{
diff --git a/indra/newview/llfloatermodelpreview.cpp b/indra/newview/llfloatermodelpreview.cpp
index 5869cf6fee..881f087d7b 100644
--- a/indra/newview/llfloatermodelpreview.cpp
+++ b/indra/newview/llfloatermodelpreview.cpp
@@ -388,7 +388,7 @@ mCalculateBtn(NULL)
 	mLastMouseX = 0;
 	mLastMouseY = 0;
 	mGLName = 0;
-	mStatusLock = new LLMutex();
+	mStatusLock = new LLMutex(NULL);
 	mModelPreview = NULL;
 
 	mLODMode[LLModel::LOD_HIGH] = 0;
@@ -3077,7 +3077,7 @@ LLColor4 LLModelLoader::getDaeColor(daeElement* element)
 //-----------------------------------------------------------------------------
 
 LLModelPreview::LLModelPreview(S32 width, S32 height, LLFloater* fmp)
-: LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE), LLMutex()
+: LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE), LLMutex(NULL)
 , mPelvisZOffset( 0.0f )
 , mLegacyRigValid( false )
 , mRigValidJointUpload( false )
diff --git a/indra/newview/llmeshrepository.cpp b/indra/newview/llmeshrepository.cpp
index e12f140747..a97e256c89 100644
--- a/indra/newview/llmeshrepository.cpp
+++ b/indra/newview/llmeshrepository.cpp
@@ -447,9 +447,9 @@ LLMeshRepoThread::LLMeshRepoThread()
 : LLThread("mesh repo") 
 { 
 	mWaiting = false;
-	mMutex = new LLMutex();
-	mHeaderMutex = new LLMutex();
-	mSignal = new LLCondition();
+	mMutex = new LLMutex(NULL);
+	mHeaderMutex = new LLMutex(NULL);
+	mSignal = new LLCondition(NULL);
 }
 
 LLMeshRepoThread::~LLMeshRepoThread()
@@ -1198,7 +1198,7 @@ LLMeshUploadThread::LLMeshUploadThread(LLMeshUploadThread::instance_list& data,
 	mUploadTextures = upload_textures;
 	mUploadSkin = upload_skin;
 	mUploadJoints = upload_joints;
-	mMutex = new LLMutex();
+	mMutex = new LLMutex(NULL);
 	mCurlRequest = NULL;
 	mPendingUploads = 0;
 	mFinished = false;
@@ -2043,7 +2043,7 @@ LLMeshRepository::LLMeshRepository()
 
 void LLMeshRepository::init()
 {
-	mMeshMutex = new LLMutex();
+	mMeshMutex = new LLMutex(NULL);
 	
 	LLConvexDecomposition::getInstance()->initSystem();
 
@@ -2866,8 +2866,8 @@ LLPhysicsDecomp::LLPhysicsDecomp()
 	mQuitting = false;
 	mDone = false;
 
-	mSignal = new LLCondition();
-	mMutex = new LLMutex();
+	mSignal = new LLCondition(NULL);
+	mMutex = new LLMutex(NULL);
 }
 
 LLPhysicsDecomp::~LLPhysicsDecomp()
diff --git a/indra/newview/lltexturecache.cpp b/indra/newview/lltexturecache.cpp
index 9e2aede3ef..70b0a31308 100644
--- a/indra/newview/lltexturecache.cpp
+++ b/indra/newview/lltexturecache.cpp
@@ -36,6 +36,7 @@
 
 // Included to allow LLTextureCache::purgeTextures() to pause watchdog timeout
 #include "llappviewer.h" 
+#include "llmemory.h"
 
 // Cache organization:
 // cache/texture.entries
diff --git a/indra/newview/llviewermenufile.cpp b/indra/newview/llviewermenufile.cpp
index 2f9a555903..b9293b3b31 100644
--- a/indra/newview/llviewermenufile.cpp
+++ b/indra/newview/llviewermenufile.cpp
@@ -149,7 +149,7 @@ void LLFilePickerThread::run()
 //static
 void LLFilePickerThread::initClass()
 {
-	sMutex = new LLMutex();
+	sMutex = new LLMutex(NULL);
 }
 
 //static
-- 
cgit v1.2.3