From a5b94a90c11917b9c7f1d91c9a2069badc22819e Mon Sep 17 00:00:00 2001 From: "darin@google.com" Date: Tue, 12 Aug 2008 23:25:43 +0000 Subject: git-svn-id: svn://svn.chromium.org/chrome/trunk/src@760 0039d316-1c4b-4281-b951-d872f2087c98 --- base/timer.h | 114 +++++++++++++++++++++++++---------------------------------- 1 file changed, 48 insertions(+), 66 deletions(-) (limited to 'base/timer.h') diff --git a/base/timer.h b/base/timer.h index de22b64..c23f725 100644 --- a/base/timer.h +++ b/base/timer.h @@ -30,54 +30,50 @@ #ifndef BASE_TIMER_H_ #define BASE_TIMER_H_ -#include - #include #include #include "base/basictypes.h" #include "base/time.h" -#ifdef OS_WIN -#include -#endif // OS_WIN - +//----------------------------------------------------------------------------- // Timer/TimerManager are objects designed to help setting timers. // Goals of TimerManager: -// - have only one Windows system timer for all app timer functionality +// - have only one system timer for all app timer functionality // - work around bugs with timers firing arbitrarily earlier than specified // - provide the ability to run timers even if the application is in a // windows modal app loop. +//----------------------------------------------------------------------------- +class MessageLoop; class TimerManager; class Task; -class MessageLoop; +//----------------------------------------------------------------------------- +// The core timer object. Use TimerManager to create and control timers. class Timer { public: Timer(int delay, Task* task, bool repeating); + // The task to be run when the timer fires. Task* task() const { return task_; } void set_task(Task* task) { task_ = task; } - int current_delay() 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. Implement ceiling by adding 999us prior to - // conversion to ms. - double delay = ceil((fire_time_ - Time::Now()).InMillisecondsF()); - return static_cast(delay); - } + // Returns the time in msec relative to now that the timer should fire. + int GetCurrentDelay() const; + // Returns the absolute time at which the timer should fire. const Time &fire_time() const { return fire_time_; } + // A repeating timer is a timer that is automatically scheduled to fire again + // after it fires. bool repeating() const { return repeating_; } // Update (or fill in) creation_time_, and calculate future fire_time_ based // on current time plus delay_. void Reset(); + // A unique identifier for this timer. int id() const { return timer_id_; } protected: @@ -93,9 +89,8 @@ class Timer { // Timer delay in milliseconds. int delay_; - // A monotonically increasing timer id. Used - // for ordering two timers which have the same - // timestamp in a FIFO manner. + // A monotonically increasing timer id. Used for ordering two timers which + // have the same timestamp in a FIFO manner. int timer_id_; // Whether or not this timer repeats. @@ -105,9 +100,11 @@ class Timer { // iteration started.) Time creation_time_; - DISALLOW_EVIL_CONSTRUCTORS(Timer); + DISALLOW_COPY_AND_ASSIGN(Timer); }; +//----------------------------------------------------------------------------- +// Used to implement TimerPQueue class TimerComparison { public: bool operator() (const Timer* t1, const Timer* t2) const { @@ -126,14 +123,14 @@ class TimerComparison { } }; +//----------------------------------------------------------------------------- // Subclass priority_queue to provide convenient access to removal from this // list. // // Terminology: The "pending" timer is the timer at the top of the queue, // i.e. the timer whose task needs to be Run next. -class TimerPQueue : public std::priority_queue, - TimerComparison> { +class TimerPQueue : + public std::priority_queue, TimerComparison> { public: // Removes |timer| from the queue. void RemoveTimer(Timer* timer); @@ -142,32 +139,26 @@ class TimerPQueue : public std::priority_queue