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: |
11 // a) An existing singleton that can manage your object's lifetime | 11 // a) An existing singleton that can manage your object's lifetime |
12 // b) Locations where you can deterministically create the object and pass | 12 // b) Locations where you can deterministically create the object and pass |
13 // into other objects | 13 // into other objects |
14 // | 14 // |
15 // If you absolutely need a singleton, please keep them as trivial as possible | 15 // If you absolutely need a singleton, please keep them as trivial as possible |
16 // and ideally a leaf dependency. Singletons get problematic when they attempt | 16 // and ideally a leaf dependency. Singletons get problematic when they attempt |
17 // to do too much in their destructor or have circular dependencies. | 17 // to do too much in their destructor or have circular dependencies. |
18 | 18 |
19 #ifndef BASE_MEMORY_SINGLETON_H_ | 19 #ifndef BASE_MEMORY_SINGLETON_H_ |
20 #define BASE_MEMORY_SINGLETON_H_ | 20 #define BASE_MEMORY_SINGLETON_H_ |
21 | 21 |
22 #include "base/at_exit.h" | 22 #include "base/at_exit.h" |
23 #include "base/atomicops.h" | 23 #include "base/atomicops.h" |
24 #include "base/base_export.h" | 24 #include "base/base_export.h" |
| 25 #include "base/macros.h" |
25 #include "base/memory/aligned_memory.h" | 26 #include "base/memory/aligned_memory.h" |
26 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
27 | 28 |
28 namespace base { | 29 namespace base { |
29 namespace internal { | 30 namespace internal { |
30 | 31 |
31 // Our AtomicWord doubles as a spinlock, where a value of | 32 // Our AtomicWord doubles as a spinlock, where a value of |
32 // kBeingCreatedMarker means the spinlock is being held for creation. | 33 // kBeingCreatedMarker means the spinlock is being held for creation. |
33 static const subtle::AtomicWord kBeingCreatedMarker = 1; | 34 static const subtle::AtomicWord kBeingCreatedMarker = 1; |
34 | 35 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 275 } |
275 static subtle::AtomicWord instance_; | 276 static subtle::AtomicWord instance_; |
276 }; | 277 }; |
277 | 278 |
278 template <typename Type, typename Traits, typename DifferentiatingType> | 279 template <typename Type, typename Traits, typename DifferentiatingType> |
279 subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::instance_ = 0; | 280 subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::instance_ = 0; |
280 | 281 |
281 } // namespace base | 282 } // namespace base |
282 | 283 |
283 #endif // BASE_MEMORY_SINGLETON_H_ | 284 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |