| 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 187 } |
| 189 | 188 |
| 190 Type* Pointer() { | 189 Type* Pointer() { |
| 191 #if DCHECK_IS_ON() | 190 #if DCHECK_IS_ON() |
| 192 // Avoid making TLS lookup on release builds. | 191 // Avoid making TLS lookup on release builds. |
| 193 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 192 if (!Traits::kAllowedToAccessOnNonjoinableThread) |
| 194 ThreadRestrictions::AssertSingletonAllowed(); | 193 ThreadRestrictions::AssertSingletonAllowed(); |
| 195 #endif | 194 #endif |
| 196 return static_cast<Type*>(internal::GetOrCreateLazyPointer( | 195 return static_cast<Type*>(internal::GetOrCreateLazyPointer( |
| 197 &private_instance_, | 196 &private_instance_, |
| 198 [this]() { return Traits::New(private_buf_.void_data()); }, | 197 [this]() { return Traits::New(private_buf_); }, |
| 199 Traits::kRegisterOnExit ? OnExit : nullptr, this)); | 198 Traits::kRegisterOnExit ? OnExit : nullptr, this)); |
| 200 } | 199 } |
| 201 | 200 |
| 202 bool operator==(Type* p) { | 201 bool operator==(Type* p) { |
| 203 switch (subtle::NoBarrier_Load(&private_instance_)) { | 202 switch (subtle::NoBarrier_Load(&private_instance_)) { |
| 204 case 0: | 203 case 0: |
| 205 return p == NULL; | 204 return p == NULL; |
| 206 case internal::kLazyInstanceStateCreating: | 205 case internal::kLazyInstanceStateCreating: |
| 207 return static_cast<void*>(p) == private_buf_.void_data(); | 206 return static_cast<void*>(p) == private_buf_; |
| 208 default: | 207 default: |
| 209 return p == instance(); | 208 return p == instance(); |
| 210 } | 209 } |
| 211 } | 210 } |
| 212 | 211 |
| 212 // MSVC gives a warning that the alignment expands the size of the |
| 213 // LazyInstance struct to make the size a multiple of the alignment. This |
| 214 // is expected in this case. |
| 215 #if defined(OS_WIN) |
| 216 #pragma warning(push) |
| 217 #pragma warning(disable: 4324) |
| 218 #endif |
| 219 |
| 213 // Effectively private: member data is only public to allow the linker to | 220 // Effectively private: member data is only public to allow the linker to |
| 214 // statically initialize it and to maintain a POD class. DO NOT USE FROM | 221 // statically initialize it and to maintain a POD class. DO NOT USE FROM |
| 215 // OUTSIDE THIS CLASS. | 222 // OUTSIDE THIS CLASS. |
| 223 subtle::AtomicWord private_instance_; |
| 216 | 224 |
| 217 subtle::AtomicWord private_instance_; | |
| 218 // Preallocated space for the Type instance. | 225 // Preallocated space for the Type instance. |
| 219 base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_; | 226 alignas(Type) char private_buf_[sizeof(Type)]; |
| 227 |
| 228 #if defined(OS_WIN) |
| 229 #pragma warning(pop) |
| 230 #endif |
| 220 | 231 |
| 221 private: | 232 private: |
| 222 Type* instance() { | 233 Type* instance() { |
| 223 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); | 234 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); |
| 224 } | 235 } |
| 225 | 236 |
| 226 // Adapter function for use with AtExit. This should be called single | 237 // Adapter function for use with AtExit. This should be called single |
| 227 // threaded, so don't synchronize across threads. | 238 // threaded, so don't synchronize across threads. |
| 228 // Calling OnExit while the instance is in use by other threads is a mistake. | 239 // Calling OnExit while the instance is in use by other threads is a mistake. |
| 229 static void OnExit(void* lazy_instance) { | 240 static void OnExit(void* lazy_instance) { |
| 230 LazyInstance<Type, Traits>* me = | 241 LazyInstance<Type, Traits>* me = |
| 231 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 242 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
| 232 Traits::Delete(me->instance()); | 243 Traits::Delete(me->instance()); |
| 233 subtle::NoBarrier_Store(&me->private_instance_, 0); | 244 subtle::NoBarrier_Store(&me->private_instance_, 0); |
| 234 } | 245 } |
| 235 }; | 246 }; |
| 236 | 247 |
| 237 } // namespace base | 248 } // namespace base |
| 238 | 249 |
| 239 #endif // BASE_LAZY_INSTANCE_H_ | 250 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |