diff options
author | Jeff Brown <jeffbrown@google.com> | 2010-09-17 17:01:23 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2010-09-17 17:01:23 -0700 |
commit | 960498728904a1a6ea65171f0cfb3b41187c29ae (patch) | |
tree | f789343e9aeaf4af635cee25e965f188a46ee8ce /libs | |
parent | 838e93ed19879ce3d88b5be51630137d6a6999c6 (diff) | |
download | frameworks_base-960498728904a1a6ea65171f0cfb3b41187c29ae.zip frameworks_base-960498728904a1a6ea65171f0cfb3b41187c29ae.tar.gz frameworks_base-960498728904a1a6ea65171f0cfb3b41187c29ae.tar.bz2 |
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')
-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 4aa50d6..b46279e 100644 --- a/libs/utils/Looper.cpp +++ b/libs/utils/Looper.cpp @@ -184,44 +184,50 @@ int Looper::pollInner(int timeoutMillis) { #if DEBUG_POLL_AND_WAKE LOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount); #endif - { // acquire lock - AutoMutex _l(mLock); - for (int i = 0; i < eventCount; i++) { - int fd = eventItems[i].data.fd; - uint32_t epollEvents = eventItems[i].events; - if (fd == mWakeReadPipeFd) { - if (epollEvents & EPOLLIN) { + bool acquiredLock = false; + for (int i = 0; i < eventCount; i++) { + int fd = eventItems[i].data.fd; + uint32_t epollEvents = eventItems[i].events; + if (fd == mWakeReadPipeFd) { + if (epollEvents & EPOLLIN) { #if DEBUG_POLL_AND_WAKE - LOGD("%p ~ pollOnce - awoken", this); + LOGD("%p ~ pollOnce - awoken", this); #endif - char buffer[16]; - ssize_t nRead; - do { - nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); - } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); - } else { - LOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents); - } + char buffer[16]; + ssize_t nRead; + do { + nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); + } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); + } else { + LOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents); + } + } else { + if (! acquiredLock) { + mLock.lock(); + acquiredLock = true; + } + + ssize_t requestIndex = mRequests.indexOfKey(fd); + if (requestIndex >= 0) { + int events = 0; + if (epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT; + if (epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT; + if (epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR; + if (epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP; + + Response response; + response.events = events; + response.request = mRequests.valueAt(requestIndex); + mResponses.push(response); } else { - ssize_t requestIndex = mRequests.indexOfKey(fd); - if (requestIndex >= 0) { - int events = 0; - if (epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT; - if (epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT; - if (epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR; - if (epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP; - - Response response; - response.events = events; - response.request = mRequests.valueAt(requestIndex); - mResponses.push(response); - } else { - LOGW("Ignoring unexpected epoll events 0x%x on fd %d that is " - "no longer registered.", epollEvents, fd); - } + LOGW("Ignoring unexpected epoll events 0x%x on fd %d that is " + "no longer registered.", epollEvents, fd); } } } + if (acquiredLock) { + mLock.unlock(); + } for (size_t i = 0; i < mResponses.size(); i++) { const Response& response = mResponses.itemAt(i); |