[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #include "base/bind.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 8 | #include "base/callback.h" |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 9 | #include "base/cancelable_callback.h" |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 12 | #include "base/no_destructor.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 14 | #include "base/threading/thread_local.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 15 | #include "base/threading/thread_task_runner_handle.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 20 | namespace { |
| 21 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 22 | LazyInstance<ThreadLocalPointer<RunLoop::Delegate>>::Leaky tls_delegate = |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 23 | LAZY_INSTANCE_INITIALIZER; |
| 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 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 36 | ThreadLocalPointer<RunLoop::ScopedRunTimeoutForTest>* |
| 37 | ScopedRunTimeoutForTestTLS() { |
| 38 | static NoDestructor<ThreadLocalPointer<RunLoop::ScopedRunTimeoutForTest>> tls; |
| 39 | return tls.get(); |
| 40 | } |
| 41 | |
| 42 | void OnRunTimeout(RunLoop* run_loop, RepeatingClosure on_timeout) { |
| 43 | run_loop->Quit(); |
| 44 | if (on_timeout) |
| 45 | on_timeout.Run(); |
| 46 | } |
| 47 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 48 | } // namespace |
| 49 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 50 | RunLoop::ScopedRunTimeoutForTest::ScopedRunTimeoutForTest(TimeDelta timeout) |
| 51 | : ScopedRunTimeoutForTest(timeout, RepeatingClosure()) {} |
| 52 | |
| 53 | RunLoop::ScopedRunTimeoutForTest::ScopedRunTimeoutForTest( |
| 54 | TimeDelta timeout, |
| 55 | RepeatingClosure on_timeout) |
| 56 | : timeout_(timeout), |
| 57 | on_timeout_(std::move(on_timeout)), |
| 58 | nested_timeout_(ScopedRunTimeoutForTestTLS()->Get()) { |
| 59 | ScopedRunTimeoutForTestTLS()->Set(this); |
| 60 | } |
| 61 | |
| 62 | RunLoop::ScopedRunTimeoutForTest::~ScopedRunTimeoutForTest() { |
| 63 | ScopedRunTimeoutForTestTLS()->Set(nested_timeout_); |
| 64 | } |
| 65 | |
| 66 | // static |
| 67 | const RunLoop::ScopedRunTimeoutForTest* |
| 68 | RunLoop::ScopedRunTimeoutForTest::Current() { |
| 69 | return ScopedRunTimeoutForTestTLS()->Get(); |
| 70 | } |
| 71 | |
Gabriel Charette | 50518fc | 2018-05-22 17:53:22 | [diff] [blame] | 72 | RunLoop::Delegate::Delegate() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 73 | // The Delegate can be created on another thread. It is only bound in |
| 74 | // RegisterDelegateForCurrentThread(). |
| 75 | DETACH_FROM_THREAD(bound_thread_checker_); |
| 76 | } |
| 77 | |
| 78 | RunLoop::Delegate::~Delegate() { |
| 79 | DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); |
| 80 | // A RunLoop::Delegate may be destroyed before it is bound, if so it may still |
| 81 | // be on its creation thread (e.g. a Thread that fails to start) and |
| 82 | // shouldn't disrupt that thread's state. |
| 83 | if (bound_) |
| 84 | tls_delegate.Get().Set(nullptr); |
| 85 | } |
| 86 | |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 87 | bool RunLoop::Delegate::ShouldQuitWhenIdle() { |
Gabriel Charette | 50518fc | 2018-05-22 17:53:22 | [diff] [blame] | 88 | return active_run_loops_.top()->quit_when_idle_received_; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 89 | } |
| 90 | |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 91 | // static |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 92 | void RunLoop::RegisterDelegateForCurrentThread(Delegate* delegate) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 93 | // Bind |delegate| to this thread. |
| 94 | DCHECK(!delegate->bound_); |
| 95 | DCHECK_CALLED_ON_VALID_THREAD(delegate->bound_thread_checker_); |
| 96 | |
| 97 | // There can only be one RunLoop::Delegate per thread. |
Gabriel Charette | fa5e7f0d | 2018-01-29 12:08:21 | [diff] [blame] | 98 | DCHECK(!tls_delegate.Get().Get()) |
| 99 | << "Error: Multiple RunLoop::Delegates registered on the same thread.\n\n" |
| 100 | "Hint: You perhaps instantiated a second " |
| 101 | "MessageLoop/ScopedTaskEnvironment on a thread that already had one?"; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 102 | tls_delegate.Get().Set(delegate); |
| 103 | delegate->bound_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 104 | } |
| 105 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 106 | RunLoop::RunLoop(Type type) |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 107 | : delegate_(tls_delegate.Get().Get()), |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 108 | type_(type), |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 109 | origin_task_runner_(ThreadTaskRunnerHandle::Get()), |
| 110 | weak_factory_(this) { |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 111 | DCHECK(delegate_) << "A RunLoop::Delegate must be bound to this thread prior " |
| 112 | "to using RunLoop."; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 113 | DCHECK(origin_task_runner_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 116 | RunLoop::~RunLoop() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 117 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 118 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void RunLoop::Run() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 122 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 123 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 124 | if (!BeforeRun()) |
| 125 | return; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 126 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 127 | // If there is a ScopedRunTimeoutForTest active then set the timeout. |
| 128 | // TODO(crbug.com/905412): Use real-time for Run() timeouts so that they |
| 129 | // can be applied even in tests which mock TimeTicks::Now(). |
| 130 | CancelableOnceClosure cancelable_timeout; |
| 131 | ScopedRunTimeoutForTest* run_timeout = ScopedRunTimeoutForTestTLS()->Get(); |
| 132 | if (run_timeout && !run_timeout->timeout().is_zero()) { |
| 133 | cancelable_timeout.Reset( |
| 134 | BindOnce(&OnRunTimeout, Unretained(this), run_timeout->on_timeout())); |
| 135 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 136 | FROM_HERE, cancelable_timeout.callback(), run_timeout->timeout()); |
| 137 | } |
| 138 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 139 | // It is okay to access this RunLoop from another sequence while Run() is |
| 140 | // active as this RunLoop won't touch its state until after that returns (if |
| 141 | // the RunLoop's state is accessed while processing Run(), it will be re-bound |
| 142 | // to the accessing sequence for the remainder of that Run() -- accessing from |
| 143 | // multiple sequences is still disallowed). |
| 144 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 145 | |
Gabriel Charette | b030a4a | 2017-10-26 01:04:40 | [diff] [blame] | 146 | DCHECK_EQ(this, delegate_->active_run_loops_.top()); |
| 147 | const bool application_tasks_allowed = |
| 148 | delegate_->active_run_loops_.size() == 1U || |
| 149 | type_ == Type::kNestableTasksAllowed; |
| 150 | delegate_->Run(application_tasks_allowed); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 151 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 152 | // Rebind this RunLoop to the current thread after Run(). |
| 153 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 154 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 155 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 156 | AfterRun(); |
| 157 | } |
| 158 | |
| 159 | void RunLoop::RunUntilIdle() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 160 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 161 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 162 | quit_when_idle_received_ = true; |
| 163 | Run(); |
| 164 | } |
| 165 | |
| 166 | void RunLoop::Quit() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 167 | // Thread-safe. |
| 168 | |
| 169 | // This can only be hit if run_loop->Quit() is called directly (QuitClosure() |
| 170 | // proxies through ProxyToTaskRunner() as it can only deref its WeakPtr on |
| 171 | // |origin_task_runner_|). |
| 172 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 173 | origin_task_runner_->PostTask(FROM_HERE, |
| 174 | BindOnce(&RunLoop::Quit, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 175 | return; |
| 176 | } |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 177 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 178 | quit_called_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 179 | if (running_ && delegate_->active_run_loops_.top() == this) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 180 | // This is the inner-most RunLoop, so quit now. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 181 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 185 | void RunLoop::QuitWhenIdle() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 186 | // Thread-safe. |
| 187 | |
| 188 | // This can only be hit if run_loop->QuitWhenIdle() is called directly |
| 189 | // (QuitWhenIdleClosure() proxies through ProxyToTaskRunner() as it can only |
| 190 | // deref its WeakPtr on |origin_task_runner_|). |
| 191 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
| 192 | origin_task_runner_->PostTask( |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 193 | FROM_HERE, BindOnce(&RunLoop::QuitWhenIdle, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 197 | quit_when_idle_received_ = true; |
| 198 | } |
| 199 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 200 | Closure RunLoop::QuitClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 201 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 202 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 203 | allow_quit_current_deprecated_ = false; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 204 | |
| 205 | // Need to use ProxyToTaskRunner() as WeakPtrs vended from |
| 206 | // |weak_factory_| may only be accessed on |origin_task_runner_|. |
| 207 | // TODO(gab): It feels wrong that QuitClosure() is bound to a WeakPtr. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 208 | return Bind(&ProxyToTaskRunner, origin_task_runner_, |
| 209 | Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr())); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 210 | } |
| 211 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 212 | Closure RunLoop::QuitWhenIdleClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 213 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 214 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 215 | allow_quit_current_deprecated_ = false; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 216 | |
| 217 | // Need to use ProxyToTaskRunner() as WeakPtrs vended from |
| 218 | // |weak_factory_| may only be accessed on |origin_task_runner_|. |
| 219 | // TODO(gab): It feels wrong that QuitWhenIdleClosure() is bound to a WeakPtr. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame^] | 220 | return Bind(&ProxyToTaskRunner, origin_task_runner_, |
| 221 | Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr())); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 222 | } |
| 223 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 224 | // static |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 225 | bool RunLoop::IsRunningOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 226 | Delegate* delegate = tls_delegate.Get().Get(); |
| 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 | Delegate* delegate = tls_delegate.Get().Get(); |
| 233 | return delegate && delegate->active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // static |
| 237 | void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 238 | Delegate* delegate = tls_delegate.Get().Get(); |
| 239 | DCHECK(delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 240 | delegate->nesting_observers_.AddObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // static |
| 244 | void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 245 | Delegate* delegate = tls_delegate.Get().Get(); |
| 246 | DCHECK(delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 247 | delegate->nesting_observers_.RemoveObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // static |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 251 | void RunLoop::QuitCurrentDeprecated() { |
| 252 | DCHECK(IsRunningOnCurrentThread()); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 253 | Delegate* delegate = tls_delegate.Get().Get(); |
| 254 | DCHECK(delegate->active_run_loops_.top()->allow_quit_current_deprecated_) |
| 255 | << "Please migrate off QuitCurrentDeprecated(), e.g. to QuitClosure()."; |
| 256 | delegate->active_run_loops_.top()->Quit(); |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // static |
| 260 | void RunLoop::QuitCurrentWhenIdleDeprecated() { |
| 261 | DCHECK(IsRunningOnCurrentThread()); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 262 | Delegate* delegate = tls_delegate.Get().Get(); |
| 263 | DCHECK(delegate->active_run_loops_.top()->allow_quit_current_deprecated_) |
| 264 | << "Please migrate off QuitCurrentWhenIdleDeprecated(), e.g. to " |
| 265 | "QuitWhenIdleClosure()."; |
| 266 | delegate->active_run_loops_.top()->QuitWhenIdle(); |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 267 | } |
| 268 | |
Gabriel Charette | 5bc8841 | 2018-05-16 03:28:25 | [diff] [blame] | 269 | // static |
| 270 | Closure RunLoop::QuitCurrentWhenIdleClosureDeprecated() { |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 271 | // TODO(844016): Fix callsites and enable this check, or remove the API. |
| 272 | // Delegate* delegate = tls_delegate.Get().Get(); |
| 273 | // DCHECK(delegate->active_run_loops_.top()->allow_quit_current_deprecated_) |
| 274 | // << "Please migrate off QuitCurrentWhenIdleClosureDeprecated(), e.g to " |
| 275 | // "QuitWhenIdleClosure()."; |
Gabriel Charette | 5bc8841 | 2018-05-16 03:28:25 | [diff] [blame] | 276 | return Bind(&RunLoop::QuitCurrentWhenIdleDeprecated); |
| 277 | } |
| 278 | |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 279 | #if DCHECK_IS_ON() |
| 280 | RunLoop::ScopedDisallowRunningForTesting::ScopedDisallowRunningForTesting() |
| 281 | : current_delegate_(tls_delegate.Get().Get()), |
| 282 | previous_run_allowance_( |
| 283 | current_delegate_ ? current_delegate_->allow_running_for_testing_ |
| 284 | : false) { |
| 285 | if (current_delegate_) |
| 286 | current_delegate_->allow_running_for_testing_ = false; |
| 287 | } |
| 288 | |
| 289 | RunLoop::ScopedDisallowRunningForTesting::~ScopedDisallowRunningForTesting() { |
| 290 | DCHECK_EQ(current_delegate_, tls_delegate.Get().Get()); |
| 291 | if (current_delegate_) |
| 292 | current_delegate_->allow_running_for_testing_ = previous_run_allowance_; |
| 293 | } |
| 294 | #else // DCHECK_IS_ON() |
| 295 | // Defined out of line so that the compiler doesn't inline these and realize |
| 296 | // the scope has no effect and then throws an "unused variable" warning in |
| 297 | // non-dcheck builds. |
| 298 | RunLoop::ScopedDisallowRunningForTesting::ScopedDisallowRunningForTesting() = |
| 299 | default; |
| 300 | RunLoop::ScopedDisallowRunningForTesting::~ScopedDisallowRunningForTesting() = |
| 301 | default; |
| 302 | #endif // DCHECK_IS_ON() |
| 303 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 304 | bool RunLoop::BeforeRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 305 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 306 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 307 | #if DCHECK_IS_ON() |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 308 | DCHECK(delegate_->allow_running_for_testing_) |
| 309 | << "RunLoop::Run() isn't allowed in the scope of a " |
| 310 | "ScopedDisallowRunningForTesting. Hint: if mixing " |
| 311 | "TestMockTimeTaskRunners on same thread, use TestMockTimeTaskRunner's " |
| 312 | "API instead of RunLoop to drive individual task runners."; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 313 | DCHECK(!run_called_); |
| 314 | run_called_ = true; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 315 | #endif // DCHECK_IS_ON() |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 316 | |
| 317 | // Allow Quit to be called before Run. |
| 318 | if (quit_called_) |
| 319 | return false; |
| 320 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 321 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 322 | active_run_loops_.push(this); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 323 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 324 | const bool is_nested = active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 325 | |
| 326 | if (is_nested) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 327 | for (auto& observer : delegate_->nesting_observers_) |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 328 | observer.OnBeginNestedRunLoop(); |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 329 | if (type_ == Type::kNestableTasksAllowed) |
| 330 | delegate_->EnsureWorkScheduled(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 331 | } |
jamescook | aacdfd0 | 2016-04-28 00:50:03 | [diff] [blame] | 332 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 333 | running_ = true; |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | void RunLoop::AfterRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 338 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 339 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 340 | running_ = false; |
| 341 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 342 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 343 | DCHECK_EQ(active_run_loops_.top(), this); |
| 344 | active_run_loops_.pop(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 345 | |
| 346 | RunLoop* previous_run_loop = |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 347 | active_run_loops_.empty() ? nullptr : active_run_loops_.top(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 348 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 349 | if (previous_run_loop) { |
| 350 | for (auto& observer : delegate_->nesting_observers_) |
| 351 | observer.OnExitNestedRunLoop(); |
| 352 | } |
| 353 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 354 | // Execute deferred Quit, if any: |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 355 | if (previous_run_loop && previous_run_loop->quit_called_) |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 356 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | } // namespace base |