Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chromecast/base/alarm_manager.h

Issue 2695223008: [Chromecast] Add an alarm manager for firing events on wall clock time. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromecast/base/BUILD.gn ('k') | chromecast/base/alarm_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_
OLDNEW
« no previous file with comments | « chromecast/base/BUILD.gn ('k') | chromecast/base/alarm_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698