blob: e09656033128be35ec77b37f497f211e405c02af [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>
36#include <vector>
37
Josh Gao39c1f4b2018-10-05 17:09:07 -070038#include <android-base/chrono_utils.h>
39#include <android-base/file.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/logging.h>
41#include <android-base/stringprintf.h>
Josh Gao4c936392017-05-03 14:10:39 -070042#include <android-base/thread_annotations.h>
Josh Gao5791e212018-03-16 14:25:42 -070043#include <android-base/threads.h>
Yabin Cuia1080162015-09-04 16:19:56 -070044
Dan Albertcc731cc2015-02-24 21:26:58 -080045#include "adb_io.h"
leozwangd3fc15f2014-07-29 12:50:02 -070046#include "adb_trace.h"
Josh Gao4c936392017-05-03 14:10:39 -070047#include "adb_unique_fd.h"
Yabin Cui6dfef252015-10-06 15:10:05 -070048#include "adb_utils.h"
Josh Gao39c1f4b2018-10-05 17:09:07 -070049#include "sysdeps/chrono.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#define FDE_EVENTMASK 0x00ff
52#define FDE_STATEMASK 0xff00
53
54#define FDE_ACTIVE 0x0100
55#define FDE_PENDING 0x0200
56#define FDE_CREATED 0x0400
57
Yabin Cuia1080162015-09-04 16:19:56 -070058struct PollNode {
59 fdevent* fde;
Josh Gao3777d2e2016-02-16 17:34:53 -080060 adb_pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070062 explicit PollNode(fdevent* fde) : fde(fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070063 memset(&pollfd, 0, sizeof(pollfd));
Josh Gao3b37fa22018-05-14 13:42:49 -070064 pollfd.fd = fde->fd.get();
Yabin Cuiaa77e222015-09-29 12:25:33 -070065
66#if defined(__linux__)
67 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
68 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
69 pollfd.events = POLLRDHUP;
70#endif
Yabin Cuia1080162015-09-04 16:19:56 -070071 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072};
73
Yabin Cuia1080162015-09-04 16:19:56 -070074// All operations to fdevent should happen only in the main thread.
75// That's why we don't need a lock for fdevent.
Josh Gaob7b1edf2015-11-11 17:56:12 -080076static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
77static auto& g_pending_list = *new std::list<fdevent*>();
Josh Gao022d4472016-02-10 14:49:00 -080078static std::atomic<bool> terminate_loop(false);
Yabin Cuifbfa8402015-10-30 18:37:26 -070079static bool main_thread_valid;
Josh Gao5791e212018-03-16 14:25:42 -070080static uint64_t main_thread_id;
Yabin Cuifbfa8402015-10-30 18:37:26 -070081
Josh Gaoded557f2018-06-18 14:55:27 -070082static uint64_t fdevent_id;
83
Josh Gaofa30bf32018-03-29 16:23:43 -070084static bool run_needs_flush = false;
Josh Gao4c936392017-05-03 14:10:39 -070085static auto& run_queue_notify_fd = *new unique_fd();
86static auto& run_queue_mutex = *new std::mutex();
Josh Gaoe39ccd32018-02-23 14:37:07 -080087static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
Josh Gao4c936392017-05-03 14:10:39 -070088
Yabin Cuib5e11412017-03-10 16:01:01 -080089void check_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070090 if (main_thread_valid) {
Josh Gao5791e212018-03-16 14:25:42 -070091 CHECK_EQ(main_thread_id, android::base::GetThreadId());
Yabin Cuifbfa8402015-10-30 18:37:26 -070092 }
93}
94
Yabin Cuib5e11412017-03-10 16:01:01 -080095void set_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070096 main_thread_valid = true;
Josh Gao5791e212018-03-16 14:25:42 -070097 main_thread_id = android::base::GetThreadId();
Yabin Cuifbfa8402015-10-30 18:37:26 -070098}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Yabin Cuia1080162015-09-04 16:19:56 -0700100static std::string dump_fde(const fdevent* fde) {
101 std::string state;
102 if (fde->state & FDE_ACTIVE) {
103 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 }
Yabin Cuia1080162015-09-04 16:19:56 -0700105 if (fde->state & FDE_PENDING) {
106 state += "P";
107 }
108 if (fde->state & FDE_CREATED) {
109 state += "C";
110 }
111 if (fde->state & FDE_READ) {
112 state += "R";
113 }
114 if (fde->state & FDE_WRITE) {
115 state += "W";
116 }
117 if (fde->state & FDE_ERROR) {
118 state += "E";
119 }
Josh Gaoded557f2018-06-18 14:55:27 -0700120 return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
121 state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122}
123
Yabin Cuia1080162015-09-04 16:19:56 -0700124void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700125 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700126 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 memset(fde, 0, sizeof(fdevent));
Josh Gao71f775a2018-05-14 11:14:33 -0700128}
129
130fdevent* fdevent_create(int fd, fd_func func, void* arg) {
131 check_main_thread();
132 CHECK_GE(fd, 0);
133
134 fdevent* fde = new fdevent();
Josh Gaoded557f2018-06-18 14:55:27 -0700135 fde->id = fdevent_id++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 fde->state = FDE_ACTIVE;
Josh Gao3b37fa22018-05-14 13:42:49 -0700137 fde->fd.reset(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 fde->func = func;
139 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700140 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700141 // Here is not proper to handle the error. If it fails here, some error is
142 // likely to be detected by poll(), then we can let the callback function
143 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700144 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700145 }
Josh Gao3b37fa22018-05-14 13:42:49 -0700146 auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
Yabin Cuia1080162015-09-04 16:19:56 -0700147 CHECK(pair.second) << "install existing fd " << fd;
Josh Gao71f775a2018-05-14 11:14:33 -0700148
149 fde->state |= FDE_CREATED;
150 return fde;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700153unique_fd fdevent_release(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700154 check_main_thread();
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700155 if (!fde) {
156 return {};
157 }
158
Josh Gao71f775a2018-05-14 11:14:33 -0700159 if (!(fde->state & FDE_CREATED)) {
160 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
161 }
162
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700163 unique_fd result = std::move(fde->fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700164 if (fde->state & FDE_ACTIVE) {
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700165 g_poll_node_map.erase(result.get());
166
Yabin Cuia1080162015-09-04 16:19:56 -0700167 if (fde->state & FDE_PENDING) {
168 g_pending_list.remove(fde);
169 }
Yabin Cuia1080162015-09-04 16:19:56 -0700170 fde->state = 0;
171 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 }
Josh Gao71f775a2018-05-14 11:14:33 -0700173
174 delete fde;
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700175 return result;
176}
177
178void fdevent_destroy(fdevent* fde) {
179 // Release, and then let unique_fd's destructor cleanup.
180 fdevent_release(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181}
182
Yabin Cuia1080162015-09-04 16:19:56 -0700183static void fdevent_update(fdevent* fde, unsigned events) {
Josh Gao3b37fa22018-05-14 13:42:49 -0700184 auto it = g_poll_node_map.find(fde->fd.get());
Yabin Cuia1080162015-09-04 16:19:56 -0700185 CHECK(it != g_poll_node_map.end());
186 PollNode& node = it->second;
187 if (events & FDE_READ) {
188 node.pollfd.events |= POLLIN;
189 } else {
190 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
192
Yabin Cuia1080162015-09-04 16:19:56 -0700193 if (events & FDE_WRITE) {
194 node.pollfd.events |= POLLOUT;
195 } else {
196 node.pollfd.events &= ~POLLOUT;
197 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700199}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Yabin Cuia1080162015-09-04 16:19:56 -0700201void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700202 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700203 events &= FDE_EVENTMASK;
204 if ((fde->state & FDE_EVENTMASK) == events) {
205 return;
206 }
Spencer Low888a7482015-09-29 18:33:38 -0700207 CHECK(fde->state & FDE_ACTIVE);
208 fdevent_update(fde, events);
209 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700210
Spencer Low888a7482015-09-29 18:33:38 -0700211 if (fde->state & FDE_PENDING) {
212 // If we are pending, make sure we don't signal an event that is no longer wanted.
213 fde->events &= events;
214 if (fde->events == 0) {
215 g_pending_list.remove(fde);
216 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 }
218 }
219}
220
Yabin Cuia1080162015-09-04 16:19:56 -0700221void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700222 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700223 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224}
225
Yabin Cuia1080162015-09-04 16:19:56 -0700226void fdevent_del(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
Josh Gao3777d2e2016-02-16 17:34:53 -0800231static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700232 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700233 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700234 std::string op;
235 if (pollfd.events & POLLIN) {
236 op += "R";
237 }
238 if (pollfd.events & POLLOUT) {
239 op += "W";
240 }
241 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
242 }
243 return result;
244}
245
246static void fdevent_process() {
Josh Gao3777d2e2016-02-16 17:34:53 -0800247 std::vector<adb_pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700248 for (const auto& pair : g_poll_node_map) {
249 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700250 }
251 CHECK_GT(pollfds.size(), 0u);
252 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao39c1f4b2018-10-05 17:09:07 -0700253
Josh Gao3777d2e2016-02-16 17:34:53 -0800254 int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
Yabin Cuia1080162015-09-04 16:19:56 -0700255 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700256 PLOG(ERROR) << "poll(), ret = " << ret;
257 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700258 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700259 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700260 if (pollfd.revents != 0) {
261 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
262 }
Yabin Cuia1080162015-09-04 16:19:56 -0700263 unsigned events = 0;
264 if (pollfd.revents & POLLIN) {
265 events |= FDE_READ;
266 }
267 if (pollfd.revents & POLLOUT) {
268 events |= FDE_WRITE;
269 }
270 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
271 // We fake a read, as the rest of the code assumes that errors will
272 // be detected at that point.
273 events |= FDE_READ | FDE_ERROR;
274 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700275#if defined(__linux__)
276 if (pollfd.revents & POLLRDHUP) {
277 events |= FDE_READ | FDE_ERROR;
278 }
279#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700280 if (events != 0) {
281 auto it = g_poll_node_map.find(pollfd.fd);
282 CHECK(it != g_poll_node_map.end());
283 fdevent* fde = it->second.fde;
Josh Gao3b37fa22018-05-14 13:42:49 -0700284 CHECK_EQ(fde->fd.get(), pollfd.fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700285 fde->events |= events;
286 D("%s got events %x", dump_fde(fde).c_str(), events);
287 fde->state |= FDE_PENDING;
288 g_pending_list.push_back(fde);
289 }
290 }
291}
292
Josh Gao4c936392017-05-03 14:10:39 -0700293static void fdevent_call_fdfunc(fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -0700294 unsigned events = fde->events;
295 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700296 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700297 fde->state &= (~FDE_PENDING);
298 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
Josh Gao3b37fa22018-05-14 13:42:49 -0700299 fde->func(fde->fd.get(), events, fde->arg);
Yabin Cuia1080162015-09-04 16:19:56 -0700300}
301
Josh Gaoe39ccd32018-02-23 14:37:07 -0800302static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
303 // We need to be careful around reentrancy here, since a function we call can queue up another
304 // function.
305 while (true) {
306 std::function<void()> fn;
307 {
308 std::lock_guard<std::mutex> lock(run_queue_mutex);
309 if (run_queue.empty()) {
310 break;
311 }
312 fn = run_queue.front();
313 run_queue.pop_front();
314 }
315 fn();
Josh Gao4c936392017-05-03 14:10:39 -0700316 }
Josh Gao4c936392017-05-03 14:10:39 -0700317}
318
319static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
320 CHECK_GE(fd, 0);
321 CHECK(ev & FDE_READ);
322
323 char buf[1024];
324
325 // Empty the fd.
326 if (adb_read(fd, buf, sizeof(buf)) == -1) {
327 PLOG(FATAL) << "failed to empty run queue notify fd";
328 }
329
Josh Gaofa30bf32018-03-29 16:23:43 -0700330 // Mark that we need to flush, and then run it at the end of fdevent_loop.
331 run_needs_flush = true;
Josh Gao4c936392017-05-03 14:10:39 -0700332}
333
334static void fdevent_run_setup() {
Josh Gaoe39ccd32018-02-23 14:37:07 -0800335 {
336 std::lock_guard<std::mutex> lock(run_queue_mutex);
337 CHECK(run_queue_notify_fd.get() == -1);
338 int s[2];
339 if (adb_socketpair(s) != 0) {
340 PLOG(FATAL) << "failed to create run queue notify socketpair";
341 }
Josh Gao4c936392017-05-03 14:10:39 -0700342
Josh Gao1222abc2018-03-19 15:19:45 -0700343 if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
344 PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
345 }
346
Josh Gaoe39ccd32018-02-23 14:37:07 -0800347 run_queue_notify_fd.reset(s[0]);
348 fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
349 CHECK(fde != nullptr);
350 fdevent_add(fde, FDE_READ);
351 }
Josh Gao4c936392017-05-03 14:10:39 -0700352
353 fdevent_run_flush();
354}
355
356void fdevent_run_on_main_thread(std::function<void()> fn) {
357 std::lock_guard<std::mutex> lock(run_queue_mutex);
358 run_queue.push_back(std::move(fn));
359
360 // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
361 // In that case, rely on the setup code to flush the queue without a notification being needed.
362 if (run_queue_notify_fd != -1) {
Josh Gao1222abc2018-03-19 15:19:45 -0700363 int rc = adb_write(run_queue_notify_fd.get(), "", 1);
364
365 // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
366 if (rc == 0) {
367 PLOG(FATAL) << "run queue notify fd was closed?";
368 } else if (rc == -1 && errno != EAGAIN) {
Josh Gao4c936392017-05-03 14:10:39 -0700369 PLOG(FATAL) << "failed to write to run queue notify fd";
370 }
371 }
372}
373
Josh Gao39c1f4b2018-10-05 17:09:07 -0700374static void fdevent_check_spin(uint64_t cycle) {
375 // Check to see if we're spinning because we forgot about an fdevent
376 // by keeping track of how long fdevents have been continuously pending.
377 struct SpinCheck {
378 fdevent* fde;
379 android::base::boot_clock::time_point timestamp;
380 uint64_t cycle;
381 };
382 static auto& g_continuously_pending = *new std::unordered_map<uint64_t, SpinCheck>();
383 static auto last_cycle = android::base::boot_clock::now();
384
385 auto now = android::base::boot_clock::now();
386 if (now - last_cycle > 10ms) {
387 // We're not spinning.
388 g_continuously_pending.clear();
389 last_cycle = now;
390 return;
391 }
392 last_cycle = now;
393
394 for (auto* fde : g_pending_list) {
395 auto it = g_continuously_pending.find(fde->id);
396 if (it == g_continuously_pending.end()) {
397 g_continuously_pending[fde->id] =
398 SpinCheck{.fde = fde, .timestamp = now, .cycle = cycle};
399 } else {
400 it->second.cycle = cycle;
401 }
402 }
403
404 for (auto it = g_continuously_pending.begin(); it != g_continuously_pending.end();) {
405 if (it->second.cycle != cycle) {
406 it = g_continuously_pending.erase(it);
407 } else {
408 // Use an absurdly long window, since all we really care about is
409 // getting a bugreport eventually.
410 if (now - it->second.timestamp > 300s) {
411 LOG(FATAL_WITHOUT_ABORT)
412 << "detected spin in fdevent: " << dump_fde(it->second.fde);
413#if defined(__linux__)
414 int fd = it->second.fde->fd.get();
415 std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
416 std::string path;
417 if (!android::base::Readlink(fd_path, &path)) {
418 PLOG(FATAL_WITHOUT_ABORT) << "readlink of fd " << fd << " failed";
419 }
420 LOG(FATAL_WITHOUT_ABORT) << "fd " << fd << " = " << path;
421#endif
422 abort();
423 }
424 ++it;
425 }
426 }
427}
428
Josh Gao4c936392017-05-03 14:10:39 -0700429void fdevent_loop() {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700430 set_main_thread();
Josh Gao4c936392017-05-03 14:10:39 -0700431 fdevent_run_setup();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200432
Josh Gao39c1f4b2018-10-05 17:09:07 -0700433 uint64_t cycle = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700434 while (true) {
Josh Gao022d4472016-02-10 14:49:00 -0800435 if (terminate_loop) {
436 return;
437 }
438
Yabin Cuia1080162015-09-04 16:19:56 -0700439 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200442
Josh Gao39c1f4b2018-10-05 17:09:07 -0700443 fdevent_check_spin(cycle++);
444
Yabin Cuia1080162015-09-04 16:19:56 -0700445 while (!g_pending_list.empty()) {
446 fdevent* fde = g_pending_list.front();
447 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700448 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 }
Josh Gaofa30bf32018-03-29 16:23:43 -0700450
451 if (run_needs_flush) {
452 fdevent_run_flush();
453 run_needs_flush = false;
454 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 }
456}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700457
Josh Gao022d4472016-02-10 14:49:00 -0800458void fdevent_terminate_loop() {
459 terminate_loop = true;
460}
461
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700462size_t fdevent_installed_count() {
463 return g_poll_node_map.size();
464}
465
466void fdevent_reset() {
467 g_poll_node_map.clear();
468 g_pending_list.clear();
Josh Gao4c936392017-05-03 14:10:39 -0700469
470 std::lock_guard<std::mutex> lock(run_queue_mutex);
471 run_queue_notify_fd.reset();
472 run_queue.clear();
473
Yabin Cuifbfa8402015-10-30 18:37:26 -0700474 main_thread_valid = false;
Josh Gao022d4472016-02-10 14:49:00 -0800475 terminate_loop = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700476}