blob: fcb00fd335fd8b48a3a03303040309549aed72c9 [file] [log] [blame]
Gabriel Charettef297f012018-01-17 20:59:071// Copyright 2018 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
Gabriel Charettef297f012018-01-17 20:59:075#include "base/lazy_instance_helpers.h"
[email protected]30039e62008-09-08 14:11:136
Benoit Lizeb82171a62022-06-17 13:37:587#include <atomic>
8
[email protected]30039e62008-09-08 14:11:139#include "base/at_exit.h"
[email protected]ce072a72010-12-31 20:02:1610#include "base/threading/platform_thread.h"
[email protected]30039e62008-09-08 14:11:1311
12namespace base {
[email protected]6de0fd1d2011-11-15 13:31:4913namespace internal {
[email protected]30039e62008-09-08 14:11:1314
Benoit Lizeb82171a62022-06-17 13:37:5815bool NeedsLazyInstance(std::atomic<uintptr_t>& state) {
[email protected]6de0fd1d2011-11-15 13:31:4916 // Try to create the instance, if we're the first, will go from 0 to
17 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
18 // The memory access has no memory ordering as state 0 and
19 // kLazyInstanceStateCreating have no associated data (memory barriers are
20 // all about ordering of memory accesses to *associated* data).
Benoit Lizeb82171a62022-06-17 13:37:5821 uintptr_t expected = 0;
22 if (state.compare_exchange_strong(expected, kLazyInstanceStateCreating,
23 std::memory_order_relaxed,
24 std::memory_order_relaxed)) {
[email protected]c1aeaac2010-03-12 15:28:4825 // Caller must create instance
26 return true;
Gabriel Charettef297f012018-01-17 20:59:0727 }
[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().
Benoit Lizeb82171a62022-06-17 13:37:5834 if (state.load(std::memory_order_acquire) == kLazyInstanceStateCreating) {
Francois Doray94a0386f2018-01-23 14:18:4635 const base::TimeTicks start = base::TimeTicks::Now();
Bruce Dawsoncdf5fae2018-01-05 22:12:4936 do {
Francois Doray94a0386f2018-01-23 14:18:4637 const base::TimeDelta elapsed = base::TimeTicks::Now() - start;
Benoit Lizeb82171a62022-06-17 13:37:5838 // Spin with YieldCurrentThread for at most one ms - this ensures
39 // maximum responsiveness. After that spin with Sleep(1ms) so that we
40 // don't burn excessive CPU time - this also avoids infinite loops due
41 // to priority inversions (https://crbug.com/797129).
Peter Kasting53fd6ee2021-10-05 20:40:4842 if (elapsed < Milliseconds(1))
Bruce Dawsoncdf5fae2018-01-05 22:12:4943 PlatformThread::YieldCurrentThread();
44 else
Peter Kasting53fd6ee2021-10-05 20:40:4845 PlatformThread::Sleep(Milliseconds(1));
Benoit Lizeb82171a62022-06-17 13:37:5846 } while (state.load(std::memory_order_acquire) ==
47 kLazyInstanceStateCreating);
[email protected]6de0fd1d2011-11-15 13:31:4948 }
[email protected]c1aeaac2010-03-12 15:28:4849 // Someone else created the instance.
50 return false;
51}
52
Benoit Lizeb82171a62022-06-17 13:37:5853void CompleteLazyInstance(std::atomic<uintptr_t>& state,
54 uintptr_t new_instance,
Francois Doraya50035b2017-06-12 17:55:5055 void (*destructor)(void*),
56 void* destructor_arg) {
Gabriel Charettef297f012018-01-17 20:59:0757 // Instance is created, go from CREATING to CREATED (or reset it if
58 // |new_instance| is null). Releases visibility over |private_buf_| to
59 // readers. Pairing Acquire_Load is in NeedsLazyInstance().
Benoit Lizeb82171a62022-06-17 13:37:5860 state.store(new_instance, std::memory_order_release);
[email protected]c1aeaac2010-03-12 15:28:4861
[email protected]c1aeaac2010-03-12 15:28:4862 // Make sure that the lazily instantiated object will get destroyed at exit.
Gabriel Charettef297f012018-01-17 20:59:0763 if (new_instance && destructor)
Francois Doraya50035b2017-06-12 17:55:5064 AtExitManager::RegisterCallback(destructor, destructor_arg);
[email protected]30039e62008-09-08 14:11:1365}
66
[email protected]6de0fd1d2011-11-15 13:31:4967} // namespace internal
[email protected]30039e62008-09-08 14:11:1368} // namespace base