Mailbox
A mailbox is an object used to achieve synchronization and communication by passing messages in system (shared) memory space. Functions are provided for creating and deleting a mailbox, sending and receiving messages in a mailbox, and referencing the mailbox status. A mailbox is an object identified by an ID number called the mailbox ID.
A mailbox has a message queue for sent messages, and a task queue for tasks waiting to receive messages. At the message sending end (making event notification), messages to be sent go in the message queue. On the message receiving end (waiting for event notification), a task fetches one message from the message queue. If there are no queued messages, the task goes to a state of waiting to receive a message from the mailbox until the next message is sent. Tasks waiting for message receipt from a mailbox are put in the task queue of that mailbox.
Since the contents of messages using this function are in memory shared by the sending and receiving sides, only the start address of a message located in this shared space is actually sent and received. The contents of the messages themselves are not copied. µT-Kernel manages messages in the message queue by means of a link list. At the beginning of a message to be sent, an application program must allocate space for link list use by µT-Kernel. This area is called the message header. The message header and the message body together are called a message packet. When a system call sends a message to a mailbox, the start address of the message packet (pk_msg) is passed in a parameter. When a system call receives a message from a mailbox, the start address of the message packet is passed in a return parameter. If messages are assigned a priority in the message queue, the message priority (msgpri) of each message must be specified in the message header (see Figure 4.2). The user puts the message contents not at the beginning of the packet but after the header part (msgcont in the figure).

Figure 4.2: Format of Messages Using a Mailbox
µT-Kernel overwrites the contents of the header when a message is put in the message queue (except for the message priority area). An application, on the other hand, must not overwrite the header of a message in the queue (including the message priority area). This restriction applies not only to the direct writing of a message header by an application program, but also to the passing of a header address to µT-Kernel and having µT-Kernel overwrite the message header with the contents.
Since the application program allocates the message header space for this mailbox function, there is no limit on the number of messages that can be queued. A system call sending a message does not enter WAIT state.
Memory blocks can be allocated dynamically from a fixed-size memory pool, a variable-size memory pool or a statically allocated area can be used for message packets; however, these must not be located in task space.
Generally, a sending task allocates a memory block from a memory pool, sending that as a message packet. After a task on the receiving end receives the message, it returns the memory block directly to its memory pool.
In µT-Kernel, there is no concept of user space and there is only what T-Kernel calls shared space. Only the start address of the message in shared memory is actually sent and received between the mailboxes. So, it must be noted that the message be placed in shared space instead of user space when used in T-Kernel. In µT-Kernel, awareness of this is not required because everything is placed in shared space.
See the following for more information:

Comments
Click here to Post a Comment