Chromium Code Reviews| 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> | |
|
halliwell
2017/02/22 19:09:15
unused?
ryanchung
2017/02/22 19:39:39
Done.
| |
| 9 #include <queue> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/threading/thread.h" | |
| 17 #include "base/time/clock.h" | |
| 18 #include "base/timer/timer.h" | |
|
halliwell
2017/02/22 19:09:15
nit, can just forward-declare for things held in u
ryanchung
2017/02/22 19:39:39
Done. thx.
| |
| 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 // Construct and start the alarm manager. | |
| 39 AlarmManager(); | |
| 40 ~AlarmManager(); | |
| 41 | |
| 42 // For testing only. Allows setting a fake clock and using a custom task | |
| 43 // runner. | |
| 44 AlarmManager(std::unique_ptr<base::Clock> clock, | |
| 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 46 | |
| 47 // Add an alarm. | |
| 48 // |task| will be executed at around |time|. | |
| 49 void PostAlarmTask(const base::Closure& task, base::Time time); | |
| 50 | |
| 51 private: | |
| 52 class AlarmInfo { | |
| 53 public: | |
| 54 AlarmInfo(const base::Closure& task, | |
| 55 base::Time time, | |
| 56 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 57 ~AlarmInfo(); | |
| 58 | |
| 59 const base::Closure task() const { return task_; } | |
| 60 base::Time time() const { return time_; } | |
| 61 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { | |
| 62 return task_runner_; | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 const base::Closure task_; | |
| 67 const base::Time time_; | |
| 68 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 69 DISALLOW_COPY_AND_ASSIGN(AlarmInfo); | |
| 70 }; | |
| 71 | |
| 72 // Check if an alarm should fire. | |
| 73 void CheckAlarm(); | |
| 74 // Add the alarm to the queue. | |
| 75 void AddAlarm(const base::Closure& task, | |
| 76 base::Time time, | |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 78 | |
| 79 // Ordering alarms by earliest time. | |
| 80 struct alarm_compare | |
| 81 : public std::binary_function<std::unique_ptr<AlarmInfo>&, | |
| 82 std::unique_ptr<AlarmInfo>&, | |
| 83 bool> { | |
| 84 bool operator()(const std::unique_ptr<AlarmInfo>& lhs, | |
| 85 const std::unique_ptr<AlarmInfo>& rhs) const { | |
| 86 return lhs->time() > rhs->time(); | |
| 87 } | |
| 88 }; | |
| 89 | |
| 90 // Store a list of the alarms to fire at a certain time. | |
| 91 std::priority_queue<std::unique_ptr<AlarmInfo>, | |
| 92 std::vector<std::unique_ptr<AlarmInfo>>, | |
| 93 alarm_compare> | |
| 94 next_alarm_; | |
| 95 | |
| 96 // Poller for wall clock time. | |
| 97 std::unique_ptr<base::Clock> clock_; | |
| 98 std::unique_ptr<base::Timer> clock_tick_timer_; | |
| 99 std::unique_ptr<base::Thread> alarm_thread_; | |
| 100 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 101 | |
| 102 base::WeakPtrFactory<AlarmManager> weak_factory_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | |
| 105 }; | |
| 106 | |
| 107 } // namespace chromecast | |
| 108 | |
| 109 #endif // CHROMECAST_BASE_ALARM_MANAGER_H_ | |
| OLD | NEW |