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 22 matching lines...) Expand all Loading... |
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 | 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/logging.h" | 44 #include "base/logging.h" |
44 #include "base/memory/aligned_memory.h" | 45 #include "base/memory/aligned_memory.h" |
45 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 46 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
46 #include "base/threading/thread_restrictions.h" | 47 #include "base/threading/thread_restrictions.h" |
47 | 48 |
48 // LazyInstance uses its own struct initializer-list style static | 49 // LazyInstance uses its own struct initializer-list style static |
49 // initialization, as base's LINKER_INITIALIZED requires a constructor and on | 50 // initialization, as base's LINKER_INITIALIZED requires a constructor and on |
50 // some compilers (notably gcc 4.4) this still ends up needing runtime | 51 // some compilers (notably gcc 4.4) this still ends up needing runtime |
51 // initialization. | 52 // initialization. |
52 #define LAZY_INSTANCE_INITIALIZER {0} | 53 #define LAZY_INSTANCE_INITIALIZER {0} |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // my_leaky_lazy_instance; | 85 // my_leaky_lazy_instance; |
85 // (especially when T is MyLongTypeNameImplClientHolderFactory). | 86 // (especially when T is MyLongTypeNameImplClientHolderFactory). |
86 // Only use this internal::-qualified verbose form to extend this traits class | 87 // Only use this internal::-qualified verbose form to extend this traits class |
87 // (depending on its implementation details). | 88 // (depending on its implementation details). |
88 template <typename Type> | 89 template <typename Type> |
89 struct LeakyLazyInstanceTraits { | 90 struct LeakyLazyInstanceTraits { |
90 static const bool kRegisterOnExit = false; | 91 static const bool kRegisterOnExit = false; |
91 static const bool kAllowedToAccessOnNonjoinableThread = true; | 92 static const bool kAllowedToAccessOnNonjoinableThread = true; |
92 | 93 |
93 static Type* New(void* instance) { | 94 static Type* New(void* instance) { |
| 95 ANNOTATE_SCOPED_MEMORY_LEAK; |
94 return DefaultLazyInstanceTraits<Type>::New(instance); | 96 return DefaultLazyInstanceTraits<Type>::New(instance); |
95 } | 97 } |
96 static void Delete(Type* instance) { | 98 static void Delete(Type* instance) { |
97 } | 99 } |
98 }; | 100 }; |
99 | 101 |
100 // Our AtomicWord doubles as a spinlock, where a value of | 102 // Our AtomicWord doubles as a spinlock, where a value of |
101 // kBeingCreatedMarker means the spinlock is being held for creation. | 103 // kBeingCreatedMarker means the spinlock is being held for creation. |
102 static const subtle::AtomicWord kLazyInstanceStateCreating = 1; | 104 static const subtle::AtomicWord kLazyInstanceStateCreating = 1; |
103 | 105 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 LazyInstance<Type, Traits>* me = | 203 LazyInstance<Type, Traits>* me = |
202 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 204 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
203 Traits::Delete(me->instance()); | 205 Traits::Delete(me->instance()); |
204 subtle::NoBarrier_Store(&me->private_instance_, 0); | 206 subtle::NoBarrier_Store(&me->private_instance_, 0); |
205 } | 207 } |
206 }; | 208 }; |
207 | 209 |
208 } // namespace base | 210 } // namespace base |
209 | 211 |
210 #endif // BASE_LAZY_INSTANCE_H_ | 212 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |