The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c |
| 2 | ** |
| 3 | ** Copyright 2006, Brian Swetland <swetland@frotz.net> |
| 4 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 5 | ** 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 11 | ** 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 18 | #define TRACE_TAG FDEVENT |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 19 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | #include "sysdeps.h" |
| 21 | #include "fdevent.h" |
| 22 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <fcntl.h> |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 29 | #include <atomic> |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 30 | #include <deque> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 31 | #include <functional> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 32 | #include <list> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 33 | #include <mutex> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
| 35 | #include <vector> |
| 36 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/stringprintf.h> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 39 | #include <android-base/thread_annotations.h> |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 40 | #include <android-base/threads.h> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 41 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 42 | #include "adb_io.h" |
leozwang | d3fc15f | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 43 | #include "adb_trace.h" |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 44 | #include "adb_unique_fd.h" |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 45 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #define FDE_EVENTMASK 0x00ff |
| 48 | #define FDE_STATEMASK 0xff00 |
| 49 | |
| 50 | #define FDE_ACTIVE 0x0100 |
| 51 | #define FDE_PENDING 0x0200 |
| 52 | #define FDE_CREATED 0x0400 |
| 53 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 54 | struct PollNode { |
| 55 | fdevent* fde; |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 56 | adb_pollfd pollfd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 58 | explicit PollNode(fdevent* fde) : fde(fde) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 59 | memset(&pollfd, 0, sizeof(pollfd)); |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 60 | pollfd.fd = fde->fd.get(); |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 61 | |
| 62 | #if defined(__linux__) |
| 63 | // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. |
| 64 | // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034. |
| 65 | pollfd.events = POLLRDHUP; |
| 66 | #endif |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 67 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 70 | // All operations to fdevent should happen only in the main thread. |
| 71 | // That's why we don't need a lock for fdevent. |
Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 72 | static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>(); |
| 73 | static auto& g_pending_list = *new std::list<fdevent*>(); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 74 | static std::atomic<bool> terminate_loop(false); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 75 | static bool main_thread_valid; |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 76 | static uint64_t main_thread_id; |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 77 | |
Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 78 | static bool run_needs_flush = false; |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 79 | static auto& run_queue_notify_fd = *new unique_fd(); |
| 80 | static auto& run_queue_mutex = *new std::mutex(); |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 81 | static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 82 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 83 | void check_main_thread() { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 84 | if (main_thread_valid) { |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 85 | CHECK_EQ(main_thread_id, android::base::GetThreadId()); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 89 | void set_main_thread() { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 90 | main_thread_valid = true; |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 91 | main_thread_id = android::base::GetThreadId(); |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 92 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 94 | static std::string dump_fde(const fdevent* fde) { |
| 95 | std::string state; |
| 96 | if (fde->state & FDE_ACTIVE) { |
| 97 | state += "A"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 99 | if (fde->state & FDE_PENDING) { |
| 100 | state += "P"; |
| 101 | } |
| 102 | if (fde->state & FDE_CREATED) { |
| 103 | state += "C"; |
| 104 | } |
| 105 | if (fde->state & FDE_READ) { |
| 106 | state += "R"; |
| 107 | } |
| 108 | if (fde->state & FDE_WRITE) { |
| 109 | state += "W"; |
| 110 | } |
| 111 | if (fde->state & FDE_ERROR) { |
| 112 | state += "E"; |
| 113 | } |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 114 | return android::base::StringPrintf("(fdevent %d %s)", fde->fd.get(), state.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 117 | void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 118 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 119 | CHECK_GE(fd, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | memset(fde, 0, sizeof(fdevent)); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | fdevent* fdevent_create(int fd, fd_func func, void* arg) { |
| 124 | check_main_thread(); |
| 125 | CHECK_GE(fd, 0); |
| 126 | |
| 127 | fdevent* fde = new fdevent(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | fde->state = FDE_ACTIVE; |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 129 | fde->fd.reset(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | fde->func = func; |
| 131 | fde->arg = arg; |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 132 | if (!set_file_block_mode(fd, false)) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 133 | // Here is not proper to handle the error. If it fails here, some error is |
| 134 | // likely to be detected by poll(), then we can let the callback function |
| 135 | // to handle it. |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 136 | LOG(ERROR) << "failed to set non-blocking mode for fd " << fd; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 137 | } |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 138 | auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde)); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 139 | CHECK(pair.second) << "install existing fd " << fd; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 140 | |
| 141 | fde->state |= FDE_CREATED; |
| 142 | return fde; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 145 | void fdevent_destroy(fdevent* fde) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 146 | check_main_thread(); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 147 | if (fde == 0) return; |
| 148 | if (!(fde->state & FDE_CREATED)) { |
| 149 | LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde); |
| 150 | } |
| 151 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 152 | if (fde->state & FDE_ACTIVE) { |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 153 | g_poll_node_map.erase(fde->fd.get()); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 154 | if (fde->state & FDE_PENDING) { |
| 155 | g_pending_list.remove(fde); |
| 156 | } |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 157 | fde->fd.reset(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 158 | fde->state = 0; |
| 159 | fde->events = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | } |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 161 | |
| 162 | delete fde; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 165 | static void fdevent_update(fdevent* fde, unsigned events) { |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 166 | auto it = g_poll_node_map.find(fde->fd.get()); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 167 | CHECK(it != g_poll_node_map.end()); |
| 168 | PollNode& node = it->second; |
| 169 | if (events & FDE_READ) { |
| 170 | node.pollfd.events |= POLLIN; |
| 171 | } else { |
| 172 | node.pollfd.events &= ~POLLIN; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 175 | if (events & FDE_WRITE) { |
| 176 | node.pollfd.events |= POLLOUT; |
| 177 | } else { |
| 178 | node.pollfd.events &= ~POLLOUT; |
| 179 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | fde->state = (fde->state & FDE_STATEMASK) | events; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 181 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 183 | void fdevent_set(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 184 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 185 | events &= FDE_EVENTMASK; |
| 186 | if ((fde->state & FDE_EVENTMASK) == events) { |
| 187 | return; |
| 188 | } |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 189 | CHECK(fde->state & FDE_ACTIVE); |
| 190 | fdevent_update(fde, events); |
| 191 | D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 192 | |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 193 | if (fde->state & FDE_PENDING) { |
| 194 | // If we are pending, make sure we don't signal an event that is no longer wanted. |
| 195 | fde->events &= events; |
| 196 | if (fde->events == 0) { |
| 197 | g_pending_list.remove(fde); |
| 198 | fde->state &= ~FDE_PENDING; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 203 | void fdevent_add(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 204 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 205 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 208 | void fdevent_del(fdevent* fde, unsigned events) { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 209 | check_main_thread(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 210 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 213 | static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 214 | std::string result; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 215 | for (const auto& pollfd : pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 216 | std::string op; |
| 217 | if (pollfd.events & POLLIN) { |
| 218 | op += "R"; |
| 219 | } |
| 220 | if (pollfd.events & POLLOUT) { |
| 221 | op += "W"; |
| 222 | } |
| 223 | android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str()); |
| 224 | } |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | static void fdevent_process() { |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 229 | std::vector<adb_pollfd> pollfds; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 230 | for (const auto& pair : g_poll_node_map) { |
| 231 | pollfds.push_back(pair.second.pollfd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 232 | } |
| 233 | CHECK_GT(pollfds.size(), 0u); |
| 234 | D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str()); |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 235 | int ret = adb_poll(&pollfds[0], pollfds.size(), -1); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 236 | if (ret == -1) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 237 | PLOG(ERROR) << "poll(), ret = " << ret; |
| 238 | return; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 239 | } |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 240 | for (const auto& pollfd : pollfds) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 241 | if (pollfd.revents != 0) { |
| 242 | D("for fd %d, revents = %x", pollfd.fd, pollfd.revents); |
| 243 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 244 | unsigned events = 0; |
| 245 | if (pollfd.revents & POLLIN) { |
| 246 | events |= FDE_READ; |
| 247 | } |
| 248 | if (pollfd.revents & POLLOUT) { |
| 249 | events |= FDE_WRITE; |
| 250 | } |
| 251 | if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 252 | // We fake a read, as the rest of the code assumes that errors will |
| 253 | // be detected at that point. |
| 254 | events |= FDE_READ | FDE_ERROR; |
| 255 | } |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 256 | #if defined(__linux__) |
| 257 | if (pollfd.revents & POLLRDHUP) { |
| 258 | events |= FDE_READ | FDE_ERROR; |
| 259 | } |
| 260 | #endif |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 261 | if (events != 0) { |
| 262 | auto it = g_poll_node_map.find(pollfd.fd); |
| 263 | CHECK(it != g_poll_node_map.end()); |
| 264 | fdevent* fde = it->second.fde; |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 265 | CHECK_EQ(fde->fd.get(), pollfd.fd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 266 | fde->events |= events; |
| 267 | D("%s got events %x", dump_fde(fde).c_str(), events); |
| 268 | fde->state |= FDE_PENDING; |
| 269 | g_pending_list.push_back(fde); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 274 | static void fdevent_call_fdfunc(fdevent* fde) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 275 | unsigned events = fde->events; |
| 276 | fde->events = 0; |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 277 | CHECK(fde->state & FDE_PENDING); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 278 | fde->state &= (~FDE_PENDING); |
| 279 | D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 280 | fde->func(fde->fd.get(), events, fde->arg); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 283 | static void fdevent_run_flush() EXCLUDES(run_queue_mutex) { |
| 284 | // We need to be careful around reentrancy here, since a function we call can queue up another |
| 285 | // function. |
| 286 | while (true) { |
| 287 | std::function<void()> fn; |
| 288 | { |
| 289 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 290 | if (run_queue.empty()) { |
| 291 | break; |
| 292 | } |
| 293 | fn = run_queue.front(); |
| 294 | run_queue.pop_front(); |
| 295 | } |
| 296 | fn(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 297 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) { |
| 301 | CHECK_GE(fd, 0); |
| 302 | CHECK(ev & FDE_READ); |
| 303 | |
| 304 | char buf[1024]; |
| 305 | |
| 306 | // Empty the fd. |
| 307 | if (adb_read(fd, buf, sizeof(buf)) == -1) { |
| 308 | PLOG(FATAL) << "failed to empty run queue notify fd"; |
| 309 | } |
| 310 | |
Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 311 | // Mark that we need to flush, and then run it at the end of fdevent_loop. |
| 312 | run_needs_flush = true; |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void fdevent_run_setup() { |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 316 | { |
| 317 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 318 | CHECK(run_queue_notify_fd.get() == -1); |
| 319 | int s[2]; |
| 320 | if (adb_socketpair(s) != 0) { |
| 321 | PLOG(FATAL) << "failed to create run queue notify socketpair"; |
| 322 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 323 | |
Josh Gao | 1222abc | 2018-03-19 15:19:45 -0700 | [diff] [blame] | 324 | if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) { |
| 325 | PLOG(FATAL) << "failed to make run queue notify socket nonblocking"; |
| 326 | } |
| 327 | |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 328 | run_queue_notify_fd.reset(s[0]); |
| 329 | fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr); |
| 330 | CHECK(fde != nullptr); |
| 331 | fdevent_add(fde, FDE_READ); |
| 332 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 333 | |
| 334 | fdevent_run_flush(); |
| 335 | } |
| 336 | |
| 337 | void fdevent_run_on_main_thread(std::function<void()> fn) { |
| 338 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 339 | run_queue.push_back(std::move(fn)); |
| 340 | |
| 341 | // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up. |
| 342 | // In that case, rely on the setup code to flush the queue without a notification being needed. |
| 343 | if (run_queue_notify_fd != -1) { |
Josh Gao | 1222abc | 2018-03-19 15:19:45 -0700 | [diff] [blame] | 344 | int rc = adb_write(run_queue_notify_fd.get(), "", 1); |
| 345 | |
| 346 | // It's possible that we get EAGAIN here, if lots of notifications came in while handling. |
| 347 | if (rc == 0) { |
| 348 | PLOG(FATAL) << "run queue notify fd was closed?"; |
| 349 | } else if (rc == -1 && errno != EAGAIN) { |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 350 | PLOG(FATAL) << "failed to write to run queue notify fd"; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void fdevent_loop() { |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 356 | set_main_thread(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 357 | fdevent_run_setup(); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 358 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 359 | while (true) { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 360 | if (terminate_loop) { |
| 361 | return; |
| 362 | } |
| 363 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 364 | D("--- --- waiting for events"); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 365 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | fdevent_process(); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 367 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 368 | while (!g_pending_list.empty()) { |
| 369 | fdevent* fde = g_pending_list.front(); |
| 370 | g_pending_list.pop_front(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 371 | fdevent_call_fdfunc(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | } |
Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 373 | |
| 374 | if (run_needs_flush) { |
| 375 | fdevent_run_flush(); |
| 376 | run_needs_flush = false; |
| 377 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 380 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 381 | void fdevent_terminate_loop() { |
| 382 | terminate_loop = true; |
| 383 | } |
| 384 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 385 | size_t fdevent_installed_count() { |
| 386 | return g_poll_node_map.size(); |
| 387 | } |
| 388 | |
| 389 | void fdevent_reset() { |
| 390 | g_poll_node_map.clear(); |
| 391 | g_pending_list.clear(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 392 | |
| 393 | std::lock_guard<std::mutex> lock(run_queue_mutex); |
| 394 | run_queue_notify_fd.reset(); |
| 395 | run_queue.clear(); |
| 396 | |
Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 397 | main_thread_valid = false; |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 398 | terminate_loop = false; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 399 | } |