| 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_chromeos.h" | 5 #include "components/timers/alarm_timer_chromeos.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <sys/timerfd.h> | 8 #include <sys/timerfd.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/debug/task_annotator.h" | 14 #include "base/debug/task_annotator.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/pending_task.h" | 18 #include "base/pending_task.h" |
| 19 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
| 20 | 20 |
| 21 namespace timers { | 21 namespace timers { |
| 22 | 22 |
| 23 AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating) | 23 AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating) |
| 24 : base::Timer(retain_user_task, is_repeating), | 24 : base::Timer(retain_user_task, is_repeating), |
| 25 alarm_fd_(timerfd_create(CLOCK_REALTIME_ALARM, 0)), | 25 alarm_fd_(timerfd_create(CLOCK_REALTIME_ALARM, 0)), |
| 26 weak_factory_(this) {} | 26 weak_factory_(this) {} |
| 27 | 27 |
| 28 AlarmTimer::~AlarmTimer() { | 28 AlarmTimer::~AlarmTimer() { |
| 29 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 29 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
| 30 Stop(); | 30 Stop(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void AlarmTimer::Stop() { | 33 void AlarmTimer::Stop() { |
| 34 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 34 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
| 35 | 35 |
| 36 if (!base::Timer::is_running()) | 36 if (!base::Timer::is_running()) |
| 37 return; | 37 return; |
| 38 | 38 |
| 39 if (!CanWakeFromSuspend()) { | 39 if (!CanWakeFromSuspend()) { |
| 40 base::Timer::Stop(); | 40 base::Timer::Stop(); |
| 41 return; | 41 return; |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Cancel any previous callbacks. | 44 // Cancel any previous callbacks. |
| 45 weak_factory_.InvalidateWeakPtrs(); | 45 weak_factory_.InvalidateWeakPtrs(); |
| 46 | 46 |
| 47 base::Timer::set_is_running(false); | 47 base::Timer::set_is_running(false); |
| 48 alarm_fd_watcher_.reset(); | 48 alarm_fd_watcher_.reset(); |
| 49 pending_task_.reset(); | 49 pending_task_.reset(); |
| 50 | 50 |
| 51 if (!base::Timer::retain_user_task()) | 51 if (!base::Timer::retain_user_task()) |
| 52 base::Timer::set_user_task(base::Closure()); | 52 base::Timer::set_user_task(base::Closure()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void AlarmTimer::Reset() { | 55 void AlarmTimer::Reset() { |
| 56 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 56 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
| 57 DCHECK(!base::Timer::user_task().is_null()); | 57 DCHECK(!base::Timer::user_task().is_null()); |
| 58 | 58 |
| 59 if (!CanWakeFromSuspend()) { | 59 if (!CanWakeFromSuspend()) { |
| 60 base::Timer::Reset(); | 60 base::Timer::Reset(); |
| 61 return; | 61 return; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Cancel any previous callbacks and stop watching |alarm_fd_|. | 64 // Cancel any previous callbacks and stop watching |alarm_fd_|. |
| 65 weak_factory_.InvalidateWeakPtrs(); | 65 weak_factory_.InvalidateWeakPtrs(); |
| 66 alarm_fd_watcher_.reset(); | 66 alarm_fd_watcher_.reset(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // that the task was posted and watch |alarm_fd_|. | 100 // that the task was posted and watch |alarm_fd_|. |
| 101 base::debug::TaskAnnotator().DidQueueTask("AlarmTimer::Reset", | 101 base::debug::TaskAnnotator().DidQueueTask("AlarmTimer::Reset", |
| 102 *pending_task_); | 102 *pending_task_); |
| 103 alarm_fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( | 103 alarm_fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 104 alarm_fd_, base::Bind(&AlarmTimer::OnAlarmFdReadableWithoutBlocking, | 104 alarm_fd_, base::Bind(&AlarmTimer::OnAlarmFdReadableWithoutBlocking, |
| 105 weak_factory_.GetWeakPtr())); | 105 weak_factory_.GetWeakPtr())); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 void AlarmTimer::OnAlarmFdReadableWithoutBlocking() { | 109 void AlarmTimer::OnAlarmFdReadableWithoutBlocking() { |
| 110 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 110 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
| 111 DCHECK(base::Timer::IsRunning()); | 111 DCHECK(base::Timer::IsRunning()); |
| 112 | 112 |
| 113 // Read from |alarm_fd_| to ack the event. | 113 // Read from |alarm_fd_| to ack the event. |
| 114 char val[sizeof(uint64_t)]; | 114 char val[sizeof(uint64_t)]; |
| 115 if (!base::ReadFromFD(alarm_fd_, val, sizeof(uint64_t))) | 115 if (!base::ReadFromFD(alarm_fd_, val, sizeof(uint64_t))) |
| 116 PLOG(DFATAL) << "Unable to read from timer file descriptor."; | 116 PLOG(DFATAL) << "Unable to read from timer file descriptor."; |
| 117 | 117 |
| 118 OnTimerFired(); | 118 OnTimerFired(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void AlarmTimer::OnTimerFired() { | 121 void AlarmTimer::OnTimerFired() { |
| 122 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 122 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
| 123 DCHECK(base::Timer::IsRunning()); | 123 DCHECK(base::Timer::IsRunning()); |
| 124 DCHECK(pending_task_.get()); | 124 DCHECK(pending_task_.get()); |
| 125 | 125 |
| 126 // Take ownership of the PendingTask to prevent it from being deleted if the | 126 // Take ownership of the PendingTask to prevent it from being deleted if the |
| 127 // AlarmTimer is deleted. | 127 // AlarmTimer is deleted. |
| 128 const auto pending_user_task = std::move(pending_task_); | 128 const auto pending_user_task = std::move(pending_task_); |
| 129 | 129 |
| 130 base::WeakPtr<AlarmTimer> weak_ptr = weak_factory_.GetWeakPtr(); | 130 base::WeakPtr<AlarmTimer> weak_ptr = weak_factory_.GetWeakPtr(); |
| 131 | 131 |
| 132 // Run the task. | 132 // Run the task. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 160 RepeatingAlarmTimer::~RepeatingAlarmTimer() { | 160 RepeatingAlarmTimer::~RepeatingAlarmTimer() { |
| 161 } | 161 } |
| 162 | 162 |
| 163 SimpleAlarmTimer::SimpleAlarmTimer() : AlarmTimer(true, false) { | 163 SimpleAlarmTimer::SimpleAlarmTimer() : AlarmTimer(true, false) { |
| 164 } | 164 } |
| 165 | 165 |
| 166 SimpleAlarmTimer::~SimpleAlarmTimer() { | 166 SimpleAlarmTimer::~SimpleAlarmTimer() { |
| 167 } | 167 } |
| 168 | 168 |
| 169 } // namespace timers | 169 } // namespace timers |
| OLD | NEW |