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