Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [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 | |
| 5 | #include "base/run_loop.h" |
| 6 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 7 | #include "base/cancelable_callback.h" |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 8 | #include "base/check.h" |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback.h" |
David Sanders | 97aac0de | 2022-02-07 14:33:53 | [diff] [blame] | 12 | #include "base/observer_list.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 13 | #include "base/task/single_thread_task_runner.h" |
Etienne Pierre-doray | fc7952f0 | 2025-06-06 00:04:33 | [diff] [blame] | 14 | #include "base/trace_event/trace_event.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 19 | namespace { |
| 20 | |
Peter Kasting | 4b18d0c | 2024-09-18 00:56:11 | [diff] [blame] | 21 | constinit thread_local RunLoop::Delegate* delegate = nullptr; |
| 22 | constinit thread_local const RunLoop::RunLoopTimeout* run_loop_timeout = |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 23 | nullptr; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 24 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 25 | // Runs |closure| immediately if this is called on |task_runner|, otherwise |
| 26 | // forwards |closure| to it. |
| 27 | void ProxyToTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner, |
| 28 | OnceClosure closure) { |
| 29 | if (task_runner->RunsTasksInCurrentSequence()) { |
| 30 | std::move(closure).Run(); |
| 31 | return; |
| 32 | } |
| 33 | task_runner->PostTask(FROM_HERE, std::move(closure)); |
| 34 | } |
| 35 | |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 36 | void OnRunLoopTimeout(RunLoop* run_loop, |
| 37 | const Location& location, |
| 38 | OnceCallback<void(const Location&)> on_timeout) { |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 39 | run_loop->Quit(); |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 40 | std::move(on_timeout).Run(location); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 41 | } |
| 42 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 43 | } // namespace |
| 44 | |
Gabriel Charette | 50518fc | 2018-05-22 17:53:22 | [diff] [blame] | 45 | RunLoop::Delegate::Delegate() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 46 | // The Delegate can be created on another thread. It is only bound in |
| 47 | // RegisterDelegateForCurrentThread(). |
| 48 | DETACH_FROM_THREAD(bound_thread_checker_); |
| 49 | } |
| 50 | |
| 51 | RunLoop::Delegate::~Delegate() { |
| 52 | DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 53 | DCHECK(active_run_loops_.empty()); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 54 | // A RunLoop::Delegate may be destroyed before it is bound, if so it may still |
| 55 | // be on its creation thread (e.g. a Thread that fails to start) and |
| 56 | // shouldn't disrupt that thread's state. |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 57 | if (bound_) { |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 58 | DCHECK_EQ(this, delegate); |
| 59 | delegate = nullptr; |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 60 | } |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 61 | } |
| 62 | |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 63 | bool RunLoop::Delegate::ShouldQuitWhenIdle() { |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 64 | const auto* top_loop = active_run_loops_.top().get(); |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 65 | if (top_loop->quit_when_idle_) { |
Bruce Dawson | 9066bfa | 2021-10-14 06:17:27 | [diff] [blame] | 66 | TRACE_EVENT_WITH_FLOW0("toplevel.flow", "RunLoop_ExitedOnIdle", |
| 67 | TRACE_ID_LOCAL(top_loop), TRACE_EVENT_FLAG_FLOW_IN); |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | return false; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 71 | } |
| 72 | |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 73 | // static |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 74 | void RunLoop::RegisterDelegateForCurrentThread(Delegate* new_delegate) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 75 | // Bind |delegate| to this thread. |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 76 | DCHECK(!new_delegate->bound_); |
| 77 | DCHECK_CALLED_ON_VALID_THREAD(new_delegate->bound_thread_checker_); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 78 | |
| 79 | // There can only be one RunLoop::Delegate per thread. |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 80 | DCHECK(!delegate) |
Gabriel Charette | fa5e7f0d | 2018-01-29 12:08:21 | [diff] [blame] | 81 | << "Error: Multiple RunLoop::Delegates registered on the same thread.\n\n" |
| 82 | "Hint: You perhaps instantiated a second " |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 83 | "MessageLoop/TaskEnvironment on a thread that already had one?"; |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 84 | delegate = new_delegate; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 85 | delegate->bound_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 86 | } |
| 87 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 88 | RunLoop::RunLoop(Type type) |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 89 | : delegate_(delegate), |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 90 | type_(type), |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 91 | origin_task_runner_(SingleThreadTaskRunner::GetCurrentDefault()) { |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 92 | DCHECK(delegate_) << "A RunLoop::Delegate must be bound to this thread prior " |
| 93 | "to using RunLoop."; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 94 | DCHECK(origin_task_runner_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 97 | RunLoop::~RunLoop() { |
Gabriel Charette | 78fd335 | 2019-08-01 23:24:15 | [diff] [blame] | 98 | // ~RunLoop() must happen-after the RunLoop is done running but it doesn't |
| 99 | // have to be on |sequence_checker_| (it usually is but sometimes it can be a |
| 100 | // member of a RefCountedThreadSafe object and be destroyed on another thread |
| 101 | // after being quit). |
| 102 | DCHECK(!running_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 103 | } |
| 104 | |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 105 | void RunLoop::Run(const Location& location) { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 106 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Alexander Timin | 26ac38f | 2021-10-05 00:50:36 | [diff] [blame] | 107 | // "test" tracing category is used here because in regular scenarios RunLoop |
| 108 | // trace events are not useful (each process normally has one RunLoop covering |
| 109 | // its entire lifetime) and might be confusing (they make idle processes look |
| 110 | // non-idle). In tests, however, creating a RunLoop is a frequent and an |
| 111 | // explicit action making this trace event very useful. |
| 112 | TRACE_EVENT("test", "RunLoop::Run", "location", location); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 113 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 114 | if (!BeforeRun()) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 115 | return; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 116 | } |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 117 | |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 118 | // If there is a RunLoopTimeout active then set the timeout. |
Alison Gale | 59c007a | 2024-04-20 03:05:40 | [diff] [blame] | 119 | // TODO(crbug.com/40602467): Use real-time for Run() timeouts so that they |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 120 | // can be applied even in tests which mock TimeTicks::Now(). |
| 121 | CancelableOnceClosure cancelable_timeout; |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 122 | const RunLoopTimeout* run_timeout = GetTimeoutForCurrentThread(); |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 123 | if (run_timeout) { |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 124 | cancelable_timeout.Reset(BindOnce(&OnRunLoopTimeout, Unretained(this), |
| 125 | location, run_timeout->on_timeout)); |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 126 | origin_task_runner_->PostDelayedTask( |
| 127 | FROM_HERE, cancelable_timeout.callback(), run_timeout->timeout); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 128 | } |
| 129 | |
Gabriel Charette | b030a4a | 2017-10-26 01:04:40 | [diff] [blame] | 130 | DCHECK_EQ(this, delegate_->active_run_loops_.top()); |
| 131 | const bool application_tasks_allowed = |
| 132 | delegate_->active_run_loops_.size() == 1U || |
| 133 | type_ == Type::kNestableTasksAllowed; |
Alex Clarke | cfde7b3 | 2019-08-15 17:11:47 | [diff] [blame] | 134 | delegate_->Run(application_tasks_allowed, TimeDelta::Max()); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 135 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 136 | AfterRun(); |
| 137 | } |
| 138 | |
| 139 | void RunLoop::RunUntilIdle() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 140 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 141 | |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 142 | quit_when_idle_ = true; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 143 | Run(); |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 144 | |
| 145 | if (!AnyQuitCalled()) { |
| 146 | quit_when_idle_ = false; |
| 147 | #if DCHECK_IS_ON() |
| 148 | run_allowed_ = true; |
| 149 | #endif |
| 150 | } |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void RunLoop::Quit() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 154 | // Thread-safe. |
| 155 | |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 156 | // This can only be hit if RunLoop::Quit() is called directly (QuitClosure() |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 157 | // proxies through ProxyToTaskRunner() as it can only deref its WeakPtr on |
| 158 | // |origin_task_runner_|). |
| 159 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 160 | origin_task_runner_->PostTask(FROM_HERE, |
| 161 | BindOnce(&RunLoop::Quit, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 162 | return; |
| 163 | } |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 164 | |
Bruce Dawson | 9066bfa | 2021-10-14 06:17:27 | [diff] [blame] | 165 | // While Quit() is an "OUT" call to reach one of the quit-states ("IN"), |
| 166 | // OUT|IN is used to visually link multiple Quit*() together which can help |
| 167 | // when debugging flaky tests. |
| 168 | TRACE_EVENT_WITH_FLOW0("toplevel.flow", "RunLoop::Quit", TRACE_ID_LOCAL(this), |
| 169 | TRACE_EVENT_FLAG_FLOW_OUT | TRACE_EVENT_FLAG_FLOW_IN); |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 170 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 171 | quit_called_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 172 | if (running_ && delegate_->active_run_loops_.top() == this) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 173 | // This is the inner-most RunLoop, so quit now. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 174 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 178 | void RunLoop::QuitWhenIdle() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 179 | // Thread-safe. |
| 180 | |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 181 | // This can only be hit if RunLoop::QuitWhenIdle() is called directly |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 182 | // (QuitWhenIdleClosure() proxies through ProxyToTaskRunner() as it can only |
| 183 | // deref its WeakPtr on |origin_task_runner_|). |
| 184 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
| 185 | origin_task_runner_->PostTask( |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 186 | FROM_HERE, BindOnce(&RunLoop::QuitWhenIdle, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Bruce Dawson | 9066bfa | 2021-10-14 06:17:27 | [diff] [blame] | 190 | // OUT|IN as in Quit() to link all Quit*() together should there be multiple. |
| 191 | TRACE_EVENT_WITH_FLOW0("toplevel.flow", "RunLoop::QuitWhenIdle", |
| 192 | TRACE_ID_LOCAL(this), |
| 193 | TRACE_EVENT_FLAG_FLOW_OUT | TRACE_EVENT_FLAG_FLOW_IN); |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 194 | |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 195 | quit_when_idle_ = true; |
| 196 | quit_when_idle_called_ = true; |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 197 | } |
| 198 | |
Peter Kasting | 0bc6a68 | 2024-09-09 09:38:34 | [diff] [blame] | 199 | RepeatingClosure RunLoop::QuitClosure() & { |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 200 | // Obtaining the QuitClosure() is not thread-safe; either obtain the |
| 201 | // QuitClosure() from the owning thread before Run() or invoke Quit() directly |
| 202 | // (which is thread-safe). |
Gabriel Charette | d913e5b | 2019-06-27 05:41:59 | [diff] [blame] | 203 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 204 | |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 205 | return BindRepeating( |
| 206 | &ProxyToTaskRunner, origin_task_runner_, |
| 207 | BindRepeating(&RunLoop::Quit, weak_factory_.GetWeakPtr())); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 208 | } |
| 209 | |
Peter Kasting | 0bc6a68 | 2024-09-09 09:38:34 | [diff] [blame] | 210 | RepeatingClosure RunLoop::QuitWhenIdleClosure() & { |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 211 | // Obtaining the QuitWhenIdleClosure() is not thread-safe; either obtain the |
| 212 | // QuitWhenIdleClosure() from the owning thread before Run() or invoke |
| 213 | // QuitWhenIdle() directly (which is thread-safe). |
Gabriel Charette | d913e5b | 2019-06-27 05:41:59 | [diff] [blame] | 214 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 215 | |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 216 | return BindRepeating( |
| 217 | &ProxyToTaskRunner, origin_task_runner_, |
| 218 | BindRepeating(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr())); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 219 | } |
| 220 | |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 221 | bool RunLoop::AnyQuitCalled() { |
| 222 | return quit_called_ || quit_when_idle_called_; |
| 223 | } |
| 224 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 225 | // static |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 226 | bool RunLoop::IsRunningOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 227 | return delegate && !delegate->active_run_loops_.empty(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // static |
| 231 | bool RunLoop::IsNestedOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 232 | return delegate && delegate->active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // static |
| 236 | void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 237 | DCHECK(delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 238 | delegate->nesting_observers_.AddObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // static |
| 242 | void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 243 | DCHECK(delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 244 | delegate->nesting_observers_.RemoveObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 245 | } |
| 246 | |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 247 | #if DCHECK_IS_ON() |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 248 | ScopedDisallowRunningRunLoop::ScopedDisallowRunningRunLoop() |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 249 | : current_delegate_(delegate), |
| 250 | previous_run_allowance_(current_delegate_ && |
| 251 | current_delegate_->allow_running_for_testing_) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 252 | if (current_delegate_) { |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 253 | current_delegate_->allow_running_for_testing_ = false; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 254 | } |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 255 | } |
| 256 | |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 257 | ScopedDisallowRunningRunLoop::~ScopedDisallowRunningRunLoop() { |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 258 | DCHECK_EQ(current_delegate_, delegate); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 259 | if (current_delegate_) { |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 260 | current_delegate_->allow_running_for_testing_ = previous_run_allowance_; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 261 | } |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 262 | } |
| 263 | #else // DCHECK_IS_ON() |
| 264 | // Defined out of line so that the compiler doesn't inline these and realize |
| 265 | // the scope has no effect and then throws an "unused variable" warning in |
| 266 | // non-dcheck builds. |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 267 | ScopedDisallowRunningRunLoop::ScopedDisallowRunningRunLoop() = default; |
| 268 | ScopedDisallowRunningRunLoop::~ScopedDisallowRunningRunLoop() = default; |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 269 | #endif // DCHECK_IS_ON() |
| 270 | |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 271 | RunLoop::RunLoopTimeout::RunLoopTimeout() = default; |
| 272 | |
| 273 | RunLoop::RunLoopTimeout::~RunLoopTimeout() = default; |
| 274 | |
| 275 | // static |
| 276 | void RunLoop::SetTimeoutForCurrentThread(const RunLoopTimeout* timeout) { |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 277 | run_loop_timeout = timeout; |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // static |
| 281 | const RunLoop::RunLoopTimeout* RunLoop::GetTimeoutForCurrentThread() { |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 282 | // Workaround false-positive MSAN use-of-uninitialized-value on |
| 283 | // thread_local storage for loaded libraries: |
| 284 | // https://github.com/google/sanitizers/issues/1265 |
| 285 | MSAN_UNPOISON(&run_loop_timeout, sizeof(RunLoopTimeout*)); |
| 286 | |
| 287 | return run_loop_timeout; |
Wez | 212eeb7 | 2020-02-04 17:54:54 | [diff] [blame] | 288 | } |
| 289 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 290 | bool RunLoop::BeforeRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 291 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 292 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 293 | #if DCHECK_IS_ON() |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 294 | DCHECK(delegate_->allow_running_for_testing_) |
| 295 | << "RunLoop::Run() isn't allowed in the scope of a " |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 296 | "ScopedDisallowRunningRunLoop. Hint: if mixing " |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 297 | "TestMockTimeTaskRunners on same thread, use TestMockTimeTaskRunner's " |
| 298 | "API instead of RunLoop to drive individual task runners."; |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 299 | DCHECK(run_allowed_); |
| 300 | run_allowed_ = false; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 301 | #endif // DCHECK_IS_ON() |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 302 | |
| 303 | // Allow Quit to be called before Run. |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 304 | if (quit_called_) { |
Bruce Dawson | 9066bfa | 2021-10-14 06:17:27 | [diff] [blame] | 305 | TRACE_EVENT_WITH_FLOW0("toplevel.flow", "RunLoop_ExitedEarly", |
| 306 | TRACE_ID_LOCAL(this), TRACE_EVENT_FLAG_FLOW_IN); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 307 | return false; |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 308 | } |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 309 | |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 310 | auto& active_run_loops = delegate_->active_run_loops_; |
| 311 | active_run_loops.push(this); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 312 | |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 313 | const bool is_nested = active_run_loops.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 314 | |
| 315 | if (is_nested) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 316 | for (auto& observer : delegate_->nesting_observers_) { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 317 | observer.OnBeginNestedRunLoop(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 318 | } |
| 319 | if (type_ == Type::kNestableTasksAllowed) { |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 320 | delegate_->EnsureWorkScheduled(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 321 | } |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 322 | } |
jamescook | aacdfd0 | 2016-04-28 00:50:03 | [diff] [blame] | 323 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 324 | running_ = true; |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | void RunLoop::AfterRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 329 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 330 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 331 | running_ = false; |
| 332 | |
Bruce Dawson | 9066bfa | 2021-10-14 06:17:27 | [diff] [blame] | 333 | TRACE_EVENT_WITH_FLOW0("toplevel.flow", "RunLoop_Exited", |
| 334 | TRACE_ID_LOCAL(this), TRACE_EVENT_FLAG_FLOW_IN); |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 335 | |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 336 | auto& active_run_loops = delegate_->active_run_loops_; |
| 337 | DCHECK_EQ(active_run_loops.top(), this); |
| 338 | active_run_loops.pop(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 339 | |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 340 | // Exiting a nested RunLoop? |
| 341 | if (!active_run_loops.empty()) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 342 | for (auto& observer : delegate_->nesting_observers_) { |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 343 | observer.OnExitNestedRunLoop(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 344 | } |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 345 | |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 346 | // Execute deferred Quit, if any: |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 347 | if (active_run_loops.top()->quit_called_) { |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 348 | delegate_->Quit(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 349 | } |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 350 | } |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | } // namespace base |