2010-09-14 06:17:30 +00:00
|
|
|
//
|
|
|
|
// Copyright 2010 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// A looper implementation based on epoll().
|
|
|
|
//
|
|
|
|
#define LOG_TAG "Looper"
|
|
|
|
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
|
|
|
// Debugs poll and wake interactions.
|
|
|
|
#define DEBUG_POLL_AND_WAKE 0
|
|
|
|
|
|
|
|
// Debugs callback registration and invocation.
|
|
|
|
#define DEBUG_CALLBACKS 0
|
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include <utils/Looper.h>
|
|
|
|
#include <utils/Timers.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2011-03-02 22:41:58 +00:00
|
|
|
#include <limits.h>
|
2010-09-14 06:17:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// --- WeakMessageHandler ---
|
|
|
|
|
|
|
|
WeakMessageHandler::WeakMessageHandler(const wp<MessageHandler>& handler) :
|
|
|
|
mHandler(handler) {
|
|
|
|
}
|
|
|
|
|
2012-05-31 23:15:35 +00:00
|
|
|
WeakMessageHandler::~WeakMessageHandler() {
|
|
|
|
}
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
void WeakMessageHandler::handleMessage(const Message& message) {
|
|
|
|
sp<MessageHandler> handler = mHandler.promote();
|
|
|
|
if (handler != NULL) {
|
|
|
|
handler->handleMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-31 23:15:35 +00:00
|
|
|
// --- SimpleLooperCallback ---
|
|
|
|
|
|
|
|
SimpleLooperCallback::SimpleLooperCallback(ALooper_callbackFunc callback) :
|
|
|
|
mCallback(callback) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleLooperCallback::~SimpleLooperCallback() {
|
|
|
|
}
|
|
|
|
|
|
|
|
int SimpleLooperCallback::handleEvent(int fd, int events, void* data) {
|
|
|
|
return mCallback(fd, events, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// --- Looper ---
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
// Hint for number of file descriptors to be associated with the epoll instance.
|
|
|
|
static const int EPOLL_SIZE_HINT = 8;
|
|
|
|
|
|
|
|
// Maximum number of file descriptors for which to retrieve poll events each iteration.
|
|
|
|
static const int EPOLL_MAX_EVENTS = 16;
|
|
|
|
|
2010-09-21 22:11:18 +00:00
|
|
|
static pthread_once_t gTLSOnce = PTHREAD_ONCE_INIT;
|
|
|
|
static pthread_key_t gTLSKey = 0;
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
Looper::Looper(bool allowNonCallbacks) :
|
2011-03-02 22:41:58 +00:00
|
|
|
mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
|
|
|
|
mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
|
2010-09-14 06:17:30 +00:00
|
|
|
int wakeFds[2];
|
|
|
|
int result = pipe(wakeFds);
|
|
|
|
LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
|
|
|
|
|
|
|
|
mWakeReadPipeFd = wakeFds[0];
|
|
|
|
mWakeWritePipeFd = wakeFds[1];
|
|
|
|
|
|
|
|
result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
|
|
|
|
LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
|
|
|
|
errno);
|
|
|
|
|
|
|
|
result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
|
|
|
|
LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
|
|
|
|
errno);
|
|
|
|
|
2013-05-06 21:25:20 +00:00
|
|
|
mIdling = false;
|
|
|
|
|
2010-10-05 22:35:37 +00:00
|
|
|
// Allocate the epoll instance and register the wake pipe.
|
|
|
|
mEpollFd = epoll_create(EPOLL_SIZE_HINT);
|
|
|
|
LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
struct epoll_event eventItem;
|
2010-09-21 22:11:18 +00:00
|
|
|
memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
|
2010-09-14 06:17:30 +00:00
|
|
|
eventItem.events = EPOLLIN;
|
|
|
|
eventItem.data.fd = mWakeReadPipeFd;
|
|
|
|
result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);
|
|
|
|
LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
|
|
|
|
errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
Looper::~Looper() {
|
|
|
|
close(mWakeReadPipeFd);
|
|
|
|
close(mWakeWritePipeFd);
|
|
|
|
close(mEpollFd);
|
|
|
|
}
|
|
|
|
|
2010-09-21 22:11:18 +00:00
|
|
|
void Looper::initTLSKey() {
|
|
|
|
int result = pthread_key_create(& gTLSKey, threadDestructor);
|
|
|
|
LOG_ALWAYS_FATAL_IF(result != 0, "Could not allocate TLS key.");
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
void Looper::threadDestructor(void *st) {
|
|
|
|
Looper* const self = static_cast<Looper*>(st);
|
|
|
|
if (self != NULL) {
|
|
|
|
self->decStrong((void*)threadDestructor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::setForThread(const sp<Looper>& looper) {
|
|
|
|
sp<Looper> old = getForThread(); // also has side-effect of initializing TLS
|
|
|
|
|
|
|
|
if (looper != NULL) {
|
|
|
|
looper->incStrong((void*)threadDestructor);
|
|
|
|
}
|
|
|
|
|
2010-09-21 22:11:18 +00:00
|
|
|
pthread_setspecific(gTLSKey, looper.get());
|
2010-09-14 06:17:30 +00:00
|
|
|
|
|
|
|
if (old != NULL) {
|
|
|
|
old->decStrong((void*)threadDestructor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<Looper> Looper::getForThread() {
|
2010-09-21 22:11:18 +00:00
|
|
|
int result = pthread_once(& gTLSOnce, initTLSKey);
|
|
|
|
LOG_ALWAYS_FATAL_IF(result != 0, "pthread_once failed");
|
2010-09-14 06:17:30 +00:00
|
|
|
|
2010-09-21 22:11:18 +00:00
|
|
|
return (Looper*)pthread_getspecific(gTLSKey);
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sp<Looper> Looper::prepare(int opts) {
|
|
|
|
bool allowNonCallbacks = opts & ALOOPER_PREPARE_ALLOW_NON_CALLBACKS;
|
|
|
|
sp<Looper> looper = Looper::getForThread();
|
|
|
|
if (looper == NULL) {
|
|
|
|
looper = new Looper(allowNonCallbacks);
|
|
|
|
Looper::setForThread(looper);
|
|
|
|
}
|
|
|
|
if (looper->getAllowNonCallbacks() != allowNonCallbacks) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Looper already prepared for this thread with a different value for the "
|
2010-09-14 06:17:30 +00:00
|
|
|
"ALOOPER_PREPARE_ALLOW_NON_CALLBACKS option.");
|
|
|
|
}
|
|
|
|
return looper;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Looper::getAllowNonCallbacks() const {
|
|
|
|
return mAllowNonCallbacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
|
|
|
|
int result = 0;
|
|
|
|
for (;;) {
|
|
|
|
while (mResponseIndex < mResponses.size()) {
|
|
|
|
const Response& response = mResponses.itemAt(mResponseIndex++);
|
2012-05-31 23:15:35 +00:00
|
|
|
int ident = response.request.ident;
|
|
|
|
if (ident >= 0) {
|
2011-03-02 22:41:58 +00:00
|
|
|
int fd = response.request.fd;
|
|
|
|
int events = response.events;
|
|
|
|
void* data = response.request.data;
|
2010-09-14 06:17:30 +00:00
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - returning signalled identifier %d: "
|
2011-03-02 22:41:58 +00:00
|
|
|
"fd=%d, events=0x%x, data=%p",
|
|
|
|
this, ident, fd, events, data);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
2011-03-02 22:41:58 +00:00
|
|
|
if (outFd != NULL) *outFd = fd;
|
|
|
|
if (outEvents != NULL) *outEvents = events;
|
|
|
|
if (outData != NULL) *outData = data;
|
|
|
|
return ident;
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != 0) {
|
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - returning result %d", this, result);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
|
|
|
if (outFd != NULL) *outFd = 0;
|
2012-05-31 23:15:35 +00:00
|
|
|
if (outEvents != NULL) *outEvents = 0;
|
2010-09-14 06:17:30 +00:00
|
|
|
if (outData != NULL) *outData = NULL;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = pollInner(timeoutMillis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Looper::pollInner(int timeoutMillis) {
|
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - waiting: timeoutMillis=%d", this, timeoutMillis);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
2010-10-05 22:35:37 +00:00
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// Adjust the timeout based on when the next message is due.
|
|
|
|
if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX) {
|
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
2011-03-17 08:34:19 +00:00
|
|
|
int messageTimeoutMillis = toMillisecondTimeoutDelay(now, mNextMessageUptime);
|
|
|
|
if (messageTimeoutMillis >= 0
|
|
|
|
&& (timeoutMillis < 0 || messageTimeoutMillis < timeoutMillis)) {
|
|
|
|
timeoutMillis = messageTimeoutMillis;
|
2011-03-02 22:41:58 +00:00
|
|
|
}
|
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - next message in %lldns, adjusted timeout: timeoutMillis=%d",
|
2011-03-02 22:41:58 +00:00
|
|
|
this, mNextMessageUptime - now, timeoutMillis);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll.
|
2010-10-05 22:35:37 +00:00
|
|
|
int result = ALOOPER_POLL_WAKE;
|
|
|
|
mResponses.clear();
|
|
|
|
mResponseIndex = 0;
|
|
|
|
|
2013-05-06 21:25:20 +00:00
|
|
|
// We are about to idle.
|
|
|
|
mIdling = true;
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
|
|
|
|
int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
|
2010-10-05 22:35:37 +00:00
|
|
|
|
2013-05-06 21:25:20 +00:00
|
|
|
// No longer idling.
|
|
|
|
mIdling = false;
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// Acquire lock.
|
|
|
|
mLock.lock();
|
|
|
|
|
|
|
|
// Check for poll error.
|
2010-09-14 06:17:30 +00:00
|
|
|
if (eventCount < 0) {
|
2010-09-17 00:04:52 +00:00
|
|
|
if (errno == EINTR) {
|
2010-10-05 22:35:37 +00:00
|
|
|
goto Done;
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Poll failed with an unexpected error, errno=%d", errno);
|
2010-10-05 22:35:37 +00:00
|
|
|
result = ALOOPER_POLL_ERROR;
|
|
|
|
goto Done;
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// Check for poll timeout.
|
2010-09-14 06:17:30 +00:00
|
|
|
if (eventCount == 0) {
|
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - timeout", this);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
2010-10-05 22:35:37 +00:00
|
|
|
result = ALOOPER_POLL_TIMEOUT;
|
|
|
|
goto Done;
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// Handle all events.
|
2010-09-14 06:17:30 +00:00
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
2010-10-05 22:35:37 +00:00
|
|
|
|
2010-09-18 00:01:23 +00:00
|
|
|
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) {
|
2010-10-05 22:35:37 +00:00
|
|
|
awoken();
|
2010-09-14 06:17:30 +00:00
|
|
|
} else {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);
|
2010-09-18 00:01:23 +00:00
|
|
|
}
|
|
|
|
} 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;
|
2010-10-05 22:35:37 +00:00
|
|
|
pushResponse(events, mRequests.valueAt(requestIndex));
|
2010-09-18 00:01:23 +00:00
|
|
|
} else {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Ignoring unexpected epoll events 0x%x on fd %d that is "
|
2010-09-18 00:01:23 +00:00
|
|
|
"no longer registered.", epollEvents, fd);
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-05 22:35:37 +00:00
|
|
|
Done: ;
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
// Invoke pending message callbacks.
|
|
|
|
mNextMessageUptime = LLONG_MAX;
|
|
|
|
while (mMessageEnvelopes.size() != 0) {
|
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(0);
|
|
|
|
if (messageEnvelope.uptime <= now) {
|
|
|
|
// Remove the envelope from the list.
|
|
|
|
// We keep a strong reference to the handler until the call to handleMessage
|
|
|
|
// finishes. Then we drop it so that the handler can be deleted *before*
|
|
|
|
// we reacquire our lock.
|
|
|
|
{ // obtain handler
|
|
|
|
sp<MessageHandler> handler = messageEnvelope.handler;
|
|
|
|
Message message = messageEnvelope.message;
|
|
|
|
mMessageEnvelopes.removeAt(0);
|
|
|
|
mSendingMessage = true;
|
|
|
|
mLock.unlock();
|
|
|
|
|
|
|
|
#if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - sending message: handler=%p, what=%d",
|
2011-03-02 22:41:58 +00:00
|
|
|
this, handler.get(), message.what);
|
|
|
|
#endif
|
|
|
|
handler->handleMessage(message);
|
|
|
|
} // release handler
|
|
|
|
|
|
|
|
mLock.lock();
|
|
|
|
mSendingMessage = false;
|
|
|
|
result = ALOOPER_POLL_CALLBACK;
|
|
|
|
} else {
|
|
|
|
// The last message left at the head of the queue determines the next wakeup time.
|
|
|
|
mNextMessageUptime = messageEnvelope.uptime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release lock.
|
|
|
|
mLock.unlock();
|
|
|
|
|
|
|
|
// Invoke all response callbacks.
|
2010-09-14 06:17:30 +00:00
|
|
|
for (size_t i = 0; i < mResponses.size(); i++) {
|
2012-05-31 23:15:35 +00:00
|
|
|
Response& response = mResponses.editItemAt(i);
|
|
|
|
if (response.request.ident == ALOOPER_POLL_CALLBACK) {
|
2011-03-02 22:41:58 +00:00
|
|
|
int fd = response.request.fd;
|
|
|
|
int events = response.events;
|
|
|
|
void* data = response.request.data;
|
2010-09-14 06:17:30 +00:00
|
|
|
#if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p",
|
2012-05-31 23:15:35 +00:00
|
|
|
this, response.request.callback.get(), fd, events, data);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
2012-05-31 23:15:35 +00:00
|
|
|
int callbackResult = response.request.callback->handleEvent(fd, events, data);
|
2010-09-14 06:17:30 +00:00
|
|
|
if (callbackResult == 0) {
|
2011-03-02 22:41:58 +00:00
|
|
|
removeFd(fd);
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
2012-05-31 23:15:35 +00:00
|
|
|
// Clear the callback reference in the response structure promptly because we
|
|
|
|
// will not clear the response vector itself until the next poll.
|
|
|
|
response.request.callback.clear();
|
2010-09-14 06:17:30 +00:00
|
|
|
result = ALOOPER_POLL_CALLBACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Looper::pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
|
|
|
|
if (timeoutMillis <= 0) {
|
|
|
|
int result;
|
|
|
|
do {
|
|
|
|
result = pollOnce(timeoutMillis, outFd, outEvents, outData);
|
|
|
|
} while (result == ALOOPER_POLL_CALLBACK);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
nsecs_t endTime = systemTime(SYSTEM_TIME_MONOTONIC)
|
|
|
|
+ milliseconds_to_nanoseconds(timeoutMillis);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int result = pollOnce(timeoutMillis, outFd, outEvents, outData);
|
|
|
|
if (result != ALOOPER_POLL_CALLBACK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-17 08:34:19 +00:00
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
timeoutMillis = toMillisecondTimeoutDelay(now, endTime);
|
|
|
|
if (timeoutMillis == 0) {
|
2010-09-14 06:17:30 +00:00
|
|
|
return ALOOPER_POLL_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::wake() {
|
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ wake", this);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
|
|
|
|
2010-09-17 00:04:52 +00:00
|
|
|
ssize_t nWrite;
|
|
|
|
do {
|
|
|
|
nWrite = write(mWakeWritePipeFd, "W", 1);
|
|
|
|
} while (nWrite == -1 && errno == EINTR);
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
if (nWrite != 1) {
|
|
|
|
if (errno != EAGAIN) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Could not write wake signal, errno=%d", errno);
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-05 22:35:37 +00:00
|
|
|
void Looper::awoken() {
|
|
|
|
#if DEBUG_POLL_AND_WAKE
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ awoken", this);
|
2010-10-05 22:35:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
char buffer[16];
|
|
|
|
ssize_t nRead;
|
|
|
|
do {
|
|
|
|
nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
|
|
|
|
} while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::pushResponse(int events, const Request& request) {
|
|
|
|
Response response;
|
|
|
|
response.events = events;
|
|
|
|
response.request = request;
|
|
|
|
mResponses.push(response);
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
int Looper::addFd(int fd, int ident, int events, ALooper_callbackFunc callback, void* data) {
|
2012-05-31 23:15:35 +00:00
|
|
|
return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : NULL, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, void* data) {
|
2010-09-14 06:17:30 +00:00
|
|
|
#if DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ addFd - fd=%d, ident=%d, events=0x%x, callback=%p, data=%p", this, fd, ident,
|
2012-05-31 23:15:35 +00:00
|
|
|
events, callback.get(), data);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
|
|
|
|
2012-05-31 23:15:35 +00:00
|
|
|
if (!callback.get()) {
|
2010-09-14 06:17:30 +00:00
|
|
|
if (! mAllowNonCallbacks) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Invalid attempt to set NULL callback but not allowed for this looper.");
|
2010-09-14 06:17:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ident < 0) {
|
2012-05-31 23:15:35 +00:00
|
|
|
ALOGE("Invalid attempt to set NULL callback with ident < 0.");
|
2010-09-14 06:17:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-31 23:15:35 +00:00
|
|
|
} else {
|
|
|
|
ident = ALOOPER_POLL_CALLBACK;
|
2010-09-14 06:17:30 +00:00
|
|
|
}
|
|
|
|
|
2010-10-05 22:35:37 +00:00
|
|
|
int epollEvents = 0;
|
|
|
|
if (events & ALOOPER_EVENT_INPUT) epollEvents |= EPOLLIN;
|
|
|
|
if (events & ALOOPER_EVENT_OUTPUT) epollEvents |= EPOLLOUT;
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
{ // acquire lock
|
|
|
|
AutoMutex _l(mLock);
|
|
|
|
|
|
|
|
Request request;
|
|
|
|
request.fd = fd;
|
|
|
|
request.ident = ident;
|
|
|
|
request.callback = callback;
|
|
|
|
request.data = data;
|
|
|
|
|
|
|
|
struct epoll_event eventItem;
|
2010-09-21 22:11:18 +00:00
|
|
|
memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
|
2010-09-14 06:17:30 +00:00
|
|
|
eventItem.events = epollEvents;
|
|
|
|
eventItem.data.fd = fd;
|
|
|
|
|
|
|
|
ssize_t requestIndex = mRequests.indexOfKey(fd);
|
|
|
|
if (requestIndex < 0) {
|
|
|
|
int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
|
|
|
|
if (epollResult < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Error adding epoll events for fd %d, errno=%d", fd, errno);
|
2010-09-14 06:17:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mRequests.add(fd, request);
|
|
|
|
} else {
|
|
|
|
int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_MOD, fd, & eventItem);
|
|
|
|
if (epollResult < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Error modifying epoll events for fd %d, errno=%d", fd, errno);
|
2010-09-14 06:17:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mRequests.replaceValueAt(requestIndex, request);
|
|
|
|
}
|
|
|
|
} // release lock
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Looper::removeFd(int fd) {
|
|
|
|
#if DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ removeFd - fd=%d", this, fd);
|
2010-09-14 06:17:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{ // acquire lock
|
|
|
|
AutoMutex _l(mLock);
|
|
|
|
ssize_t requestIndex = mRequests.indexOfKey(fd);
|
|
|
|
if (requestIndex < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
|
|
|
|
if (epollResult < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Error removing epoll events for fd %d, errno=%d", fd, errno);
|
2010-09-14 06:17:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequests.removeItemsAt(requestIndex);
|
2010-10-05 22:35:37 +00:00
|
|
|
} // release lock
|
2010-09-14 06:17:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-02 22:41:58 +00:00
|
|
|
void Looper::sendMessage(const sp<MessageHandler>& handler, const Message& message) {
|
2011-04-13 05:39:53 +00:00
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
sendMessageAtTime(now, handler, message);
|
2011-03-02 22:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::sendMessageDelayed(nsecs_t uptimeDelay, const sp<MessageHandler>& handler,
|
|
|
|
const Message& message) {
|
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
sendMessageAtTime(now + uptimeDelay, handler, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::sendMessageAtTime(nsecs_t uptime, const sp<MessageHandler>& handler,
|
|
|
|
const Message& message) {
|
|
|
|
#if DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ sendMessageAtTime - uptime=%lld, handler=%p, what=%d",
|
2011-03-02 22:41:58 +00:00
|
|
|
this, uptime, handler.get(), message.what);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
{ // acquire lock
|
|
|
|
AutoMutex _l(mLock);
|
|
|
|
|
|
|
|
size_t messageCount = mMessageEnvelopes.size();
|
|
|
|
while (i < messageCount && uptime >= mMessageEnvelopes.itemAt(i).uptime) {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageEnvelope messageEnvelope(uptime, handler, message);
|
|
|
|
mMessageEnvelopes.insertAt(messageEnvelope, i, 1);
|
|
|
|
|
|
|
|
// Optimization: If the Looper is currently sending a message, then we can skip
|
|
|
|
// the call to wake() because the next thing the Looper will do after processing
|
|
|
|
// messages is to decide when the next wakeup time should be. In fact, it does
|
|
|
|
// not even matter whether this code is running on the Looper thread.
|
|
|
|
if (mSendingMessage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // release lock
|
|
|
|
|
|
|
|
// Wake the poll loop only when we enqueue a new message at the head.
|
|
|
|
if (i == 0) {
|
|
|
|
wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::removeMessages(const sp<MessageHandler>& handler) {
|
|
|
|
#if DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ removeMessages - handler=%p", this, handler.get());
|
2011-03-02 22:41:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{ // acquire lock
|
|
|
|
AutoMutex _l(mLock);
|
|
|
|
|
|
|
|
for (size_t i = mMessageEnvelopes.size(); i != 0; ) {
|
|
|
|
const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(--i);
|
|
|
|
if (messageEnvelope.handler == handler) {
|
|
|
|
mMessageEnvelopes.removeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // release lock
|
|
|
|
}
|
|
|
|
|
|
|
|
void Looper::removeMessages(const sp<MessageHandler>& handler, int what) {
|
|
|
|
#if DEBUG_CALLBACKS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%p ~ removeMessages - handler=%p, what=%d", this, handler.get(), what);
|
2011-03-02 22:41:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{ // acquire lock
|
|
|
|
AutoMutex _l(mLock);
|
|
|
|
|
|
|
|
for (size_t i = mMessageEnvelopes.size(); i != 0; ) {
|
|
|
|
const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(--i);
|
|
|
|
if (messageEnvelope.handler == handler
|
|
|
|
&& messageEnvelope.message.what == what) {
|
|
|
|
mMessageEnvelopes.removeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // release lock
|
|
|
|
}
|
|
|
|
|
2013-05-06 21:25:20 +00:00
|
|
|
bool Looper::isIdling() const {
|
|
|
|
return mIdling;
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
} // namespace android
|