Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 5 | #include "base/lazy_instance_helpers.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 6 | |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 7 | #include <atomic> |
| 8 | |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 9 | #include "base/at_exit.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 10 | #include "base/threading/platform_thread.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 11 | |
| 12 | namespace base { |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 13 | namespace internal { |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 14 | |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 15 | bool NeedsLazyInstance(std::atomic<uintptr_t>& state) { |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 16 | // 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 Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 21 | uintptr_t expected = 0; |
| 22 | if (state.compare_exchange_strong(expected, kLazyInstanceStateCreating, |
| 23 | std::memory_order_relaxed, |
| 24 | std::memory_order_relaxed)) { |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 25 | // Caller must create instance |
| 26 | return true; |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 27 | } |
[email protected] | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 28 | |
| 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] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 33 | // CompleteLazyInstance(). |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 34 | if (state.load(std::memory_order_acquire) == kLazyInstanceStateCreating) { |
Francois Doray | 94a0386f | 2018-01-23 14:18:46 | [diff] [blame] | 35 | const base::TimeTicks start = base::TimeTicks::Now(); |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 36 | do { |
Francois Doray | 94a0386f | 2018-01-23 14:18:46 | [diff] [blame] | 37 | const base::TimeDelta elapsed = base::TimeTicks::Now() - start; |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 38 | // 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 Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 42 | if (elapsed < Milliseconds(1)) |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 43 | PlatformThread::YieldCurrentThread(); |
| 44 | else |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 45 | PlatformThread::Sleep(Milliseconds(1)); |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 46 | } while (state.load(std::memory_order_acquire) == |
| 47 | kLazyInstanceStateCreating); |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 48 | } |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 49 | // Someone else created the instance. |
| 50 | return false; |
| 51 | } |
| 52 | |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 53 | void CompleteLazyInstance(std::atomic<uintptr_t>& state, |
| 54 | uintptr_t new_instance, |
Francois Doray | a50035b | 2017-06-12 17:55:50 | [diff] [blame] | 55 | void (*destructor)(void*), |
| 56 | void* destructor_arg) { |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 57 | // 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 Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 60 | state.store(new_instance, std::memory_order_release); |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 61 | |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 62 | // Make sure that the lazily instantiated object will get destroyed at exit. |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 63 | if (new_instance && destructor) |
Francois Doray | a50035b | 2017-06-12 17:55:50 | [diff] [blame] | 64 | AtExitManager::RegisterCallback(destructor, destructor_arg); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 67 | } // namespace internal |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 68 | } // namespace base |