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