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

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(scheduled_task_ == nullptr);
danakj 2014/10/18 19:00:00 use DCHECK_EQ i still don't understand what this
petrcermak 2014/10/20 12:42:41 I originally added it to check that no other threa
98 DCHECK(thread_id_ == 0);
danakj 2014/10/18 19:00:00 use DCHECK_EQ
petrcermak 2014/10/20 12:42:41 Done.
99 task_runner_.swap(task_runner);
100 }
101
96 void Timer::Start(const tracked_objects::Location& posted_from, 102 void Timer::Start(const tracked_objects::Location& posted_from,
97 TimeDelta delay, 103 TimeDelta delay,
98 const base::Closure& user_task) { 104 const base::Closure& user_task) {
99 SetTaskInfo(posted_from, delay, user_task); 105 SetTaskInfo(posted_from, delay, user_task);
100 Reset(); 106 Reset();
101 } 107 }
102 108
103 void Timer::Stop() { 109 void Timer::Stop() {
104 is_running_ = false; 110 is_running_ = false;
105 if (!retain_user_task_) 111 if (!retain_user_task_)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 posted_from_ = posted_from; 145 posted_from_ = posted_from;
140 delay_ = delay; 146 delay_ = delay;
141 user_task_ = user_task; 147 user_task_ = user_task;
142 } 148 }
143 149
144 void Timer::PostNewScheduledTask(TimeDelta delay) { 150 void Timer::PostNewScheduledTask(TimeDelta delay) {
145 DCHECK(scheduled_task_ == NULL); 151 DCHECK(scheduled_task_ == NULL);
146 is_running_ = true; 152 is_running_ = true;
147 scheduled_task_ = new BaseTimerTaskInternal(this); 153 scheduled_task_ = new BaseTimerTaskInternal(this);
148 if (delay > TimeDelta::FromMicroseconds(0)) { 154 if (delay > TimeDelta::FromMicroseconds(0)) {
149 ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_, 155 GetTaskRunner()->PostDelayedTask(posted_from_,
150 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), 156 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
151 delay); 157 delay);
152 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay; 158 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay;
153 } else { 159 } else {
154 ThreadTaskRunnerHandle::Get()->PostTask(posted_from_, 160 GetTaskRunner()->PostTask(posted_from_,
155 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); 161 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
156 scheduled_run_time_ = desired_run_time_ = TimeTicks(); 162 scheduled_run_time_ = desired_run_time_ = TimeTicks();
157 } 163 }
158 // Remember the thread ID that posts the first task -- this will be verified 164 // 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. 165 // later when the task is abandoned to detect misuse from multiple threads.
160 if (!thread_id_) 166 if (!thread_id_)
161 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); 167 thread_id_ = static_cast<int>(PlatformThread::CurrentId());
162 } 168 }
163 169
170 scoped_refptr<SingleThreadTaskRunner> Timer::GetTaskRunner() {
171 return task_runner_.get() == nullptr ?
danakj 2014/10/18 19:00:00 == nullptr is redundant here return task_runner_.
petrcermak 2014/10/20 12:42:41 Done.
172 ThreadTaskRunnerHandle::Get() : task_runner_;
173 }
174
164 void Timer::AbandonScheduledTask() { 175 void Timer::AbandonScheduledTask() {
165 DCHECK(thread_id_ == 0 || 176 DCHECK(thread_id_ == 0 ||
166 thread_id_ == static_cast<int>(PlatformThread::CurrentId())); 177 thread_id_ == static_cast<int>(PlatformThread::CurrentId()));
167 if (scheduled_task_) { 178 if (scheduled_task_) {
168 scheduled_task_->Abandon(); 179 scheduled_task_->Abandon();
169 scheduled_task_ = NULL; 180 scheduled_task_ = NULL;
170 } 181 }
171 } 182 }
172 183
173 void Timer::RunScheduledTask() { 184 void Timer::RunScheduledTask() {
(...skipping 23 matching lines...) Expand all
197 PostNewScheduledTask(delay_); 208 PostNewScheduledTask(delay_);
198 else 209 else
199 Stop(); 210 Stop();
200 211
201 task.Run(); 212 task.Run();
202 213
203 // No more member accesses here: *this could be deleted at this point. 214 // No more member accesses here: *this could be deleted at this point.
204 } 215 }
205 216
206 } // namespace base 217 } // namespace base
OLDNEW
« base/timer/timer.h ('K') | « base/timer/timer.h ('k') | base/timer/timer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698