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

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: Use caller's thread Created 3 years, 9 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
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 <queue>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15
16 namespace base {
17 class Clock;
18 class SingleThreadTaskRunner;
19 class Timer;
20 }
21
22 namespace chromecast {
23
24 // Alarm manager allows setting a task for wall clock time rather than for an
25 // elapsed amount of time. This is different from using long PostDelayedTasks
26 // that are sensitive to time changes, clock drift, and other factors.
27 //
28 // Alarm manager polls the wall clock time every 5 seconds. If the clock is
29 // equal or past the requested time, the alarm will fire.
30 //
31 // Any thread can add or remove alarms. The alarm will be fired on the original
32 // thread used to set the alarm.
halliwell 2017/02/22 19:58:55 nit, I guess remove functionality is gone now. An
ryanchung 2017/02/22 21:36:35 Done.
33 //
34 // When an alarm is added to the alarm manager, the task is guaranteed to not
35 // run before the clock passes the requested time. The task may not run even if
36 // it is past the requested time if the software is suspended. However, once
37 // woken up, the event will fire within 5 seconds if the target time has passed.
38 class AlarmManager {
39 public:
40 // Construct and start the alarm manager.
41 AlarmManager();
42 ~AlarmManager();
43
44 // For testing only. Allows setting a fake clock and using a custom task
45 // runner.
46 AlarmManager(std::unique_ptr<base::Clock> clock,
47 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
48
49 // Add an alarm.
50 // |task| will be executed at around |time|.
51 void PostAlarmTask(const base::Closure& task, base::Time time);
52
53 private:
54 class AlarmInfo {
55 public:
56 AlarmInfo(const base::Closure& task,
57 base::Time time,
58 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
59 ~AlarmInfo();
60
61 const base::Closure task() const { return task_; }
62 base::Time time() const { return time_; }
63 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
64 return task_runner_;
65 }
66
67 private:
68 const base::Closure task_;
69 const base::Time time_;
70 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
71 DISALLOW_COPY_AND_ASSIGN(AlarmInfo);
72 };
73
74 // Check if an alarm should fire.
75 void CheckAlarm();
76 // Add the alarm to the queue.
77 void AddAlarm(const base::Closure& task,
78 base::Time time,
79 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
80
81 // Ordering alarms by earliest time.
82 struct alarm_compare
83 : public std::binary_function<std::unique_ptr<AlarmInfo>&,
84 std::unique_ptr<AlarmInfo>&,
85 bool> {
86 bool operator()(const std::unique_ptr<AlarmInfo>& lhs,
87 const std::unique_ptr<AlarmInfo>& rhs) const {
88 return lhs->time() > rhs->time();
89 }
90 };
91
92 // Store a list of the alarms to fire at a certain time.
93 std::priority_queue<std::unique_ptr<AlarmInfo>,
94 std::vector<std::unique_ptr<AlarmInfo>>,
95 alarm_compare>
96 next_alarm_;
97
98 // Poller for wall clock time.
99 std::unique_ptr<base::Clock> clock_;
100 std::unique_ptr<base::Timer> clock_tick_timer_;
101 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
102
103 base::WeakPtrFactory<AlarmManager> weak_factory_;
104
105 DISALLOW_COPY_AND_ASSIGN(AlarmManager);
106 };
107
108 } // namespace chromecast
109
110 #endif // CHROMECAST_BASE_ALARM_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698