ChibiOS/RT
6.0.3
|
Asynchronous messages.
A mailbox is an asynchronous communication mechanism.
Operations defined for mailboxes:
A message is a variable of type msg_t that is guaranteed to have the same size of and be compatible with (data) pointers (anyway an explicit cast is needed). If larger messages need to be exchanged then a pointer to a structure can be posted in the mailbox but the posting side has no predefined way to know when the message has been processed. A possible approach is to allocate memory (from a memory pool for example) from the posting side and free it on the fetching side. Another approach is to set a "done" flag into the structure pointed by the message.
CH_CFG_USE_MAILBOXES
option must be enabled in chconf.h
. Macros | |
#define | _MAILBOX_DATA(name, buffer, size) |
Data part of a static mailbox initializer. More... | |
#define | MAILBOX_DECL(name, buffer, size) mailbox_t name = _MAILBOX_DATA(name, buffer, size) |
Static mailbox initializer. More... | |
Data Structures | |
struct | mailbox_t |
Structure representing a mailbox object. More... | |
Functions | |
void | chMBObjectInit (mailbox_t *mbp, msg_t *buf, size_t n) |
Initializes a mailbox_t object. More... | |
void | chMBReset (mailbox_t *mbp) |
Resets a mailbox_t object. More... | |
void | chMBResetI (mailbox_t *mbp) |
Resets a mailbox_t object. More... | |
msg_t | chMBPostTimeout (mailbox_t *mbp, msg_t msg, sysinterval_t timeout) |
Posts a message into a mailbox. More... | |
msg_t | chMBPostTimeoutS (mailbox_t *mbp, msg_t msg, sysinterval_t timeout) |
Posts a message into a mailbox. More... | |
msg_t | chMBPostI (mailbox_t *mbp, msg_t msg) |
Posts a message into a mailbox. More... | |
msg_t | chMBPostAheadTimeout (mailbox_t *mbp, msg_t msg, sysinterval_t timeout) |
Posts an high priority message into a mailbox. More... | |
msg_t | chMBPostAheadTimeoutS (mailbox_t *mbp, msg_t msg, sysinterval_t timeout) |
Posts an high priority message into a mailbox. More... | |
msg_t | chMBPostAheadI (mailbox_t *mbp, msg_t msg) |
Posts an high priority message into a mailbox. More... | |
msg_t | chMBFetchTimeout (mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout) |
Retrieves a message from a mailbox. More... | |
msg_t | chMBFetchTimeoutS (mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout) |
Retrieves a message from a mailbox. More... | |
msg_t | chMBFetchI (mailbox_t *mbp, msg_t *msgp) |
Retrieves a message from a mailbox. More... | |
static size_t | chMBGetSizeI (const mailbox_t *mbp) |
Returns the mailbox buffer size as number of messages. More... | |
static size_t | chMBGetUsedCountI (const mailbox_t *mbp) |
Returns the number of used message slots into a mailbox. More... | |
static size_t | chMBGetFreeCountI (const mailbox_t *mbp) |
Returns the number of free message slots into a mailbox. More... | |
static msg_t | chMBPeekI (const mailbox_t *mbp) |
Returns the next message in the queue without removing it. More... | |
static void | chMBResumeX (mailbox_t *mbp) |
Terminates the reset state. More... | |
#define _MAILBOX_DATA | ( | name, | |
buffer, | |||
size | |||
) |
Data part of a static mailbox initializer.
This macro should be used when statically initializing a mailbox that is part of a bigger structure.
[in] | name | the name of the mailbox variable |
[in] | buffer | pointer to the mailbox buffer array of msg_t |
[in] | size | number of msg_t elements in the buffer array |
Definition at line 78 of file chmboxes.h.
#define MAILBOX_DECL | ( | name, | |
buffer, | |||
size | |||
) | mailbox_t name = _MAILBOX_DATA(name, buffer, size) |
Static mailbox initializer.
Statically initialized mailboxes require no explicit initialization using chMBObjectInit()
.
[in] | name | the name of the mailbox variable |
[in] | buffer | pointer to the mailbox buffer array of msg_t |
[in] | size | number of msg_t elements in the buffer array |
Definition at line 98 of file chmboxes.h.
void chMBObjectInit | ( | mailbox_t * | mbp, |
msg_t * | buf, | ||
size_t | n | ||
) |
Initializes a mailbox_t
object.
[out] | mbp | the pointer to the mailbox_t structure to be initialized |
[in] | buf | pointer to the messages buffer as an array of msg_t |
[in] | n | number of elements in the buffer array |
Definition at line 87 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chThdQueueObjectInit(), mailbox_t::cnt, mailbox_t::qr, mailbox_t::qw, mailbox_t::rdptr, mailbox_t::reset, mailbox_t::top, and mailbox_t::wrptr.
Referenced by chFactoryCreateMailbox(), and chFifoObjectInitAligned().
void chMBReset | ( | mailbox_t * | mbp | ) |
Resets a mailbox_t
object.
All the waiting threads are resumed with status MSG_RESET
and the queued messages are lost.
MSG_RESET
until the mailbox is enabled again using chMBResumeX()
.[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 113 of file chmboxes.c.
References chMBResetI(), chSchRescheduleS(), chSysLock(), and chSysUnlock().
void chMBResetI | ( | mailbox_t * | mbp | ) |
Resets a mailbox_t
object.
All the waiting threads are resumed with status MSG_RESET
and the queued messages are lost.
MSG_RESET
until the mailbox is enabled again using chMBResumeX()
.[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 133 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassI(), chThdDequeueAllI(), mailbox_t::cnt, MSG_RESET, mailbox_t::qr, mailbox_t::qw, mailbox_t::rdptr, mailbox_t::reset, and mailbox_t::wrptr.
Referenced by chMBReset().
msg_t chMBPostTimeout | ( | mailbox_t * | mbp, |
msg_t | msg, | ||
sysinterval_t | timeout | ||
) |
Posts a message into a mailbox.
The invoking thread waits until a empty slot in the mailbox becomes available or the specified time runs out.
[in] | mbp | the pointer to an initialized mailbox_t object |
[in] | msg | the message to be posted on the mailbox |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if a message has been correctly posted. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the operation has timed out. |
Definition at line 165 of file chmboxes.c.
References chMBPostTimeoutS(), chSysLock(), and chSysUnlock().
Referenced by chFifoSendObject().
msg_t chMBPostTimeoutS | ( | mailbox_t * | mbp, |
msg_t | msg, | ||
sysinterval_t | timeout | ||
) |
Posts a message into a mailbox.
The invoking thread waits until a empty slot in the mailbox becomes available or the specified time runs out.
[in] | mbp | the pointer to an initialized mailbox_t object |
[in] | msg | the message to be posted on the mailbox |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if a message has been correctly posted. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the operation has timed out. |
Definition at line 194 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassS(), chMBGetFreeCountI(), chSchRescheduleS(), chThdDequeueNextI(), chThdEnqueueTimeoutS(), mailbox_t::cnt, MSG_OK, MSG_RESET, mailbox_t::qr, mailbox_t::qw, mailbox_t::reset, mailbox_t::top, and mailbox_t::wrptr.
Referenced by chFifoSendObjectS(), and chMBPostTimeout().
msg_t chMBPostI | ( | mailbox_t * | mbp, |
msg_t | msg | ||
) |
Posts a message into a mailbox.
This variant is non-blocking, the function returns a timeout condition if the queue is full.
[in] | mbp | the pointer to an initialized mailbox_t object |
[in] | msg | the message to be posted on the mailbox |
MSG_OK | if a message has been correctly posted. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the mailbox is full and the message cannot be posted. |
Definition at line 243 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassI(), chMBGetFreeCountI(), chThdDequeueNextI(), mailbox_t::cnt, MSG_OK, MSG_RESET, MSG_TIMEOUT, mailbox_t::qr, mailbox_t::reset, mailbox_t::top, and mailbox_t::wrptr.
Referenced by chFifoSendObjectI().
msg_t chMBPostAheadTimeout | ( | mailbox_t * | mbp, |
msg_t | msg, | ||
sysinterval_t | timeout | ||
) |
Posts an high priority message into a mailbox.
The invoking thread waits until a empty slot in the mailbox becomes available or the specified time runs out.
[in] | mbp | the pointer to an initialized mailbox_t object |
[in] | msg | the message to be posted on the mailbox |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if a message has been correctly posted. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the operation has timed out. |
Definition at line 290 of file chmboxes.c.
References chMBPostAheadTimeoutS(), chSysLock(), and chSysUnlock().
Referenced by chFifoSendObjectAhead().
msg_t chMBPostAheadTimeoutS | ( | mailbox_t * | mbp, |
msg_t | msg, | ||
sysinterval_t | timeout | ||
) |
Posts an high priority message into a mailbox.
The invoking thread waits until a empty slot in the mailbox becomes available or the specified time runs out.
[in] | mbp | the pointer to an initialized mailbox_t object |
[in] | msg | the message to be posted on the mailbox |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if a message has been correctly posted. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the operation has timed out. |
Definition at line 319 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassS(), chMBGetFreeCountI(), chSchRescheduleS(), chThdDequeueNextI(), chThdEnqueueTimeoutS(), mailbox_t::cnt, MSG_OK, MSG_RESET, mailbox_t::qr, mailbox_t::qw, mailbox_t::rdptr, mailbox_t::reset, and mailbox_t::top.
Referenced by chFifoSendObjectAheadS(), and chMBPostAheadTimeout().
msg_t chMBPostAheadI | ( | mailbox_t * | mbp, |
msg_t | msg | ||
) |
Posts an high priority message into a mailbox.
This variant is non-blocking, the function returns a timeout condition if the queue is full.
[in] | mbp | the pointer to an initialized mailbox_t object |
[in] | msg | the message to be posted on the mailbox |
MSG_OK | if a message has been correctly posted. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the mailbox is full and the message cannot be posted. |
Definition at line 368 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassI(), chMBGetFreeCountI(), chThdDequeueNextI(), mailbox_t::cnt, MSG_OK, MSG_RESET, MSG_TIMEOUT, mailbox_t::qr, mailbox_t::rdptr, mailbox_t::reset, and mailbox_t::top.
Referenced by chFifoSendObjectAheadI().
msg_t chMBFetchTimeout | ( | mailbox_t * | mbp, |
msg_t * | msgp, | ||
sysinterval_t | timeout | ||
) |
Retrieves a message from a mailbox.
The invoking thread waits until a message is posted in the mailbox or the specified time runs out.
[in] | mbp | the pointer to an initialized mailbox_t object |
[out] | msgp | pointer to a message variable for the received message |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if a message has been correctly fetched. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the operation has timed out. |
Definition at line 415 of file chmboxes.c.
References chMBFetchTimeoutS(), chSysLock(), and chSysUnlock().
Referenced by chFifoReceiveObjectTimeout().
msg_t chMBFetchTimeoutS | ( | mailbox_t * | mbp, |
msg_t * | msgp, | ||
sysinterval_t | timeout | ||
) |
Retrieves a message from a mailbox.
The invoking thread waits until a message is posted in the mailbox or the specified time runs out.
[in] | mbp | the pointer to an initialized mailbox_t object |
[out] | msgp | pointer to a message variable for the received message |
[in] | timeout | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if a message has been correctly fetched. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the operation has timed out. |
Definition at line 444 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassS(), chMBGetUsedCountI(), chSchRescheduleS(), chThdDequeueNextI(), chThdEnqueueTimeoutS(), mailbox_t::cnt, MSG_OK, MSG_RESET, mailbox_t::qr, mailbox_t::qw, mailbox_t::rdptr, mailbox_t::reset, and mailbox_t::top.
Referenced by chFifoReceiveObjectTimeoutS(), and chMBFetchTimeout().
msg_t chMBFetchI | ( | mailbox_t * | mbp, |
msg_t * | msgp | ||
) |
Retrieves a message from a mailbox.
This variant is non-blocking, the function returns a timeout condition if the queue is empty.
[in] | mbp | the pointer to an initialized mailbox_t object |
[out] | msgp | pointer to a message variable for the received message |
MSG_OK | if a message has been correctly fetched. |
MSG_RESET | if the mailbox has been reset. |
MSG_TIMEOUT | if the mailbox is empty and a message cannot be fetched. |
Definition at line 493 of file chmboxes.c.
References mailbox_t::buffer, chDbgCheck, chDbgCheckClassI(), chMBGetUsedCountI(), chThdDequeueNextI(), mailbox_t::cnt, MSG_OK, MSG_RESET, MSG_TIMEOUT, mailbox_t::qw, mailbox_t::rdptr, mailbox_t::reset, and mailbox_t::top.
Referenced by chFifoReceiveObjectI().
|
inlinestatic |
Returns the mailbox buffer size as number of messages.
[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 136 of file chmboxes.h.
References mailbox_t::buffer, and mailbox_t::top.
Referenced by chMBGetFreeCountI().
|
inlinestatic |
Returns the number of used message slots into a mailbox.
[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 152 of file chmboxes.h.
References chDbgCheckClassI(), and mailbox_t::cnt.
Referenced by chMBFetchI(), chMBFetchTimeoutS(), and chMBGetFreeCountI().
|
inlinestatic |
Returns the number of free message slots into a mailbox.
[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 167 of file chmboxes.h.
References chDbgCheckClassI(), chMBGetSizeI(), and chMBGetUsedCountI().
Referenced by chMBPostAheadI(), chMBPostAheadTimeoutS(), chMBPostI(), and chMBPostTimeoutS().
|
inlinestatic |
Returns the next message in the queue without removing it.
chMBGetUsedCountI()
and then use this macro, all within a lock state.[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 186 of file chmboxes.h.
References chDbgCheckClassI(), and mailbox_t::rdptr.
|
inlinestatic |
Terminates the reset state.
[in] | mbp | the pointer to an initialized mailbox_t object |
Definition at line 200 of file chmboxes.h.
References mailbox_t::reset.