Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "base/timer/rtc_alarm_chromeos.h" | |
| 6 | |
| 7 #include <sys/timerfd.h> | |
| 8 | |
| 9 #include "base/bind.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" | |
| 16 | |
| 17 namespace base { | |
| 18 | |
| 19 namespace { | |
| 20 // Helper class to ensure that the IO thread we will use for watching file | |
| 21 // descriptors is started only once. | |
| 22 class IOThreadStartHelper { | |
| 23 public: | |
| 24 IOThreadStartHelper() : thread_(new base::Thread("RTC Alarm IO Thread")) { | |
| 25 bool ret = thread_->StartWithOptions( | |
| 26 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
| 27 | |
| 28 CHECK(ret); | |
|
Daniel Erat
2014/10/15 15:04:22
nit: just put the CHECK() around the call above?
Chirantan Ekbote
2014/10/16 21:26:38
Done.
| |
| 29 } | |
| 30 ~IOThreadStartHelper() {} | |
| 31 | |
| 32 base::Thread& operator*() const { return *thread_.get(); } | |
| 33 | |
| 34 base::Thread* operator->() const { return thread_.get(); } | |
| 35 | |
| 36 private: | |
| 37 scoped_ptr<base::Thread> thread_; | |
| 38 }; | |
| 39 | |
| 40 base::LazyInstance<IOThreadStartHelper> g_io_thread = LAZY_INSTANCE_INITIALIZER; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 RtcAlarmChromeos::RtcAlarmChromeos() | |
| 45 : alarm_fd_(timerfd_create(CLOCK_REALTIME_ALARM, 0)), | |
| 46 origin_event_id_(0), | |
| 47 io_event_id_(0) { | |
| 48 } | |
| 49 | |
| 50 RtcAlarmChromeos::~RtcAlarmChromeos() { | |
| 51 if (alarm_fd_ != -1) | |
| 52 close(alarm_fd_); | |
| 53 } | |
| 54 | |
| 55 bool RtcAlarmChromeos::Init(WeakPtr<AlarmTimer> parent) { | |
| 56 parent_ = parent; | |
| 57 | |
| 58 return alarm_fd_ != -1; | |
| 59 } | |
| 60 | |
| 61 void RtcAlarmChromeos::Stop() { | |
| 62 // Make sure that we stop the RTC from a MessageLoopForIO. | |
| 63 if (!MessageLoopForIO::IsCurrent()) { | |
| 64 g_io_thread.Get()->task_runner()->PostTask( | |
| 65 FROM_HERE, | |
| 66 Bind(&RtcAlarmChromeos::Stop, | |
| 67 scoped_refptr<RtcAlarmChromeos>(this))); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 // Stop watching for events. | |
| 72 fd_watcher_.reset(); | |
| 73 | |
| 74 // Now clear the timer. | |
| 75 DCHECK_NE(alarm_fd_, -1); | |
| 76 itimerspec blank_time = {}; | |
| 77 timerfd_settime(alarm_fd_, 0, &blank_time, NULL); | |
| 78 } | |
| 79 | |
| 80 void RtcAlarmChromeos::Reset(TimeDelta delay) { | |
| 81 // Get a proxy for the current message loop. When the timer fires, we will | |
| 82 // post tasks to this proxy to let the parent timer know. | |
| 83 origin_message_loop_ = MessageLoopProxy::current(); | |
| 84 | |
| 85 // Increment the event id. Used to invalidate any events that have been | |
| 86 // queued but not yet run since the last time Reset() was called. | |
| 87 origin_event_id_++; | |
| 88 | |
| 89 // Calling timerfd_settime with a zero delay actually clears the timer so if | |
| 90 // the user has requested a zero delay timer, we need to handle it separately. | |
| 91 if (delay == TimeDelta()) { | |
| 92 origin_message_loop_->PostTask(FROM_HERE, | |
| 93 Bind(&RtcAlarmChromeos::OnTimerFired, | |
| 94 scoped_refptr<RtcAlarmChromeos>(this), | |
| 95 origin_event_id_)); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 // Make sure that we are running on a MessageLoopForIO. | |
| 100 if (!MessageLoopForIO::IsCurrent()) { | |
| 101 g_io_thread.Get()->task_runner()->PostTask( | |
| 102 FROM_HERE, | |
| 103 Bind(&RtcAlarmChromeos::ResetImpl, | |
| 104 scoped_refptr<RtcAlarmChromeos>(this), | |
| 105 delay, | |
| 106 origin_event_id_)); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 ResetImpl(delay, origin_event_id_); | |
| 111 } | |
| 112 | |
| 113 void RtcAlarmChromeos::OnFileCanReadWithoutBlocking(int fd) { | |
| 114 DCHECK_EQ(alarm_fd_, fd); | |
| 115 | |
| 116 // Read from the fd to ack the event. | |
| 117 char val[sizeof(uint64_t)]; | |
| 118 ReadFromFD(alarm_fd_, val, sizeof(uint64_t)); | |
| 119 | |
| 120 // Make sure that the parent timer is informed on the proper message loop. | |
| 121 if (origin_message_loop_->RunsTasksOnCurrentThread()) { | |
| 122 OnTimerFired(io_event_id_); | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 origin_message_loop_->PostTask(FROM_HERE, | |
| 127 Bind(&RtcAlarmChromeos::OnTimerFired, | |
| 128 scoped_refptr<RtcAlarmChromeos>(this), | |
| 129 io_event_id_)); | |
| 130 } | |
| 131 | |
| 132 void RtcAlarmChromeos::OnFileCanWriteWithoutBlocking(int fd) { | |
| 133 NOTREACHED(); | |
| 134 } | |
| 135 | |
| 136 void RtcAlarmChromeos::ResetImpl(TimeDelta delay, int event_id) { | |
| 137 DCHECK(MessageLoopForIO::IsCurrent()); | |
| 138 DCHECK_NE(alarm_fd_, -1); | |
| 139 | |
| 140 // Store the event id in the IO thread variable. When the timer fires, we | |
| 141 // will bind this value to the OnTimerFired callback to ensure that we do the | |
| 142 // right thing if the timer gets reset. | |
| 143 io_event_id_ = event_id; | |
| 144 | |
| 145 // If we were already watching the fd, this will stop watching it. | |
| 146 fd_watcher_.reset(new MessageLoopForIO::FileDescriptorWatcher); | |
| 147 | |
| 148 // Start watching the fd to see when the timer fires. | |
| 149 if (!MessageLoopForIO::current()->WatchFileDescriptor( | |
| 150 alarm_fd_, | |
| 151 false, | |
| 152 MessageLoopForIO::WATCH_READ, | |
| 153 fd_watcher_.get(), | |
| 154 this)) { | |
| 155 LOG(ERROR) << "Error while attempting to watch file descriptor for RTC " | |
| 156 << "alarm. Timer will not fire."; | |
| 157 } | |
| 158 | |
| 159 // Actually set the timer. This will also clear the pre-existing timer, if | |
| 160 // any. | |
| 161 itimerspec alarm_time = {}; | |
| 162 alarm_time.it_value.tv_sec = delay.InSeconds(); | |
| 163 alarm_time.it_value.tv_nsec = | |
| 164 (delay.InMicroseconds() % Time::kMicrosecondsPerSecond) * | |
| 165 Time::kNanosecondsPerMicrosecond; | |
| 166 if (timerfd_settime(alarm_fd_, 0, &alarm_time, NULL) < 0) | |
| 167 PLOG(ERROR) << "Error while setting alarm time. Timer will not fire"; | |
| 168 } | |
| 169 | |
| 170 void RtcAlarmChromeos::OnTimerFired(int event_id) { | |
| 171 DCHECK(origin_message_loop_->RunsTasksOnCurrentThread()); | |
| 172 | |
| 173 // Check to make sure that the timer was not reset in the time between when | |
| 174 // this task was queued to run and now. If it was reset, then don't do | |
| 175 // anything. | |
| 176 if (event_id != origin_event_id_) | |
| 177 return; | |
| 178 | |
| 179 if (parent_) | |
| 180 parent_->OnTimerFired(); | |
| 181 } | |
| 182 | |
| 183 } // namespace base | |
| OLD | NEW |