diff options
author | Jeff Brown | 2010-09-17 19:01:23 -0500 |
---|---|---|
committer | Alex Ray | 2013-07-30 15:56:54 -0500 |
commit | 9da1810050d8825e51dabdd0262173432d49c16c (patch) | |
tree | 3eeaa9c346cecb27e4de8f5db596a8bf4a929f23 /libs/utils/Looper.cpp | |
parent | 905682a19609e862633f01e69bec58384df2cdf7 (diff) | |
download | platform-system-core-9da1810050d8825e51dabdd0262173432d49c16c.tar.gz platform-system-core-9da1810050d8825e51dabdd0262173432d49c16c.tar.xz platform-system-core-9da1810050d8825e51dabdd0262173432d49c16c.zip |
Reduce lock thrashing in native Looper.
In the common case, there is nothing interesting happening on the native
Looper besides occasional wake ups. There is no point grabbing the
semaphore then.
Change-Id: Ib5c426d0e158dfa37891b7ff5537b6f833592fad
Diffstat (limited to 'libs/utils/Looper.cpp')
-rw-r--r-- | libs/utils/Looper.cpp | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/libs/utils/Looper.cpp b/libs/utils/Looper.cpp index 4aa50d675..b46279efc 100644 --- a/libs/utils/Looper.cpp +++ b/libs/utils/Looper.cpp | |||
@@ -184,44 +184,50 @@ int Looper::pollInner(int timeoutMillis) { | |||
184 | #if DEBUG_POLL_AND_WAKE | 184 | #if DEBUG_POLL_AND_WAKE |
185 | LOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount); | 185 | LOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount); |
186 | #endif | 186 | #endif |
187 | { // acquire lock | 187 | bool acquiredLock = false; |
188 | AutoMutex _l(mLock); | 188 | for (int i = 0; i < eventCount; i++) { |
189 | for (int i = 0; i < eventCount; i++) { | 189 | int fd = eventItems[i].data.fd; |
190 | int fd = eventItems[i].data.fd; | 190 | uint32_t epollEvents = eventItems[i].events; |
191 | uint32_t epollEvents = eventItems[i].events; | 191 | if (fd == mWakeReadPipeFd) { |
192 | if (fd == mWakeReadPipeFd) { | 192 | if (epollEvents & EPOLLIN) { |
193 | if (epollEvents & EPOLLIN) { | ||
194 | #if DEBUG_POLL_AND_WAKE | 193 | #if DEBUG_POLL_AND_WAKE |
195 | LOGD("%p ~ pollOnce - awoken", this); | 194 | LOGD("%p ~ pollOnce - awoken", this); |
196 | #endif | 195 | #endif |
197 | char buffer[16]; | 196 | char buffer[16]; |
198 | ssize_t nRead; | 197 | ssize_t nRead; |
199 | do { | 198 | do { |
200 | nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); | 199 | nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); |
201 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); | 200 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); |
202 | } else { | 201 | } else { |
203 | LOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents); | 202 | LOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents); |
204 | } | 203 | } |
204 | } else { | ||
205 | if (! acquiredLock) { | ||
206 | mLock.lock(); | ||
207 | acquiredLock = true; | ||
208 | } | ||
209 | |||
210 | ssize_t requestIndex = mRequests.indexOfKey(fd); | ||
211 | if (requestIndex >= 0) { | ||
212 | int events = 0; | ||
213 | if (epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT; | ||
214 | if (epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT; | ||
215 | if (epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR; | ||
216 | if (epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP; | ||
217 | |||
218 | Response response; | ||
219 | response.events = events; | ||
220 | response.request = mRequests.valueAt(requestIndex); | ||
221 | mResponses.push(response); | ||
205 | } else { | 222 | } else { |
206 | ssize_t requestIndex = mRequests.indexOfKey(fd); | 223 | LOGW("Ignoring unexpected epoll events 0x%x on fd %d that is " |
207 | if (requestIndex >= 0) { | 224 | "no longer registered.", epollEvents, fd); |
208 | int events = 0; | ||
209 | if (epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT; | ||
210 | if (epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT; | ||
211 | if (epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR; | ||
212 | if (epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP; | ||
213 | |||
214 | Response response; | ||
215 | response.events = events; | ||
216 | response.request = mRequests.valueAt(requestIndex); | ||
217 | mResponses.push(response); | ||
218 | } else { | ||
219 | LOGW("Ignoring unexpected epoll events 0x%x on fd %d that is " | ||
220 | "no longer registered.", epollEvents, fd); | ||
221 | } | ||
222 | } | 225 | } |
223 | } | 226 | } |
224 | } | 227 | } |
228 | if (acquiredLock) { | ||
229 | mLock.unlock(); | ||
230 | } | ||
225 | 231 | ||
226 | for (size_t i = 0; i < mResponses.size(); i++) { | 232 | for (size_t i = 0; i < mResponses.size(); i++) { |
227 | const Response& response = mResponses.itemAt(i); | 233 | const Response& response = mResponses.itemAt(i); |