Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010 The Android Open Source Project |
| 3 | // |
| 4 | // A looper implementation based on epoll(). |
| 5 | // |
| 6 | #define LOG_TAG "Looper" |
| 7 | |
| 8 | //#define LOG_NDEBUG 0 |
| 9 | |
| 10 | // Debugs poll and wake interactions. |
| 11 | #define DEBUG_POLL_AND_WAKE 0 |
| 12 | |
| 13 | // Debugs callback registration and invocation. |
| 14 | #define DEBUG_CALLBACKS 0 |
| 15 | |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 16 | #include <utils/Looper.h> |
Alessio Balsini | 1406207 | 2019-05-11 14:29:28 +0100 | [diff] [blame] | 17 | |
Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 18 | #include <sys/eventfd.h> |
Alessio Balsini | 1406207 | 2019-05-11 14:29:28 +0100 | [diff] [blame] | 19 | #include <cinttypes> |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | constexpr uint64_t WAKE_EVENT_FD_SEQ = 1; |
| 26 | |
| 27 | epoll_event createEpollEvent(uint32_t events, uint64_t seq) { |
| 28 | return {.events = events, .data = {.u64 = seq}}; |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 33 | // --- WeakMessageHandler --- |
| 34 | |
| 35 | WeakMessageHandler::WeakMessageHandler(const wp<MessageHandler>& handler) : |
| 36 | mHandler(handler) { |
| 37 | } |
| 38 | |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 39 | WeakMessageHandler::~WeakMessageHandler() { |
| 40 | } |
| 41 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 42 | void WeakMessageHandler::handleMessage(const Message& message) { |
| 43 | sp<MessageHandler> handler = mHandler.promote(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 44 | if (handler != nullptr) { |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 45 | handler->handleMessage(message); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 50 | // --- SimpleLooperCallback --- |
| 51 | |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 52 | SimpleLooperCallback::SimpleLooperCallback(Looper_callbackFunc callback) : |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 53 | mCallback(callback) { |
| 54 | } |
| 55 | |
| 56 | SimpleLooperCallback::~SimpleLooperCallback() { |
| 57 | } |
| 58 | |
| 59 | int SimpleLooperCallback::handleEvent(int fd, int events, void* data) { |
| 60 | return mCallback(fd, events, data); |
| 61 | } |
| 62 | |
| 63 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 64 | // --- Looper --- |
| 65 | |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 66 | // Maximum number of file descriptors for which to retrieve poll events each iteration. |
| 67 | static const int EPOLL_MAX_EVENTS = 16; |
| 68 | |
Jeff Brown | d180518 | 2010-09-21 15:11:18 -0700 | [diff] [blame] | 69 | static pthread_once_t gTLSOnce = PTHREAD_ONCE_INIT; |
| 70 | static pthread_key_t gTLSKey = 0; |
| 71 | |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 72 | Looper::Looper(bool allowNonCallbacks) |
| 73 | : mAllowNonCallbacks(allowNonCallbacks), |
| 74 | mSendingMessage(false), |
| 75 | mPolling(false), |
| 76 | mEpollRebuildRequired(false), |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 77 | mNextRequestSeq(WAKE_EVENT_FD_SEQ + 1), |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 78 | mResponseIndex(0), |
| 79 | mNextMessageUptime(LLONG_MAX) { |
| 80 | mWakeEventFd.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)); |
| 81 | LOG_ALWAYS_FATAL_IF(mWakeEventFd.get() < 0, "Could not make wake event fd: %s", strerror(errno)); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 82 | |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 83 | AutoMutex _l(mLock); |
| 84 | rebuildEpollLocked(); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | Looper::~Looper() { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jeff Brown | d180518 | 2010-09-21 15:11:18 -0700 | [diff] [blame] | 90 | void Looper::initTLSKey() { |
Elliott Hughes | fd12160 | 2019-04-01 12:22:55 -0700 | [diff] [blame] | 91 | int error = pthread_key_create(&gTLSKey, threadDestructor); |
| 92 | LOG_ALWAYS_FATAL_IF(error != 0, "Could not allocate TLS key: %s", strerror(error)); |
Jeff Brown | d180518 | 2010-09-21 15:11:18 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 95 | void Looper::threadDestructor(void *st) { |
| 96 | Looper* const self = static_cast<Looper*>(st); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 97 | if (self != nullptr) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 98 | self->decStrong((void*)threadDestructor); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void Looper::setForThread(const sp<Looper>& looper) { |
| 103 | sp<Looper> old = getForThread(); // also has side-effect of initializing TLS |
| 104 | |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 105 | if (looper != nullptr) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 106 | looper->incStrong((void*)threadDestructor); |
| 107 | } |
| 108 | |
Jeff Brown | d180518 | 2010-09-21 15:11:18 -0700 | [diff] [blame] | 109 | pthread_setspecific(gTLSKey, looper.get()); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 110 | |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 111 | if (old != nullptr) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 112 | old->decStrong((void*)threadDestructor); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | sp<Looper> Looper::getForThread() { |
Jeff Brown | d180518 | 2010-09-21 15:11:18 -0700 | [diff] [blame] | 117 | int result = pthread_once(& gTLSOnce, initTLSKey); |
| 118 | LOG_ALWAYS_FATAL_IF(result != 0, "pthread_once failed"); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 119 | |
Steven Moreland | a06e68c | 2021-04-27 00:09:23 +0000 | [diff] [blame] | 120 | Looper* looper = (Looper*)pthread_getspecific(gTLSKey); |
| 121 | return sp<Looper>::fromExisting(looper); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | sp<Looper> Looper::prepare(int opts) { |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 125 | bool allowNonCallbacks = opts & PREPARE_ALLOW_NON_CALLBACKS; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 126 | sp<Looper> looper = Looper::getForThread(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 127 | if (looper == nullptr) { |
Steven Moreland | a06e68c | 2021-04-27 00:09:23 +0000 | [diff] [blame] | 128 | looper = sp<Looper>::make(allowNonCallbacks); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 129 | Looper::setForThread(looper); |
| 130 | } |
| 131 | if (looper->getAllowNonCallbacks() != allowNonCallbacks) { |
Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 132 | ALOGW("Looper already prepared for this thread with a different value for the " |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 133 | "LOOPER_PREPARE_ALLOW_NON_CALLBACKS option."); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 134 | } |
| 135 | return looper; |
| 136 | } |
| 137 | |
| 138 | bool Looper::getAllowNonCallbacks() const { |
| 139 | return mAllowNonCallbacks; |
| 140 | } |
| 141 | |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 142 | void Looper::rebuildEpollLocked() { |
| 143 | // Close old epoll instance if we have one. |
| 144 | if (mEpollFd >= 0) { |
| 145 | #if DEBUG_CALLBACKS |
| 146 | ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this); |
| 147 | #endif |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 148 | mEpollFd.reset(); |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 151 | // Allocate the new epoll instance and register the WakeEventFd. |
Nick Kralevich | 0a8b4d1 | 2018-12-17 09:35:12 -0800 | [diff] [blame] | 152 | mEpollFd.reset(epoll_create1(EPOLL_CLOEXEC)); |
Elliott Hughes | 5b8ff09 | 2015-06-30 15:17:14 -0700 | [diff] [blame] | 153 | LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno)); |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 154 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 155 | epoll_event wakeEvent = createEpollEvent(EPOLLIN, WAKE_EVENT_FD_SEQ); |
| 156 | int result = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mWakeEventFd.get(), &wakeEvent); |
Elliott Hughes | 5b8ff09 | 2015-06-30 15:17:14 -0700 | [diff] [blame] | 157 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance: %s", |
| 158 | strerror(errno)); |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 159 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 160 | for (const auto& [seq, request] : mRequests) { |
| 161 | epoll_event eventItem = createEpollEvent(request.getEpollEvents(), seq); |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 162 | |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 163 | int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, request.fd, &eventItem); |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 164 | if (epollResult < 0) { |
Elliott Hughes | 5b8ff09 | 2015-06-30 15:17:14 -0700 | [diff] [blame] | 165 | ALOGE("Error adding epoll events for fd %d while rebuilding epoll set: %s", |
| 166 | request.fd, strerror(errno)); |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void Looper::scheduleEpollRebuildLocked() { |
| 172 | if (!mEpollRebuildRequired) { |
| 173 | #if DEBUG_CALLBACKS |
| 174 | ALOGD("%p ~ scheduleEpollRebuildLocked - scheduling epoll set rebuild", this); |
| 175 | #endif |
| 176 | mEpollRebuildRequired = true; |
| 177 | wake(); |
| 178 | } |
| 179 | } |
| 180 | |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 181 | int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) { |
| 182 | int result = 0; |
| 183 | for (;;) { |
| 184 | while (mResponseIndex < mResponses.size()) { |
| 185 | const Response& response = mResponses.itemAt(mResponseIndex++); |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 186 | int ident = response.request.ident; |
| 187 | if (ident >= 0) { |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 188 | int fd = response.request.fd; |
| 189 | int events = response.events; |
| 190 | void* data = response.request.data; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 191 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 192 | ALOGD("%p ~ pollOnce - returning signalled identifier %d: " |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 193 | "fd=%d, events=0x%x, data=%p", |
| 194 | this, ident, fd, events, data); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 195 | #endif |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 196 | if (outFd != nullptr) *outFd = fd; |
| 197 | if (outEvents != nullptr) *outEvents = events; |
| 198 | if (outData != nullptr) *outData = data; |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 199 | return ident; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | if (result != 0) { |
| 204 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 205 | ALOGD("%p ~ pollOnce - returning result %d", this, result); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 206 | #endif |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 207 | if (outFd != nullptr) *outFd = 0; |
| 208 | if (outEvents != nullptr) *outEvents = 0; |
| 209 | if (outData != nullptr) *outData = nullptr; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 210 | return result; |
| 211 | } |
| 212 | |
| 213 | result = pollInner(timeoutMillis); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | int Looper::pollInner(int timeoutMillis) { |
| 218 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 219 | ALOGD("%p ~ pollOnce - waiting: timeoutMillis=%d", this, timeoutMillis); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 220 | #endif |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 221 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 222 | // Adjust the timeout based on when the next message is due. |
| 223 | if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX) { |
| 224 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 43550ee | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 225 | int messageTimeoutMillis = toMillisecondTimeoutDelay(now, mNextMessageUptime); |
| 226 | if (messageTimeoutMillis >= 0 |
| 227 | && (timeoutMillis < 0 || messageTimeoutMillis < timeoutMillis)) { |
| 228 | timeoutMillis = messageTimeoutMillis; |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 229 | } |
| 230 | #if DEBUG_POLL_AND_WAKE |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 231 | ALOGD("%p ~ pollOnce - next message in %" PRId64 "ns, adjusted timeout: timeoutMillis=%d", |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 232 | this, mNextMessageUptime - now, timeoutMillis); |
| 233 | #endif |
| 234 | } |
| 235 | |
| 236 | // Poll. |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 237 | int result = POLL_WAKE; |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 238 | mResponses.clear(); |
| 239 | mResponseIndex = 0; |
| 240 | |
Dianne Hackborn | 19159f9 | 2013-05-06 14:25:20 -0700 | [diff] [blame] | 241 | // We are about to idle. |
Jeff Brown | 27e5721 | 2015-02-26 14:16:30 -0800 | [diff] [blame] | 242 | mPolling = true; |
Dianne Hackborn | 19159f9 | 2013-05-06 14:25:20 -0700 | [diff] [blame] | 243 | |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 244 | struct epoll_event eventItems[EPOLL_MAX_EVENTS]; |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 245 | int eventCount = epoll_wait(mEpollFd.get(), eventItems, EPOLL_MAX_EVENTS, timeoutMillis); |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 246 | |
Dianne Hackborn | 19159f9 | 2013-05-06 14:25:20 -0700 | [diff] [blame] | 247 | // No longer idling. |
Jeff Brown | 27e5721 | 2015-02-26 14:16:30 -0800 | [diff] [blame] | 248 | mPolling = false; |
Dianne Hackborn | 19159f9 | 2013-05-06 14:25:20 -0700 | [diff] [blame] | 249 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 250 | // Acquire lock. |
| 251 | mLock.lock(); |
| 252 | |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 253 | // Rebuild epoll set if needed. |
| 254 | if (mEpollRebuildRequired) { |
| 255 | mEpollRebuildRequired = false; |
| 256 | rebuildEpollLocked(); |
| 257 | goto Done; |
| 258 | } |
| 259 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 260 | // Check for poll error. |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 261 | if (eventCount < 0) { |
Jeff Brown | 171bf9e | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 262 | if (errno == EINTR) { |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 263 | goto Done; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 264 | } |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 265 | ALOGW("Poll failed with an unexpected error: %s", strerror(errno)); |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 266 | result = POLL_ERROR; |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 267 | goto Done; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 270 | // Check for poll timeout. |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 271 | if (eventCount == 0) { |
| 272 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 273 | ALOGD("%p ~ pollOnce - timeout", this); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 274 | #endif |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 275 | result = POLL_TIMEOUT; |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 276 | goto Done; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 279 | // Handle all events. |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 280 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 281 | ALOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 282 | #endif |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 283 | |
Jeff Brown | 9da1810 | 2010-09-17 17:01:23 -0700 | [diff] [blame] | 284 | for (int i = 0; i < eventCount; i++) { |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 285 | const SequenceNumber seq = eventItems[i].data.u64; |
Jeff Brown | 9da1810 | 2010-09-17 17:01:23 -0700 | [diff] [blame] | 286 | uint32_t epollEvents = eventItems[i].events; |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 287 | if (seq == WAKE_EVENT_FD_SEQ) { |
Jeff Brown | 9da1810 | 2010-09-17 17:01:23 -0700 | [diff] [blame] | 288 | if (epollEvents & EPOLLIN) { |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 289 | awoken(); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 290 | } else { |
Tim Kilbourn | 8892ce6 | 2015-03-26 14:36:32 -0700 | [diff] [blame] | 291 | ALOGW("Ignoring unexpected epoll events 0x%x on wake event fd.", epollEvents); |
Jeff Brown | 9da1810 | 2010-09-17 17:01:23 -0700 | [diff] [blame] | 292 | } |
| 293 | } else { |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 294 | const auto& request_it = mRequests.find(seq); |
| 295 | if (request_it != mRequests.end()) { |
| 296 | const auto& request = request_it->second; |
Jeff Brown | 9da1810 | 2010-09-17 17:01:23 -0700 | [diff] [blame] | 297 | int events = 0; |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 298 | if (epollEvents & EPOLLIN) events |= EVENT_INPUT; |
| 299 | if (epollEvents & EPOLLOUT) events |= EVENT_OUTPUT; |
| 300 | if (epollEvents & EPOLLERR) events |= EVENT_ERROR; |
| 301 | if (epollEvents & EPOLLHUP) events |= EVENT_HANGUP; |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 302 | mResponses.push({.seq = seq, .events = events, .request = request}); |
Jeff Brown | 9da1810 | 2010-09-17 17:01:23 -0700 | [diff] [blame] | 303 | } else { |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 304 | ALOGW("Ignoring unexpected epoll events 0x%x for sequence number %" PRIu64 |
| 305 | " that is no longer registered.", |
| 306 | epollEvents, seq); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 310 | Done: ; |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 311 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 312 | // Invoke pending message callbacks. |
| 313 | mNextMessageUptime = LLONG_MAX; |
| 314 | while (mMessageEnvelopes.size() != 0) { |
| 315 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 316 | const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(0); |
| 317 | if (messageEnvelope.uptime <= now) { |
| 318 | // Remove the envelope from the list. |
| 319 | // We keep a strong reference to the handler until the call to handleMessage |
| 320 | // finishes. Then we drop it so that the handler can be deleted *before* |
| 321 | // we reacquire our lock. |
| 322 | { // obtain handler |
| 323 | sp<MessageHandler> handler = messageEnvelope.handler; |
| 324 | Message message = messageEnvelope.message; |
| 325 | mMessageEnvelopes.removeAt(0); |
| 326 | mSendingMessage = true; |
| 327 | mLock.unlock(); |
| 328 | |
| 329 | #if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 330 | ALOGD("%p ~ pollOnce - sending message: handler=%p, what=%d", |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 331 | this, handler.get(), message.what); |
| 332 | #endif |
| 333 | handler->handleMessage(message); |
| 334 | } // release handler |
| 335 | |
| 336 | mLock.lock(); |
| 337 | mSendingMessage = false; |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 338 | result = POLL_CALLBACK; |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 339 | } else { |
| 340 | // The last message left at the head of the queue determines the next wakeup time. |
| 341 | mNextMessageUptime = messageEnvelope.uptime; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Release lock. |
| 347 | mLock.unlock(); |
| 348 | |
| 349 | // Invoke all response callbacks. |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 350 | for (size_t i = 0; i < mResponses.size(); i++) { |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 351 | Response& response = mResponses.editItemAt(i); |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 352 | if (response.request.ident == POLL_CALLBACK) { |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 353 | int fd = response.request.fd; |
| 354 | int events = response.events; |
| 355 | void* data = response.request.data; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 356 | #if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 357 | ALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p", |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 358 | this, response.request.callback.get(), fd, events, data); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 359 | #endif |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 360 | // Invoke the callback. Note that the file descriptor may be closed by |
| 361 | // the callback (and potentially even reused) before the function returns so |
| 362 | // we need to be a little careful when removing the file descriptor afterwards. |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 363 | int callbackResult = response.request.callback->handleEvent(fd, events, data); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 364 | if (callbackResult == 0) { |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 365 | AutoMutex _l(mLock); |
| 366 | removeSequenceNumberLocked(response.seq); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 367 | } |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 368 | |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 369 | // Clear the callback reference in the response structure promptly because we |
| 370 | // will not clear the response vector itself until the next poll. |
| 371 | response.request.callback.clear(); |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 372 | result = POLL_CALLBACK; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | return result; |
| 376 | } |
| 377 | |
| 378 | int Looper::pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData) { |
| 379 | if (timeoutMillis <= 0) { |
| 380 | int result; |
| 381 | do { |
| 382 | result = pollOnce(timeoutMillis, outFd, outEvents, outData); |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 383 | } while (result == POLL_CALLBACK); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 384 | return result; |
| 385 | } else { |
| 386 | nsecs_t endTime = systemTime(SYSTEM_TIME_MONOTONIC) |
| 387 | + milliseconds_to_nanoseconds(timeoutMillis); |
| 388 | |
| 389 | for (;;) { |
| 390 | int result = pollOnce(timeoutMillis, outFd, outEvents, outData); |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 391 | if (result != POLL_CALLBACK) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 392 | return result; |
| 393 | } |
| 394 | |
Jeff Brown | 43550ee | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 395 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 396 | timeoutMillis = toMillisecondTimeoutDelay(now, endTime); |
| 397 | if (timeoutMillis == 0) { |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 398 | return POLL_TIMEOUT; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 399 | } |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void Looper::wake() { |
| 405 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 406 | ALOGD("%p ~ wake", this); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 407 | #endif |
| 408 | |
Tim Kilbourn | 8892ce6 | 2015-03-26 14:36:32 -0700 | [diff] [blame] | 409 | uint64_t inc = 1; |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 410 | ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd.get(), &inc, sizeof(uint64_t))); |
Tim Kilbourn | 8892ce6 | 2015-03-26 14:36:32 -0700 | [diff] [blame] | 411 | if (nWrite != sizeof(uint64_t)) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 412 | if (errno != EAGAIN) { |
Elliott Hughes | fd12160 | 2019-04-01 12:22:55 -0700 | [diff] [blame] | 413 | LOG_ALWAYS_FATAL("Could not write wake signal to fd %d (returned %zd): %s", |
| 414 | mWakeEventFd.get(), nWrite, strerror(errno)); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 419 | void Looper::awoken() { |
| 420 | #if DEBUG_POLL_AND_WAKE |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 421 | ALOGD("%p ~ awoken", this); |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 422 | #endif |
| 423 | |
Tim Kilbourn | 8892ce6 | 2015-03-26 14:36:32 -0700 | [diff] [blame] | 424 | uint64_t counter; |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 425 | TEMP_FAILURE_RETRY(read(mWakeEventFd.get(), &counter, sizeof(uint64_t))); |
Jeff Brown | 8d15c74 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 428 | int Looper::addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data) { |
Steven Moreland | a06e68c | 2021-04-27 00:09:23 +0000 | [diff] [blame] | 429 | sp<SimpleLooperCallback> looperCallback; |
| 430 | if (callback) { |
| 431 | looperCallback = sp<SimpleLooperCallback>::make(callback); |
| 432 | } |
| 433 | return addFd(fd, ident, events, looperCallback, data); |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, void* data) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 437 | #if DEBUG_CALLBACKS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 438 | ALOGD("%p ~ addFd - fd=%d, ident=%d, events=0x%x, callback=%p, data=%p", this, fd, ident, |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 439 | events, callback.get(), data); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 440 | #endif |
| 441 | |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 442 | if (!callback.get()) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 443 | if (! mAllowNonCallbacks) { |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 444 | ALOGE("Invalid attempt to set NULL callback but not allowed for this looper."); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | if (ident < 0) { |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 449 | ALOGE("Invalid attempt to set NULL callback with ident < 0."); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 450 | return -1; |
| 451 | } |
Jeff Brown | dd1b037 | 2012-05-31 16:15:35 -0700 | [diff] [blame] | 452 | } else { |
Brian Carlstrom | 1693d7e | 2013-12-11 22:46:45 -0800 | [diff] [blame] | 453 | ident = POLL_CALLBACK; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | { // acquire lock |
| 457 | AutoMutex _l(mLock); |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 458 | // There is a sequence number reserved for the WakeEventFd. |
| 459 | if (mNextRequestSeq == WAKE_EVENT_FD_SEQ) mNextRequestSeq++; |
| 460 | const SequenceNumber seq = mNextRequestSeq++; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 461 | |
| 462 | Request request; |
| 463 | request.fd = fd; |
| 464 | request.ident = ident; |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 465 | request.events = events; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 466 | request.callback = callback; |
| 467 | request.data = data; |
| 468 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 469 | epoll_event eventItem = createEpollEvent(request.getEpollEvents(), seq); |
| 470 | auto seq_it = mSequenceNumberByFd.find(fd); |
| 471 | if (seq_it == mSequenceNumberByFd.end()) { |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 472 | int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, fd, &eventItem); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 473 | if (epollResult < 0) { |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 474 | ALOGE("Error adding epoll events for fd %d: %s", fd, strerror(errno)); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 475 | return -1; |
| 476 | } |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 477 | mRequests.emplace(seq, request); |
| 478 | mSequenceNumberByFd.emplace(fd, seq); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 479 | } else { |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 480 | int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_MOD, fd, &eventItem); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 481 | if (epollResult < 0) { |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 482 | if (errno == ENOENT) { |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 483 | // Tolerate ENOENT because it means that an older file descriptor was |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 484 | // closed before its callback was unregistered and meanwhile a new |
| 485 | // file descriptor with the same number has been created and is now |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 486 | // being registered for the first time. This error may occur naturally |
| 487 | // when a callback has the side-effect of closing the file descriptor |
| 488 | // before returning and unregistering itself. Callback sequence number |
| 489 | // checks further ensure that the race is benign. |
| 490 | // |
| 491 | // Unfortunately due to kernel limitations we need to rebuild the epoll |
| 492 | // set from scratch because it may contain an old file handle that we are |
| 493 | // now unable to remove since its file descriptor is no longer valid. |
| 494 | // No such problem would have occurred if we were using the poll system |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 495 | // call instead, but that approach carries other disadvantages. |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 496 | #if DEBUG_CALLBACKS |
| 497 | ALOGD("%p ~ addFd - EPOLL_CTL_MOD failed due to file descriptor " |
Elliott Hughes | 5b8ff09 | 2015-06-30 15:17:14 -0700 | [diff] [blame] | 498 | "being recycled, falling back on EPOLL_CTL_ADD: %s", |
| 499 | this, strerror(errno)); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 500 | #endif |
Josh Gao | 2d08ae5 | 2018-07-18 17:26:24 -0700 | [diff] [blame] | 501 | epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, fd, &eventItem); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 502 | if (epollResult < 0) { |
Elliott Hughes | 5b8ff09 | 2015-06-30 15:17:14 -0700 | [diff] [blame] | 503 | ALOGE("Error modifying or adding epoll events for fd %d: %s", |
| 504 | fd, strerror(errno)); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 505 | return -1; |
| 506 | } |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 507 | scheduleEpollRebuildLocked(); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 508 | } else { |
Elliott Hughes | 5b8ff09 | 2015-06-30 15:17:14 -0700 | [diff] [blame] | 509 | ALOGE("Error modifying epoll events for fd %d: %s", fd, strerror(errno)); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 510 | return -1; |
| 511 | } |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 512 | } |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 513 | const SequenceNumber oldSeq = seq_it->second; |
| 514 | mRequests.erase(oldSeq); |
| 515 | mRequests.emplace(seq, request); |
| 516 | seq_it->second = seq; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 517 | } |
| 518 | } // release lock |
| 519 | return 1; |
| 520 | } |
| 521 | |
| 522 | int Looper::removeFd(int fd) { |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 523 | AutoMutex _l(mLock); |
| 524 | const auto& it = mSequenceNumberByFd.find(fd); |
| 525 | if (it == mSequenceNumberByFd.end()) { |
| 526 | return 0; |
| 527 | } |
| 528 | return removeSequenceNumberLocked(it->second); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 531 | int Looper::removeSequenceNumberLocked(SequenceNumber seq) { |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 532 | #if DEBUG_CALLBACKS |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 533 | ALOGD("%p ~ removeFd - fd=%d, seq=%u", this, fd, seq); |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 534 | #endif |
| 535 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 536 | const auto& request_it = mRequests.find(seq); |
| 537 | if (request_it == mRequests.end()) { |
| 538 | return 0; |
| 539 | } |
| 540 | const int fd = request_it->second.fd; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 541 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 542 | // Always remove the FD from the request map even if an error occurs while |
| 543 | // updating the epoll set so that we avoid accidentally leaking callbacks. |
| 544 | mRequests.erase(request_it); |
| 545 | mSequenceNumberByFd.erase(fd); |
| 546 | |
| 547 | int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_DEL, fd, nullptr); |
| 548 | if (epollResult < 0) { |
| 549 | if (errno == EBADF || errno == ENOENT) { |
| 550 | // Tolerate EBADF or ENOENT because it means that the file descriptor was closed |
| 551 | // before its callback was unregistered. This error may occur naturally when a |
| 552 | // callback has the side-effect of closing the file descriptor before returning and |
| 553 | // unregistering itself. |
| 554 | // |
| 555 | // Unfortunately due to kernel limitations we need to rebuild the epoll |
| 556 | // set from scratch because it may contain an old file handle that we are |
| 557 | // now unable to remove since its file descriptor is no longer valid. |
| 558 | // No such problem would have occurred if we were using the poll system |
| 559 | // call instead, but that approach carries other disadvantages. |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 560 | #if DEBUG_CALLBACKS |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 561 | ALOGD("%p ~ removeFd - EPOLL_CTL_DEL failed due to file descriptor " |
| 562 | "being closed: %s", |
| 563 | this, strerror(errno)); |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 564 | #endif |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 565 | scheduleEpollRebuildLocked(); |
| 566 | } else { |
| 567 | // Some other error occurred. This is really weird because it means |
| 568 | // our list of callbacks got out of sync with the epoll set somehow. |
| 569 | // We defensively rebuild the epoll set to avoid getting spurious |
| 570 | // notifications with nowhere to go. |
| 571 | ALOGE("Error removing epoll events for fd %d: %s", fd, strerror(errno)); |
| 572 | scheduleEpollRebuildLocked(); |
| 573 | return -1; |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 574 | } |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 575 | } |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 576 | return 1; |
| 577 | } |
| 578 | |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 579 | void Looper::sendMessage(const sp<MessageHandler>& handler, const Message& message) { |
Jeff Brown | aa13c1b | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 580 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 581 | sendMessageAtTime(now, handler, message); |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void Looper::sendMessageDelayed(nsecs_t uptimeDelay, const sp<MessageHandler>& handler, |
| 585 | const Message& message) { |
| 586 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 587 | sendMessageAtTime(now + uptimeDelay, handler, message); |
| 588 | } |
| 589 | |
| 590 | void Looper::sendMessageAtTime(nsecs_t uptime, const sp<MessageHandler>& handler, |
| 591 | const Message& message) { |
| 592 | #if DEBUG_CALLBACKS |
Jeff Brown | 7a0310e | 2015-03-10 18:31:12 -0700 | [diff] [blame] | 593 | ALOGD("%p ~ sendMessageAtTime - uptime=%" PRId64 ", handler=%p, what=%d", |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 594 | this, uptime, handler.get(), message.what); |
| 595 | #endif |
| 596 | |
| 597 | size_t i = 0; |
| 598 | { // acquire lock |
| 599 | AutoMutex _l(mLock); |
| 600 | |
| 601 | size_t messageCount = mMessageEnvelopes.size(); |
| 602 | while (i < messageCount && uptime >= mMessageEnvelopes.itemAt(i).uptime) { |
| 603 | i += 1; |
| 604 | } |
| 605 | |
| 606 | MessageEnvelope messageEnvelope(uptime, handler, message); |
| 607 | mMessageEnvelopes.insertAt(messageEnvelope, i, 1); |
| 608 | |
| 609 | // Optimization: If the Looper is currently sending a message, then we can skip |
| 610 | // the call to wake() because the next thing the Looper will do after processing |
| 611 | // messages is to decide when the next wakeup time should be. In fact, it does |
| 612 | // not even matter whether this code is running on the Looper thread. |
| 613 | if (mSendingMessage) { |
| 614 | return; |
| 615 | } |
| 616 | } // release lock |
| 617 | |
| 618 | // Wake the poll loop only when we enqueue a new message at the head. |
| 619 | if (i == 0) { |
| 620 | wake(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void Looper::removeMessages(const sp<MessageHandler>& handler) { |
| 625 | #if DEBUG_CALLBACKS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 626 | ALOGD("%p ~ removeMessages - handler=%p", this, handler.get()); |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 627 | #endif |
| 628 | |
| 629 | { // acquire lock |
| 630 | AutoMutex _l(mLock); |
| 631 | |
| 632 | for (size_t i = mMessageEnvelopes.size(); i != 0; ) { |
| 633 | const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(--i); |
| 634 | if (messageEnvelope.handler == handler) { |
| 635 | mMessageEnvelopes.removeAt(i); |
| 636 | } |
| 637 | } |
| 638 | } // release lock |
| 639 | } |
| 640 | |
| 641 | void Looper::removeMessages(const sp<MessageHandler>& handler, int what) { |
| 642 | #if DEBUG_CALLBACKS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 643 | ALOGD("%p ~ removeMessages - handler=%p, what=%d", this, handler.get(), what); |
Jeff Brown | 3e2e38b | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 644 | #endif |
| 645 | |
| 646 | { // acquire lock |
| 647 | AutoMutex _l(mLock); |
| 648 | |
| 649 | for (size_t i = mMessageEnvelopes.size(); i != 0; ) { |
| 650 | const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(--i); |
| 651 | if (messageEnvelope.handler == handler |
| 652 | && messageEnvelope.message.what == what) { |
| 653 | mMessageEnvelopes.removeAt(i); |
| 654 | } |
| 655 | } |
| 656 | } // release lock |
| 657 | } |
| 658 | |
Jeff Brown | 27e5721 | 2015-02-26 14:16:30 -0800 | [diff] [blame] | 659 | bool Looper::isPolling() const { |
| 660 | return mPolling; |
Dianne Hackborn | 19159f9 | 2013-05-06 14:25:20 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 663 | uint32_t Looper::Request::getEpollEvents() const { |
| 664 | uint32_t epollEvents = 0; |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 665 | if (events & EVENT_INPUT) epollEvents |= EPOLLIN; |
| 666 | if (events & EVENT_OUTPUT) epollEvents |= EPOLLOUT; |
Prabir Pradhan | 729057a | 2021-08-12 12:36:31 -0700 | [diff] [blame] | 667 | return epollEvents; |
Jeff Brown | e7d54f8 | 2015-03-12 19:32:39 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 670 | MessageHandler::~MessageHandler() { } |
| 671 | |
| 672 | LooperCallback::~LooperCallback() { } |
| 673 | |
Jeff Brown | 7901eb2 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 674 | } // namespace android |