chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 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 "components/timers/alarm_timer_chromeos.h" |
| 6 | |
avi | 2f891b6 | 2015-12-26 07:30:46 +0900 | [diff] [blame] | 7 | #include <stdint.h> |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 8 | #include <sys/timerfd.h> |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 9 | |
| 10 | #include <algorithm> |
dcheng | 968547c | 2015-12-31 13:54:47 +0900 | [diff] [blame] | 11 | #include <utility> |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 12 | |
| 13 | #include "base/bind.h" |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 14 | #include "base/debug/task_annotator.h" |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 16 | #include "base/logging.h" |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 18 | #include "base/pending_task.h" |
caseq | 341a961 | 2015-07-17 04:13:21 +0900 | [diff] [blame] | 19 | #include "base/trace_event/trace_event.h" |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 20 | |
| 21 | namespace timers { |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 22 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 23 | AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating) |
| 24 | : base::Timer(retain_user_task, is_repeating), |
| 25 | alarm_fd_(timerfd_create(CLOCK_REALTIME_ALARM, 0)), |
| 26 | weak_factory_(this) {} |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 27 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 28 | AlarmTimer::~AlarmTimer() { |
| 29 | DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 30 | Stop(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 31 | } |
| 32 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 33 | void AlarmTimer::Stop() { |
| 34 | DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 35 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 36 | if (!base::Timer::is_running()) |
| 37 | return; |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 38 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 39 | if (!CanWakeFromSuspend()) { |
| 40 | base::Timer::Stop(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 44 | // Cancel any previous callbacks. |
| 45 | weak_factory_.InvalidateWeakPtrs(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 46 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 47 | base::Timer::set_is_running(false); |
| 48 | alarm_fd_watcher_.reset(); |
| 49 | pending_task_.reset(); |
| 50 | |
| 51 | if (!base::Timer::retain_user_task()) |
| 52 | base::Timer::set_user_task(base::Closure()); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 53 | } |
| 54 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 55 | void AlarmTimer::Reset() { |
| 56 | DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 57 | DCHECK(!base::Timer::user_task().is_null()); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 58 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 59 | if (!CanWakeFromSuspend()) { |
| 60 | base::Timer::Reset(); |
| 61 | return; |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 62 | } |
| 63 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 64 | // Cancel any previous callbacks and stop watching |alarm_fd_|. |
| 65 | weak_factory_.InvalidateWeakPtrs(); |
| 66 | alarm_fd_watcher_.reset(); |
| 67 | |
| 68 | // Ensure that the delay is not negative. |
| 69 | const base::TimeDelta delay = |
| 70 | std::max(base::TimeDelta(), base::Timer::GetCurrentDelay()); |
| 71 | |
| 72 | // Set up the pending task. |
| 73 | base::Timer::set_desired_run_time( |
| 74 | delay.is_zero() ? base::TimeTicks() : base::TimeTicks::Now() + delay); |
| 75 | pending_task_ = base::MakeUnique<base::PendingTask>( |
| 76 | base::Timer::posted_from(), base::Timer::user_task(), |
| 77 | base::Timer::desired_run_time(), true /* nestable */); |
| 78 | |
| 79 | // Set |alarm_fd_| to be signaled when the delay expires. If the delay is |
| 80 | // zero, |alarm_fd_| will never be signaled. This overrides the previous |
| 81 | // delay, if any. |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 82 | itimerspec alarm_time = {}; |
| 83 | alarm_time.it_value.tv_sec = delay.InSeconds(); |
| 84 | alarm_time.it_value.tv_nsec = |
| 85 | (delay.InMicroseconds() % base::Time::kMicrosecondsPerSecond) * |
| 86 | base::Time::kNanosecondsPerMicrosecond; |
| 87 | if (timerfd_settime(alarm_fd_, 0, &alarm_time, NULL) < 0) |
| 88 | PLOG(ERROR) << "Error while setting alarm time. Timer will not fire"; |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 89 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 90 | // The timer is running. |
| 91 | base::Timer::set_is_running(true); |
fdoray | f7084ad | 2016-10-07 04:03:34 +0900 | [diff] [blame] | 92 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 93 | // If the delay is zero, post the task now. |
| 94 | if (delay.is_zero()) { |
nasko | d3fd0db | 2016-10-07 06:22:32 +0900 | [diff] [blame] | 95 | origin_task_runner_->PostTask( |
| 96 | FROM_HERE, |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 97 | base::Bind(&AlarmTimer::OnTimerFired, weak_factory_.GetWeakPtr())); |
nasko | d3fd0db | 2016-10-07 06:22:32 +0900 | [diff] [blame] | 98 | } else { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 99 | // Otherwise, if the delay is not zero, generate a tracing event to indicate |
| 100 | // that the task was posted and watch |alarm_fd_|. |
| 101 | base::debug::TaskAnnotator().DidQueueTask("AlarmTimer::Reset", |
| 102 | *pending_task_); |
| 103 | alarm_fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 104 | alarm_fd_, base::Bind(&AlarmTimer::OnAlarmFdReadableWithoutBlocking, |
| 105 | weak_factory_.GetWeakPtr())); |
nasko | d3fd0db | 2016-10-07 06:22:32 +0900 | [diff] [blame] | 106 | } |
nasko | d3fd0db | 2016-10-07 06:22:32 +0900 | [diff] [blame] | 107 | } |
| 108 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 109 | void AlarmTimer::OnAlarmFdReadableWithoutBlocking() { |
| 110 | DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 111 | DCHECK(base::Timer::IsRunning()); |
| 112 | |
| 113 | // Read from |alarm_fd_| to ack the event. |
| 114 | char val[sizeof(uint64_t)]; |
| 115 | if (!base::ReadFromFD(alarm_fd_, val, sizeof(uint64_t))) |
| 116 | PLOG(DFATAL) << "Unable to read from timer file descriptor."; |
| 117 | |
| 118 | OnTimerFired(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void AlarmTimer::OnTimerFired() { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 122 | DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 123 | DCHECK(base::Timer::IsRunning()); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 124 | DCHECK(pending_task_.get()); |
| 125 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 126 | // Take ownership of the PendingTask to prevent it from being deleted if the |
| 127 | // AlarmTimer is deleted. |
| 128 | const auto pending_user_task = std::move(pending_task_); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 129 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 130 | base::WeakPtr<AlarmTimer> weak_ptr = weak_factory_.GetWeakPtr(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 131 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 132 | // Run the task. |
caseq | 341a961 | 2015-07-17 04:13:21 +0900 | [diff] [blame] | 133 | TRACE_TASK_EXECUTION("AlarmTimer::OnTimerFired", *pending_user_task); |
tzik | e82b7e8 | 2016-10-14 23:34:58 +0900 | [diff] [blame] | 134 | base::debug::TaskAnnotator().RunTask("AlarmTimer::Reset", |
| 135 | pending_user_task.get()); |
caseq | 341a961 | 2015-07-17 04:13:21 +0900 | [diff] [blame] | 136 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 137 | // If the timer wasn't deleted, stopped or reset by the callback, reset or |
| 138 | // stop it. |
| 139 | if (weak_ptr.get()) { |
| 140 | if (base::Timer::is_repeating()) |
| 141 | Reset(); |
| 142 | else |
| 143 | Stop(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | bool AlarmTimer::CanWakeFromSuspend() const { |
| 148 | return alarm_fd_ != -1; |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | OneShotAlarmTimer::OneShotAlarmTimer() : AlarmTimer(false, false) { |
| 152 | } |
| 153 | |
| 154 | OneShotAlarmTimer::~OneShotAlarmTimer() { |
| 155 | } |
| 156 | |
| 157 | RepeatingAlarmTimer::RepeatingAlarmTimer() : AlarmTimer(true, true) { |
| 158 | } |
| 159 | |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 160 | RepeatingAlarmTimer::~RepeatingAlarmTimer() { |
| 161 | } |
| 162 | |
| 163 | SimpleAlarmTimer::SimpleAlarmTimer() : AlarmTimer(true, false) { |
| 164 | } |
| 165 | |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 166 | SimpleAlarmTimer::~SimpleAlarmTimer() { |
| 167 | } |
| 168 | |
| 169 | } // namespace timers |