Sleep waiting and wake-up mechanisms in Logoff

Source: Internet
Author: User

The sleep wait and wake-up mechanisms in logoff C ++ logoff and sleep mechanisms are provided through the pollOnce and wake functions. They also use the Operating System (Linux kernel) the epoll mechanism. When the monitored file (added through EPOLL_CTL_ADD of epoll_ctl) can be I/O, epoll_wait will wake up from sleep. In this case, you can check which one is (or which) the file corresponding to the file descriptor can be read and written by I/O for further processing. Users can use them to have sleep waiting and wakeup mechanisms. The following details. In the logoff constructor, a pipeline is created (row 73 below), and epoll_create is called to obtain the descriptor (row 88) of an epoll instance ), finally, add the read end descriptor of the MPs queue as an event report item to epoll (row 95 ). In this way, when the read end of the MPs queue has readable data, a report is generated. The logoff constructor is as follows (see The logoff. cpp file): The logoff pollOnce function will eventually call its pollInner function. In the latter, epoll_wait will be called to sleep and wait for the arrival of the file descriptor monitored by it to see if there is an I/O event. If there is (even if there is only one), epoll_wait will wake up, then you can check which file descriptor contains the I/O events. The related code in the pollInner function is as follows (see the file logoff. cpp): it can be seen that calling the pollOnce function of logoff in a thread loop will cause sleep to wait for epoll_wait at Step 1 in the preceding row. When a message is sent to the message queue and is awakened, row 218 is awakened. Therefore, the message is returned from the pollOnce function and can be retrieved from the message queue for processing. Logoff's wake function is used to write characters (the row below is 367) to the pipeline to wake up pollOnce: Let's take a look at how MessageQueue of the Java layer uses this mechanism. Previously mentioned in android. OS. when the next function of MessageQueue retrieves the next message, the native implementation is called when the nativePollOnce function is implemented in the native layer (see android_ OS _MessageQueue.cpp in the file ): the code of the pollOnce function in the above line 157 is (see the file android_ OS _MessageQueue.cpp): in this way, they implement sleep waiting on the read end of the pipeline in the logoff through the pollOnce Of The logoff. When android. OS. after the enqueueMessage function of MessageQueue adds a new message to the queue or removeSyncBarrier removes the synchronization barrier, it may need to call nativeWake to wake up. The native implementation is as follows: (See android_ OS _messagequeu ): the above line 162 calls the following function. The Code is as follows (see the file android_ OS _MessageQueue.cpp): in this way, logoff writes characters to the write end of the pipeline to wake up its sleep wait on the read end of the pipeline. Therefore, with the help of logoff's wake and pollOnce functions, other message queues (such as Java-layer message queues) can have a sleep wake-up mechanism: when there is no message, the pollOnce caller will wait for sleep, when there is a message, let the wake function wake up the sleep wait.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.