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/reactor.h"
18
19 #include <fcntl.h>
20 #include <sys/epoll.h>
21 #include <sys/eventfd.h>
22 #include <unistd.h>
23 #include <algorithm>
24 #include <cerrno>
25 #include <cstring>
26
27 #include "os/log.h"
28
29 namespace {
30
31 // Use at most sizeof(epoll_event) * kEpollMaxEvents kernel memory
32 constexpr int kEpollMaxEvents = 64;
33
34 } // namespace
35
36 namespace bluetooth {
37 namespace os {
38
39 class Reactor::Reactable {
40 public:
Reactable(int fd,Closure on_read_ready,Closure on_write_ready)41 Reactable(int fd, Closure on_read_ready, Closure on_write_ready)
42 : fd_(fd),
43 on_read_ready_(std::move(on_read_ready)),
44 on_write_ready_(std::move(on_write_ready)),
45 is_executing_(false) {}
46 const int fd_;
47 Closure on_read_ready_;
48 Closure on_write_ready_;
49 bool is_executing_;
50 std::recursive_mutex lock_;
51 };
52
Reactor()53 Reactor::Reactor()
54 : epoll_fd_(0),
55 control_fd_(0),
56 is_running_(false),
57 reactable_removed_(false) {
58 RUN_NO_INTR(epoll_fd_ = epoll_create1(EPOLL_CLOEXEC));
59 ASSERT_LOG(epoll_fd_ != -1, "could not create epoll fd: %s", strerror(errno));
60
61 control_fd_ = eventfd(0, EFD_NONBLOCK);
62 ASSERT(control_fd_ != -1);
63
64 epoll_event control_epoll_event = {EPOLLIN, {.ptr = nullptr}};
65 int result;
66 RUN_NO_INTR(result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, control_fd_, &control_epoll_event));
67 ASSERT(result != -1);
68 }
69
~Reactor()70 Reactor::~Reactor() {
71 int result;
72 RUN_NO_INTR(result = epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, control_fd_, nullptr));
73 ASSERT(result != -1);
74
75 RUN_NO_INTR(result = close(control_fd_));
76 ASSERT(result != -1);
77
78 RUN_NO_INTR(result = close(epoll_fd_));
79 ASSERT(result != -1);
80 }
81
Run()82 void Reactor::Run() {
83 bool previously_running = is_running_.exchange(true);
84 ASSERT(!previously_running);
85
86 for (;;) {
87 invalidation_list_.clear();
88 epoll_event events[kEpollMaxEvents];
89 int count;
90 RUN_NO_INTR(count = epoll_wait(epoll_fd_, events, kEpollMaxEvents, -1));
91 ASSERT(count != -1);
92
93 for (int i = 0; i < count; ++i) {
94 auto event = events[i];
95 ASSERT(event.events != 0u);
96
97 // If the ptr stored in epoll_event.data is nullptr, it means the control fd triggered
98 if (event.data.ptr == nullptr) {
99 uint64_t value;
100 eventfd_read(control_fd_, &value);
101 is_running_ = false;
102 return;
103 }
104 auto* reactable = static_cast<Reactor::Reactable*>(event.data.ptr);
105 {
106 std::unique_lock<std::mutex> lock(mutex_);
107 // See if this reactable has been removed in the meantime.
108 if (std::find(invalidation_list_.begin(), invalidation_list_.end(), reactable) != invalidation_list_.end()) {
109 continue;
110 }
111
112 std::lock_guard<std::recursive_mutex> reactable_lock(reactable->lock_);
113 lock.unlock();
114 reactable_removed_ = false;
115 reactable->is_executing_ = true;
116 if (event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR) && reactable->on_read_ready_ != nullptr) {
117 reactable->on_read_ready_();
118 }
119 if (!reactable_removed_ && event.events & EPOLLOUT && reactable->on_write_ready_ != nullptr) {
120 reactable->on_write_ready_();
121 }
122 reactable->is_executing_ = false;
123 }
124 if (reactable_removed_) {
125 delete reactable;
126 }
127 }
128 }
129 }
130
Stop()131 void Reactor::Stop() {
132 if (!is_running_) {
133 LOG_WARN("not running, will stop once it's started");
134 }
135 auto control = eventfd_write(control_fd_, 1);
136 ASSERT(control != -1)
137 }
138
Register(int fd,Closure on_read_ready,Closure on_write_ready)139 Reactor::Reactable* Reactor::Register(int fd, Closure on_read_ready, Closure on_write_ready) {
140 uint32_t poll_event_type = 0;
141 if (on_read_ready != nullptr) {
142 poll_event_type |= (EPOLLIN | EPOLLRDHUP);
143 }
144 if (on_write_ready != nullptr) {
145 poll_event_type |= EPOLLOUT;
146 }
147 auto* reactable = new Reactable(fd, on_read_ready, on_write_ready);
148 epoll_event event = {
149 .events = poll_event_type,
150 {.ptr = reactable}
151 };
152 int register_fd;
153 RUN_NO_INTR(register_fd = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event));
154 ASSERT(register_fd != -1)
155 return reactable;
156 }
157
Unregister(Reactor::Reactable * reactable)158 void Reactor::Unregister(Reactor::Reactable* reactable) {
159 ASSERT(reactable != nullptr);
160 {
161 std::lock_guard<std::mutex> lock(mutex_);
162 invalidation_list_.push_back(reactable);
163 }
164 {
165 int result;
166 std::lock_guard<std::recursive_mutex> reactable_lock(reactable->lock_);
167 RUN_NO_INTR(result = epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, reactable->fd_, nullptr));
168 if (result == -1 && errno == ENOENT) {
169 LOG_INFO("reactable is invalid or unregistered");
170 } else {
171 ASSERT(result != -1);
172 }
173 // If we are unregistering during the callback event from this reactable, we delete it after the callback is executed.
174 // reactable->is_executing_ is protected by reactable->lock_, so it's thread safe.
175 if (reactable->is_executing_) {
176 reactable_removed_ = true;
177 }
178 }
179 // If we are unregistering outside of the callback event from this reactable, we delete it now
180 if (!reactable_removed_) {
181 delete reactable;
182 }
183 }
184
ModifyRegistration(Reactor::Reactable * reactable,Closure on_read_ready,Closure on_write_ready)185 void Reactor::ModifyRegistration(Reactor::Reactable* reactable, Closure on_read_ready, Closure on_write_ready) {
186 ASSERT(reactable != nullptr);
187
188 uint32_t poll_event_type = 0;
189 if (on_read_ready != nullptr) {
190 poll_event_type |= (EPOLLIN | EPOLLRDHUP);
191 }
192 if (on_write_ready != nullptr) {
193 poll_event_type |= EPOLLOUT;
194 }
195 {
196 std::lock_guard<std::recursive_mutex> reactable_lock(reactable->lock_);
197 reactable->on_read_ready_ = std::move(on_read_ready);
198 reactable->on_write_ready_ = std::move(on_write_ready);
199 }
200 epoll_event event = {
201 .events = poll_event_type,
202 {.ptr = reactable}
203 };
204 int modify_fd;
205 RUN_NO_INTR(modify_fd = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, reactable->fd_, &event));
206 ASSERT(modify_fd != -1);
207 }
208
209 } // namespace os
210 } // namespace bluetooth
211