OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROMECAST_BASE_ALARM_MANAGER_H_ | |
6 #define CHROMECAST_BASE_ALARM_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <queue> | |
10 #include <vector> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/threading/thread.h" | |
17 #include "base/time/clock.h" | |
18 #include "base/timer/timer.h" | |
19 | |
20 namespace chromecast { | |
21 | |
22 // Alarm manager allows setting a task for wall clock time rather than for an | |
23 // elapsed amount of time. This is different from using long PostDelayedTasks | |
24 // that are sensitive to time changes, clock drift, and other factors. | |
25 // | |
26 // Alarm manager polls the wall clock time every 5 seconds. If the clock is | |
27 // equal or past the requested time, the alarm will fire. | |
28 // | |
29 // Any thread can add or remove alarms. The alarm will be fired on the original | |
30 // thread used to set the alarm. | |
31 // | |
32 // When an alarm is added to the alarm manager, the task is guaranteed to not | |
33 // run before the clock passes the requested time. The task may not run even if | |
34 // it is past the requested time if the software is suspended. However, once | |
35 // woken up, the event will fire within 5 seconds if the target time has passed. | |
36 class AlarmManager { | |
37 public: | |
38 class AlarmDelegate { | |
halliwell
2017/02/20 22:28:26
I wonder if it would be less intrusive for the cal
ryanchung
2017/02/21 23:36:47
Done.
I'm wondering if there's a need to explicitl
| |
39 public: | |
40 // Called when the alarm fires. | |
41 virtual void OnAlarmFire() = 0; | |
42 | |
43 protected: | |
44 virtual ~AlarmDelegate() {} | |
45 }; | |
46 | |
47 // Construct and start the alarm manager. | |
halliwell
2017/02/20 22:28:26
Is this intended to be some kind of singleton? Wh
ryanchung
2017/02/21 23:36:47
Yes, this will be some sort of singleton instantia
| |
48 AlarmManager(); | |
49 ~AlarmManager(); | |
50 | |
51 // For testing only. Allows setting a fake clock. | |
52 AlarmManager(std::unique_ptr<base::Clock> clock); | |
53 | |
54 // Add an alarm. | |
55 // OnAlarmFire() will be called on |delegate| at around |time|. | |
56 void AddAlarm(AlarmDelegate* delegate, base::Time time); | |
57 | |
58 // Remove an alarm. | |
59 void RemoveAlarm(AlarmDelegate* delegate); | |
60 | |
61 private: | |
62 class AlarmInfo { | |
63 public: | |
64 AlarmInfo(base::Time time, | |
65 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
66 ~AlarmInfo(); | |
67 base::Time time_; | |
68 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
69 | |
70 private: | |
71 DISALLOW_COPY_AND_ASSIGN(AlarmInfo); | |
72 }; | |
73 | |
74 typedef std::map<AlarmDelegate*, std::unique_ptr<AlarmInfo>> AlarmMap; | |
halliwell
2017/02/20 22:28:26
nit: 'using' preferred over typedef now.
ryanchung
2017/02/21 23:36:47
Done.
| |
75 | |
76 // Check if an alarm should fire. | |
77 void CheckAlarm(); | |
78 // Start the poller. | |
79 void Start(); | |
halliwell
2017/02/20 22:28:26
nit: seems like 'Start' code could be inlined in c
ryanchung
2017/02/21 23:36:47
Done.
| |
80 // Stop the poller. | |
81 void Stop(); | |
halliwell
2017/02/20 22:28:26
nit: could inline this code in dtor.
ryanchung
2017/02/21 23:36:47
Done.
| |
82 | |
83 base::Lock alarms_lock_; | |
halliwell
2017/02/20 22:28:26
Is there a strong reason to use a lock (vs posting
ryanchung
2017/02/21 23:36:47
Done. Posting is better. Updated code.
| |
84 std::unique_ptr<base::Clock> clock_; | |
85 std::unique_ptr<base::Timer> clock_tick_timer_; | |
86 // Store a list of the alarms to fire at a certain time. | |
87 AlarmMap alarms_; | |
88 // Store a list of times when a timer should fire. | |
89 std::priority_queue<base::Time, | |
90 std::vector<base::Time>, | |
91 std::greater<base::Time>> | |
92 next_alarm_; | |
93 | |
94 base::WeakPtrFactory<AlarmManager> weak_factory_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | |
97 }; | |
98 | |
99 } // chromecast | |
halliwell
2017/02/20 22:28:26
nit: "namespace chromecast"
ryanchung
2017/02/21 23:36:46
Done.
| |
100 | |
101 #endif // CHROMECAST_BASE_ALARM_MANAGER_H_ | |
OLD | NEW |