Android 中 MessageQueue 的 nativePollOnce

Android 中 MessageQueue 的 nativePollOnce,第1张

概述Android SDK 中的事件循环已经是一个老生常谈的问题了, 像 Handler Looper MessageQueue 这几个类也是被大家研究透彻了. 但是再回头看以前自己的分析, 总感觉差点什么, 不够透彻. 心里隐隐感觉自己没有把事情完全吃透, 于是今日又回顾 Android 中的事件循环机制, 注意到 MessageQueue 中获取下一条消息时会执行一个 native 调用 nati @H_404_6@AndroID SDK 中的事件循环已经是一个老生常谈的问题了,像 Handler Looper MessageQueue 这几个类也是被大家研究透彻了.
但是再回头看以前自己的分析,总感觉差点什么,不够透彻. 心里隐隐感觉自己没有把事情完全吃透,于是今日又回顾 AndroID 中的事件循环机制,注意到
MessageQueue 中获取下一条消息时会执行一个 native 调用 nativePollOnce,翻看 AndroID 系统源码发现有内容.

来自高手的解释
@H_404_6@首先,先梭哈一把 stackoverflow 上高手对这个问题(android - what is message queue native poll once in android?)的回答原文:

@H_404_6@Short answer:

@H_404_6@The nativePollOnce method is used to "wait" till the next Message becomes available. If the time spent during this call is long,your main (UI) thread has no real work to do and waits for next events to process. There‘s no need to worry about that.

@H_404_6@Explanation:

@H_404_6@Because the "main" thread is responsible for drawing UI and handling varIoUs events,it‘s Runnable has a loop which processes all these events. The loop is managed by a Looper and its job is quite straightforward: it processes all Messages in the MessageQueue.

@H_404_6@A Message is added to the queue for example in response to input events,as frame rendering callback or even your own Handler.post calls. Sometimes the main thread has no work to do (that is,no messages in the queue),which may happen e.g. just after finishing rendering single frame (the thread has just drawn one frame and is ready for the next one,just waits for a proper time). Two Java methods in the MessageQueue class are interesting to us: Message next() and boolean enqueueMessage(Message,long). Message next(),as its name suggest,takes and returns the next Message from the queue. If the queue is empty (and there‘s nothing to return),the method calls native voID nativePollOnce(long,int) which blocks until a new message is added. At this point you might ask how does nativePollOnce kNow when to wake up. That‘s a very good question. When a Message is added to the queue,the framework calls the enqueueMessage method,which not only inserts the message into the queue,but also calls native static voID nativeWake(long),if there‘s need to wake up the queue. The core magic of nativePollOnce and nativeWake happens in the native (actually,C++) code. Native MessageQueue utilizes a linux system call named epoll,which allows to monitor a file descriptor for IO events. nativePollOnce calls epoll_wait on a certain file descriptor,whereas nativeWake writes to the descriptor,which is one of the IO operations,epoll_wait waits for. The kernel then takes out the epoll-waiting thread from the waiting state and the thread proceeds with handling the new message. If you‘re familiar with Java‘s Object.wait() and Object.notify() methods,you can imagine that nativePollOnce is a rough equivalent for Object.wait() and nativeWake for Object.notify(),except they‘re implemented completely differently: nativePollOnce uses epoll and Object.wait() uses futex linux call. It‘s worth noticing that neither nativePollOnce nor Object.wait() waste cpu cycles,as when a thread enters either method,it becomes Disabled for thread scheduling purposes (quoting the javadoc for the Object class). However,some profilers may mistakenly recognize epoll-waiting (or even Object-waiting) threads as running and consuming cpu time,which is incorrect. If those methods actually wasted cpu cycles,all IDle apps would use 100% of the cpu,heating and slowing down the device.

@H_404_6@Conclusion:

@H_404_6@You shouldn‘t worry about nativePollOnce. It just indicates that processing of all Messages has been finished and the thread waits for the next one. Well,that simply means you don‘t give too much work to your main thread ;)

@H_404_6@translate.Google 一下 ??:

@H_404_6@简短答案:@H_403_44@

@H_404_6@nativePollOnce 方法用于“等待”,直到下一条消息可用为止. 如果在此调用期间花费的时间很长,则您的主线程没有实际工作要做,而是等待下一个事件处理.无需担心.

@H_404_6@说明:@H_403_44@

@H_404_6@因为主线程负责绘制 UI 和处理各种事件,所以主线程拥有一个处理所有这些事件的循环. 该循环由 L??ooper 管理,其工作非常简单: 它处理 MessageQueue 中的所有 Message.
例如,响应于输入事件,将消息添加到队列,帧渲染回调,甚至您的 Handler.post 调用. 有时主线程无事可做(即队列中没有消息),例如在完成渲染单帧之后(线程刚绘制了一帧,并准备好下一帧,等待适当的时间). MessageQueue 类中的两个 Java 方法对我们很有趣: Message next()boolean enqueueMessage(Message,long). 顾名思义,Message next() 从队列中获取并返回下一个消息. 如果队列为空(无返回值),则该方法将调用 native voID nativePollOnce(long,int),该方法将一直阻塞直到添加新消息为止. 此时,您可能会问nativePollOnce 如何知道何时醒来. 这是一个很好的问题. 当将 Message 添加到队列时,框架调用 enqueueMessage 方法,该方法不仅将消息插入队列,而且还会调用native static voID nativeWake(long). nativePollOncenativeWake 的核心魔术发生在 native 代码中. native MessageQueue 利用名为 epoll 的 linux 系统调用,该系统调用可以监视文件描述符中的 IO 事件. nativePollOnce 在某个文件描述符上调用 epoll_wait,而 nativeWake 写入一个 IO *** 作到描述符,epoll_wait 等待. 然后,内核从等待状态中取出 epoll 等待线程,并且该线程继续处理新消息. 如果您熟悉 Java 的 Object.wait()Object.notify()方法,可以想象一下 nativePollOnce 大致等同于 Object.wait(),nativeWake 等同于 Object.notify(),但它们的实现完全不同: nativePollOnce 使用 epoll,而 Object.wait 使用 futex linux 调用. 值得注意的是,nativePollOnceObject.wait 都不会浪费 cpu 周期,因为当线程进入任一方法时,出于线程调度的目的,该线程将被禁用(引用Object类的javadoc). 但是,某些事件探查器可能会错误地将等待 epoll 等待(甚至是 Object.wait)的线程识别为正在运行并消耗 cpu 时间,这是不正确的. 如果这些方法实际上浪费了 cpu 周期,则所有空闲的应用程序都将使用 100% 的 cpu,从而加热并降低设备速度.

@H_404_6@结论:@H_403_44@

@H_404_6@nativePollOnce. 它只是表明所有消息的处理已完成,线程正在等待下一个消息.

我以前的理解分享 @H_404_6@

@H_404_6@

@H_404_6@linux 有多个 IO 模型:

阻塞 IO 非阻塞 IO IO 复用,对应 select poll epoll 都属于基于 IO 复用模式的调用 信号驱动 IO 异步 IO @H_404_6@

看下源码 Java 这边 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) {        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) {            // 这里唤醒 nativePollOnce 的沉睡            nativeWake(mPtr);        }    }    return true;}
next:
Message next() {    //...    int pendingIDleHandlerCount = -1; // -1 only during first iteration    int nextPollTimeoutMillis = 0;    for (;;) {        if (nextPollTimeoutMillis != 0) {            Binder.flushPendingCommands();        }        // nativePollOnce 这里陷入沉睡,等待唤醒        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;            }            //...        }        //...    }}
CPP 那边 nativeWake
voID NativeMessageQueue::wake() {    mLooper->wake();}
voID Looper::wake() {    uint64_t inc = 1;    ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd,&inc,sizeof(uint64_t)));    if (nWrite != sizeof(uint64_t)) {        if (errno != EAGAIN) {            LOG_ALWAYS_FATAL("Could not write wake signal to fd %d: %s",mWakeEventFd,strerror(errno));        }    }}
nativePollOnce:
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;    }}
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);    }}
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.    mPolling = true;    struct epoll_event eventItems[EPolL_MAX_EVENTS];    // 这里重点    int eventCount = epoll_wait(mEpollFd,eventItems,EPolL_MAX_EVENTS,timeoutMillis);    // No longer IDling.    mPolling = false;    // Acquire lock.    mlock.lock();    // Rebuild epoll set if needed.    if (mEpollRebuildrequired) {        mEpollRebuildrequired = false;        rebuildEpollLocked();        goto Done;    }    // Check for poll error.    if (eventCount < 0) {        if (errno == EINTR) {            goto Done;        }        ALOGW("Poll Failed with an unexpected error: %s",strerror(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 == 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);            }        }    }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;            // 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;        }    }    return result;}
总结

以上是内存溢出为你收集整理的Android 中 MessageQueue 的 nativePollOnce全部内容,希望文章能够帮你解决Android 中 MessageQueue 的 nativePollOnce所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存