blob: 594c1fee4ffef91b5f4f8358de280412f78562a5 [file] [log] [blame]
dvyukov@google.com6beeb832011-05-17 00:01:54 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
deanm@google.coma6deb002008-09-08 23:11:13 +09002// 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.org61391822011-01-01 05:02:16 +090010#include "base/threading/platform_thread.h"
deanm@google.coma6deb002008-09-08 23:11:13 +090011
12namespace base {
joth@chromium.orgb24883c2011-11-15 22:31:49 +090013namespace internal {
deanm@google.coma6deb002008-09-08 23:11:13 +090014
joth@chromium.orgb24883c2011-11-15 22:31:49 +090015// TODO(joth): This function could be shared with Singleton, in place of its
16// WaitForInstance() call.
17bool NeedsLazyInstance(subtle::AtomicWord* state) {
18 // Try to create the instance, if we're the first, will go from 0 to
19 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
20 // The memory access has no memory ordering as state 0 and
21 // kLazyInstanceStateCreating have no associated data (memory barriers are
22 // all about ordering of memory accesses to *associated* data).
23 if (subtle::NoBarrier_CompareAndSwap(state, 0,
24 kLazyInstanceStateCreating) == 0)
craig.schlenter@chromium.org0cf58332010-03-13 00:28:48 +090025 // Caller must create instance
26 return true;
dvyukov@google.com6beeb832011-05-17 00:01:54 +090027
28 // It's either in the process of being created, or already created. Spin.
29 // The load has acquire memory ordering as a thread which sees
30 // state_ == STATE_CREATED needs to acquire visibility over
31 // the associated data (buf_). Pairing Release_Store is in
joth@chromium.orgb24883c2011-11-15 22:31:49 +090032 // CompleteLazyInstance().
33 while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
dvyukov@google.com6beeb832011-05-17 00:01:54 +090034 PlatformThread::YieldCurrentThread();
joth@chromium.orgb24883c2011-11-15 22:31:49 +090035 }
craig.schlenter@chromium.org0cf58332010-03-13 00:28:48 +090036 // Someone else created the instance.
37 return false;
38}
39
joth@chromium.orgb24883c2011-11-15 22:31:49 +090040void CompleteLazyInstance(subtle::AtomicWord* state,
41 subtle::AtomicWord new_instance,
42 void* lazy_instance,
43 void (*dtor)(void*)) {
craig.schlenter@chromium.org0cf58332010-03-13 00:28:48 +090044 // Instance is created, go from CREATING to CREATED.
joth@chromium.orgb24883c2011-11-15 22:31:49 +090045 // Releases visibility over private_buf_ to readers. Pairing Acquire_Load's
46 // are in NeedsInstance() and Pointer().
47 subtle::Release_Store(state, new_instance);
craig.schlenter@chromium.org0cf58332010-03-13 00:28:48 +090048
craig.schlenter@chromium.org0cf58332010-03-13 00:28:48 +090049 // Make sure that the lazily instantiated object will get destroyed at exit.
evan@chromium.orga04f70c2010-10-22 05:41:47 +090050 if (dtor)
joth@chromium.orgb24883c2011-11-15 22:31:49 +090051 AtExitManager::RegisterCallback(dtor, lazy_instance);
deanm@google.coma6deb002008-09-08 23:11:13 +090052}
53
joth@chromium.orgb24883c2011-11-15 22:31:49 +090054} // namespace internal
deanm@google.coma6deb002008-09-08 23:11:13 +090055} // namespace base