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 { |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 14 | namespace internal { |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 15 | |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 16 | // TODO(joth): This function could be shared with Singleton, in place of its |
| 17 | // WaitForInstance() call. |
| 18 | bool NeedsLazyInstance(subtle::AtomicWord* state) { |
| 19 | // Try to create the instance, if we're the first, will go from 0 to |
| 20 | // kLazyInstanceStateCreating, otherwise we've already been beaten here. |
| 21 | // The memory access has no memory ordering as state 0 and |
| 22 | // kLazyInstanceStateCreating have no associated data (memory barriers are |
| 23 | // all about ordering of memory accesses to *associated* data). |
| 24 | if (subtle::NoBarrier_CompareAndSwap(state, 0, |
| 25 | kLazyInstanceStateCreating) == 0) |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 26 | // Caller must create instance |
| 27 | return true; |
dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 28 | |
| 29 | // It's either in the process of being created, or already created. Spin. |
| 30 | // The load has acquire memory ordering as a thread which sees |
| 31 | // state_ == STATE_CREATED needs to acquire visibility over |
| 32 | // the associated data (buf_). Pairing Release_Store is in |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 33 | // CompleteLazyInstance(). |
| 34 | while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) { |
dvyukov@google.com | 6beeb83 | 2011-05-17 00:01:54 +0900 | [diff] [blame] | 35 | PlatformThread::YieldCurrentThread(); |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 36 | } |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 37 | // Someone else created the instance. |
| 38 | return false; |
| 39 | } |
| 40 | |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 41 | void CompleteLazyInstance(subtle::AtomicWord* state, |
| 42 | subtle::AtomicWord new_instance, |
| 43 | void* lazy_instance, |
| 44 | void (*dtor)(void*)) { |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 45 | // See the comment to the corresponding HAPPENS_AFTER in Pointer(). |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 46 | ANNOTATE_HAPPENS_BEFORE(state); |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 47 | |
| 48 | // Instance is created, go from CREATING to CREATED. |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 49 | // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's |
| 50 | // are in NeedsInstance() and Pointer(). |
| 51 | subtle::Release_Store(state, new_instance); |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 52 | |
craig.schlenter@chromium.org | 0cf5833 | 2010-03-13 00:28:48 +0900 | [diff] [blame] | 53 | // 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] | 54 | if (dtor) |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 55 | AtExitManager::RegisterCallback(dtor, lazy_instance); |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 56 | } |
| 57 | |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 58 | } // namespace internal |
deanm@google.com | a6deb00 | 2008-09-08 23:11:13 +0900 | [diff] [blame] | 59 | } // namespace base |