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

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

Issue 790473002: Remove the happens-before annotations from base/memory/singleton.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compilation of content/browser/gamepad/gamepad_provider.cc 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 | « no previous file | base/trace_event/trace_event_synthetic_delay.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"
27 #include "base/threading/thread_restrictions.h" 26 #include "base/threading/thread_restrictions.h"
28 27
29 namespace base { 28 namespace base {
30 namespace internal { 29 namespace internal {
31 30
32 // Our AtomicWord doubles as a spinlock, where a value of 31 // Our AtomicWord doubles as a spinlock, where a value of
33 // kBeingCreatedMarker means the spinlock is being held for creation. 32 // kBeingCreatedMarker means the spinlock is being held for creation.
34 static const subtle::AtomicWord kBeingCreatedMarker = 1; 33 static const subtle::AtomicWord kBeingCreatedMarker = 1;
35 34
36 // We pull out some of the functionality into a non-templated function, so that 35 // 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
230 #ifndef NDEBUG 229 #ifndef NDEBUG
231 // Avoid making TLS lookup on release builds. 230 // Avoid making TLS lookup on release builds.
232 if (!Traits::kAllowedToAccessOnNonjoinableThread) 231 if (!Traits::kAllowedToAccessOnNonjoinableThread)
233 base::ThreadRestrictions::AssertSingletonAllowed(); 232 base::ThreadRestrictions::AssertSingletonAllowed();
234 #endif 233 #endif
235 234
236 // The load has acquire memory ordering as the thread which reads the 235 // The load has acquire memory ordering as the thread which reads the
237 // instance_ pointer must acquire visibility over the singleton data. 236 // instance_ pointer must acquire visibility over the singleton data.
238 base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_); 237 base::subtle::AtomicWord value = base::subtle::Acquire_Load(&instance_);
239 if (value != 0 && value != base::internal::kBeingCreatedMarker) { 238 if (value != 0 && value != base::internal::kBeingCreatedMarker) {
240 // See the corresponding HAPPENS_BEFORE below.
241 ANNOTATE_HAPPENS_AFTER(&instance_);
242 return reinterpret_cast<Type*>(value); 239 return reinterpret_cast<Type*>(value);
243 } 240 }
244 241
245 // Object isn't created yet, maybe we will get to create it, let's try... 242 // Object isn't created yet, maybe we will get to create it, let's try...
246 if (base::subtle::Acquire_CompareAndSwap( 243 if (base::subtle::Acquire_CompareAndSwap(
247 &instance_, 0, base::internal::kBeingCreatedMarker) == 0) { 244 &instance_, 0, base::internal::kBeingCreatedMarker) == 0) {
248 // instance_ was NULL and is now kBeingCreatedMarker. Only one thread 245 // instance_ was NULL and is now kBeingCreatedMarker. Only one thread
249 // will ever get here. Threads might be spinning on us, and they will 246 // will ever get here. Threads might be spinning on us, and they will
250 // stop right after we do this store. 247 // stop right after we do this store.
251 Type* newval = Traits::New(); 248 Type* newval = Traits::New();
252 249
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_);
257 // Releases the visibility over instance_ to the readers. 250 // Releases the visibility over instance_ to the readers.
258 base::subtle::Release_Store( 251 base::subtle::Release_Store(
259 &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval)); 252 &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval));
260 253
261 if (newval != NULL && Traits::kRegisterAtExit) 254 if (newval != NULL && Traits::kRegisterAtExit)
262 base::AtExitManager::RegisterCallback(OnExit, NULL); 255 base::AtExitManager::RegisterCallback(OnExit, NULL);
263 256
264 return newval; 257 return newval;
265 } 258 }
266 259
267 // We hit a race. Wait for the other thread to complete it. 260 // We hit a race. Wait for the other thread to complete it.
268 value = base::internal::WaitForInstance(&instance_); 261 value = base::internal::WaitForInstance(&instance_);
269 262
270 // See the corresponding HAPPENS_BEFORE above.
271 ANNOTATE_HAPPENS_AFTER(&instance_);
272 return reinterpret_cast<Type*>(value); 263 return reinterpret_cast<Type*>(value);
273 } 264 }
274 265
275 // Adapter function for use with AtExit(). This should be called single 266 // Adapter function for use with AtExit(). This should be called single
276 // threaded, so don't use atomic operations. 267 // threaded, so don't use atomic operations.
277 // Calling OnExit while singleton is in use by other threads is a mistake. 268 // Calling OnExit while singleton is in use by other threads is a mistake.
278 static void OnExit(void* /*unused*/) { 269 static void OnExit(void* /*unused*/) {
279 // AtExit should only ever be register after the singleton instance was 270 // AtExit should only ever be register after the singleton instance was
280 // created. We should only ever get here with a valid instance_ pointer. 271 // created. We should only ever get here with a valid instance_ pointer.
281 Traits::Delete( 272 Traits::Delete(
282 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); 273 reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_)));
283 instance_ = 0; 274 instance_ = 0;
284 } 275 }
285 static base::subtle::AtomicWord instance_; 276 static base::subtle::AtomicWord instance_;
286 }; 277 };
287 278
288 template <typename Type, typename Traits, typename DifferentiatingType> 279 template <typename Type, typename Traits, typename DifferentiatingType>
289 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: 280 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>::
290 instance_ = 0; 281 instance_ = 0;
291 282
292 #endif // BASE_MEMORY_SINGLETON_H_ 283 #endif // BASE_MEMORY_SINGLETON_H_
OLDNEW
« no previous file with comments | « no previous file | base/trace_event/trace_event_synthetic_delay.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698