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