Android7.0 MessageQueue detailed _android

Source: Internet
Author: User

The message processing mechanism in Android relies heavily on handler. Each handler has a corresponding looper that is used to continuously remove message processing from the corresponding MessageQueue.

It has always been felt that MessageQueue should be an abstraction of the Java layer, whereas in fact MessageQueue's main part is in the native layer.
I am not familiar with the work of MessageQueue in the native layer, take this opportunity to analyze.

First, the creation of MessageQueue

When you need to use Looper, we call the Looper prepare function:

public static void Prepare () {
 prepare (true);
}

private static void Prepare (Boolean quitallowed) {
 if (sthreadlocal.get ()!= null) {
 throw new RuntimeException (" Only one looper could created per thread ");
 }
 Sthreadlocal is a thread-local store; Each thread has only one looper
 sthreadlocal.set (new Looper (quitallowed))
;

Private Looper (Boolean quitallowed) {
 //create MessageQueue
 mqueue = new MessageQueue (quitallowed);
 Mthread = Thread.CurrentThread ();
}

1 Nativemessagequeue

Let's look at the MessageQueue constructor:

MessageQueue (Boolean quitallowed) {
 mquitallowed = quitallowed;
 The mptr type is long?
 mptr = Nativeinit ();
}

The native function is called in the MessageQueue constructor, and we look at the implementation in Android_os_messagequeue.cpp:

Static Jlong Android_os_messagequeue_nativeinit (jnienv* env, Jclass clazz) {//messagequeue
 layer entity
 nativemessagequeue* nativemessagequeue = new Nativemessagequeue ();
 ............
 This should be similar to converting the pointer to a long type, saving it in the Java layer, and estimating that the Java layer uses a long pointer at the native layer to manipulate the queue return
 Reinterpret_cast<jlong > (nativemessagequeue);
}

We follow up on the Nativemessagequeue constructor:

Nativemessagequeue::nativemessagequeue ():
 mpollenv (null), mpollobj (null), mexceptionobj (null) {
 // Create a native layer of looper, also thread-only
 Mlooper = Looper::getforthread ();
 if (Mlooper = = NULL) {
 mlooper = new Looper (false);
 Looper::setforthread (Mlooper);
 }


From the code point of view, the native layer and the Java layer have Looper objects, should be operating MessageQueue. MessageQueue has its own storage structure at the Java and native levels, storing messages for both the Java and native tiers.

2 native layer of Looper

Let's look at the native layer Looper constructor:

Looper::looper (bool allownoncallbacks):
 mallownoncallbacks (Allownoncallbacks), Msendingmessage (false),
 Mpolling (False), MEPOLLFD ( -1), mepollrebuildrequired (false),
 mnextrequestseq (0), Mresponseindex (0), Mnextmessageuptime (Llong_max) {
 //This creates an FD
 mwakeeventfd = eventfd (0, Efd_nonblock | EFD_CLOEXEC);
 .......
 Rebuildepolllocked ();
}

In the native layer, the rebuildepolllocked function is also called when the Looper in MessageQueue is initialized, and we follow up:

 void looper::rebuildepolllocked () {//close old Epoll instance if we have one.
 if (MEPOLLFD >= 0) {close (MEPOLLFD);
 }//Allocate The new Epoll instance and register the wake pipe.
 MEPOLLFD = Epoll_create (epoll_size_hint);
 ... struct epoll_event eventitem, etc... memset (& eventitem, 0, sizeof (epoll_event));
 Zero out unused members of data field Union eventitem.events = Epollin;
 EVENTITEM.DATA.FD = MWAKEEVENTFD;
 Listen on the MEPOLLFD on the MWAKEEVENTFD whether there is data coming int result = Epoll_ctl (MEPOLLFD, Epoll_ctl_add, MWAKEEVENTFD, & Eventitem);
 ... for (size_t i = 0; i < mrequests.size (); i++) {const request& Request = Mrequests.valueat (i);
 struct Epoll_event eventitem;
 Request.initeventitem (&eventitem);
 Listener request corresponds to the arrival of data on FD int epollresult = Epoll_ctl (MEPOLLFD, Epoll_ctl_add, REQUEST.FD, & Eventitem);
............
 } }

From the looper of the native layer, we know that the native layer relies on Epoll to drive event handling. Here we first keep the approximate image, after the detailed analysis.

Ii. use of MessageQueue

1 Writing messages
Android can either write messages to MessageQueue in the Java layer or write messages to MessageQueue at the native layer. Let's take a look at the corresponding operation process separately.

1.1 Java Layer Write message
The Java layer writes messages to MessageQueue and relies on the Enqueuemessage function:

Boolean Enqueuemessage (msg, long when) {if (Msg.target = = null) {throw new IllegalArgumentException ("message
 Must have a target. ");}
 if (Msg.isinuse ()) {throw new IllegalStateException (msg + "This message being already in use.");
 } synchronized (this) {if (mquitting) {.... return false;
 } 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;
 Insert data in the head, if the previous MessageQueue is blocked, then now need to wake needwake = mblocked; else {//Inserted within the middle of the queue. Usually we don ' t have to wake//up the event queue unless there are a barrier at the head of the queue//and the mess
  The age is the earliest asynchronous the queue.
  Needwake = mblocked && P.target = = null && msg.isasynchronous ();
  Message prev; for (;;)
  {prev = p;
  p = p.next; If p = null | | When <P.when) {break;
  Needwake is set to False if (Needwake && p.isasynchronous ()) {Needwake = False when the first asynchronous message is not; } msg.next = P;
 Invariant:p = = Prev.next Prev.next = msg;
 }//We can assume mptr!= 0 because mquitting is false.
 if (needwake) {nativewake (mptr);

 } return true;}

The above code is simpler, mainly by inserting the newly added message into the original queue by execution time, and then calling the Nativeawake function according to the situation.

Let's follow up on Nativeawake:

void Nativemessagequeue::wake () {
 mlooper->wake ();
}

void Looper::wake () {
 uint64_t inc = 1;
 is to write data to mwakeeventfd
 ssize_t nwrite = temp_failure_retry (Write (mwakeeventfd, &inc, sizeof (uint64_t));
 .............
}

In the Looper initialization of the native layer, we mentioned that the looper of the native layer would use the Epoll to drive the event, in which the Epoll handle was constructed to listen for MWAKEEVENTFD.
When the data is actually fetched from the MessageQueue, the Epoll is used to wait if no data arrives, so when the Java layer writes the message, it wakes up the waiting state of the MessageQueue.
This problem is analyzed again when the message is extracted from the MessageQueue in the following article.

1.2 Native Layer Write message
The native layer writes messages, depending on the SendMessage function of the native layer looper:

void Looper::sendmessage (const sp<messagehandler>& Handler, const message& message) {nsecs_t now = System
 Time (System_time_monotonic);
Sendmessageattime (now, handler, message); } void Looper::sendmessageattime (nsecs_t uptime, const sp<messagehandler>& handler, const message& MESSAG
 e) {size_t i = 0;

 {Automutex _l (mlock);
 Also need to insert size_t Messagecount = Mmessageenvelopes.size () by Time;
 while (I < Messagecount && uptime >= mmessageenvelopes.itemat (i). Uptime) {i = 1;
 ///The message is packaged into a Messageenvelope object Messageenvelope messageenvelope (uptime, handler, message);

 Mmessageenvelopes.insertat (Messageenvelope, I, 1); Optimization:if the Looper is currently sending a message, then we can skip//The call to wake () because the next th ing the looper won't after processing//messages are to decide when the next wakeup time should be.
 In fact, it does//isn't even matter whether this code are running on the looper thread. if (MsendinGmessage) {return;
 }//Wake The poll loop only when we enqueue a new in the head.
 if (i = = 0) {//if inserted in the queue head, also use the Wake function to trigger the Epoll wake Wake ();

 }
}

That's the main process of adding messages to MessageQueue, and then we'll look at the process of taking messages out of MessageQueue.

2. Extract Message
when the Looper object of the Java layer invokes the loop function, it begins to extract the message using the MessageQueue:

public static void Loop () {
 final looper me = Mylooper ();
 .......
 for (;;) {
 msg = Queue.next ();//might block
 ...
 .. The try {
  //Call message processing function handles
  msg.target.dispatchMessage (msg);
 } ........
 }
}

Here we look at the next function of MessageQueue:

Message Next () {//mptr Saves the Nativemessagequeue pointer to the final long ptr = mptr; ... int pendingidlehandlercount =-1;

 -1 only during iteration int nextpolltimeoutmillis = 0; for (;;) {if (nextpolltimeoutmillis!= 0) {//Will call the native function, eventually call the Ipcthread Talkwithdriver, write the data to the binder driver or read the data once//Don't know the rationale for doing this here
  By?
 Binder.flushpendingcommands ();

 //processing native layer data, where the epoll is used for blocked nativepollonce (PTR, nextpolltimeoutmillis);
  Synchronized (this) {final Long now = Systemclock.uptimemillis ();
  Message prevmsg = NULL;
  Message msg = Mmessages; The following is a message that finds the next asynchronous processing type, and the asynchronous processing type message that contains the corresponding execution function if (msg!= null && msg.target = null) {//stalled by a barrier.
  Find the next asynchronous the queue.
   do {prevmsg = msg;
  msg = Msg.next;
  while (msg!= null &&!msg.isasynchronous ()); } if (msg!= null) {if (now < Msg.when) {//Next, not ready.
   Set a timeout to wake the It is ready. Nextpolltimeoutmillis = (int) MAth.min (Msg.when-now, Integer.max_value);
   else {//Got a message.
   mblocked = false;
   The store if (prevmsg!= null) {Prevmsg.next = Msg.next of the next record is completed;
   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 and all pending messages have been handled.
  if (mquitting) {Dispose ();
  return null; The Idlehandler interface was introduced in//messagequeue, that is, when MessageQueue does not have data processing, call Idlehandler to do some work// Pendingidlehandlercount represents the Idlehandler to be processed, starting with-1 if (Pendingidlehandlercount < 0 && (mmessages = = NULL | | no W < Mmessages.when) {//midlehandlers size defaults to 0, calling interface Addidlehandler to increase Pendingidlehandlercount = Midlehandlers.siz
  E (); } if (Pendingidlehandlercount <= 0) {//No idle handlers to run.
  Loop and wait some more.
  Mblocked = true;
  Continue }//Will be pending IDlehandler Add to Pendingidlehandlers if (mpendingidlehandlers = null) {mpendingidlehandlers = new Idlehandler[math.max (
  Pendingidlehandlercount, 4)]; }//Call Arraylist.toarray (t[]) saves the cost of each allocation; After all, for Message.next this call a higher frequency function, can save a little bit mpendingidlehandlers =
 Midlehandlers.toarray (mpendingidlehandlers);
  for (int i = 0; i < Pendingidlehandlercount i++) {final Idlehandler idler = mpendingidlehandlers[i]; Mpendingidlehandlers[i] = null;
  Release of the reference to the handler Boolean keep = false;
  try {//execute the Queueidle function of the implementation class, the return value determines whether the Keep = Idler.queueidle () is persisted;
  catch (Throwable t) {log.wtf (TAG, "Idlehandler threw exception", T);
  } if (!keep) {synchronized (this) {midlehandlers.remove (idler);
 }} pendingidlehandlercount = 0;
 Nextpolltimeoutmillis = 0;

 }
}

The entire process of extracting the message is roughly as shown in the figure above.
As you can see in the Java layer, Looper performs idlehandler-defined functions in the idle period of the queue in addition to removing MessageQueue messages.

2.1 Nativepollonce
Now the only doubt is nativepollonce how to deal with native layer of data, we look at the corresponding native function:

static void Android_os_messagequeue_nativepollonce (jnienv* env, Jobject obj,
 jlong ptr, Jint timeoutmillis) {
 / /Sure enough, when the Java layer calls the native layer MessageQueue, the long type of PTR becomes a pointer
 nativemessagequeue* nativemessagequeue = reinterpret_cast< Nativemessagequeue*> (PTR);
 Nativemessagequeue->pollonce (env, obj, timeoutmillis);
}

void Nativemessagequeue::p ollonce (jnienv* env, jobject pollobj, int timeoutmillis) {
 mpollenv = env;
 Mpollobj = pollobj;
 Finally, the Pollonce function
 mlooper->pollonce (Timeoutmillis)
 is entered into the native layer looper. Mpollobj = NULL;
 mpollenv = NULL;

 if (mexceptionobj) {
 ...
 }}


Look at the Pollonce function of the native layer looper:

Timeoutmillis is the timeout wait time. A value of 1 indicates an infinite wait until an event arrives; a value of 0 o'clock indicates that no wait//outfd is null at this time, meaning that the file handle that generated the event//outevents is null at this point: what events occurred on the storage outfd, including readable, writable, Error and interrupt//outdata is null at this time, meaning that the context data is stored, in fact, the arguments passed in at the time of the call int looper::p ollonce (int timeoutmillis, int* outfd, int* outevents, void*
 * outdata) {int result = 0; for (;;) {//Processing Response, we are not concerned about Response's included while (Mresponseindex < Mresponses.size ()) {const response& Response = MREs
  Ponses.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;
  The operation if (result!= 0) {if (outfd!= NULL) *outfd = 0, based on pollinner results;
  if (outevents!= NULL) *outevents = 0;
  if (outdata!= null) *outdata = NULL;
 return result;
 //The main force or rely on pollinner result = Pollinner (Timeoutmillis);

 }
}

Follow the Pollinner function:

int looper::p ollinner (int timeoutmillis) {//Adjust the timeout based on ' when the next ' is due. Timeoutmillis is the Java Layer Event wait event//native layer maintains the native message wait time//This is actually choosing the minimum wait time if (timeoutmillis!= 0 && Mnextme
  Ssageuptime!= Llong_max) {nsecs_t now = SystemTime (system_time_monotonic);
  int messagetimeoutmillis = Tomillisecondtimeoutdelay (now, mnextmessageuptime); if (messagetimeoutmillis >= 0 && (Timeoutmillis < 0 | | Messagetimeoutmillis < timeoutmillis)) {Timeo
 Utmillis = Messagetimeoutmillis;
 int result = Poll_wake;
 Pollinner initially emptied response mresponses.clear ();

 Mresponseindex = 0;
 We are about to idle.

 Mpolling = true;
 Using Epoll to wait for MEPOLLFD to monitor the event on the handle to reach 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 (); When rebuildepolllocked is invoked, the Epoll handle will be able to listen for the FD if the new join request corresponds to (mEpollrebuildrequired) {mepollrebuildrequired = false;
 Rebuildepolllocked ();
 Goto done;
 }//Check for poll error.
 if (EventCount < 0) {if (errno = = eintr) {goto done;
 ... result = Poll_error;
 Goto done;
 }//Check for poll timeout.
 if (EventCount = = 0) {result = Poll_timeout;
 Goto done; for (int i = 0; i < EventCount i++) {if (fd = = mwakeeventfd) {if (Epollevents & Epollin) {//previous analysis, when J
  When the AVA layer or the native layer has data written to the queue, the MWAKEEVENTFD is written to trigger the Epoll wakeup//awoken to read and empty the data mwakeeventfd on awoken ();
 } else {. ...}
  else {//epoll similarly listens to the request corresponding to the FD 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; Store this FD corresponds to the response Pushresponse (events, Mrequests.valueat (requestindex));
 } else {.. ...}

 }
 }

Done:

 Invoke pending message callbacks.
 Mnextmessageuptime = Llong_max;
 The message while processing the native layer (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 this handler can be deleted *before*//We reacquire our lock.
  {sp<messagehandler> handler = Messageenvelope.handler;
  Message message = Messageenvelope.message;
  Mmessageenvelopes.removeat (0);
  Msendingmessage = true;

  Mlock.unlock ();
  Processing native message handler->handlemessage (message);
  } mlock.lock ();
  Msendingmessage = false;
 result = Poll_callback;
  else {//The last message left at the ' queue determines the ' next wakeup time.
  Mnextmessageuptime = Messageenvelope.uptime;
 Break
}
 }
 Release lock.

 Mlock.unlock (); Process Response for with callback (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;
  Call response callback int callbackresult = Response.request.callback->handleevent (FD, events, data);
  if (Callbackresult = = 0) {removefd (fd, RESPONSE.REQUEST.SEQ);
  } response.request.callback.clear ();
 result = Poll_callback;
} return result;

 }

To tell the truth native layer of code is very messy, the function of more.
As shown in the figure above, use Epoll to listen for data arrival in Nativepollonce, and then process native message, native response.

Finally, let's see how to add a request to the native layer.

3 Adding a monitoring request
The native layer adds request dependencies to the Looper interface ADDFD:

FD indicates that the handle that needs to be tapped
//ident is not clear.
//events represents events that need to be monitored, such as event_input, Event_output, Event_error, and Event_ One or more//callback in Hangup
for the callback function after the event
//data the parameter int LOOPER::ADDFD for the callback function
(int fd, int ident, int events, Looper_callbackfunc callback, void* data) {return
 ADDFD (FD, ident, events, callback? New Simpleloopercallback (CALLB ACK): NULL, data);


In conjunction with the native Layer polling queue above, we can generally know that the purpose of the ADDFD is to let the native layer Looper monitor whether a specified event occurs on the newly added FD.
If a specified event occurs, the corresponding response is constructed using a callback function and a parameter.
Native layer of Looper processing response, you can perform the corresponding callback function.

Look at the actual code:

int looper::addfd (int fd, int ident, int events, const sp<loopercallback>& callback, void* data) {...

 Automutex _l (Mlock);
 Using parameters to construct a request request request;
 REQUEST.FD = FD;
 Request.ident = ident;
 Request.events = events;
 Request.seq = mnextrequestseq++;
 Request.callback = callback;
 Request.data = data; if (mnextrequestseq = = 1) mnextrequestseq = 0;
 Reserve sequence number-1 struct epoll_event eventitem;

 Request.initeventitem (&eventitem);
 To determine whether prior use of the FD has been constructed by request ssize_t Requestindex = Mrequests.indexofkey (FD); if (Requestindex < 0) {//MEPOLLFD Add a need to listen for FD int epollresult = Epoll_ctl (MEPOLLFD, Epoll_ctl_add, FD, & Eventite
  m);
 ... mrequests.add (fd, request);
  else {//MEPOLLFD modifies old FD corresponding listener event int epollresult = Epoll_ctl (MEPOLLFD, Epoll_ctl_mod, FD, & Eventitem); if (Epollresult < 0) {if (errno = = enoent) {//tolerate enoent because it means that. older file descriptor WA S//closed before its callBack were unregistered and meanwhile a new//file descriptor with the same number has been created and are now//bei 
   NG registered for the.
   Epollresult = Epoll_ctl (MEPOLLFD, Epoll_ctl_add, FD, & Eventitem);
  .......
  }
  When an error is epollrebuildlocked, schedule the EPOLLFD to add a second FD scheduleepollrebuildlocked () to be monitored;
 } mrequests.replacevalueat (Requestindex, request);

 }
 }
}

The process of joining a monitoring request, as described above in the Pollinner function, is not discussed here.

Third, summary

1. Process Summary


The whole process of MessageQueue includes the Java part and the native part, from which we can see that the proportion of native layer is still very large. In conjunction with the above picture, we recall the entire MessageQueue corresponding processing process:
1. When the Java layer creates the Looper object, the native of MessageQueue layer is created using the native function when the MessageQueue initialization of the Messagequeue;java layer of the Java layer is created.

2. After the MessageQueue initialization of the native layer, the corresponding native Looper object is created. When the native object is initialized, the corresponding EPOLLFD and WAKEEVENTFD are created. In this case, EPOLLFD will act as a Epoll listener, initially EPOLLFD only monitor wakeeventfd.

3, the red line in the figure for looper from the MessageQueue when the message, processing logic flow.
3.1, when the Java layer Looper began to cycle, the first need to call through the JNI function native Looper pollonce operation.

3.2, Native looper start running, you need to wait for EPOLLFD to be awakened. Native Looper can begin to handle events when EPOLLFD waits for a timeout or the listener's handle has an event coming.

3.3, in the Native layer, Native Looper will first process the message in the Native MessageQueue, and then call the corresponding callback function response.

3.4, in this cycle, the native Layer event processing, only to start processing the Java Layer MessageQueue message. If there is no message to be processed in the MessageQueue and Idlehandler is present in the MessageQueue, the Idlehandler-defined handler function is invoked.

The blue portion of the figure is called for the corresponding function:
in the Java layer:
The use of MessageQueue Addidlehandler can increase idlehandler for MessageQueue;
Using MessageQueue's Enqueuemessage, you can add messages to MessageQueue and, if necessary, write messages to native layer wakeeventfd using the native function to wake up EPOLLFD.

in the native layer:
Using Looper:sendmessage, you can add messages for native MessageQueue, and in the same way, you will write messages to the native layer wakeeventfd to wake up EPOLLFD;
Using LOOPER:ADDFD, you can register the listening request to the native Looper, the listening request contains the FD, the monitoring event and the corresponding callback function, etc., and the corresponding FD of the listening request will be the object of EPOLLFD listening. When the corresponding event of the monitored FD occurs, the EPOLLFD will be awakened, and the corresponding response join response list will be generated, waiting for processing. Once the response is processed, the corresponding callback function is invoked.

2. Attention Matters
MessageQueue have their own storage structure in the Java and native layers, and can add messages separately. From the processing logic, it takes precedence over the message of the native layer, then handles the response generated by the native layer, and finally the message that processes the Java layer.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.