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

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

Issue 188603002: Fix base::internal::Singleton to use acquire loads instead of nobarrier ones. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « base/memory/singleton.h ('k') | no next file » | 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 #include "base/memory/singleton.h" 5 #include "base/memory/singleton.h"
6 #include "base/threading/platform_thread.h" 6 #include "base/threading/platform_thread.h"
7 7
8 namespace base { 8 namespace base {
9 namespace internal { 9 namespace internal {
10 10
11 subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) { 11 subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) {
12 // Handle the race. Another thread beat us and either: 12 // Handle the race. Another thread beat us and either:
13 // - Has the object in BeingCreated state 13 // - Has the object in BeingCreated state
14 // - Already has the object created... 14 // - Already has the object created...
15 // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr. 15 // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr.
16 // Unless your constructor can be very time consuming, it is very unlikely 16 // Unless your constructor can be very time consuming, it is very unlikely
17 // to hit this race. When it does, we just spin and yield the thread until 17 // to hit this race. When it does, we just spin and yield the thread until
18 // the object has been created. 18 // the object has been created.
19 subtle::AtomicWord value; 19 subtle::AtomicWord value;
20 while (true) { 20 while (true) {
21 value = subtle::NoBarrier_Load(instance); 21 // The load has acquire memory ordering as the thread which reads the
22 // instance pointer must acquire visibility over the associated data.
23 // The pairing Release_Store operation is in Singleton::get().
24 value = subtle::Acquire_Load(instance);
22 if (value != kBeingCreatedMarker) 25 if (value != kBeingCreatedMarker)
23 break; 26 break;
24 PlatformThread::YieldCurrentThread(); 27 PlatformThread::YieldCurrentThread();
25 } 28 }
26 return value; 29 return value;
27 } 30 }
28 31
29 } // namespace internal 32 } // namespace internal
30 } // namespace base 33 } // namespace base
31 34
OLDNEW
« no previous file with comments | « base/memory/singleton.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698