[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" |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 10 | #include "base/single_thread_task_runner.h" |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 11 | #include "base/threading/thread_local.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 12 | #include "base/threading/thread_task_runner_handle.h" |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 13 | #include "base/tracked_objects.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 18 | namespace { |
| 19 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 20 | LazyInstance<ThreadLocalPointer<RunLoop::Delegate>>::Leaky tls_delegate = |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 21 | LAZY_INSTANCE_INITIALIZER; |
| 22 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 23 | // Runs |closure| immediately if this is called on |task_runner|, otherwise |
| 24 | // forwards |closure| to it. |
| 25 | void ProxyToTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner, |
| 26 | OnceClosure closure) { |
| 27 | if (task_runner->RunsTasksInCurrentSequence()) { |
| 28 | std::move(closure).Run(); |
| 29 | return; |
| 30 | } |
| 31 | task_runner->PostTask(FROM_HERE, std::move(closure)); |
| 32 | } |
| 33 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 34 | } // namespace |
| 35 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 36 | RunLoop::Delegate::Delegate() { |
| 37 | // The Delegate can be created on another thread. It is only bound in |
| 38 | // RegisterDelegateForCurrentThread(). |
| 39 | DETACH_FROM_THREAD(bound_thread_checker_); |
| 40 | } |
| 41 | |
| 42 | RunLoop::Delegate::~Delegate() { |
| 43 | DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); |
| 44 | // A RunLoop::Delegate may be destroyed before it is bound, if so it may still |
| 45 | // be on its creation thread (e.g. a Thread that fails to start) and |
| 46 | // shouldn't disrupt that thread's state. |
| 47 | if (bound_) |
| 48 | tls_delegate.Get().Set(nullptr); |
| 49 | } |
| 50 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame^] | 51 | bool RunLoop::Delegate::Client::ShouldQuitWhenIdle() const { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 52 | DCHECK_CALLED_ON_VALID_THREAD(outer_->bound_thread_checker_); |
| 53 | DCHECK(outer_->bound_); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame^] | 54 | return outer_->active_run_loops_.top()->quit_when_idle_received_; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool RunLoop::Delegate::Client::IsNested() const { |
| 58 | DCHECK_CALLED_ON_VALID_THREAD(outer_->bound_thread_checker_); |
| 59 | DCHECK(outer_->bound_); |
| 60 | return outer_->active_run_loops_.size() > 1; |
| 61 | } |
| 62 | |
| 63 | RunLoop::Delegate::Client::Client(Delegate* outer) : outer_(outer) {} |
| 64 | |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 65 | // static |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 66 | RunLoop::Delegate::Client* RunLoop::RegisterDelegateForCurrentThread( |
| 67 | Delegate* delegate) { |
| 68 | // Bind |delegate| to this thread. |
| 69 | DCHECK(!delegate->bound_); |
| 70 | DCHECK_CALLED_ON_VALID_THREAD(delegate->bound_thread_checker_); |
| 71 | |
| 72 | // There can only be one RunLoop::Delegate per thread. |
| 73 | DCHECK(!tls_delegate.Get().Get()); |
| 74 | tls_delegate.Get().Set(delegate); |
| 75 | delegate->bound_ = true; |
| 76 | |
| 77 | return &delegate->client_interface_; |
| 78 | } |
| 79 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 80 | RunLoop::RunLoop() |
| 81 | : delegate_(tls_delegate.Get().Get()), |
| 82 | origin_task_runner_(ThreadTaskRunnerHandle::Get()), |
| 83 | weak_factory_(this) { |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame^] | 84 | DCHECK(delegate_) << "A RunLoop::Delegate must be bound to this thread prior " |
| 85 | "to using RunLoop."; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 86 | DCHECK(origin_task_runner_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 89 | RunLoop::~RunLoop() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 90 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 91 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void RunLoop::Run() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 95 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 96 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 97 | if (!BeforeRun()) |
| 98 | return; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 99 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 100 | // It is okay to access this RunLoop from another sequence while Run() is |
| 101 | // active as this RunLoop won't touch its state until after that returns (if |
| 102 | // the RunLoop's state is accessed while processing Run(), it will be re-bound |
| 103 | // to the accessing sequence for the remainder of that Run() -- accessing from |
| 104 | // multiple sequences is still disallowed). |
| 105 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 106 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 107 | // Use task stopwatch to exclude the loop run time from the current task, if |
| 108 | // any. |
| 109 | tracked_objects::TaskStopwatch stopwatch; |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 110 | stopwatch.Start(); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 111 | delegate_->Run(); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 112 | stopwatch.Stop(); |
| 113 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 114 | // Rebind this RunLoop to the current thread after Run(). |
| 115 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 116 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 117 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 118 | AfterRun(); |
| 119 | } |
| 120 | |
| 121 | void RunLoop::RunUntilIdle() { |
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 | quit_when_idle_received_ = true; |
| 125 | Run(); |
| 126 | } |
| 127 | |
| 128 | void RunLoop::Quit() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 129 | // Thread-safe. |
| 130 | |
| 131 | // This can only be hit if run_loop->Quit() is called directly (QuitClosure() |
| 132 | // proxies through ProxyToTaskRunner() as it can only deref its WeakPtr on |
| 133 | // |origin_task_runner_|). |
| 134 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 135 | origin_task_runner_->PostTask( |
| 136 | FROM_HERE, base::BindOnce(&RunLoop::Quit, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 137 | return; |
| 138 | } |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 139 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 140 | quit_called_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 141 | if (running_ && delegate_->active_run_loops_.top() == this) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 142 | // This is the inner-most RunLoop, so quit now. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 143 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 147 | void RunLoop::QuitWhenIdle() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 148 | // Thread-safe. |
| 149 | |
| 150 | // This can only be hit if run_loop->QuitWhenIdle() is called directly |
| 151 | // (QuitWhenIdleClosure() proxies through ProxyToTaskRunner() as it can only |
| 152 | // deref its WeakPtr on |origin_task_runner_|). |
| 153 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
| 154 | origin_task_runner_->PostTask( |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 155 | FROM_HERE, base::BindOnce(&RunLoop::QuitWhenIdle, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 159 | quit_when_idle_received_ = true; |
| 160 | } |
| 161 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 162 | base::Closure RunLoop::QuitClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 163 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 164 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 165 | |
| 166 | // Need to use ProxyToTaskRunner() as WeakPtrs vended from |
| 167 | // |weak_factory_| may only be accessed on |origin_task_runner_|. |
| 168 | // TODO(gab): It feels wrong that QuitClosure() is bound to a WeakPtr. |
| 169 | return base::Bind(&ProxyToTaskRunner, origin_task_runner_, |
| 170 | base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr())); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 171 | } |
| 172 | |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 173 | base::Closure RunLoop::QuitWhenIdleClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 174 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 175 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 176 | |
| 177 | // Need to use ProxyToTaskRunner() as WeakPtrs vended from |
| 178 | // |weak_factory_| may only be accessed on |origin_task_runner_|. |
| 179 | // TODO(gab): It feels wrong that QuitWhenIdleClosure() is bound to a WeakPtr. |
| 180 | return base::Bind( |
| 181 | &ProxyToTaskRunner, origin_task_runner_, |
| 182 | base::Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr())); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 183 | } |
| 184 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 185 | // static |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 186 | bool RunLoop::IsRunningOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 187 | Delegate* delegate = tls_delegate.Get().Get(); |
| 188 | return delegate && !delegate->active_run_loops_.empty(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // static |
| 192 | bool RunLoop::IsNestedOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 193 | Delegate* delegate = tls_delegate.Get().Get(); |
| 194 | return delegate && delegate->active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // static |
| 198 | void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 199 | Delegate* delegate = tls_delegate.Get().Get(); |
| 200 | DCHECK(delegate); |
| 201 | CHECK(delegate->allow_nesting_); |
| 202 | delegate->nesting_observers_.AddObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // static |
| 206 | void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 207 | Delegate* delegate = tls_delegate.Get().Get(); |
| 208 | DCHECK(delegate); |
| 209 | CHECK(delegate->allow_nesting_); |
| 210 | delegate->nesting_observers_.RemoveObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // static |
| 214 | bool RunLoop::IsNestingAllowedOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 215 | return tls_delegate.Get().Get()->allow_nesting_; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // static |
| 219 | void RunLoop::DisallowNestingOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 220 | tls_delegate.Get().Get()->allow_nesting_ = false; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 221 | } |
| 222 | |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 223 | // static |
| 224 | void RunLoop::QuitCurrentDeprecated() { |
| 225 | DCHECK(IsRunningOnCurrentThread()); |
| 226 | tls_delegate.Get().Get()->active_run_loops_.top()->Quit(); |
| 227 | } |
| 228 | |
| 229 | // static |
| 230 | void RunLoop::QuitCurrentWhenIdleDeprecated() { |
| 231 | DCHECK(IsRunningOnCurrentThread()); |
| 232 | tls_delegate.Get().Get()->active_run_loops_.top()->QuitWhenIdle(); |
| 233 | } |
| 234 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 235 | bool RunLoop::BeforeRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 236 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 237 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 238 | #if DCHECK_IS_ON() |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 239 | DCHECK(!run_called_); |
| 240 | run_called_ = true; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 241 | #endif // DCHECK_IS_ON() |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 242 | |
| 243 | // Allow Quit to be called before Run. |
| 244 | if (quit_called_) |
| 245 | return false; |
| 246 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 247 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 248 | active_run_loops_.push(this); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 249 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 250 | const bool is_nested = active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 251 | |
| 252 | if (is_nested) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 253 | CHECK(delegate_->allow_nesting_); |
| 254 | for (auto& observer : delegate_->nesting_observers_) |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 255 | observer.OnBeginNestedRunLoop(); |
| 256 | } |
jamescook | aacdfd0 | 2016-04-28 00:50:03 | [diff] [blame] | 257 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 258 | running_ = true; |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | void RunLoop::AfterRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 263 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 264 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 265 | running_ = false; |
| 266 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 267 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 268 | DCHECK_EQ(active_run_loops_.top(), this); |
| 269 | active_run_loops_.pop(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 270 | |
| 271 | RunLoop* previous_run_loop = |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 272 | active_run_loops_.empty() ? nullptr : active_run_loops_.top(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 273 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame^] | 274 | // Execute deferred Quit, if any: |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 275 | if (previous_run_loop && previous_run_loop->quit_called_) |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 276 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | } // namespace base |