| 6 |
| // API. As the names suggest, OneShotTimer calls you back once after a time |
| 7 | // delay expires. |
| 8 | // RepeatingTimer on the other hand calls you back periodically with the |
| 9 | // prescribed time interval. |
| 10 | // RetainingOneShotTimer doesn't repeat the task itself like RepeatingTimer, but |
| 11 | // retains the given task after the time out. You can restart it with Reset |
| 12 | // again without giving new task to Start. |
| 13 | // |
| 14 | // All of OneShotTimer, RepeatingTimer and RetainingOneShotTimer cancel the |
| 15 | // timer when they go out of scope, which makes it easy to ensure that you do |
| 16 | // not get called when your object has gone out of scope. Just instantiate a |
| 17 | // timer as a member variable of the class for which you wish to receive timer |
| 18 | // events. |
| 19 | // |
| 20 | // Sample RepeatingTimer usage: |
| 21 | // |
| 22 | // class MyClass { |
| 23 | // public: |
| 24 | // void StartDoingStuff() { |
| 25 | // timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1), |
| 26 | // this, &MyClass::DoStuff); |
| 27 | // } |
| 28 | // void StopDoingStuff() { |
| 29 | // timer_.Stop(); |
| 30 | // } |
| 31 | // private: |
| 32 | // void DoStuff() { |
| 33 | // // This method is called every second to do stuff. |
| 34 | // ... |
| 35 | // } |
| 36 | // base::RepeatingTimer timer_; |
| 37 | // }; |
| 38 | // |
| 39 | // Timers also support a Reset method, which allows you to easily defer the |
| 40 | // timer event until the timer delay passes once again. So, in the above |
| 41 | // example, if 0.5 seconds have already passed, calling Reset on |timer_| |
| 42 | // would postpone DoStuff by another 1 second. In other words, Reset is |
| 43 | // shorthand for calling Stop and then Start again with the same arguments. |
| 44 | // |
| 45 | // These APIs are not thread safe. When a method is called (except the |
| 46 | // constructor), all further method calls must be on the same sequence until |
| 47 | // Stop(). |
| 48 | // |
| 49 | // By default, the scheduled tasks will be run on the same sequence that the |
| 50 | // Timer was *started on*. To mock time in unit tests, some old tests used |
| 51 | // SetTaskRunner() to schedule the delay on a test-controlled TaskRunner. The |
| 52 | // modern and preferred approach to mock time is to use TaskEnvironment's |
| 53 | // MOCK_TIME mode. |
| 54 |
| 55 | #ifndef BASE_TIMER_TIMER_H_ |
| 56 | #define BASE_TIMER_TIMER_H_ |
| 57 |
| 58 | // IMPORTANT: If you change timer code, make sure that all tests (including |
| 59 | // disabled ones) from timer_unittests.cc pass locally. Some are disabled |
| 60 | // because they're flaky on the buildbot, but when you run them locally you |
| 61 | // should be able to tell the difference. |
| 62 |
| 63 | #include <memory> |
| 64 |
| 65 | #include "base/base_export.h" |
| 66 | #include "base/bind.h" |
| 67 | #include "base/bind_helpers.h" |
| 68 | #include "base/callback.h" |
| 69 | #include "base/location.h" |
| 70 | #include "base/macros.h" |
| 71 | #include "base/sequence_checker_impl.h" |
| 72 | #include "base/sequenced_task_runner.h" |
| 73 | #include "base/time/time.h" |
| 74 |
| 75 | namespace base { |
| 76 |
| 77 | class TickClock; |
| 78 |
| 79 | namespace internal { |
| 80 |
| 81 | class BaseTimerTaskInternal; |
| 82 |
| 83 | //----------------------------------------------------------------------------- |
| 84 | // This class wraps TaskRunner::PostDelayedTask to manage delayed and repeating |
| 85 | // tasks. See meta comment above for thread-safety requirements. |
| 86 | // Do not use this class directly. Use one of OneShotTimer, RepeatingTimer or |
| 87 | // RetainingOneShotTimer. |
| 88 | // |
| 89 | class BASE_EXPORT TimerBase { |
| 90 | public: |
| 91 | // Constructs a timer. Start must be called later to set task info. |
| 92 | // If |tick_clock| is provided, it is used instead of TimeTicks::Now() to get |
| 93 | // TimeTicks when scheduling tasks. |
| 94 | TimerBase(); |
| 95 | explicit TimerBase(const TickClock* tick_clock); |
| 96 |
| 97 | // Construct a timer with task info. |
| 98 | // If |tick_clock| is provided, it is used instead of TimeTicks::Now() to get |
| 99 | // TimeTicks when scheduling tasks. |
| 100 | TimerBase(const Location& posted_from, TimeDelta delay); |
| 101 | TimerBase(const Location& posted_from, |
| 102 | TimeDelta delay, |
| 103 | const TickClock* tick_clock); |
| 104 |
| 105 | virtual ~TimerBase(); |
| 106 |
| 107 | // Returns true if the timer is running (i.e., not stopped). |
| 108 | bool IsRunning() const; |
| 109 |
| 110 | // Returns the current delay for this timer. |
| 111 | TimeDelta GetCurrentDelay() const; |
| 112 |
| 113 | // Sets the task runner on which the delayed task should be scheduled when |
| 114 | // this Timer is running. This method can only be called while this Timer |
| 115 | // isn't running. This is an alternative (old) approach to mock time in tests. |
| 116 | // The modern and preferred approach is to use |
| 117 | // TaskEnvironment::TimeSource::MOCK_TIME. To avoid racy usage of Timer, |
| 118 | // |task_runner| must run tasks on the same sequence which this Timer is bound |
| 119 | // to (started from). TODO(gab): Migrate all callers to |
| 120 | // TaskEnvironment::TimeSource::MOCK_TIME. |
| 121 | virtual void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner); |
| 122 |
| 123 | // Call this method to stop and cancel the timer. It is a no-op if the timer |
| 124 | // is not running. |
| 125 | virtual void Stop(); |
| 126 |
| 127 | // Stop running task (if any) and abandon scheduled task (if any). |
| 128 | void AbandonAndStop() { |
| 129 | AbandonScheduledTask(); |
| 130 |
| 131 | Stop(); |
| 132 | // No more member accesses here: |this| could be deleted at this point. |
| 133 | } |
| 134 |
| 135 | // Call this method to reset the timer delay. The user task must be set. If |
| 136 | // the timer is not running, this will start it by posting a task. |
| 137 | virtual void Reset(); |
| 138 |
| 139 | const TimeTicks& desired_run_time() const { return desired_run_time_; } |
| 140 |
| 141 | protected: |
| 142 | virtual void OnStop() = 0; |
| 143 | virtual void RunUserTask() = 0; |
| 144 |
| 145 | // Returns the current tick count. |
| 146 | TimeTicks Now() const; |
| 147 |
| 148 | void set_desired_run_time(TimeTicks desired) { desired_run_time_ = desired; } |
| 149 | void set_is_running(bool running) { is_running_ = running; } |
| 150 |
| 151 | const Location& posted_from() const { return posted_from_; } |
| 152 |
| 153 | // The task runner on which the task should be scheduled. If it is null, the |
| 154 | // task runner for the current sequence will be used. |
| 155 | scoped_refptr<SequencedTaskRunner> task_runner_; |
| 156 |
| 157 | // Timer isn't thread-safe and must only be used on its origin sequence |
| 158 | // (sequence on which it was started). Once fully Stop()'ed it may be |
| 159 | // destroyed or restarted on another sequence. |
| 160 | SequenceChecker origin_sequence_checker_; |
| 161 |
| 162 | // Allocates a new |scheduled_task_| and posts it on the current sequence with |
| 163 | // the given |delay|. |scheduled_task_| must be null. |scheduled_run_time_| |
| 164 | // and |desired_run_time_| are reset to Now() + delay. |
| 165 | void PostNewScheduledTask(TimeDelta delay); |
| 166 |
| 167 | void StartInternal(const Location& posted_from, TimeDelta delay); |
| 168 |
| 169 | private: |
| 170 | friend class BaseTimerTaskInternal; |
| 171 |
| 172 | // Returns the task runner on which the task should be scheduled. If the |
| 173 | // corresponding |task_runner_| field is null, the task runner for the current |
| 174 | // sequence is returned. |
| 175 | scoped_refptr<SequencedTaskRunner> GetTaskRunner(); |
| 176 |
| 177 | // Disable |scheduled_task_| and abandon it so that it no longer refers back |
| 178 | // to this object. |
| 179 | void AbandonScheduledTask(); |
| 180 |
| 181 | // Called by BaseTimerTaskInternal when the delayed task fires. |
| 182 | void RunScheduledTask(); |
| 183 |
| 184 | // When non-null, the |scheduled_task_| was posted to call RunScheduledTask() |
| 185 | // at |scheduled_run_time_|. |
| 186 | BaseTimerTaskInternal* scheduled_task_; |
| 187 |
| 188 | // Location in user code. |
| 189 | Location posted_from_; |
| 190 | // Delay requested by user. |
| 191 | TimeDelta delay_; |
| 192 |
| 193 | // The time at which |scheduled_task_| is expected to fire. This time can be a |
| 194 | // "zero" TimeTicks if the task must be run immediately. |
| 195 | TimeTicks scheduled_run_time_; |
| 196 |
| 197 | // The desired run time of |user_task_|. The user may update this at any time, |
| 198 | // even if their previous request has not run yet. If |desired_run_time_| is |
| 199 | // greater than |scheduled_run_time_|, a continuation task will be posted to |
| 200 | // wait for the remaining time. This allows us to reuse the pending task so as |
| 201 | // not to flood the delayed queues with orphaned tasks when the user code |
| 202 | // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks |
| 203 | // if the task must be run immediately. |
| 204 | TimeTicks desired_run_time_; |
| 205 |
| 206 | // The tick clock used to calculate the run time for scheduled tasks. |
| 207 | const TickClock* const tick_clock_; |
| 208 |
| 209 | // If true, |user_task_| is scheduled to run sometime in the future. |
| 210 | bool is_running_; |
| 211 |
| 212 | DISALLOW_COPY_AND_ASSIGN(TimerBase); |
| 213 | }; |
| 214 |
| 215 | } // namespace internal |
| 216 |
| 217 | //----------------------------------------------------------------------------- |
| 218 | // A simple, one-shot timer. See usage notes at the top of the file. |
| 219 | class BASE_EXPORT OneShotTimer : public internal::TimerBase { |
| 220 | public: |
| 221 | OneShotTimer(); |
| 222 | explicit OneShotTimer(const TickClock* tick_clock); |
| 223 | ~OneShotTimer() override; |
| 224 |
| 225 | // Start the timer to run at the given |delay| from now. If the timer is |
| 226 | // already running, it will be replaced to call the given |user_task|. |
| 227 | virtual void Start(const Location& posted_from, |
| 228 | TimeDelta delay, |
| 229 | OnceClosure user_task); |
| 230 |
| 231 | // Start the timer to run at the given |delay| from now. If the timer is |
| 232 | // already running, it will be replaced to call a task formed from |
| 233 | // |receiver->*method|. |
| 234 | template <class Receiver> |
| 235 | void Start(const Location& posted_from, |
| 236 | TimeDelta delay, |
| 237 | Receiver* receiver, |
| 238 | void (Receiver::*method)()) { |
| 239 | Start(posted_from, delay, BindOnce(method, Unretained(receiver))); |
| 240 | } |
| 241 |
| 242 | // Run the scheduled task immediately, and stop the timer. The timer needs to |
| 243 | // be running. |
| 244 | void FireNow(); |
| 245 |
| 246 | private: |
| 247 | void OnStop() final; |
| 248 | void RunUserTask() final; |
| 249 |
| 250 | OnceClosure user_task_; |
| 251 |
| 252 | DISALLOW_COPY_AND_ASSIGN(OneShotTimer); |
| 253 | }; |
| 254 |
| 255 | //----------------------------------------------------------------------------- |
| 256 | // A simple, repeating timer. See usage notes at the top of the file. |
| 257 | class BASE_EXPORT RepeatingTimer : public internal::TimerBase { |
| 258 | public: |
| 259 | RepeatingTimer(); |
| 260 | explicit RepeatingTimer(const TickClock* tick_clock); |
| 261 | ~RepeatingTimer() override; |
| 262 |
| 263 | RepeatingTimer(const Location& posted_from, |
| 264 | TimeDelta delay, |
| 265 | RepeatingClosure user_task); |
| 266 | RepeatingTimer(const Location& posted_from, |
| 267 | TimeDelta delay, |
| 268 | RepeatingClosure user_task, |
| 269 | const TickClock* tick_clock); |
| 270 |
| 271 | // Start the timer to run at the given |delay| from now. If the timer is |
| 272 | // already running, it will be replaced to call the given |user_task|. |
| 273 | virtual void Start(const Location& posted_from, |
| 274 | TimeDelta delay, |
| 275 | RepeatingClosure user_task); |
| 276 |
| 277 | // Start the timer to run at the given |delay| from now. If the timer is |
| 278 | // already running, it will be replaced to call a task formed from |
| 279 | // |receiver->*method|. |
| 280 | template <class Receiver> |
| 281 | void Start(const Location& posted_from, |
| 282 | TimeDelta delay, |
| 283 | Receiver* receiver, |
| 284 | void (Receiver::*method)()) { |
| 285 | Start(posted_from, delay, BindRepeating(method, Unretained(receiver))); |
| 286 | } |
| 287 |
| 288 | const RepeatingClosure& user_task() const { return user_task_; } |
| 289 |
| 290 | private: |
| 291 | // Mark this final, so that the destructor can call this safely. |
| 292 | void OnStop() final; |
| 293 |
| 294 | void RunUserTask() override; |
| 295 |
| 296 | RepeatingClosure user_task_; |
| 297 |
| 298 | DISALLOW_COPY_AND_ASSIGN(RepeatingTimer); |
| 299 | }; |
| 300 |
| 301 | //----------------------------------------------------------------------------- |
| 302 | // A simple, one-shot timer with the retained user_task which is reused for |
| 303 | // multiple invocations of Start(). See usage notes at the top of the file. |
| 304 | class BASE_EXPORT RetainingOneShotTimer : public internal::TimerBase { |
| 305 | public: |
| 306 | RetainingOneShotTimer(); |
| 307 | explicit RetainingOneShotTimer(const TickClock* tick_clock); |
| 308 | ~RetainingOneShotTimer() override; |
| 309 |
| 310 | RetainingOneShotTimer(const Location& posted_from, |
| 311 | TimeDelta delay, |
| 312 | RepeatingClosure user_task); |
| 313 | RetainingOneShotTimer(const Location& posted_from, |
| 314 | TimeDelta delay, |
| 315 | RepeatingClosure user_task, |
| 316 | const TickClock* tick_clock); |
| 317 |
| 318 | // Start the timer to run at the given |delay| from now. If the timer is |
| 319 | // already running, it will be replaced to call the given |user_task|. |
| 320 | virtual void Start(const Location& posted_from, |
| 321 | TimeDelta delay, |
| 322 | RepeatingClosure user_task); |
| 323 |
| 324 | // Start the timer to run at the given |delay| from now. If the timer is |
| 325 | // already running, it will be replaced to call a task formed from |
| 326 | // |receiver->*method|. |
| 327 | template <class Receiver> |
| 328 | void Start(const Location& posted_from, |
| 329 | TimeDelta delay, |
| 330 | Receiver* receiver, |
| 331 | void (Receiver::*method)()) { |
| 332 | Start(posted_from, delay, BindRepeating(method, Unretained(receiver))); |
| 333 | } |
| 334 |
| 335 | const RepeatingClosure& user_task() const { return user_task_; } |
| 336 |
| 337 | protected: |
| 338 | void set_user_task(const RepeatingClosure& task) { user_task_ = task; } |
| 339 |
| 340 | private: |
| 341 | // Mark this final, so that the destructor can call this safely. |
| 342 | void OnStop() final; |
| 343 |
| 344 | void RunUserTask() override; |
| 345 |
| 346 | RepeatingClosure user_task_; |
| 347 |
| 348 | DISALLOW_COPY_AND_ASSIGN(RetainingOneShotTimer); |
| 349 | }; |
| 350 |
| 351 | //----------------------------------------------------------------------------- |
| 352 | // A Delay timer is like The Button from Lost. Once started, you have to keep |
| 353 | // calling Reset otherwise it will call the given method on the sequence it was |
| 354 | // initially Reset() from. |
| 355 | // |
| 356 | // Once created, it is inactive until Reset is called. Once |delay| seconds have |
| 357 | // passed since the last call to Reset, the callback is made. Once the callback |
| 358 | // has been made, it's inactive until Reset is called again. |
| 359 | // |
| 360 | // If destroyed, the timeout is canceled and will not occur even if already |
| 361 | // inflight. |
| 362 | class DelayTimer { |
| 363 | public: |
| 364 | template <class Receiver> |
| 365 | DelayTimer(const Location& posted_from, |
| 366 | TimeDelta delay, |
| 367 | Receiver* receiver, |
| 368 | void (Receiver::*method)()) |
| 369 | : DelayTimer(posted_from, delay, receiver, method, nullptr) {} |
| 370 |
| 371 | template <class Receiver> |
| 372 | DelayTimer(const Location& posted_from, |
| 373 | TimeDelta delay, |
| 374 | Receiver* receiver, |
| 375 | void (Receiver::*method)(), |
| 376 | const TickClock* tick_clock) |
| 377 | : timer_(posted_from, |
| 378 | delay, |
| 379 | BindRepeating(method, Unretained(receiver)), |
| 380 | tick_clock) {} |
| 381 |
| 382 | void Reset() { timer_.Reset(); } |
| 383 |
| 384 | private: |
| 385 | RetainingOneShotTimer timer_; |
| 386 |
| 387 | DISALLOW_COPY_AND_ASSIGN(DelayTimer); |
| 388 | }; |
| 389 |
| 390 | } // namespace base |
| 391 |
| 392 | #endif // BASE_TIMER_TIMER_H_ |
| 393 |