| 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> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/sequenced_task_runner_handle.h" |
| 17 #include "base/time/tick_clock.h" | 16 #include "base/time/tick_clock.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 | 19 |
| 21 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to | 20 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to Timer |
| 22 // Timer in the thread's default task runner. It also handles the following | 21 // on the current sequence. It also handles the following edge cases: |
| 23 // edge cases: | |
| 24 // - deleted by the task runner. | 22 // - deleted by the task runner. |
| 25 // - abandoned (orphaned) by Timer. | 23 // - abandoned (orphaned) by Timer. |
| 26 class BaseTimerTaskInternal { | 24 class BaseTimerTaskInternal { |
| 27 public: | 25 public: |
| 28 explicit BaseTimerTaskInternal(Timer* timer) | 26 explicit BaseTimerTaskInternal(Timer* timer) |
| 29 : timer_(timer) { | 27 : timer_(timer) { |
| 30 } | 28 } |
| 31 | 29 |
| 32 ~BaseTimerTaskInternal() { | 30 ~BaseTimerTaskInternal() { |
| 33 // This task may be getting cleared because the task runner has been | 31 // This task may be getting cleared because the task runner has been |
| 34 // destructed. If so, don't leave Timer with a dangling pointer | 32 // destructed. If so, don't leave Timer with a dangling pointer |
| 35 // to this. | 33 // to this. |
| 36 if (timer_) | 34 if (timer_) |
| 37 timer_->StopAndAbandon(); | 35 timer_->AbandonAndStop(); |
| 38 } | 36 } |
| 39 | 37 |
| 40 void Run() { | 38 void Run() { |
| 41 // timer_ is NULL if we were abandoned. | 39 // |timer_| is nullptr if we were abandoned. |
| 42 if (!timer_) | 40 if (!timer_) |
| 43 return; | 41 return; |
| 44 | 42 |
| 45 // *this will be deleted by the task runner, so Timer needs to | 43 // |this| will be deleted by the task runner, so Timer needs to forget us: |
| 46 // forget us: | 44 timer_->scheduled_task_ = nullptr; |
| 47 timer_->scheduled_task_ = NULL; | |
| 48 | 45 |
| 49 // Although Timer should not call back into *this, let's clear | 46 // Although Timer should not call back into |this|, let's clear |timer_| |
| 50 // the timer_ member first to be pedantic. | 47 // first to be pedantic. |
| 51 Timer* timer = timer_; | 48 Timer* timer = timer_; |
| 52 timer_ = NULL; | 49 timer_ = nullptr; |
| 53 timer->RunScheduledTask(); | 50 timer->RunScheduledTask(); |
| 54 } | 51 } |
| 55 | 52 |
| 56 // The task remains in the MessageLoop queue, but nothing will happen when it | 53 // The task remains in the queue, but nothing will happen when it runs. |
| 57 // runs. | 54 void Abandon() { timer_ = nullptr; } |
| 58 void Abandon() { | |
| 59 timer_ = NULL; | |
| 60 } | |
| 61 | 55 |
| 62 private: | 56 private: |
| 63 Timer* timer_; | 57 Timer* timer_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(BaseTimerTaskInternal); |
| 64 }; | 60 }; |
| 65 | 61 |
| 66 Timer::Timer(bool retain_user_task, bool is_repeating) | 62 Timer::Timer(bool retain_user_task, bool is_repeating) |
| 67 : Timer(retain_user_task, is_repeating, nullptr) {} | 63 : Timer(retain_user_task, is_repeating, nullptr) {} |
| 68 | 64 |
| 69 Timer::Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock) | 65 Timer::Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock) |
| 70 : scheduled_task_(nullptr), | 66 : scheduled_task_(nullptr), |
| 71 thread_id_(0), | |
| 72 is_repeating_(is_repeating), | 67 is_repeating_(is_repeating), |
| 73 retain_user_task_(retain_user_task), | 68 retain_user_task_(retain_user_task), |
| 74 tick_clock_(tick_clock), | 69 tick_clock_(tick_clock), |
| 75 is_running_(false) {} | 70 is_running_(false) { |
| 71 // It is safe for the timer to be created on a different thread/sequence than |
| 72 // the one from which the timer APIs are called. The first call to the |
| 73 // checker's CalledOnValidSequence() method will re-bind the checker, and |
| 74 // later calls will verify that the same task runner is used. |
| 75 origin_sequence_checker_.DetachFromSequence(); |
| 76 } |
| 76 | 77 |
| 77 Timer::Timer(const tracked_objects::Location& posted_from, | 78 Timer::Timer(const tracked_objects::Location& posted_from, |
| 78 TimeDelta delay, | 79 TimeDelta delay, |
| 79 const base::Closure& user_task, | 80 const base::Closure& user_task, |
| 80 bool is_repeating) | 81 bool is_repeating) |
| 81 : Timer(posted_from, delay, user_task, is_repeating, nullptr) {} | 82 : Timer(posted_from, delay, user_task, is_repeating, nullptr) {} |
| 82 | 83 |
| 83 Timer::Timer(const tracked_objects::Location& posted_from, | 84 Timer::Timer(const tracked_objects::Location& posted_from, |
| 84 TimeDelta delay, | 85 TimeDelta delay, |
| 85 const base::Closure& user_task, | 86 const base::Closure& user_task, |
| 86 bool is_repeating, | 87 bool is_repeating, |
| 87 TickClock* tick_clock) | 88 TickClock* tick_clock) |
| 88 : scheduled_task_(nullptr), | 89 : scheduled_task_(nullptr), |
| 89 posted_from_(posted_from), | 90 posted_from_(posted_from), |
| 90 delay_(delay), | 91 delay_(delay), |
| 91 user_task_(user_task), | 92 user_task_(user_task), |
| 92 thread_id_(0), | |
| 93 is_repeating_(is_repeating), | 93 is_repeating_(is_repeating), |
| 94 retain_user_task_(true), | 94 retain_user_task_(true), |
| 95 tick_clock_(tick_clock), | 95 tick_clock_(tick_clock), |
| 96 is_running_(false) {} | 96 is_running_(false) { |
| 97 // See comment in other constructor. |
| 98 origin_sequence_checker_.DetachFromSequence(); |
| 99 } |
| 97 | 100 |
| 98 Timer::~Timer() { | 101 Timer::~Timer() { |
| 99 StopAndAbandon(); | 102 // TODO(gab): Enable this once Stop() properly detaches from sequence. |
| 103 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 104 AbandonAndStop(); |
| 100 } | 105 } |
| 101 | 106 |
| 102 bool Timer::IsRunning() const { | 107 bool Timer::IsRunning() const { |
| 108 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 103 return is_running_; | 109 return is_running_; |
| 104 } | 110 } |
| 105 | 111 |
| 106 TimeDelta Timer::GetCurrentDelay() const { | 112 TimeDelta Timer::GetCurrentDelay() const { |
| 113 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 107 return delay_; | 114 return delay_; |
| 108 } | 115 } |
| 109 | 116 |
| 110 void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { | 117 void Timer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) { |
| 111 // Do not allow changing the task runner once something has been scheduled. | 118 // Do not allow changing the task runner when the Timer is running. |
| 112 DCHECK_EQ(thread_id_, 0); | 119 // Don't check for |origin_sequence_checker_.CalledOnValidSequence()| here to |
| 120 // allow the use case of constructing the Timer and immediatetly invoking |
| 121 // SetTaskRunner() before starting it (CalledOnValidSequence() would undo the |
| 122 // DetachFromSequence() from the constructor). The |!is_running| check kind of |
| 123 // verifies the same thing (and TSAN should catch callers that do it wrong but |
| 124 // somehow evade all debug checks). |
| 125 DCHECK(!is_running_); |
| 113 task_runner_.swap(task_runner); | 126 task_runner_.swap(task_runner); |
| 114 } | 127 } |
| 115 | 128 |
| 116 void Timer::Start(const tracked_objects::Location& posted_from, | 129 void Timer::Start(const tracked_objects::Location& posted_from, |
| 117 TimeDelta delay, | 130 TimeDelta delay, |
| 118 const base::Closure& user_task) { | 131 const base::Closure& user_task) { |
| 119 SetTaskInfo(posted_from, delay, user_task); | 132 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 133 |
| 134 posted_from_ = posted_from; |
| 135 delay_ = delay; |
| 136 user_task_ = user_task; |
| 137 |
| 120 Reset(); | 138 Reset(); |
| 121 } | 139 } |
| 122 | 140 |
| 123 void Timer::Stop() { | 141 void Timer::Stop() { |
| 142 // TODO(gab): Enable this when it's no longer called racily from |
| 143 // RunScheduledTask(): https://crbug.com/587199. |
| 144 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 145 |
| 124 is_running_ = false; | 146 is_running_ = false; |
| 125 if (!retain_user_task_) | 147 if (!retain_user_task_) |
| 126 user_task_.Reset(); | 148 user_task_.Reset(); |
| 149 // No more member accesses here: |this| could be deleted after freeing |
| 150 // |user_task_|. |
| 127 } | 151 } |
| 128 | 152 |
| 129 void Timer::Reset() { | 153 void Timer::Reset() { |
| 154 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 130 DCHECK(!user_task_.is_null()); | 155 DCHECK(!user_task_.is_null()); |
| 131 | 156 |
| 132 // If there's no pending task, start one up and return. | 157 // If there's no pending task, start one up and return. |
| 133 if (!scheduled_task_) { | 158 if (!scheduled_task_) { |
| 134 PostNewScheduledTask(delay_); | 159 PostNewScheduledTask(delay_); |
| 135 return; | 160 return; |
| 136 } | 161 } |
| 137 | 162 |
| 138 // Set the new desired_run_time_. | 163 // Set the new |desired_run_time_|. |
| 139 if (delay_ > TimeDelta::FromMicroseconds(0)) | 164 if (delay_ > TimeDelta::FromMicroseconds(0)) |
| 140 desired_run_time_ = Now() + delay_; | 165 desired_run_time_ = Now() + delay_; |
| 141 else | 166 else |
| 142 desired_run_time_ = TimeTicks(); | 167 desired_run_time_ = TimeTicks(); |
| 143 | 168 |
| 144 // We can use the existing scheduled task if it arrives before the new | 169 // We can use the existing scheduled task if it arrives before the new |
| 145 // desired_run_time_. | 170 // |desired_run_time_|. |
| 146 if (desired_run_time_ >= scheduled_run_time_) { | 171 if (desired_run_time_ >= scheduled_run_time_) { |
| 147 is_running_ = true; | 172 is_running_ = true; |
| 148 return; | 173 return; |
| 149 } | 174 } |
| 150 | 175 |
| 151 // We can't reuse the scheduled_task_, so abandon it and post a new one. | 176 // We can't reuse the |scheduled_task_|, so abandon it and post a new one. |
| 152 AbandonScheduledTask(); | 177 AbandonScheduledTask(); |
| 153 PostNewScheduledTask(delay_); | 178 PostNewScheduledTask(delay_); |
| 154 } | 179 } |
| 155 | 180 |
| 156 TimeTicks Timer::Now() const { | 181 TimeTicks Timer::Now() const { |
| 182 // TODO(gab): Enable this when it's no longer called racily from |
| 183 // RunScheduledTask(): https://crbug.com/587199. |
| 184 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 157 return tick_clock_ ? tick_clock_->NowTicks() : TimeTicks::Now(); | 185 return tick_clock_ ? tick_clock_->NowTicks() : TimeTicks::Now(); |
| 158 } | 186 } |
| 159 | 187 |
| 160 void Timer::SetTaskInfo(const tracked_objects::Location& posted_from, | |
| 161 TimeDelta delay, | |
| 162 const base::Closure& user_task) { | |
| 163 posted_from_ = posted_from; | |
| 164 delay_ = delay; | |
| 165 user_task_ = user_task; | |
| 166 } | |
| 167 | |
| 168 void Timer::PostNewScheduledTask(TimeDelta delay) { | 188 void Timer::PostNewScheduledTask(TimeDelta delay) { |
| 169 DCHECK(scheduled_task_ == NULL); | 189 // TODO(gab): Enable this when it's no longer called racily from |
| 190 // RunScheduledTask(): https://crbug.com/587199. |
| 191 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 192 DCHECK(!scheduled_task_); |
| 170 is_running_ = true; | 193 is_running_ = true; |
| 171 scheduled_task_ = new BaseTimerTaskInternal(this); | 194 scheduled_task_ = new BaseTimerTaskInternal(this); |
| 172 if (delay > TimeDelta::FromMicroseconds(0)) { | 195 if (delay > TimeDelta::FromMicroseconds(0)) { |
| 196 // TODO(gab): Posting BaseTimerTaskInternal::Run to another sequence makes |
| 197 // this code racy. https://crbug.com/587199 |
| 173 GetTaskRunner()->PostDelayedTask( | 198 GetTaskRunner()->PostDelayedTask( |
| 174 posted_from_, | 199 posted_from_, |
| 175 base::BindOnce(&BaseTimerTaskInternal::Run, | 200 base::BindOnce(&BaseTimerTaskInternal::Run, |
| 176 base::Owned(scheduled_task_)), | 201 base::Owned(scheduled_task_)), |
| 177 delay); | 202 delay); |
| 178 scheduled_run_time_ = desired_run_time_ = Now() + delay; | 203 scheduled_run_time_ = desired_run_time_ = Now() + delay; |
| 179 } else { | 204 } else { |
| 180 GetTaskRunner()->PostTask(posted_from_, | 205 GetTaskRunner()->PostTask(posted_from_, |
| 181 base::BindOnce(&BaseTimerTaskInternal::Run, | 206 base::BindOnce(&BaseTimerTaskInternal::Run, |
| 182 base::Owned(scheduled_task_))); | 207 base::Owned(scheduled_task_))); |
| 183 scheduled_run_time_ = desired_run_time_ = TimeTicks(); | 208 scheduled_run_time_ = desired_run_time_ = TimeTicks(); |
| 184 } | 209 } |
| 185 // Remember the thread ID that posts the first task -- this will be verified | |
| 186 // later when the task is abandoned to detect misuse from multiple threads. | |
| 187 if (!thread_id_) | |
| 188 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); | |
| 189 } | 210 } |
| 190 | 211 |
| 191 scoped_refptr<SingleThreadTaskRunner> Timer::GetTaskRunner() { | 212 scoped_refptr<SequencedTaskRunner> Timer::GetTaskRunner() { |
| 192 return task_runner_.get() ? task_runner_ : ThreadTaskRunnerHandle::Get(); | 213 return task_runner_.get() ? task_runner_ : SequencedTaskRunnerHandle::Get(); |
| 193 } | 214 } |
| 194 | 215 |
| 195 void Timer::AbandonScheduledTask() { | 216 void Timer::AbandonScheduledTask() { |
| 196 DCHECK(thread_id_ == 0 || | 217 // TODO(gab): Enable this when it's no longer called racily from |
| 197 thread_id_ == static_cast<int>(PlatformThread::CurrentId())); | 218 // RunScheduledTask() -> Stop(): https://crbug.com/587199. |
| 219 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 198 if (scheduled_task_) { | 220 if (scheduled_task_) { |
| 199 scheduled_task_->Abandon(); | 221 scheduled_task_->Abandon(); |
| 200 scheduled_task_ = NULL; | 222 scheduled_task_ = nullptr; |
| 201 } | 223 } |
| 202 } | 224 } |
| 203 | 225 |
| 204 void Timer::RunScheduledTask() { | 226 void Timer::RunScheduledTask() { |
| 227 // TODO(gab): Enable this when it's no longer called racily: |
| 228 // https://crbug.com/587199. |
| 229 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); |
| 230 |
| 205 // Task may have been disabled. | 231 // Task may have been disabled. |
| 206 if (!is_running_) | 232 if (!is_running_) |
| 207 return; | 233 return; |
| 208 | 234 |
| 209 // First check if we need to delay the task because of a new target time. | 235 // First check if we need to delay the task because of a new target time. |
| 210 if (desired_run_time_ > scheduled_run_time_) { | 236 if (desired_run_time_ > scheduled_run_time_) { |
| 211 // Now() can be expensive, so only call it if we know the user has changed | 237 // Now() can be expensive, so only call it if we know the user has changed |
| 212 // the desired_run_time_. | 238 // the |desired_run_time_|. |
| 213 TimeTicks now = Now(); | 239 TimeTicks now = Now(); |
| 214 // Task runner may have called us late anyway, so only post a continuation | 240 // Task runner may have called us late anyway, so only post a continuation |
| 215 // task if the desired_run_time_ is in the future. | 241 // task if the |desired_run_time_| is in the future. |
| 216 if (desired_run_time_ > now) { | 242 if (desired_run_time_ > now) { |
| 217 // Post a new task to span the remaining time. | 243 // Post a new task to span the remaining time. |
| 218 PostNewScheduledTask(desired_run_time_ - now); | 244 PostNewScheduledTask(desired_run_time_ - now); |
| 219 return; | 245 return; |
| 220 } | 246 } |
| 221 } | 247 } |
| 222 | 248 |
| 223 // Make a local copy of the task to run. The Stop method will reset the | 249 // Make a local copy of the task to run. The Stop method will reset the |
| 224 // user_task_ member if retain_user_task_ is false. | 250 // |user_task_| member if |retain_user_task_| is false. |
| 225 base::Closure task = user_task_; | 251 base::Closure task = user_task_; |
| 226 | 252 |
| 227 if (is_repeating_) | 253 if (is_repeating_) |
| 228 PostNewScheduledTask(delay_); | 254 PostNewScheduledTask(delay_); |
| 229 else | 255 else |
| 230 Stop(); | 256 Stop(); |
| 231 | 257 |
| 232 task.Run(); | 258 task.Run(); |
| 233 | 259 |
| 234 // No more member accesses here: *this could be deleted at this point. | 260 // No more member accesses here: |this| could be deleted at this point. |
| 235 } | 261 } |
| 236 | 262 |
| 237 } // namespace base | 263 } // namespace base |
| OLD | NEW |