| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 struct LeakyLazyInstanceTraits { | 90 struct LeakyLazyInstanceTraits { |
| 91 static const bool kRegisterOnExit = false; | 91 static const bool kRegisterOnExit = false; |
| 92 #ifndef NDEBUG | 92 #ifndef NDEBUG |
| 93 static const bool kAllowedToAccessOnNonjoinableThread = true; | 93 static const bool kAllowedToAccessOnNonjoinableThread = true; |
| 94 #endif | 94 #endif |
| 95 | 95 |
| 96 static Type* New(void* instance) { | 96 static Type* New(void* instance) { |
| 97 ANNOTATE_SCOPED_MEMORY_LEAK; | 97 ANNOTATE_SCOPED_MEMORY_LEAK; |
| 98 return DefaultLazyInstanceTraits<Type>::New(instance); | 98 return DefaultLazyInstanceTraits<Type>::New(instance); |
| 99 } | 99 } |
| 100 static void Delete(Type* instance) { | 100 static void Delete(Type*) {} |
| 101 } | |
| 102 }; | 101 }; |
| 103 | 102 |
| 104 // Our AtomicWord doubles as a spinlock, where a value of | 103 // Our AtomicWord doubles as a spinlock, where a value of |
| 105 // kBeingCreatedMarker means the spinlock is being held for creation. | 104 // kBeingCreatedMarker means the spinlock is being held for creation. |
| 106 static const subtle::AtomicWord kLazyInstanceStateCreating = 1; | 105 static const subtle::AtomicWord kLazyInstanceStateCreating = 1; |
| 107 | 106 |
| 108 // Check if instance needs to be created. If so return true otherwise | 107 // Check if instance needs to be created. If so return true otherwise |
| 109 // if another thread has beat us, wait for instance to be created and | 108 // if another thread has beat us, wait for instance to be created and |
| 110 // return false. | 109 // return false. |
| 111 BASE_EXPORT bool NeedsLazyInstance(subtle::AtomicWord* state); | 110 BASE_EXPORT bool NeedsLazyInstance(subtle::AtomicWord* state); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 LazyInstance<Type, Traits>* me = | 197 LazyInstance<Type, Traits>* me = |
| 199 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 198 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
| 200 Traits::Delete(me->instance()); | 199 Traits::Delete(me->instance()); |
| 201 subtle::NoBarrier_Store(&me->private_instance_, 0); | 200 subtle::NoBarrier_Store(&me->private_instance_, 0); |
| 202 } | 201 } |
| 203 }; | 202 }; |
| 204 | 203 |
| 205 } // namespace base | 204 } // namespace base |
| 206 | 205 |
| 207 #endif // BASE_LAZY_INSTANCE_H_ | 206 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |