OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/dynamic_annotations.h" | 9 #include "base/dynamic_annotations.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/platform_thread.h" | 11 #include "base/platform_thread.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 void LazyInstanceHelper::EnsureInstance(void* instance, | 15 bool LazyInstanceHelper::NeedsInstance() { |
16 void (*ctor)(void*), | |
17 void (*dtor)(void*)) { | |
18 // Try to create the instance, if we're the first, will go from EMPTY | 16 // Try to create the instance, if we're the first, will go from EMPTY |
19 // to CREATING, otherwise we've already been beaten here. | 17 // to CREATING, otherwise we've already been beaten here. |
20 if (base::subtle::Acquire_CompareAndSwap( | 18 if (base::subtle::Acquire_CompareAndSwap( |
21 &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) { | 19 &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) { |
22 // Created the instance in the space provided by |instance|. | 20 // Caller must create instance |
23 ctor(instance); | 21 return true; |
24 | |
25 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). | |
26 ANNOTATE_HAPPENS_BEFORE(&state_); | |
27 | |
28 // Instance is created, go from CREATING to CREATED. | |
29 base::subtle::Release_Store(&state_, STATE_CREATED); | |
30 | |
31 // Allow reusing the LazyInstance (reset it to the initial state). This | |
32 // makes possible calling all AtExit callbacks between tests. Assumes that | |
33 // no other threads execute when AtExit callbacks are processed. | |
34 base::AtExitManager::RegisterCallback(&LazyInstanceHelper::ResetState, | |
35 this); | |
36 | |
37 // Make sure that the lazily instantiated object will get destroyed at exit. | |
38 base::AtExitManager::RegisterCallback(dtor, instance); | |
39 } else { | 22 } else { |
40 // It's either in the process of being created, or already created. Spin. | 23 // It's either in the process of being created, or already created. Spin. |
41 while (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) | 24 while (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) |
42 PlatformThread::YieldCurrentThread(); | 25 PlatformThread::YieldCurrentThread(); |
43 } | 26 } |
| 27 |
| 28 // Someone else created the instance. |
| 29 return false; |
| 30 } |
| 31 |
| 32 void LazyInstanceHelper::CompleteInstance(void* instance, void (*dtor)(void*)) { |
| 33 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). |
| 34 ANNOTATE_HAPPENS_BEFORE(&state_); |
| 35 |
| 36 // Instance is created, go from CREATING to CREATED. |
| 37 base::subtle::Release_Store(&state_, STATE_CREATED); |
| 38 |
| 39 // Allow reusing the LazyInstance (reset it to the initial state). This |
| 40 // makes possible calling all AtExit callbacks between tests. Assumes that |
| 41 // no other threads execute when AtExit callbacks are processed. |
| 42 base::AtExitManager::RegisterCallback(&LazyInstanceHelper::ResetState, |
| 43 this); |
| 44 |
| 45 // Make sure that the lazily instantiated object will get destroyed at exit. |
| 46 base::AtExitManager::RegisterCallback(dtor, instance); |
44 } | 47 } |
45 | 48 |
46 // static | 49 // static |
47 void LazyInstanceHelper::ResetState(void* helper) { | 50 void LazyInstanceHelper::ResetState(void* helper) { |
48 reinterpret_cast<LazyInstanceHelper*>(helper)->state_ = STATE_EMPTY; | 51 reinterpret_cast<LazyInstanceHelper*>(helper)->state_ = STATE_EMPTY; |
49 } | 52 } |
50 | 53 |
51 } // namespace base | 54 } // namespace base |
OLD | NEW |