summaryrefslogtreecommitdiff
path: root/indra/llcommon/llsingleton.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llcommon/llsingleton.cpp')
-rw-r--r--indra/llcommon/llsingleton.cpp80
1 files changed, 59 insertions, 21 deletions
diff --git a/indra/llcommon/llsingleton.cpp b/indra/llcommon/llsingleton.cpp
index 5f1a89670e..2a38d6d896 100644
--- a/indra/llcommon/llsingleton.cpp
+++ b/indra/llcommon/llsingleton.cpp
@@ -1,25 +1,25 @@
-/**
+/**
* @file llsingleton.cpp
* @author Brad Kittenbrink
*
* $LicenseInfo:firstyear=2009&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
- *
+ *
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
+ *
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
@@ -27,11 +27,12 @@
#include "linden_common.h"
#include "llsingleton.h"
+#include "llcoros.h"
+#include "lldependencies.h"
#include "llerror.h"
#include "llerrorcontrol.h"
-#include "lldependencies.h"
#include "llexception.h"
-#include "llcoros.h"
+#include "llmainthreadtask.h"
#include <algorithm>
#include <iostream> // std::cerr in dire emergency
#include <sstream>
@@ -59,9 +60,8 @@ private:
// it's safe to log -- which involves querying a different LLSingleton --
// which requires accessing the master list.
typedef std::recursive_mutex mutex_t;
- typedef std::unique_lock<mutex_t> lock_t;
-
- mutex_t mMutex;
+ LL_PROFILE_MUTEX_NAMED(mutex_t, mMutex, "Singleton MasterList");
+ typedef std::unique_lock<decltype(mMutex)> lock_t;
public:
// Instantiate this to both obtain a reference to MasterList::instance()
@@ -271,17 +271,29 @@ void LLSingletonBase::reset_initializing(list_t::size_type size)
void LLSingletonBase::MasterList::LockedInitializing::log(const char* verb, const char* name)
{
- LL_DEBUGS("LLSingleton") << verb << ' ' << demangle(name) << ';';
- if (mList)
+ LL_DEBUGS("LLSingleton") << verb << ' ' << demangle(name) << ';';
+ if (mList)
+ {
+ for (list_t::const_reverse_iterator ri(mList->rbegin()), rend(mList->rend());
+ ri != rend; ++ri)
{
- for (list_t::const_reverse_iterator ri(mList->rbegin()), rend(mList->rend());
- ri != rend; ++ri)
- {
- LLSingletonBase* sb(*ri);
- LL_CONT << ' ' << classname(sb);
- }
+ LLSingletonBase* sb(*ri);
+ LL_CONT << ' ' << classname(sb);
}
- LL_ENDL;
+ }
+ LL_ENDL;
+}
+
+void LLSingletonBase::capture_dependency(LLSingletonBase* sb)
+{
+ // If we're called very late during application shutdown, the Boost.Fibers
+ // library may have shut down, and MasterList::mInitializing.get() might
+ // blow up. But if we're called that late, there's really no point in
+ // trying to capture this dependency.
+ if (boost::fibers::context::active())
+ {
+ sb->capture_dependency();
+ }
}
void LLSingletonBase::capture_dependency()
@@ -363,7 +375,7 @@ LLSingletonBase::vec_t LLSingletonBase::dep_sort()
// should happen basically once: for deleteAll().
typedef LLDependencies<LLSingletonBase*> SingletonDeps;
SingletonDeps sdeps;
- // Lock while traversing the master list
+ // Lock while traversing the master list
MasterList::LockedMaster master;
for (LLSingletonBase* sp : master.get())
{
@@ -455,7 +467,7 @@ std::ostream& operator<<(std::ostream& out, const LLSingletonBase::string_params
return out;
}
-} // anonymous namespace
+} // anonymous namespace
//static
void LLSingletonBase::logwarns(const string_params& args)
@@ -485,3 +497,29 @@ std::string LLSingletonBase::demangle(const char* mangled)
{
return LLError::Log::demangle(mangled);
}
+
+LLSingletonBase* LLSingletonBase::getInstanceForSecondaryThread(
+ const std::string& name,
+ const std::string& method,
+ const std::function<LLSingletonBase*()>& getInstance)
+{
+ // Normally it would be the height of folly to reference-bind args into a
+ // lambda to be executed on some other thread! By the time that thread
+ // executed the lambda, the references would all be dangling, and Bad
+ // Things would result. But LLMainThreadTask::dispatch() promises to block
+ // the calling thread until the passed task has completed. So in this case
+ // we know the references will remain valid until the lambda has run, so
+ // we dare to bind references.
+ return LLMainThreadTask::dispatch(
+ [&name, &method, &getInstance](){
+ // VERY IMPORTANT to call getInstance() on the main thread,
+ // rather than going straight to constructSingleton()!
+ // During the time window before mInitState is INITIALIZED,
+ // multiple requests might be queued. It's essential that, as
+ // the main thread processes them, only the FIRST such request
+ // actually constructs the instance -- every subsequent one
+ // simply returns the existing instance.
+ loginfos({name, "::", method, " on main thread"});
+ return getInstance();
+ });
+}