[Android] Handler源码解析 (Native层)

接前文[Android] Handler源码解析 (Java层),接下来对Handler机制在Native层上作解析。

Java层的MessageQueue中有4个native方法:

// 初始化和销毁
private native static long nativeInit();
private native static void nativeDestroy(long ptr);
// 等待和唤醒
private native static void nativePollOnce(long ptr, int timeoutMillis);
private native static void nativeWake(long ptr);
// 判断native层的状态
private native static boolean nativeIsIdling(long ptr);

下面分别进行介绍。

nativeInit()和nativeDestroy(long ptr)

nativeInit()在MessageQueue初始化时被调用,返回一个long值,保存在mPtr中。

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

nativeInit()的实现在/frameworks/base/core/jni/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);
}

该JNI方法新建了一个NativeMessageQueue对象,然后将其指针用reinterpret_cast为long并返回给java层。同样地:

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

nativeDestory()方法中,将long型的ptr转换为NativeMessageQueue指针,然后再销毁对象。

NativeMessageQueue对象初始化的代码如下所示:

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

可以看到初始化方法中对mLooper进行了赋值。留意到Looper::getForThread();一句,结合其下的代码,猜想这是类似ThreadLocal模式的应用。接下来看看Looper类。

Looper类的声明在/system/core/include/utils/中,实现在/system/core/libutils/中,先来看一下Looper类的初始化方法:

Looper::Looper(bool allowNonCallbacks) :
        mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
        mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
    int wakeFds[2];
    // 1. 创建一个匿名管道,
    //    wakeFds[0]代表管道的输出,应用程序读它。
    //    wakeFds[1]代表管道的输入,应用程序写它。
    int result = pipe(wakeFds);
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe.  errno=%d", errno);

    mWakeReadPipeFd = wakeFds[0];
    mWakeWritePipeFd = wakeFds[1];

    // 2. 设置读写管道为non-blocking
    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;

    // 3. 新建epoll实体,并将读管道注册到epoll
    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
    // 表示对应的文件描述符可以读时触发event        
    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对象中维护着两个描述符,分别用于读和写。其中读描述符注册到epoll中。合理猜想looper的夸进程的睡眠和唤醒机制是通过epoll实现的。目标线程在读描述符mWakeReadPipeFd上等待,其他线程往mWakeWritePipeFd写入数据时,即可通过epoll机制将目标线程唤醒。

nativePollOnce(long ptr, int timeoutMillis)和nativeWake(long ptr)

nativePollOnce和nativeWake方法的实现如下所示:

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

void NativeMessageQueue::wake() {
    mLooper->wake();
}

可见这两个方法只是对Looper类的pollOnce和wake方法的简单封装。先看一下Looper对象的pollOnce方法实现如下所示:

inline int pollOnce(int timeoutMillis) {
    return pollOnce(timeoutMillis, NULL, NULL, NULL);
}

...

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

先不管什么mResponses、outFd、outEvents和outData,我们先来看一下pollInner的实现。pollInner实现比较复杂,这里只看对本文有用的部分:

int Looper::pollInner(int timeoutMillis) {

    ...

    // 1. 设置默认result
    int result = POLL_WAKE;

    ...

    // 2. 开始在mWakeReadPipeFd上等待
    mIdling = true;

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

    // 3. 等待结束
    mIdling = false;

    ...

    // 4. 根据epoll_wait返回的结果设置result
    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;
    }

    // 5. 通过awoken()从mWakeReadPipeFd读出标记字符“W”

    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 {
            ...
        }
    }
Done: ;

    ...

    return result;
}

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

awoken()只是简单地读出wake()在mWakeWritePipeFd上写入的数据。Looper对象的wake方法实现如下所示:

void Looper::wake() {

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

正如前面所述,往mWakeWritePipeFd写数据即可唤醒在mWakeReadPipeFd上等待的线程。

总结

综上,在native层,一次wait/wake过程简述如下:

  1. native层Looper对象初始化时,新建了一个匿名管道,并将读管道(mWakeReadPipeFd)注册到epoll上。
  2. pollOnce方法调用pollInner方法,其中epoll_wait方法在mWakeReadPipeFd上等待读取。(wait
  3. wake方法被调用,往写管道(mWakeWritePipeFd)上写入字符“W”。
  4. pollInner方法继续执行,调用awoken从mWakeReadPipeFd读出数据。(wake

可画出框架图如下所示:

            +------------------+
            |      Handler     |
            +----^--------+----+
                 |        |
        dispatch |        | send
                 |        |
                 |        v
                +----+ <---+
                |          |
                |  Looper  |
                |          |
                |          |
                +---> +----+
                  ^      |
             next |      | enqueue
                  |      |
         +--------+------v----------+
         |       MessageQueue       |
         +--------+------+----------+
                  |      |
  nativePollOnce  |      |   nativeWake
                  |      |
+----------------------------------------------+ Native Layer
                  |      |
       pollOnce   |      |  wake
                  |      |
         +--------v------v--------+
         |   NativeMessageQueue   |
         +--------+------+--------+
                  |      |
         pollOnce |      |  wake
         pollInner|      |  awoken
                  |      |
              +---v------v---+
              |    Looper    |
              +-+----------+-+
                |          |
     epoll_wait |          |  wake
  +-------------v-+      +-v--------------+
  |mWakeReadPipeFd|      |mWakeWritePipeFd|
  +-------------^-+      +-+--------------+
                |          |
          read  |          | write
                |          |
              +-+----------v-+
              |     Pipe     |
              +--------------+

2pharmaceuticals.com

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据