国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

android源碼分析-深入MessageQueue

LeexMuller / 1736人閱讀

摘要:相當于層的初始化。注意,這里是層層自己的消息,與層的沒關系。好吧,這個過程基本上分析完畢了,其實就是通過不斷的處理消息,并且調用消息的回調。

承接上文在looper中會在一開始就創建一個MessageQueue,并且在loop中每次都會從其中取出一個message處理。那么我們就來看看這個MessageQueue:

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

nativeInit,無可避免的又要進入c層進行分析。對應的文件是/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(nativeMessageQueue);
}

這里創建了一個新的NativeMessageQueue并返回他的指針。這個類的定義也在此文件中,看看他的構造做了什么:

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

新建了一個Looper對象,這個肯定不是java層的那個了,但是前后都有getForThread和setForThread。那么他們分別在干什么呢?我的理解是在做tls線程本地變量的處理,確保本線程只有一個looper。具體的內容在這里不再論述,后續有機會可以剖析下。

我們下面來看看這個Looper是什么吧,他的構造函數如下:

Looper::Looper(bool allowNonCallbacks) :
        mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
        mPolling(false), mEpollFd(-1), mEpollRebuildRequired(false),
        mNextRequestSeq(0), mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
    mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
    LOG_ALWAYS_FATAL_IF(mWakeEventFd < 0, "Could not make wake event fd: %s",
                        strerror(errno));

    AutoMutex _l(mLock);
    rebuildEpollLocked();
}

除了狀態的值得設置外,就是rebuildEpollLocked:

void Looper::rebuildEpollLocked() {
    // Close old epoll instance if we have one.
    if (mEpollFd >= 0) {
#if DEBUG_CALLBACKS
        ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this);
#endif
        close(mEpollFd);
    }

    // Allocate the new epoll instance and register the wake pipe.
    mEpollFd = epoll_create(EPOLL_SIZE_HINT);
    LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(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 = mWakeEventFd;
    int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance: %s",
                        strerror(errno));

    for (size_t i = 0; i < mRequests.size(); i++) {
        const Request& request = mRequests.valueAt(i);
        struct epoll_event eventItem;
        request.initEventItem(&eventItem);

        int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, request.fd, & eventItem);
        if (epollResult < 0) {
            ALOGE("Error adding epoll events for fd %d while rebuilding epoll set: %s",
                  request.fd, strerror(errno));
        }
    }
}

我們看到了什么?epoll。這不是linux中的epoll嗎?就是這個玩意,為了控制多個fd(文件描述符)的讀寫等事件而誕生的,一般多用于網絡開發,類似win上的完成端口。然后新建了一個eventItem用于監聽mWakeEventFd,就是將喚醒的eventfd放到epoll的監聽隊列中,用于喚醒機制。然后呢,進行了一個循環,取出所有的request,并且都放到了epoll監聽,首次調用這個for循環不會被執行,因為mRequests的size是0。這些request都是什么呢?看定義:

    struct Request {
        int fd;
        int ident;
        int events;
        int seq;
        sp callback;
        void* data;

        void initEventItem(struct epoll_event* eventItem) const;
    };

那么他們對應的具體內容又是什么呢?先放一放,往下看。

回到java層的loop函數中,每次調用next方法獲取message,那么看看這個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;
        }
    }

首先看到獲取了mPtr,這個ptr就是c層的nativeMessageQueue的地址。然后進入了一個死循環,率先走了一個nativePollOnce(ptr, nextPollTimeoutMillis);內部調用了android_os_MessageQueue_nativePollOnce:

static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jobject obj,
        jlong ptr, jint timeoutMillis) {
    NativeMessageQueue* nativeMessageQueue = reinterpret_cast(ptr);
    nativeMessageQueue->pollOnce(env, obj, timeoutMillis);
}

這里實際上還原了地址為NativeMessageQueue對象,并調用了pollOnce方法:

void NativeMessageQueue::pollOnce(JNIEnv* env, jobject pollObj, int timeoutMillis) {
    mPollEnv = env;
    mPollObj = pollObj;
    mLooper->pollOnce(timeoutMillis);
    mPollObj = NULL;
    mPollEnv = NULL;

    if (mExceptionObj) {
        env->Throw(mExceptionObj);
        env->DeleteLocalRef(mExceptionObj);
        mExceptionObj = NULL;
    }
}

保留了pollObj對象,并且調用了Looper的pollOnce。相當于c層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);
    }
}

一個死循環,里面先是一個while,優先處理應答response(一個request對應一個response),并返回。如果沒有response需要處理的時候,走pollInner。這個pollInner是個關鍵,代碼比較多,我們節選看:

    ......
    struct epoll_event eventItems[EPOLL_MAX_EVENTS];
    int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
    ......
    for (int i = 0; i < eventCount; i++) {
        int fd = eventItems[i].data.fd;
        uint32_t epollEvents = eventItems[i].events;
        if (fd == mWakeEventFd) {
            if (epollEvents & EPOLLIN) {
                awoken();
            } else {
                ALOGW("Ignoring unexpected epoll events 0x%x on wake event fd.", 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);
            }
        }
    }
    ......

epoll_wait在mEpollFd上阻塞等待,直到有事件發生。如果等到了就執行下面的for循環,枚舉每一個epoll_event,如果等待到的消息是喚醒消息(fd==mWakeEventFd),則執行awoken喚醒,否則判斷epollEvents是否含有相關事件,如果有填寫生成好的events,這個應該是轉換一下事件為了上層使用。然后進行了pushResponse的動作,這里終于有個response生成的過程了,繼續看下去:

void Looper::pushResponse(int events, const Request& request) {
    Response response;
    response.events = events;
    response.request = request;
    mResponses.push(response);
}

看到了吧,就是個填充response的過程,并將其push到mResponses中。再回到pollInner中往下看:

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

一上來就是一個while循環,處理一下之前堆積的事件。注意,這里是c層(native層)自己的消息,與java層的沒關系。這里有個時間的對比,如果每個messageEnvelope的uptime<=now,也即是小于等于當前時間,那么這個uptime是個什么呢?我的理解是一個喚醒時間,也就是message的執行時間,因為message是允許被后置一段時間執行的。如果需要被執行的時間比當前時間晚,就調用這個message的handler的handleMessage??雌饋砗芎侠?,就是為了清除一下之前堆積還未執行的事件的handle的回調。
之后又是一個for循環:

    ......
    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
            // Invoke the callback.  Note that the file descriptor may be closed by
            // the callback (and potentially even reused) before the function returns so
            // we need to be a little careful when removing the file descriptor afterwards.
            int callbackResult = response.request.callback->handleEvent(fd, events, data);
            if (callbackResult == 0) {
                removeFd(fd, response.request.seq);
            }

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

這里就是處理response了,就是走一個response.request.callback->handleEvent。
我們現在繼續找線索下,在Looper的構造中出現了mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);,這個eventfd就是用來支持進程或者線程間通訊的通道,類似管道。

好吧,這個過程基本上分析完畢了,其實就是通過epoll不斷的處理消息,并且調用消息的回調。但是其實整個過程還有很多不是很明確的地方,例如:1.這個epoll綁定的fd到底是個什么東西?是管道嗎?網上的文章基本上都是說管道,這里我沒有找到線索,不好確定。2.這個c層的looper中的sendmessage已經很明確是根據傳遞進來的參數來設定messageEnvelope的handler。但是調用他的是哪個東西呢?怎么和java層結合起來呢?有不少問題。

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/66783.html

相關文章

  • android源碼分析-深入消息機制

    摘要:輔助功能類,提供接口向消息池中發送各類消息事件,并且提供響應消息的機制。進入消息泵循環體,以阻塞的方式獲取待處理消息。執行消息的派發。并且將返回值保存在了。我們深入下去看看層的部分,在這里明顯生成了一個新的,并且將地址作為返回值返回了。 概述 android里的消息機制是非常重要的部分,這次我希望能夠系統的剖析這個部分,作為一個總結。首先這里涉及到幾個部分,從層次上看,分為java層和...

    superw 評論0 收藏0
  • android源碼分析-深入looper handler message

    摘要:在這里表示的是不允許退出。這之后會調用到進入實質性的工作函數中。看到了吧,由于函數運行在主線程中,因此以上這些都是在主線程中運行的代碼。注意,這個是排他性的,如果前面的可以執行就不會走后面的。現在比較清楚了吧,整個消息循環是如何運轉的。 本來是不想寫這篇文章的,但是很早以前看過的東西容易遺忘,希望還是給自己一個記錄吧,另外此篇希望能夠寫的深入一些。looper是什么就不介紹了吧,一個線...

    DobbyKim 評論0 收藏0
  • Android異步消息機制

    摘要:在子線程中發送消息,主線程接受到消息并且處理邏輯。也稱之為消息隊列,特點是先進先出,底層實現是單鏈表數據結構得出結論方法初始話了一個對象并關聯在一個對象,并且一個線程中只有一個對象,只有一個對象。 目錄介紹 1.Handler的常見的使用方式 2.如何在子線程中定義Handler 3.主線程如何自動調用Looper.prepare() 4.Looper.prepare()方法源碼分析...

    王晗 評論0 收藏0
  • Android異步消息機制

    摘要:在子線程中發送消息,主線程接受到消息并且處理邏輯。子線程往消息隊列發送消息,并且往管道文件寫數據,主線程即被喚醒,從管道文件讀取數據,主線程被喚醒只是為了讀取消息,當消息讀取完畢,再次睡眠。 目錄介紹 1.Handler的常見的使用方式 2.如何在子線程中定義Handler 3.主線程如何自動調用Looper.prepare() 4.Looper.prepare()方法源碼分析 5....

    blair 評論0 收藏0

發表評論

0條評論

LeexMuller

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<