| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 return p == instance(); | 180 return p == instance(); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 // Effectively private: member data is only public to allow the linker to | 184 // Effectively private: member data is only public to allow the linker to |
| 185 // statically initialize it and to maintain a POD class. DO NOT USE FROM | 185 // statically initialize it and to maintain a POD class. DO NOT USE FROM |
| 186 // OUTSIDE THIS CLASS. | 186 // OUTSIDE THIS CLASS. |
| 187 | 187 |
| 188 subtle::AtomicWord private_instance_; | 188 subtle::AtomicWord private_instance_; |
| 189 // Preallocated space for the Type instance. | 189 // Preallocated space for the Type instance. |
| 190 base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_; | 190 base::AlignedMemory<ALIGNOF(Type), sizeof(Type)> private_buf_; |
| 191 | 191 |
| 192 private: | 192 private: |
| 193 Type* instance() { | 193 Type* instance() { |
| 194 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); | 194 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); |
| 195 } | 195 } |
| 196 | 196 |
| 197 // Adapter function for use with AtExit. This should be called single | 197 // Adapter function for use with AtExit. This should be called single |
| 198 // threaded, so don't synchronize across threads. | 198 // threaded, so don't synchronize across threads. |
| 199 // Calling OnExit while the instance is in use by other threads is a mistake. | 199 // Calling OnExit while the instance is in use by other threads is a mistake. |
| 200 static void OnExit(void* lazy_instance) { | 200 static void OnExit(void* lazy_instance) { |
| 201 LazyInstance<Type, Traits>* me = | 201 LazyInstance<Type, Traits>* me = |
| 202 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 202 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
| 203 Traits::Delete(me->instance()); | 203 Traits::Delete(me->instance()); |
| 204 subtle::NoBarrier_Store(&me->private_instance_, 0); | 204 subtle::NoBarrier_Store(&me->private_instance_, 0); |
| 205 } | 205 } |
| 206 }; | 206 }; |
| 207 | 207 |
| 208 } // namespace base | 208 } // namespace base |
| 209 | 209 |
| 210 #endif // BASE_LAZY_INSTANCE_H_ | 210 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |