| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // The LazyInstance<Type, Traits> class manages a single instance of Type, | 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 | 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 | 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 | 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() | 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. | 10 // and Pointer() will always return the same, completely initialized instance. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 #ifndef BASE_LAZY_INSTANCE_H_ | 35 #ifndef BASE_LAZY_INSTANCE_H_ |
| 36 #define BASE_LAZY_INSTANCE_H_ | 36 #define BASE_LAZY_INSTANCE_H_ |
| 37 | 37 |
| 38 #include <new> // For placement new. | 38 #include <new> // For placement new. |
| 39 | 39 |
| 40 #include "base/atomicops.h" | 40 #include "base/atomicops.h" |
| 41 #include "base/base_export.h" | 41 #include "base/base_export.h" |
| 42 #include "base/debug/leak_annotations.h" | 42 #include "base/debug/leak_annotations.h" |
| 43 #include "base/logging.h" | 43 #include "base/logging.h" |
| 44 #include "base/memory/aligned_memory.h" | |
| 45 #include "base/threading/thread_restrictions.h" | 44 #include "base/threading/thread_restrictions.h" |
| 46 | 45 |
| 47 // LazyInstance uses its own struct initializer-list style static | 46 // LazyInstance uses its own struct initializer-list style static |
| 48 // initialization, as base's LINKER_INITIALIZED requires a constructor and on | 47 // initialization, as base's LINKER_INITIALIZED requires a constructor and on |
| 49 // some compilers (notably gcc 4.4) this still ends up needing runtime | 48 // some compilers (notably gcc 4.4) this still ends up needing runtime |
| 50 // initialization. | 49 // initialization. |
| 51 #define LAZY_INSTANCE_INITIALIZER {0} | 50 #define LAZY_INSTANCE_INITIALIZER {0} |
| 52 | 51 |
| 53 namespace base { | 52 namespace base { |
| 54 | 53 |
| 55 template <typename Type> | 54 template <typename Type> |
| 56 struct LazyInstanceTraitsBase { | 55 struct LazyInstanceTraitsBase { |
| 57 static Type* New(void* instance) { | 56 static Type* New(void* instance) { |
| 58 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u); | 57 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (alignof(Type) - 1), 0u); |
| 59 // Use placement new to initialize our instance in our preallocated space. | 58 // Use placement new to initialize our instance in our preallocated space. |
| 60 // The parenthesis is very important here to force POD type initialization. | 59 // The parenthesis is very important here to force POD type initialization. |
| 61 return new (instance) Type(); | 60 return new (instance) Type(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 static void CallDestructor(Type* instance) { | 63 static void CallDestructor(Type* instance) { |
| 65 // Explicitly call the destructor. | 64 // Explicitly call the destructor. |
| 66 instance->~Type(); | 65 instance->~Type(); |
| 67 } | 66 } |
| 68 }; | 67 }; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating | 171 // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating |
| 173 // at most once, the load is taken out of NeedsInstance() as a fast-path. | 172 // at most once, the load is taken out of NeedsInstance() as a fast-path. |
| 174 // The load has acquire memory ordering as a thread which sees | 173 // The load has acquire memory ordering as a thread which sees |
| 175 // private_instance_ > creating needs to acquire visibility over | 174 // private_instance_ > creating needs to acquire visibility over |
| 176 // the associated data (private_buf_). Pairing Release_Store is in | 175 // the associated data (private_buf_). Pairing Release_Store is in |
| 177 // CompleteLazyInstance(). | 176 // CompleteLazyInstance(). |
| 178 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); | 177 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); |
| 179 if (!(value & kLazyInstanceCreatedMask) && | 178 if (!(value & kLazyInstanceCreatedMask) && |
| 180 internal::NeedsLazyInstance(&private_instance_)) { | 179 internal::NeedsLazyInstance(&private_instance_)) { |
| 181 // Create the instance in the space provided by |private_buf_|. | 180 // Create the instance in the space provided by |private_buf_|. |
| 182 value = reinterpret_cast<subtle::AtomicWord>( | 181 value = reinterpret_cast<subtle::AtomicWord>(Traits::New(private_buf_)); |
| 183 Traits::New(private_buf_.void_data())); | |
| 184 internal::CompleteLazyInstance(&private_instance_, value, this, | 182 internal::CompleteLazyInstance(&private_instance_, value, this, |
| 185 Traits::kRegisterOnExit ? OnExit : NULL); | 183 Traits::kRegisterOnExit ? OnExit : NULL); |
| 186 } | 184 } |
| 187 return instance(); | 185 return instance(); |
| 188 } | 186 } |
| 189 | 187 |
| 190 bool operator==(Type* p) { | 188 bool operator==(Type* p) { |
| 191 switch (subtle::NoBarrier_Load(&private_instance_)) { | 189 switch (subtle::NoBarrier_Load(&private_instance_)) { |
| 192 case 0: | 190 case 0: |
| 193 return p == NULL; | 191 return p == NULL; |
| 194 case internal::kLazyInstanceStateCreating: | 192 case internal::kLazyInstanceStateCreating: |
| 195 return static_cast<void*>(p) == private_buf_.void_data(); | 193 return static_cast<void*>(p) == private_buf_; |
| 196 default: | 194 default: |
| 197 return p == instance(); | 195 return p == instance(); |
| 198 } | 196 } |
| 199 } | 197 } |
| 200 | 198 |
| 199 // MSVC gives a warning that the alignment expands the size of the |
| 200 // LazyInstance struct to make the size a multiple of the alignment. This |
| 201 // is expected in this case. |
| 202 #if defined(OS_WIN) |
| 203 #pragma warning(push) |
| 204 #pragma warning(disable: 4324) |
| 205 #endif |
| 206 |
| 201 // Effectively private: member data is only public to allow the linker to | 207 // Effectively private: member data is only public to allow the linker to |
| 202 // statically initialize it and to maintain a POD class. DO NOT USE FROM | 208 // statically initialize it and to maintain a POD class. DO NOT USE FROM |
| 203 // OUTSIDE THIS CLASS. | 209 // OUTSIDE THIS CLASS. |
| 210 subtle::AtomicWord private_instance_; |
| 204 | 211 |
| 205 subtle::AtomicWord private_instance_; | |
| 206 // Preallocated space for the Type instance. | 212 // Preallocated space for the Type instance. |
| 207 base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_; | 213 alignas(Type) char private_buf_[sizeof(Type)]; |
| 214 |
| 215 #if defined(OS_WIN) |
| 216 #pragma warning(pop) |
| 217 #endif |
| 208 | 218 |
| 209 private: | 219 private: |
| 210 Type* instance() { | 220 Type* instance() { |
| 211 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); | 221 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); |
| 212 } | 222 } |
| 213 | 223 |
| 214 // Adapter function for use with AtExit. This should be called single | 224 // Adapter function for use with AtExit. This should be called single |
| 215 // threaded, so don't synchronize across threads. | 225 // threaded, so don't synchronize across threads. |
| 216 // Calling OnExit while the instance is in use by other threads is a mistake. | 226 // Calling OnExit while the instance is in use by other threads is a mistake. |
| 217 static void OnExit(void* lazy_instance) { | 227 static void OnExit(void* lazy_instance) { |
| 218 LazyInstance<Type, Traits>* me = | 228 LazyInstance<Type, Traits>* me = |
| 219 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 229 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
| 220 Traits::Delete(me->instance()); | 230 Traits::Delete(me->instance()); |
| 221 subtle::NoBarrier_Store(&me->private_instance_, 0); | 231 subtle::NoBarrier_Store(&me->private_instance_, 0); |
| 222 } | 232 } |
| 223 }; | 233 }; |
| 224 | 234 |
| 225 } // namespace base | 235 } // namespace base |
| 226 | 236 |
| 227 #endif // BASE_LAZY_INSTANCE_H_ | 237 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |