| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/timer/timer.h" | 5 #include "base/timer/timer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> |
| 10 |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 11 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/time/default_tick_clock.h" |
| 18 #include "base/time/tick_clock.h" |
| 14 | 19 |
| 15 namespace base { | 20 namespace base { |
| 16 | 21 |
| 17 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to | 22 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to |
| 18 // Timer in the thread's default task runner. It also handles the following | 23 // Timer in the thread's default task runner. It also handles the following |
| 19 // edge cases: | 24 // edge cases: |
| 20 // - deleted by the task runner. | 25 // - deleted by the task runner. |
| 21 // - abandoned (orphaned) by Timer. | 26 // - abandoned (orphaned) by Timer. |
| 22 class BaseTimerTaskInternal { | 27 class BaseTimerTaskInternal { |
| 23 public: | 28 public: |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 void Abandon() { | 59 void Abandon() { |
| 55 timer_ = NULL; | 60 timer_ = NULL; |
| 56 } | 61 } |
| 57 | 62 |
| 58 private: | 63 private: |
| 59 Timer* timer_; | 64 Timer* timer_; |
| 60 }; | 65 }; |
| 61 | 66 |
| 62 Timer::Timer(bool retain_user_task, bool is_repeating) | 67 Timer::Timer(bool retain_user_task, bool is_repeating) |
| 63 : scheduled_task_(NULL), | 68 : scheduled_task_(NULL), |
| 69 tick_clock_(base::MakeUnique<DefaultTickClock>()), |
| 64 thread_id_(0), | 70 thread_id_(0), |
| 65 is_repeating_(is_repeating), | 71 is_repeating_(is_repeating), |
| 66 retain_user_task_(retain_user_task), | 72 retain_user_task_(retain_user_task), |
| 67 is_running_(false) { | 73 is_running_(false) { |
| 68 } | 74 } |
| 69 | 75 |
| 70 Timer::Timer(const tracked_objects::Location& posted_from, | 76 Timer::Timer(const tracked_objects::Location& posted_from, |
| 71 TimeDelta delay, | 77 TimeDelta delay, |
| 72 const base::Closure& user_task, | 78 const base::Closure& user_task, |
| 73 bool is_repeating) | 79 bool is_repeating) |
| 74 : scheduled_task_(NULL), | 80 : scheduled_task_(NULL), |
| 81 tick_clock_(base::MakeUnique<DefaultTickClock>()), |
| 75 posted_from_(posted_from), | 82 posted_from_(posted_from), |
| 76 delay_(delay), | 83 delay_(delay), |
| 77 user_task_(user_task), | 84 user_task_(user_task), |
| 78 thread_id_(0), | 85 thread_id_(0), |
| 79 is_repeating_(is_repeating), | 86 is_repeating_(is_repeating), |
| 80 retain_user_task_(true), | 87 retain_user_task_(true), |
| 81 is_running_(false) { | 88 is_running_(false) { |
| 82 } | 89 } |
| 83 | 90 |
| 84 Timer::~Timer() { | 91 Timer::~Timer() { |
| 85 StopAndAbandon(); | 92 StopAndAbandon(); |
| 86 } | 93 } |
| 87 | 94 |
| 88 bool Timer::IsRunning() const { | 95 bool Timer::IsRunning() const { |
| 89 return is_running_; | 96 return is_running_; |
| 90 } | 97 } |
| 91 | 98 |
| 92 TimeDelta Timer::GetCurrentDelay() const { | 99 TimeDelta Timer::GetCurrentDelay() const { |
| 93 return delay_; | 100 return delay_; |
| 94 } | 101 } |
| 95 | 102 |
| 96 void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { | 103 void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 97 // Do not allow changing the task runner once something has been scheduled. | 104 // Do not allow changing the task runner once something has been scheduled. |
| 98 DCHECK_EQ(thread_id_, 0); | 105 DCHECK_EQ(thread_id_, 0); |
| 99 task_runner_.swap(task_runner); | 106 task_runner_.swap(task_runner); |
| 100 } | 107 } |
| 101 | 108 |
| 109 void Timer::SetTickClock(std::unique_ptr<TickClock> tick_clock) { |
| 110 DCHECK(tick_clock); |
| 111 tick_clock_ = std::move(tick_clock); |
| 112 } |
| 113 |
| 102 void Timer::Start(const tracked_objects::Location& posted_from, | 114 void Timer::Start(const tracked_objects::Location& posted_from, |
| 103 TimeDelta delay, | 115 TimeDelta delay, |
| 104 const base::Closure& user_task) { | 116 const base::Closure& user_task) { |
| 105 SetTaskInfo(posted_from, delay, user_task); | 117 SetTaskInfo(posted_from, delay, user_task); |
| 106 Reset(); | 118 Reset(); |
| 107 } | 119 } |
| 108 | 120 |
| 109 void Timer::Stop() { | 121 void Timer::Stop() { |
| 110 is_running_ = false; | 122 is_running_ = false; |
| 111 if (!retain_user_task_) | 123 if (!retain_user_task_) |
| 112 user_task_.Reset(); | 124 user_task_.Reset(); |
| 113 } | 125 } |
| 114 | 126 |
| 115 void Timer::Reset() { | 127 void Timer::Reset() { |
| 116 DCHECK(!user_task_.is_null()); | 128 DCHECK(!user_task_.is_null()); |
| 117 | 129 |
| 118 // If there's no pending task, start one up and return. | 130 // If there's no pending task, start one up and return. |
| 119 if (!scheduled_task_) { | 131 if (!scheduled_task_) { |
| 120 PostNewScheduledTask(delay_); | 132 PostNewScheduledTask(delay_); |
| 121 return; | 133 return; |
| 122 } | 134 } |
| 123 | 135 |
| 124 // Set the new desired_run_time_. | 136 // Set the new desired_run_time_. |
| 125 if (delay_ > TimeDelta::FromMicroseconds(0)) | 137 if (delay_ > TimeDelta::FromMicroseconds(0)) |
| 126 desired_run_time_ = TimeTicks::Now() + delay_; | 138 desired_run_time_ = tick_clock_->NowTicks() + delay_; |
| 127 else | 139 else |
| 128 desired_run_time_ = TimeTicks(); | 140 desired_run_time_ = TimeTicks(); |
| 129 | 141 |
| 130 // We can use the existing scheduled task if it arrives before the new | 142 // We can use the existing scheduled task if it arrives before the new |
| 131 // desired_run_time_. | 143 // desired_run_time_. |
| 132 if (desired_run_time_ >= scheduled_run_time_) { | 144 if (desired_run_time_ >= scheduled_run_time_) { |
| 133 is_running_ = true; | 145 is_running_ = true; |
| 134 return; | 146 return; |
| 135 } | 147 } |
| 136 | 148 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 148 } | 160 } |
| 149 | 161 |
| 150 void Timer::PostNewScheduledTask(TimeDelta delay) { | 162 void Timer::PostNewScheduledTask(TimeDelta delay) { |
| 151 DCHECK(scheduled_task_ == NULL); | 163 DCHECK(scheduled_task_ == NULL); |
| 152 is_running_ = true; | 164 is_running_ = true; |
| 153 scheduled_task_ = new BaseTimerTaskInternal(this); | 165 scheduled_task_ = new BaseTimerTaskInternal(this); |
| 154 if (delay > TimeDelta::FromMicroseconds(0)) { | 166 if (delay > TimeDelta::FromMicroseconds(0)) { |
| 155 GetTaskRunner()->PostDelayedTask(posted_from_, | 167 GetTaskRunner()->PostDelayedTask(posted_from_, |
| 156 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), | 168 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), |
| 157 delay); | 169 delay); |
| 158 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay; | 170 scheduled_run_time_ = desired_run_time_ = tick_clock_->NowTicks() + delay; |
| 159 } else { | 171 } else { |
| 160 GetTaskRunner()->PostTask(posted_from_, | 172 GetTaskRunner()->PostTask(posted_from_, |
| 161 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); | 173 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); |
| 162 scheduled_run_time_ = desired_run_time_ = TimeTicks(); | 174 scheduled_run_time_ = desired_run_time_ = TimeTicks(); |
| 163 } | 175 } |
| 164 // Remember the thread ID that posts the first task -- this will be verified | 176 // Remember the thread ID that posts the first task -- this will be verified |
| 165 // later when the task is abandoned to detect misuse from multiple threads. | 177 // later when the task is abandoned to detect misuse from multiple threads. |
| 166 if (!thread_id_) { | 178 if (!thread_id_) { |
| 167 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 179 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 168 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); | 180 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 182 } | 194 } |
| 183 } | 195 } |
| 184 | 196 |
| 185 void Timer::RunScheduledTask() { | 197 void Timer::RunScheduledTask() { |
| 186 // Task may have been disabled. | 198 // Task may have been disabled. |
| 187 if (!is_running_) | 199 if (!is_running_) |
| 188 return; | 200 return; |
| 189 | 201 |
| 190 // First check if we need to delay the task because of a new target time. | 202 // First check if we need to delay the task because of a new target time. |
| 191 if (desired_run_time_ > scheduled_run_time_) { | 203 if (desired_run_time_ > scheduled_run_time_) { |
| 192 // TimeTicks::Now() can be expensive, so only call it if we know the user | 204 // TickClock::NowTicks() can be expensive, so only call it if we know the |
| 193 // has changed the desired_run_time_. | 205 // user has changed the desired_run_time_. |
| 194 TimeTicks now = TimeTicks::Now(); | 206 TimeTicks now = tick_clock_->NowTicks(); |
| 195 // Task runner may have called us late anyway, so only post a continuation | 207 // Task runner may have called us late anyway, so only post a continuation |
| 196 // task if the desired_run_time_ is in the future. | 208 // task if the desired_run_time_ is in the future. |
| 197 if (desired_run_time_ > now) { | 209 if (desired_run_time_ > now) { |
| 198 // Post a new task to span the remaining time. | 210 // Post a new task to span the remaining time. |
| 199 PostNewScheduledTask(desired_run_time_ - now); | 211 PostNewScheduledTask(desired_run_time_ - now); |
| 200 return; | 212 return; |
| 201 } | 213 } |
| 202 } | 214 } |
| 203 | 215 |
| 204 // Make a local copy of the task to run. The Stop method will reset the | 216 // Make a local copy of the task to run. The Stop method will reset the |
| 205 // user_task_ member if retain_user_task_ is false. | 217 // user_task_ member if retain_user_task_ is false. |
| 206 base::Closure task = user_task_; | 218 base::Closure task = user_task_; |
| 207 | 219 |
| 208 if (is_repeating_) | 220 if (is_repeating_) |
| 209 PostNewScheduledTask(delay_); | 221 PostNewScheduledTask(delay_); |
| 210 else | 222 else |
| 211 Stop(); | 223 Stop(); |
| 212 | 224 |
| 213 task.Run(); | 225 task.Run(); |
| 214 | 226 |
| 215 // No more member accesses here: *this could be deleted at this point. | 227 // No more member accesses here: *this could be deleted at this point. |
| 216 } | 228 } |
| 217 | 229 |
| 218 } // namespace base | 230 } // namespace base |
| OLD | NEW |