[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. |
| 4 | |
| 5 | // The LazyInstance<Type, Traits> class manages a single instance of Type, |
| 6 | // which will be lazily created on the first time it's accessed. This class is |
| 7 | // useful for places you would normally use a function-level static, but you |
| 8 | // need to have guaranteed thread-safety. The Type constructor will only ever |
| 9 | // be called once, even if two threads are racing to create the object. Get() |
| 10 | // and Pointer() will always return the same, completely initialized instance. |
| 11 | // When the instance is constructed it is registered with AtExitManager. The |
| 12 | // destructor will be called on program exit. |
| 13 | // |
| 14 | // LazyInstance is completely thread safe, assuming that you create it safely. |
| 15 | // The class was designed to be POD initialized, so it shouldn't require a |
| 16 | // static constructor. It really only makes sense to declare a LazyInstance as |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 17 | // a global variable using the LAZY_INSTANCE_INITIALIZER initializer. |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 18 | // |
| 19 | // LazyInstance is similar to Singleton, except it does not have the singleton |
| 20 | // property. You can have multiple LazyInstance's of the same type, and each |
| 21 | // will manage a unique instance. It also preallocates the space for Type, as |
| 22 | // to avoid allocating the Type instance on the heap. This may help with the |
| 23 | // performance of creating the instance, and reducing heap fragmentation. This |
| 24 | // requires that Type be a complete type so we can determine the size. |
| 25 | // |
| 26 | // Example usage: |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 27 | // static LazyInstance<MyClass> my_instance = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 28 | // void SomeMethod() { |
| 29 | // my_instance.Get().SomeMethod(); // MyClass::SomeMethod() |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 30 | // |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 31 | // MyClass* ptr = my_instance.Pointer(); |
| 32 | // ptr->DoDoDo(); // MyClass::DoDoDo |
| 33 | // } |
| 34 | |
| 35 | #ifndef BASE_LAZY_INSTANCE_H_ |
| 36 | #define BASE_LAZY_INSTANCE_H_ |
| 37 | |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 38 | #include <new> // For placement new. |
| 39 | |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 40 | #include "base/atomicops.h" |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 41 | #include "base/base_export.h" |
[email protected] | b2206da5 | 2013-06-07 22:09:32 | [diff] [blame] | 42 | #include "base/debug/leak_annotations.h" |
[email protected] | e4a638f76 | 2011-10-21 19:46:00 | [diff] [blame] | 43 | #include "base/logging.h" |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 44 | #include "base/memory/aligned_memory.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 45 | #include "base/threading/thread_restrictions.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 46 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 47 | // LazyInstance uses its own struct initializer-list style static |
| 48 | // initialization, as base's LINKER_INITIALIZED requires a constructor and on |
| 49 | // some compilers (notably gcc 4.4) this still ends up needing runtime |
| 50 | // initialization. |
| 51 | #define LAZY_INSTANCE_INITIALIZER {0} |
| 52 | |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 53 | namespace base { |
| 54 | |
| 55 | template <typename Type> |
| 56 | struct DefaultLazyInstanceTraits { |
[email protected] | 113eee0 | 2011-10-25 19:04:48 | [diff] [blame] | 57 | static const bool kRegisterOnExit = true; |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 58 | #if DCHECK_IS_ON() |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 59 | static const bool kAllowedToAccessOnNonjoinableThread = false; |
[email protected] | 5a8a806 | 2014-03-06 01:11:54 | [diff] [blame] | 60 | #endif |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 61 | |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 62 | static Type* New(void* instance) { |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 63 | DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u) |
[email protected] | e4a638f76 | 2011-10-21 19:46:00 | [diff] [blame] | 64 | << ": Bad boy, the buffer passed to placement new is not aligned!\n" |
| 65 | "This may break some stuff like SSE-based optimizations assuming the " |
| 66 | "<Type> objects are word aligned."; |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 67 | // Use placement new to initialize our instance in our preallocated space. |
| 68 | // The parenthesis is very important here to force POD type initialization. |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 69 | return new (instance) Type(); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 70 | } |
[email protected] | 113eee0 | 2011-10-25 19:04:48 | [diff] [blame] | 71 | static void Delete(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 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 81 | // Use LazyInstance<T>::Leaky for a less-verbose call-site typedef; e.g.: |
| 82 | // base::LazyInstance<T>::Leaky my_leaky_lazy_instance; |
| 83 | // instead of: |
[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 84 | // base::LazyInstance<T, base::internal::LeakyLazyInstanceTraits<T> > |
| 85 | // my_leaky_lazy_instance; |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 86 | // (especially when T is MyLongTypeNameImplClientHolderFactory). |
[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 87 | // Only use this internal::-qualified verbose form to extend this traits class |
| 88 | // (depending on its implementation details). |
[email protected] | dcc6933 | 2010-10-21 20:41:47 | [diff] [blame] | 89 | template <typename Type> |
| 90 | struct LeakyLazyInstanceTraits { |
[email protected] | 113eee0 | 2011-10-25 19:04:48 | [diff] [blame] | 91 | static const bool kRegisterOnExit = false; |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 92 | #if DCHECK_IS_ON() |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 93 | static const bool kAllowedToAccessOnNonjoinableThread = true; |
[email protected] | 5a8a806 | 2014-03-06 01:11:54 | [diff] [blame] | 94 | #endif |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 95 | |
[email protected] | dcc6933 | 2010-10-21 20:41:47 | [diff] [blame] | 96 | static Type* New(void* instance) { |
[email protected] | b2206da5 | 2013-06-07 22:09:32 | [diff] [blame] | 97 | ANNOTATE_SCOPED_MEMORY_LEAK; |
[email protected] | dcc6933 | 2010-10-21 20:41:47 | [diff] [blame] | 98 | return DefaultLazyInstanceTraits<Type>::New(instance); |
| 99 | } |
[email protected] | 113eee0 | 2011-10-25 19:04:48 | [diff] [blame] | 100 | static void Delete(Type* instance) { |
| 101 | } |
[email protected] | dcc6933 | 2010-10-21 20:41:47 | [diff] [blame] | 102 | }; |
| 103 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 104 | // Our AtomicWord doubles as a spinlock, where a value of |
| 105 | // kBeingCreatedMarker means the spinlock is being held for creation. |
| 106 | static const subtle::AtomicWord kLazyInstanceStateCreating = 1; |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 107 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 108 | // Check if instance needs to be created. If so return true otherwise |
| 109 | // if another thread has beat us, wait for instance to be created and |
| 110 | // return false. |
| 111 | BASE_EXPORT bool NeedsLazyInstance(subtle::AtomicWord* state); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 112 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 113 | // After creating an instance, call this to register the dtor to be called |
| 114 | // at program exit and to update the atomic state to hold the |new_instance| |
| 115 | BASE_EXPORT void CompleteLazyInstance(subtle::AtomicWord* state, |
| 116 | subtle::AtomicWord new_instance, |
| 117 | void* lazy_instance, |
| 118 | void (*dtor)(void*)); |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 119 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 120 | } // namespace internal |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 121 | |
| 122 | template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 123 | class LazyInstance { |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 124 | public: |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 125 | // Do not define a destructor, as doing so makes LazyInstance a |
| 126 | // non-POD-struct. We don't want that because then a static initializer will |
| 127 | // be created to register the (empty) destructor with atexit() under MSVC, for |
| 128 | // example. We handle destruction of the contained Type class explicitly via |
| 129 | // the OnExit member function, where needed. |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 130 | // ~LazyInstance() {} |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 131 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 132 | // Convenience typedef to avoid having to repeat Type for leaky lazy |
| 133 | // instances. |
[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 134 | typedef LazyInstance<Type, internal::LeakyLazyInstanceTraits<Type> > Leaky; |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 135 | |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 136 | Type& Get() { |
| 137 | return *Pointer(); |
| 138 | } |
| 139 | |
| 140 | Type* Pointer() { |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 141 | #if DCHECK_IS_ON() |
[email protected] | 3c7a7f3 | 2011-11-04 17:29:10 | [diff] [blame] | 142 | // Avoid making TLS lookup on release builds. |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 143 | if (!Traits::kAllowedToAccessOnNonjoinableThread) |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 144 | ThreadRestrictions::AssertSingletonAllowed(); |
[email protected] | 3c7a7f3 | 2011-11-04 17:29:10 | [diff] [blame] | 145 | #endif |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 146 | // If any bit in the created mask is true, the instance has already been |
| 147 | // fully constructed. |
| 148 | static const subtle::AtomicWord kLazyInstanceCreatedMask = |
| 149 | ~internal::kLazyInstanceStateCreating; |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 150 | |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 151 | // We will hopefully have fast access when the instance is already created. |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 152 | // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating |
| 153 | // at most once, the load is taken out of NeedsInstance() as a fast-path. |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 154 | // The load has acquire memory ordering as a thread which sees |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 155 | // private_instance_ > creating needs to acquire visibility over |
| 156 | // the associated data (private_buf_). Pairing Release_Store is in |
| 157 | // CompleteLazyInstance(). |
| 158 | subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); |
| 159 | if (!(value & kLazyInstanceCreatedMask) && |
| 160 | internal::NeedsLazyInstance(&private_instance_)) { |
| 161 | // Create the instance in the space provided by |private_buf_|. |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 162 | value = reinterpret_cast<subtle::AtomicWord>( |
| 163 | Traits::New(private_buf_.void_data())); |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 164 | internal::CompleteLazyInstance(&private_instance_, value, this, |
| 165 | Traits::kRegisterOnExit ? OnExit : NULL); |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 166 | } |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 167 | return instance(); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 332710b | 2011-02-22 19:21:59 | [diff] [blame] | 170 | bool operator==(Type* p) { |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 171 | switch (subtle::NoBarrier_Load(&private_instance_)) { |
| 172 | case 0: |
[email protected] | 332710b | 2011-02-22 19:21:59 | [diff] [blame] | 173 | return p == NULL; |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 174 | case internal::kLazyInstanceStateCreating: |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 175 | return static_cast<void*>(p) == private_buf_.void_data(); |
[email protected] | 332710b | 2011-02-22 19:21:59 | [diff] [blame] | 176 | default: |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 177 | return p == instance(); |
[email protected] | 332710b | 2011-02-22 19:21:59 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 181 | // Effectively private: member data is only public to allow the linker to |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 182 | // statically initialize it and to maintain a POD class. DO NOT USE FROM |
| 183 | // OUTSIDE THIS CLASS. |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 184 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 185 | subtle::AtomicWord private_instance_; |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 186 | // Preallocated space for the Type instance. |
| 187 | base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_; |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 188 | |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 189 | private: |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 190 | Type* instance() { |
| 191 | return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); |
| 192 | } |
| 193 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 194 | // Adapter function for use with AtExit. This should be called single |
[email protected] | 113eee0 | 2011-10-25 19:04:48 | [diff] [blame] | 195 | // threaded, so don't synchronize across threads. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 196 | // Calling OnExit while the instance is in use by other threads is a mistake. |
| 197 | static void OnExit(void* lazy_instance) { |
| 198 | LazyInstance<Type, Traits>* me = |
| 199 | reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 200 | Traits::Delete(me->instance()); |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 201 | subtle::NoBarrier_Store(&me->private_instance_, 0); |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 202 | } |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } // namespace base |
| 206 | |
| 207 | #endif // BASE_LAZY_INSTANCE_H_ |