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 18 matching lines...) Expand all Loading... |
29 // inst.Get().SomeMethod(); // MyClass::SomeMethod() | 29 // inst.Get().SomeMethod(); // MyClass::SomeMethod() |
30 // | 30 // |
31 // MyClass* ptr = inst.Pointer(); | 31 // MyClass* ptr = inst.Pointer(); |
32 // ptr->DoDoDo(); // MyClass::DoDoDo | 32 // ptr->DoDoDo(); // MyClass::DoDoDo |
33 // } | 33 // } |
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 #include <type_traits> |
39 | 40 |
40 #include "base/atomicops.h" | 41 #include "base/atomicops.h" |
41 #include "base/base_export.h" | 42 #include "base/base_export.h" |
42 #include "base/debug/leak_annotations.h" | 43 #include "base/debug/leak_annotations.h" |
43 #include "base/logging.h" | 44 #include "base/logging.h" |
44 #include "base/memory/aligned_memory.h" | |
45 #include "base/threading/thread_restrictions.h" | 45 #include "base/threading/thread_restrictions.h" |
46 | 46 |
47 // LazyInstance uses its own struct initializer-list style static | 47 // LazyInstance uses its own struct initializer-list style static |
48 // initialization, as base's LINKER_INITIALIZED requires a constructor and on | 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 | 49 // some compilers (notably gcc 4.4) this still ends up needing runtime |
50 // initialization. | 50 // initialization. |
51 #define LAZY_INSTANCE_INITIALIZER {0} | 51 #define LAZY_INSTANCE_INITIALIZER {0} |
52 | 52 |
53 namespace base { | 53 namespace base { |
54 | 54 |
55 template <typename Type> | 55 template <typename Type> |
56 struct LazyInstanceTraitsBase { | 56 struct LazyInstanceTraitsBase { |
57 static Type* New(void* instance) { | 57 static Type* New(void* instance) { |
58 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u); | 58 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (alignof(Type) - 1), 0u); |
59 // Use placement new to initialize our instance in our preallocated space. | 59 // Use placement new to initialize our instance in our preallocated space. |
60 // The parenthesis is very important here to force POD type initialization. | 60 // The parenthesis is very important here to force POD type initialization. |
61 return new (instance) Type(); | 61 return new (instance) Type(); |
62 } | 62 } |
63 | 63 |
64 static void CallDestructor(Type* instance) { | 64 static void CallDestructor(Type* instance) { |
65 // Explicitly call the destructor. | 65 // Explicitly call the destructor. |
66 instance->~Type(); | 66 instance->~Type(); |
67 } | 67 } |
68 }; | 68 }; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating | 172 // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating |
173 // at most once, the load is taken out of NeedsInstance() as a fast-path. | 173 // 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 | 174 // The load has acquire memory ordering as a thread which sees |
175 // private_instance_ > creating needs to acquire visibility over | 175 // private_instance_ > creating needs to acquire visibility over |
176 // the associated data (private_buf_). Pairing Release_Store is in | 176 // the associated data (private_buf_). Pairing Release_Store is in |
177 // CompleteLazyInstance(). | 177 // CompleteLazyInstance(). |
178 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); | 178 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); |
179 if (!(value & kLazyInstanceCreatedMask) && | 179 if (!(value & kLazyInstanceCreatedMask) && |
180 internal::NeedsLazyInstance(&private_instance_)) { | 180 internal::NeedsLazyInstance(&private_instance_)) { |
181 // Create the instance in the space provided by |private_buf_|. | 181 // Create the instance in the space provided by |private_buf_|. |
182 value = reinterpret_cast<subtle::AtomicWord>( | 182 value = reinterpret_cast<subtle::AtomicWord>(Traits::New(&private_buf_)); |
183 Traits::New(private_buf_.void_data())); | |
184 internal::CompleteLazyInstance(&private_instance_, value, this, | 183 internal::CompleteLazyInstance(&private_instance_, value, this, |
185 Traits::kRegisterOnExit ? OnExit : NULL); | 184 Traits::kRegisterOnExit ? OnExit : NULL); |
186 } | 185 } |
187 return instance(); | 186 return instance(); |
188 } | 187 } |
189 | 188 |
190 bool operator==(Type* p) { | 189 bool operator==(Type* p) { |
191 switch (subtle::NoBarrier_Load(&private_instance_)) { | 190 switch (subtle::NoBarrier_Load(&private_instance_)) { |
192 case 0: | 191 case 0: |
193 return p == NULL; | 192 return p == NULL; |
194 case internal::kLazyInstanceStateCreating: | 193 case internal::kLazyInstanceStateCreating: |
195 return static_cast<void*>(p) == private_buf_.void_data(); | 194 return static_cast<void*>(p) == &private_buf_; |
196 default: | 195 default: |
197 return p == instance(); | 196 return p == instance(); |
198 } | 197 } |
199 } | 198 } |
200 | 199 |
201 // Effectively private: member data is only public to allow the linker to | 200 // 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 | 201 // statically initialize it and to maintain a POD class. DO NOT USE FROM |
203 // OUTSIDE THIS CLASS. | 202 // OUTSIDE THIS CLASS. |
204 | 203 |
205 subtle::AtomicWord private_instance_; | 204 subtle::AtomicWord private_instance_; |
206 // Preallocated space for the Type instance. | 205 // Preallocated space for the Type instance. |
207 base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_; | 206 typename std::aligned_storage<sizeof(Type), alignof(Type)>::type private_buf_; |
208 | 207 |
209 private: | 208 private: |
210 Type* instance() { | 209 Type* instance() { |
211 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); | 210 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); |
212 } | 211 } |
213 | 212 |
214 // Adapter function for use with AtExit. This should be called single | 213 // Adapter function for use with AtExit. This should be called single |
215 // threaded, so don't synchronize across threads. | 214 // threaded, so don't synchronize across threads. |
216 // Calling OnExit while the instance is in use by other threads is a mistake. | 215 // Calling OnExit while the instance is in use by other threads is a mistake. |
217 static void OnExit(void* lazy_instance) { | 216 static void OnExit(void* lazy_instance) { |
218 LazyInstance<Type, Traits>* me = | 217 LazyInstance<Type, Traits>* me = |
219 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 218 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
220 Traits::Delete(me->instance()); | 219 Traits::Delete(me->instance()); |
221 subtle::NoBarrier_Store(&me->private_instance_, 0); | 220 subtle::NoBarrier_Store(&me->private_instance_, 0); |
222 } | 221 } |
223 }; | 222 }; |
224 | 223 |
225 } // namespace base | 224 } // namespace base |
226 | 225 |
227 #endif // BASE_LAZY_INSTANCE_H_ | 226 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |