1*635a8641SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/lazy_instance_helpers.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include "base/at_exit.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/atomicops.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
10*635a8641SAndroid Build Coastguard Worker
11*635a8641SAndroid Build Coastguard Worker namespace base {
12*635a8641SAndroid Build Coastguard Worker namespace internal {
13*635a8641SAndroid Build Coastguard Worker
NeedsLazyInstance(subtle::AtomicWord * state)14*635a8641SAndroid Build Coastguard Worker bool NeedsLazyInstance(subtle::AtomicWord* state) {
15*635a8641SAndroid Build Coastguard Worker // Try to create the instance, if we're the first, will go from 0 to
16*635a8641SAndroid Build Coastguard Worker // kLazyInstanceStateCreating, otherwise we've already been beaten here.
17*635a8641SAndroid Build Coastguard Worker // The memory access has no memory ordering as state 0 and
18*635a8641SAndroid Build Coastguard Worker // kLazyInstanceStateCreating have no associated data (memory barriers are
19*635a8641SAndroid Build Coastguard Worker // all about ordering of memory accesses to *associated* data).
20*635a8641SAndroid Build Coastguard Worker if (subtle::NoBarrier_CompareAndSwap(state, 0, kLazyInstanceStateCreating) ==
21*635a8641SAndroid Build Coastguard Worker 0) {
22*635a8641SAndroid Build Coastguard Worker // Caller must create instance
23*635a8641SAndroid Build Coastguard Worker return true;
24*635a8641SAndroid Build Coastguard Worker }
25*635a8641SAndroid Build Coastguard Worker
26*635a8641SAndroid Build Coastguard Worker // It's either in the process of being created, or already created. Spin.
27*635a8641SAndroid Build Coastguard Worker // The load has acquire memory ordering as a thread which sees
28*635a8641SAndroid Build Coastguard Worker // state_ == STATE_CREATED needs to acquire visibility over
29*635a8641SAndroid Build Coastguard Worker // the associated data (buf_). Pairing Release_Store is in
30*635a8641SAndroid Build Coastguard Worker // CompleteLazyInstance().
31*635a8641SAndroid Build Coastguard Worker if (subtle::Acquire_Load(state) == kLazyInstanceStateCreating) {
32*635a8641SAndroid Build Coastguard Worker const base::TimeTicks start = base::TimeTicks::Now();
33*635a8641SAndroid Build Coastguard Worker do {
34*635a8641SAndroid Build Coastguard Worker const base::TimeDelta elapsed = base::TimeTicks::Now() - start;
35*635a8641SAndroid Build Coastguard Worker // Spin with YieldCurrentThread for at most one ms - this ensures maximum
36*635a8641SAndroid Build Coastguard Worker // responsiveness. After that spin with Sleep(1ms) so that we don't burn
37*635a8641SAndroid Build Coastguard Worker // excessive CPU time - this also avoids infinite loops due to priority
38*635a8641SAndroid Build Coastguard Worker // inversions (https://crbug.com/797129).
39*635a8641SAndroid Build Coastguard Worker if (elapsed < TimeDelta::FromMilliseconds(1))
40*635a8641SAndroid Build Coastguard Worker PlatformThread::YieldCurrentThread();
41*635a8641SAndroid Build Coastguard Worker else
42*635a8641SAndroid Build Coastguard Worker PlatformThread::Sleep(TimeDelta::FromMilliseconds(1));
43*635a8641SAndroid Build Coastguard Worker } while (subtle::Acquire_Load(state) == kLazyInstanceStateCreating);
44*635a8641SAndroid Build Coastguard Worker }
45*635a8641SAndroid Build Coastguard Worker // Someone else created the instance.
46*635a8641SAndroid Build Coastguard Worker return false;
47*635a8641SAndroid Build Coastguard Worker }
48*635a8641SAndroid Build Coastguard Worker
CompleteLazyInstance(subtle::AtomicWord * state,subtle::AtomicWord new_instance,void (* destructor)(void *),void * destructor_arg)49*635a8641SAndroid Build Coastguard Worker void CompleteLazyInstance(subtle::AtomicWord* state,
50*635a8641SAndroid Build Coastguard Worker subtle::AtomicWord new_instance,
51*635a8641SAndroid Build Coastguard Worker void (*destructor)(void*),
52*635a8641SAndroid Build Coastguard Worker void* destructor_arg) {
53*635a8641SAndroid Build Coastguard Worker // Instance is created, go from CREATING to CREATED (or reset it if
54*635a8641SAndroid Build Coastguard Worker // |new_instance| is null). Releases visibility over |private_buf_| to
55*635a8641SAndroid Build Coastguard Worker // readers. Pairing Acquire_Load is in NeedsLazyInstance().
56*635a8641SAndroid Build Coastguard Worker subtle::Release_Store(state, new_instance);
57*635a8641SAndroid Build Coastguard Worker
58*635a8641SAndroid Build Coastguard Worker // Make sure that the lazily instantiated object will get destroyed at exit.
59*635a8641SAndroid Build Coastguard Worker if (new_instance && destructor)
60*635a8641SAndroid Build Coastguard Worker AtExitManager::RegisterCallback(destructor, destructor_arg);
61*635a8641SAndroid Build Coastguard Worker }
62*635a8641SAndroid Build Coastguard Worker
63*635a8641SAndroid Build Coastguard Worker } // namespace internal
64*635a8641SAndroid Build Coastguard Worker } // namespace base
65