summaryrefslogtreecommitdiffstats
path: root/base/memory/singleton.cc
diff options
context:
space:
mode:
authorjoth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-07 11:11:51 +0000
committerjoth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-07 11:11:51 +0000
commitee5272d8cc1cd65bb185fdbdd8a00836fa3c9a4d (patch)
tree22a88ed7e88674949a53b0637a144ac3f5cdac1e /base/memory/singleton.cc
parent628a22ae07ae79d1184ca081b36b226863e536ca (diff)
downloadchromium_src-ee5272d8cc1cd65bb185fdbdd8a00836fa3c9a4d.zip
chromium_src-ee5272d8cc1cd65bb185fdbdd8a00836fa3c9a4d.tar.gz
chromium_src-ee5272d8cc1cd65bb185fdbdd8a00836fa3c9a4d.tar.bz2
Thin out the Singeton template code
- hides platform_thread.h from users of singelton - knock 4KB off the release binary size BUG=None TEST=Nothing breaks Review URL: http://codereview.chromium.org/8475007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108853 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/memory/singleton.cc')
-rw-r--r--base/memory/singleton.cc31
1 files changed, 31 insertions, 0 deletions
diff --git a/base/memory/singleton.cc b/base/memory/singleton.cc
new file mode 100644
index 0000000..ee5e58d
--- /dev/null
+++ b/base/memory/singleton.cc
@@ -0,0 +1,31 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/singleton.h"
+#include "base/threading/platform_thread.h"
+
+namespace base {
+namespace internal {
+
+subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) {
+ // Handle the race. Another thread beat us and either:
+ // - Has the object in BeingCreated state
+ // - Already has the object created...
+ // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr.
+ // Unless your constructor can be very time consuming, it is very unlikely
+ // to hit this race. When it does, we just spin and yield the thread until
+ // the object has been created.
+ subtle::AtomicWord value;
+ while (true) {
+ value = subtle::NoBarrier_Load(instance);
+ if (value != kBeingCreatedMarker)
+ break;
+ PlatformThread::YieldCurrentThread();
+ }
+ return value;
+}
+
+} // namespace internal
+} // namespace base
+