blob: 54680655a2a639455cfebebc8c6c2edbbf742776 [file] [log] [blame]
[email protected]1b651d52011-05-16 15:01:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]30039e62008-09-08 14:11:132// 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]ce072a72010-12-31 20:02:169#include "base/threading/platform_thread.h"
[email protected]30039e62008-09-08 14:11:1310
11namespace base {
[email protected]6de0fd1d2011-11-15 13:31:4912namespace internal {
[email protected]30039e62008-09-08 14:11:1313
[email protected]6de0fd1d2011-11-15 13:31:4914// TODO(joth): This function could be shared with Singleton, in place of its
15// WaitForInstance() call.
16bool 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]c1aeaac2010-03-12 15:28:4824 // Caller must create instance
25 return true;
[email protected]1b651d52011-05-16 15:01:5426
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]6de0fd1d2011-11-15 13:31:4931 // CompleteLazyInstance().
32 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
[email protected]1b651d52011-05-16 15:01:5433 PlatformThread::YieldCurrentThread();
[email protected]6de0fd1d2011-11-15 13:31:4934 }
[email protected]c1aeaac2010-03-12 15:28:4835 // Someone else created the instance.
36 return false;
37}
38
[email protected]6de0fd1d2011-11-15 13:31:4939void CompleteLazyInstance(subtle::AtomicWord* state,
40 subtle::AtomicWord new_instance,
41 void* lazy_instance,
42 void (*dtor)(void*)) {
[email protected]c1aeaac2010-03-12 15:28:4843 // Instance is created, go from CREATING to CREATED.
[email protected]6de0fd1d2011-11-15 13:31:4944 // 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]c1aeaac2010-03-12 15:28:4847
[email protected]c1aeaac2010-03-12 15:28:4848 // Make sure that the lazily instantiated object will get destroyed at exit.
[email protected]dcc69332010-10-21 20:41:4749 if (dtor)
[email protected]6de0fd1d2011-11-15 13:31:4950 AtExitManager::RegisterCallback(dtor, lazy_instance);
[email protected]30039e62008-09-08 14:11:1351}
52
[email protected]6de0fd1d2011-11-15 13:31:4953} // namespace internal
[email protected]30039e62008-09-08 14:11:1354} // namespace base