[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [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" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 9 | #include "base/threading/platform_thread.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 10 | |
| 11 | namespace base { |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 12 | namespace internal { |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 13 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 14 | // TODO(joth): This function could be shared with Singleton, in place of its |
| 15 | // WaitForInstance() call. |
| 16 | bool NeedsLazyInstance(subtle::AtomicWord* state) { |
| 17 | // Try to create the instance, if we're the first, will go from 0 to |
| 18 | // kLazyInstanceStateCreating, otherwise we've already been beaten here. |
| 19 | // The memory access has no memory ordering as state 0 and |
| 20 | // kLazyInstanceStateCreating have no associated data (memory barriers are |
| 21 | // all about ordering of memory accesses to *associated* data). |
| 22 | if (subtle::NoBarrier_CompareAndSwap(state, 0, |
| 23 | kLazyInstanceStateCreating) == 0) |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 24 | // Caller must create instance |
| 25 | return true; |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 26 | |
| 27 | // It's either in the process of being created, or already created. Spin. |
| 28 | // The load has acquire memory ordering as a thread which sees |
| 29 | // state_ == STATE_CREATED needs to acquire visibility over |
| 30 | // the associated data (buf_). Pairing Release_Store is in |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 31 | // CompleteLazyInstance(). |
| 32 | while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) { |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 33 | PlatformThread::YieldCurrentThread(); |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 34 | } |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 35 | // Someone else created the instance. |
| 36 | return false; |
| 37 | } |
| 38 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 39 | void CompleteLazyInstance(subtle::AtomicWord* state, |
| 40 | subtle::AtomicWord new_instance, |
| 41 | void* lazy_instance, |
| 42 | void (*dtor)(void*)) { |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 43 | // Instance is created, go from CREATING to CREATED. |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 44 | // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's |
| 45 | // are in NeedsInstance() and Pointer(). |
| 46 | subtle::Release_Store(state, new_instance); |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 47 | |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 48 | // Make sure that the lazily instantiated object will get destroyed at exit. |
[email protected] | dcc6933 | 2010-10-21 20:41:47 | [diff] [blame] | 49 | if (dtor) |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 50 | AtExitManager::RegisterCallback(dtor, lazy_instance); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 53 | } // namespace internal |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 54 | } // namespace base |