blob: f98c11a2f6550166c7c9b9baffc2bb79afd5aae9 [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 Gao3bbc8162018-06-18 16:37:01 -070038#include <android-base/file.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080039#include <android-base/logging.h>
40#include <android-base/stringprintf.h>
Josh Gao4c936392017-05-03 14:10:39 -070041#include <android-base/thread_annotations.h>
Josh Gao5791e212018-03-16 14:25:42 -070042#include <android-base/threads.h>
Yabin Cuia1080162015-09-04 16:19:56 -070043
Dan Albertcc731cc2015-02-24 21:26:58 -080044#include "adb_io.h"
leozwangd3fc15f2014-07-29 12:50:02 -070045#include "adb_trace.h"
Josh Gao4c936392017-05-03 14:10:39 -070046#include "adb_unique_fd.h"
Yabin Cui6dfef252015-10-06 15:10:05 -070047#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#define FDE_EVENTMASK 0x00ff
50#define FDE_STATEMASK 0xff00
51
52#define FDE_ACTIVE 0x0100
53#define FDE_PENDING 0x0200
54#define FDE_CREATED 0x0400
55
Yabin Cuia1080162015-09-04 16:19:56 -070056struct PollNode {
57 fdevent* fde;
Josh Gao3777d2e2016-02-16 17:34:53 -080058 adb_pollfd pollfd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070060 explicit PollNode(fdevent* fde) : fde(fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070061 memset(&pollfd, 0, sizeof(pollfd));
Josh Gao3b37fa22018-05-14 13:42:49 -070062 pollfd.fd = fde->fd.get();
Yabin Cuiaa77e222015-09-29 12:25:33 -070063
64#if defined(__linux__)
65 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
66 // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
67 pollfd.events = POLLRDHUP;
68#endif
Yabin Cuia1080162015-09-04 16:19:56 -070069 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070};
71
Yabin Cuia1080162015-09-04 16:19:56 -070072// All operations to fdevent should happen only in the main thread.
73// That's why we don't need a lock for fdevent.
Josh Gaob7b1edf2015-11-11 17:56:12 -080074static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
75static auto& g_pending_list = *new std::list<fdevent*>();
Josh Gao022d4472016-02-10 14:49:00 -080076static std::atomic<bool> terminate_loop(false);
Yabin Cuifbfa8402015-10-30 18:37:26 -070077static bool main_thread_valid;
Josh Gao5791e212018-03-16 14:25:42 -070078static uint64_t main_thread_id;
Yabin Cuifbfa8402015-10-30 18:37:26 -070079
Josh Gaoded557f2018-06-18 14:55:27 -070080static uint64_t fdevent_id;
81
Josh Gaofa30bf32018-03-29 16:23:43 -070082static bool run_needs_flush = false;
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 Gao5791e212018-03-16 14:25:42 -070089 CHECK_EQ(main_thread_id, android::base::GetThreadId());
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 Gao5791e212018-03-16 14:25:42 -070095 main_thread_id = android::base::GetThreadId();
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 }
Josh Gaoded557f2018-06-18 14:55:27 -0700118 return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
119 state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120}
121
Yabin Cuia1080162015-09-04 16:19:56 -0700122void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700123 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700124 CHECK_GE(fd, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 memset(fde, 0, sizeof(fdevent));
Josh Gao71f775a2018-05-14 11:14:33 -0700126}
127
128fdevent* fdevent_create(int fd, fd_func func, void* arg) {
129 check_main_thread();
130 CHECK_GE(fd, 0);
131
132 fdevent* fde = new fdevent();
Josh Gaoded557f2018-06-18 14:55:27 -0700133 fde->id = fdevent_id++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 fde->state = FDE_ACTIVE;
Josh Gao3b37fa22018-05-14 13:42:49 -0700135 fde->fd.reset(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 fde->func = func;
137 fde->arg = arg;
Yabin Cui6dfef252015-10-06 15:10:05 -0700138 if (!set_file_block_mode(fd, false)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700139 // Here is not proper to handle the error. If it fails here, some error is
140 // likely to be detected by poll(), then we can let the callback function
141 // to handle it.
Yabin Cui6dfef252015-10-06 15:10:05 -0700142 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
Yabin Cuia1080162015-09-04 16:19:56 -0700143 }
Josh Gao3b37fa22018-05-14 13:42:49 -0700144 auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
Yabin Cuia1080162015-09-04 16:19:56 -0700145 CHECK(pair.second) << "install existing fd " << fd;
Josh Gao71f775a2018-05-14 11:14:33 -0700146
147 fde->state |= FDE_CREATED;
148 return fde;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149}
150
Josh Gao71f775a2018-05-14 11:14:33 -0700151void fdevent_destroy(fdevent* fde) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700152 check_main_thread();
Josh Gao71f775a2018-05-14 11:14:33 -0700153 if (fde == 0) return;
154 if (!(fde->state & FDE_CREATED)) {
155 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
156 }
157
Yabin Cuia1080162015-09-04 16:19:56 -0700158 if (fde->state & FDE_ACTIVE) {
Josh Gao3b37fa22018-05-14 13:42:49 -0700159 g_poll_node_map.erase(fde->fd.get());
Yabin Cuia1080162015-09-04 16:19:56 -0700160 if (fde->state & FDE_PENDING) {
161 g_pending_list.remove(fde);
162 }
Josh Gao3b37fa22018-05-14 13:42:49 -0700163 fde->fd.reset();
Yabin Cuia1080162015-09-04 16:19:56 -0700164 fde->state = 0;
165 fde->events = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 }
Josh Gao71f775a2018-05-14 11:14:33 -0700167
168 delete fde;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169}
170
Yabin Cuia1080162015-09-04 16:19:56 -0700171static void fdevent_update(fdevent* fde, unsigned events) {
Josh Gao3b37fa22018-05-14 13:42:49 -0700172 auto it = g_poll_node_map.find(fde->fd.get());
Yabin Cuia1080162015-09-04 16:19:56 -0700173 CHECK(it != g_poll_node_map.end());
174 PollNode& node = it->second;
175 if (events & FDE_READ) {
176 node.pollfd.events |= POLLIN;
177 } else {
178 node.pollfd.events &= ~POLLIN;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 }
180
Yabin Cuia1080162015-09-04 16:19:56 -0700181 if (events & FDE_WRITE) {
182 node.pollfd.events |= POLLOUT;
183 } else {
184 node.pollfd.events &= ~POLLOUT;
185 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 fde->state = (fde->state & FDE_STATEMASK) | events;
Yabin Cuia1080162015-09-04 16:19:56 -0700187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Yabin Cuia1080162015-09-04 16:19:56 -0700189void fdevent_set(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700190 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700191 events &= FDE_EVENTMASK;
192 if ((fde->state & FDE_EVENTMASK) == events) {
193 return;
194 }
Spencer Low888a7482015-09-29 18:33:38 -0700195 CHECK(fde->state & FDE_ACTIVE);
196 fdevent_update(fde, events);
197 D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
Yabin Cuia1080162015-09-04 16:19:56 -0700198
Spencer Low888a7482015-09-29 18:33:38 -0700199 if (fde->state & FDE_PENDING) {
200 // If we are pending, make sure we don't signal an event that is no longer wanted.
201 fde->events &= events;
202 if (fde->events == 0) {
203 g_pending_list.remove(fde);
204 fde->state &= ~FDE_PENDING;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 }
206 }
207}
208
Yabin Cuia1080162015-09-04 16:19:56 -0700209void fdevent_add(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700210 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700211 fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212}
213
Yabin Cuia1080162015-09-04 16:19:56 -0700214void fdevent_del(fdevent* fde, unsigned events) {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700215 check_main_thread();
Yabin Cuia1080162015-09-04 16:19:56 -0700216 fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217}
218
Josh Gao3777d2e2016-02-16 17:34:53 -0800219static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700220 std::string result;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700221 for (const auto& pollfd : pollfds) {
Yabin Cuia1080162015-09-04 16:19:56 -0700222 std::string op;
223 if (pollfd.events & POLLIN) {
224 op += "R";
225 }
226 if (pollfd.events & POLLOUT) {
227 op += "W";
228 }
229 android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
230 }
231 return result;
232}
233
234static void fdevent_process() {
Josh Gao3777d2e2016-02-16 17:34:53 -0800235 std::vector<adb_pollfd> pollfds;
Elliott Hughes65fe2512015-10-07 15:59:35 -0700236 for (const auto& pair : g_poll_node_map) {
237 pollfds.push_back(pair.second.pollfd);
Yabin Cuia1080162015-09-04 16:19:56 -0700238 }
239 CHECK_GT(pollfds.size(), 0u);
240 D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
Josh Gao3777d2e2016-02-16 17:34:53 -0800241 int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
Yabin Cuia1080162015-09-04 16:19:56 -0700242 if (ret == -1) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700243 PLOG(ERROR) << "poll(), ret = " << ret;
244 return;
Yabin Cuia1080162015-09-04 16:19:56 -0700245 }
Elliott Hughes65fe2512015-10-07 15:59:35 -0700246 for (const auto& pollfd : pollfds) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700247 if (pollfd.revents != 0) {
248 D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
249 }
Yabin Cuia1080162015-09-04 16:19:56 -0700250 unsigned events = 0;
251 if (pollfd.revents & POLLIN) {
252 events |= FDE_READ;
253 }
254 if (pollfd.revents & POLLOUT) {
255 events |= FDE_WRITE;
256 }
257 if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
258 // We fake a read, as the rest of the code assumes that errors will
259 // be detected at that point.
260 events |= FDE_READ | FDE_ERROR;
261 }
Yabin Cuiaa77e222015-09-29 12:25:33 -0700262#if defined(__linux__)
263 if (pollfd.revents & POLLRDHUP) {
264 events |= FDE_READ | FDE_ERROR;
265 }
266#endif
Yabin Cuia1080162015-09-04 16:19:56 -0700267 if (events != 0) {
268 auto it = g_poll_node_map.find(pollfd.fd);
269 CHECK(it != g_poll_node_map.end());
270 fdevent* fde = it->second.fde;
Josh Gao3b37fa22018-05-14 13:42:49 -0700271 CHECK_EQ(fde->fd.get(), pollfd.fd);
Yabin Cuia1080162015-09-04 16:19:56 -0700272 fde->events |= events;
273 D("%s got events %x", dump_fde(fde).c_str(), events);
274 fde->state |= FDE_PENDING;
275 g_pending_list.push_back(fde);
276 }
277 }
278}
279
Josh Gao4c936392017-05-03 14:10:39 -0700280static void fdevent_call_fdfunc(fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -0700281 unsigned events = fde->events;
282 fde->events = 0;
Spencer Low888a7482015-09-29 18:33:38 -0700283 CHECK(fde->state & FDE_PENDING);
Yabin Cuia1080162015-09-04 16:19:56 -0700284 fde->state &= (~FDE_PENDING);
285 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
Josh Gao3b37fa22018-05-14 13:42:49 -0700286 fde->func(fde->fd.get(), events, fde->arg);
Yabin Cuia1080162015-09-04 16:19:56 -0700287}
288
Josh Gaoe39ccd32018-02-23 14:37:07 -0800289static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
290 // We need to be careful around reentrancy here, since a function we call can queue up another
291 // function.
292 while (true) {
293 std::function<void()> fn;
294 {
295 std::lock_guard<std::mutex> lock(run_queue_mutex);
296 if (run_queue.empty()) {
297 break;
298 }
299 fn = run_queue.front();
300 run_queue.pop_front();
301 }
302 fn();
Josh Gao4c936392017-05-03 14:10:39 -0700303 }
Josh Gao4c936392017-05-03 14:10:39 -0700304}
305
306static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
307 CHECK_GE(fd, 0);
308 CHECK(ev & FDE_READ);
309
310 char buf[1024];
311
312 // Empty the fd.
313 if (adb_read(fd, buf, sizeof(buf)) == -1) {
314 PLOG(FATAL) << "failed to empty run queue notify fd";
315 }
316
Josh Gaofa30bf32018-03-29 16:23:43 -0700317 // Mark that we need to flush, and then run it at the end of fdevent_loop.
318 run_needs_flush = true;
Josh Gao4c936392017-05-03 14:10:39 -0700319}
320
321static void fdevent_run_setup() {
Josh Gaoe39ccd32018-02-23 14:37:07 -0800322 {
323 std::lock_guard<std::mutex> lock(run_queue_mutex);
324 CHECK(run_queue_notify_fd.get() == -1);
325 int s[2];
326 if (adb_socketpair(s) != 0) {
327 PLOG(FATAL) << "failed to create run queue notify socketpair";
328 }
Josh Gao4c936392017-05-03 14:10:39 -0700329
Josh Gao1222abc2018-03-19 15:19:45 -0700330 if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
331 PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
332 }
333
Josh Gaoe39ccd32018-02-23 14:37:07 -0800334 run_queue_notify_fd.reset(s[0]);
335 fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
336 CHECK(fde != nullptr);
337 fdevent_add(fde, FDE_READ);
338 }
Josh Gao4c936392017-05-03 14:10:39 -0700339
340 fdevent_run_flush();
341}
342
343void fdevent_run_on_main_thread(std::function<void()> fn) {
344 std::lock_guard<std::mutex> lock(run_queue_mutex);
345 run_queue.push_back(std::move(fn));
346
347 // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
348 // In that case, rely on the setup code to flush the queue without a notification being needed.
349 if (run_queue_notify_fd != -1) {
Josh Gao1222abc2018-03-19 15:19:45 -0700350 int rc = adb_write(run_queue_notify_fd.get(), "", 1);
351
352 // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
353 if (rc == 0) {
354 PLOG(FATAL) << "run queue notify fd was closed?";
355 } else if (rc == -1 && errno != EAGAIN) {
Josh Gao4c936392017-05-03 14:10:39 -0700356 PLOG(FATAL) << "failed to write to run queue notify fd";
357 }
358 }
359}
360
Josh Gao3bbc8162018-06-18 16:37:01 -0700361static void fdevent_check_spin(uint64_t cycle) {
362 // Check to see if we're spinning because we forgot about an fdevent
363 // by keeping track of how long fdevents have been continuously pending.
364 struct SpinCheck {
365 fdevent* fde;
366 std::chrono::steady_clock::time_point timestamp;
367 uint64_t cycle;
368 };
369 static auto& g_continuously_pending = *new std::unordered_map<uint64_t, SpinCheck>();
370
371 auto now = std::chrono::steady_clock::now();
372 for (auto* fde : g_pending_list) {
373 auto it = g_continuously_pending.find(fde->id);
374 if (it == g_continuously_pending.end()) {
375 g_continuously_pending[fde->id] =
376 SpinCheck{.fde = fde, .timestamp = now, .cycle = cycle};
377 } else {
378 it->second.cycle = cycle;
379 }
380 }
381
382 for (auto it = g_continuously_pending.begin(); it != g_continuously_pending.end();) {
383 if (it->second.cycle != cycle) {
384 it = g_continuously_pending.erase(it);
385 } else {
386 // Use an absurdly long window, since all we really care about is
387 // getting a bugreport eventually.
388 if (now - it->second.timestamp > std::chrono::minutes(5)) {
389 LOG(FATAL_WITHOUT_ABORT) << "detected spin in fdevent: " << dump_fde(it->second.fde);
390#if defined(__linux__)
391 int fd = it->second.fde->fd.get();
392 std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
393 std::string path;
394 if (!android::base::Readlink(fd_path, &path)) {
395 PLOG(FATAL_WITHOUT_ABORT) << "readlink of fd " << fd << " failed";
396 }
397 LOG(FATAL_WITHOUT_ABORT) << "fd " << fd << " = " << path;
398#endif
399 abort();
400 }
401 ++it;
402 }
403 }
404}
405
Josh Gao4c936392017-05-03 14:10:39 -0700406void fdevent_loop() {
Yabin Cuifbfa8402015-10-30 18:37:26 -0700407 set_main_thread();
Josh Gao4c936392017-05-03 14:10:39 -0700408 fdevent_run_setup();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200409
Josh Gao3bbc8162018-06-18 16:37:01 -0700410 uint64_t cycle = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700411 while (true) {
Josh Gao022d4472016-02-10 14:49:00 -0800412 if (terminate_loop) {
413 return;
414 }
415
Yabin Cuia1080162015-09-04 16:19:56 -0700416 D("--- --- waiting for events");
JP Abgrall408fa572011-03-16 15:57:42 -0700417
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 fdevent_process();
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200419
Josh Gao3bbc8162018-06-18 16:37:01 -0700420 fdevent_check_spin(cycle++);
421
Yabin Cuia1080162015-09-04 16:19:56 -0700422 while (!g_pending_list.empty()) {
423 fdevent* fde = g_pending_list.front();
424 g_pending_list.pop_front();
JP Abgrall408fa572011-03-16 15:57:42 -0700425 fdevent_call_fdfunc(fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 }
Josh Gaofa30bf32018-03-29 16:23:43 -0700427
428 if (run_needs_flush) {
429 fdevent_run_flush();
430 run_needs_flush = false;
431 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 }
433}
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700434
Josh Gao022d4472016-02-10 14:49:00 -0800435void fdevent_terminate_loop() {
436 terminate_loop = true;
437}
438
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700439size_t fdevent_installed_count() {
440 return g_poll_node_map.size();
441}
442
443void fdevent_reset() {
444 g_poll_node_map.clear();
445 g_pending_list.clear();
Josh Gao4c936392017-05-03 14:10:39 -0700446
447 std::lock_guard<std::mutex> lock(run_queue_mutex);
448 run_queue_notify_fd.reset();
449 run_queue.clear();
450
Yabin Cuifbfa8402015-10-30 18:37:26 -0700451 main_thread_valid = false;
Josh Gao022d4472016-02-10 14:49:00 -0800452 terminate_loop = false;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700453}