上面分析的是Looper的prepare函数,prepare函数的作用是为当前线程设置个Looper对象,保存到线程的ThreadLocal中去。
当调用完Looper的prepare函数之后,还需要调用Looper的loop函数,才能使Handler来收取和处理消息:
loop函数的定义如下:
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
[// final MessageQueue queue = me.mQueue
上面这段代码是取到当前线程的Looper对象,并得到Looper对象的MessageQueue对象,保存到变量queue中。
]// final MessageQueue queue = me.mQueue
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
[// final long ident = Binder.clearCallingIdentity()
这两句不知道干什么的。
]// final long ident = Binder.clearCallingIdentity()
for (;;) {
[// for (;;)
在这里loop函数会陷入到一个死循环中去。
]// for (;;)
Message msg = queue.next(); // might block
[// Message msg = queue.next()
这里调用MessageQueue的next函数来得到下一个要处理的Message。
next函数的定义如下:
Message next() {
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
[// f (ptr == 0)
mPtr是0,表示该MessageQueue已经退出,则返回null。这样Looper也会退出。
]// f (ptr == 0)
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
[// int nextPollTimeoutMillis = 0
nextPollTimeoutMillis变量记录下一次需要取Message的时间点
]// int nextPollTimeoutMillis = 0
for (;;) {
[// for (;;)
这里next函数也会进入一个死循环中.这里为什么又要进入一个死循环呢?
MessageQueue是如何确定下一次需要等待的时间的呢?
最直观的方法是比较下一个消息和当前这个消息的时间差。
但是这里用到了一个巧妙的方法:
在每一次进入这个循环前,nextPollTimeoutMillis都为0. 这样进入nativePollOnce会马上返回,返回之后再用当前时间和队列头部的消息进行比较,就得到了下次要等待的时间。
]// for (;;)
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
[// if (nextPollTimeoutMillis != 0)
nextPollTimeoutMillis不为0,则表示距离下次取消息有时间间隔。调用flushPendingCommands是为了处理进程间的Binder通信,避免在这段等待时间里Binder线程阻塞。
]// if (nextPollTimeoutMillis != 0)
nativePollOnce(ptr, nextPollTimeoutMillis);
[// nativePollOnce(ptr, nextPollTimeoutMillis)
这里调用nativePollOnce函数,nativePollOnce函数的定义如下:
private native static void nativePollOnce(long ptr, int timeoutMillis);
nativePollOnce函数是个native函数,实际调用的是在android_os_MessageQueue.cpp中的函数android_os_MessageQueue_nativePollOnce:
static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jclass clazz, jlong ptr, jint timeoutMillis) {
NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
nativeMessageQueue->pollOnce(env, timeoutMillis);
[// nativeMessageQueue->pollOnce(env, timeoutMillis)
void NativeMessageQueue::pollOnce(JNIEnv* env, int timeoutMillis) {
mInCallback = true;
mLooper->pollOnce(timeoutMillis);
[// mLooper->pollOnce(timeoutMillis)
在NativeMessageQueue的pollOnce函数中,会调用C++层Looper的pollOnce函数。
inline int pollOnce(int timeoutMillis) {
return pollOnce(timeoutMillis, NULL, NULL, NULL);
}
pollOnce函数会调用一个重载的4个参数的pollOnce函数。定义如下:
int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
int result = 0;
for (;;) {
while (mResponseIndex < mResponses.size()) {
[// while (mResponseIndex < mResponses.size())
在C++层Looper的构造函数中,我们知道Looper利用epoll机制注册了两个文件描述符,一个用于监测读,一个用于监测写。
这里在这个while循环里,发现那个通道有数据传输。
Vector<Response> mResponses;
size_t mResponseIndex;
]// while (mResponseIndex < mResponses.size())
const Response& response = mResponses.itemAt(mResponseIndex++);
int ident = response.request.ident;
if (ident >= 0) {
int fd = response.request.fd;
int events = response.events;
void* data = response.request.data;
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - returning signalled identifier %d: "
"fd=%d, events=0x%x, data=%p",
this, ident, fd, events, data);
#endif
if (outFd != NULL) *outFd = fd;
if (outEvents != NULL) *outEvents = events;
if (outData != NULL) *outData = data;
return ident;
}
[// if (ident >= 0)
ident大于等于0,则表示需要返回
]// if (ident >= 0)
}
if (result != 0) {
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - returning result %d", this, result);
#endif
if (outFd != NULL) *outFd = 0;
if (outEvents != NULL) *outEvents = 0;
if (outData != NULL) *outData = NULL;
return result;
}
result = pollInner(timeoutMillis);
[// result = pollInner(timeoutMillis)
int Looper::pollInner(int timeoutMillis) {
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - waiting: timeoutMillis=%d", this, timeoutMillis);
#endif
// Adjust the timeout based on when the next message is due.
if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX) {
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
int messageTimeoutMillis = toMillisecondTimeoutDelay(now, mNextMessageUptime);
if (messageTimeoutMillis >= 0
&& (timeoutMillis < 0 || messageTimeoutMillis < timeoutMillis)) {
timeoutMillis = messageTimeoutMillis;
}
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - next message in %lldns, adjusted timeout: timeoutMillis=%d",
this, mNextMessageUptime - now, timeoutMillis);
#endif
}
[// if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX)
在这个if语句里计算epoll等待的时间
]// if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX)
// Poll.
int result = POLL_WAKE;
mResponses.clear();
mResponseIndex = 0;
// We are about to idle.
mIdling = true;
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
[// int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
这里调用epoll_wait来等待timeoutMillis时间. 注意这里timeoutMillis有可能为-1, 表示永久阻塞。
]// int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
// No longer idling.
mIdling = false;
// Acquire lock.
mLock.lock();
// Check for poll error.
if (eventCount < 0) {
if (errno == EINTR) {
goto Done;
}
ALOGW("Poll failed with an unexpected error, errno=%d", errno);
result = POLL_ERROR;
goto Done;
}
[// if (eventCount < 0)
这里处理从epoll_wait函数返回,但是有错误的情况
]// if (eventCount < 0)
// Check for poll timeout.
if (eventCount == 0) {
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - timeout", this);
#endif
result = POLL_TIMEOUT;
goto Done;
}
[// if (eventCount == 0)
这里处理从epoll_wait函数返回,超时的情况
]// if (eventCount == 0)
// Handle all events.
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount);
#endif
for (int i = 0; i < eventCount; i++) {
[// for (int i = 0; i < eventCount; i++)
这里处理epoll_wait函数返回,有数据读或写的情况
]// for (int i = 0; i < eventCount; i++)
int fd = eventItems[i].data.fd;
uint32_t epollEvents = eventItems[i].events;
if (fd == mWakeReadPipeFd) {
[// if (fd == mWakeReadPipeFd)
这里是读管道有数据
]// if (fd == mWakeReadPipeFd)
if (epollEvents & EPOLLIN) {
awoken();
[// awoken()
如果是读管道有数据,则说明在epoll_wait的过程中,有人向管道里写入了数据, 则这时候要调用awoken函数来唤醒。awoken函数的定义如下:
void Looper::awoken() {
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ awoken", this);
#endif
char buffer[16];
ssize_t nRead;
do {
nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
} while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
}
awoken函数只是将管道里的数读出来
]// awoken()
} else {
ALOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);
}
} else {
[// else
else分支是写管道有数据
]// else
ssize_t requestIndex = mRequests.indexOfKey(fd);
if (requestIndex >= 0) {
int events = 0;
if (epollEvents & EPOLLIN) events |= EVENT_INPUT;
if (epollEvents & EPOLLOUT) events |= EVENT_OUTPUT;
if (epollEvents & EPOLLERR) events |= EVENT_ERROR;
if (epollEvents & EPOLLHUP) events |= EVENT_HANGUP;
pushResponse(events, mRequests.valueAt(requestIndex));
[// pushResponse(events, mRequests.valueAt(requestIndex))
void Looper::pushResponse(int events, const Request& request) {
Response response;
response.events = events;
response.request = request;
mResponses.push(response);
}
]// pushResponse(events, mRequests.valueAt(requestIndex))
} else {
ALOGW("Ignoring unexpected epoll events 0x%x on fd %d that is "
"no longer registered.", epollEvents, fd);
}
}
}
Done: ;
// 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
ALOGD("%p ~ pollOnce - sending message: handler=%p, what=%d",
this, handler.get(), message.what);
#endif
handler->handleMessage(message);
} // release handler
mLock.lock();
mSendingMessage = false;
result = 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.
for (size_t i = 0; i < mResponses.size(); i++) {
Response& response = mResponses.editItemAt(i);
if (response.request.ident == POLL_CALLBACK) {
int fd = response.request.fd;
int events = response.events;
void* data = response.request.data;
#if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS
ALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p",
this, response.request.callback.get(), fd, events, data);
#endif
int callbackResult = response.request.callback->handleEvent(fd, events, data);
if (callbackResult == 0) {
removeFd(fd);
}
// 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();
result = POLL_CALLBACK;
}
}
return result;
}
]// result = pollInner(timeoutMillis)
}
}
]// mLooper->pollOnce(timeoutMillis)
mInCallback = false;
if (mExceptionObj) {
env->Throw(mExceptionObj);
env->DeleteLocalRef(mExceptionObj);
mExceptionObj = NULL;
}
}
]// nativeMessageQueue->pollOnce(env, timeoutMillis)
}
]// nativePollOnce(ptr, nextPollTimeoutMillis)
synchronized (this) {
[// synchronized (this)
程序运行到这里表示已经从nativePoolOnce返回,这时候表示需要处理一个消息了。
]// synchronized (this)
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
[// if (msg != null && msg.target == null)
这个if是在干什么,msg的target成员为null表示什么呢?注释中说是barrier,这尼玛是个什么东西?
从代码中看,是寻找下一个Asynchronous的消息,恩。找一个异步的消息去处理。
]// if (msg != null && msg.target == null)
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (false) Log.v("MessageQueue", "Returning message: " + msg);
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
[// if (msg != null)
上面这段代码就是从Message的队列中,返回一个Message对象。默认情况下,会返回队列的头。
]// if (msg != null)
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
[// dispose()
private void dispose() {
if (mPtr != 0) {
nativeDestroy(mPtr);
[// nativeDestroy(mPtr)
nativeDestroy函数是个native函数,实际会调用android_os_MessageQueue.cpp中的android_os_MessageQueue_nativeDestroy函数。
static void android_os_MessageQueue_nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
nativeMessageQueue->decStrong(env);
}
我们可以看见,android_os_MessageQueue_nativeDestroy函数只是简单的减少NativeMessageQueue的引用计数。
]// nativeDestroy(mPtr)
mPtr = 0;
}
}
]// dispose()
return null;
}
[// if (mQuitting)
如果mQuitting为true,则表示需要退出,返回null. 这样Looper的loop函数发现返回的是null则会退出死循环。
]// if (mQuitting)
// If first time idle, then get the number of idlers to run.
// Idle handles only run if the queue is empty or if the first message
// in the queue (possibly a barrier) is due to be handled in the future.
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf("MessageQueue", "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
]// Message msg = queue.next()
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
[// if (msg == null)
如果返回的消息是个null,则表示要停止该loop了,则要退出死循环。
]// if (msg == null)
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
[// msg.target.dispatchMessage(msg)
这句代码就是来分发处理消息的
msg的target成员指向一个Handler对象,这个Handler对象就是处理这个消息的。
我们来看一下Handler类的dispatchMessage函数的实现:
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
[// if (msg.callback != null)
Message类可以有一个Runnable类型的成员callback, 如果callback不为null,则首先执行该Runnable
]// if (msg.callback != null)
handleCallback(msg);
[// handleCallback(msg)
private static void handleCallback(Message message) {
message.callback.run();
}
]// handleCallback(msg)
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
[// if (mCallback != null)
在Handler类中也有一个类型为Callback的成员变量mCallback,如果mCallback不是null,则让mCallback处理这个消息。
]// if (mCallback != null)
handleMessage(msg);
[// handleMessage(msg)
最后调用handleMessage来处理Message,这个函数一般由子类重写。
]// handleMessage(msg)
}
}
]// msg.target.dispatchMessage(msg)
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
[// msg.recycleUnchecked()
当处理完这个消息之后,还要调用recycleUnchecked函数来将该消息加入到Message Cache中。
void recycleUnchecked() {
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
sendingUid = -1;
when = 0;
target = null;
callback = null;
data = null;
synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
[// synchronized (sPoolSync)
插入到Cache队列的头部
]// synchronized (sPoolSync)
}
recycleUnchecked函数就是将Message的各个字段清空成默认值。
]// msg.recycleUnchecked()
}
}
]// mLooper = Looper.myLooper()
if (mLooper == null) {
throw new RuntimeException("Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
[// public Handler(Callback callback, boolean async)
这个重载版本的Handler的构造函数没有传入Looper对象, 而成员变量mLooper是通过Looper.myLooper()来初始化的, 其实得到的Looper就是当前线程的Looper.
这个构造函数会被Handler的默认构造函数调用.
]// public Handler(Callback callback, boolean async)