diff options
Diffstat (limited to 'base/timer.cc')
-rw-r--r-- | base/timer.cc | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/base/timer.cc b/base/timer.cc index aced8cb..46fae6e 100644 --- a/base/timer.cc +++ b/base/timer.cc @@ -55,6 +55,14 @@ Timer::Timer(int delay, Task* task, bool repeating) Reset(); } +int Timer::GetCurrentDelay() const { + // Be careful here. Timers have a precision of microseconds, but this API is + // in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? It + // should be 6 to avoid timers firing early. + double delay = ceil((fire_time_ - Time::Now()).InMillisecondsF()); + return static_cast<int>(delay); +} + void Timer::Reset() { creation_time_ = Time::Now(); fire_time_ = creation_time_ + TimeDelta::FromMilliseconds(delay_); @@ -155,7 +163,7 @@ bool TimerManager::RunSomePendingTimers() { // timers have been set. const int kMaxTimersPerCall = 2; for (int i = 0; i < kMaxTimersPerCall; ++i) { - if (timers_.empty() || timers_.top()->fire_time() > Time::Now()) + if (timers_.empty() || GetCurrentDelay() > 0) break; // Get a pending timer. Deal with updating the timers_ queue and setting @@ -203,11 +211,13 @@ void TimerManager::StartTimer(Timer* timer) { DidChangeNextTimer(); } -Time TimerManager::GetNextFireTime() const { +int TimerManager::GetCurrentDelay() { if (timers_.empty()) - return Time(); - - return timers_.top()->fire_time(); + return -1; + int delay = timers_.top()->GetCurrentDelay(); + if (delay < 0) + delay = 0; + return delay; } void TimerManager::DidChangeNextTimer() { |