OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 // Use placement new to initialize our instance in our preallocated space. | 66 // Use placement new to initialize our instance in our preallocated space. |
67 // The parenthesis is very important here to force POD type initialization. | 67 // The parenthesis is very important here to force POD type initialization. |
68 return new (instance) Type(); | 68 return new (instance) Type(); |
69 } | 69 } |
70 static void Delete(Type* instance) { | 70 static void Delete(Type* instance) { |
71 // Explicitly call the destructor. | 71 // Explicitly call the destructor. |
72 instance->~Type(); | 72 instance->~Type(); |
73 } | 73 } |
74 }; | 74 }; |
75 | 75 |
76 // Use LazyInstance<T>::Leaky for a less-verbose call-site typedef; e.g.: | |
77 // LazyInstance<MyType>::Leaky my_leaky_lazy_instance; | |
wtc
2012/01/21 01:47:36
I suggest also showing the verbose form it replace
Ami GONE FROM CHROMIUM
2012/01/21 04:49:20
Done.
| |
76 template <typename Type> | 78 template <typename Type> |
77 struct LeakyLazyInstanceTraits { | 79 struct LeakyLazyInstanceTraits { |
78 static const bool kRegisterOnExit = false; | 80 static const bool kRegisterOnExit = false; |
79 static const bool kAllowedToAccessOnNonjoinableThread = true; | 81 static const bool kAllowedToAccessOnNonjoinableThread = true; |
80 | 82 |
81 static Type* New(void* instance) { | 83 static Type* New(void* instance) { |
82 return DefaultLazyInstanceTraits<Type>::New(instance); | 84 return DefaultLazyInstanceTraits<Type>::New(instance); |
83 } | 85 } |
84 static void Delete(Type* instance) { | 86 static void Delete(Type* instance) { |
85 } | 87 } |
(...skipping 24 matching lines...) Expand all Loading... | |
110 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > | 112 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
111 class LazyInstance { | 113 class LazyInstance { |
112 public: | 114 public: |
113 // Do not define a destructor, as doing so makes LazyInstance a | 115 // Do not define a destructor, as doing so makes LazyInstance a |
114 // non-POD-struct. We don't want that because then a static initializer will | 116 // non-POD-struct. We don't want that because then a static initializer will |
115 // be created to register the (empty) destructor with atexit() under MSVC, for | 117 // be created to register the (empty) destructor with atexit() under MSVC, for |
116 // example. We handle destruction of the contained Type class explicitly via | 118 // example. We handle destruction of the contained Type class explicitly via |
117 // the OnExit member function, where needed. | 119 // the OnExit member function, where needed. |
118 // ~LazyInstance() {} | 120 // ~LazyInstance() {} |
119 | 121 |
122 // Convenience typedef to avoid having to repeat Type for leaky lazy | |
123 // instances. | |
124 typedef LazyInstance<Type, LeakyLazyInstanceTraits<Type> > Leaky; | |
125 | |
120 Type& Get() { | 126 Type& Get() { |
121 return *Pointer(); | 127 return *Pointer(); |
122 } | 128 } |
123 | 129 |
124 Type* Pointer() { | 130 Type* Pointer() { |
125 #ifndef NDEBUG | 131 #ifndef NDEBUG |
126 // Avoid making TLS lookup on release builds. | 132 // Avoid making TLS lookup on release builds. |
127 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 133 if (!Traits::kAllowedToAccessOnNonjoinableThread) |
128 ThreadRestrictions::AssertSingletonAllowed(); | 134 ThreadRestrictions::AssertSingletonAllowed(); |
129 #endif | 135 #endif |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 LazyInstance<Type, Traits>* me = | 195 LazyInstance<Type, Traits>* me = |
190 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 196 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
191 Traits::Delete(me->instance()); | 197 Traits::Delete(me->instance()); |
192 subtle::Release_Store(&me->private_instance_, 0); | 198 subtle::Release_Store(&me->private_instance_, 0); |
193 } | 199 } |
194 }; | 200 }; |
195 | 201 |
196 } // namespace base | 202 } // namespace base |
197 | 203 |
198 #endif // BASE_LAZY_INSTANCE_H_ | 204 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |