dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/lazy_instance.h" |
| 6 | |
| 7 | #include "base/at_exit.h" |
| 8 | #include "base/atomicops.h" |
| 9 | #include "base/basictypes.h" |
brettw@chromium.org | 6139182 | 2011-01-01 05:02:16 +0900 | [diff] [blame] | 10 | #include "base/threading/platform_thread.h" |
timurrrr@chromium.org | f39c3ff | 2010-05-14 17:24:42 +0900 | [diff] [blame] | 11 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 15 | bool LazyInstanceHelper::NeedsInstance() { |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 16 | // Try to create the instance, if we're the first, will go from EMPTY |
| 17 | // to CREATING, otherwise we've already been beaten here. |
dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 18 | // The memory access has no memory ordering as STATE_EMPTY and STATE_CREATING |
| 19 | // has no associated data (memory barriers are all about ordering |
| 20 | // of memory accesses to *associated* data). |
| 21 | if (base::subtle::NoBarrier_CompareAndSwap( |
| 22 | &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 23 | // Caller must create instance |
| 24 | return true; |
dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 25 | |
| 26 | // 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 |
| 28 | // state_ == STATE_CREATED needs to acquire visibility over |
| 29 | // the associated data (buf_). Pairing Release_Store is in |
| 30 | // CompleteInstance(). |
| 31 | while (base::subtle::Acquire_Load(&state_) != STATE_CREATED) |
| 32 | PlatformThread::YieldCurrentThread(); |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 33 | |
| 34 | // Someone else created the instance. |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | void LazyInstanceHelper::CompleteInstance(void* instance, void (*dtor)(void*)) { |
| 39 | // See the comment to the corresponding HAPPENS_AFTER in Pointer(). |
| 40 | ANNOTATE_HAPPENS_BEFORE(&state_); |
| 41 | |
| 42 | // Instance is created, go from CREATING to CREATED. |
dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 43 | // Releases visibility over buf_ to readers. Pairing Acquire_Load's are in |
| 44 | // NeedsInstance() and Pointer(). |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 45 | base::subtle::Release_Store(&state_, STATE_CREATED); |
| 46 | |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 47 | // Make sure that the lazily instantiated object will get destroyed at exit. |
evan@chromium.org | a04f70c | 2010-10-22 05:41:47 +0900 | [diff] [blame] | 48 | if (dtor) |
| 49 | base::AtExitManager::RegisterCallback(dtor, instance); |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 50 | } |
| 51 | |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 52 | } // namespace base |
dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 53 | |
| 54 | |
| 55 | |