| 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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 friend Type* Type::GetInstance(); | 206 friend Type* Type::GetInstance(); |
| 207 | 207 |
| 208 // Allow TraceLog tests to test tracing after OnExit. | 208 // Allow TraceLog tests to test tracing after OnExit. |
| 209 friend class DeleteTraceLogForTesting; | 209 friend class DeleteTraceLogForTesting; |
| 210 | 210 |
| 211 // This class is safe to be constructed and copy-constructed since it has no | 211 // This class is safe to be constructed and copy-constructed since it has no |
| 212 // member. | 212 // member. |
| 213 | 213 |
| 214 // Return a pointer to the one true instance of the class. | 214 // Return a pointer to the one true instance of the class. |
| 215 static Type* get() { | 215 static Type* get() { |
| 216 #ifndef NDEBUG |
| 217 // Avoid making TLS lookup on release builds. |
| 216 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 218 if (!Traits::kAllowedToAccessOnNonjoinableThread) |
| 217 base::ThreadRestrictions::AssertSingletonAllowed(); | 219 base::ThreadRestrictions::AssertSingletonAllowed(); |
| 220 #endif |
| 218 | 221 |
| 219 // Our AtomicWord doubles as a spinlock, where a value of | 222 // Our AtomicWord doubles as a spinlock, where a value of |
| 220 // kBeingCreatedMarker means the spinlock is being held for creation. | 223 // kBeingCreatedMarker means the spinlock is being held for creation. |
| 221 static const base::subtle::AtomicWord kBeingCreatedMarker = 1; | 224 static const base::subtle::AtomicWord kBeingCreatedMarker = 1; |
| 222 | 225 |
| 223 base::subtle::AtomicWord value = base::subtle::NoBarrier_Load(&instance_); | 226 base::subtle::AtomicWord value = base::subtle::NoBarrier_Load(&instance_); |
| 224 if (value != 0 && value != kBeingCreatedMarker) { | 227 if (value != 0 && value != kBeingCreatedMarker) { |
| 225 // See the corresponding HAPPENS_BEFORE below. | 228 // See the corresponding HAPPENS_BEFORE below. |
| 226 ANNOTATE_HAPPENS_AFTER(&instance_); | 229 ANNOTATE_HAPPENS_AFTER(&instance_); |
| 227 return reinterpret_cast<Type*>(value); | 230 return reinterpret_cast<Type*>(value); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 instance_ = 0; | 282 instance_ = 0; |
| 280 } | 283 } |
| 281 static base::subtle::AtomicWord instance_; | 284 static base::subtle::AtomicWord instance_; |
| 282 }; | 285 }; |
| 283 | 286 |
| 284 template <typename Type, typename Traits, typename DifferentiatingType> | 287 template <typename Type, typename Traits, typename DifferentiatingType> |
| 285 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 288 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
| 286 instance_ = 0; | 289 instance_ = 0; |
| 287 | 290 |
| 288 #endif // BASE_MEMORY_SINGLETON_H_ | 291 #endif // BASE_MEMORY_SINGLETON_H_ |
| OLD | NEW |