| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef BASE_SINGLETON_H_ | 5 #ifndef BASE_SINGLETON_H_ |
| 6 #define BASE_SINGLETON_H_ | 6 #define BASE_SINGLETON_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 11 #include "base/platform_thread.h" | |
| 12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "base/thread_restrictions.h" | 13 #include "base/thread_restrictions.h" |
| 14 | 14 |
| 15 // Default traits for Singleton<Type>. Calls operator new and operator delete on | 15 // Default traits for Singleton<Type>. Calls operator new and operator delete on |
| 16 // the object. Registers automatic deletion at process exit. | 16 // the object. Registers automatic deletion at process exit. |
| 17 // Overload if you need arguments or another memory allocation function. | 17 // Overload if you need arguments or another memory allocation function. |
| 18 template<typename Type> | 18 template<typename Type> |
| 19 struct DefaultSingletonTraits { | 19 struct DefaultSingletonTraits { |
| 20 // Allocates the object. | 20 // Allocates the object. |
| 21 static Type* New() { | 21 static Type* New() { |
| 22 // The parenthesis is very important here; it forces POD type | 22 // The parenthesis is very important here; it forces POD type |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 // - Has the object in BeingCreated state | 236 // - Has the object in BeingCreated state |
| 237 // - Already has the object created... | 237 // - Already has the object created... |
| 238 // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr. | 238 // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr. |
| 239 // Unless your constructor can be very time consuming, it is very unlikely | 239 // Unless your constructor can be very time consuming, it is very unlikely |
| 240 // to hit this race. When it does, we just spin and yield the thread until | 240 // to hit this race. When it does, we just spin and yield the thread until |
| 241 // the object has been created. | 241 // the object has been created. |
| 242 while (true) { | 242 while (true) { |
| 243 value = base::subtle::NoBarrier_Load(&instance_); | 243 value = base::subtle::NoBarrier_Load(&instance_); |
| 244 if (value != kBeingCreatedMarker) | 244 if (value != kBeingCreatedMarker) |
| 245 break; | 245 break; |
| 246 PlatformThread::YieldCurrentThread(); | 246 base::PlatformThread::YieldCurrentThread(); |
| 247 } | 247 } |
| 248 | 248 |
| 249 // See the corresponding HAPPENS_BEFORE above. | 249 // See the corresponding HAPPENS_BEFORE above. |
| 250 ANNOTATE_HAPPENS_AFTER(&instance_); | 250 ANNOTATE_HAPPENS_AFTER(&instance_); |
| 251 return reinterpret_cast<Type*>(value); | 251 return reinterpret_cast<Type*>(value); |
| 252 } | 252 } |
| 253 | 253 |
| 254 // Adapter function for use with AtExit(). This should be called single | 254 // Adapter function for use with AtExit(). This should be called single |
| 255 // threaded, so don't use atomic operations. | 255 // threaded, so don't use atomic operations. |
| 256 // Calling OnExit while singleton is in use by other threads is a mistake. | 256 // Calling OnExit while singleton is in use by other threads is a mistake. |
| 257 static void OnExit(void* unused) { | 257 static void OnExit(void* unused) { |
| 258 // AtExit should only ever be register after the singleton instance was | 258 // AtExit should only ever be register after the singleton instance was |
| 259 // created. We should only ever get here with a valid instance_ pointer. | 259 // created. We should only ever get here with a valid instance_ pointer. |
| 260 Traits::Delete( | 260 Traits::Delete( |
| 261 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); | 261 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); |
| 262 instance_ = 0; | 262 instance_ = 0; |
| 263 } | 263 } |
| 264 static base::subtle::AtomicWord instance_; | 264 static base::subtle::AtomicWord instance_; |
| 265 }; | 265 }; |
| 266 | 266 |
| 267 template <typename Type, typename Traits, typename DifferentiatingType> | 267 template <typename Type, typename Traits, typename DifferentiatingType> |
| 268 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 268 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
| 269 instance_ = 0; | 269 instance_ = 0; |
| 270 | 270 |
| 271 #endif // BASE_SINGLETON_H_ | 271 #endif // BASE_SINGLETON_H_ |
| OLD | NEW |