[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" |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 8 | #include "base/tracked_objects.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | RunLoop::RunLoop() |
| 14 | : loop_(MessageLoop::current()), |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 15 | previous_run_loop_(NULL), |
| 16 | run_depth_(0), |
| 17 | run_called_(false), |
| 18 | quit_called_(false), |
| 19 | running_(false), |
[email protected] | dcf1063 | 2013-10-08 19:23:33 | [diff] [blame] | 20 | quit_when_idle_received_(false), |
| 21 | weak_factory_(this) { |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 22 | DCHECK(loop_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 25 | RunLoop::~RunLoop() { |
| 26 | } |
| 27 | |
| 28 | void RunLoop::Run() { |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 29 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 30 | if (!BeforeRun()) |
| 31 | return; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 32 | |
| 33 | // Use task stopwatch to exclude the loop run time from the current task, if |
| 34 | // any. |
| 35 | tracked_objects::TaskStopwatch stopwatch; |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 36 | stopwatch.Start(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 37 | loop_->RunHandler(); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 38 | stopwatch.Stop(); |
| 39 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 40 | AfterRun(); |
| 41 | } |
| 42 | |
| 43 | void RunLoop::RunUntilIdle() { |
| 44 | quit_when_idle_received_ = true; |
| 45 | Run(); |
| 46 | } |
| 47 | |
| 48 | void RunLoop::Quit() { |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 49 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 50 | quit_called_ = true; |
| 51 | if (running_ && loop_->run_loop_ == this) { |
| 52 | // This is the inner-most RunLoop, so quit now. |
| 53 | loop_->QuitNow(); |
| 54 | } |
| 55 | } |
| 56 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 57 | void RunLoop::QuitWhenIdle() { |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 58 | DCHECK(thread_checker_.CalledOnValidThread()); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 59 | quit_when_idle_received_ = true; |
| 60 | } |
| 61 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 62 | base::Closure RunLoop::QuitClosure() { |
| 63 | return base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr()); |
| 64 | } |
| 65 | |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 66 | base::Closure RunLoop::QuitWhenIdleClosure() { |
| 67 | return base::Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr()); |
| 68 | } |
| 69 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 70 | bool RunLoop::BeforeRun() { |
| 71 | DCHECK(!run_called_); |
| 72 | run_called_ = true; |
| 73 | |
| 74 | // Allow Quit to be called before Run. |
| 75 | if (quit_called_) |
| 76 | return false; |
| 77 | |
| 78 | // Push RunLoop stack: |
| 79 | previous_run_loop_ = loop_->run_loop_; |
| 80 | run_depth_ = previous_run_loop_? previous_run_loop_->run_depth_ + 1 : 1; |
| 81 | loop_->run_loop_ = this; |
| 82 | |
jamescook | aacdfd0 | 2016-04-28 00:50:03 | [diff] [blame] | 83 | if (run_depth_ > 1) |
| 84 | loop_->NotifyBeginNestedLoop(); |
| 85 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 86 | running_ = true; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void RunLoop::AfterRun() { |
| 91 | running_ = false; |
| 92 | |
| 93 | // Pop RunLoop stack: |
| 94 | loop_->run_loop_ = previous_run_loop_; |
| 95 | |
| 96 | // Execute deferred QuitNow, if any: |
| 97 | if (previous_run_loop_ && previous_run_loop_->quit_called_) |
| 98 | loop_->QuitNow(); |
| 99 | } |
| 100 | |
| 101 | } // namespace base |