blob: 4d206dd091828849194ab18ac3e2140d52a2f061 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
[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
Peter Kasting811504a72025-01-09 03:18:5012namespace base::internal {
[email protected]30039e62008-09-08 14:11:1313
Benoit Lizeb82171a62022-06-17 13:37:5814bool NeedsLazyInstance(std::atomic<uintptr_t>& state) {
[email protected]6de0fd1d2011-11-15 13:31:4915 // Try to create the instance, if we're the first, will go from 0 to
16 // kLazyInstanceStateCreating, otherwise we've already been beaten here.
17 // The memory access has no memory ordering as state 0 and
18 // kLazyInstanceStateCreating have no associated data (memory barriers are
19 // all about ordering of memory accesses to *associated* data).
Benoit Lizeb82171a62022-06-17 13:37:5820 uintptr_t expected = 0;
21 if (state.compare_exchange_strong(expected, kLazyInstanceStateCreating,
22 std::memory_order_relaxed,
23 std::memory_order_relaxed)) {
[email protected]c1aeaac2010-03-12 15:28:4824 // Caller must create instance
25 return true;
Gabriel Charettef297f012018-01-17 20:59:0726 }
[email protected]1b651d52011-05-16 15:01:5427
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
[email protected]6de0fd1d2011-11-15 13:31:4932 // CompleteLazyInstance().
Benoit Lizeb82171a62022-06-17 13:37:5833 if (state.load(std::memory_order_acquire) == kLazyInstanceStateCreating) {
Francois Doray94a0386f2018-01-23 14:18:4634 const base::TimeTicks start = base::TimeTicks::Now();
Bruce Dawsoncdf5fae2018-01-05 22:12:4935 do {
Francois Doray94a0386f2018-01-23 14:18:4636 const base::TimeDelta elapsed = base::TimeTicks::Now() - start;
Benoit Lizeb82171a62022-06-17 13:37:5837 // Spin with YieldCurrentThread for at most one ms - this ensures
38 // maximum responsiveness. After that spin with Sleep(1ms) so that we
39 // don't burn excessive CPU time - this also avoids infinite loops due
40 // to priority inversions (https://crbug.com/797129).
Peter Kasting134ef9af2024-12-28 02:30:0941 if (elapsed < Milliseconds(1)) {
Bruce Dawsoncdf5fae2018-01-05 22:12:4942 PlatformThread::YieldCurrentThread();
Peter Kasting134ef9af2024-12-28 02:30:0943 } else {
Peter Kasting53fd6ee2021-10-05 20:40:4844 PlatformThread::Sleep(Milliseconds(1));
Peter Kasting134ef9af2024-12-28 02:30:0945 }
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.
Peter Kasting134ef9af2024-12-28 02:30:0963 if (new_instance && destructor) {
Francois Doraya50035b2017-06-12 17:55:5064 AtExitManager::RegisterCallback(destructor, destructor_arg);
Peter Kasting134ef9af2024-12-28 02:30:0965 }
[email protected]30039e62008-09-08 14:11:1366}
67
Peter Kasting811504a72025-01-09 03:18:5068} // namespace base::internal