blob: fa3738d1699476b0c160dac36de75b9ffc7a5bdb [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c
2**
3** Copyright 2006, Brian Swetland <swetland@frotz.net>
4**
Elliott Hughesaa245492015-08-03 10:38:08 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008**
Elliott Hughesaa245492015-08-03 10:38:08 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010**
Elliott Hughesaa245492015-08-03 10:38:08 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015** limitations under the License.
16*/
17
Yabin Cuiaed3c612015-09-22 15:52:57 -070018#define TRACE_TAG FDEVENT
JP Abgrall408fa572011-03-16 15:57:42 -070019
Dan Albert33134262015-03-19 15:21:08 -070020#include "sysdeps.h"
21#include "fdevent.h"
22
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <fcntl.h>
Josh Gaoded557f2018-06-18 14:55:27 -070024#include <inttypes.h>
Josh Gao5791e212018-03-16 14:25:42 -070025#include <stdint.h>
Dan Albert33134262015-03-19 15:21:08 -070026#include <stdlib.h>
27#include <string.h>
Dan Albert33134262015-03-19 15:21:08 -070028#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Josh Gao022d4472016-02-10 14:49:00 -080030#include <atomic>
Josh Gaoe39ccd32018-02-23 14:37:07 -080031#include <deque>
Josh Gao4c936392017-05-03 14:10:39 -070032#include <functional>
Yabin Cuia1080162015-09-04 16:19:56 -070033#include <list>
Josh Gao4c936392017-05-03 14:10:39 -070034#include <mutex>
Yabin Cuia1080162015-09-04 16:19:56 -070035#include <unordered_map>
Josh Gaoc162c712019-01-25 15:30:25 -080036#include <utility>
37#include <variant>
Yabin Cuia1080162015-09-04 16:19:56 -070038#include <vector>
39
Josh Gao39c1f4b2018-10-05 17:09:07 -070040#include <android-base/chrono_utils.h>
41#include <android-base/file.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080042#include <android-base/logging.h>
43#include <android-base/stringprintf.h>
Josh Gao4c936392017-05-03 14:10:39 -070044#include <android-base/thread_annotations.h>
Josh Gao5791e212018-03-16 14:25:42 -070045#include <android-base/threads.h>
Yabin Cuia1080162015-09-04 16:19:56 -070046
Dan Albertcc731cc2015-02-24 21:26:58 -080047#include "adb_io.h"
leozwangd3fc15f2014-07-29 12:50:02 -070048#include "adb_trace.h"
Josh Gao4c936392017-05-03 14:10:39 -070049#include "adb_unique_fd.h"
Yabin Cui6dfef252015-10-06 15:10:05 -070050#include "adb_utils.h"
Josh Gao39c1f4b2018-10-05 17:09:07 -070051#include "sysdeps/chrono.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#define FDE_EVENTMASK 0x00ff
54#define FDE_STATEMASK 0xff00
55
56#define FDE_ACTIVE 0x0100
57#define FDE_PENDING 0x0200
58#define FDE_CREATED 0x0400
59
Yabin Cuia1080162015-09-04 16:19:56 -070060struct PollNode {
61 fdevent* fde;
Josh Gao3777d2e2016-02-16 17:34:53 -080062 adb_pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070064 explicit PollNode(fdevent* fde) : fde(fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070065 memset(&pollfd, 0, sizeof(pollfd));
Josh Gao3b37fa22018-05-14 13:42:49 -070066 pollfd.fd = fde->fd.get();
Yabin Cuiaa77e222015-09-29 12:25:33 -070067
68#if defined(__linux__)
69 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
70 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
71 pollfd.events = POLLRDHUP;
72#endif
Yabin Cuia1080162015-09-04 16:19:56 -070073 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074};
75
Yabin Cuia1080162015-09-04 16:19:56 -070076// All operations to fdevent should happen only in the main thread.
77// That's why we don't need a lock for fdevent.
Josh Gaob7b1edf2015-11-11 17:56:12 -080078static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
79static auto& g_pending_list = *new std::list<fdevent*>();
Josh Gao022d4472016-02-10 14:49:00 -080080static std::atomic<bool> terminate_loop(false);
Yabin Cuifbfa8402015-10-30 18:37:26 -070081static bool main_thread_valid;
Josh Gao5791e212018-03-16 14:25:42 -070082static uint64_t main_thread_id;
Yabin Cuifbfa8402015-10-30 18:37:26 -070083
Josh Gaoded557f2018-06-18 14:55:27 -070084static uint64_t fdevent_id;
85
Josh Gaofa30bf32018-03-29 16:23:43 -070086static bool run_needs_flush = false;
Josh Gao4c936392017-05-03 14:10:39 -070087static auto& run_queue_notify_fd = *new unique_fd();
88static auto& run_queue_mutex = *new std::mutex();
Josh Gaoe39ccd32018-02-23 14:37:07 -080089static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
Josh Gao4c936392017-05-03 14:10:39 -070090
Yabin Cuib5e11412017-03-10 16:01:01 -080091void check_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070092 if (main_thread_valid) {
Josh Gao5791e212018-03-16 14:25:42 -070093 CHECK_EQ(main_thread_id, android::base::GetThreadId());
Yabin Cuifbfa8402015-10-30 18:37:26 -070094 }
95}
96
Yabin Cuib5e11412017-03-10 16:01:01 -080097void set_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070098 main_thread_valid = true;
Josh Gao5791e212018-03-16 14:25:42 -070099 main_thread_id = android::base::GetThreadId();
Yabin Cuifbfa8402015-10-30 18:37:26 -0700100}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
Yabin Cuia1080162015-09-04 16:19:56 -0700102static std::string dump_fde(const fdevent* fde) {
103 std::string state;
104 if (fde->state & FDE_ACTIVE) {
105 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 }
Yabin Cuia1080162015-09-04 16:19:56 -0700107 if (fde->state & FDE_PENDING) {
108 state += "P";
109 }
110 if (fde->state & FDE_CREATED) {
111 state += "C";
112 }
113 if (fde->state & FDE_READ) {
114 state += "R";
115 }
116 if (fde->state & FDE_WRITE) {
117 state += "W";
118 }
119 if (fde->state & FDE_ERROR) {
120 state += "E";
121 }
Josh Gaoded557f2018-06-18 14:55:27 -0700122 return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
123 state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124}
125
Josh Gaoc162c712019-01-25 15:30:25 -0800126template <typename F>
127static fdevent* fdevent_create_impl(int fd, F func, void* arg) {
Josh Gao71f775a2018-05-14 11:14:33 -0700128 check_main_thread();
129 CHECK_GE(fd, 0);
130
131 fdevent* fde = new fdevent();
Josh Gaoded557f2018-06-18 14:55:27 -0700132 fde->id = fdevent_id++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 fde->state = FDE_ACTIVE;
Josh Gao3b37fa22018-05-14 13:42:49 -0700134 fde->fd.reset(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 fde->func = func;
136 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700137 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700138 // Here is not proper to handle the error. If it fails here, some error is
139 // likely to be detected by poll(), then we can let the callback function
140 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700141 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700142 }
Josh Gao3b37fa22018-05-14 13:42:49 -0700143 auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
Yabin Cuia1080162015-09-04 16:19:56 -0700144 CHECK(pair.second) << "install existing fd " << fd;
Josh Gao71f775a2018-05-14 11:14:33 -0700145
146 fde->state |= FDE_CREATED;
147 return fde;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148}
149
Josh Gaoc162c712019-01-25 15:30:25 -0800150fdevent* fdevent_create(int fd, fd_func func, void* arg) {
151 return fdevent_create_impl(fd, func, arg);
152}
153
154fdevent* fdevent_create(int fd, fd_func2 func, void* arg) {
155 return fdevent_create_impl(fd, func, arg);
156}
157
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700158unique_fd fdevent_release(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700159 check_main_thread();
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700160 if (!fde) {
161 return {};
162 }
163
Josh Gao71f775a2018-05-14 11:14:33 -0700164 if (!(fde->state & FDE_CREATED)) {
165 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
166 }
167
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700168 unique_fd result = std::move(fde->fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700169 if (fde->state & FDE_ACTIVE) {
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700170 g_poll_node_map.erase(result.get());
171
Yabin Cuia1080162015-09-04 16:19:56 -0700172 if (fde->state & FDE_PENDING) {
173 g_pending_list.remove(fde);
174 }
Yabin Cuia1080162015-09-04 16:19:56 -0700175 fde->state = 0;
176 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
Josh Gao71f775a2018-05-14 11:14:33 -0700178
179 delete fde;
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700180 return result;
181}
182
183void fdevent_destroy(fdevent* fde) {
184 // Release, and then let unique_fd's destructor cleanup.
185 fdevent_release(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186}
187
Yabin Cuia1080162015-09-04 16:19:56 -0700188static void fdevent_update(fdevent* fde, unsigned events) {
Josh Gao3b37fa22018-05-14 13:42:49 -0700189 auto it = g_poll_node_map.find(fde->fd.get());
Yabin Cuia1080162015-09-04 16:19:56 -0700190 CHECK(it != g_poll_node_map.end());
191 PollNode& node = it->second;
192 if (events & FDE_READ) {
193 node.pollfd.events |= POLLIN;
194 } else {
195 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197
Yabin Cuia1080162015-09-04 16:19:56 -0700198 if (events & FDE_WRITE) {
199 node.pollfd.events |= POLLOUT;
200 } else {
201 node.pollfd.events &= ~POLLOUT;
202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700204}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
Yabin Cuia1080162015-09-04 16:19:56 -0700206void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700207 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700208 events &= FDE_EVENTMASK;
209 if ((fde->state & FDE_EVENTMASK) == events) {
210 return;
211 }
Spencer Low888a7482015-09-29 18:33:38 -0700212 CHECK(fde->state & FDE_ACTIVE);
213 fdevent_update(fde, events);
214 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700215
Spencer Low888a7482015-09-29 18:33:38 -0700216 if (fde->state & FDE_PENDING) {
217 // If we are pending, make sure we don't signal an event that is no longer wanted.
218 fde->events &= events;
219 if (fde->events == 0) {
220 g_pending_list.remove(fde);
221 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 }
223 }
224}
225
Yabin Cuia1080162015-09-04 16:19:56 -0700226void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700227 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700228 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229}
230
Yabin Cuia1080162015-09-04 16:19:56 -0700231void fdevent_del(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700232 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700233 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234}
235
Josh Gao3777d2e2016-02-16 17:34:53 -0800236static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700237 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700238 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700239 std::string op;
240 if (pollfd.events & POLLIN) {
241 op += "R";
242 }
243 if (pollfd.events & POLLOUT) {
244 op += "W";
245 }
246 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
247 }
248 return result;
249}
250
251static void fdevent_process() {
Josh Gao3777d2e2016-02-16 17:34:53 -0800252 std::vector<adb_pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700253 for (const auto& pair : g_poll_node_map) {
254 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700255 }
256 CHECK_GT(pollfds.size(), 0u);
257 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao39c1f4b2018-10-05 17:09:07 -0700258
Josh Gao3777d2e2016-02-16 17:34:53 -0800259 int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
Yabin Cuia1080162015-09-04 16:19:56 -0700260 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700261 PLOG(ERROR) << "poll(), ret = " << ret;
262 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700263 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700264 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700265 if (pollfd.revents != 0) {
266 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
267 }
Yabin Cuia1080162015-09-04 16:19:56 -0700268 unsigned events = 0;
269 if (pollfd.revents & POLLIN) {
270 events |= FDE_READ;
271 }
272 if (pollfd.revents & POLLOUT) {
273 events |= FDE_WRITE;
274 }
275 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
276 // We fake a read, as the rest of the code assumes that errors will
277 // be detected at that point.
278 events |= FDE_READ | FDE_ERROR;
279 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700280#if defined(__linux__)
281 if (pollfd.revents & POLLRDHUP) {
282 events |= FDE_READ | FDE_ERROR;
283 }
284#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700285 if (events != 0) {
286 auto it = g_poll_node_map.find(pollfd.fd);
287 CHECK(it != g_poll_node_map.end());
288 fdevent* fde = it->second.fde;
Josh Gao3b37fa22018-05-14 13:42:49 -0700289 CHECK_EQ(fde->fd.get(), pollfd.fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700290 fde->events |= events;
291 D("%s got events %x", dump_fde(fde).c_str(), events);
292 fde->state |= FDE_PENDING;
293 g_pending_list.push_back(fde);
294 }
295 }
296}
297
Josh Gaoc162c712019-01-25 15:30:25 -0800298template <class T>
299struct always_false : std::false_type {};
300
Josh Gao4c936392017-05-03 14:10:39 -0700301static void fdevent_call_fdfunc(fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -0700302 unsigned events = fde->events;
303 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700304 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700305 fde->state &= (~FDE_PENDING);
306 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
Josh Gaoc162c712019-01-25 15:30:25 -0800307 std::visit(
308 [&](auto&& f) {
309 using F = std::decay_t<decltype(f)>;
310 if constexpr (std::is_same_v<fd_func, F>) {
311 f(fde->fd.get(), events, fde->arg);
312 } else if constexpr (std::is_same_v<fd_func2, F>) {
313 f(fde, events, fde->arg);
314 } else {
315 static_assert(always_false<F>::value, "non-exhaustive visitor");
316 }
317 },
318 fde->func);
Yabin Cuia1080162015-09-04 16:19:56 -0700319}
320
Josh Gaoe39ccd32018-02-23 14:37:07 -0800321static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
322 // We need to be careful around reentrancy here, since a function we call can queue up another
323 // function.
324 while (true) {
325 std::function<void()> fn;
326 {
327 std::lock_guard<std::mutex> lock(run_queue_mutex);
328 if (run_queue.empty()) {
329 break;
330 }
331 fn = run_queue.front();
332 run_queue.pop_front();
333 }
334 fn();
Josh Gao4c936392017-05-03 14:10:39 -0700335 }
Josh Gao4c936392017-05-03 14:10:39 -0700336}
337
338static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
339 CHECK_GE(fd, 0);
340 CHECK(ev & FDE_READ);
341
342 char buf[1024];
343
344 // Empty the fd.
345 if (adb_read(fd, buf, sizeof(buf)) == -1) {
346 PLOG(FATAL) << "failed to empty run queue notify fd";
347 }
348
Josh Gaofa30bf32018-03-29 16:23:43 -0700349 // Mark that we need to flush, and then run it at the end of fdevent_loop.
350 run_needs_flush = true;
Josh Gao4c936392017-05-03 14:10:39 -0700351}
352
353static void fdevent_run_setup() {
Josh Gaoe39ccd32018-02-23 14:37:07 -0800354 {
355 std::lock_guard<std::mutex> lock(run_queue_mutex);
356 CHECK(run_queue_notify_fd.get() == -1);
357 int s[2];
358 if (adb_socketpair(s) != 0) {
359 PLOG(FATAL) << "failed to create run queue notify socketpair";
360 }
Josh Gao4c936392017-05-03 14:10:39 -0700361
Josh Gao1222abc2018-03-19 15:19:45 -0700362 if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
363 PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
364 }
365
Josh Gaoe39ccd32018-02-23 14:37:07 -0800366 run_queue_notify_fd.reset(s[0]);
367 fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
368 CHECK(fde != nullptr);
369 fdevent_add(fde, FDE_READ);
370 }
Josh Gao4c936392017-05-03 14:10:39 -0700371
372 fdevent_run_flush();
373}
374
375void fdevent_run_on_main_thread(std::function<void()> fn) {
376 std::lock_guard<std::mutex> lock(run_queue_mutex);
377 run_queue.push_back(std::move(fn));
378
379 // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
380 // In that case, rely on the setup code to flush the queue without a notification being needed.
381 if (run_queue_notify_fd != -1) {
Josh Gao1222abc2018-03-19 15:19:45 -0700382 int rc = adb_write(run_queue_notify_fd.get(), "", 1);
383
384 // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
385 if (rc == 0) {
386 PLOG(FATAL) << "run queue notify fd was closed?";
387 } else if (rc == -1 && errno != EAGAIN) {
Josh Gao4c936392017-05-03 14:10:39 -0700388 PLOG(FATAL) << "failed to write to run queue notify fd";
389 }
390 }
391}
392
Josh Gao39c1f4b2018-10-05 17:09:07 -0700393static void fdevent_check_spin(uint64_t cycle) {
394 // Check to see if we're spinning because we forgot about an fdevent
395 // by keeping track of how long fdevents have been continuously pending.
396 struct SpinCheck {
397 fdevent* fde;
398 android::base::boot_clock::time_point timestamp;
399 uint64_t cycle;
400 };
401 static auto& g_continuously_pending = *new std::unordered_map<uint64_t, SpinCheck>();
402 static auto last_cycle = android::base::boot_clock::now();
403
404 auto now = android::base::boot_clock::now();
405 if (now - last_cycle > 10ms) {
406 // We're not spinning.
407 g_continuously_pending.clear();
408 last_cycle = now;
409 return;
410 }
411 last_cycle = now;
412
413 for (auto* fde : g_pending_list) {
414 auto it = g_continuously_pending.find(fde->id);
415 if (it == g_continuously_pending.end()) {
416 g_continuously_pending[fde->id] =
417 SpinCheck{.fde = fde, .timestamp = now, .cycle = cycle};
418 } else {
419 it->second.cycle = cycle;
420 }
421 }
422
423 for (auto it = g_continuously_pending.begin(); it != g_continuously_pending.end();) {
424 if (it->second.cycle != cycle) {
425 it = g_continuously_pending.erase(it);
426 } else {
427 // Use an absurdly long window, since all we really care about is
428 // getting a bugreport eventually.
429 if (now - it->second.timestamp > 300s) {
430 LOG(FATAL_WITHOUT_ABORT)
431 << "detected spin in fdevent: " << dump_fde(it->second.fde);
432#if defined(__linux__)
433 int fd = it->second.fde->fd.get();
434 std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
435 std::string path;
436 if (!android::base::Readlink(fd_path, &path)) {
437 PLOG(FATAL_WITHOUT_ABORT) << "readlink of fd " << fd << " failed";
438 }
439 LOG(FATAL_WITHOUT_ABORT) << "fd " << fd << " = " << path;
440#endif
441 abort();
442 }
443 ++it;
444 }
445 }
446}
447
Josh Gao4c936392017-05-03 14:10:39 -0700448void fdevent_loop() {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700449 set_main_thread();
Josh Gao4c936392017-05-03 14:10:39 -0700450 fdevent_run_setup();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200451
Josh Gao39c1f4b2018-10-05 17:09:07 -0700452 uint64_t cycle = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700453 while (true) {
Josh Gao022d4472016-02-10 14:49:00 -0800454 if (terminate_loop) {
455 return;
456 }
457
Yabin Cuia1080162015-09-04 16:19:56 -0700458 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200461
Josh Gao39c1f4b2018-10-05 17:09:07 -0700462 fdevent_check_spin(cycle++);
463
Yabin Cuia1080162015-09-04 16:19:56 -0700464 while (!g_pending_list.empty()) {
465 fdevent* fde = g_pending_list.front();
466 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700467 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 }
Josh Gaofa30bf32018-03-29 16:23:43 -0700469
470 if (run_needs_flush) {
471 fdevent_run_flush();
472 run_needs_flush = false;
473 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 }
475}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700476
Josh Gao022d4472016-02-10 14:49:00 -0800477void fdevent_terminate_loop() {
478 terminate_loop = true;
479}
480
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700481size_t fdevent_installed_count() {
482 return g_poll_node_map.size();
483}
484
485void fdevent_reset() {
486 g_poll_node_map.clear();
487 g_pending_list.clear();
Josh Gao4c936392017-05-03 14:10:39 -0700488
489 std::lock_guard<std::mutex> lock(run_queue_mutex);
490 run_queue_notify_fd.reset();
491 run_queue.clear();
492
Yabin Cuifbfa8402015-10-30 18:37:26 -0700493 main_thread_valid = false;
Josh Gao022d4472016-02-10 14:49:00 -0800494 terminate_loop = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700495}