Message Queue Processing Mechanism in Logoff
Logoff also provides the definition of the message, the sending of the message, the customization of the message processor, and the message queue. In the header file login.h, the message struct message is defined. It has only one type member and the message is defined as follows (see the login.h file, the same below ):
Messagehandler can process the message:
You can extend the message by deriving a subclass. To process a message, you must overload the handlemessage function in the subclass of messagehandler.
They will be encapsulated in the message "envelope" messageenvolope and then sent to the message queue. Messageenvolope is defined as follows:
Logoff maintains a message queue:
You can use sendmessageattime of logoff (the other two versions also use sendmessageattime) to send messages to the Message Queue:
The message sending function packs the parameters into the message envelope and places them in the message queue. Finally, if there is no message in the Message Queue (that is, waiting for sleep on the receiver), let the wake function write characters to the pipeline write end to wake up the sleeping wait on the receiver epoll_wait in the pollinner, start receiving and processing the message. The pollinner code snippet for Message Queue processing is as follows:
After waking up, if the message queue is not empty in row 275 above, check whether the first item in the queue has timed out. If it times out, remove it and call the handlemessage function of messagehandler specified in the message envelope to process the message (line 294 ).
Therefore, with the help of the sendmessagexxx function and pollonce function of logoff, you can send messages to the message queue in logoff. Then, the caller of pollonce will be awakened to process the messages, when the message is processed and there is no message, the system will wait.
This article is excerpted from the book "in-depth analysis of Android system ".
Yang Changgang
Published by Electronic Industry Publishing House