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

Unified Diff: base/timer/timer.cc

Issue 2484023002: Use a TickClock instead of TimeTicks::Now() in Timer. (Closed)
Patch Set: Created 4 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
« base/timer/timer.h ('K') | « base/timer/timer.h ('k') | no next file » | 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 e554905ffff47e06e276caa54d3d59754ef39527..d950fd8832ed88b62ea1485507ff0be30682a461 100644
--- a/base/timer/timer.cc
+++ b/base/timer/timer.cc
@@ -6,11 +6,16 @@
#include <stddef.h>
+#include <utility>
+
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "base/time/default_tick_clock.h"
+#include "base/time/tick_clock.h"
namespace base {
@@ -61,6 +66,7 @@ class BaseTimerTaskInternal {
Timer::Timer(bool retain_user_task, bool is_repeating)
: scheduled_task_(NULL),
+ tick_clock_(base::MakeUnique<DefaultTickClock>()),
thread_id_(0),
is_repeating_(is_repeating),
retain_user_task_(retain_user_task),
@@ -72,6 +78,7 @@ Timer::Timer(const tracked_objects::Location& posted_from,
const base::Closure& user_task,
bool is_repeating)
: scheduled_task_(NULL),
+ tick_clock_(base::MakeUnique<DefaultTickClock>()),
posted_from_(posted_from),
delay_(delay),
user_task_(user_task),
@@ -99,6 +106,11 @@ void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) {
task_runner_.swap(task_runner);
}
+void Timer::SetTickClock(std::unique_ptr<TickClock> tick_clock) {
+ DCHECK(tick_clock);
+ tick_clock_ = std::move(tick_clock);
+}
+
void Timer::Start(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
@@ -123,7 +135,7 @@ void Timer::Reset() {
// Set the new desired_run_time_.
if (delay_ > TimeDelta::FromMicroseconds(0))
- desired_run_time_ = TimeTicks::Now() + delay_;
+ desired_run_time_ = tick_clock_->NowTicks() + delay_;
else
desired_run_time_ = TimeTicks();
@@ -155,7 +167,7 @@ void Timer::PostNewScheduledTask(TimeDelta delay) {
GetTaskRunner()->PostDelayedTask(posted_from_,
base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
delay);
- scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay;
+ scheduled_run_time_ = desired_run_time_ = tick_clock_->NowTicks() + delay;
} else {
GetTaskRunner()->PostTask(posted_from_,
base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
@@ -189,9 +201,9 @@ void Timer::RunScheduledTask() {
// First check if we need to delay the task because of a new target time.
if (desired_run_time_ > scheduled_run_time_) {
- // TimeTicks::Now() can be expensive, so only call it if we know the user
- // has changed the desired_run_time_.
- TimeTicks now = TimeTicks::Now();
+ // TickClock::NowTicks() can be expensive, so only call it if we know the
+ // user has changed the desired_run_time_.
+ TimeTicks now = tick_clock_->NowTicks();
// Task runner may have called us late anyway, so only post a continuation
// task if the desired_run_time_ is in the future.
if (desired_run_time_ > now) {
« base/timer/timer.h ('K') | « base/timer/timer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698