Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
deanm@google.com | 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" |
deanm@google.com | 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 | |
deanm@google.com | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 9 | #include"base/at_exit.h" |
brettw@chromium.org | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 10 | #include"base/threading/platform_thread.h" |
deanm@google.com | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 11 | |
Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 12 | namespace base::internal{ |
deanm@google.com | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 13 | |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 14 | boolNeedsLazyInstance(std::atomic<uintptr_t>& state){ |
joth@chromium.org | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 15 | // 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 Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 20 | uintptr_t expected=0; |
| 21 | if(state.compare_exchange_strong(expected, kLazyInstanceStateCreating, |
| 22 | std::memory_order_relaxed, |
| 23 | std::memory_order_relaxed)){ |
craig.schlenter@chromium.org | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 24 | // Caller must create instance |
| 25 | returntrue; |
Gabriel Charette | f297f01 | 2018-01-17 20:59:07 | [diff] [blame] | 26 | } |
dvyukov@google.com | 1b651d5 | 2011-05-16 15:01:54 | [diff] [blame] | 27 | |
| 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 |
joth@chromium.org | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 32 | // CompleteLazyInstance(). |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 33 | if(state.load(std::memory_order_acquire)== kLazyInstanceStateCreating){ |
Francois Doray | 94a0386f | 2018-01-23 14:18:46 | [diff] [blame] | 34 | const base::TimeTicks start= base::TimeTicks::Now(); |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 35 | do{ |
Francois Doray | 94a0386f | 2018-01-23 14:18:46 | [diff] [blame] | 36 | const base::TimeDelta elapsed= base::TimeTicks::Now()- start; |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 37 | // 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 Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 41 | if(elapsed<Milliseconds(1)){ |
Bruce Dawson | cdf5fae | 2018-01-05 22:12:49 | [diff] [blame] | 42 | PlatformThread::YieldCurrentThread(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 43 | }else{ |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 44 | PlatformThread::Sleep(Milliseconds(1)); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 45 | } |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 46 | }while(state.load(std::memory_order_acquire)== |
| 47 | kLazyInstanceStateCreating); |
joth@chromium.org | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 48 | } |
craig.schlenter@chromium.org | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 49 | // Someone else created the instance. |
| 50 | returnfalse; |
| 51 | } |
| 52 | |
Benoit Lize | b82171a6 | 2022-06-17 13:37:58 | [diff] [blame] | 53 | voidCompleteLazyInstance(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); |
craig.schlenter@chromium.org | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 61 | |
craig.schlenter@chromium.org | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 62 | // Make sure that the lazily instantiated object will get destroyed at exit. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 63 | if(new_instance&& destructor){ |
Francois Doray | a50035b | 2017-06-12 17:55:50 | [diff] [blame] | 64 | AtExitManager::RegisterCallback(destructor, destructor_arg); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 65 | } |
deanm@google.com | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 66 | } |
| 67 | |
Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 68 | }// namespace base::internal |