Bare Metal Programming Tool Kit
|
synchronous handling over of a data item More...
#include <rtos.h>
Public Member Functions | |
mailbox (const char *name="") | |
constructor, specify mailbox name | |
void | write (const T item) |
write an item into the mailbox | |
T | read (void) |
read a value from the mailbox | |
synchronous handling over of a data item
A mailbox is a template class synchronization mechanism. A single value can be written to the mailbox. Another task can read the value from the mailbox. The read and write calls each wait on each other before they are allowed to proceed. A mailbox is not created for a particular task, and it is not a waitable.
Initially a mailbox is empty. The write() operation bloks the calling task until the mailbox is empty, then writes to the mailbox, and blocks. The read() operation blocks the calling task until there is a value in the mailbox. Then it reads the value, unblocks the task that wrote to the mailbox, and returns.
The example below uses a mailbox to buffer writing cout data to the UART. The function that writes each cout character writes it to a mailbox, which is read by the UART task. This task waits 2 MS after each write to the UART, to avoid busy waiting.
|
inline |
|
inline |
read a value from the mailbox
If one or more tasks are waiting for the mutex the first one is released, and it now owns the mutex. Otherwise, if the mutex is cleared it is now set.
It is an error for a task to call signal() on a mutex that it does not own (that it did not call wait() on). After the signal the task no longer owns the mutex.
|
inline |
write an item into the mailbox
If the mutex was set it it is now cleared, and the calling task owns the mutex.
Otherwise the current task waits (is halted) until the owning task calls signal() on the same mutex. The signal() calls will release the tasks in the order of their wait() calls.