[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 | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 8 | #include "base/lazy_instance.h" |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 9 | #include "base/threading/thread_local.h" |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 10 | #include "base/tracked_objects.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 15 | namespace { |
| 16 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 17 | LazyInstance<ThreadLocalPointer<RunLoop::Delegate>>::Leaky tls_delegate = |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 18 | LAZY_INSTANCE_INITIALIZER; |
| 19 | |
| 20 | } // namespace |
| 21 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 22 | RunLoop::Delegate::Delegate() { |
| 23 | // The Delegate can be created on another thread. It is only bound in |
| 24 | // RegisterDelegateForCurrentThread(). |
| 25 | DETACH_FROM_THREAD(bound_thread_checker_); |
| 26 | } |
| 27 | |
| 28 | RunLoop::Delegate::~Delegate() { |
| 29 | DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); |
| 30 | // A RunLoop::Delegate may be destroyed before it is bound, if so it may still |
| 31 | // be on its creation thread (e.g. a Thread that fails to start) and |
| 32 | // shouldn't disrupt that thread's state. |
| 33 | if (bound_) |
| 34 | tls_delegate.Get().Set(nullptr); |
| 35 | } |
| 36 | |
| 37 | RunLoop* RunLoop::Delegate::Client::GetTopMostRunLoop() const { |
| 38 | DCHECK_CALLED_ON_VALID_THREAD(outer_->bound_thread_checker_); |
| 39 | DCHECK(outer_->bound_); |
| 40 | return outer_->active_run_loops_.empty() ? nullptr |
| 41 | : outer_->active_run_loops_.top(); |
| 42 | } |
| 43 | |
| 44 | bool RunLoop::Delegate::Client::IsNested() const { |
| 45 | DCHECK_CALLED_ON_VALID_THREAD(outer_->bound_thread_checker_); |
| 46 | DCHECK(outer_->bound_); |
| 47 | return outer_->active_run_loops_.size() > 1; |
| 48 | } |
| 49 | |
| 50 | RunLoop::Delegate::Client::Client(Delegate* outer) : outer_(outer) {} |
| 51 | |
| 52 | RunLoop::Delegate::Client* RunLoop::RegisterDelegateForCurrentThread( |
| 53 | Delegate* delegate) { |
| 54 | // Bind |delegate| to this thread. |
| 55 | DCHECK(!delegate->bound_); |
| 56 | DCHECK_CALLED_ON_VALID_THREAD(delegate->bound_thread_checker_); |
| 57 | |
| 58 | // There can only be one RunLoop::Delegate per thread. |
| 59 | DCHECK(!tls_delegate.Get().Get()); |
| 60 | tls_delegate.Get().Set(delegate); |
| 61 | delegate->bound_ = true; |
| 62 | |
| 63 | return &delegate->client_interface_; |
| 64 | } |
| 65 | |
| 66 | RunLoop::RunLoop() : delegate_(tls_delegate.Get().Get()), weak_factory_(this) { |
| 67 | // A RunLoop::Delegate must be bound to this thread prior to using RunLoop. |
| 68 | DCHECK(delegate_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 71 | RunLoop::~RunLoop() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 72 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
| 73 | // DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void RunLoop::Run() { |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 77 | DCHECK(thread_checker_.CalledOnValidThread()); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 78 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 79 | if (!BeforeRun()) |
| 80 | return; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 81 | |
| 82 | // Use task stopwatch to exclude the loop run time from the current task, if |
| 83 | // any. |
| 84 | tracked_objects::TaskStopwatch stopwatch; |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 85 | stopwatch.Start(); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 86 | delegate_->Run(); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 87 | stopwatch.Stop(); |
| 88 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 89 | AfterRun(); |
| 90 | } |
| 91 | |
| 92 | void RunLoop::RunUntilIdle() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 93 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 94 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 95 | quit_when_idle_received_ = true; |
| 96 | Run(); |
| 97 | } |
| 98 | |
| 99 | void RunLoop::Quit() { |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 100 | DCHECK(thread_checker_.CalledOnValidThread()); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 101 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 102 | quit_called_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 103 | if (running_ && delegate_->active_run_loops_.top() == this) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 104 | // This is the inner-most RunLoop, so quit now. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 105 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 109 | void RunLoop::QuitWhenIdle() { |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 110 | DCHECK(thread_checker_.CalledOnValidThread()); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 111 | quit_when_idle_received_ = true; |
| 112 | } |
| 113 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 114 | base::Closure RunLoop::QuitClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 115 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
| 116 | // DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 117 | return base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr()); |
| 118 | } |
| 119 | |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 120 | base::Closure RunLoop::QuitWhenIdleClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 121 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
| 122 | // DCHECK(thread_checker_.CalledOnValidThread()); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 123 | return base::Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr()); |
| 124 | } |
| 125 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 126 | // static |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 127 | bool RunLoop::IsRunningOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 128 | Delegate* delegate = tls_delegate.Get().Get(); |
| 129 | return delegate && !delegate->active_run_loops_.empty(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // static |
| 133 | bool RunLoop::IsNestedOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 134 | Delegate* delegate = tls_delegate.Get().Get(); |
| 135 | return delegate && delegate->active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // static |
| 139 | void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 140 | Delegate* delegate = tls_delegate.Get().Get(); |
| 141 | DCHECK(delegate); |
| 142 | CHECK(delegate->allow_nesting_); |
| 143 | delegate->nesting_observers_.AddObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // static |
| 147 | void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 148 | Delegate* delegate = tls_delegate.Get().Get(); |
| 149 | DCHECK(delegate); |
| 150 | CHECK(delegate->allow_nesting_); |
| 151 | delegate->nesting_observers_.RemoveObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // static |
| 155 | bool RunLoop::IsNestingAllowedOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 156 | return tls_delegate.Get().Get()->allow_nesting_; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // static |
| 160 | void RunLoop::DisallowNestingOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 161 | tls_delegate.Get().Get()->allow_nesting_ = false; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 164 | bool RunLoop::BeforeRun() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 165 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 166 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 167 | DCHECK(!run_called_); |
| 168 | run_called_ = true; |
| 169 | |
| 170 | // Allow Quit to be called before Run. |
| 171 | if (quit_called_) |
| 172 | return false; |
| 173 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 174 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 175 | active_run_loops_.push(this); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 176 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 177 | const bool is_nested = active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 178 | |
| 179 | if (is_nested) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 180 | CHECK(delegate_->allow_nesting_); |
| 181 | for (auto& observer : delegate_->nesting_observers_) |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 182 | observer.OnBeginNestedRunLoop(); |
| 183 | } |
jamescook | aacdfd0 | 2016-04-28 00:50:03 | [diff] [blame] | 184 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 185 | running_ = true; |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | void RunLoop::AfterRun() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 190 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 191 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 192 | running_ = false; |
| 193 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 194 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 195 | DCHECK_EQ(active_run_loops_.top(), this); |
| 196 | active_run_loops_.pop(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 197 | |
| 198 | RunLoop* previous_run_loop = |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 199 | active_run_loops_.empty() ? nullptr : active_run_loops_.top(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 200 | |
| 201 | // Execute deferred QuitNow, if any: |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 202 | if (previous_run_loop && previous_run_loop->quit_called_) |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame^] | 203 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // namespace base |