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