blob: fd550200f3671affaedbef7e8835b6bd4c1e77b0 [file] [log] [blame]
Josh Gao95068bb2019-07-08 15:23:17 -07001/*
2 * Copyright 2006, Brian Swetland <swetland@frotz.net>
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 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG FDEVENT
JP Abgrall408fa572011-03-16 15:57:42 -070018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert33134262015-03-19 15:21:08 -070020
Josh Gaoded557f2018-06-18 14:55:27 -070021#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
Josh Gao2c95bf72019-07-08 18:05:16 -070023#include <android-base/logging.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080024#include <android-base/stringprintf.h>
Josh Gao2c95bf72019-07-08 18:05:16 -070025#include <android-base/threads.h>
Yabin Cuia1080162015-09-04 16:19:56 -070026
Josh Gao33944a22019-07-08 18:16:19 -070027#include "adb_utils.h"
Josh Gao95068bb2019-07-08 15:23:17 -070028#include "fdevent.h"
Josh Gaob43ad442019-07-11 13:47:44 -070029#include "fdevent_epoll.h"
Josh Gao95068bb2019-07-08 15:23:17 -070030#include "fdevent_poll.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Josh Gaoe22cb9f2019-07-11 16:35:37 -070032using namespace std::chrono_literals;
33using std::chrono::duration_cast;
34
35void invoke_fde(struct fdevent* fde, unsigned events) {
36 if (auto f = std::get_if<fd_func>(&fde->func)) {
37 (*f)(fde->fd.get(), events, fde->arg);
38 } else if (auto f = std::get_if<fd_func2>(&fde->func)) {
39 (*f)(fde, events, fde->arg);
40 } else {
41 __builtin_unreachable();
42 }
43}
44
Josh Gao95068bb2019-07-08 15:23:17 -070045std::string dump_fde(const fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070046 std::string state;
Yabin Cuia1080162015-09-04 16:19:56 -070047 if (fde->state & FDE_READ) {
48 state += "R";
49 }
50 if (fde->state & FDE_WRITE) {
51 state += "W";
52 }
53 if (fde->state & FDE_ERROR) {
54 state += "E";
55 }
Josh Gaoded557f2018-06-18 14:55:27 -070056 return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
57 state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058}
59
Josh Gao33944a22019-07-08 18:16:19 -070060fdevent* fdevent_context::Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg) {
61 CheckMainThread();
62 CHECK_GE(fd.get(), 0);
63
Josh Gaoe22cb9f2019-07-11 16:35:37 -070064 int fd_num = fd.get();
65
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -070066 auto [it, inserted] = this->installed_fdevents_.emplace(fd_num, fdevent{});
67 CHECK(inserted);
68
69 fdevent* fde = &it->second;
Josh Gao33944a22019-07-08 18:16:19 -070070 fde->id = fdevent_id_++;
Josh Gaoe22cb9f2019-07-11 16:35:37 -070071 fde->state = 0;
Josh Gao33944a22019-07-08 18:16:19 -070072 fde->fd = std::move(fd);
73 fde->func = func;
74 fde->arg = arg;
75 if (!set_file_block_mode(fde->fd, false)) {
76 // Here is not proper to handle the error. If it fails here, some error is
77 // likely to be detected by poll(), then we can let the callback function
78 // to handle it.
79 LOG(ERROR) << "failed to set non-blocking mode for fd " << fde->fd.get();
80 }
81
82 this->Register(fde);
83 return fde;
84}
85
86unique_fd fdevent_context::Destroy(fdevent* fde) {
87 CheckMainThread();
88 if (!fde) {
89 return {};
90 }
91
92 this->Unregister(fde);
93
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -070094 unique_fd fd = std::move(fde->fd);
95
96 auto erased = this->installed_fdevents_.erase(fd.get());
Josh Gaoe22cb9f2019-07-11 16:35:37 -070097 CHECK_EQ(1UL, erased);
98
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -070099 return fd;
Josh Gao33944a22019-07-08 18:16:19 -0700100}
101
Josh Gao35b29362019-07-08 18:17:41 -0700102void fdevent_context::Add(fdevent* fde, unsigned events) {
Josh Gaoe22cb9f2019-07-11 16:35:37 -0700103 CHECK(!(events & FDE_TIMEOUT));
104 Set(fde, fde->state | events);
Josh Gao35b29362019-07-08 18:17:41 -0700105}
106
107void fdevent_context::Del(fdevent* fde, unsigned events) {
108 CHECK(!(events & FDE_TIMEOUT));
Josh Gaoe22cb9f2019-07-11 16:35:37 -0700109 Set(fde, fde->state & ~events);
Josh Gao35b29362019-07-08 18:17:41 -0700110}
111
112void fdevent_context::SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) {
113 CheckMainThread();
114 fde->timeout = timeout;
115 fde->last_active = std::chrono::steady_clock::now();
116}
117
Josh Gaoe22cb9f2019-07-11 16:35:37 -0700118std::optional<std::chrono::milliseconds> fdevent_context::CalculatePollDuration() {
119 std::optional<std::chrono::milliseconds> result = std::nullopt;
120 auto now = std::chrono::steady_clock::now();
121 CheckMainThread();
122
123 for (const auto& [fd, fde] : this->installed_fdevents_) {
124 UNUSED(fd);
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -0700125 auto timeout_opt = fde.timeout;
Josh Gaoe22cb9f2019-07-11 16:35:37 -0700126 if (timeout_opt) {
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -0700127 auto deadline = fde.last_active + *timeout_opt;
Josh Gaoe22cb9f2019-07-11 16:35:37 -0700128 auto time_left = duration_cast<std::chrono::milliseconds>(deadline - now);
129 if (time_left < 0ms) {
130 time_left = 0ms;
131 }
132
133 if (!result) {
134 result = time_left;
135 } else {
136 result = std::min(*result, time_left);
137 }
138 }
139 }
140
141 return result;
142}
143
144void fdevent_context::HandleEvents(const std::vector<fdevent_event>& events) {
145 for (const auto& event : events) {
146 invoke_fde(event.fde, event.events);
147 }
148 FlushRunQueue();
149}
150
151void fdevent_context::FlushRunQueue() {
152 // We need to be careful around reentrancy here, since a function we call can queue up another
153 // function.
154 while (true) {
155 std::function<void()> fn;
156 {
157 std::lock_guard<std::mutex> lock(this->run_queue_mutex_);
158 if (this->run_queue_.empty()) {
159 break;
160 }
161 fn = std::move(this->run_queue_.front());
162 this->run_queue_.pop_front();
163 }
164 fn();
165 }
166}
167
Josh Gao2c95bf72019-07-08 18:05:16 -0700168void fdevent_context::CheckMainThread() {
169 if (main_thread_id_) {
170 CHECK_EQ(*main_thread_id_, android::base::GetThreadId());
171 }
172}
173
Josh Gao95eef6b2019-07-08 17:37:23 -0700174void fdevent_context::Run(std::function<void()> fn) {
175 {
176 std::lock_guard<std::mutex> lock(run_queue_mutex_);
177 run_queue_.push_back(std::move(fn));
178 }
179
180 Interrupt();
181}
182
Josh Gaoebaa3482019-07-08 18:08:06 -0700183void fdevent_context::TerminateLoop() {
184 terminate_loop_ = true;
185 Interrupt();
186}
187
Josh Gaob43ad442019-07-11 13:47:44 -0700188static std::unique_ptr<fdevent_context> fdevent_create_context() {
189#if defined(__linux__)
190 return std::make_unique<fdevent_context_epoll>();
191#else
192 return std::make_unique<fdevent_context_poll>();
193#endif
194}
195
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -0700196static auto& g_ambient_fdevent_context() {
197 static auto context = fdevent_create_context().release();
198 return context;
199}
Josh Gaoc2cf1212019-06-28 16:34:36 -0700200
201static fdevent_context* fdevent_get_ambient() {
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -0700202 return g_ambient_fdevent_context();
Josh Gaoc2cf1212019-06-28 16:34:36 -0700203}
204
Josh Gaoc2cf1212019-06-28 16:34:36 -0700205fdevent* fdevent_create(int fd, fd_func func, void* arg) {
206 unique_fd ufd(fd);
207 return fdevent_get_ambient()->Create(std::move(ufd), func, arg);
208}
209
210fdevent* fdevent_create(int fd, fd_func2 func, void* arg) {
211 unique_fd ufd(fd);
212 return fdevent_get_ambient()->Create(std::move(ufd), func, arg);
213}
214
215unique_fd fdevent_release(fdevent* fde) {
216 return fdevent_get_ambient()->Destroy(fde);
217}
218
219void fdevent_destroy(fdevent* fde) {
220 fdevent_get_ambient()->Destroy(fde);
221}
222
223void fdevent_set(fdevent* fde, unsigned events) {
224 fdevent_get_ambient()->Set(fde, events);
225}
226
227void fdevent_add(fdevent* fde, unsigned events) {
228 fdevent_get_ambient()->Add(fde, events);
229}
230
231void fdevent_del(fdevent* fde, unsigned events) {
232 fdevent_get_ambient()->Del(fde, events);
233}
234
235void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) {
236 fdevent_get_ambient()->SetTimeout(fde, timeout);
237}
238
239void fdevent_run_on_main_thread(std::function<void()> fn) {
240 fdevent_get_ambient()->Run(std::move(fn));
241}
242
243void fdevent_loop() {
244 fdevent_get_ambient()->Loop();
245}
246
247void check_main_thread() {
248 fdevent_get_ambient()->CheckMainThread();
249}
250
Josh Gao022d4472016-02-10 14:49:00 -0800251void fdevent_terminate_loop() {
Josh Gaoc2cf1212019-06-28 16:34:36 -0700252 fdevent_get_ambient()->TerminateLoop();
Josh Gao022d4472016-02-10 14:49:00 -0800253}
254
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700255size_t fdevent_installed_count() {
Josh Gaoc2cf1212019-06-28 16:34:36 -0700256 return fdevent_get_ambient()->InstalledCount();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700257}
258
259void fdevent_reset() {
Yurii Zubrytskyi4eb910e2020-03-18 22:29:10 -0700260 auto old = std::exchange(g_ambient_fdevent_context(), fdevent_create_context().release());
261 delete old;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700262}