![]() |
SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Fixed-capacity, fixed-message-size FIFO queue for inter-task communication. More...
#include <stk_sync_msgqueue.h>
Public Member Functions | |
| MessageQueue (uint8_t *buf, size_t capacity, size_t msg_size) | |
| Constructor. | |
| ~MessageQueue ()=default | |
| Destructor. | |
| bool | Put (const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE) |
| Put a message into the back of the queue (FIFO order). | |
| bool | TryPut (const void *msg_ptr) |
| Attempt to put a message into the back of the queue without blocking. | |
| bool | PutFront (const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE) |
| Put a message into the front of the queue (LIFO / priority-insert order). | |
| bool | TryPutFront (const void *msg_ptr) |
| Attempt to put a message into the front of the queue without blocking. | |
| bool | Get (void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE) |
| Get a message from the queue. | |
| bool | TryGet (void *msg_ptr) |
| Attempt to get a message from the queue without blocking. | |
| bool | Peek (void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE) |
| Peek at the next message to be delivered (back of the FIFO) without removing it. | |
| bool | TryPeek (void *msg_ptr) |
| Attempt to peek at the next message without blocking. | |
| bool | PeekFront (void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE) |
| Peek at the most recently front-inserted message (front of the FIFO) without removing it. | |
| bool | TryPeekFront (void *msg_ptr) |
| Attempt to peek at the front message without blocking. | |
| void | Reset () |
| Discard all messages and reset the queue to the empty state. | |
| size_t | GetCapacity () const |
| Get the maximum number of messages the queue can hold. | |
| size_t | GetMsgSize () const |
| Get the size of each message in bytes. | |
| size_t | GetCount () const |
| Get the current number of messages in the queue. | |
| size_t | GetSpace () const |
| Get the number of free slots currently available. | |
| uint8_t * | GetBuffer () |
| Get pointer to the message buffer. | |
| bool | IsEmpty () const |
| Check whether the queue is currently empty. | |
| bool | IsFull () const |
| Check whether the queue is currently full. | |
| bool | IsStorageValid () const |
| Verify that the backing storage is valid and the pool is ready for use. | |
| void | SetTraceName (const char *name) |
| Set name. | |
| const char * | GetTraceName () const |
| Get name. | |
Static Public Attributes | |
| static const size_t | CAPACITY_MAX = 0xFFFEU |
| Max capacity supported (number of messages). | |
Private Member Functions | |
| MessageQueue (const MessageQueue &)=delete | |
| MessageQueue & | operator= (const MessageQueue &)=delete |
| uint8_t * | Slot (size_t idx) const |
| size_t | Next (size_t idx) const |
| size_t | Prev (size_t idx) const |
Private Attributes | |
| uint8_t * | m_buffer |
| flat byte ring-buffer: capacity slots of msg_size bytes each | |
| const size_t | m_capacity |
| maximum number of messages stored in the queue | |
| const size_t | m_msg_size |
| size of each message in bytes | |
| size_t | m_count |
| current number of messages stored in the queue | |
| size_t | m_head |
| write index (next slot to be written by Put()) | |
| size_t | m_tail |
| read index (next slot to be read by Get()) | |
| ConditionVariable | m_cv_not_empty |
| signaled by Put() when the queue transitions from empty | |
| ConditionVariable | m_cv_not_full |
| signaled by Get()/Reset() when the queue is no longer full | |
Fixed-capacity, fixed-message-size FIFO queue for inter-task communication.
MessageQueue provides a synchronized ring-buffer that transports opaque, fixed-size byte messages between tasks. It follows the following blocking semantics:
Put() blocks if the queue is full until space becomes available or the timeout expires.Get() blocks if the queue is empty until a message is produced or the timeout expires.Unlike stk::sync::Pipe, which is parameterised on an element type, MessageQueue is parameterised on a byte count (MSG). This makes it suitable for passing heterogeneous or C-ABI structs without requiring the message type to be copyable via the C++ assignment operator. The message payload is always copied with memcpy.
Definition at line 55 of file stk_sync_msgqueue.h.
|
inlineexplicit |
Constructor.
| [in] | buf | Pointer to the externally-allocated storage. Must be at least capacity * msg_size bytes. |
| [in] | capacity | Maximum number of messages [1, CAPACITY_MAX]. |
| [in] | msg_size | Size of each message in bytes (>= 1). |
Definition at line 304 of file stk_sync_msgqueue.h.
References CAPACITY_MAX, m_buffer, m_capacity, m_count, m_head, m_msg_size, m_tail, and STK_ASSERT.
Referenced by MessageQueue(), and stk::sync::MessageQueueT< N, MSG >::MessageQueueT().
|
default |
Destructor.
References STK_VIRT_DTOR, and stk::WAIT_INFINITE.
|
privatedelete |
|
inline |
Get a message from the queue.
Copies msg_size bytes from the oldest slot in the ring buffer into the buffer pointed to by msg_ptr. If the queue is empty the calling task is suspended until a message is produced or the timeout expires.
| [out] | msg_ptr | Destination buffer for the retrieved message (must be at least msg_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for a message (ticks). Use WAIT_INFINITE to block indefinitely, NO_WAIT for a non-blocking attempt. |
NO_WAIT, ISR-unsafe otherwise. true if a message was successfully retrieved, false if the timeout expired before a message was available. Definition at line 391 of file stk_sync_msgqueue.h.
References m_count, m_cv_not_empty, m_cv_not_full, m_msg_size, m_tail, Next(), Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_msgq_get(), TryGet(), xMessageBufferReceive(), and xQueueSelectFromSet().
|
inline |
Get pointer to the message buffer.
Definition at line 253 of file stk_sync_msgqueue.h.
References m_buffer.
Referenced by osMessageQueueNew(), and stk_msgq_get_buffer().
|
inline |
Get the maximum number of messages the queue can hold.
Definition at line 228 of file stk_sync_msgqueue.h.
References m_capacity.
Referenced by osMessageQueueGetCapacity(), and stk_msgq_get_capacity().
|
inline |
Get the current number of messages in the queue.
size_t-aligned read is atomic. Definition at line 241 of file stk_sync_msgqueue.h.
References m_count.
Referenced by osMessageQueueGetCount(), stk_msgq_get_count(), uxQueueMessagesWaiting(), and uxQueueMessagesWaitingFromISR().
|
inline |
Get the size of each message in bytes.
Definition at line 234 of file stk_sync_msgqueue.h.
References m_msg_size.
Referenced by osMessageQueueGetMsgSize(), and stk_msgq_get_msg_size().
|
inline |
Get the number of free slots currently available.
Definition at line 247 of file stk_sync_msgqueue.h.
References m_capacity, and m_count.
Referenced by osMessageQueueGetSpace(), stk_msgq_get_space(), and uxQueueSpacesAvailable().
|
inlineinherited |
Get name.
NULL if not set or if STK_SYNC_DEBUG_NAMES is 0. Definition at line 515 of file stk_common.h.
|
inline |
Check whether the queue is currently empty.
true if the queue contains no messages. Definition at line 259 of file stk_sync_msgqueue.h.
References m_count.
Referenced by stk_msgq_is_empty(), xQueueAddToSet(), and xQueueRemoveFromSet().
|
inline |
Check whether the queue is currently full.
true if the queue contains capacity messages. Definition at line 265 of file stk_sync_msgqueue.h.
References m_capacity, and m_count.
Referenced by stk_msgq_is_full().
|
inline |
Verify that the backing storage is valid and the pool is ready for use.
Always true for pools constructed with external storage. For heap-constructed queue, false if operator new failed. Must be checked after the heap constructor when operating without exceptions (the typical embedded configuration).
true if the queue is ready for use. Definition at line 275 of file stk_sync_msgqueue.h.
References m_buffer.
Referenced by stk_msgq_is_storage_valid(), xMessageBufferCreate(), xMessageBufferCreateWithCallback(), and xQueueCreate().
|
inlineprivate |
Definition at line 284 of file stk_sync_msgqueue.h.
References m_capacity.
Referenced by Get(), and Put().
|
privatedelete |
|
inline |
Peek at the next message to be delivered (back of the FIFO) without removing it.
Copies msg_size bytes from the oldest slot in the ring buffer into the buffer pointed to by msg_ptr, leaving the message in place so that a subsequent Get() will return the same message. If the queue is empty the calling task is suspended until a message is produced or the timeout expires.
| [out] | msg_ptr | Destination buffer for the peeked message (must be at least msg_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for a message (ticks). Use WAIT_INFINITE to block indefinitely, NO_WAIT for a non-blocking attempt. |
NO_WAIT, ISR-unsafe otherwise. true if a message was successfully peeked, false if the timeout expired before a message was available. Definition at line 423 of file stk_sync_msgqueue.h.
References m_count, m_cv_not_empty, m_msg_size, m_tail, Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_msgq_peek(), and TryPeek().
|
inline |
Peek at the most recently front-inserted message (front of the FIFO) without removing it.
Copies msg_size bytes from the slot immediately before the current write pointer (i.e. the message that PutFront() most recently placed) into the buffer pointed to by msg_ptr, leaving the message in place. If the queue is empty the calling task is suspended until a message is produced or the timeout expires.
| [out] | msg_ptr | Destination buffer for the peeked message (must be at least msg_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for a message (ticks). Use WAIT_INFINITE to block indefinitely, NO_WAIT for a non-blocking attempt. |
NO_WAIT, ISR-unsafe otherwise. true if a message was successfully peeked, false if the timeout expired before a message was available. Definition at line 453 of file stk_sync_msgqueue.h.
References m_count, m_cv_not_empty, m_msg_size, m_tail, Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_msgq_peekfront(), and TryPeekFront().
|
inlineprivate |
Definition at line 288 of file stk_sync_msgqueue.h.
References m_capacity.
Referenced by PutFront().
|
inline |
Put a message into the back of the queue (FIFO order).
Copies msg_size bytes from msg_ptr into the next available slot in the ring buffer. If the queue is full the calling task is suspended until space becomes available or the timeout expires.
| [in] | msg_ptr | Pointer to the message payload (must be at least msg_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for a free slot (ticks). Use WAIT_INFINITE to block indefinitely, NO_WAIT for a non-blocking attempt. |
NO_WAIT, ISR-unsafe otherwise. true if the message was successfully enqueued, false if the timeout expired before space became available. Definition at line 322 of file stk_sync_msgqueue.h.
References CAPACITY_MAX, m_capacity, m_count, m_cv_not_empty, m_cv_not_full, m_head, m_msg_size, Next(), Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_msgq_put(), TryPut(), xMessageBufferSend(), and xQueueSend().
|
inline |
Put a message into the front of the queue (LIFO / priority-insert order).
Copies msg_size bytes from msg_ptr into the slot immediately before the current read pointer, making it the next message that Get() will return. If the queue is full the calling task is suspended until space becomes available or the timeout expires.
| [in] | msg_ptr | Pointer to the message payload (must be at least msg_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for a free slot (ticks). Use WAIT_INFINITE to block indefinitely, NO_WAIT for a non-blocking attempt. |
NO_WAIT, ISR-unsafe otherwise. true if the message was successfully enqueued at the front, false if the timeout expired before space became available. Definition at line 355 of file stk_sync_msgqueue.h.
References CAPACITY_MAX, m_capacity, m_count, m_cv_not_empty, m_cv_not_full, m_msg_size, m_tail, Prev(), Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_msgq_putfront(), TryPutFront(), and xQueueSendToFront().
|
inline |
Discard all messages and reset the queue to the empty state.
Resets the head, tail and count to zero. Any tasks blocked in Put() are woken so they can re-evaluate and enqueue their messages into the now-empty queue.
Definition at line 484 of file stk_sync_msgqueue.h.
References m_count, m_cv_not_full, m_head, and m_tail.
Referenced by stk_msgq_reset(), xMessageBufferReset(), xMessageBufferResetFromISR(), xQueueOverwrite(), and xQueueOverwriteFromISR().
|
inlineinherited |
Set name.
| [in] | name | Null-terminated string or NULL. |
Definition at line 503 of file stk_common.h.
References STK_UNUSED.
Referenced by stk::memory::BlockMemoryPool::BlockMemoryPool(), and stk::memory::BlockMemoryPool::BlockMemoryPool().
|
inlineprivate |
Definition at line 281 of file stk_sync_msgqueue.h.
References m_buffer, and m_msg_size.
Referenced by Get(), Peek(), PeekFront(), Put(), and PutFront().
|
inline |
Attempt to get a message from the queue without blocking.
Dequeues a message only if one is immediately available. Returns false instantly if the queue is empty.
| [out] | msg_ptr | Destination buffer for the retrieved message. |
true if a message was retrieved, false if the queue was empty. Definition at line 154 of file stk_sync_msgqueue.h.
References Get(), and stk::NO_WAIT.
Referenced by stk_msgq_tryget(), xMessageBufferReceiveFromISR(), xMessageBufferReset(), xMessageBufferResetFromISR(), and xQueueSelectFromSetFromISR().
|
inline |
Attempt to peek at the next message without blocking.
Copies the oldest message into msg_ptr only if one is immediately available. The message is not removed from the queue. Returns false instantly if the queue is empty.
| [out] | msg_ptr | Destination buffer for the peeked message. |
true if a message was peeked, false if the queue was empty. Definition at line 183 of file stk_sync_msgqueue.h.
References stk::NO_WAIT, and Peek().
Referenced by stk_msgq_trypeek(), and xMessageBufferNextLengthBytes().
|
inline |
Attempt to peek at the front message without blocking.
Copies the most recently front-inserted message into msg_ptr only if one is immediately available. The message is not removed from the queue. Returns false instantly if the queue is empty.
| [out] | msg_ptr | Destination buffer for the peeked message. |
true if a message was peeked, false if the queue was empty. Definition at line 212 of file stk_sync_msgqueue.h.
References stk::NO_WAIT, and PeekFront().
Referenced by stk_msgq_trypeekfront().
|
inline |
Attempt to put a message into the back of the queue without blocking.
Enqueues the message only if a free slot is immediately available. Returns false instantly if the queue is full.
| [in] | msg_ptr | Pointer to the message payload. |
true if the message was enqueued, false if the queue was full. Definition at line 101 of file stk_sync_msgqueue.h.
References stk::NO_WAIT, and Put().
Referenced by stk_msgq_tryput(), xMessageBufferReceive(), xMessageBufferSendFromISR(), xQueueOverwrite(), xQueueOverwriteFromISR(), and xQueueSendFromISR().
|
inline |
Attempt to put a message into the front of the queue without blocking.
Enqueues the message at the front only if a free slot is immediately available. Returns false instantly if the queue is full.
| [in] | msg_ptr | Pointer to the message payload. |
true if the message was enqueued at the front, false if the queue was full. Definition at line 128 of file stk_sync_msgqueue.h.
References stk::NO_WAIT, and PutFront().
Referenced by stk_msgq_tryputfront(), xMessageBufferReceiveFromISR(), and xQueueSendToFrontFromISR().
|
static |
Max capacity supported (number of messages).
Definition at line 60 of file stk_sync_msgqueue.h.
Referenced by MessageQueue(), osMessageQueueNew(), Put(), PutFront(), xQueueCreate(), xQueueCreateSet(), and xQueueCreateStatic().
|
private |
flat byte ring-buffer: capacity slots of msg_size bytes each
Definition at line 290 of file stk_sync_msgqueue.h.
Referenced by GetBuffer(), IsStorageValid(), MessageQueue(), and Slot().
|
private |
maximum number of messages stored in the queue
Definition at line 291 of file stk_sync_msgqueue.h.
Referenced by GetCapacity(), GetSpace(), IsFull(), MessageQueue(), Next(), Prev(), Put(), and PutFront().
|
private |
current number of messages stored in the queue
Definition at line 293 of file stk_sync_msgqueue.h.
Referenced by Get(), GetCount(), GetSpace(), IsEmpty(), IsFull(), MessageQueue(), Peek(), PeekFront(), Put(), PutFront(), and Reset().
|
private |
signaled by Put() when the queue transitions from empty
Definition at line 296 of file stk_sync_msgqueue.h.
Referenced by Get(), Peek(), PeekFront(), Put(), and PutFront().
|
private |
signaled by Get()/Reset() when the queue is no longer full
Definition at line 297 of file stk_sync_msgqueue.h.
Referenced by Get(), Put(), PutFront(), and Reset().
|
private |
write index (next slot to be written by Put())
Definition at line 294 of file stk_sync_msgqueue.h.
Referenced by MessageQueue(), Put(), and Reset().
|
private |
size of each message in bytes
Definition at line 292 of file stk_sync_msgqueue.h.
Referenced by Get(), GetMsgSize(), MessageQueue(), Peek(), PeekFront(), Put(), PutFront(), and Slot().
|
private |
read index (next slot to be read by Get())
Definition at line 295 of file stk_sync_msgqueue.h.
Referenced by Get(), MessageQueue(), Peek(), PeekFront(), PutFront(), and Reset().