| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names | 5 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names |
| 6 // suggest, OneShotTimer calls you back once after a time delay expires. | 6 // suggest, OneShotTimer calls you back once after a time delay expires. |
| 7 // RepeatingTimer on the other hand calls you back periodically with the | 7 // RepeatingTimer on the other hand calls you back periodically with the |
| 8 // prescribed time interval. | 8 // prescribed time interval. |
| 9 // | 9 // |
| 10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of | 10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // } | 30 // } |
| 31 // base::RepeatingTimer<MyClass> timer_; | 31 // base::RepeatingTimer<MyClass> timer_; |
| 32 // }; | 32 // }; |
| 33 // | 33 // |
| 34 // Both OneShotTimer and RepeatingTimer also support a Reset method, which | 34 // Both OneShotTimer and RepeatingTimer also support a Reset method, which |
| 35 // allows you to easily defer the timer event until the timer delay passes once | 35 // allows you to easily defer the timer event until the timer delay passes once |
| 36 // again. So, in the above example, if 0.5 seconds have already passed, | 36 // again. So, in the above example, if 0.5 seconds have already passed, |
| 37 // calling Reset on timer_ would postpone DoStuff by another 1 second. In | 37 // calling Reset on timer_ would postpone DoStuff by another 1 second. In |
| 38 // other words, Reset is shorthand for calling Stop and then Start again with | 38 // other words, Reset is shorthand for calling Stop and then Start again with |
| 39 // the same arguments. | 39 // the same arguments. |
| 40 // | |
| 41 // NOTE: The older TimerManager / Timer API is deprecated. New code should | |
| 42 // use OneShotTimer or RepeatingTimer. | |
| 43 | 40 |
| 44 #ifndef BASE_TIMER_H_ | 41 #ifndef BASE_TIMER_H_ |
| 45 #define BASE_TIMER_H_ | 42 #define BASE_TIMER_H_ |
| 46 | 43 |
| 47 #include <queue> | |
| 48 #include <vector> | |
| 49 | |
| 50 #include "base/basictypes.h" | |
| 51 #include "base/task.h" | 44 #include "base/task.h" |
| 52 #include "base/time.h" | 45 #include "base/time.h" |
| 53 | 46 |
| 54 //----------------------------------------------------------------------------- | |
| 55 // Timer/TimerManager are objects designed to help setting timers. | |
| 56 // Goals of TimerManager: | |
| 57 // - have only one system timer for all app timer functionality | |
| 58 // - work around bugs with timers firing arbitrarily earlier than specified | |
| 59 // - provide the ability to run timers even if the application is in a | |
| 60 // windows modal app loop. | |
| 61 //----------------------------------------------------------------------------- | |
| 62 | |
| 63 class MessageLoop; | 47 class MessageLoop; |
| 64 | 48 |
| 65 namespace base { | 49 namespace base { |
| 66 | 50 |
| 67 class TimerManager; | |
| 68 | |
| 69 //----------------------------------------------------------------------------- | |
| 70 // The core timer object. Use TimerManager to create and control timers. | |
| 71 // | |
| 72 // NOTE: This class is DEPRECATED. Do not use! | |
| 73 class Timer { | |
| 74 public: | |
| 75 Timer(int delay, Task* task, bool repeating); | |
| 76 | |
| 77 // For one-shot timers, you can also specify the exact fire time. | |
| 78 Timer(Time fire_time, Task* task); | |
| 79 | |
| 80 // The task to be run when the timer fires. | |
| 81 Task* task() const { return task_; } | |
| 82 void set_task(Task* task) { task_ = task; } | |
| 83 | |
| 84 // Returns the absolute time at which the timer should fire. | |
| 85 const Time &fire_time() const { return fire_time_; } | |
| 86 | |
| 87 // A repeating timer is a timer that is automatically scheduled to fire again | |
| 88 // after it fires. | |
| 89 bool repeating() const { return repeating_; } | |
| 90 | |
| 91 // Update (or fill in) creation_time_, and calculate future fire_time_ based | |
| 92 // on current time plus delay_. | |
| 93 void Reset(); | |
| 94 | |
| 95 // A unique identifier for this timer. | |
| 96 int id() const { return timer_id_; } | |
| 97 | |
| 98 protected: | |
| 99 // Protected (rather than private) so that we can access from unit tests. | |
| 100 | |
| 101 // The time when the timer should fire. | |
| 102 Time fire_time_; | |
| 103 | |
| 104 private: | |
| 105 // The task that is run when this timer fires. | |
| 106 Task* task_; | |
| 107 | |
| 108 // Timer delay in milliseconds. | |
| 109 int delay_; | |
| 110 | |
| 111 // A monotonically increasing timer id. Used for ordering two timers which | |
| 112 // have the same timestamp in a FIFO manner. | |
| 113 int timer_id_; | |
| 114 | |
| 115 // Whether or not this timer repeats. | |
| 116 const bool repeating_; | |
| 117 | |
| 118 // The tick count when the timer was "created". (i.e. when its current | |
| 119 // iteration started.) | |
| 120 Time creation_time_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(Timer); | |
| 123 }; | |
| 124 | |
| 125 //----------------------------------------------------------------------------- | |
| 126 // Used to implement TimerPQueue | |
| 127 // | |
| 128 // NOTE: This class is DEPRECATED. Do not use! | |
| 129 class TimerComparison { | |
| 130 public: | |
| 131 bool operator() (const Timer* t1, const Timer* t2) const { | |
| 132 const Time& f1 = t1->fire_time(); | |
| 133 const Time& f2 = t2->fire_time(); | |
| 134 // If the two timers have the same delay, revert to using | |
| 135 // the timer_id to maintain FIFO ordering. | |
| 136 if (f1 == f2) { | |
| 137 // Gracefully handle wrap as we try to return (t1->id() > t2->id()); | |
| 138 int delta = t1->id() - t2->id(); | |
| 139 // Assuming the delta is smaller than 2**31, we'll always get the right | |
| 140 // answer (in terms of sign of delta). | |
| 141 return delta > 0; | |
| 142 } | |
| 143 return f1 > f2; | |
| 144 } | |
| 145 }; | |
| 146 | |
| 147 //----------------------------------------------------------------------------- | |
| 148 // Subclass priority_queue to provide convenient access to removal from this | |
| 149 // list. | |
| 150 // | |
| 151 // Terminology: The "pending" timer is the timer at the top of the queue, | |
| 152 // i.e. the timer whose task needs to be Run next. | |
| 153 // | |
| 154 // NOTE: This class is DEPRECATED. Do not use! | |
| 155 class TimerPQueue : | |
| 156 public std::priority_queue<Timer*, std::vector<Timer*>, TimerComparison> { | |
| 157 public: | |
| 158 // Removes |timer| from the queue. | |
| 159 void RemoveTimer(Timer* timer); | |
| 160 | |
| 161 // Returns true if the queue contains |timer|. | |
| 162 bool ContainsTimer(const Timer* timer) const; | |
| 163 }; | |
| 164 | |
| 165 //----------------------------------------------------------------------------- | |
| 166 // There is one TimerManager per thread, owned by the MessageLoop. Timers can | |
| 167 // either be fired by the MessageLoop from within its run loop or via a system | |
| 168 // timer event that the MesssageLoop constructs. The advantage of the former | |
| 169 // is that we can make timers fire significantly faster than the granularity | |
| 170 // provided by the system. The advantage of a system timer is that modal | |
| 171 // message loops which don't run our MessageLoop code will still be able to | |
| 172 // process system timer events. | |
| 173 // | |
| 174 // NOTE: TimerManager is not thread safe. You cannot set timers onto a thread | |
| 175 // other than your own. | |
| 176 // | |
| 177 // NOTE: This class is DEPRECATED. Do not use! | |
| 178 class TimerManager { | |
| 179 public: | |
| 180 explicit TimerManager(MessageLoop* message_loop); | |
| 181 ~TimerManager(); | |
| 182 | |
| 183 // Create and start a new timer. |task| is owned by the caller, as is the | |
| 184 // timer object that is returned. | |
| 185 Timer* StartTimer(int delay, Task* task, bool repeating); | |
| 186 | |
| 187 // Starts a timer. This is a no-op if the timer is already started. | |
| 188 void StartTimer(Timer* timer); | |
| 189 | |
| 190 // Stop a timer. This is a no-op if the timer is already stopped. | |
| 191 void StopTimer(Timer* timer); | |
| 192 | |
| 193 // Reset an existing timer, which may or may not be currently in the queue of | |
| 194 // upcoming timers. The timer's parameters are unchanged; it simply begins | |
| 195 // counting down again as if it was just created. | |
| 196 void ResetTimer(Timer* timer); | |
| 197 | |
| 198 // Returns true if |timer| is in the queue of upcoming timers. | |
| 199 bool IsTimerRunning(const Timer* timer) const; | |
| 200 | |
| 201 // Run some small number of timers. | |
| 202 // Returns true if it runs a task, false otherwise. | |
| 203 bool RunSomePendingTimers(); | |
| 204 | |
| 205 // The absolute time at which the next timer is to fire. If there is not a | |
| 206 // next timer to run, then the is_null property of the returned Time object | |
| 207 // will be true. NOTE: This could be a time in the past! | |
| 208 Time GetNextFireTime() const; | |
| 209 | |
| 210 #ifdef UNIT_TEST | |
| 211 // For testing only, used to simulate broken early-firing WM_TIMER | |
| 212 // notifications by setting arbitrarily small delays in SetTimer. | |
| 213 void set_use_broken_delay(bool use_broken_delay) { | |
| 214 use_broken_delay_ = use_broken_delay; | |
| 215 } | |
| 216 #endif // UNIT_TEST | |
| 217 | |
| 218 bool use_broken_delay() const { | |
| 219 return use_broken_delay_; | |
| 220 } | |
| 221 | |
| 222 protected: | |
| 223 // Peek at the timer which will fire soonest. | |
| 224 Timer* PeekTopTimer(); | |
| 225 | |
| 226 private: | |
| 227 void DidChangeNextTimer(); | |
| 228 | |
| 229 // A cached value that indicates the time when we think the next timer is to | |
| 230 // fire. We use this to determine if we should call DidChangeNextTimerExpiry | |
| 231 // on the MessageLoop. | |
| 232 Time next_timer_expiry_; | |
| 233 | |
| 234 TimerPQueue timers_; | |
| 235 | |
| 236 bool use_broken_delay_; | |
| 237 | |
| 238 // A lazily cached copy of MessageLoop::current. | |
| 239 MessageLoop* message_loop_; | |
| 240 | |
| 241 DISALLOW_COPY_AND_ASSIGN(TimerManager); | |
| 242 }; | |
| 243 | |
| 244 //----------------------------------------------------------------------------- | 51 //----------------------------------------------------------------------------- |
| 245 // This class is an implementation detail of OneShotTimer and RepeatingTimer. | 52 // This class is an implementation detail of OneShotTimer and RepeatingTimer. |
| 246 // Please do not use this class directly. | 53 // Please do not use this class directly. |
| 247 // | 54 // |
| 248 // This class exists to share code between BaseTimer<T> template instantiations. | 55 // This class exists to share code between BaseTimer<T> template instantiations. |
| 249 // | 56 // |
| 250 class BaseTimer_Helper { | 57 class BaseTimer_Helper { |
| 251 public: | 58 public: |
| 252 // Stops the timer. | 59 // Stops the timer. |
| 253 ~BaseTimer_Helper() { | 60 ~BaseTimer_Helper() { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 template <class Receiver> | 153 template <class Receiver> |
| 347 class OneShotTimer : public BaseTimer<Receiver, false> {}; | 154 class OneShotTimer : public BaseTimer<Receiver, false> {}; |
| 348 | 155 |
| 349 //----------------------------------------------------------------------------- | 156 //----------------------------------------------------------------------------- |
| 350 // A simple, repeating timer. See usage notes at the top of the file. | 157 // A simple, repeating timer. See usage notes at the top of the file. |
| 351 template <class Receiver> | 158 template <class Receiver> |
| 352 class RepeatingTimer : public BaseTimer<Receiver, true> {}; | 159 class RepeatingTimer : public BaseTimer<Receiver, true> {}; |
| 353 | 160 |
| 354 } // namespace base | 161 } // namespace base |
| 355 | 162 |
| 356 // TODO(darin): b/1346553: Remove these once Timer and TimerManager are unused. | |
| 357 using base::Timer; | |
| 358 using base::TimerManager; | |
| 359 | |
| 360 #endif // BASE_TIMER_H_ | 163 #endif // BASE_TIMER_H_ |
| OLD | NEW |