| 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 | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 24 | #include <inttypes.h> | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 25 | #include <stdint.h> | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 28 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 30 | #include <atomic> | 
| Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 31 | #include <deque> | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 32 | #include <functional> | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 33 | #include <list> | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 34 | #include <mutex> | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 35 | #include <optional> | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 36 | #include <unordered_map> | 
| Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 37 | #include <utility> | 
|  | 38 | #include <variant> | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 39 | #include <vector> | 
|  | 40 |  | 
| Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 41 | #include <android-base/chrono_utils.h> | 
|  | 42 | #include <android-base/file.h> | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> | 
|  | 44 | #include <android-base/stringprintf.h> | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 45 | #include <android-base/thread_annotations.h> | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 46 | #include <android-base/threads.h> | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 47 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 48 | #include "adb_io.h" | 
| leozwang | d3fc15f | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 49 | #include "adb_trace.h" | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 50 | #include "adb_unique_fd.h" | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 51 | #include "adb_utils.h" | 
| Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 52 | #include "sysdeps/chrono.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | #define FDE_EVENTMASK  0x00ff | 
|  | 55 | #define FDE_STATEMASK  0xff00 | 
|  | 56 |  | 
|  | 57 | #define FDE_ACTIVE     0x0100 | 
|  | 58 | #define FDE_PENDING    0x0200 | 
|  | 59 | #define FDE_CREATED    0x0400 | 
|  | 60 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 61 | struct PollNode { | 
|  | 62 | fdevent* fde; | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 63 | adb_pollfd pollfd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 |  | 
| Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 65 | explicit PollNode(fdevent* fde) : fde(fde) { | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 66 | memset(&pollfd, 0, sizeof(pollfd)); | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 67 | pollfd.fd = fde->fd.get(); | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | #if defined(__linux__) | 
|  | 70 | // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. | 
|  | 71 | // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034. | 
|  | 72 | pollfd.events = POLLRDHUP; | 
|  | 73 | #endif | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 74 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | }; | 
|  | 76 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 77 | // All operations to fdevent should happen only in the main thread. | 
|  | 78 | // That's why we don't need a lock for fdevent. | 
| Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 79 | static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>(); | 
|  | 80 | static auto& g_pending_list = *new std::list<fdevent*>(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 81 | static std::atomic<bool> terminate_loop(false); | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 82 | static bool main_thread_valid; | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 83 | static uint64_t main_thread_id; | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 84 |  | 
| Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 85 | static uint64_t fdevent_id; | 
|  | 86 |  | 
| Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 87 | static bool run_needs_flush = false; | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 88 | static auto& run_queue_notify_fd = *new unique_fd(); | 
|  | 89 | static auto& run_queue_mutex = *new std::mutex(); | 
| Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 90 | 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] | 91 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 92 | void check_main_thread() { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 93 | if (main_thread_valid) { | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 94 | CHECK_EQ(main_thread_id, android::base::GetThreadId()); | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 95 | } | 
|  | 96 | } | 
|  | 97 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 98 | void set_main_thread() { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 99 | main_thread_valid = true; | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 100 | main_thread_id = android::base::GetThreadId(); | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 101 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 103 | static std::string dump_fde(const fdevent* fde) { | 
|  | 104 | std::string state; | 
|  | 105 | if (fde->state & FDE_ACTIVE) { | 
|  | 106 | state += "A"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | } | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 108 | if (fde->state & FDE_PENDING) { | 
|  | 109 | state += "P"; | 
|  | 110 | } | 
|  | 111 | if (fde->state & FDE_CREATED) { | 
|  | 112 | state += "C"; | 
|  | 113 | } | 
|  | 114 | if (fde->state & FDE_READ) { | 
|  | 115 | state += "R"; | 
|  | 116 | } | 
|  | 117 | if (fde->state & FDE_WRITE) { | 
|  | 118 | state += "W"; | 
|  | 119 | } | 
|  | 120 | if (fde->state & FDE_ERROR) { | 
|  | 121 | state += "E"; | 
|  | 122 | } | 
| Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 123 | return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(), | 
|  | 124 | state.c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
| Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 127 | template <typename F> | 
|  | 128 | static fdevent* fdevent_create_impl(int fd, F func, void* arg) { | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 129 | check_main_thread(); | 
|  | 130 | CHECK_GE(fd, 0); | 
|  | 131 |  | 
|  | 132 | fdevent* fde = new fdevent(); | 
| Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 133 | fde->id = fdevent_id++; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | fde->state = FDE_ACTIVE; | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 135 | fde->fd.reset(fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | fde->func = func; | 
|  | 137 | fde->arg = arg; | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 138 | if (!set_file_block_mode(fd, false)) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 139 | // 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 Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 142 | LOG(ERROR) << "failed to set non-blocking mode for fd " << fd; | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 143 | } | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 144 | auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde)); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 145 | CHECK(pair.second) << "install existing fd " << fd; | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | fde->state |= FDE_CREATED; | 
|  | 148 | return fde; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 151 | fdevent* fdevent_create(int fd, fd_func func, void* arg) { | 
|  | 152 | return fdevent_create_impl(fd, func, arg); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | fdevent* fdevent_create(int fd, fd_func2 func, void* arg) { | 
|  | 156 | return fdevent_create_impl(fd, func, arg); | 
|  | 157 | } | 
|  | 158 |  | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 159 | unique_fd fdevent_release(fdevent* fde) { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 160 | check_main_thread(); | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 161 | if (!fde) { | 
|  | 162 | return {}; | 
|  | 163 | } | 
|  | 164 |  | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 165 | if (!(fde->state & FDE_CREATED)) { | 
|  | 166 | LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde); | 
|  | 167 | } | 
|  | 168 |  | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 169 | unique_fd result = std::move(fde->fd); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 170 | if (fde->state & FDE_ACTIVE) { | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 171 | g_poll_node_map.erase(result.get()); | 
|  | 172 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 173 | if (fde->state & FDE_PENDING) { | 
|  | 174 | g_pending_list.remove(fde); | 
|  | 175 | } | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 176 | fde->state = 0; | 
|  | 177 | fde->events = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | } | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | delete fde; | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 181 | return result; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | void fdevent_destroy(fdevent* fde) { | 
|  | 185 | // Release, and then let unique_fd's destructor cleanup. | 
|  | 186 | fdevent_release(fde); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 189 | static void fdevent_update(fdevent* fde, unsigned events) { | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 190 | auto it = g_poll_node_map.find(fde->fd.get()); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 191 | CHECK(it != g_poll_node_map.end()); | 
|  | 192 | PollNode& node = it->second; | 
|  | 193 | if (events & FDE_READ) { | 
|  | 194 | node.pollfd.events |= POLLIN; | 
|  | 195 | } else { | 
|  | 196 | node.pollfd.events &= ~POLLIN; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 199 | if (events & FDE_WRITE) { | 
|  | 200 | node.pollfd.events |= POLLOUT; | 
|  | 201 | } else { | 
|  | 202 | node.pollfd.events &= ~POLLOUT; | 
|  | 203 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | fde->state = (fde->state & FDE_STATEMASK) | events; | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 205 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 207 | void fdevent_set(fdevent* fde, unsigned events) { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 208 | check_main_thread(); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 209 | events &= FDE_EVENTMASK; | 
|  | 210 | if ((fde->state & FDE_EVENTMASK) == events) { | 
|  | 211 | return; | 
|  | 212 | } | 
| Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 213 | CHECK(fde->state & FDE_ACTIVE); | 
|  | 214 | fdevent_update(fde, events); | 
|  | 215 | D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 216 |  | 
| Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 217 | if (fde->state & FDE_PENDING) { | 
|  | 218 | // If we are pending, make sure we don't signal an event that is no longer wanted. | 
|  | 219 | fde->events &= events; | 
|  | 220 | if (fde->events == 0) { | 
|  | 221 | g_pending_list.remove(fde); | 
|  | 222 | fde->state &= ~FDE_PENDING; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | } | 
|  | 224 | } | 
|  | 225 | } | 
|  | 226 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 227 | void fdevent_add(fdevent* fde, unsigned events) { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 228 | check_main_thread(); | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 229 | CHECK(!(events & FDE_TIMEOUT)); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 230 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 233 | void fdevent_del(fdevent* fde, unsigned events) { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 234 | check_main_thread(); | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 235 | CHECK(!(events & FDE_TIMEOUT)); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 236 | fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 239 | void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) { | 
|  | 240 | check_main_thread(); | 
|  | 241 | fde->timeout = timeout; | 
|  | 242 | fde->last_active = std::chrono::steady_clock::now(); | 
|  | 243 | } | 
|  | 244 |  | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 245 | static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) { | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 246 | std::string result; | 
| Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 247 | for (const auto& pollfd : pollfds) { | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 248 | std::string op; | 
|  | 249 | if (pollfd.events & POLLIN) { | 
|  | 250 | op += "R"; | 
|  | 251 | } | 
|  | 252 | if (pollfd.events & POLLOUT) { | 
|  | 253 | op += "W"; | 
|  | 254 | } | 
|  | 255 | android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str()); | 
|  | 256 | } | 
|  | 257 | return result; | 
|  | 258 | } | 
|  | 259 |  | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 260 | static std::optional<std::chrono::milliseconds> calculate_timeout() { | 
|  | 261 | std::optional<std::chrono::milliseconds> result = std::nullopt; | 
|  | 262 | auto now = std::chrono::steady_clock::now(); | 
|  | 263 | check_main_thread(); | 
|  | 264 |  | 
|  | 265 | for (const auto& [fd, pollnode] : g_poll_node_map) { | 
|  | 266 | UNUSED(fd); | 
|  | 267 | auto timeout_opt = pollnode.fde->timeout; | 
|  | 268 | if (timeout_opt) { | 
|  | 269 | auto deadline = pollnode.fde->last_active + *timeout_opt; | 
|  | 270 | auto time_left = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now); | 
|  | 271 | if (time_left < std::chrono::milliseconds::zero()) { | 
|  | 272 | time_left = std::chrono::milliseconds::zero(); | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | if (!result) { | 
|  | 276 | result = time_left; | 
|  | 277 | } else { | 
|  | 278 | result = std::min(*result, time_left); | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | return result; | 
|  | 284 | } | 
|  | 285 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 286 | static void fdevent_process() { | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 287 | std::vector<adb_pollfd> pollfds; | 
| Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 288 | for (const auto& pair : g_poll_node_map) { | 
|  | 289 | pollfds.push_back(pair.second.pollfd); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 290 | } | 
|  | 291 | CHECK_GT(pollfds.size(), 0u); | 
|  | 292 | D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str()); | 
| Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 293 |  | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 294 | auto timeout = calculate_timeout(); | 
|  | 295 | int timeout_ms; | 
|  | 296 | if (!timeout) { | 
|  | 297 | timeout_ms = -1; | 
|  | 298 | } else { | 
|  | 299 | timeout_ms = timeout->count(); | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | int ret = adb_poll(&pollfds[0], pollfds.size(), timeout_ms); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 303 | if (ret == -1) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 304 | PLOG(ERROR) << "poll(), ret = " << ret; | 
|  | 305 | return; | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 306 | } | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 307 |  | 
|  | 308 | auto post_poll = std::chrono::steady_clock::now(); | 
|  | 309 |  | 
| Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 310 | for (const auto& pollfd : pollfds) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 311 | if (pollfd.revents != 0) { | 
|  | 312 | D("for fd %d, revents = %x", pollfd.fd, pollfd.revents); | 
|  | 313 | } | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 314 | unsigned events = 0; | 
|  | 315 | if (pollfd.revents & POLLIN) { | 
|  | 316 | events |= FDE_READ; | 
|  | 317 | } | 
|  | 318 | if (pollfd.revents & POLLOUT) { | 
|  | 319 | events |= FDE_WRITE; | 
|  | 320 | } | 
|  | 321 | if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { | 
|  | 322 | // We fake a read, as the rest of the code assumes that errors will | 
|  | 323 | // be detected at that point. | 
|  | 324 | events |= FDE_READ | FDE_ERROR; | 
|  | 325 | } | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 326 | #if defined(__linux__) | 
|  | 327 | if (pollfd.revents & POLLRDHUP) { | 
|  | 328 | events |= FDE_READ | FDE_ERROR; | 
|  | 329 | } | 
|  | 330 | #endif | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 331 | auto it = g_poll_node_map.find(pollfd.fd); | 
|  | 332 | CHECK(it != g_poll_node_map.end()); | 
|  | 333 | fdevent* fde = it->second.fde; | 
|  | 334 |  | 
|  | 335 | if (events == 0) { | 
|  | 336 | // Check for timeout. | 
|  | 337 | if (fde->timeout) { | 
|  | 338 | auto deadline = fde->last_active + *fde->timeout; | 
|  | 339 | if (deadline < post_poll) { | 
|  | 340 | events |= FDE_TIMEOUT; | 
|  | 341 | } | 
|  | 342 | } | 
|  | 343 | } | 
|  | 344 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 345 | if (events != 0) { | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 346 | CHECK_EQ(fde->fd.get(), pollfd.fd); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 347 | fde->events |= events; | 
| Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 348 | fde->last_active = post_poll; | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 349 | D("%s got events %x", dump_fde(fde).c_str(), events); | 
|  | 350 | fde->state |= FDE_PENDING; | 
|  | 351 | g_pending_list.push_back(fde); | 
|  | 352 | } | 
|  | 353 | } | 
|  | 354 | } | 
|  | 355 |  | 
| Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 356 | template <class T> | 
|  | 357 | struct always_false : std::false_type {}; | 
|  | 358 |  | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 359 | static void fdevent_call_fdfunc(fdevent* fde) { | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 360 | unsigned events = fde->events; | 
|  | 361 | fde->events = 0; | 
| Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 362 | CHECK(fde->state & FDE_PENDING); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 363 | fde->state &= (~FDE_PENDING); | 
|  | 364 | D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); | 
| Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 365 | std::visit( | 
|  | 366 | [&](auto&& f) { | 
|  | 367 | using F = std::decay_t<decltype(f)>; | 
|  | 368 | if constexpr (std::is_same_v<fd_func, F>) { | 
|  | 369 | f(fde->fd.get(), events, fde->arg); | 
|  | 370 | } else if constexpr (std::is_same_v<fd_func2, F>) { | 
|  | 371 | f(fde, events, fde->arg); | 
|  | 372 | } else { | 
|  | 373 | static_assert(always_false<F>::value, "non-exhaustive visitor"); | 
|  | 374 | } | 
|  | 375 | }, | 
|  | 376 | fde->func); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
| Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 379 | static void fdevent_run_flush() EXCLUDES(run_queue_mutex) { | 
|  | 380 | // We need to be careful around reentrancy here, since a function we call can queue up another | 
|  | 381 | // function. | 
|  | 382 | while (true) { | 
|  | 383 | std::function<void()> fn; | 
|  | 384 | { | 
|  | 385 | std::lock_guard<std::mutex> lock(run_queue_mutex); | 
|  | 386 | if (run_queue.empty()) { | 
|  | 387 | break; | 
|  | 388 | } | 
|  | 389 | fn = run_queue.front(); | 
|  | 390 | run_queue.pop_front(); | 
|  | 391 | } | 
|  | 392 | fn(); | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 393 | } | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
|  | 396 | static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) { | 
|  | 397 | CHECK_GE(fd, 0); | 
|  | 398 | CHECK(ev & FDE_READ); | 
|  | 399 |  | 
|  | 400 | char buf[1024]; | 
|  | 401 |  | 
|  | 402 | // Empty the fd. | 
|  | 403 | if (adb_read(fd, buf, sizeof(buf)) == -1) { | 
|  | 404 | PLOG(FATAL) << "failed to empty run queue notify fd"; | 
|  | 405 | } | 
|  | 406 |  | 
| Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 407 | // Mark that we need to flush, and then run it at the end of fdevent_loop. | 
|  | 408 | run_needs_flush = true; | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 409 | } | 
|  | 410 |  | 
|  | 411 | static void fdevent_run_setup() { | 
| Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 412 | { | 
|  | 413 | std::lock_guard<std::mutex> lock(run_queue_mutex); | 
|  | 414 | CHECK(run_queue_notify_fd.get() == -1); | 
|  | 415 | int s[2]; | 
|  | 416 | if (adb_socketpair(s) != 0) { | 
|  | 417 | PLOG(FATAL) << "failed to create run queue notify socketpair"; | 
|  | 418 | } | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 419 |  | 
| Josh Gao | 1222abc | 2018-03-19 15:19:45 -0700 | [diff] [blame] | 420 | if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) { | 
|  | 421 | PLOG(FATAL) << "failed to make run queue notify socket nonblocking"; | 
|  | 422 | } | 
|  | 423 |  | 
| Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 424 | run_queue_notify_fd.reset(s[0]); | 
|  | 425 | fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr); | 
|  | 426 | CHECK(fde != nullptr); | 
|  | 427 | fdevent_add(fde, FDE_READ); | 
|  | 428 | } | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 429 |  | 
|  | 430 | fdevent_run_flush(); | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | void fdevent_run_on_main_thread(std::function<void()> fn) { | 
|  | 434 | std::lock_guard<std::mutex> lock(run_queue_mutex); | 
|  | 435 | run_queue.push_back(std::move(fn)); | 
|  | 436 |  | 
|  | 437 | // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up. | 
|  | 438 | // In that case, rely on the setup code to flush the queue without a notification being needed. | 
|  | 439 | if (run_queue_notify_fd != -1) { | 
| Josh Gao | 1222abc | 2018-03-19 15:19:45 -0700 | [diff] [blame] | 440 | int rc = adb_write(run_queue_notify_fd.get(), "", 1); | 
|  | 441 |  | 
|  | 442 | // It's possible that we get EAGAIN here, if lots of notifications came in while handling. | 
|  | 443 | if (rc == 0) { | 
|  | 444 | PLOG(FATAL) << "run queue notify fd was closed?"; | 
|  | 445 | } else if (rc == -1 && errno != EAGAIN) { | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 446 | PLOG(FATAL) << "failed to write to run queue notify fd"; | 
|  | 447 | } | 
|  | 448 | } | 
|  | 449 | } | 
|  | 450 |  | 
| Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 451 | static void fdevent_check_spin(uint64_t cycle) { | 
|  | 452 | // Check to see if we're spinning because we forgot about an fdevent | 
|  | 453 | // by keeping track of how long fdevents have been continuously pending. | 
|  | 454 | struct SpinCheck { | 
|  | 455 | fdevent* fde; | 
|  | 456 | android::base::boot_clock::time_point timestamp; | 
|  | 457 | uint64_t cycle; | 
|  | 458 | }; | 
|  | 459 | static auto& g_continuously_pending = *new std::unordered_map<uint64_t, SpinCheck>(); | 
|  | 460 | static auto last_cycle = android::base::boot_clock::now(); | 
|  | 461 |  | 
|  | 462 | auto now = android::base::boot_clock::now(); | 
|  | 463 | if (now - last_cycle > 10ms) { | 
|  | 464 | // We're not spinning. | 
|  | 465 | g_continuously_pending.clear(); | 
|  | 466 | last_cycle = now; | 
|  | 467 | return; | 
|  | 468 | } | 
|  | 469 | last_cycle = now; | 
|  | 470 |  | 
|  | 471 | for (auto* fde : g_pending_list) { | 
|  | 472 | auto it = g_continuously_pending.find(fde->id); | 
|  | 473 | if (it == g_continuously_pending.end()) { | 
|  | 474 | g_continuously_pending[fde->id] = | 
|  | 475 | SpinCheck{.fde = fde, .timestamp = now, .cycle = cycle}; | 
|  | 476 | } else { | 
|  | 477 | it->second.cycle = cycle; | 
|  | 478 | } | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | for (auto it = g_continuously_pending.begin(); it != g_continuously_pending.end();) { | 
|  | 482 | if (it->second.cycle != cycle) { | 
|  | 483 | it = g_continuously_pending.erase(it); | 
|  | 484 | } else { | 
|  | 485 | // Use an absurdly long window, since all we really care about is | 
|  | 486 | // getting a bugreport eventually. | 
|  | 487 | if (now - it->second.timestamp > 300s) { | 
|  | 488 | LOG(FATAL_WITHOUT_ABORT) | 
|  | 489 | << "detected spin in fdevent: " << dump_fde(it->second.fde); | 
|  | 490 | #if defined(__linux__) | 
|  | 491 | int fd = it->second.fde->fd.get(); | 
|  | 492 | std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd); | 
|  | 493 | std::string path; | 
|  | 494 | if (!android::base::Readlink(fd_path, &path)) { | 
|  | 495 | PLOG(FATAL_WITHOUT_ABORT) << "readlink of fd " << fd << " failed"; | 
|  | 496 | } | 
|  | 497 | LOG(FATAL_WITHOUT_ABORT) << "fd " << fd << " = " << path; | 
|  | 498 | #endif | 
|  | 499 | abort(); | 
|  | 500 | } | 
|  | 501 | ++it; | 
|  | 502 | } | 
|  | 503 | } | 
|  | 504 | } | 
|  | 505 |  | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 506 | void fdevent_loop() { | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 507 | set_main_thread(); | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 508 | fdevent_run_setup(); | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 509 |  | 
| Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 510 | uint64_t cycle = 0; | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 511 | while (true) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 512 | if (terminate_loop) { | 
|  | 513 | return; | 
|  | 514 | } | 
|  | 515 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 516 | D("--- --- waiting for events"); | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 517 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | fdevent_process(); | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 519 |  | 
| Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 520 | fdevent_check_spin(cycle++); | 
|  | 521 |  | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 522 | while (!g_pending_list.empty()) { | 
|  | 523 | fdevent* fde = g_pending_list.front(); | 
|  | 524 | g_pending_list.pop_front(); | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 525 | fdevent_call_fdfunc(fde); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | } | 
| Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 527 |  | 
|  | 528 | if (run_needs_flush) { | 
|  | 529 | fdevent_run_flush(); | 
|  | 530 | run_needs_flush = false; | 
|  | 531 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 532 | } | 
|  | 533 | } | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 534 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 535 | void fdevent_terminate_loop() { | 
|  | 536 | terminate_loop = true; | 
|  | 537 | } | 
|  | 538 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 539 | size_t fdevent_installed_count() { | 
|  | 540 | return g_poll_node_map.size(); | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | void fdevent_reset() { | 
|  | 544 | g_poll_node_map.clear(); | 
|  | 545 | g_pending_list.clear(); | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 546 |  | 
|  | 547 | std::lock_guard<std::mutex> lock(run_queue_mutex); | 
|  | 548 | run_queue_notify_fd.reset(); | 
|  | 549 | run_queue.clear(); | 
|  | 550 |  | 
| Yabin Cui | fbfa840 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 551 | main_thread_valid = false; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 552 | terminate_loop = false; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 553 | } |