1. 程式人生 > >Handler機制情景分析

Handler機制情景分析

一. 概述

在整個Android的原始碼世界裡,有兩大利劍,其一是Binder IPC機制,,另一個便是訊息機制(由Handler/Looper/MessageQueue等構成的).
Android有大量的訊息驅動方式來進行互動,比如Android的四劍客Activity, Service, Broadcast, ContentProvider的啟動過程的互動,都離不開訊息機制,Android某種意義上也可以說成是一個以訊息驅動的系統。訊息機制涉及MessageQueue/Message/Looper/Handler這4個類。

1.1 模型

訊息機制主要包含:

  • Message:訊息分為硬體產生的訊息(如按鈕、觸控)和軟體生成的訊息;
  • MessageQueue:訊息佇列的主要功能向訊息池投遞訊息(MessageQueue.enqueueMessage)和取走訊息池的訊息(MessageQueue.next);
  • Handler:訊息輔助類,主要功能向訊息池傳送各種訊息事件(Handler.sendMessage)和處理相應訊息事件(Handler.handleMessage);
  • Looper:不斷迴圈執行(Looper.loop),按分發機制將訊息分發給目標處理者。

1.2 架構圖

clipboard.png

1.3 Demo

public class MainActivity extends AppCompatActivity {

    private Button mButton;
    private final String TAG="MessageTest";
    private int ButtonCount = 0;
    private MyThread myThread;
    private Handler mHandler;
    private int mMessageCount = 0;

    class MyThread extends Thread {
        private Looper mLooper;
        @Override
        public void run() {
            super.run();
            /* Initialize the current thread as a looper */
            Looper.prepare();
            synchronized (this) {
                mLooper = Looper.myLooper();
                notifyAll();
            }
            /* Run the message queue in this thread */
            Looper.loop();
        }

        public Looper getLooper(){
            if (!isAlive()) {
                return null;
            }

            // If the thread has been started, wait until the looper has been created.
            synchronized (this) {
                while (isAlive() && mLooper == null) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }
            }
            return mLooper;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = (Button)findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Log.d(TAG, "Send Message "+ ButtonCount);
                ButtonCount++;
                /* 按下按鍵後通過mHandler傳送一個訊息 */
                Message msg = new Message();
                mHandler.sendMessage(msg);
            }
        });

        myThread = new MyThread();
        myThread.start();

        /* 建立一個handle例項(詳見4.3.2),這個handle為執行緒myThread服務,當收到mesg時會呼叫設定的回撥函式*/
        mHandler = new Handler(myThread.getLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                Log.d(TAG, "get Message "+ mMessageCount);
                mMessageCount++;
                return false;
            }
        });
    }
}

大概流程:先建立的一個執行緒,該執行緒中呼叫了Looper.prepare()(詳見2.1)和Looper.loop()(詳見2.2)方法,接著啟動了該執行緒,緊接著初始化了一個Handler例項(詳見4.3.2).用於服務message,在按下按鍵後通過mHandler傳送了一個訊息(詳見4.2),此時handleMessage被回撥(詳見4.1).接下來進行詳細分析.

該Demo中有個兩點getLooper方法,當外界呼叫該方法時,他會判斷當前mLooper是否為空,空的話就會一直等待.
為什麼要這麼做?
因為在建立執行緒後去獲取mLooper,此時執行緒的run方法可能還為執行,所以此時mLooper

值應該為null;
當運行了Looper.prepare()方法建立了looper後,通過Looper.myLooper()獲取到mLooper,再notifyAll;

二. Looper

2.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));        ③
    }
①:無參情況下呼叫prepare(true),形參置true表示允許退出。
②: sThreadLocal 會先去獲取本地的資料,如果能獲取到說明已經prepare過,則丟擲異常。
③:設定sThreadLocal資料

sThreadLocal是ThreadLocal型別(static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();)
ThreadLocal: 執行緒本地儲存區,每個執行緒都有自己的私有本地儲存區域,不同的執行緒之間彼此不能訪問對方的儲存區。

接下來看下剛儲存的TLS區域的Looper物件:

    private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);           ①
        mThread = Thread.currentThread();                 ②
    }
①:建立一個訊息佇列
②:獲取當前執行緒物件

這裡為該執行緒建立了一個訊息佇列MessageQueue的建構函式中呼叫的hal層的本地方法:

    MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        mPtr = nativeInit();
    }

這個流程的分析先到這。

2.2 Looper.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;

        // 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;
            }

            // 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);               ③

            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();                        ④
        }
    }
①: 獲取TLS中儲存的Looper物件
②: 獲取訊息,沒有訊息的時候會阻塞(詳見3.1)
③: 分發訊息(詳見4.1)
④: 回收訊息到訊息池(詳見5.1)

三. MesageQueue

3.1 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
        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 (false) Log.v("MessageQueue", "Returning message: " + msg);
                        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("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;
        }
    }
①: 呼叫本地epoll方法, 當沒有訊息時會阻塞在這,阻塞時間為nextPollTimeoutMillis(詳見6.1.1)
②: 查詢訊息佇列中的非同步訊息(詳見4.2)
③: 如果當前時間小於非同步訊息的觸發時間,則設定下一輪poll的超時時間(相當於休眠時間),否則返回將要執行的非同步訊息.
④: 沒有非同步訊息,下輪poll則無限等待,直到新的訊息來臨
⑤: 檢測下退出標誌
⑥: 如果訊息佇列未空或是第一個msg(訊息剛放進佇列且未達到觸發時間),則執行空閒的handler
⑦: IdleHandler一個臨時存放陣列物件(下面可以看到一個列表轉陣列的方法被呼叫)
⑧: 執行空閒的handler(只有第一次迴圈時會執行idle handle)
⑨: 重置idle handler計數,防止下次執行

往往在第一次進入next函式迴圈時,在nativePollOnce阻塞之後,都會執行idle handle函式.
獲取到非同步訊息,立馬把該訊息返回給上一層,否則繼續迴圈等待新的訊息產生.

3.2 enqueueMessage()

boolean enqueueMessage(Message msg, long when) {
    if (msg.target == null) {                                                             ①
        throw new IllegalArgumentException("Message must have a target.");
    }
    if (msg.isInUse()) {                                                                  ②
        throw new IllegalStateException(msg + " This message is already in use.");
    }

    synchronized (this) {
        if (mQuitting) {
            IllegalStateException e = new IllegalStateException(
                    msg.target + " sending message to a Handler on a dead thread");
            Log.w("MessageQueue", e.getMessage(), e);
            msg.recycle();
            return false;
        }

        msg.markInUse();
        msg.when = when;
        Message p = mMessages;
        boolean needWake;
        if (p == null || when == 0 || when < p.when) {                                    ③
            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;
    }

①: 判斷該訊息是否有handler,每個msg必須有個對應的handler;
②: 判斷該訊息是否已經使用;
③: 判斷是否有已經準備好的訊息(表頭訊息)或當前傳送訊息的延時時間為0或next ready msg延時時間大於當前訊息延時時間則將當前訊息變為新的表頭.;

  根據判斷當前阻塞標誌,來覺得是否需要喚醒;  

④: 根據時間將訊息插入到訊息對列中;
⑤: 上文分析在next()方法中會被阻塞,在這裡就可以喚醒阻塞(詳見6.1.2);

四. Handler

4.1 訊息分發

    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);                                                          ①
        } else {
            if (mCallback != null) {                                                      ②
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);                                                           ③
        }
    }
①: 如果該msg設定了回撥函式,則直接呼叫回撥方法message.callback.run();
②: 當handler設定了回撥函式,則回撥方法mCallback.handleMessage(msg);
③: 呼叫handler自身的方法handleMessage,該方法預設為空,一般通過子類覆蓋來完成具體的邏輯;

我們Demo程式中,是使用第二種方法,設定回撥來實現具體的邏輯,分發訊息的本意是響應訊息的對應的執行方法.

4.2 訊息傳送

clipboard.png

可以看到呼叫sendMessage方法後,最終呼叫的是enqueueMessage方法.

    public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }

    public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }

可以看到傳送訊息時都有一個時間引數選擇,該引數就是我們前面分析的延時觸發時間(相對時間).

    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;                                                        ①
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }

    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;                                                                  ②
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }
①: 判斷handler建立時,傳進來的訊息對列是否為空(詳見4.3)
②: 訊息的target為該物件本身,handler型別

這裡有對發生的訊息進行非同步標誌設定,通過判斷mAsynchronous標誌,該標誌是在建立handler時初始化的(詳見4.3);
Handler.enqueueMessage方法呼叫的是MessageQueue.enqueueMessage方法(詳見3.2);

4.3 建立Handler

4.3.1 無參構造

    public Handler() {
        this(null, false);
    }

    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;
    }

無參構造方式比起我們Demo中的方式,它自己回撥用 Looper.myLooper()靜態方法獲取looper;

4.3.2 有參構造

    public Handler(Looper looper, Callback callback) {
        this(looper, callback, false);                                                    ①
    }
    
    public Handler(Looper looper, Callback callback, boolean async) {
        mLooper = looper;
        mQueue = looper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }
①: 呼叫有參建構函式建立handler且非同步標誌置false說明該handler傳送的訊息都為同步訊息.

Demo中的handler就是使用該方式建立,自己傳入looper引數.

五. Message

5.1 recycle()

    public void recycle() {
        if (isInUse()) {
            if (gCheckRecycle) {
                throw new IllegalStateException("This message cannot be recycled because it "
                        + "is still in use.");
            }
            return;
        }
        recycleUnchecked();
    }

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++;
            }
        }
    }
①: 將該訊息標誌置為使用中並清除其他引數為default
②: 將該訊息加入訊息池,當訊息池未滿時

將訊息回收到訊息池都是將訊息加入到訊息池的連結串列表頭.

5.2 obtain()

    public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;                                                        ①
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }
        return new Message();                                                             ②
    }
①: 從訊息池表頭拿出一個訊息
②: 如果訊息池為空則建立一個訊息

可以看出每次從訊息池取出訊息都是從連結串列的表頭取出,再對訊息的計數做減法.

六. HAL層

native層本身也有一套完整的訊息機制,用於處理native的訊息;
在整個訊息機制中,MessageQueue是連線java層和native層的紐帶;

6.1 MessageQueue

檔案

android_os_MessageQueue.c
static JNINativeMethod gMessageQueueMethods[] = {
    /* name, signature, funcPtr */
    { "nativeInit", "()J", (void*)android_os_MessageQueue_nativeInit },
    { "nativeDestroy", "(J)V", (void*)android_os_MessageQueue_nativeDestroy },
    { "nativePollOnce", "(JI)V", (void*)android_os_MessageQueue_nativePollOnce },
    { "nativeWake", "(J)V", (void*)android_os_MessageQueue_nativeWake },
    { "nativeIsIdling", "(J)Z", (void*)android_os_MessageQueue_nativeIsIdling }
};

以上可以看出上層呼叫nativePollOnce方法實質是呼叫HAL層的android_os_MessageQueue_nativePollOnce方法

6.1.1 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);
}

void NativeMessageQueue::pollOnce(JNIEnv* env, int timeoutMillis) {
    mInCallback = true;
    mLooper->pollOnce(timeoutMillis);
    mInCallback = false;
    if (mExceptionObj) {
        env->Throw(mExceptionObj);
        env->DeleteLocalRef(mExceptionObj);
        mExceptionObj = NULL;
    }
}

通過原始碼可以看出訊息佇列中的pollOnce實質是呼叫的looper中的pollOnce方法(詳見6.2.1)

6.1.2 nativeWake

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();
}

通過原始碼可以看出訊息佇列中的wake實質是呼叫的looper中的wake方法(詳見6.2.4)

6.1.3 nativeInit

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);
}


NativeMessageQueue::NativeMessageQueue() : mInCallback(false), mExceptionObj(NULL) {
    mLooper = Looper::getForThread();
    if (mLooper == NULL) {
        mLooper = new Looper(false);
        Looper::setForThread(mLooper);
    }
}

可以看到hal層和java層中建立looper的時序幾乎是一樣的,先建立一個訊息對列,再建立一個looper(Looper的構造詳見6.2.3);

6.1.4 nativeIsIdling

static jboolean android_os_MessageQueue_nativeIsIdling(JNIEnv* env, jclass clazz, jlong ptr) {
    NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
    return nativeMessageQueue->getLooper()->isIdling();
}

bool Looper::isIdling() const {
    return mIdling;
}

還是呼叫looper中的方法,來看看這個標誌具體表示什麼狀態:

    // We are about to idle.
    mIdling = true;

    struct epoll_event eventItems[EPOLL_MAX_EVENTS];
    int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);

    // No longer idling.
    mIdling = false;

以上程式碼片為Looper::pollInner中的一段,在wait時是空閒,當有資料來臨時是非空閒的;
以前也用過這樣的方法來判斷執行緒是否在使用,想不到在這裡也看到了這種方法;

6.1.5 nativeDestroy

static void android_os_MessageQueue_nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
    NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
    nativeMessageQueue->decStrong(env);
}

6.2 Looper

Looper.cpp: system/core/lib/libutils

6.2.1 Looper::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 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 (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);
    }
}

Looper::pollOnce是通過呼叫Looper::pollInner方法實現;

6.2.2 Looper::pollInner

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
    }

    // 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);    ①

    // 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;
    }

    // Check for poll timeout.
    if (eventCount == 0) {                                                                  ③
#if DEBUG_POLL_AND_WAKE
        ALOGD("%p ~ pollOnce - timeout", this);
#endif
        result = POLL_TIMEOUT;
        goto Done;
    }

    // Handle all events.
#if DEBUG_POLL_AND_WAKE
    ALOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount);
#endif

    /* 處理epoll後的所有事件 */
    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) {
                    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();

#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;
}
①: 等待mEpollFd有事件產生,等待時間為timeoutMilli;
當上層發訊息時且判斷需要喚醒,則會往管道的讀端寫入資料用於喚醒(詳見6.2.3);
②: 檢測poll是否出錯;
③: 檢測poll是否超時;
④: 如果是因為往管道讀端寫入資料被喚醒,則都去並清空管道中的資料;

6.2.3 Looper::Looper()

Looper::Looper(bool allowNonCallbacks) :
        mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
        mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
    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);

    mIdling = false;

    // 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);

    struct epoll_event eventItem;
    memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
    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);
}
①: 建立一個無名管道, wakeFds[0]:讀檔案描述符, wakeFds[1]: 寫檔案描述符;
②: 更改為無阻塞方式;
③: EPOLLIN:連線到達,有資料來臨;
④: 監測管道讀端是否有資料來臨;

6.2.4 Looper::wake()

void Looper::wake() {
#if DEBUG_POLL_AND_WAKE
    ALOGD("%p ~ wake", this);
#endif

    ssize_t nWrite;
    do {
        nWrite = write(mWakeWritePipeFd, "W", 1);
    } while (nWrite == -1 && errno == EINTR);

    if (nWrite != 1) {
        if (errno != EAGAIN) {
            ALOGW("Could not write wake signal, errno=%d", errno);
        }
    }
}

喚醒只是向管道的寫端寫入一個位元組資料,epoll_wait則會得到返回;

總結

在這裡做個總結針對java層(因為native層的訊息機制未進行詳細分析不過估計和java層的流程差不多);
當呼叫j靜態方法Looper.prepare()初始化後,再呼叫Looper.loop()方法進行訊息迴圈處理;
Looper.loop()方法中呼叫MesageQueue.next()方法檢索新訊息,沒有則阻塞,有則將訊息插入訊息連結串列頭後立即返回;
阻塞方式是呼叫本地的nativePollOnce()方法實現,其原理是利用epoll管道檔案描述符實現;
Looper.loop()呼叫dispatchMessage方法實現訊息的分發處理;
傳送一個訊息的實質是呼叫個MessageQueue.enqueueMessage()方法往訊息連結串列中插入一個訊息,插入位置的條件為延時時間;
然後再呼叫一個本地方法nativeWake對前面阻塞的進行喚醒,實質是往管道中寫入一個位元組資料;

參考