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