diff options
Diffstat (limited to 'indra/llcommon')
-rw-r--r-- | indra/llcommon/lleventapi.h | 4 | ||||
-rw-r--r-- | indra/llcommon/lleventtimer.h | 2 | ||||
-rw-r--r-- | indra/llcommon/llfasttimer.cpp | 8 | ||||
-rw-r--r-- | indra/llcommon/llfasttimer.h | 4 | ||||
-rw-r--r-- | indra/llcommon/llinstancetracker.cpp | 22 | ||||
-rw-r--r-- | indra/llcommon/llinstancetracker.h | 58 | ||||
-rw-r--r-- | indra/llcommon/llleap.h | 2 | ||||
-rw-r--r-- | indra/llcommon/llsingleton.cpp | 1 | ||||
-rw-r--r-- | indra/llcommon/llsingleton.h | 60 | ||||
-rw-r--r-- | indra/llcommon/tests/llinstancetracker_test.cpp | 6 |
10 files changed, 29 insertions, 138 deletions
diff --git a/indra/llcommon/lleventapi.h b/indra/llcommon/lleventapi.h index 10c7e7a23f..5991fe8fd5 100644 --- a/indra/llcommon/lleventapi.h +++ b/indra/llcommon/lleventapi.h @@ -41,10 +41,10 @@ * Deriving from LLInstanceTracker lets us enumerate instances. */ class LL_COMMON_API LLEventAPI: public LLDispatchListener, - public INSTANCE_TRACKER_KEYED(LLEventAPI, std::string) + public LLInstanceTracker<LLEventAPI, std::string> { typedef LLDispatchListener lbase; - typedef INSTANCE_TRACKER_KEYED(LLEventAPI, std::string) ibase; + typedef LLInstanceTracker<LLEventAPI, std::string> ibase; public: diff --git a/indra/llcommon/lleventtimer.h b/indra/llcommon/lleventtimer.h index e55f851758..dc918121e1 100644 --- a/indra/llcommon/lleventtimer.h +++ b/indra/llcommon/lleventtimer.h @@ -33,7 +33,7 @@ #include "lltimer.h" // class for scheduling a function to be called at a given frequency (approximate, inprecise) -class LL_COMMON_API LLEventTimer : public INSTANCE_TRACKER(LLEventTimer) +class LL_COMMON_API LLEventTimer : public LLInstanceTracker<LLEventTimer> { public: diff --git a/indra/llcommon/llfasttimer.cpp b/indra/llcommon/llfasttimer.cpp index 9b15804e97..51a9441cd5 100644 --- a/indra/llcommon/llfasttimer.cpp +++ b/indra/llcommon/llfasttimer.cpp @@ -107,17 +107,13 @@ class NamedTimerFactory : public LLSingleton<NamedTimerFactory> { public: NamedTimerFactory() - : mTimerRoot(NULL) - {} - - /*virtual */ void initSingleton() + : mTimerRoot(new LLFastTimer::NamedTimer("root")) { - mTimerRoot = new LLFastTimer::NamedTimer("root"); mRootFrameState.setNamedTimer(mTimerRoot); mTimerRoot->setFrameState(&mRootFrameState); mTimerRoot->mParent = mTimerRoot; mTimerRoot->setCollapsed(false); - mRootFrameState.mParent = &mRootFrameState; + mRootFrameState.mParent = &mRootFrameState; } ~NamedTimerFactory() diff --git a/indra/llcommon/llfasttimer.h b/indra/llcommon/llfasttimer.h index a6b34bdc69..a99a1d88af 100644 --- a/indra/llcommon/llfasttimer.h +++ b/indra/llcommon/llfasttimer.h @@ -63,7 +63,7 @@ public: // stores a "named" timer instance to be reused via multiple LLFastTimer stack instances class LL_COMMON_API NamedTimer - : public LLInstanceTracker<NamedTimer, InstanceTrackType_NamedTimer > + : public LLInstanceTracker<NamedTimer> { friend class DeclareTimer; public: @@ -139,7 +139,7 @@ public: // used to statically declare a new named timer class LL_COMMON_API DeclareTimer - : public LLInstanceTracker< DeclareTimer, InstanceTrackType_DeclareTimer > + : public LLInstanceTracker< DeclareTimer > { friend class LLFastTimer; public: diff --git a/indra/llcommon/llinstancetracker.cpp b/indra/llcommon/llinstancetracker.cpp index 89430f82d7..247d0dc4fb 100644 --- a/indra/llcommon/llinstancetracker.cpp +++ b/indra/llcommon/llinstancetracker.cpp @@ -32,26 +32,4 @@ // external library headers // other Linden headers -static bool sInstanceTrackerData_initialized = false; -static void* sInstanceTrackerData[ kInstanceTrackTypeCount ]; - - -void * & LLInstanceTrackerBase::getInstances(InstanceTrackType t) -{ - // std::map::insert() is just what we want here. You attempt to insert a - // (key, value) pair. If the specified key doesn't yet exist, it inserts - // the pair and returns a std::pair of (iterator, true). If the specified - // key DOES exist, insert() simply returns (iterator, false). One lookup - // handles both cases. - if (!sInstanceTrackerData_initialized) - { - for (S32 i = 0; i < (S32) kInstanceTrackTypeCount; i++) - { - sInstanceTrackerData[i] = NULL; - } - sInstanceTrackerData_initialized = true; - } - - return sInstanceTrackerData[t]; -} diff --git a/indra/llcommon/llinstancetracker.h b/indra/llcommon/llinstancetracker.h index b290526754..361182380a 100644 --- a/indra/llcommon/llinstancetracker.h +++ b/indra/llcommon/llinstancetracker.h @@ -38,33 +38,6 @@ #include <boost/iterator/transform_iterator.hpp> #include <boost/iterator/indirect_iterator.hpp> -enum InstanceTrackType -{ - InstanceTrackType_LLEventAPI, - InstanceTrackType_LLEventTimer, - InstanceTrackType_NamedTimer, - InstanceTrackType_DeclareTimer, - InstanceTrackType_LLLeap, - InstanceTrackType_LLGLNamePool, - InstanceTrackType_LLConsole, - InstanceTrackType_LLFloater, - InstanceTrackType_LLFloaterWebContent, - InstanceTrackType_LLLayoutStack, - InstanceTrackType_LLNotificationContext, - InstanceTrackType_LLWindow, - InstanceTrackType_LLControlGroup, - InstanceTrackType_LLControlCache, - InstanceTrackType_LLMediaCtrl, - InstanceTrackType_LLNameListCtrl, - InstanceTrackType_LLToast, - InstanceTrackType_Keyed, // for integ tests - InstanceTrackType_Unkeyed, // for integ tests - kInstanceTrackTypeCount -}; - -#define INSTANCE_TRACKER(T) LLInstanceTracker< T, InstanceTrackType_##T > -#define INSTANCE_TRACKER_KEYED(T,K) LLInstanceTracker< T, InstanceTrackType_##T, K > - /** * Base class manages "class-static" data that must actually have singleton * semantics: one instance per process, rather than one instance per module as @@ -73,22 +46,7 @@ enum InstanceTrackType class LL_COMMON_API LLInstanceTrackerBase { protected: - /// Get a process-unique void* pointer slot for the specified type_info - //static void * & getInstances(std::type_info const & info); - static void * & getInstances(InstanceTrackType t); - - /// Find or create a STATICDATA instance for the specified TRACKED class. - /// STATICDATA must be default-constructible. - template<typename STATICDATA, class TRACKED, class INST, InstanceTrackType TRACKEDTYPE> - static STATICDATA& getStatic() - { - void *& instances = getInstances(TRACKEDTYPE); - if (! instances) - { - instances = new STATICDATA; - } - return *static_cast<STATICDATA*>(instances); - } + /// It's not essential to derive your STATICDATA (for use with /// getStatic()) from StaticBase; it's just that both known @@ -108,16 +66,16 @@ LL_COMMON_API void assert_main_thread(); /// The (optional) key associates a value of type KEY with a given instance of T, for quick lookup /// If KEY is not provided, then instances are stored in a simple set /// @NOTE: see explicit specialization below for default KEY==T* case -template<typename T, enum InstanceTrackType TRACKED, typename KEY = T*> +template<typename T, typename KEY = T*> class LLInstanceTracker : public LLInstanceTrackerBase { - typedef LLInstanceTracker<T, TRACKED, KEY> MyT; + typedef LLInstanceTracker<T, KEY> self_t; typedef typename std::map<KEY, T*> InstanceMap; struct StaticData: public StaticBase { InstanceMap sMap; }; - static StaticData& getStatic() { return LLInstanceTrackerBase::getStatic<StaticData, MyT, T, TRACKED>(); } + static StaticData& getStatic() { static StaticData sData; return sData;} static InstanceMap& getMap_() { // assert_main_thread(); fwiw this class is not thread safe, and it used by multiple threads. Bad things happen. @@ -263,16 +221,16 @@ private: /// explicit specialization for default case where KEY is T* /// use a simple std::set<T*> -template<typename T, enum InstanceTrackType TRACKED> -class LLInstanceTracker<T, TRACKED, T*> : public LLInstanceTrackerBase +template<typename T> +class LLInstanceTracker<T, T*> : public LLInstanceTrackerBase { - typedef LLInstanceTracker<T, TRACKED, T*> MyT; + typedef LLInstanceTracker<T, T*> self_t; typedef typename std::set<T*> InstanceSet; struct StaticData: public StaticBase { InstanceSet sSet; }; - static StaticData& getStatic() { return LLInstanceTrackerBase::getStatic<StaticData, MyT, T, TRACKED>(); } + static StaticData& getStatic() { static StaticData sData; return sData; } static InstanceSet& getSet_() { return getStatic().sSet; } public: diff --git a/indra/llcommon/llleap.h b/indra/llcommon/llleap.h index d4e138f4be..e33f25e530 100644 --- a/indra/llcommon/llleap.h +++ b/indra/llcommon/llleap.h @@ -29,7 +29,7 @@ * LLLeap* pointer should be validated before use by * LLLeap::getInstance(LLLeap*) (see LLInstanceTracker). */ -class LL_COMMON_API LLLeap: public INSTANCE_TRACKER(LLLeap) +class LL_COMMON_API LLLeap: public LLInstanceTracker<LLLeap> { public: 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 49d99f2cd0..84afeb563e 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. @@ -93,7 +61,6 @@ class LLSingleton : private boost::noncopyable private: typedef enum e_init_state { - UNINITIALIZED, CONSTRUCTING, INITIALIZING, INITIALIZED, @@ -109,8 +76,11 @@ private: SingletonInstanceData() : mSingletonInstance(NULL), - mInitState(UNINITIALIZED) - {} + mInitState(CONSTRUCTING) + { + mSingletonInstance = new DERIVED_TYPE(); + mInitState = INITIALIZING; + } ~SingletonInstanceData() { @@ -159,16 +129,8 @@ public: static SingletonInstanceData& getData() { // 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; } static DERIVED_TYPE* getInstance() @@ -185,13 +147,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; + // go ahead and flag ourselves as initialized so we can be reentrant during initialization + data.mInitState = INITIALIZED; data.mSingletonInstance->initSingleton(); - data.mInitState = INITIALIZED; } return data.mSingletonInstance; diff --git a/indra/llcommon/tests/llinstancetracker_test.cpp b/indra/llcommon/tests/llinstancetracker_test.cpp index b2d82d1843..e769c3e22c 100644 --- a/indra/llcommon/tests/llinstancetracker_test.cpp +++ b/indra/llcommon/tests/llinstancetracker_test.cpp @@ -48,16 +48,16 @@ struct Badness: public std::runtime_error Badness(const std::string& what): std::runtime_error(what) {} }; -struct Keyed: public INSTANCE_TRACKER_KEYED(Keyed, std::string) +struct Keyed: public LLInstanceTracker<Keyed, std::string> { Keyed(const std::string& name): - INSTANCE_TRACKER_KEYED(Keyed, std::string)(name), + LLInstanceTracker<Keyed, std::string>(name), mName(name) {} std::string mName; }; -struct Unkeyed: public INSTANCE_TRACKER(Unkeyed) +struct Unkeyed: public LLInstanceTracker<Unkeyed> { Unkeyed(const std::string& thrw="") { |