blob: d2855619be3f28174c8ac1b5428d6512d169ce27 [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>
Dan Albert33134262015-03-19 15:21:08 -070024#include <stdlib.h>
25#include <string.h>
Dan Albert33134262015-03-19 15:21:08 -070026#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Josh Gao022d4472016-02-10 14:49:00 -080028#include <atomic>
Josh Gaoe39ccd32018-02-23 14:37:07 -080029#include <deque>
Josh Gao4c936392017-05-03 14:10:39 -070030#include <functional>
Yabin Cuia1080162015-09-04 16:19:56 -070031#include <list>
Josh Gao4c936392017-05-03 14:10:39 -070032#include <mutex>
Yabin Cuia1080162015-09-04 16:19:56 -070033#include <unordered_map>
34#include <vector>
35
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/logging.h>
37#include <android-base/stringprintf.h>
Josh Gao4c936392017-05-03 14:10:39 -070038#include <android-base/thread_annotations.h>
Yabin Cuia1080162015-09-04 16:19:56 -070039
Dan Albertcc731cc2015-02-24 21:26:58 -080040#include "adb_io.h"
leozwangd3fc15f2014-07-29 12:50:02 -070041#include "adb_trace.h"
Josh Gao4c936392017-05-03 14:10:39 -070042#include "adb_unique_fd.h"
Yabin Cui6dfef252015-10-06 15:10:05 -070043#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Yabin Cuia1080162015-09-04 16:19:56 -070045#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -070046// This socket is used when a subproc shell service exists.
47// It wakes up the fdevent_loop() and cause the correct handling
48// of the shell's pseudo-tty master. I.e. force close it.
49int SHELL_EXIT_NOTIFY_FD = -1;
Alex Vallée947cb3e2015-07-17 15:30:59 -040050#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#define FDE_EVENTMASK 0x00ff
53#define FDE_STATEMASK 0xff00
54
55#define FDE_ACTIVE 0x0100
56#define FDE_PENDING 0x0200
57#define FDE_CREATED 0x0400
58
Yabin Cuia1080162015-09-04 16:19:56 -070059struct PollNode {
60 fdevent* fde;
Josh Gao3777d2e2016-02-16 17:34:53 -080061 adb_pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070063 explicit PollNode(fdevent* fde) : fde(fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070064 memset(&pollfd, 0, sizeof(pollfd));
65 pollfd.fd = fde->fd;
Yabin Cuiaa77e222015-09-29 12:25:33 -070066
67#if defined(__linux__)
68 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
69 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
70 pollfd.events = POLLRDHUP;
71#endif
Yabin Cuia1080162015-09-04 16:19:56 -070072 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073};
74
Yabin Cuia1080162015-09-04 16:19:56 -070075// All operations to fdevent should happen only in the main thread.
76// That's why we don't need a lock for fdevent.
Josh Gaob7b1edf2015-11-11 17:56:12 -080077static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
78static auto& g_pending_list = *new std::list<fdevent*>();
Josh Gao022d4472016-02-10 14:49:00 -080079static std::atomic<bool> terminate_loop(false);
Yabin Cuifbfa8402015-10-30 18:37:26 -070080static bool main_thread_valid;
Josh Gao3777d2e2016-02-16 17:34:53 -080081static unsigned long main_thread_id;
Yabin Cuifbfa8402015-10-30 18:37:26 -070082
Josh Gao4c936392017-05-03 14:10:39 -070083static auto& run_queue_notify_fd = *new unique_fd();
84static auto& run_queue_mutex = *new std::mutex();
Josh Gaoe39ccd32018-02-23 14:37:07 -080085static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
Josh Gao4c936392017-05-03 14:10:39 -070086
Yabin Cuib5e11412017-03-10 16:01:01 -080087void check_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070088 if (main_thread_valid) {
Josh Gao3777d2e2016-02-16 17:34:53 -080089 CHECK_EQ(main_thread_id, adb_thread_id());
Yabin Cuifbfa8402015-10-30 18:37:26 -070090 }
91}
92
Yabin Cuib5e11412017-03-10 16:01:01 -080093void set_main_thread() {
Yabin Cuifbfa8402015-10-30 18:37:26 -070094 main_thread_valid = true;
Josh Gao3777d2e2016-02-16 17:34:53 -080095 main_thread_id = adb_thread_id();
Yabin Cuifbfa8402015-10-30 18:37:26 -070096}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Yabin Cuia1080162015-09-04 16:19:56 -070098static std::string dump_fde(const fdevent* fde) {
99 std::string state;
100 if (fde->state & FDE_ACTIVE) {
101 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 }
Yabin Cuia1080162015-09-04 16:19:56 -0700103 if (fde->state & FDE_PENDING) {
104 state += "P";
105 }
106 if (fde->state & FDE_CREATED) {
107 state += "C";
108 }
109 if (fde->state & FDE_READ) {
110 state += "R";
111 }
112 if (fde->state & FDE_WRITE) {
113 state += "W";
114 }
115 if (fde->state & FDE_ERROR) {
116 state += "E";
117 }
118 if (fde->state & FDE_DONT_CLOSE) {
119 state += "D";
120 }
121 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122}
123
Josh Gao4c936392017-05-03 14:10:39 -0700124fdevent* fdevent_create(int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700125 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
127 if(fde == 0) return 0;
128 fdevent_install(fde, fd, func, arg);
129 fde->state |= FDE_CREATED;
130 return fde;
131}
132
Josh Gao4c936392017-05-03 14:10:39 -0700133void fdevent_destroy(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700134 check_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 if(fde == 0) return;
136 if(!(fde->state & FDE_CREATED)) {
Yabin Cuia1080162015-09-04 16:19:56 -0700137 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
139 fdevent_remove(fde);
SungHyun Kwonabb80e02015-03-03 13:56:42 +0900140 free(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141}
142
Yabin Cuia1080162015-09-04 16:19:56 -0700143void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700144 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700145 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 memset(fde, 0, sizeof(fdevent));
147 fde->state = FDE_ACTIVE;
148 fde->fd = fd;
149 fde->func = func;
150 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700151 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700152 // Here is not proper to handle the error. If it fails here, some error is
153 // likely to be detected by poll(), then we can let the callback function
154 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700155 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700156 }
157 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
158 CHECK(pair.second) << "install existing fd " << fd;
159 D("fdevent_install %s", dump_fde(fde).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160}
161
Yabin Cuia1080162015-09-04 16:19:56 -0700162void fdevent_remove(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700163 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700164 D("fdevent_remove %s", dump_fde(fde).c_str());
165 if (fde->state & FDE_ACTIVE) {
166 g_poll_node_map.erase(fde->fd);
167 if (fde->state & FDE_PENDING) {
168 g_pending_list.remove(fde);
169 }
170 if (!(fde->state & FDE_DONT_CLOSE)) {
171 adb_close(fde->fd);
172 fde->fd = -1;
173 }
174 fde->state = 0;
175 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177}
178
Yabin Cuia1080162015-09-04 16:19:56 -0700179static void fdevent_update(fdevent* fde, unsigned events) {
180 auto it = g_poll_node_map.find(fde->fd);
181 CHECK(it != g_poll_node_map.end());
182 PollNode& node = it->second;
183 if (events & FDE_READ) {
184 node.pollfd.events |= POLLIN;
185 } else {
186 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 }
188
Yabin Cuia1080162015-09-04 16:19:56 -0700189 if (events & FDE_WRITE) {
190 node.pollfd.events |= POLLOUT;
191 } else {
192 node.pollfd.events &= ~POLLOUT;
193 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700195}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Yabin Cuia1080162015-09-04 16:19:56 -0700197void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700198 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700199 events &= FDE_EVENTMASK;
200 if ((fde->state & FDE_EVENTMASK) == events) {
201 return;
202 }
Spencer Low888a7482015-09-29 18:33:38 -0700203 CHECK(fde->state & FDE_ACTIVE);
204 fdevent_update(fde, events);
205 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700206
Spencer Low888a7482015-09-29 18:33:38 -0700207 if (fde->state & FDE_PENDING) {
208 // If we are pending, make sure we don't signal an event that is no longer wanted.
209 fde->events &= events;
210 if (fde->events == 0) {
211 g_pending_list.remove(fde);
212 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 }
214 }
215}
216
Yabin Cuia1080162015-09-04 16:19:56 -0700217void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700218 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700219 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220}
221
Yabin Cuia1080162015-09-04 16:19:56 -0700222void fdevent_del(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700223 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700224 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225}
226
Josh Gao3777d2e2016-02-16 17:34:53 -0800227static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700228 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700229 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700230 std::string op;
231 if (pollfd.events & POLLIN) {
232 op += "R";
233 }
234 if (pollfd.events & POLLOUT) {
235 op += "W";
236 }
237 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
238 }
239 return result;
240}
241
242static void fdevent_process() {
Josh Gao3777d2e2016-02-16 17:34:53 -0800243 std::vector<adb_pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700244 for (const auto& pair : g_poll_node_map) {
245 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700246 }
247 CHECK_GT(pollfds.size(), 0u);
248 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao3777d2e2016-02-16 17:34:53 -0800249 int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
Yabin Cuia1080162015-09-04 16:19:56 -0700250 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700251 PLOG(ERROR) << "poll(), ret = " << ret;
252 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700253 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700254 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700255 if (pollfd.revents != 0) {
256 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
257 }
Yabin Cuia1080162015-09-04 16:19:56 -0700258 unsigned events = 0;
259 if (pollfd.revents & POLLIN) {
260 events |= FDE_READ;
261 }
262 if (pollfd.revents & POLLOUT) {
263 events |= FDE_WRITE;
264 }
265 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
266 // We fake a read, as the rest of the code assumes that errors will
267 // be detected at that point.
268 events |= FDE_READ | FDE_ERROR;
269 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700270#if defined(__linux__)
271 if (pollfd.revents & POLLRDHUP) {
272 events |= FDE_READ | FDE_ERROR;
273 }
274#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700275 if (events != 0) {
276 auto it = g_poll_node_map.find(pollfd.fd);
277 CHECK(it != g_poll_node_map.end());
278 fdevent* fde = it->second.fde;
279 CHECK_EQ(fde->fd, pollfd.fd);
280 fde->events |= events;
281 D("%s got events %x", dump_fde(fde).c_str(), events);
282 fde->state |= FDE_PENDING;
283 g_pending_list.push_back(fde);
284 }
285 }
286}
287
Josh Gao4c936392017-05-03 14:10:39 -0700288static void fdevent_call_fdfunc(fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -0700289 unsigned events = fde->events;
290 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700291 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700292 fde->state &= (~FDE_PENDING);
293 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
294 fde->func(fde->fd, events, fde->arg);
295}
296
297#if !ADB_HOST
Josh Gao3777d2e2016-02-16 17:34:53 -0800298
299#include <sys/ioctl.h>
300
Josh Gao4c936392017-05-03 14:10:39 -0700301static void fdevent_subproc_event_func(int fd, unsigned ev, void* /* userdata */) {
Yabin Cuia1080162015-09-04 16:19:56 -0700302 D("subproc handling on fd = %d, ev = %x", fd, ev);
303
304 CHECK_GE(fd, 0);
305
306 if (ev & FDE_READ) {
307 int subproc_fd;
308
309 if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
310 LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
311 }
312 auto it = g_poll_node_map.find(subproc_fd);
313 if (it == g_poll_node_map.end()) {
314 D("subproc_fd %d cleared from fd_table", subproc_fd);
Josh Gaoc65fae92016-01-19 16:21:17 -0800315 adb_close(subproc_fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700316 return;
317 }
318 fdevent* subproc_fde = it->second.fde;
319 if(subproc_fde->fd != subproc_fd) {
320 // Already reallocated?
Josh Gaoc65fae92016-01-19 16:21:17 -0800321 LOG(FATAL) << "subproc_fd(" << subproc_fd << ") != subproc_fde->fd(" << subproc_fde->fd
322 << ")";
Yabin Cuia1080162015-09-04 16:19:56 -0700323 return;
324 }
325
326 subproc_fde->force_eof = 1;
327
328 int rcount = 0;
329 ioctl(subproc_fd, FIONREAD, &rcount);
330 D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
331 if (rcount != 0) {
332 // If there is data left, it will show up in the select().
333 // This works because there is no other thread reading that
334 // data when in this fd_func().
335 return;
336 }
337
338 D("subproc_fde %s", dump_fde(subproc_fde).c_str());
339 subproc_fde->events |= FDE_READ;
340 if(subproc_fde->state & FDE_PENDING) {
341 return;
342 }
343 subproc_fde->state |= FDE_PENDING;
344 fdevent_call_fdfunc(subproc_fde);
345 }
346}
347
Josh Gao4c936392017-05-03 14:10:39 -0700348static void fdevent_subproc_setup() {
Yabin Cuia1080162015-09-04 16:19:56 -0700349 int s[2];
350
351 if(adb_socketpair(s)) {
352 PLOG(FATAL) << "cannot create shell-exit socket-pair";
353 }
354 D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
355
356 SHELL_EXIT_NOTIFY_FD = s[0];
357 fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
358 CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
359 fdevent_add(fde, FDE_READ);
360}
361#endif // !ADB_HOST
362
Josh Gaoe39ccd32018-02-23 14:37:07 -0800363static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
364 // We need to be careful around reentrancy here, since a function we call can queue up another
365 // function.
366 while (true) {
367 std::function<void()> fn;
368 {
369 std::lock_guard<std::mutex> lock(run_queue_mutex);
370 if (run_queue.empty()) {
371 break;
372 }
373 fn = run_queue.front();
374 run_queue.pop_front();
375 }
376 fn();
Josh Gao4c936392017-05-03 14:10:39 -0700377 }
Josh Gao4c936392017-05-03 14:10:39 -0700378}
379
380static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
381 CHECK_GE(fd, 0);
382 CHECK(ev & FDE_READ);
383
384 char buf[1024];
385
386 // Empty the fd.
387 if (adb_read(fd, buf, sizeof(buf)) == -1) {
388 PLOG(FATAL) << "failed to empty run queue notify fd";
389 }
390
Josh Gao4c936392017-05-03 14:10:39 -0700391 fdevent_run_flush();
392}
393
394static void fdevent_run_setup() {
Josh Gaoe39ccd32018-02-23 14:37:07 -0800395 {
396 std::lock_guard<std::mutex> lock(run_queue_mutex);
397 CHECK(run_queue_notify_fd.get() == -1);
398 int s[2];
399 if (adb_socketpair(s) != 0) {
400 PLOG(FATAL) << "failed to create run queue notify socketpair";
401 }
Josh Gao4c936392017-05-03 14:10:39 -0700402
Josh Gaoe39ccd32018-02-23 14:37:07 -0800403 run_queue_notify_fd.reset(s[0]);
404 fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
405 CHECK(fde != nullptr);
406 fdevent_add(fde, FDE_READ);
407 }
Josh Gao4c936392017-05-03 14:10:39 -0700408
409 fdevent_run_flush();
410}
411
412void fdevent_run_on_main_thread(std::function<void()> fn) {
413 std::lock_guard<std::mutex> lock(run_queue_mutex);
414 run_queue.push_back(std::move(fn));
415
416 // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
417 // In that case, rely on the setup code to flush the queue without a notification being needed.
418 if (run_queue_notify_fd != -1) {
419 if (adb_write(run_queue_notify_fd.get(), "", 1) != 1) {
420 PLOG(FATAL) << "failed to write to run queue notify fd";
421 }
422 }
423}
424
425void fdevent_loop() {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700426 set_main_thread();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400427#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700428 fdevent_subproc_setup();
Alex Vallée947cb3e2015-07-17 15:30:59 -0400429#endif // !ADB_HOST
Josh Gao4c936392017-05-03 14:10:39 -0700430 fdevent_run_setup();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200431
Elliott Hughesaa245492015-08-03 10:38:08 -0700432 while (true) {
Josh Gao022d4472016-02-10 14:49:00 -0800433 if (terminate_loop) {
434 return;
435 }
436
Yabin Cuia1080162015-09-04 16:19:56 -0700437 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200440
Yabin Cuia1080162015-09-04 16:19:56 -0700441 while (!g_pending_list.empty()) {
442 fdevent* fde = g_pending_list.front();
443 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700444 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 }
446 }
447}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700448
Josh Gao022d4472016-02-10 14:49:00 -0800449void fdevent_terminate_loop() {
450 terminate_loop = true;
451}
452
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700453size_t fdevent_installed_count() {
454 return g_poll_node_map.size();
455}
456
457void fdevent_reset() {
458 g_poll_node_map.clear();
459 g_pending_list.clear();
Josh Gao4c936392017-05-03 14:10:39 -0700460
461 std::lock_guard<std::mutex> lock(run_queue_mutex);
462 run_queue_notify_fd.reset();
463 run_queue.clear();
464
Yabin Cuifbfa8402015-10-30 18:37:26 -0700465 main_thread_valid = false;
Josh Gao022d4472016-02-10 14:49:00 -0800466 terminate_loop = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700467}