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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 p->Type::~Type(); | 123 p->Type::~Type(); |
124 } | 124 } |
125 | 125 |
126 static const bool kRegisterAtExit = true; | 126 static const bool kRegisterAtExit = true; |
127 static const bool kAllowedToAccessOnNonjoinableThread = true; | 127 static const bool kAllowedToAccessOnNonjoinableThread = true; |
128 | 128 |
129 // Exposed for unittesting. | 129 // Exposed for unittesting. |
130 static void Resurrect() { subtle::NoBarrier_Store(&dead_, 0); } | 130 static void Resurrect() { subtle::NoBarrier_Store(&dead_, 0); } |
131 | 131 |
132 private: | 132 private: |
133 static AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_; | 133 static AlignedMemory<sizeof(Type), alignof(Type)> buffer_; |
134 // Signal the object was already deleted, so it is not revived. | 134 // Signal the object was already deleted, so it is not revived. |
135 static subtle::Atomic32 dead_; | 135 static subtle::Atomic32 dead_; |
136 }; | 136 }; |
137 | 137 |
138 template <typename Type> | 138 template <typename Type> |
139 AlignedMemory<sizeof(Type), ALIGNOF(Type)> | 139 AlignedMemory<sizeof(Type), alignof(Type)> |
140 StaticMemorySingletonTraits<Type>::buffer_; | 140 StaticMemorySingletonTraits<Type>::buffer_; |
141 template <typename Type> | 141 template <typename Type> |
142 subtle::Atomic32 StaticMemorySingletonTraits<Type>::dead_ = 0; | 142 subtle::Atomic32 StaticMemorySingletonTraits<Type>::dead_ = 0; |
143 | 143 |
144 // The Singleton<Type, Traits, DifferentiatingType> class manages a single | 144 // The Singleton<Type, Traits, DifferentiatingType> class manages a single |
145 // instance of Type which will be created on first use and will be destroyed at | 145 // instance of Type which will be created on first use and will be destroyed at |
146 // normal process exit). The Trait::Delete function will not be called on | 146 // normal process exit). The Trait::Delete function will not be called on |
147 // abnormal process exit. | 147 // abnormal process exit. |
148 // | 148 // |
149 // DifferentiatingType is used as a key to differentiate two different | 149 // DifferentiatingType is used as a key to differentiate two different |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 } | 276 } |
277 static subtle::AtomicWord instance_; | 277 static subtle::AtomicWord instance_; |
278 }; | 278 }; |
279 | 279 |
280 template <typename Type, typename Traits, typename DifferentiatingType> | 280 template <typename Type, typename Traits, typename DifferentiatingType> |
281 subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::instance_ = 0; | 281 subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::instance_ = 0; |
282 | 282 |
283 } // namespace base | 283 } // namespace base |
284 | 284 |
285 #endif // BASE_MEMORY_SINGLETON_H_ | 285 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |