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 // PLEASE READ: Do you really need a singleton? | 5 // PLEASE READ: Do you really need a singleton? |
6 // | 6 // |
7 // Singletons make it hard to determine the lifetime of an object, which can | 7 // Singletons make it hard to determine the lifetime of an object, which can |
8 // lead to buggy code and spurious crashes. | 8 // lead to buggy code and spurious crashes. |
9 // | 9 // |
10 // Instead of adding another singleton into the mix, try to identify either: | 10 // Instead of adding another singleton into the mix, try to identify either: |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 // Our AtomicWord doubles as a spinlock, where a value of | 31 // Our AtomicWord doubles as a spinlock, where a value of |
32 // kBeingCreatedMarker means the spinlock is being held for creation. | 32 // kBeingCreatedMarker means the spinlock is being held for creation. |
33 static const subtle::AtomicWord kBeingCreatedMarker = 1; | 33 static const subtle::AtomicWord kBeingCreatedMarker = 1; |
34 | 34 |
35 // We pull out some of the functionality into a non-templated function, so that | 35 // We pull out some of the functionality into a non-templated function, so that |
36 // we can implement the more complicated pieces out of line in the .cc file. | 36 // we can implement the more complicated pieces out of line in the .cc file. |
37 BASE_EXPORT subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance); | 37 BASE_EXPORT subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance); |
38 | 38 |
39 } // namespace internal | 39 } // namespace internal |
40 } // namespace base | |
41 | 40 |
42 // TODO(joth): Move more of this file into namespace base | 41 // TODO(joth): Move more of this file into namespace base |
43 | 42 |
44 // Default traits for Singleton<Type>. Calls operator new and operator delete on | 43 // Default traits for Singleton<Type>. Calls operator new and operator delete on |
45 // the object. Registers automatic deletion at process exit. | 44 // the object. Registers automatic deletion at process exit. |
46 // Overload if you need arguments or another memory allocation function. | 45 // Overload if you need arguments or another memory allocation function. |
47 template<typename Type> | 46 template<typename Type> |
48 struct DefaultSingletonTraits { | 47 struct DefaultSingletonTraits { |
49 // Allocates the object. | 48 // Allocates the object. |
50 static Type* New() { | 49 static Type* New() { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // this is also used in e.g. Chrome Frame, where you have to allow for the | 102 // this is also used in e.g. Chrome Frame, where you have to allow for the |
104 // possibility of loading briefly into someone else's process space, and | 103 // possibility of loading briefly into someone else's process space, and |
105 // so leaking is not an option, as that would sabotage the state of your host | 104 // so leaking is not an option, as that would sabotage the state of your host |
106 // process once you've unloaded. | 105 // process once you've unloaded. |
107 template <typename Type> | 106 template <typename Type> |
108 struct StaticMemorySingletonTraits { | 107 struct StaticMemorySingletonTraits { |
109 // WARNING: User has to deal with get() in the singleton class | 108 // WARNING: User has to deal with get() in the singleton class |
110 // this is traits for returning NULL. | 109 // this is traits for returning NULL. |
111 static Type* New() { | 110 static Type* New() { |
112 // Only constructs once and returns pointer; otherwise returns NULL. | 111 // Only constructs once and returns pointer; otherwise returns NULL. |
113 if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) | 112 if (subtle::NoBarrier_AtomicExchange(&dead_, 1)) |
114 return NULL; | 113 return NULL; |
115 | 114 |
116 return new(buffer_.void_data()) Type(); | 115 return new(buffer_.void_data()) Type(); |
117 } | 116 } |
118 | 117 |
119 static void Delete(Type* p) { | 118 static void Delete(Type* p) { |
120 if (p != NULL) | 119 if (p != NULL) |
121 p->Type::~Type(); | 120 p->Type::~Type(); |
122 } | 121 } |
123 | 122 |
124 static const bool kRegisterAtExit = true; | 123 static const bool kRegisterAtExit = true; |
125 static const bool kAllowedToAccessOnNonjoinableThread = true; | 124 static const bool kAllowedToAccessOnNonjoinableThread = true; |
126 | 125 |
127 // Exposed for unittesting. | 126 // Exposed for unittesting. |
128 static void Resurrect() { | 127 static void Resurrect() { subtle::NoBarrier_Store(&dead_, 0); } |
129 base::subtle::NoBarrier_Store(&dead_, 0); | |
130 } | |
131 | 128 |
132 private: | 129 private: |
133 static base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_; | 130 static AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_; |
134 // Signal the object was already deleted, so it is not revived. | 131 // Signal the object was already deleted, so it is not revived. |
135 static base::subtle::Atomic32 dead_; | 132 static subtle::Atomic32 dead_; |
136 }; | 133 }; |
137 | 134 |
138 template <typename Type> base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> | 135 template <typename Type> |
| 136 AlignedMemory<sizeof(Type), ALIGNOF(Type)> |
139 StaticMemorySingletonTraits<Type>::buffer_; | 137 StaticMemorySingletonTraits<Type>::buffer_; |
140 template <typename Type> base::subtle::Atomic32 | 138 template <typename Type> |
141 StaticMemorySingletonTraits<Type>::dead_ = 0; | 139 subtle::Atomic32 StaticMemorySingletonTraits<Type>::dead_ = 0; |
142 | 140 |
143 // The Singleton<Type, Traits, DifferentiatingType> class manages a single | 141 // The Singleton<Type, Traits, DifferentiatingType> class manages a single |
144 // instance of Type which will be created on first use and will be destroyed at | 142 // instance of Type which will be created on first use and will be destroyed at |
145 // normal process exit). The Trait::Delete function will not be called on | 143 // normal process exit). The Trait::Delete function will not be called on |
146 // abnormal process exit. | 144 // abnormal process exit. |
147 // | 145 // |
148 // DifferentiatingType is used as a key to differentiate two different | 146 // DifferentiatingType is used as a key to differentiate two different |
149 // singletons having the same memory allocation functions but serving a | 147 // singletons having the same memory allocation functions but serving a |
150 // different purpose. This is mainly used for Locks serving different purposes. | 148 // different purpose. This is mainly used for Locks serving different purposes. |
151 // | 149 // |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // instantiated. | 181 // instantiated. |
184 // | 182 // |
185 // This class is itself thread-safe. The underlying Type must of course be | 183 // This class is itself thread-safe. The underlying Type must of course be |
186 // thread-safe if you want to use it concurrently. Two parameters may be tuned | 184 // thread-safe if you want to use it concurrently. Two parameters may be tuned |
187 // depending on the user's requirements. | 185 // depending on the user's requirements. |
188 // | 186 // |
189 // Glossary: | 187 // Glossary: |
190 // RAE = kRegisterAtExit | 188 // RAE = kRegisterAtExit |
191 // | 189 // |
192 // On every platform, if Traits::RAE is true, the singleton will be destroyed at | 190 // On every platform, if Traits::RAE is true, the singleton will be destroyed at |
193 // process exit. More precisely it uses base::AtExitManager which requires an | 191 // process exit. More precisely it uses AtExitManager which requires an |
194 // object of this type to be instantiated. AtExitManager mimics the semantics | 192 // object of this type to be instantiated. AtExitManager mimics the semantics |
195 // of atexit() such as LIFO order but under Windows is safer to call. For more | 193 // of atexit() such as LIFO order but under Windows is safer to call. For more |
196 // information see at_exit.h. | 194 // information see at_exit.h. |
197 // | 195 // |
198 // If Traits::RAE is false, the singleton will not be freed at process exit, | 196 // If Traits::RAE is false, the singleton will not be freed at process exit, |
199 // thus the singleton will be leaked if it is ever accessed. Traits::RAE | 197 // thus the singleton will be leaked if it is ever accessed. Traits::RAE |
200 // shouldn't be false unless absolutely necessary. Remember that the heap where | 198 // shouldn't be false unless absolutely necessary. Remember that the heap where |
201 // the object is allocated may be destroyed by the CRT anyway. | 199 // the object is allocated may be destroyed by the CRT anyway. |
202 // | 200 // |
203 // Caveats: | 201 // Caveats: |
(...skipping 18 matching lines...) Expand all Loading... |
222 friend class DeleteTraceLogForTesting; | 220 friend class DeleteTraceLogForTesting; |
223 | 221 |
224 // This class is safe to be constructed and copy-constructed since it has no | 222 // This class is safe to be constructed and copy-constructed since it has no |
225 // member. | 223 // member. |
226 | 224 |
227 // Return a pointer to the one true instance of the class. | 225 // Return a pointer to the one true instance of the class. |
228 static Type* get() { | 226 static Type* get() { |
229 #ifndef NDEBUG | 227 #ifndef NDEBUG |
230 // Avoid making TLS lookup on release builds. | 228 // Avoid making TLS lookup on release builds. |
231 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 229 if (!Traits::kAllowedToAccessOnNonjoinableThread) |
232 base::ThreadRestrictions::AssertSingletonAllowed(); | 230 ThreadRestrictions::AssertSingletonAllowed(); |
233 #endif | 231 #endif |
234 | 232 |
235 // The load has acquire memory ordering as the thread which reads the | 233 // The load has acquire memory ordering as the thread which reads the |
236 // instance_ pointer must acquire visibility over the singleton data. | 234 // instance_ pointer must acquire visibility over the singleton data. |
237 base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_); | 235 subtle::AtomicWord value = subtle::Acquire_Load(&instance_); |
238 if (value != 0 && value != base::internal::kBeingCreatedMarker) { | 236 if (value != 0 && value != internal::kBeingCreatedMarker) { |
239 return reinterpret_cast<Type*>(value); | 237 return reinterpret_cast<Type*>(value); |
240 } | 238 } |
241 | 239 |
242 // Object isn't created yet, maybe we will get to create it, let's try... | 240 // Object isn't created yet, maybe we will get to create it, let's try... |
243 if (base::subtle::Acquire_CompareAndSwap( | 241 if (subtle::Acquire_CompareAndSwap(&instance_, 0, |
244 &instance_, 0, base::internal::kBeingCreatedMarker) == 0) { | 242 internal::kBeingCreatedMarker) == 0) { |
245 // instance_ was NULL and is now kBeingCreatedMarker. Only one thread | 243 // instance_ was NULL and is now kBeingCreatedMarker. Only one thread |
246 // will ever get here. Threads might be spinning on us, and they will | 244 // will ever get here. Threads might be spinning on us, and they will |
247 // stop right after we do this store. | 245 // stop right after we do this store. |
248 Type* newval = Traits::New(); | 246 Type* newval = Traits::New(); |
249 | 247 |
250 // Releases the visibility over instance_ to the readers. | 248 // Releases the visibility over instance_ to the readers. |
251 base::subtle::Release_Store( | 249 subtle::Release_Store(&instance_, |
252 &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval)); | 250 reinterpret_cast<subtle::AtomicWord>(newval)); |
253 | 251 |
254 if (newval != NULL && Traits::kRegisterAtExit) | 252 if (newval != NULL && Traits::kRegisterAtExit) |
255 base::AtExitManager::RegisterCallback(OnExit, NULL); | 253 AtExitManager::RegisterCallback(OnExit, NULL); |
256 | 254 |
257 return newval; | 255 return newval; |
258 } | 256 } |
259 | 257 |
260 // We hit a race. Wait for the other thread to complete it. | 258 // We hit a race. Wait for the other thread to complete it. |
261 value = base::internal::WaitForInstance(&instance_); | 259 value = internal::WaitForInstance(&instance_); |
262 | 260 |
263 return reinterpret_cast<Type*>(value); | 261 return reinterpret_cast<Type*>(value); |
264 } | 262 } |
265 | 263 |
266 // Adapter function for use with AtExit(). This should be called single | 264 // Adapter function for use with AtExit(). This should be called single |
267 // threaded, so don't use atomic operations. | 265 // threaded, so don't use atomic operations. |
268 // Calling OnExit while singleton is in use by other threads is a mistake. | 266 // Calling OnExit while singleton is in use by other threads is a mistake. |
269 static void OnExit(void* /*unused*/) { | 267 static void OnExit(void* /*unused*/) { |
270 // AtExit should only ever be register after the singleton instance was | 268 // AtExit should only ever be register after the singleton instance was |
271 // created. We should only ever get here with a valid instance_ pointer. | 269 // created. We should only ever get here with a valid instance_ pointer. |
272 Traits::Delete( | 270 Traits::Delete(reinterpret_cast<Type*>(subtle::NoBarrier_Load(&instance_))); |
273 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); | |
274 instance_ = 0; | 271 instance_ = 0; |
275 } | 272 } |
276 static base::subtle::AtomicWord instance_; | 273 static subtle::AtomicWord instance_; |
277 }; | 274 }; |
278 | 275 |
279 template <typename Type, typename Traits, typename DifferentiatingType> | 276 template <typename Type, typename Traits, typename DifferentiatingType> |
280 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 277 subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::instance_ = 0; |
281 instance_ = 0; | |
282 | 278 |
| 279 } // namespace base |
283 #endif // BASE_MEMORY_SINGLETON_H_ | 280 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |