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

Unified 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: Final rebase Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/timer/timer.h ('k') | base/timer/timer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/timer/timer.cc
diff --git a/base/timer/timer.cc b/base/timer/timer.cc
index 1916ccc34ebb7c7bf328533ba8911ddd11679b17..11f73ca4290e4947a537cfa370dadcef7dca85d5 100644
--- a/base/timer/timer.cc
+++ b/base/timer/timer.cc
@@ -93,6 +93,12 @@ TimeDelta Timer::GetCurrentDelay() const {
return delay_;
}
+void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) {
+ // Do not allow changing the task runner once something has been scheduled.
+ DCHECK_EQ(thread_id_, 0);
+ task_runner_.swap(task_runner);
+}
+
void Timer::Start(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
@@ -146,12 +152,12 @@ void Timer::PostNewScheduledTask(TimeDelta delay) {
is_running_ = true;
scheduled_task_ = new BaseTimerTaskInternal(this);
if (delay > TimeDelta::FromMicroseconds(0)) {
- ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_,
+ GetTaskRunner()->PostDelayedTask(posted_from_,
base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
delay);
scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay;
} else {
- ThreadTaskRunnerHandle::Get()->PostTask(posted_from_,
+ GetTaskRunner()->PostTask(posted_from_,
base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
scheduled_run_time_ = desired_run_time_ = TimeTicks();
}
@@ -161,6 +167,10 @@ void Timer::PostNewScheduledTask(TimeDelta delay) {
thread_id_ = static_cast<int>(PlatformThread::CurrentId());
}
+scoped_refptr<SingleThreadTaskRunner> Timer::GetTaskRunner() {
+ return task_runner_.get() ? task_runner_ : ThreadTaskRunnerHandle::Get();
+}
+
void Timer::AbandonScheduledTask() {
DCHECK(thread_id_ == 0 ||
thread_id_ == static_cast<int>(PlatformThread::CurrentId()));
« no previous file with comments | « 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