Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: base/timer/timer.cc

Issue 637983003: Add support to base::Timer for custom task runners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update for review feedback Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
13 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
14 13
15 namespace base { 14 namespace base {
16 15
17 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to 16 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to
18 // Timer in the thread's default task runner. It also handles the following 17 // Timer in the thread's default task runner. It also handles the following
19 // edge cases: 18 // edge cases:
20 // - deleted by the task runner. 19 // - deleted by the task runner.
21 // - abandoned (orphaned) by Timer. 20 // - abandoned (orphaned) by Timer.
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 } 85 }
87 86
88 bool Timer::IsRunning() const { 87 bool Timer::IsRunning() const {
89 return is_running_; 88 return is_running_;
90 } 89 }
91 90
92 TimeDelta Timer::GetCurrentDelay() const { 91 TimeDelta Timer::GetCurrentDelay() const {
93 return delay_; 92 return delay_;
94 } 93 }
95 94
95 void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) {
96 // Do not allow changing the task runner once something has been scheduled.
97 DCHECK_EQ(thread_id_, 0);
98 task_runner_.swap(task_runner);
99 }
100
96 void Timer::Start(const tracked_objects::Location& posted_from, 101 void Timer::Start(const tracked_objects::Location& posted_from,
97 TimeDelta delay, 102 TimeDelta delay,
98 const base::Closure& user_task) { 103 const base::Closure& user_task) {
99 SetTaskInfo(posted_from, delay, user_task); 104 SetTaskInfo(posted_from, delay, user_task);
100 Reset(); 105 Reset();
101 } 106 }
102 107
103 void Timer::Stop() { 108 void Timer::Stop() {
104 is_running_ = false; 109 is_running_ = false;
105 if (!retain_user_task_) 110 if (!retain_user_task_)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 posted_from_ = posted_from; 144 posted_from_ = posted_from;
140 delay_ = delay; 145 delay_ = delay;
141 user_task_ = user_task; 146 user_task_ = user_task;
142 } 147 }
143 148
144 void Timer::PostNewScheduledTask(TimeDelta delay) { 149 void Timer::PostNewScheduledTask(TimeDelta delay) {
145 DCHECK(scheduled_task_ == NULL); 150 DCHECK(scheduled_task_ == NULL);
146 is_running_ = true; 151 is_running_ = true;
147 scheduled_task_ = new BaseTimerTaskInternal(this); 152 scheduled_task_ = new BaseTimerTaskInternal(this);
148 if (delay > TimeDelta::FromMicroseconds(0)) { 153 if (delay > TimeDelta::FromMicroseconds(0)) {
149 ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_, 154 GetTaskRunner()->PostDelayedTask(posted_from_,
150 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), 155 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
151 delay); 156 delay);
152 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay; 157 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay;
153 } else { 158 } else {
154 ThreadTaskRunnerHandle::Get()->PostTask(posted_from_, 159 GetTaskRunner()->PostTask(posted_from_,
155 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); 160 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
156 scheduled_run_time_ = desired_run_time_ = TimeTicks(); 161 scheduled_run_time_ = desired_run_time_ = TimeTicks();
157 } 162 }
158 // Remember the thread ID that posts the first task -- this will be verified 163 // Remember the thread ID that posts the first task -- this will be verified
159 // later when the task is abandoned to detect misuse from multiple threads. 164 // later when the task is abandoned to detect misuse from multiple threads.
160 if (!thread_id_) 165 if (!thread_id_)
161 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); 166 thread_id_ = static_cast<int>(PlatformThread::CurrentId());
162 } 167 }
163 168
169 scoped_refptr<SingleThreadTaskRunner> Timer::GetTaskRunner() {
170 return task_runner_.get() ? task_runner_ : ThreadTaskRunnerHandle::Get();
171 }
172
164 void Timer::AbandonScheduledTask() { 173 void Timer::AbandonScheduledTask() {
165 DCHECK(thread_id_ == 0 || 174 DCHECK(thread_id_ == 0 ||
166 thread_id_ == static_cast<int>(PlatformThread::CurrentId())); 175 thread_id_ == static_cast<int>(PlatformThread::CurrentId()));
167 if (scheduled_task_) { 176 if (scheduled_task_) {
168 scheduled_task_->Abandon(); 177 scheduled_task_->Abandon();
169 scheduled_task_ = NULL; 178 scheduled_task_ = NULL;
170 } 179 }
171 } 180 }
172 181
173 void Timer::RunScheduledTask() { 182 void Timer::RunScheduledTask() {
(...skipping 23 matching lines...) Expand all
197 PostNewScheduledTask(delay_); 206 PostNewScheduledTask(delay_);
198 else 207 else
199 Stop(); 208 Stop();
200 209
201 task.Run(); 210 task.Run();
202 211
203 // No more member accesses here: *this could be deleted at this point. 212 // No more member accesses here: *this could be deleted at this point.
204 } 213 }
205 214
206 } // namespace base 215 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698