Chromium Code Reviews| 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" | 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 Singlton, in place of its |
|
M-A Ruel
2011/11/11 21:34:06
Singleton
joth
2011/11/14 16:58:23
Done.
| |
| 17 // WaitForInstance() call. | |
| 18 bool NeedsLazyInstance(base::subtle::AtomicWord* state) { | |
| 16 // Try to create the instance, if we're the first, will go from EMPTY | 19 // Try to create the instance, if we're the first, will go from EMPTY |
| 17 // to CREATING, otherwise we've already been beaten here. | 20 // to CREATING, otherwise we've already been beaten here. |
| 18 // The memory access has no memory ordering as STATE_EMPTY and STATE_CREATING | 21 // The memory access has no memory ordering as STATE_EMPTY and STATE_CREATING |
| 19 // has no associated data (memory barriers are all about ordering | 22 // has no associated data (memory barriers are all about ordering |
| 20 // of memory accesses to *associated* data). | 23 // of memory accesses to *associated* data). |
| 21 if (base::subtle::NoBarrier_CompareAndSwap( | 24 if (base::subtle::NoBarrier_CompareAndSwap( |
| 22 &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) | 25 state, 0, internal::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 // CompleteInstance(). |
| 31 while (base::subtle::Acquire_Load(&state_) != STATE_CREATED) | 34 while (base::subtle::Acquire_Load(state) == |
| 35 internal::kLazyInstanceStateCreating) { | |
| 32 PlatformThread::YieldCurrentThread(); | 36 PlatformThread::YieldCurrentThread(); |
| 33 | 37 } |
| 34 // Someone else created the instance. | 38 // Someone else created the instance. |
| 35 return false; | 39 return false; |
| 36 } | 40 } |
| 37 | 41 |
| 38 void LazyInstanceHelper::CompleteInstance(void* instance, void (*dtor)(void*)) { | 42 void CompleteLazyInstance(base::subtle::AtomicWord* state, |
| 43 base::subtle::AtomicWord new_instance, | |
| 44 void* lazy_instance, | |
| 45 void (*dtor)(void*)) { | |
| 39 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). | 46 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). |
| 40 ANNOTATE_HAPPENS_BEFORE(&state_); | 47 ANNOTATE_HAPPENS_BEFORE(state); |
| 41 | 48 |
| 42 // Instance is created, go from CREATING to CREATED. | 49 // Instance is created, go from CREATING to CREATED. |
| 43 // Releases visibility over buf_ to readers. Pairing Acquire_Load's are in | 50 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's |
| 44 // NeedsInstance() and Pointer(). | 51 // are in NeedsInstance() and Pointer(). |
| 45 base::subtle::Release_Store(&state_, STATE_CREATED); | 52 base::subtle::Release_Store(state, new_instance); |
| 46 | 53 |
| 47 // Make sure that the lazily instantiated object will get destroyed at exit. | 54 // Make sure that the lazily instantiated object will get destroyed at exit. |
| 48 if (dtor) | 55 if (dtor) |
| 49 base::AtExitManager::RegisterCallback(dtor, instance); | 56 base::AtExitManager::RegisterCallback(dtor, lazy_instance); |
| 50 } | 57 } |
| 51 | 58 |
| 59 } // namespace internal | |
| 52 } // namespace base | 60 } // namespace base |
| 53 | 61 |
|
M-A Ruel
2011/11/11 21:34:06
remove too
joth
2011/11/14 16:58:23
Done.
| |
| 54 | |
| 55 | |
| OLD | NEW |