Message passing

From Embedded Xinu
Revision as of 22:05, 23 June 2008 by Amallen (talk | contribs) (made changes to reflect the discovery that "send" is actually non-blocking)
Jump to navigation Jump to search

Message passing is one method used by XINU threads for interprocess communication. It allows threads to send individual messages to other threads by using the system calls send and receive. Each thread has memory allocated for a single message in its thread control block specifically for messages sent and received using this method. This form of message passing should not be confused with the mailbox messaging queue system also used for interprocess communication.

Upon creation, each thread is allocated memory in its thread control block for two fields which apply to this message passing system: a 4 byte (int type) message box to contain a single message sent to this thread, and a one byte flag (bool type) to signal if there is an unreceived message waiting in that thread's message box.

Threads use the functions send(tid_typ tid, int msg) and receive(bool block) to utilize this system of message passing.

send(tid_typ tid, int msg) delivers the message passed in as the parameter msg to the message box in the thread control block of the thread with a thread id of tid, also passed in as a parameter. send will always yield the processor by calling reschedule if the receiving thread was in a state of waiting to receive a message (THRRECV).

receive(bool block) returns the message waiting in the message box of the thread control block of the thread which called receive. If there is no message waiting for the thread then one of two things will happen depending on if the receive call was blocking or non-blocking. If it was blocking then the thread will go into a state of waiting to receive a message (THRRECV) until a message is passed to the thread. If it was non-blocking then receive will simply return NULL signifying that there was no message waiting for the thread.