1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker // 5*635a8641SAndroid Build Coastguard Worker // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPRECATED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 6*635a8641SAndroid Build Coastguard Worker // Please don't introduce new instances of LazyInstance<T>. Use a function-local 7*635a8641SAndroid Build Coastguard Worker // static of type base::NoDestructor<T> instead: 8*635a8641SAndroid Build Coastguard Worker // 9*635a8641SAndroid Build Coastguard Worker // Factory& Factory::GetInstance() { 10*635a8641SAndroid Build Coastguard Worker // static base::NoDestructor<Factory> instance; 11*635a8641SAndroid Build Coastguard Worker // return *instance; 12*635a8641SAndroid Build Coastguard Worker // } 13*635a8641SAndroid Build Coastguard Worker // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 14*635a8641SAndroid Build Coastguard Worker // 15*635a8641SAndroid Build Coastguard Worker // The LazyInstance<Type, Traits> class manages a single instance of Type, 16*635a8641SAndroid Build Coastguard Worker // which will be lazily created on the first time it's accessed. This class is 17*635a8641SAndroid Build Coastguard Worker // useful for places you would normally use a function-level static, but you 18*635a8641SAndroid Build Coastguard Worker // need to have guaranteed thread-safety. The Type constructor will only ever 19*635a8641SAndroid Build Coastguard Worker // be called once, even if two threads are racing to create the object. Get() 20*635a8641SAndroid Build Coastguard Worker // and Pointer() will always return the same, completely initialized instance. 21*635a8641SAndroid Build Coastguard Worker // When the instance is constructed it is registered with AtExitManager. The 22*635a8641SAndroid Build Coastguard Worker // destructor will be called on program exit. 23*635a8641SAndroid Build Coastguard Worker // 24*635a8641SAndroid Build Coastguard Worker // LazyInstance is completely thread safe, assuming that you create it safely. 25*635a8641SAndroid Build Coastguard Worker // The class was designed to be POD initialized, so it shouldn't require a 26*635a8641SAndroid Build Coastguard Worker // static constructor. It really only makes sense to declare a LazyInstance as 27*635a8641SAndroid Build Coastguard Worker // a global variable using the LAZY_INSTANCE_INITIALIZER initializer. 28*635a8641SAndroid Build Coastguard Worker // 29*635a8641SAndroid Build Coastguard Worker // LazyInstance is similar to Singleton, except it does not have the singleton 30*635a8641SAndroid Build Coastguard Worker // property. You can have multiple LazyInstance's of the same type, and each 31*635a8641SAndroid Build Coastguard Worker // will manage a unique instance. It also preallocates the space for Type, as 32*635a8641SAndroid Build Coastguard Worker // to avoid allocating the Type instance on the heap. This may help with the 33*635a8641SAndroid Build Coastguard Worker // performance of creating the instance, and reducing heap fragmentation. This 34*635a8641SAndroid Build Coastguard Worker // requires that Type be a complete type so we can determine the size. 35*635a8641SAndroid Build Coastguard Worker // 36*635a8641SAndroid Build Coastguard Worker // Example usage: 37*635a8641SAndroid Build Coastguard Worker // static LazyInstance<MyClass>::Leaky inst = LAZY_INSTANCE_INITIALIZER; 38*635a8641SAndroid Build Coastguard Worker // void SomeMethod() { 39*635a8641SAndroid Build Coastguard Worker // inst.Get().SomeMethod(); // MyClass::SomeMethod() 40*635a8641SAndroid Build Coastguard Worker // 41*635a8641SAndroid Build Coastguard Worker // MyClass* ptr = inst.Pointer(); 42*635a8641SAndroid Build Coastguard Worker // ptr->DoDoDo(); // MyClass::DoDoDo 43*635a8641SAndroid Build Coastguard Worker // } 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard Worker #ifndef BASE_LAZY_INSTANCE_H_ 46*635a8641SAndroid Build Coastguard Worker #define BASE_LAZY_INSTANCE_H_ 47*635a8641SAndroid Build Coastguard Worker 48*635a8641SAndroid Build Coastguard Worker #include <new> // For placement new. 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker #include "base/atomicops.h" 51*635a8641SAndroid Build Coastguard Worker #include "base/debug/leak_annotations.h" 52*635a8641SAndroid Build Coastguard Worker #include "base/lazy_instance_helpers.h" 53*635a8641SAndroid Build Coastguard Worker #include "base/logging.h" 54*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h" 55*635a8641SAndroid Build Coastguard Worker 56*635a8641SAndroid Build Coastguard Worker // LazyInstance uses its own struct initializer-list style static 57*635a8641SAndroid Build Coastguard Worker // initialization, which does not require a constructor. 58*635a8641SAndroid Build Coastguard Worker #define LAZY_INSTANCE_INITIALIZER {} 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker namespace base { 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker template <typename Type> 63*635a8641SAndroid Build Coastguard Worker struct LazyInstanceTraitsBase { NewLazyInstanceTraitsBase64*635a8641SAndroid Build Coastguard Worker static Type* New(void* instance) { 65*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (alignof(Type) - 1), 0u); 66*635a8641SAndroid Build Coastguard Worker // Use placement new to initialize our instance in our preallocated space. 67*635a8641SAndroid Build Coastguard Worker // The parenthesis is very important here to force POD type initialization. 68*635a8641SAndroid Build Coastguard Worker return new (instance) Type(); 69*635a8641SAndroid Build Coastguard Worker } 70*635a8641SAndroid Build Coastguard Worker CallDestructorLazyInstanceTraitsBase71*635a8641SAndroid Build Coastguard Worker static void CallDestructor(Type* instance) { 72*635a8641SAndroid Build Coastguard Worker // Explicitly call the destructor. 73*635a8641SAndroid Build Coastguard Worker instance->~Type(); 74*635a8641SAndroid Build Coastguard Worker } 75*635a8641SAndroid Build Coastguard Worker }; 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker // We pull out some of the functionality into non-templated functions, so we 78*635a8641SAndroid Build Coastguard Worker // can implement the more complicated pieces out of line in the .cc file. 79*635a8641SAndroid Build Coastguard Worker namespace internal { 80*635a8641SAndroid Build Coastguard Worker 81*635a8641SAndroid Build Coastguard Worker // This traits class causes destruction the contained Type at process exit via 82*635a8641SAndroid Build Coastguard Worker // AtExitManager. This is probably generally not what you want. Instead, prefer 83*635a8641SAndroid Build Coastguard Worker // Leaky below. 84*635a8641SAndroid Build Coastguard Worker template <typename Type> 85*635a8641SAndroid Build Coastguard Worker struct DestructorAtExitLazyInstanceTraits { 86*635a8641SAndroid Build Coastguard Worker static const bool kRegisterOnExit = true; 87*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON() 88*635a8641SAndroid Build Coastguard Worker static const bool kAllowedToAccessOnNonjoinableThread = false; 89*635a8641SAndroid Build Coastguard Worker #endif 90*635a8641SAndroid Build Coastguard Worker NewDestructorAtExitLazyInstanceTraits91*635a8641SAndroid Build Coastguard Worker static Type* New(void* instance) { 92*635a8641SAndroid Build Coastguard Worker return LazyInstanceTraitsBase<Type>::New(instance); 93*635a8641SAndroid Build Coastguard Worker } 94*635a8641SAndroid Build Coastguard Worker DeleteDestructorAtExitLazyInstanceTraits95*635a8641SAndroid Build Coastguard Worker static void Delete(Type* instance) { 96*635a8641SAndroid Build Coastguard Worker LazyInstanceTraitsBase<Type>::CallDestructor(instance); 97*635a8641SAndroid Build Coastguard Worker } 98*635a8641SAndroid Build Coastguard Worker }; 99*635a8641SAndroid Build Coastguard Worker 100*635a8641SAndroid Build Coastguard Worker // Use LazyInstance<T>::Leaky for a less-verbose call-site typedef; e.g.: 101*635a8641SAndroid Build Coastguard Worker // base::LazyInstance<T>::Leaky my_leaky_lazy_instance; 102*635a8641SAndroid Build Coastguard Worker // instead of: 103*635a8641SAndroid Build Coastguard Worker // base::LazyInstance<T, base::internal::LeakyLazyInstanceTraits<T> > 104*635a8641SAndroid Build Coastguard Worker // my_leaky_lazy_instance; 105*635a8641SAndroid Build Coastguard Worker // (especially when T is MyLongTypeNameImplClientHolderFactory). 106*635a8641SAndroid Build Coastguard Worker // Only use this internal::-qualified verbose form to extend this traits class 107*635a8641SAndroid Build Coastguard Worker // (depending on its implementation details). 108*635a8641SAndroid Build Coastguard Worker template <typename Type> 109*635a8641SAndroid Build Coastguard Worker struct LeakyLazyInstanceTraits { 110*635a8641SAndroid Build Coastguard Worker static const bool kRegisterOnExit = false; 111*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON() 112*635a8641SAndroid Build Coastguard Worker static const bool kAllowedToAccessOnNonjoinableThread = true; 113*635a8641SAndroid Build Coastguard Worker #endif 114*635a8641SAndroid Build Coastguard Worker NewLeakyLazyInstanceTraits115*635a8641SAndroid Build Coastguard Worker static Type* New(void* instance) { 116*635a8641SAndroid Build Coastguard Worker ANNOTATE_SCOPED_MEMORY_LEAK; 117*635a8641SAndroid Build Coastguard Worker return LazyInstanceTraitsBase<Type>::New(instance); 118*635a8641SAndroid Build Coastguard Worker } DeleteLeakyLazyInstanceTraits119*635a8641SAndroid Build Coastguard Worker static void Delete(Type* instance) { 120*635a8641SAndroid Build Coastguard Worker } 121*635a8641SAndroid Build Coastguard Worker }; 122*635a8641SAndroid Build Coastguard Worker 123*635a8641SAndroid Build Coastguard Worker template <typename Type> 124*635a8641SAndroid Build Coastguard Worker struct ErrorMustSelectLazyOrDestructorAtExitForLazyInstance {}; 125*635a8641SAndroid Build Coastguard Worker 126*635a8641SAndroid Build Coastguard Worker } // namespace internal 127*635a8641SAndroid Build Coastguard Worker 128*635a8641SAndroid Build Coastguard Worker template < 129*635a8641SAndroid Build Coastguard Worker typename Type, 130*635a8641SAndroid Build Coastguard Worker typename Traits = 131*635a8641SAndroid Build Coastguard Worker internal::ErrorMustSelectLazyOrDestructorAtExitForLazyInstance<Type>> 132*635a8641SAndroid Build Coastguard Worker class LazyInstance { 133*635a8641SAndroid Build Coastguard Worker public: 134*635a8641SAndroid Build Coastguard Worker // Do not define a destructor, as doing so makes LazyInstance a 135*635a8641SAndroid Build Coastguard Worker // non-POD-struct. We don't want that because then a static initializer will 136*635a8641SAndroid Build Coastguard Worker // be created to register the (empty) destructor with atexit() under MSVC, for 137*635a8641SAndroid Build Coastguard Worker // example. We handle destruction of the contained Type class explicitly via 138*635a8641SAndroid Build Coastguard Worker // the OnExit member function, where needed. 139*635a8641SAndroid Build Coastguard Worker // ~LazyInstance() {} 140*635a8641SAndroid Build Coastguard Worker 141*635a8641SAndroid Build Coastguard Worker // Convenience typedef to avoid having to repeat Type for leaky lazy 142*635a8641SAndroid Build Coastguard Worker // instances. 143*635a8641SAndroid Build Coastguard Worker typedef LazyInstance<Type, internal::LeakyLazyInstanceTraits<Type>> Leaky; 144*635a8641SAndroid Build Coastguard Worker typedef LazyInstance<Type, internal::DestructorAtExitLazyInstanceTraits<Type>> 145*635a8641SAndroid Build Coastguard Worker DestructorAtExit; 146*635a8641SAndroid Build Coastguard Worker Get()147*635a8641SAndroid Build Coastguard Worker Type& Get() { 148*635a8641SAndroid Build Coastguard Worker return *Pointer(); 149*635a8641SAndroid Build Coastguard Worker } 150*635a8641SAndroid Build Coastguard Worker Pointer()151*635a8641SAndroid Build Coastguard Worker Type* Pointer() { 152*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON() 153*635a8641SAndroid Build Coastguard Worker if (!Traits::kAllowedToAccessOnNonjoinableThread) 154*635a8641SAndroid Build Coastguard Worker ThreadRestrictions::AssertSingletonAllowed(); 155*635a8641SAndroid Build Coastguard Worker #endif 156*635a8641SAndroid Build Coastguard Worker 157*635a8641SAndroid Build Coastguard Worker return subtle::GetOrCreateLazyPointer( 158*635a8641SAndroid Build Coastguard Worker &private_instance_, &Traits::New, private_buf_, 159*635a8641SAndroid Build Coastguard Worker Traits::kRegisterOnExit ? OnExit : nullptr, this); 160*635a8641SAndroid Build Coastguard Worker } 161*635a8641SAndroid Build Coastguard Worker 162*635a8641SAndroid Build Coastguard Worker // Returns true if the lazy instance has been created. Unlike Get() and 163*635a8641SAndroid Build Coastguard Worker // Pointer(), calling IsCreated() will not instantiate the object of Type. IsCreated()164*635a8641SAndroid Build Coastguard Worker bool IsCreated() { 165*635a8641SAndroid Build Coastguard Worker // Return true (i.e. "created") if |private_instance_| is either being 166*635a8641SAndroid Build Coastguard Worker // created right now (i.e. |private_instance_| has value of 167*635a8641SAndroid Build Coastguard Worker // internal::kLazyInstanceStateCreating) or was already created (i.e. 168*635a8641SAndroid Build Coastguard Worker // |private_instance_| has any other non-zero value). 169*635a8641SAndroid Build Coastguard Worker return 0 != subtle::NoBarrier_Load(&private_instance_); 170*635a8641SAndroid Build Coastguard Worker } 171*635a8641SAndroid Build Coastguard Worker 172*635a8641SAndroid Build Coastguard Worker // MSVC gives a warning that the alignment expands the size of the 173*635a8641SAndroid Build Coastguard Worker // LazyInstance struct to make the size a multiple of the alignment. This 174*635a8641SAndroid Build Coastguard Worker // is expected in this case. 175*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 176*635a8641SAndroid Build Coastguard Worker #pragma warning(push) 177*635a8641SAndroid Build Coastguard Worker #pragma warning(disable: 4324) 178*635a8641SAndroid Build Coastguard Worker #endif 179*635a8641SAndroid Build Coastguard Worker 180*635a8641SAndroid Build Coastguard Worker // Effectively private: member data is only public to allow the linker to 181*635a8641SAndroid Build Coastguard Worker // statically initialize it and to maintain a POD class. DO NOT USE FROM 182*635a8641SAndroid Build Coastguard Worker // OUTSIDE THIS CLASS. 183*635a8641SAndroid Build Coastguard Worker subtle::AtomicWord private_instance_; 184*635a8641SAndroid Build Coastguard Worker 185*635a8641SAndroid Build Coastguard Worker // Preallocated space for the Type instance. 186*635a8641SAndroid Build Coastguard Worker alignas(Type) char private_buf_[sizeof(Type)]; 187*635a8641SAndroid Build Coastguard Worker 188*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 189*635a8641SAndroid Build Coastguard Worker #pragma warning(pop) 190*635a8641SAndroid Build Coastguard Worker #endif 191*635a8641SAndroid Build Coastguard Worker 192*635a8641SAndroid Build Coastguard Worker private: instance()193*635a8641SAndroid Build Coastguard Worker Type* instance() { 194*635a8641SAndroid Build Coastguard Worker return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); 195*635a8641SAndroid Build Coastguard Worker } 196*635a8641SAndroid Build Coastguard Worker 197*635a8641SAndroid Build Coastguard Worker // Adapter function for use with AtExit. This should be called single 198*635a8641SAndroid Build Coastguard Worker // threaded, so don't synchronize across threads. 199*635a8641SAndroid Build Coastguard Worker // Calling OnExit while the instance is in use by other threads is a mistake. OnExit(void * lazy_instance)200*635a8641SAndroid Build Coastguard Worker static void OnExit(void* lazy_instance) { 201*635a8641SAndroid Build Coastguard Worker LazyInstance<Type, Traits>* me = 202*635a8641SAndroid Build Coastguard Worker reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 203*635a8641SAndroid Build Coastguard Worker Traits::Delete(me->instance()); 204*635a8641SAndroid Build Coastguard Worker subtle::NoBarrier_Store(&me->private_instance_, 0); 205*635a8641SAndroid Build Coastguard Worker } 206*635a8641SAndroid Build Coastguard Worker }; 207*635a8641SAndroid Build Coastguard Worker 208*635a8641SAndroid Build Coastguard Worker } // namespace base 209*635a8641SAndroid Build Coastguard Worker 210*635a8641SAndroid Build Coastguard Worker #endif // BASE_LAZY_INSTANCE_H_ 211