OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/timers/alarm_timer.h" | 5 #include "components/timers/alarm_timer.h" |
6 | 6 |
7 #include <sys/timerfd.h> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/lazy_instance.h" | |
10 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/message_loop/message_loop_proxy.h" | |
11 #include "base/pending_task.h" | 17 #include "base/pending_task.h" |
12 #include "components/timers/rtc_alarm.h" | 18 #include "base/threading/thread.h" |
13 | 19 |
14 namespace timers { | 20 namespace timers { |
21 namespace { | |
22 // This class represents the IO thread that the AlarmTimer::Delegate may use for | |
23 // watching file descriptors if it gets called from a thread that does not have | |
24 // a MessageLoopForIO. It is a lazy global instance because it may not always | |
25 // be necessary. | |
26 class RtcAlarmIOThread : public base::Thread { | |
27 public: | |
28 RtcAlarmIOThread() : Thread("RTC Alarm IO Thread") { | |
29 CHECK( | |
30 StartWithOptions(base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | |
31 } | |
32 ~RtcAlarmIOThread() override {} | |
33 }; | |
34 | |
35 base::LazyInstance<RtcAlarmIOThread> g_io_thread = LAZY_INSTANCE_INITIALIZER; | |
36 | |
37 } // namespace | |
38 | |
39 // Watches a MessageLoop and runs a callback if that MessageLoop will be | |
40 // destroyed. | |
41 class AlarmTimer::MessageLoopObserver | |
42 : public base::MessageLoop::DestructionObserver { | |
43 public: | |
44 // Constructs a MessageLoopObserver that will observe |message_loop| and will | |
45 // call |on_will_be_destroyed_callback| when |message_loop| is about to be | |
46 // destroyed. | |
47 MessageLoopObserver(base::MessageLoop* message_loop, | |
48 base::Closure on_will_be_destroyed_callback) | |
49 : message_loop_(message_loop), | |
50 on_will_be_destroyed_callback_(on_will_be_destroyed_callback) { | |
51 DCHECK(message_loop_); | |
52 message_loop_->AddDestructionObserver(this); | |
53 } | |
54 | |
55 ~MessageLoopObserver() override { | |
56 // If |message_loop_| was destroyed, then this class will have already | |
57 // unregistered itself. Doing it again will trigger a warning. | |
58 if (message_loop_) | |
59 message_loop_->RemoveDestructionObserver(this); | |
60 } | |
61 | |
62 // base::MessageLoop::DestructionObserver override. | |
63 void WillDestroyCurrentMessageLoop() override { | |
64 message_loop_->RemoveDestructionObserver(this); | |
65 message_loop_ = NULL; | |
66 | |
67 on_will_be_destroyed_callback_.Run(); | |
68 } | |
69 | |
70 private: | |
71 // The MessageLoop that this class should watch. Is a weak pointer. | |
72 base::MessageLoop* message_loop_; | |
73 | |
74 // The callback to run when |message_loop_| will be destroyed. | |
75 base::Closure on_will_be_destroyed_callback_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver); | |
78 }; | |
79 | |
80 // This class manages a Real Time Clock (RTC) alarm, a feature that is available | |
81 // from linux version 3.11 onwards. It creates a file descriptor for the RTC | |
82 // alarm timer and then watches that file descriptor to see when it can be read | |
83 // without blocking, indicating that the timer has fired. | |
84 // | |
85 // A major problem for this class is that watching file descriptors is only | |
86 // available on a MessageLoopForIO but there is no guarantee the timer is going | |
87 // to be created on one. To get around this, the timer has a dedicated thread | |
88 // with a MessageLoopForIO that posts tasks back to the thread that started the | |
89 // timer. | |
90 class AlarmTimer::Delegate | |
91 : public base::RefCountedThreadSafe<AlarmTimer::Delegate>, | |
92 public base::MessageLoopForIO::Watcher { | |
93 public: | |
94 // Construct a Delegate for the AlarmTimer. |parent| should be a valid | |
95 // WeakPtr to the parent AlarmTimer. | |
96 explicit Delegate(base::Closure on_timer_fired_callback); | |
97 | |
98 // Returns true if the system timer managed by this delegate is capable of | |
99 // waking the system from suspend. | |
100 bool CanWakeFromSuspend(); | |
gromer
2015/02/17 22:02:08
FWIW, I think this would be a reasonable method to
| |
101 | |
102 // Resets the timer to fire after |delay| has passed. Cancels any | |
103 // pre-existing delay. | |
104 void Reset(base::TimeDelta delay); | |
105 | |
106 // Stops the currently running timer. It should be safe to call this even if | |
107 // the timer is not running. | |
108 void Stop(); | |
109 | |
110 // base::MessageLoopForIO::Watcher overrides. | |
111 void OnFileCanReadWithoutBlocking(int fd) override; | |
112 void OnFileCanWriteWithoutBlocking(int fd) override; | |
113 | |
114 private: | |
115 friend class base::RefCountedThreadSafe<Delegate>; | |
116 ~Delegate() override; | |
117 | |
118 // Actually performs the system calls to set up the timer. This must be | |
119 // called on a MessageLoopForIO. | |
120 void ResetImpl(base::TimeDelta delay, int reset_sequence_number); | |
121 | |
122 // Callback that is run when the timer fires. Must be run on | |
123 // |origin_message_loop_|. | |
124 void OnTimerFired(int reset_sequence_number); | |
125 | |
126 // File descriptor associated with the alarm timer. | |
127 int alarm_fd_; | |
128 | |
129 // Message loop which initially started the timer. | |
130 scoped_refptr<base::MessageLoopProxy> origin_message_loop_; | |
131 | |
132 // Callback that should be run when the timer fires. | |
133 base::Closure on_timer_fired_callback_; | |
134 | |
135 // Manages watching file descriptors. | |
136 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> fd_watcher_; | |
137 | |
138 // The sequence numbers of the last Reset() call handled respectively on | |
139 // |origin_message_loop_| and on the MessageLoopForIO used for watching the | |
140 // timer file descriptor. Note that these can be the same MessageLoop. | |
141 // OnTimerFired() runs |on_timer_fired_callback_| only if the sequence number | |
142 // it receives from the MessageLoopForIO matches | |
143 // |origin_reset_sequence_number_|. | |
144 int origin_reset_sequence_number_; | |
145 int io_reset_sequence_number_; | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
148 }; | |
149 | |
150 // Construct a Delegate for the AlarmTimer. |parent| should be a valid | |
151 // WeakPtr to the parent AlarmTimer. | |
152 AlarmTimer::Delegate::Delegate(base::Closure on_timer_fired_callback) | |
153 : alarm_fd_(timerfd_create(CLOCK_REALTIME_ALARM, 0)), | |
154 on_timer_fired_callback_(on_timer_fired_callback), | |
155 origin_reset_sequence_number_(0), | |
156 io_reset_sequence_number_(0) { | |
157 } | |
158 | |
159 AlarmTimer::Delegate::~Delegate() { | |
160 if (alarm_fd_ != -1) | |
161 close(alarm_fd_); | |
162 } | |
163 | |
164 bool AlarmTimer::Delegate::CanWakeFromSuspend() { | |
165 return alarm_fd_ != -1; | |
166 } | |
167 | |
168 void AlarmTimer::Delegate::Reset(base::TimeDelta delay) { | |
169 // Get a proxy for the current message loop. When the timer fires, we will | |
170 // post tasks to this proxy to let the parent timer know. | |
171 origin_message_loop_ = base::MessageLoopProxy::current(); | |
172 | |
173 // Increment the sequence number. Used to invalidate any events that have | |
174 // been queued but not yet run since the last time Reset() was called. | |
175 origin_reset_sequence_number_++; | |
176 | |
177 // Calling timerfd_settime with a zero delay actually clears the timer so if | |
178 // the user has requested a zero delay timer, we need to handle it | |
179 // differently. We queue the task here but we still go ahead and call | |
180 // timerfd_settime with the zero delay anyway to cancel any previous delay | |
181 // that might have been programmed. | |
182 if (delay <= base::TimeDelta::FromMicroseconds(0)) { | |
183 // The timerfd_settime documentation is vague on what happens when it is | |
184 // passed a negative delay. We can sidestep the issue by ensuring that | |
185 // the delay is 0. | |
186 delay = base::TimeDelta::FromMicroseconds(0); | |
187 origin_message_loop_->PostTask( | |
188 FROM_HERE, | |
189 base::Bind(&Delegate::OnTimerFired, scoped_refptr<Delegate>(this), | |
190 origin_reset_sequence_number_)); | |
191 } | |
192 | |
193 // Run ResetImpl() on a MessageLoopForIO. | |
194 if (base::MessageLoopForIO::IsCurrent()) { | |
195 ResetImpl(delay, origin_reset_sequence_number_); | |
196 } else { | |
197 g_io_thread.Pointer()->task_runner()->PostTask( | |
198 FROM_HERE, | |
199 base::Bind(&Delegate::ResetImpl, scoped_refptr<Delegate>(this), delay, | |
200 origin_reset_sequence_number_)); | |
201 } | |
202 } | |
203 | |
204 void AlarmTimer::Delegate::Stop() { | |
205 // Stop the RTC from a MessageLoopForIO. | |
206 if (!base::MessageLoopForIO::IsCurrent()) { | |
207 g_io_thread.Pointer()->task_runner()->PostTask( | |
208 FROM_HERE, base::Bind(&Delegate::Stop, scoped_refptr<Delegate>(this))); | |
209 return; | |
210 } | |
211 | |
212 // Stop watching for events. | |
213 fd_watcher_.reset(); | |
214 | |
215 // Now clear the timer. | |
216 DCHECK_NE(alarm_fd_, -1); | |
217 itimerspec blank_time = {}; | |
218 timerfd_settime(alarm_fd_, 0, &blank_time, NULL); | |
219 } | |
220 | |
221 void AlarmTimer::Delegate::OnFileCanReadWithoutBlocking(int fd) { | |
222 DCHECK_EQ(alarm_fd_, fd); | |
223 | |
224 // Read from the fd to ack the event. | |
225 char val[sizeof(uint64_t)]; | |
226 base::ReadFromFD(alarm_fd_, val, sizeof(uint64_t)); | |
227 | |
228 // Make sure that the parent timer is informed on the proper message loop. | |
229 if (origin_message_loop_->RunsTasksOnCurrentThread()) { | |
230 OnTimerFired(io_reset_sequence_number_); | |
231 } else { | |
232 origin_message_loop_->PostTask( | |
233 FROM_HERE, | |
234 base::Bind(&Delegate::OnTimerFired, scoped_refptr<Delegate>(this), | |
235 io_reset_sequence_number_)); | |
236 } | |
237 } | |
238 | |
239 void AlarmTimer::Delegate::OnFileCanWriteWithoutBlocking(int fd) { | |
240 NOTREACHED(); | |
241 } | |
242 | |
243 void AlarmTimer::Delegate::ResetImpl(base::TimeDelta delay, | |
244 int reset_sequence_number) { | |
245 DCHECK(base::MessageLoopForIO::IsCurrent()); | |
246 DCHECK_NE(alarm_fd_, -1); | |
247 | |
248 // Store the sequence number in the IO thread variable. When the timer | |
249 // fires, we will bind this value to the OnTimerFired callback to ensure | |
250 // that we do the right thing if the timer gets reset. | |
251 io_reset_sequence_number_ = reset_sequence_number; | |
252 | |
253 // If we were already watching the fd, this will stop watching it. | |
254 fd_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); | |
255 | |
256 // Start watching the fd to see when the timer fires. | |
257 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | |
258 alarm_fd_, false, base::MessageLoopForIO::WATCH_READ, | |
259 fd_watcher_.get(), this)) { | |
260 LOG(ERROR) << "Error while attempting to watch file descriptor for RTC " | |
261 << "alarm. Timer will not fire."; | |
262 } | |
263 | |
264 // Actually set the timer. This will also clear the pre-existing timer, if | |
265 // any. | |
266 itimerspec alarm_time = {}; | |
267 alarm_time.it_value.tv_sec = delay.InSeconds(); | |
268 alarm_time.it_value.tv_nsec = | |
269 (delay.InMicroseconds() % base::Time::kMicrosecondsPerSecond) * | |
270 base::Time::kNanosecondsPerMicrosecond; | |
271 if (timerfd_settime(alarm_fd_, 0, &alarm_time, NULL) < 0) | |
272 PLOG(ERROR) << "Error while setting alarm time. Timer will not fire"; | |
273 } | |
274 | |
275 void AlarmTimer::Delegate::OnTimerFired(int reset_sequence_number) { | |
276 DCHECK(origin_message_loop_->RunsTasksOnCurrentThread()); | |
277 | |
278 // Check to make sure that the timer was not reset in the time between when | |
279 // this task was queued to run and now. If it was reset, then don't do | |
280 // anything. | |
281 if (reset_sequence_number != origin_reset_sequence_number_) | |
282 return; | |
283 | |
284 on_timer_fired_callback_.Run(); | |
285 } | |
15 | 286 |
16 AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating) | 287 AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating) |
17 : base::Timer(retain_user_task, is_repeating), | 288 : base::Timer(retain_user_task, is_repeating), |
18 delegate_(new RtcAlarm()), | |
19 can_wake_from_suspend_(false), | 289 can_wake_from_suspend_(false), |
20 origin_message_loop_(NULL), | 290 origin_message_loop_(NULL), |
21 weak_factory_(this) { | 291 weak_factory_(this) { |
22 can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr()); | 292 Init(); |
23 } | 293 } |
24 | 294 |
25 AlarmTimer::AlarmTimer(const tracked_objects::Location& posted_from, | 295 AlarmTimer::AlarmTimer(const tracked_objects::Location& posted_from, |
26 base::TimeDelta delay, | 296 base::TimeDelta delay, |
27 const base::Closure& user_task, | 297 const base::Closure& user_task, |
28 bool is_repeating) | 298 bool is_repeating) |
29 : base::Timer(posted_from, delay, user_task, is_repeating), | 299 : base::Timer(posted_from, delay, user_task, is_repeating), |
30 delegate_(new RtcAlarm()), | |
31 can_wake_from_suspend_(false), | 300 can_wake_from_suspend_(false), |
32 origin_message_loop_(NULL), | 301 origin_message_loop_(NULL), |
33 weak_factory_(this) { | 302 weak_factory_(this) { |
34 can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr()); | 303 Init(); |
35 } | 304 } |
36 | 305 |
37 AlarmTimer::~AlarmTimer() { | 306 AlarmTimer::~AlarmTimer() { |
38 Stop(); | 307 Stop(); |
39 } | 308 } |
40 | 309 |
41 void AlarmTimer::OnTimerFired() { | 310 void AlarmTimer::Init() { |
42 if (!base::Timer::IsRunning()) | 311 delegate_ = make_scoped_refptr(new AlarmTimer::Delegate( |
43 return; | 312 base::Bind(&AlarmTimer::OnTimerFired, weak_factory_.GetWeakPtr()))); |
44 | 313 can_wake_from_suspend_ = delegate_->CanWakeFromSuspend(); |
45 DCHECK(pending_task_.get()); | |
46 | |
47 // Take ownership of the pending user task, which is going to be cleared by | |
48 // the Stop() or Reset() functions below. | |
49 scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass()); | |
50 | |
51 // Re-schedule or stop the timer as requested. | |
52 if (base::Timer::is_repeating()) | |
53 Reset(); | |
54 else | |
55 Stop(); | |
56 | |
57 // Now run the user task. | |
58 base::MessageLoop::current()->task_annotator()->RunTask( | |
59 "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task); | |
60 } | 314 } |
61 | 315 |
62 void AlarmTimer::Stop() { | 316 void AlarmTimer::Stop() { |
63 if (!can_wake_from_suspend_) { | 317 if (!can_wake_from_suspend_) { |
64 base::Timer::Stop(); | 318 base::Timer::Stop(); |
65 return; | 319 return; |
66 } | 320 } |
67 | 321 |
68 // Clear the running flag, stop the delegate, and delete the pending task. | 322 // Clear the running flag, stop the delegate, and delete the pending task. |
69 base::Timer::set_is_running(false); | 323 base::Timer::set_is_running(false); |
70 delegate_->Stop(); | 324 delegate_->Stop(); |
71 pending_task_.reset(); | 325 pending_task_.reset(); |
72 | 326 |
73 // Stop is called when the AlarmTimer is destroyed so we need to remove | 327 // Stop watching |origin_message_loop_|. |
74 // ourselves as a MessageLoop::DestructionObserver to prevent a segfault | 328 origin_message_loop_ = NULL; |
75 // later. | 329 message_loop_observer_.reset(); |
76 if (origin_message_loop_) { | |
77 origin_message_loop_->RemoveDestructionObserver(this); | |
78 origin_message_loop_ = NULL; | |
79 } | |
80 | 330 |
81 if (!base::Timer::retain_user_task()) | 331 if (!base::Timer::retain_user_task()) |
82 base::Timer::set_user_task(base::Closure()); | 332 base::Timer::set_user_task(base::Closure()); |
83 } | 333 } |
84 | 334 |
85 void AlarmTimer::Reset() { | 335 void AlarmTimer::Reset() { |
86 if (!can_wake_from_suspend_) { | 336 if (!can_wake_from_suspend_) { |
87 base::Timer::Reset(); | 337 base::Timer::Reset(); |
88 return; | 338 return; |
89 } | 339 } |
90 | 340 |
91 DCHECK(!base::Timer::user_task().is_null()); | 341 DCHECK(!base::Timer::user_task().is_null()); |
92 DCHECK(!origin_message_loop_ || | 342 DCHECK(!origin_message_loop_ || |
93 origin_message_loop_->task_runner()->RunsTasksOnCurrentThread()); | 343 origin_message_loop_->task_runner()->RunsTasksOnCurrentThread()); |
94 | 344 |
95 // Make sure that the timer will stop if the underlying message loop is | 345 // Make sure that the timer will stop if the underlying message loop is |
96 // destroyed. | 346 // destroyed. |
97 if (!origin_message_loop_) { | 347 if (!origin_message_loop_) { |
98 origin_message_loop_ = base::MessageLoop::current(); | 348 origin_message_loop_ = base::MessageLoop::current(); |
99 origin_message_loop_->AddDestructionObserver(this); | 349 message_loop_observer_.reset(new MessageLoopObserver( |
350 origin_message_loop_, | |
351 base::Bind(&AlarmTimer::WillDestroyCurrentMessageLoop, | |
352 weak_factory_.GetWeakPtr()))); | |
100 } | 353 } |
101 | 354 |
102 // Set up the pending task. | 355 // Set up the pending task. |
103 if (base::Timer::GetCurrentDelay() > base::TimeDelta::FromMicroseconds(0)) { | 356 if (base::Timer::GetCurrentDelay() > base::TimeDelta::FromMicroseconds(0)) { |
104 base::Timer::set_desired_run_time( | 357 base::Timer::set_desired_run_time(base::TimeTicks::Now() + |
105 base::TimeTicks::Now() + base::Timer::GetCurrentDelay()); | 358 base::Timer::GetCurrentDelay()); |
106 pending_task_.reset(new base::PendingTask(base::Timer::posted_from(), | 359 pending_task_.reset(new base::PendingTask( |
107 base::Timer::user_task(), | 360 base::Timer::posted_from(), base::Timer::user_task(), |
108 base::Timer::desired_run_time(), | 361 base::Timer::desired_run_time(), true /* nestable */)); |
109 true /* nestable */)); | |
110 } else { | 362 } else { |
111 base::Timer::set_desired_run_time(base::TimeTicks()); | 363 base::Timer::set_desired_run_time(base::TimeTicks()); |
112 pending_task_.reset(new base::PendingTask(base::Timer::posted_from(), | 364 pending_task_.reset(new base::PendingTask(base::Timer::posted_from(), |
113 base::Timer::user_task())); | 365 base::Timer::user_task())); |
114 } | 366 } |
115 base::MessageLoop::current()->task_annotator()->DidQueueTask( | 367 base::MessageLoop::current()->task_annotator()->DidQueueTask( |
116 "AlarmTimer::Reset", *pending_task_); | 368 "AlarmTimer::Reset", *pending_task_); |
117 | 369 |
118 // Now start up the timer. | 370 // Now start up the timer. |
119 delegate_->Reset(base::Timer::GetCurrentDelay()); | 371 delegate_->Reset(base::Timer::GetCurrentDelay()); |
120 base::Timer::set_is_running(true); | 372 base::Timer::set_is_running(true); |
121 } | 373 } |
122 | 374 |
123 void AlarmTimer::WillDestroyCurrentMessageLoop() { | 375 void AlarmTimer::WillDestroyCurrentMessageLoop() { |
124 Stop(); | 376 Stop(); |
125 } | 377 } |
126 | 378 |
379 void AlarmTimer::OnTimerFired() { | |
380 if (!base::Timer::IsRunning()) | |
381 return; | |
382 | |
383 DCHECK(pending_task_.get()); | |
384 | |
385 // Take ownership of the pending user task, which is going to be cleared by | |
386 // the Stop() or Reset() functions below. | |
387 scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass()); | |
388 | |
389 // Re-schedule or stop the timer as requested. | |
390 if (base::Timer::is_repeating()) | |
391 Reset(); | |
392 else | |
393 Stop(); | |
394 | |
395 // Now run the user task. | |
396 base::MessageLoop::current()->task_annotator()->RunTask( | |
397 "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task); | |
398 } | |
399 | |
127 } // namespace timers | 400 } // namespace timers |
OLD | NEW |