From 84af0e9852486231b5ef0cde7ad1704d41689a3a Mon Sep 17 00:00:00 2001
From: Richard Linden <none@none>
Date: Wed, 24 Apr 2013 14:13:45 -0700
Subject: SH-4080 WIP interesting: random crash on Mac potential fix for
 crasher cleaned up llsingleton

---
 indra/llcommon/llsingleton.cpp      |  1 -
 indra/llcommon/llsingleton.h        | 74 +++++++++----------------------------
 indra/llcommon/lltracerecording.cpp |  4 ++
 indra/newview/lltoolselect.h        |  2 +-
 indra/newview/llviewertexturelist.h |  1 +
 5 files changed, 24 insertions(+), 58 deletions(-)

diff --git a/indra/llcommon/llsingleton.cpp b/indra/llcommon/llsingleton.cpp
index eb8e2c9456..9b49e52377 100644
--- a/indra/llcommon/llsingleton.cpp
+++ b/indra/llcommon/llsingleton.cpp
@@ -28,5 +28,4 @@
 
 #include "llsingleton.h"
 
-std::map<std::string, void *> * LLSingletonRegistry::sSingletonMap = NULL;
 
diff --git a/indra/llcommon/llsingleton.h b/indra/llcommon/llsingleton.h
index f6b0a7194b..697d1b042a 100644
--- a/indra/llcommon/llsingleton.h
+++ b/indra/llcommon/llsingleton.h
@@ -30,38 +30,6 @@
 #include <typeinfo>
 #include <boost/noncopyable.hpp>
 
-/// @brief A global registry of all singletons to prevent duplicate allocations
-/// across shared library boundaries
-class LL_COMMON_API LLSingletonRegistry {
-	private:
-		typedef std::map<std::string, void *> TypeMap;
-		static TypeMap * sSingletonMap;
-
-		static void checkInit()
-		{
-			if(sSingletonMap == NULL)
-			{
-				sSingletonMap = new TypeMap();
-			}
-		}
-
-	public:
-		template<typename T> static void * & get()
-		{
-			std::string name(typeid(T).name());
-
-			checkInit();
-
-			// the first entry of the pair returned by insert will be either the existing
-			// iterator matching our key, or the newly inserted NULL initialized entry
-			// see "Insert element" in http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
-			TypeMap::iterator result =
-				sSingletonMap->insert(std::make_pair(name, (void*)NULL)).first;
-
-			return result->second;
-		}
-};
-
 // LLSingleton implements the getInstance() method part of the Singleton
 // pattern. It can't make the derived class constructors protected, though, so
 // you have to do that yourself.
@@ -90,10 +58,9 @@ template <typename DERIVED_TYPE>
 class LLSingleton : private boost::noncopyable
 {
 	
-protected:
+private:
 	typedef enum e_init_state
 	{
-		UNINITIALIZED,
 		CONSTRUCTING,
 		INITIALIZING,
 		INITIALIZED,
@@ -109,8 +76,11 @@ protected:
 		
 		SingletonInstanceData()
 		:	mSingletonInstance(NULL),
-			mInitState(UNINITIALIZED)
-		{}
+			mInitState(CONSTRUCTING)
+		{
+			mSingletonInstance = new DERIVED_TYPE(); 
+			mInitState = INITIALIZING;
+		}
 
 		~SingletonInstanceData()
 		{
@@ -151,12 +121,12 @@ public:
 	 */
 	static void deleteSingleton()
 	{
-		SingletonInstanceData& data = getSingletonData();
-		delete data.mSingletonInstance;
-		data.mSingletonInstance = NULL;
-		data.mInitState = DELETED;
+		delete getSingletonData().mSingletonInstance;
+		getSingletonData().mSingletonInstance = NULL;
+		getSingletonData().mInitState = DELETED;
 	}
 
+
 	static DERIVED_TYPE* getInstance()
 	{
 		SingletonInstanceData& data = getSingletonData();
@@ -171,13 +141,11 @@ public:
 			llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl;
 		}
 		
-		if (!data.mSingletonInstance) 
+		if (data.mInitState == INITIALIZING) 
 		{
-			data.mInitState = CONSTRUCTING;
-			data.mSingletonInstance = new DERIVED_TYPE(); 
-			data.mInitState = INITIALIZING;
-			data.mSingletonInstance->initSingleton(); 
+			// go ahead and flag ourselves as initialized so we can be reentrant during initialization
 			data.mInitState = INITIALIZED;	
+			data.mSingletonInstance->initSingleton(); 
 		}
 		
 		return data.mSingletonInstance;
@@ -185,7 +153,7 @@ public:
 
 	static DERIVED_TYPE* getIfExists()
 	{
-		SingletonInstanceData& data = getSingletonData();
+		SingletonInstanceData& data = getData();
 		return data.mSingletonInstance;
 	}
 
@@ -211,20 +179,14 @@ public:
 	}
 
 private:
+
 	static SingletonInstanceData& getSingletonData()
 	{
 		// this is static to cache the lookup results
-		static void * & registry = LLSingletonRegistry::get<DERIVED_TYPE>();
-
-		// *TODO - look into making this threadsafe
-		if(NULL == registry)
-		{
-			static SingletonInstanceData data;
-			registry = &data;
-		}
-
-		return *static_cast<SingletonInstanceData *>(registry);
+		static SingletonInstanceData sData;
+		return sData;
 	}
+
 	virtual void initSingleton() {}
 };
 
diff --git a/indra/llcommon/lltracerecording.cpp b/indra/llcommon/lltracerecording.cpp
index af7b61dd4e..e562f2bce2 100644
--- a/indra/llcommon/lltracerecording.cpp
+++ b/indra/llcommon/lltracerecording.cpp
@@ -362,6 +362,10 @@ PeriodicRecording::PeriodicRecording( U32 num_periods, EPlayState state)
 :	mAutoResize(num_periods == 0),
 	mCurPeriod(0)
 {
+	if (mAutoResize) 
+	{
+		num_periods = 1;
+	}
 	if (num_periods)
 	{
 		mRecordingPeriods.resize(num_periods);
diff --git a/indra/newview/lltoolselect.h b/indra/newview/lltoolselect.h
index baa27f6071..74dababe8c 100644
--- a/indra/newview/lltoolselect.h
+++ b/indra/newview/lltoolselect.h
@@ -34,7 +34,7 @@
 
 class LLObjectSelection;
 
-class LLToolSelect : public LLTool, public LLSingleton<LLToolSelect>
+class LLToolSelect : public LLTool
 {
 public:
 	LLToolSelect( LLToolComposite* composite );
diff --git a/indra/newview/llviewertexturelist.h b/indra/newview/llviewertexturelist.h
index e3df71cca2..7ce4a8fc70 100644
--- a/indra/newview/llviewertexturelist.h
+++ b/indra/newview/llviewertexturelist.h
@@ -34,6 +34,7 @@
 #include "llui.h"
 #include <list>
 #include <set>
+#include "lluiimage.h"
 
 const U32 LL_IMAGE_REZ_LOSSLESS_CUTOFF = 128;
 
-- 
cgit v1.2.3