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 #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" | |
12 | 11 |
13 namespace base { | 12 namespace base { |
14 namespace internal { | 13 namespace internal { |
15 | 14 |
16 // TODO(joth): This function could be shared with Singleton, in place of its | 15 // TODO(joth): This function could be shared with Singleton, in place of its |
17 // WaitForInstance() call. | 16 // WaitForInstance() call. |
18 bool NeedsLazyInstance(subtle::AtomicWord* state) { | 17 bool NeedsLazyInstance(subtle::AtomicWord* state) { |
19 // Try to create the instance, if we're the first, will go from 0 to | 18 // Try to create the instance, if we're the first, will go from 0 to |
20 // kLazyInstanceStateCreating, otherwise we've already been beaten here. | 19 // kLazyInstanceStateCreating, otherwise we've already been beaten here. |
21 // The memory access has no memory ordering as state 0 and | 20 // The memory access has no memory ordering as state 0 and |
(...skipping 13 matching lines...) Expand all Loading... |
35 PlatformThread::YieldCurrentThread(); | 34 PlatformThread::YieldCurrentThread(); |
36 } | 35 } |
37 // Someone else created the instance. | 36 // Someone else created the instance. |
38 return false; | 37 return false; |
39 } | 38 } |
40 | 39 |
41 void CompleteLazyInstance(subtle::AtomicWord* state, | 40 void CompleteLazyInstance(subtle::AtomicWord* state, |
42 subtle::AtomicWord new_instance, | 41 subtle::AtomicWord new_instance, |
43 void* lazy_instance, | 42 void* lazy_instance, |
44 void (*dtor)(void*)) { | 43 void (*dtor)(void*)) { |
45 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). | |
46 ANNOTATE_HAPPENS_BEFORE(state); | |
47 | |
48 // Instance is created, go from CREATING to CREATED. | 44 // Instance is created, go from CREATING to CREATED. |
49 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's | 45 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's |
50 // are in NeedsInstance() and Pointer(). | 46 // are in NeedsInstance() and Pointer(). |
51 subtle::Release_Store(state, new_instance); | 47 subtle::Release_Store(state, new_instance); |
52 | 48 |
53 // Make sure that the lazily instantiated object will get destroyed at exit. | 49 // Make sure that the lazily instantiated object will get destroyed at exit. |
54 if (dtor) | 50 if (dtor) |
55 AtExitManager::RegisterCallback(dtor, lazy_instance); | 51 AtExitManager::RegisterCallback(dtor, lazy_instance); |
56 } | 52 } |
57 | 53 |
58 } // namespace internal | 54 } // namespace internal |
59 } // namespace base | 55 } // namespace base |
OLD | NEW |