// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // A "timer" takes care of invoking a callback in the future, once or // repeatedly. The callback is invoked: // - OneShotTimer: Once after a `TimeDelta` delay has elapsed. // - RetainingOneShotTimer: Same as OneShotTimer, but the callback is retained // after being executed, allowing another invocation to be scheduled with // Reset() without specifying the callback again. // - DeadlineTimer: Once at the specified `TimeTicks` time. // - RepeatingTimer: Repeatedly, with a specified `TimeDelta` delay before the // first invocation and between invocations. // - MetronomeTimer: Repeatedly, with a specified `TimeDelta` delay between the // beginning of each invocations such that a constant phase is respected. // (Retaining)OneShotTimer and RepeatingTimer automatically apply some leeway to // the delay whereas DeadlineTimer and MetronomeTimer allow more control over // the requested time. As a result, the former are generally more // power-efficient. // Prefer using (Retaining)OneShotTimer and RepeatingTimer because they // automatically apply some leeway to the delay which enables power-efficient // scheduling. // Scheduled invocations can be cancelled with Stop() or by deleting the // Timer. The latter makes it easy to ensure that an object is not accessed by a // Timer after it has been deleted: just make the Timer a member of the object // which receives Timer events (see example below). // // Sample RepeatingTimer usage: // // class MyClass { // public: // void StartDoingStuff() { // timer_.Start(FROM_HERE, base::Seconds(1), // this, &MyClass::DoStuff); // // Alternative form if the callback is not bound to `this` or // // requires arguments: // // timer_.Start(FROM_HERE, base::Seconds(1), // // base::BindRepeating(&MyFunction, 42)); // } // void StopDoingStuff() { // timer_.Stop(); // } // private: // void DoStuff() { // // This method is called every second to do stuff. // ... // } // base::RepeatingTimer timer_; // }; // // These APIs are not thread safe. When a method is called (except the // constructor), all further method calls must be on the same sequence until // Stop(). Once stopped, it may be destroyed or restarted on another sequence. // // By default, the scheduled tasks will be run on the same sequence that the // Timer was *started on*. To mock time in unit tests, some old tests used // SetTaskRunner() to schedule the delay on a test-controlled TaskRunner. The // modern and preferred approach to mock time is to use TaskEnvironment's // MOCK_TIME mode. #ifndef BASE_TIMER_TIMER_H_ #define BASE_TIMER_TIMER_H_ // IMPORTANT: If you change timer code, make sure that all tests (including // disabled ones) from timer_unittests.cc pass locally. Some are disabled // because they're flaky on the buildbot, but when you run them locally you // should be able to tell the difference. #include "base/base_export.h" #include "base/functional/bind.h" #include "base/functional/callback.h" #include "base/functional/callback_helpers.h" #include "base/location.h" #include "base/memory/raw_ptr.h" #include "base/sequence_checker.h" #include "base/task/delayed_task_handle.h" #include "base/task/sequenced_task_runner.h" #include "base/time/time.h" #include "base/types/strong_alias.h" namespace base { class TickClock; namespace internal { // This class wraps logic shared by all timers. class BASE_EXPORT TimerBase { … }; //----------------------------------------------------------------------------- // This class wraps logic shared by (Retaining)OneShotTimer and RepeatingTimer. class BASE_EXPORT DelayTimerBase : public TimerBase { … }; } // namespace internal //----------------------------------------------------------------------------- // A simple, one-shot timer. See usage notes at the top of the file. class BASE_EXPORT OneShotTimer : public internal::DelayTimerBase { … }; //----------------------------------------------------------------------------- // A simple, repeating timer. See usage notes at the top of the file. class BASE_EXPORT RepeatingTimer : public internal::DelayTimerBase { … }; //----------------------------------------------------------------------------- // A simple, one-shot timer with the retained |user_task| which is reused when // Reset() is invoked. See usage notes at the top of the file. class BASE_EXPORT RetainingOneShotTimer : public internal::DelayTimerBase { … }; //----------------------------------------------------------------------------- // A Delay timer is like The Button from Lost. Once started, you have to keep // calling Reset otherwise it will call the given method on the sequence it was // initially Reset() from. // // Once created, it is inactive until Reset is called. Once |delay| seconds have // passed since the last call to Reset, the callback is made. Once the callback // has been made, it's inactive until Reset is called again. // // If destroyed, the timeout is canceled and will not occur even if already // inflight. class DelayTimer { … }; //----------------------------------------------------------------------------- // A one-shot timer that attempts to run |user_task| some time near specified // deadline. See usage notes at the top of the file. class BASE_EXPORT DeadlineTimer : public internal::TimerBase { … }; //----------------------------------------------------------------------------- // Repeatedly invokes a callback, waiting for a precise delay between the // beginning of each invocation. See usage notes at the top of the file. class BASE_EXPORT MetronomeTimer : public internal::TimerBase { … }; } // namespace base #endif // BASE_TIMER_TIMER_H_