1 /*
2 * Copyright 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/log.h"
28 #include "os/utils.h"
29
30 #ifdef __ANDROID__
31 #define ALARM_CLOCK CLOCK_BOOTTIME_ALARM
32 #else
33 #define ALARM_CLOCK CLOCK_BOOTTIME
34 #endif
35
36 namespace bluetooth {
37 namespace os {
38 using common::Closure;
39 using common::OnceClosure;
40
Alarm(Handler * handler)41 Alarm::Alarm(Handler* handler) : handler_(handler), fd_(TIMERFD_CREATE(ALARM_CLOCK, 0)) {
42 log::assert_that(fd_ != -1, "cannot create timerfd: {}", strerror(errno));
43
44 token_ = handler_->thread_->GetReactor()->Register(
45 fd_, common::Bind(&Alarm::on_fire, common::Unretained(this)), Closure());
46 }
47
~Alarm()48 Alarm::~Alarm() {
49 handler_->thread_->GetReactor()->Unregister(token_);
50
51 int close_status;
52 RUN_NO_INTR(close_status = TIMERFD_CLOSE(fd_));
53 log::assert_that(close_status != -1, "assert failed: close_status != -1");
54 }
55
Schedule(OnceClosure task,std::chrono::milliseconds delay)56 void Alarm::Schedule(OnceClosure task, std::chrono::milliseconds delay) {
57 std::lock_guard<std::mutex> lock(mutex_);
58 long delay_ms = delay.count();
59 itimerspec timer_itimerspec{{/* interval for periodic timer */}, {delay_ms / 1000, delay_ms % 1000 * 1000000}};
60 int result = TIMERFD_SETTIME(fd_, 0, &timer_itimerspec, nullptr);
61 log::assert_that(result == 0, "assert failed: result == 0");
62
63 task_ = std::move(task);
64 }
65
Cancel()66 void Alarm::Cancel() {
67 std::lock_guard<std::mutex> lock(mutex_);
68 itimerspec disarm_itimerspec{/* disarm timer */};
69 int result = TIMERFD_SETTIME(fd_, 0, &disarm_itimerspec, nullptr);
70 log::assert_that(result == 0, "assert failed: result == 0");
71 }
72
on_fire()73 void Alarm::on_fire() {
74 std::unique_lock<std::mutex> lock(mutex_);
75 auto task = std::move(task_);
76 uint64_t times_invoked;
77 auto bytes_read = read(fd_, ×_invoked, sizeof(uint64_t));
78 lock.unlock();
79 log::assert_that(
80 bytes_read == static_cast<ssize_t>(sizeof(uint64_t)),
81 "assert failed: bytes_read == static_cast<ssize_t>(sizeof(uint64_t))");
82 log::assert_that(
83 times_invoked == static_cast<uint64_t>(1),
84 "Invoked number of times:{} fd:{}",
85 (unsigned long)times_invoked,
86 fd_);
87 std::move(task).Run();
88 }
89
90 } // namespace os
91 } // namespace bluetooth
92