blob: a81cb8c2d073cd331eb580488bbaf80329adbecc [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"
9#include "base/basictypes.h"
[email protected]ce072a72010-12-31 20:02:1610#include "base/threading/platform_thread.h"
[email protected]ee857512010-05-14 08:24:4211#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
[email protected]30039e62008-09-08 14:11:1312
13namespace base {
[email protected]6de0fd1d2011-11-15 13:31:4914namespace internal {
[email protected]30039e62008-09-08 14:11:1315
[email protected]6de0fd1d2011-11-15 13:31:4916// TODO(joth): This function could be shared with Singleton, in place of its
17// WaitForInstance() call.
18bool 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)
[email protected]c1aeaac2010-03-12 15:28:4826 // Caller must create instance
27 return true;
[email protected]1b651d52011-05-16 15:01:5428
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
[email protected]6de0fd1d2011-11-15 13:31:4933 // CompleteLazyInstance().
34 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
[email protected]1b651d52011-05-16 15:01:5435 PlatformThread::YieldCurrentThread();
[email protected]6de0fd1d2011-11-15 13:31:4936 }
[email protected]c1aeaac2010-03-12 15:28:4837 // Someone else created the instance.
38 return false;
39}
40
[email protected]6de0fd1d2011-11-15 13:31:4941void CompleteLazyInstance(subtle::AtomicWord* state,
42 subtle::AtomicWord new_instance,
43 void* lazy_instance,
44 void (*dtor)(void*)) {
[email protected]c1aeaac2010-03-12 15:28:4845 // See the comment to the corresponding HAPPENS_AFTER in Pointer().
[email protected]6de0fd1d2011-11-15 13:31:4946 ANNOTATE_HAPPENS_BEFORE(state);
[email protected]c1aeaac2010-03-12 15:28:4847
48 // Instance is created, go from CREATING to CREATED.
[email protected]6de0fd1d2011-11-15 13:31:4949 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's
50 // are in NeedsInstance() and Pointer().
51 subtle::Release_Store(state, new_instance);
[email protected]c1aeaac2010-03-12 15:28:4852
[email protected]c1aeaac2010-03-12 15:28:4853 // Make sure that the lazily instantiated object will get destroyed at exit.
[email protected]dcc69332010-10-21 20:41:4754 if (dtor)
[email protected]6de0fd1d2011-11-15 13:31:4955 AtExitManager::RegisterCallback(dtor, lazy_instance);
[email protected]30039e62008-09-08 14:11:1356}
57
[email protected]6de0fd1d2011-11-15 13:31:4958} // namespace internal
[email protected]30039e62008-09-08 14:11:1359} // namespace base