xref: /aosp_15_r20/external/cronet/base/timer/wall_clock_timer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_TIMER_WALL_CLOCK_TIMER_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_TIMER_WALL_CLOCK_TIMER_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
9*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h"
10*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/location.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/power_monitor/power_observer.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/time/default_clock.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
16*6777b538SAndroid Build Coastguard Worker #include "base/timer/timer.h"
17*6777b538SAndroid Build Coastguard Worker 
18*6777b538SAndroid Build Coastguard Worker namespace base {
19*6777b538SAndroid Build Coastguard Worker class Clock;
20*6777b538SAndroid Build Coastguard Worker class TickClock;
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker // WallClockTimer is based on OneShotTimer and provides a simple timer API
23*6777b538SAndroid Build Coastguard Worker // which is mostly similar to OneShotTimer's API. The main difference is that
24*6777b538SAndroid Build Coastguard Worker // WallClockTimer is using Time (which is system-dependent) to schedule task.
25*6777b538SAndroid Build Coastguard Worker // WallClockTimer calls you back once scheduled time has come.
26*6777b538SAndroid Build Coastguard Worker //
27*6777b538SAndroid Build Coastguard Worker // Comparison with OneShotTimer: WallClockTimer runs |user_task_| after |delay_|
28*6777b538SAndroid Build Coastguard Worker // expires according to usual time, while OneShotTimer runs |user_task_| after
29*6777b538SAndroid Build Coastguard Worker // |delay_| expires according to TimeTicks which may freeze on some platforms
30*6777b538SAndroid Build Coastguard Worker // when power suspends (desktop falls asleep). On platforms where TimeTicks
31*6777b538SAndroid Build Coastguard Worker // don't freeze, the WallClockTimer has the same behavior as OneShotTimer.
32*6777b538SAndroid Build Coastguard Worker //
33*6777b538SAndroid Build Coastguard Worker // The API is not thread safe. All methods must be called from the same
34*6777b538SAndroid Build Coastguard Worker // sequence (not necessarily the construction sequence), except for the
35*6777b538SAndroid Build Coastguard Worker // destructor.
36*6777b538SAndroid Build Coastguard Worker // - The destructor may be called from any sequence when the timer is not
37*6777b538SAndroid Build Coastguard Worker // running and there is no scheduled task active.
38*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT WallClockTimer : public PowerSuspendObserver {
39*6777b538SAndroid Build Coastguard Worker  public:
40*6777b538SAndroid Build Coastguard Worker   // Constructs a timer. Start() must be called later to start the timer.
41*6777b538SAndroid Build Coastguard Worker   // If |clock| is provided, it's used instead of
42*6777b538SAndroid Build Coastguard Worker   // DefaultClock::GetInstance() to calulate timer's delay. If |tick_clock|
43*6777b538SAndroid Build Coastguard Worker   // is provided, it's used instead of TimeTicks::Now() to get TimeTicks when
44*6777b538SAndroid Build Coastguard Worker   // scheduling tasks.
45*6777b538SAndroid Build Coastguard Worker   WallClockTimer();
46*6777b538SAndroid Build Coastguard Worker   WallClockTimer(const Clock* clock, const TickClock* tick_clock);
47*6777b538SAndroid Build Coastguard Worker   WallClockTimer(const WallClockTimer&) = delete;
48*6777b538SAndroid Build Coastguard Worker   WallClockTimer& operator=(const WallClockTimer&) = delete;
49*6777b538SAndroid Build Coastguard Worker 
50*6777b538SAndroid Build Coastguard Worker   ~WallClockTimer() override;
51*6777b538SAndroid Build Coastguard Worker 
52*6777b538SAndroid Build Coastguard Worker   // Starts the timer to run at the given |desired_run_time|. If the timer is
53*6777b538SAndroid Build Coastguard Worker   // already running, it will be replaced to call the given |user_task|.
54*6777b538SAndroid Build Coastguard Worker   virtual void Start(const Location& posted_from,
55*6777b538SAndroid Build Coastguard Worker                      Time desired_run_time,
56*6777b538SAndroid Build Coastguard Worker                      OnceClosure user_task);
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker   // Starts the timer to run at the given |desired_run_time|. If the timer is
59*6777b538SAndroid Build Coastguard Worker   // already running, it will be replaced to call a task formed from
60*6777b538SAndroid Build Coastguard Worker   // |receiver|->*|method|.
61*6777b538SAndroid Build Coastguard Worker   template <class Receiver>
Start(const Location & posted_from,Time desired_run_time,Receiver * receiver,void (Receiver::* method)())62*6777b538SAndroid Build Coastguard Worker   void Start(const Location& posted_from,
63*6777b538SAndroid Build Coastguard Worker              Time desired_run_time,
64*6777b538SAndroid Build Coastguard Worker              Receiver* receiver,
65*6777b538SAndroid Build Coastguard Worker              void (Receiver::*method)()) {
66*6777b538SAndroid Build Coastguard Worker     Start(posted_from, desired_run_time,
67*6777b538SAndroid Build Coastguard Worker           BindOnce(method, Unretained(receiver)));
68*6777b538SAndroid Build Coastguard Worker   }
69*6777b538SAndroid Build Coastguard Worker 
70*6777b538SAndroid Build Coastguard Worker   // Stops the timer. It is a no-op if the timer is not running.
71*6777b538SAndroid Build Coastguard Worker   void Stop();
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker   // Returns true if the timer is running.
74*6777b538SAndroid Build Coastguard Worker   bool IsRunning() const;
75*6777b538SAndroid Build Coastguard Worker 
76*6777b538SAndroid Build Coastguard Worker   // PowerSuspendObserver:
77*6777b538SAndroid Build Coastguard Worker   void OnResume() override;
78*6777b538SAndroid Build Coastguard Worker 
desired_run_time()79*6777b538SAndroid Build Coastguard Worker   Time desired_run_time() const { return desired_run_time_; }
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker  private:
82*6777b538SAndroid Build Coastguard Worker   void AddObserver();
83*6777b538SAndroid Build Coastguard Worker 
84*6777b538SAndroid Build Coastguard Worker   void RemoveObserver();
85*6777b538SAndroid Build Coastguard Worker 
86*6777b538SAndroid Build Coastguard Worker   // Actually run scheduled task
87*6777b538SAndroid Build Coastguard Worker   void RunUserTask();
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker   // Returns the current time count.
90*6777b538SAndroid Build Coastguard Worker   Time Now() const;
91*6777b538SAndroid Build Coastguard Worker 
92*6777b538SAndroid Build Coastguard Worker   bool observer_added_ = false;
93*6777b538SAndroid Build Coastguard Worker 
94*6777b538SAndroid Build Coastguard Worker   // Location in user code.
95*6777b538SAndroid Build Coastguard Worker   Location posted_from_;
96*6777b538SAndroid Build Coastguard Worker 
97*6777b538SAndroid Build Coastguard Worker   // The desired run time of |user_task_|.
98*6777b538SAndroid Build Coastguard Worker   Time desired_run_time_;
99*6777b538SAndroid Build Coastguard Worker 
100*6777b538SAndroid Build Coastguard Worker   OnceClosure user_task_;
101*6777b538SAndroid Build Coastguard Worker 
102*6777b538SAndroid Build Coastguard Worker   // Timer which should notify to run task in the period while system awake
103*6777b538SAndroid Build Coastguard Worker   OneShotTimer timer_;
104*6777b538SAndroid Build Coastguard Worker 
105*6777b538SAndroid Build Coastguard Worker   // The clock used to calculate the run time for scheduled tasks.
106*6777b538SAndroid Build Coastguard Worker   const raw_ptr<const Clock> clock_ = DefaultClock::GetInstance();
107*6777b538SAndroid Build Coastguard Worker };
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker }  // namespace base
110*6777b538SAndroid Build Coastguard Worker 
111*6777b538SAndroid Build Coastguard Worker #endif  // BASE_TIMER_WALL_CLOCK_TIMER_H_
112