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
3 // found in the LICENSE file.
5 #include "components/timers/rtc_alarm.h"
7 #include <sys/timerfd.h>
10 #include "base/files/file_util.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/threading/thread.h"
20 // Helper class to ensure that the IO thread we will use for watching file
21 // descriptors is started only once.
22 class IOThreadStartHelper
{
24 IOThreadStartHelper() : thread_(new base::Thread("RTC Alarm IO Thread")) {
25 CHECK(thread_
->StartWithOptions(
26 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0)));
28 ~IOThreadStartHelper() {}
30 base::Thread
& operator*() const { return *thread_
.get(); }
32 base::Thread
* operator->() const { return thread_
.get(); }
35 scoped_ptr
<base::Thread
> thread_
;
38 base::LazyInstance
<IOThreadStartHelper
> g_io_thread
= LAZY_INSTANCE_INITIALIZER
;
43 : alarm_fd_(timerfd_create(CLOCK_REALTIME_ALARM
, 0)),
48 RtcAlarm::~RtcAlarm() {
53 bool RtcAlarm::Init(base::WeakPtr
<AlarmTimer
> parent
) {
56 return alarm_fd_
!= -1;
59 void RtcAlarm::Stop() {
60 // Make sure that we stop the RTC from a MessageLoopForIO.
61 if (!base::MessageLoopForIO::IsCurrent()) {
62 g_io_thread
.Get()->task_runner()->PostTask(
64 base::Bind(&RtcAlarm::Stop
, scoped_refptr
<RtcAlarm
>(this)));
68 // Stop watching for events.
71 // Now clear the timer.
72 DCHECK_NE(alarm_fd_
, -1);
73 itimerspec blank_time
= {};
74 timerfd_settime(alarm_fd_
, 0, &blank_time
, NULL
);
77 void RtcAlarm::Reset(base::TimeDelta delay
) {
78 // Get a proxy for the current message loop. When the timer fires, we will
79 // post tasks to this proxy to let the parent timer know.
80 origin_message_loop_
= base::MessageLoopProxy::current();
82 // Increment the event id. Used to invalidate any events that have been
83 // queued but not yet run since the last time Reset() was called.
86 // Calling timerfd_settime with a zero delay actually clears the timer so if
87 // the user has requested a zero delay timer, we need to handle it
88 // differently. We queue the task here but we still go ahead and call
89 // timerfd_settime with the zero delay anyway to cancel any previous delay
90 // that might have been programmed.
91 if (delay
== base::TimeDelta()) {
92 origin_message_loop_
->PostTask(FROM_HERE
,
93 base::Bind(&RtcAlarm::OnTimerFired
,
94 scoped_refptr
<RtcAlarm
>(this),
98 // Make sure that we are running on a MessageLoopForIO.
99 if (!base::MessageLoopForIO::IsCurrent()) {
100 g_io_thread
.Get()->task_runner()->PostTask(
102 base::Bind(&RtcAlarm::ResetImpl
,
103 scoped_refptr
<RtcAlarm
>(this),
109 ResetImpl(delay
, origin_event_id_
);
112 void RtcAlarm::OnFileCanReadWithoutBlocking(int fd
) {
113 DCHECK_EQ(alarm_fd_
, fd
);
115 // Read from the fd to ack the event.
116 char val
[sizeof(uint64_t)];
117 base::ReadFromFD(alarm_fd_
, val
, sizeof(uint64_t));
119 // Make sure that the parent timer is informed on the proper message loop.
120 if (origin_message_loop_
->RunsTasksOnCurrentThread()) {
121 OnTimerFired(io_event_id_
);
125 origin_message_loop_
->PostTask(FROM_HERE
,
126 base::Bind(&RtcAlarm::OnTimerFired
,
127 scoped_refptr
<RtcAlarm
>(this),
131 void RtcAlarm::OnFileCanWriteWithoutBlocking(int fd
) {
135 void RtcAlarm::ResetImpl(base::TimeDelta delay
, int event_id
) {
136 DCHECK(base::MessageLoopForIO::IsCurrent());
137 DCHECK_NE(alarm_fd_
, -1);
139 // Store the event id in the IO thread variable. When the timer fires, we
140 // will bind this value to the OnTimerFired callback to ensure that we do the
141 // right thing if the timer gets reset.
142 io_event_id_
= event_id
;
144 // If we were already watching the fd, this will stop watching it.
145 fd_watcher_
.reset(new base::MessageLoopForIO::FileDescriptorWatcher
);
147 // Start watching the fd to see when the timer fires.
148 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
151 base::MessageLoopForIO::WATCH_READ
,
154 LOG(ERROR
) << "Error while attempting to watch file descriptor for RTC "
155 << "alarm. Timer will not fire.";
158 // Actually set the timer. This will also clear the pre-existing timer, if
160 itimerspec alarm_time
= {};
161 alarm_time
.it_value
.tv_sec
= delay
.InSeconds();
162 alarm_time
.it_value
.tv_nsec
=
163 (delay
.InMicroseconds() % base::Time::kMicrosecondsPerSecond
) *
164 base::Time::kNanosecondsPerMicrosecond
;
165 if (timerfd_settime(alarm_fd_
, 0, &alarm_time
, NULL
) < 0)
166 PLOG(ERROR
) << "Error while setting alarm time. Timer will not fire";
169 void RtcAlarm::OnTimerFired(int event_id
) {
170 DCHECK(origin_message_loop_
->RunsTasksOnCurrentThread());
172 // Check to make sure that the timer was not reset in the time between when
173 // this task was queued to run and now. If it was reset, then don't do
175 if (event_id
!= origin_event_id_
)
179 parent_
->OnTimerFired();
182 } // namespace timers