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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // Only constructs once and returns pointer; otherwise returns NULL. | 109 // Only constructs once and returns pointer; otherwise returns NULL. |
110 if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) | 110 if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) |
111 return NULL; | 111 return NULL; |
112 | 112 |
113 return new(buffer_.void_data()) Type(); | 113 return new(buffer_.void_data()) Type(); |
114 } | 114 } |
115 | 115 |
116 static void Delete(Type* p) { | 116 static void Delete(Type* p) { |
117 if (p != NULL) | 117 if (p != NULL) |
118 p->Type::~Type(); | 118 p->Type::~Type(); |
| 119 |
| 120 // Only in unittests, automatically Resurrect() the singleton so that tests |
| 121 // do not have to manually call it. See <http://crbug.com/258047>. |
| 122 #if defined(UNIT_TEST) |
| 123 Resurrect(); |
| 124 #endif |
119 } | 125 } |
120 | 126 |
121 static const bool kRegisterAtExit = true; | 127 static const bool kRegisterAtExit = true; |
122 static const bool kAllowedToAccessOnNonjoinableThread = true; | 128 static const bool kAllowedToAccessOnNonjoinableThread = true; |
123 | 129 |
124 // Exposed for unittesting. | 130 // Exposed for unittesting. |
125 static void Resurrect() { | 131 static void Resurrect() { |
126 base::subtle::NoBarrier_Store(&dead_, 0); | 132 base::subtle::NoBarrier_Store(&dead_, 0); |
127 } | 133 } |
128 | 134 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 instance_ = 0; | 282 instance_ = 0; |
277 } | 283 } |
278 static base::subtle::AtomicWord instance_; | 284 static base::subtle::AtomicWord instance_; |
279 }; | 285 }; |
280 | 286 |
281 template <typename Type, typename Traits, typename DifferentiatingType> | 287 template <typename Type, typename Traits, typename DifferentiatingType> |
282 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 288 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
283 instance_ = 0; | 289 instance_ = 0; |
284 | 290 |
285 #endif // BASE_MEMORY_SINGLETON_H_ | 291 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |