summaryrefslogtreecommitdiff
path: root/indra/llcommon/llsingleton.h
diff options
context:
space:
mode:
authorbrad kittenbrink <brad@lindenlab.com>2009-08-06 15:53:09 -0700
committerbrad kittenbrink <brad@lindenlab.com>2009-08-06 15:53:09 -0700
commitc9ee582c12bbdcfe7f37459f947872e3ef462560 (patch)
tree9fec03a849daad076d67efcf83c32d84c9532c33 /indra/llcommon/llsingleton.h
parent843036a7d27dbffa4aafeae70b3e46f9afe84c1b (diff)
Add on-demand allocation of LLSingletonRegistry::sSingletonMap so we don't rely on static initialization order.
reviewed by nat.
Diffstat (limited to 'indra/llcommon/llsingleton.h')
-rw-r--r--indra/llcommon/llsingleton.h28
1 files changed, 20 insertions, 8 deletions
diff --git a/indra/llcommon/llsingleton.h b/indra/llcommon/llsingleton.h
index 00c87821c2..f55fafadd8 100644
--- a/indra/llcommon/llsingleton.h
+++ b/indra/llcommon/llsingleton.h
@@ -42,19 +42,30 @@
class LL_COMMON_API LLSingletonRegistry {
private:
typedef std::map<std::string, void *> TypeMap;
- static TypeMap sSingletonMap;
+ 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());
- if(0 == sSingletonMap.count(name))
- {
- sSingletonMap[name] = NULL;
- }
+ 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 sSingletonMap[typeid(T).name()];
+ return result->second;
}
};
@@ -130,12 +141,13 @@ public:
static SingletonInstanceData& getData()
{
- void * & registry = LLSingletonRegistry::get<DERIVED_TYPE>();
- static SingletonInstanceData data;
+ // 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;
}