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

Side by Side Diff: base/lazy_instance.cc

Issue 8491043: Allow linker initialization of lazy instance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: willchan comments + rebase Created 9 years, 1 month 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/lazy_instance.h ('k') | base/lazy_instance_unittest.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 #include "base/lazy_instance.h" 5 #include "base/lazy_instance.h"
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/atomicops.h" 8 #include "base/atomicops.h"
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
12 12
13 namespace base { 13 namespace base {
14 namespace internal {
14 15
15 bool LazyInstanceHelper::NeedsInstance() { 16 // TODO(joth): This function could be shared with Singleton, in place of its
16 // Try to create the instance, if we're the first, will go from EMPTY 17 // WaitForInstance() call.
17 // to CREATING, otherwise we've already been beaten here. 18 bool NeedsLazyInstance(subtle::AtomicWord* state) {
18 // The memory access has no memory ordering as STATE_EMPTY and STATE_CREATING 19 // Try to create the instance, if we're the first, will go from 0 to
19 // has no associated data (memory barriers are all about ordering 20 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
20 // of memory accesses to *associated* data). 21 // The memory access has no memory ordering as state 0 and
21 if (base::subtle::NoBarrier_CompareAndSwap( 22 // kLazyInstanceStateCreating have no associated data (memory barriers are
22 &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) 23 // all about ordering of memory accesses to *associated* data).
24 if (subtle::NoBarrier_CompareAndSwap(state, 0,
25 kLazyInstanceStateCreating) == 0)
23 // Caller must create instance 26 // Caller must create instance
24 return true; 27 return true;
25 28
26 // It's either in the process of being created, or already created. Spin. 29 // It's either in the process of being created, or already created. Spin.
27 // The load has acquire memory ordering as a thread which sees 30 // The load has acquire memory ordering as a thread which sees
28 // state_ == STATE_CREATED needs to acquire visibility over 31 // state_ == STATE_CREATED needs to acquire visibility over
29 // the associated data (buf_). Pairing Release_Store is in 32 // the associated data (buf_). Pairing Release_Store is in
30 // CompleteInstance(). 33 // CompleteLazyInstance().
31 while (base::subtle::Acquire_Load(&state_) != STATE_CREATED) 34 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
32 PlatformThread::YieldCurrentThread(); 35 PlatformThread::YieldCurrentThread();
33 36 }
34 // Someone else created the instance. 37 // Someone else created the instance.
35 return false; 38 return false;
36 } 39 }
37 40
38 void LazyInstanceHelper::CompleteInstance(void* instance, void (*dtor)(void*)) { 41 void CompleteLazyInstance(subtle::AtomicWord* state,
42 subtle::AtomicWord new_instance,
43 void* lazy_instance,
44 void (*dtor)(void*)) {
39 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). 45 // See the comment to the corresponding HAPPENS_AFTER in Pointer().
40 ANNOTATE_HAPPENS_BEFORE(&state_); 46 ANNOTATE_HAPPENS_BEFORE(state);
41 47
42 // Instance is created, go from CREATING to CREATED. 48 // Instance is created, go from CREATING to CREATED.
43 // Releases visibility over buf_ to readers. Pairing Acquire_Load's are in 49 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's
44 // NeedsInstance() and Pointer(). 50 // are in NeedsInstance() and Pointer().
45 base::subtle::Release_Store(&state_, STATE_CREATED); 51 subtle::Release_Store(state, new_instance);
46 52
47 // Make sure that the lazily instantiated object will get destroyed at exit. 53 // Make sure that the lazily instantiated object will get destroyed at exit.
48 if (dtor) 54 if (dtor)
49 base::AtExitManager::RegisterCallback(dtor, instance); 55 AtExitManager::RegisterCallback(dtor, lazy_instance);
50 } 56 }
51 57
58 } // namespace internal
52 } // namespace base 59 } // namespace base
53
54
55
OLDNEW
« no previous file with comments | « base/lazy_instance.h ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698