底層原理說明:
例如有ThreadA和ThreadB等2個(gè)子線程,如果ThreadA作為接收端,ThreadB是發(fā)送端。
在Linux系統(tǒng)中,管道是一種非常重要的通信手段方式,它有2個(gè)端,一個(gè)是讀的端,一個(gè)是寫的端口,一個(gè)進(jìn)程向管道的寫端寫入數(shù)據(jù),另一個(gè)進(jìn)程就可以在管道的讀端進(jìn)行讀取數(shù)據(jù)。handler的底層就是使用了管道作為通信的方式,接收方就是管道的讀端,發(fā)送方就對應(yīng)管道的寫端,管道作為中間通知的媒介。管道和Epoll結(jié)合充當(dāng)?shù)却蛦拘训墓ぞ摺?/p>
1、接收端代碼解讀
接收端需要執(zhí)行以下三步驟
Looper.prepare()--->定義mHandler--->Looper.loop()
1)Looper.prepare()
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
sThreadLocal是專屬線程的緩存區(qū)域,這里新建了一個(gè)Looper對象,并保存在當(dāng)前線程的sThreadLocal中。
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
Looper的構(gòu)造函數(shù)中,定義了MessageQueue對象
MessageQueue(boolean quitAllowed) {
mQuitAllowed = quitAllowed;
mPtr = nativeInit();
}
nativeInit()方法是一個(gè)netive方法,在android_os_MessageQueue.cpp中實(shí)現(xiàn)的
static jlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) {
NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue();
if (!nativeMessageQueue) {
jniThrowRuntimeException(env, "Unable to allocate native queue");
return 0;
}
nativeMessageQueue->incStrong(env);
return reinterpret_cast<jlong>(nativeMessageQueue);
}
這里定義了一個(gè)NativeMessageQueue對象,這是MessageQueue在c++層對應(yīng)的消息隊(duì)列,計(jì)數(shù)+1,并將NativeMessageQueue對象的地址返回給MessageQueue,賦值給MessageQueue的成員變量mPtr。繼續(xù)看NativeMessageQueue構(gòu)造方法:
NativeMessageQueue::NativeMessageQueue() : mInCallback(false), mExceptionObj(NULL) {
//在內(nèi)部創(chuàng)建了一個(gè)Looper對象
mLooper = Looper::getForThread();
if (mLooper == NULL) {
mLooper = new Looper(false);
Looper::setForThread(mLooper);
}
}
這里首先查詢當(dāng)前線程中是否存有mLooper 對象,這里和java層中的Looper保存在sThreadLocal有同樣效果。在這里開始會(huì)創(chuàng)建一個(gè)Looper對象,并將結(jié)果保存給當(dāng)前的線程中。c層中的Looper的構(gòu)造器:
Looper::Looper(bool allowNonCallbacks) :
mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
int wakeFds[2];
int result = pipe(wakeFds);
mWakeReadPipeFd = wakeFds[0];
mWakeWritePipeFd = wakeFds[1];
result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
mIdling = false;
// Allocate the epoll instance and register the wake pipe.
//來創(chuàng)建一個(gè)epoll專用的文件描述符
mEpollFd = epoll_create(EPOLL_SIZE_HINT);
struct epoll_event eventItem;
memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
//監(jiān)控mWakeReadPipeFd文件描述符的EPOLLIN事件,即當(dāng)管道中有內(nèi)容可讀時(shí),就喚醒當(dāng)前正在等待管道中的內(nèi)容的線程
eventItem.events = EPOLLIN;
eventItem.data.fd = mWakeReadPipeFd;
//告訴epoll要監(jiān)控相應(yīng)的文件描述符的什么事件
result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);
}
這里就引出了最終的線程通信的boss(管道),首先新建一個(gè)無名管道,wakeFds表示讀寫2端,接著調(diào)用fcntl設(shè)置管道讀寫端都為非阻塞I/O操作,后面接著調(diào)用epoll定義事件等,epoll主要處理等待和喚醒工作。
小結(jié):在Looper.prepare()中,定義了一個(gè)Looper對象,并將Looper對象保存在ThreadLocal中,接著定義了MessageQueue對象,MessageQueue又引入了c層的NativeMessageQueue對象,將地址保存在java層的MessageQueue中,同時(shí)也對應(yīng)的在c層啟用了一個(gè)Looper對象,創(chuàng)建了管道和Epoll作為通信的媒介。
2)定義Handler
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
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;
}
Looper.myLooper()方法就是從sThreadLocal中取出Looper.prepare()中定義的looper對象。
3)Looper.loop()的執(zhí)行
public static void loop() {
final Looper me = myLooper();
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();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
msg.target.dispatchMessage(msg);
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
msg.recycleUnchecked();
}
}
進(jìn)入for的無限循環(huán)中,從MessageQueue中取出Message對象,如果沒有了消息就返回了,跳出循環(huán)結(jié)束了。取出Message對象就會(huì)調(diào)用 msg.target.dispatchMessage(msg);進(jìn)行消息的分發(fā)。msg.target是一個(gè)Handler對象,由發(fā)送方?jīng)Q定的這個(gè)處理消息的工具。這里的重點(diǎn)方法是
queue.next();該方法如下:
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
//表示如果當(dāng)前消息隊(duì)列中沒有消息,它要等待的時(shí)候
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
//看看當(dāng)前消息隊(duì)列中有沒有消息
nativePollOnce(ptr, nextPollTimeoutMillis);
......
}
因?yàn)橹骶€程和子線程共用一個(gè)Handler,所以也是共用的MessageQueue對象,MessageQueue中的mMessages也是共享的資源,mMessages表示消息隊(duì)列中第一個(gè)消息。
進(jìn)入了一個(gè)for的無限循環(huán),首先執(zhí)行了 nativePollOnce(ptr, nextPollTimeoutMillis);這個(gè)方法。
static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jclass clazz,
jlong ptr, jint timeoutMillis) {
NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
nativeMessageQueue->pollOnce(env, timeoutMillis);
}
void NativeMessageQueue::pollOnce(JNIEnv* env, int timeoutMillis) {
mInCallback = true;
mLooper->pollOnce(timeoutMillis);
mInCallback = false;
if (mExceptionObj) {
env->Throw(mExceptionObj);
env->DeleteLocalRef(mExceptionObj);
mExceptionObj = NULL;
}
}
inline int pollOnce(int timeoutMillis) {
return pollOnce(timeoutMillis, NULL, NULL, NULL);
}
上面代碼都是在c層執(zhí)行的,調(diào)用關(guān)系也比較簡單,一路執(zhí)行,最后調(diào)用了Looper.cpp中的pollOnce方法。
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++);
int ident = response.request.ident;
if (ident >= 0) {
int fd = response.request.fd;
int events = response.events;
void* data = response.request.data;
if (outFd != NULL) *outFd = fd;
if (outEvents != NULL) *outEvents = events;
if (outData != NULL) *outData = data;
return ident;
}
}
if (result != 0) {
if (outFd != NULL) *outFd = 0;
if (outEvents != NULL) *outEvents = 0;
if (outData != NULL) *outData = NULL;
return result;
}
result = pollInner(timeoutMillis);
}
}
這里是一個(gè)for的無限循環(huán),第一次進(jìn)來mResponses是空的,mResponses是一個(gè)數(shù)組,存儲(chǔ)的是Response的值,在for循環(huán)中調(diào)用pollInner(timeoutMillis);pollInner方法代碼比較長
int Looper::pollInner(int timeoutMillis) {
// 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;
}
}
// Poll.
int result = POLL_WAKE;
mResponses.clear();
mResponseIndex = 0;
// We are about to idle.
mIdling = true;
//定義一個(gè)事件數(shù)組
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
//監(jiān)控的文件描述符是否有IO事件發(fā)生
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) { //出現(xiàn)錯(cuò)誤
if (errno == EINTR) {
goto Done;
}
ALOGW("Poll failed with an unexpected error, errno=%d", errno);
result = POLL_ERROR;
goto Done;
}
// Check for poll timeout.
if (eventCount == 0) { //表示超時(shí)了
result = POLL_TIMEOUT;
goto Done;
}
// Handle all events.
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) {
//在mWakeReadPipeFd文件描述符上發(fā)生了EPOLLIN就說明應(yīng)用程序中的消息隊(duì)列里面有新的消息需要處理了
awoken();
} else {
ALOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);
}
} 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));
} 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();
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;
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;
}
首先重新定義了消息處理的時(shí)間,根據(jù)時(shí)間定義了一個(gè)事件存儲(chǔ)器,調(diào)用epoll_wait方法取出事件數(shù)量,epoll_wait函數(shù)中最后一個(gè)timeout參數(shù)表示阻塞的時(shí)間,當(dāng)為-1時(shí)用久等待,0就立刻繼續(xù)執(zhí)行,大于0就等待過了這個(gè)時(shí)間就繼續(xù)執(zhí)行,這個(gè)方法是阻塞的,當(dāng)管道中沒有操作事件的時(shí)候,線程就會(huì)在這里進(jìn)行等待,直到有事件來了才開始處理
如果出現(xiàn)錯(cuò)誤或者超時(shí)了就跳過事件處理,否則進(jìn)行事件的處理
如果對應(yīng)是管道的讀端就調(diào)用awoken();
void Looper::awoken() {
char buffer[16];
ssize_t nRead;
do {
nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
} while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
}
管道有事件操作,喚醒Epoll中等待的線程,繼續(xù)執(zhí)行,這時(shí)候就回到j(luò)ava層的MessageQueue.next()中繼續(xù)執(zhí)行
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(ptr, nextPollTimeoutMillis);
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) {
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 (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
}
// 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(TAG, "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;
}
}
從消息隊(duì)列中取出消息,如果沒有消息,nextPollTimeoutMillis為-1,再次調(diào)用epoll_wait時(shí)將會(huì)進(jìn)入等待狀態(tài),有消息時(shí)需要判斷第一個(gè)消息的執(zhí)行時(shí)間還沒有到,那將繼續(xù)循環(huán)在epoll_wait中進(jìn)行等待,只有當(dāng)這個(gè)消息的時(shí)間到了才返回,否則就一次一次的循環(huán),沒有消息的情況就會(huì)讓線程進(jìn)入用久等待狀態(tài)。取出消息以后就會(huì)立即返回,這樣取消息的總流程就已經(jīng)結(jié)束了。
IdleHandler是線程空閑的時(shí)候執(zhí)行的,在next方法中當(dāng)取不到消息的時(shí)候會(huì)處理IdleHandler等,執(zhí)行完就會(huì)刪除,只執(zhí)行一次。
取出消息以后就比較簡單了,回到Looper.loop()中,
try {
msg.target.dispatchMessage(msg);
end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
直接調(diào)用msg.target處理消息,在Handler發(fā)送消息的時(shí)候已經(jīng)有了定義
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
于是由Handler處理消息,對消息做最后的分發(fā)處理,處理消息也有優(yōu)先級(jí)
/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
如果消息自己設(shè)置了回調(diào),則優(yōu)先有Message自己處理回調(diào)的消息,如果Handler設(shè)置了回調(diào),則再處理Handler的回調(diào),如果都沒有設(shè)置回調(diào),則由handleMessage()處理。
至此,消息的讀取這一部分分析結(jié)束了。
2、發(fā)送端代碼解讀 Handler.sendEmptyMessage()--->Handler.sendMessageAtTime()--->Handler.enqueueMessage()--->MessageQueue.enqueueMessage()
上面是一些簡單的調(diào)用過程,就不具體分析了
boolean enqueueMessage(Message msg, long when) {
synchronized (this) {
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
Message本身設(shè)計(jì)是一種單向鏈表的結(jié)構(gòu),含有重要成員屬性next。設(shè)置msg執(zhí)行的時(shí)間when屬性,而且Message本身排序按照when的時(shí)間先后順序進(jìn)行排序,when時(shí)間越大,將執(zhí)行的時(shí)間越長。
上面代碼中首先判斷mMessages是否為null,
1)當(dāng)為null時(shí)表示當(dāng)前的MessageQueue中沒有Message,加入的Message為第一個(gè)message,設(shè)置next為null,并將傳入消息保存在mMessages屬性中,這時(shí)的needWake由mBlocked決定,mBlocked是因?yàn)榫€程取消息的時(shí)候可能設(shè)置為true。
2)當(dāng)MessageQueue中有Message的時(shí)候,需要將當(dāng)前的Message插入到MessageQueue中。
插入Message到MessageQueue中以后就根據(jù)needWake決定是否需要喚醒nativeWake(mPtr);
static void android_os_MessageQueue_nativeWake(JNIEnv* env, jclass clazz, jlong ptr) {
NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
return nativeMessageQueue->wake();
}
void NativeMessageQueue::wake() {
mLooper->wake();
}
void Looper::wake() {
ssize_t nWrite;
do {
nWrite = write(mWakeWritePipeFd, "W", 1);
} while (nWrite == -1 && errno == EINTR);
}
這里write命令是向管道的寫端口寫入一個(gè)w字符,當(dāng)對管道進(jìn)行寫入操作的時(shí)候,讀取管道的線程處于epoll_wait()等待期的時(shí)候就會(huì)得到喚醒,繼續(xù)執(zhí)行后續(xù)的代碼,獲取消息。這樣發(fā)送消息端就已經(jīng)分析完畢了。
總結(jié):
至此,大部分流程已經(jīng)走通,留待同仁參考,具體的細(xì)節(jié)請參考源碼,這里做簡單的總結(jié)。
1)Looper是屬于哪一個(gè)線程,handler發(fā)送的消息就屬于哪一個(gè)線程進(jìn)行處理。
2)兩個(gè)線程之間傳遞數(shù)據(jù)是因?yàn)樵谕粋€(gè)進(jìn)程中,兩個(gè)線程進(jìn)行資源的共享,這里的資源共享就是消息隊(duì)列和里面的消息
3)兩個(gè)線程之間等待和喚醒是使用了無名管道,并且使用了Epoll機(jī)制進(jìn)行線程的阻塞和喚醒