Handler的源码解读

底层原理说明:

例如有ThreadA和ThreadB等2个子线程,如果ThreadA作为接收端,ThreadB是发送端。
在Linux系统中,管道是一种非常重要的通信手段方式,它有2个端,一个是读的端,一个是写的端口,一个进程向管道的写端写入数据,另一个进程就可以在管道的读端进行读取数据。handler的底层就是使用了管道作为通信的方式,接收方就是管道的读端,发送方就对应管道的写端,管道作为中间通知的媒介。管道和Epoll结合充当等待和唤醒的工具。

1、接收端代码解读
接收端需要执行以下三步骤
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是专属线程的缓存区域,这里新建了一个Looper对象,并保存在当前线程的sThreadLocal中。

 private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }

Looper的构造函数中,定义了MessageQueue对象

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

nativeInit()方法是一个netive方法,在android_os_MessageQueue.cpp中实现的

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对象,这是MessageQueue在c++层对应的消息队列,计数+1,并将NativeMessageQueue对象的地址返回给MessageQueue,赋值给MessageQueue的成员变量mPtr。继续看NativeMessageQueue构造方法:

NativeMessageQueue::NativeMessageQueue() : mInCallback(false), mExceptionObj(NULL) {
    //在内部创建了一个Looper对象
    mLooper = Looper::getForThread();
    if (mLooper == NULL) {
        mLooper = new Looper(false);
        Looper::setForThread(mLooper);
    }
}

这里首先查询当前线程中是否存有mLooper 对象,这里和java层中的Looper保存在sThreadLocal有同样效果。在这里开始会创建一个Looper对象,并将结果保存给当前的线程中。c层中的Looper的构造器:

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.
    //来创建一个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
    //监控mWakeReadPipeFd文件描述符的EPOLLIN事件,即当管道中有内容可读时,就唤醒当前正在等待管道中的内容的线程
    eventItem.events = EPOLLIN;
    eventItem.data.fd = mWakeReadPipeFd;
    //告诉epoll要监控相应的文件描述符的什么事件
    result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);
}

这里就引出了最终的线程通信的boss(管道),首先新建一个无名管道,wakeFds表示读写2端,接着调用fcntl设置管道读写端都为非阻塞I/O操作,后面接着调用epoll定义事件等,epoll主要处理等待和唤醒工作。
小结:在Looper.prepare()中,定义了一个Looper对象,并将Looper对象保存在ThreadLocal中,接着定义了MessageQueue对象,MessageQueue又引入了c层的NativeMessageQueue对象,将地址保存在java层的MessageQueue中,同时也对应的在c层启用了一个Looper对象,创建了管道和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()的执行

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

进入for的无限循环中,从MessageQueue中取出Message对象,如果没有了消息就返回了,跳出循环结束了。取出Message对象就会调用 msg.target.dispatchMessage(msg);进行消息的分发。msg.target是一个Handler对象,由发送方决定的这个处理消息的工具。这里的重点方法是
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
        //表示如果当前消息队列中没有消息,它要等待的时候
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
            //看看当前消息队列中有没有消息
            nativePollOnce(ptr, nextPollTimeoutMillis);
            ......
      }

因为主线程和子线程共用一个Handler,所以也是共用的MessageQueue对象,MessageQueue中的mMessages也是共享的资源,mMessages表示消息队列中第一个消息。
进入了一个for的无限循环,首先执行了 nativePollOnce(ptr, nextPollTimeoutMillis);这个方法。

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层执行的,调用关系也比较简单,一路执行,最后调用了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);
    }
}

这里是一个for的无限循环,第一次进来mResponses是空的,mResponses是一个数组,存储的是Response的值,在for循环中调用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;

    //定义一个事件数组
    struct epoll_event eventItems[EPOLL_MAX_EVENTS];
    //监控的文件描述符是否有IO事件发生
    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) { //表示超时了
        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文件描述符上发生了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();
                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;
}

首先重新定义了消息处理的时间,根据时间定义了一个事件存储器,调用epoll_wait方法取出事件数量,epoll_wait函数中最后一个timeout参数表示阻塞的时间,当为-1时用久等待,0就立刻继续执行,大于0就等待过了这个时间就继续执行,这个方法是阻塞的,当管道中没有操作事件的时候,线程就会在这里进行等待,直到有事件来了才开始处理

如果出现错误或者超时了就跳过事件处理,否则进行事件的处理
如果对应是管道的读端就调用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中等待的线程,继续执行,这时候就回到java层的MessageQueue.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 (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;
        }
    }

从消息队列中取出消息,如果没有消息,nextPollTimeoutMillis为-1,再次调用epoll_wait时将会进入等待状态,有消息时需要判断第一个消息的执行时间还没有到,那将继续循环在epoll_wait中进行等待,只有当这个消息的时间到了才返回,否则就一次一次的循环,没有消息的情况就会让线程进入用久等待状态。取出消息以后就会立即返回,这样取消息的总流程就已经结束了。

IdleHandler是线程空闲的时候执行的,在next方法中当取不到消息的时候会处理IdleHandler等,执行完就会删除,只执行一次。

取出消息以后就比较简单了,回到Looper.loop()中,

try {
                msg.target.dispatchMessage(msg);
                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }

直接调用msg.target处理消息,在Handler发送消息的时候已经有了定义

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

于是由Handler处理消息,对消息做最后的分发处理,处理消息也有优先级

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

如果消息自己设置了回调,则优先有Message自己处理回调的消息,如果Handler设置了回调,则再处理Handler的回调,如果都没有设置回调,则由handleMessage()处理。
至此,消息的读取这一部分分析结束了。

2、发送端代码解读 Handler.sendEmptyMessage()--->Handler.sendMessageAtTime()--->Handler.enqueueMessage()--->MessageQueue.enqueueMessage()
上面是一些简单的调用过程,就不具体分析了

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本身设计是一种单向链表的结构,含有重要成员属性next。设置msg执行的时间when属性,而且Message本身排序按照when的时间先后顺序进行排序,when时间越大,将执行的时间越长。
上面代码中首先判断mMessages是否为null,
1)当为null时表示当前的MessageQueue中没有Message,加入的Message为第一个message,设置next为null,并将传入消息保存在mMessages属性中,这时的needWake由mBlocked决定,mBlocked是因为线程取消息的时候可能设置为true。
2)当MessageQueue中有Message的时候,需要将当前的Message插入到MessageQueue中。
插入Message到MessageQueue中以后就根据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命令是向管道的写端口写入一个w字符,当对管道进行写入操作的时候,读取管道的线程处于epoll_wait()等待期的时候就会得到唤醒,继续执行后续的代码,获取消息。这样发送消息端就已经分析完毕了。

总结:
至此,大部分流程已经走通,留待同仁参考,具体的细节请参考源码,这里做简单的总结。
1)Looper是属于哪一个线程,handler发送的消息就属于哪一个线程进行处理。
2)两个线程之间传递数据是因为在同一个进程中,两个线程进行资源的共享,这里的资源共享就是消息队列和里面的消息
3)两个线程之间等待和唤醒是使用了无名管道,并且使用了Epoll机制进行线程的阻塞和唤醒

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容