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.
5 #include "base/timer.h"
7 #include "base/logging.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/threading/platform_thread.h"
14 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to
15 // Timer in the thread's default task runner. It also handles the following
17 // - deleted by the task runner.
18 // - abandoned (orphaned) by Timer.
19 class BaseTimerTaskInternal
{
21 explicit BaseTimerTaskInternal(Timer
* timer
)
25 ~BaseTimerTaskInternal() {
26 // This task may be getting cleared because the task runner has been
27 // destructed. If so, don't leave Timer with a dangling pointer
30 timer_
->StopAndAbandon();
34 // timer_ is NULL if we were abandoned.
38 // *this will be deleted by the task runner, so Timer needs to
40 timer_
->scheduled_task_
= NULL
;
42 // Although Timer should not call back into *this, let's clear
43 // the timer_ member first to be pedantic.
44 Timer
* timer
= timer_
;
46 timer
->RunScheduledTask();
49 // The task remains in the MessageLoop queue, but nothing will happen when it
59 Timer::Timer(bool retain_user_task
, bool is_repeating
)
60 : scheduled_task_(NULL
),
62 is_repeating_(is_repeating
),
63 retain_user_task_(retain_user_task
),
67 Timer::Timer(const tracked_objects::Location
& posted_from
,
69 const base::Closure
& user_task
,
71 : scheduled_task_(NULL
),
72 posted_from_(posted_from
),
74 user_task_(user_task
),
76 is_repeating_(is_repeating
),
77 retain_user_task_(true),
85 void Timer::Start(const tracked_objects::Location
& posted_from
,
87 const base::Closure
& user_task
) {
88 SetTaskInfo(posted_from
, delay
, user_task
);
94 if (!retain_user_task_
)
99 DCHECK(!user_task_
.is_null());
101 // If there's no pending task, start one up and return.
102 if (!scheduled_task_
) {
103 PostNewScheduledTask(delay_
);
107 // Set the new desired_run_time_.
108 desired_run_time_
= TimeTicks::Now() + delay_
;
110 // We can use the existing scheduled task if it arrives before the new
111 // desired_run_time_.
112 if (desired_run_time_
> scheduled_run_time_
) {
117 // We can't reuse the scheduled_task_, so abandon it and post a new one.
118 AbandonScheduledTask();
119 PostNewScheduledTask(delay_
);
122 void Timer::SetTaskInfo(const tracked_objects::Location
& posted_from
,
124 const base::Closure
& user_task
) {
125 posted_from_
= posted_from
;
127 user_task_
= user_task
;
130 void Timer::PostNewScheduledTask(TimeDelta delay
) {
131 DCHECK(scheduled_task_
== NULL
);
133 scheduled_task_
= new BaseTimerTaskInternal(this);
134 ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_
,
135 base::Bind(&BaseTimerTaskInternal::Run
, base::Owned(scheduled_task_
)),
137 scheduled_run_time_
= desired_run_time_
= TimeTicks::Now() + delay
;
138 // Remember the thread ID that posts the first task -- this will be verified
139 // later when the task is abandoned to detect misuse from multiple threads.
141 thread_id_
= static_cast<int>(PlatformThread::CurrentId());
144 void Timer::AbandonScheduledTask() {
145 DCHECK(thread_id_
== 0 ||
146 thread_id_
== static_cast<int>(PlatformThread::CurrentId()));
147 if (scheduled_task_
) {
148 scheduled_task_
->Abandon();
149 scheduled_task_
= NULL
;
153 void Timer::RunScheduledTask() {
154 // Task may have been disabled.
158 // First check if we need to delay the task because of a new target time.
159 if (desired_run_time_
> scheduled_run_time_
) {
160 // TimeTicks::Now() can be expensive, so only call it if we know the user
161 // has changed the desired_run_time_.
162 TimeTicks now
= TimeTicks::Now();
163 // Task runner may have called us late anyway, so only post a continuation
164 // task if the desired_run_time_ is in the future.
165 if (desired_run_time_
> now
) {
166 // Post a new task to span the remaining time.
167 PostNewScheduledTask(desired_run_time_
- now
);
172 // Make a local copy of the task to run. The Stop method will reset the
173 // user_task_ member if retain_user_task_ is false.
174 base::Closure task
= user_task_
;
177 PostNewScheduledTask(delay_
);
183 // No more member accesses here: *this could be deleted at this point.