Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: base/memory/singleton.h

Issue 951673002: Revert "Pull chromium at 2c3ffb2355a27c32f45e508ef861416b820c823b" (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/scoped_ptr_unittest.cc ('k') | base/process/process_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/aligned_memory.h" 25 #include "base/memory/aligned_memory.h"
26 #include "base/third_party/dynamic_annotations/dynamic_annotations.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
35 // We pull out some of the functionality into a non-templated function, so that 36 // We pull out some of the functionality into a non-templated function, so that
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 #ifndef NDEBUG 230 #ifndef NDEBUG
230 // Avoid making TLS lookup on release builds. 231 // Avoid making TLS lookup on release builds.
231 if (!Traits::kAllowedToAccessOnNonjoinableThread) 232 if (!Traits::kAllowedToAccessOnNonjoinableThread)
232 base::ThreadRestrictions::AssertSingletonAllowed(); 233 base::ThreadRestrictions::AssertSingletonAllowed();
233 #endif 234 #endif
234 235
235 // The load has acquire memory ordering as the thread which reads the 236 // The load has acquire memory ordering as the thread which reads the
236 // instance_ pointer must acquire visibility over the singleton data. 237 // instance_ pointer must acquire visibility over the singleton data.
237 base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_); 238 base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_);
238 if (value != 0 && value != base::internal::kBeingCreatedMarker) { 239 if (value != 0 && value != base::internal::kBeingCreatedMarker) {
240 // See the corresponding HAPPENS_BEFORE below.
241 ANNOTATE_HAPPENS_AFTER(&instance_);
239 return reinterpret_cast<Type*>(value); 242 return reinterpret_cast<Type*>(value);
240 } 243 }
241 244
242 // Object isn't created yet, maybe we will get to create it, let's try... 245 // Object isn't created yet, maybe we will get to create it, let's try...
243 if (base::subtle::Acquire_CompareAndSwap( 246 if (base::subtle::Acquire_CompareAndSwap(
244 &instance_, 0, base::internal::kBeingCreatedMarker) == 0) { 247 &instance_, 0, base::internal::kBeingCreatedMarker) == 0) {
245 // instance_ was NULL and is now kBeingCreatedMarker. Only one thread 248 // instance_ was NULL and is now kBeingCreatedMarker. Only one thread
246 // will ever get here. Threads might be spinning on us, and they will 249 // will ever get here. Threads might be spinning on us, and they will
247 // stop right after we do this store. 250 // stop right after we do this store.
248 Type* newval = Traits::New(); 251 Type* newval = Traits::New();
249 252
253 // This annotation helps race detectors recognize correct lock-less
254 // synchronization between different threads calling get().
255 // See the corresponding HAPPENS_AFTER below and above.
256 ANNOTATE_HAPPENS_BEFORE(&instance_);
250 // Releases the visibility over instance_ to the readers. 257 // Releases the visibility over instance_ to the readers.
251 base::subtle::Release_Store( 258 base::subtle::Release_Store(
252 &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval)); 259 &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval));
253 260
254 if (newval != NULL && Traits::kRegisterAtExit) 261 if (newval != NULL && Traits::kRegisterAtExit)
255 base::AtExitManager::RegisterCallback(OnExit, NULL); 262 base::AtExitManager::RegisterCallback(OnExit, NULL);
256 263
257 return newval; 264 return newval;
258 } 265 }
259 266
260 // We hit a race. Wait for the other thread to complete it. 267 // We hit a race. Wait for the other thread to complete it.
261 value = base::internal::WaitForInstance(&instance_); 268 value = base::internal::WaitForInstance(&instance_);
262 269
270 // See the corresponding HAPPENS_BEFORE above.
271 ANNOTATE_HAPPENS_AFTER(&instance_);
263 return reinterpret_cast<Type*>(value); 272 return reinterpret_cast<Type*>(value);
264 } 273 }
265 274
266 // Adapter function for use with AtExit(). This should be called single 275 // Adapter function for use with AtExit(). This should be called single
267 // threaded, so don't use atomic operations. 276 // threaded, so don't use atomic operations.
268 // Calling OnExit while singleton is in use by other threads is a mistake. 277 // Calling OnExit while singleton is in use by other threads is a mistake.
269 static void OnExit(void* /*unused*/) { 278 static void OnExit(void* /*unused*/) {
270 // AtExit should only ever be register after the singleton instance was 279 // AtExit should only ever be register after the singleton instance was
271 // created. We should only ever get here with a valid instance_ pointer. 280 // created. We should only ever get here with a valid instance_ pointer.
272 Traits::Delete( 281 Traits::Delete(
273 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); 282 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_)));
274 instance_ = 0; 283 instance_ = 0;
275 } 284 }
276 static base::subtle::AtomicWord instance_; 285 static base::subtle::AtomicWord instance_;
277 }; 286 };
278 287
279 template <typename Type, typename Traits, typename DifferentiatingType> 288 template <typename Type, typename Traits, typename DifferentiatingType>
280 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: 289 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::
281 instance_ = 0; 290 instance_ = 0;
282 291
283 #endif // BASE_MEMORY_SINGLETON_H_ 292 #endif // BASE_MEMORY_SINGLETON_H_
OLDNEW
« no previous file with comments | « base/memory/scoped_ptr_unittest.cc ('k') | base/process/process_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698