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