1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "os/alarm.h"
18
19 #include <bluetooth/log.h>
20 #include <sys/timerfd.h>
21 #include <unistd.h>
22
23 #include <cstring>
24
25 #include "common/bind.h"
26 #include "os/linux_generic/linux.h"
27 #include "os/utils.h"
28
29 #ifdef __ANDROID__
30 #define ALARM_CLOCK CLOCK_BOOTTIME_ALARM
31 #else
32 #define ALARM_CLOCK CLOCK_BOOTTIME
33 #endif
34
35 namespace bluetooth {
36 namespace os {
37 using common::Closure;
38 using common::OnceClosure;
39
Alarm(Handler * handler)40 Alarm::Alarm(Handler* handler) : Alarm(handler, true) {}
41
Alarm(Handler * handler,bool isWakeAlarm)42 Alarm::Alarm(Handler* handler, bool isWakeAlarm) : handler_(handler) {
43 int timerfd_flag = TFD_NONBLOCK;
44
45 fd_ = TIMERFD_CREATE(isWakeAlarm ? ALARM_CLOCK : CLOCK_BOOTTIME, timerfd_flag);
46
47 log::assert_that(fd_ != -1, "cannot create timerfd: {}", strerror(errno));
48
49 token_ = handler_->thread_->GetReactor()->Register(
50 fd_, common::Bind(&Alarm::on_fire, common::Unretained(this)), Closure());
51 }
52
~Alarm()53 Alarm::~Alarm() {
54 handler_->thread_->GetReactor()->Unregister(token_);
55
56 int close_status;
57 RUN_NO_INTR(close_status = TIMERFD_CLOSE(fd_));
58 log::assert_that(close_status != -1, "assert failed: close_status != -1");
59 }
60
Schedule(OnceClosure task,std::chrono::milliseconds delay)61 void Alarm::Schedule(OnceClosure task, std::chrono::milliseconds delay) {
62 std::lock_guard<std::mutex> lock(mutex_);
63 long delay_ms = delay.count();
64 itimerspec timer_itimerspec{{/* interval for periodic timer */},
65 {delay_ms / 1000, delay_ms % 1000 * 1000000}};
66 int result = TIMERFD_SETTIME(fd_, 0, &timer_itimerspec, nullptr);
67 log::assert_that(result == 0, "assert failed: result == 0");
68
69 task_ = std::move(task);
70 }
71
Cancel()72 void Alarm::Cancel() {
73 std::lock_guard<std::mutex> lock(mutex_);
74 itimerspec disarm_itimerspec{/* disarm timer */};
75 int result = TIMERFD_SETTIME(fd_, 0, &disarm_itimerspec, nullptr);
76 log::assert_that(result == 0, "assert failed: result == 0");
77 }
78
on_fire()79 void Alarm::on_fire() {
80 std::unique_lock<std::mutex> lock(mutex_);
81 auto task = std::move(task_);
82 uint64_t times_invoked;
83 auto bytes_read = read(fd_, ×_invoked, sizeof(uint64_t));
84 lock.unlock();
85
86 if (bytes_read == -1) {
87 log::debug("No data to read.");
88 if (errno == EAGAIN || errno == EWOULDBLOCK) {
89 log::debug("Alarm is already canceled or rescheduled.");
90 return;
91 }
92 }
93
94 log::assert_that(bytes_read == static_cast<ssize_t>(sizeof(uint64_t)),
95 "assert failed: bytes_read == static_cast<ssize_t>(sizeof(uint64_t))");
96 log::assert_that(times_invoked == 1u, "Invoked number of times:{} fd:{}", times_invoked, fd_);
97
98 if (task.is_null()) {
99 log::warn("task is null.");
100 return;
101 }
102 std::move(task).Run();
103 }
104
105 } // namespace os
106 } // namespace bluetooth
107