Make members of Singleton<T> private and only visible to the singleton type. This enforces that the Singleton<T> pattern can only be used within classes which want singleton-ness.
As part of this CL I have also fixed up files which got missed in my previous CLs to use a GetInstance() method and use Singleton<T> from the source file.
There are a small number of places where I have also switched to LazyInstance as that was more appropriate for types used in a single source file.
BUG=65298
TEST=all existing tests should continue to pass.
Review URL: http://codereview.chromium.org/5682008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69107 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/lazy_instance.h b/base/lazy_instance.h
index f8d5987..874e87e 100644
--- a/base/lazy_instance.h
+++ b/base/lazy_instance.h
@@ -127,7 +127,8 @@
NeedsInstance()) {
// Create the instance in the space provided by |buf_|.
instance_ = Traits::New(buf_);
- CompleteInstance(instance_, Traits::Delete);
+ // Traits::Delete will be null for LeakyLazyInstannceTraits
+ CompleteInstance(this, (Traits::Delete == NULL) ? NULL : OnExit);
}
// This annotation helps race detectors recognize correct lock-less
@@ -140,6 +141,17 @@
}
private:
+ // Adapter function for use with AtExit. This should be called single
+ // threaded, so don't use atomic operations.
+ // Calling OnExit while the instance is in use by other threads is a mistake.
+ static void OnExit(void* lazy_instance) {
+ LazyInstance<Type, Traits>* me =
+ reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
+ Traits::Delete(me->instance_);
+ me->instance_ = NULL;
+ base::subtle::Release_Store(&me->state_, STATE_EMPTY);
+ }
+
int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance.
Type *instance_;