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 25 matching lines...) Expand all Loading... |
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/basictypes.h" | 42 #include "base/basictypes.h" |
43 #include "base/debug/leak_annotations.h" | 43 #include "base/debug/leak_annotations.h" |
44 #include "base/logging.h" | 44 #include "base/logging.h" |
45 #include "base/memory/aligned_memory.h" | 45 #include "base/memory/aligned_memory.h" |
46 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
47 #include "base/threading/thread_restrictions.h" | 46 #include "base/threading/thread_restrictions.h" |
48 | 47 |
49 // LazyInstance uses its own struct initializer-list style static | 48 // LazyInstance uses its own struct initializer-list style static |
50 // initialization, as base's LINKER_INITIALIZED requires a constructor and on | 49 // initialization, as base's LINKER_INITIALIZED requires a constructor and on |
51 // some compilers (notably gcc 4.4) this still ends up needing runtime | 50 // some compilers (notably gcc 4.4) this still ends up needing runtime |
52 // initialization. | 51 // initialization. |
53 #define LAZY_INSTANCE_INITIALIZER {0} | 52 #define LAZY_INSTANCE_INITIALIZER {0} |
54 | 53 |
55 namespace base { | 54 namespace base { |
56 | 55 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 // CompleteLazyInstance(). | 158 // CompleteLazyInstance(). |
160 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); | 159 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); |
161 if (!(value & kLazyInstanceCreatedMask) && | 160 if (!(value & kLazyInstanceCreatedMask) && |
162 internal::NeedsLazyInstance(&private_instance_)) { | 161 internal::NeedsLazyInstance(&private_instance_)) { |
163 // Create the instance in the space provided by |private_buf_|. | 162 // Create the instance in the space provided by |private_buf_|. |
164 value = reinterpret_cast<subtle::AtomicWord>( | 163 value = reinterpret_cast<subtle::AtomicWord>( |
165 Traits::New(private_buf_.void_data())); | 164 Traits::New(private_buf_.void_data())); |
166 internal::CompleteLazyInstance(&private_instance_, value, this, | 165 internal::CompleteLazyInstance(&private_instance_, value, this, |
167 Traits::kRegisterOnExit ? OnExit : NULL); | 166 Traits::kRegisterOnExit ? OnExit : NULL); |
168 } | 167 } |
169 | |
170 // This annotation helps race detectors recognize correct lock-less | |
171 // synchronization between different threads calling Pointer(). | |
172 // We suggest dynamic race detection tool that "Traits::New" above | |
173 // and CompleteLazyInstance(...) happens before "return instance()" below. | |
174 // See the corresponding HAPPENS_BEFORE in CompleteLazyInstance(...). | |
175 ANNOTATE_HAPPENS_AFTER(&private_instance_); | |
176 return instance(); | 168 return instance(); |
177 } | 169 } |
178 | 170 |
179 bool operator==(Type* p) { | 171 bool operator==(Type* p) { |
180 switch (subtle::NoBarrier_Load(&private_instance_)) { | 172 switch (subtle::NoBarrier_Load(&private_instance_)) { |
181 case 0: | 173 case 0: |
182 return p == NULL; | 174 return p == NULL; |
183 case internal::kLazyInstanceStateCreating: | 175 case internal::kLazyInstanceStateCreating: |
184 return static_cast<void*>(p) == private_buf_.void_data(); | 176 return static_cast<void*>(p) == private_buf_.void_data(); |
185 default: | 177 default: |
(...skipping 21 matching lines...) Expand all Loading... |
207 LazyInstance<Type, Traits>* me = | 199 LazyInstance<Type, Traits>* me = |
208 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 200 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
209 Traits::Delete(me->instance()); | 201 Traits::Delete(me->instance()); |
210 subtle::NoBarrier_Store(&me->private_instance_, 0); | 202 subtle::NoBarrier_Store(&me->private_instance_, 0); |
211 } | 203 } |
212 }; | 204 }; |
213 | 205 |
214 } // namespace base | 206 } // namespace base |
215 | 207 |
216 #endif // BASE_LAZY_INSTANCE_H_ | 208 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |