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