summaryrefslogtreecommitdiffstats
path: root/base/timer
diff options
context:
space:
mode:
authorqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-04 20:05:45 +0000
committerqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-04 20:05:45 +0000
commitd04519357452f010ebc4c8e237333045058da21e (patch)
tree31d07270554363ca9206e6cc1a69c88e88d5d548 /base/timer
parentae4fa59fa635d637e5fa544d7388126d55dd1477 (diff)
downloadchromium_src-d04519357452f010ebc4c8e237333045058da21e.zip
chromium_src-d04519357452f010ebc4c8e237333045058da21e.tar.gz
chromium_src-d04519357452f010ebc4c8e237333045058da21e.tar.bz2
Refactor base::Timer to not call TimeTicks::Now for task with 0 delay.
This CL remove the calls to TimeTicks::Now when a timer is called with a delay null or negative. The reason is that TimeTicks::Now is costly on Android and is not useful in that use case. R=mark@chromium.org BUG=315070 Review URL: https://codereview.chromium.org/105003002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238741 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/timer')
-rw-r--r--base/timer/timer.cc21
-rw-r--r--base/timer/timer.h6
2 files changed, 19 insertions, 8 deletions
diff --git a/base/timer/timer.cc b/base/timer/timer.cc
index aafc01e..1c4b10c6d 100644
--- a/base/timer/timer.cc
+++ b/base/timer/timer.cc
@@ -108,11 +108,14 @@ void Timer::Reset() {
}
// Set the new desired_run_time_.
- desired_run_time_ = TimeTicks::Now() + delay_;
+ if (delay_ > TimeDelta::FromMicroseconds(0))
+ desired_run_time_ = TimeTicks::Now() + delay_;
+ else
+ desired_run_time_ = TimeTicks();
// We can use the existing scheduled task if it arrives before the new
// desired_run_time_.
- if (desired_run_time_ > scheduled_run_time_) {
+ if (desired_run_time_ >= scheduled_run_time_) {
is_running_ = true;
return;
}
@@ -134,10 +137,16 @@ void Timer::PostNewScheduledTask(TimeDelta delay) {
DCHECK(scheduled_task_ == NULL);
is_running_ = true;
scheduled_task_ = new BaseTimerTaskInternal(this);
- ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_,
- base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
- delay);
- scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay;
+ if (delay > TimeDelta::FromMicroseconds(0)) {
+ ThreadTaskRunnerHandle::Get()->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_,
+ base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
+ scheduled_run_time_ = desired_run_time_ = TimeTicks();
+ }
// Remember the thread ID that posts the first task -- this will be verified
// later when the task is abandoned to detect misuse from multiple threads.
if (!thread_id_)
diff --git a/base/timer/timer.h b/base/timer/timer.h
index f952d41..71a7ae8 100644
--- a/base/timer/timer.h
+++ b/base/timer/timer.h
@@ -148,7 +148,8 @@ class BASE_EXPORT Timer {
base::Closure user_task_;
// The estimated time that the MessageLoop will run the scheduled_task_ that
- // will call RunScheduledTask().
+ // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the
+ // task must be run immediately.
TimeTicks scheduled_run_time_;
// The desired run time of user_task_. The user may update this at any time,
@@ -156,7 +157,8 @@ class BASE_EXPORT Timer {
// greater than scheduled_run_time_, a continuation task will be posted to
// wait for the remaining time. This allows us to reuse the pending task so as
// not to flood the MessageLoop with orphaned tasks when the user code
- // excessively Stops and Starts the timer.
+ // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks
+ // if the task must be run immediately.
TimeTicks desired_run_time_;
// Thread ID of current MessageLoop for verifying single-threaded usage.