From 77a21f95e52487129e90d981d845242c7ef03f05 Mon Sep 17 00:00:00 2001 From: Aleric Inglewood Date: Wed, 1 Feb 2012 03:27:04 +0100 Subject: [PATCH] Singleton<> improvements. Allow Singleton<>::instance to be inlined and add a new warning for accesses to the singleton while its being initialized. --- indra/llcommon/llsingleton.h | 46 +++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/indra/llcommon/llsingleton.h b/indra/llcommon/llsingleton.h index 07ac7d8ce..6fac2c8a9 100644 --- a/indra/llcommon/llsingleton.h +++ b/indra/llcommon/llsingleton.h @@ -176,25 +176,11 @@ public: { SingletonInstanceData& data = getData(); - if (data.mInitState == CONSTRUCTING) + if (data.mInitState != INITIALIZED) { - llerrs << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from singleton constructor!" << llendl; + createInstance(data); } - if (data.mInitState == DELETED) - { - llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl; - } - - if (!data.mSingletonInstance) - { - data.mInitState = CONSTRUCTING; - data.mSingletonInstance = new DERIVED_TYPE(); - data.mInitState = INITIALIZING; - data.mSingletonInstance->initSingleton(); - data.mInitState = INITIALIZED; - } - return data.mSingletonInstance; } @@ -220,7 +206,35 @@ public: } private: + static void createInstance(SingletonInstanceData& data); virtual void initSingleton() {} }; +// Moved this here cause it's too big to be inlined --Aleric. +template +void LLSingleton::createInstance(SingletonInstanceData& data) +{ + if (data.mInitState == CONSTRUCTING) + { + llerrs << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from singleton constructor!" << llendl; + } + + if (data.mInitState == DELETED) + { + llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl; + } + + if (data.mInitState == INITIALIZING) + { + llwarns << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from initSingleton(), using half-initialized object" << llendl; + return; + } + + data.mInitState = CONSTRUCTING; + data.mSingletonInstance = new DERIVED_TYPE(); + data.mInitState = INITIALIZING; + data.mSingletonInstance->initSingleton(); + data.mInitState = INITIALIZED; +} + #endif