10#ifndef STK_SYNC_MSGQUEUE_H_
11#define STK_SYNC_MSGQUEUE_H_
68 explicit MessageQueue(uint8_t *buf,
size_t capacity,
size_t msg_size);
288 size_t Prev(
size_t idx)
const {
return (idx == 0U) ? (
m_capacity - 1U) : (idx - 1U); }
534template <
size_t N,
size_t MSG>
558 alignas(
alignof(max_align_t)) uint8_t
m_storage[N * MSG];
static __stk_forceinline void STK_MEMCPY(void *const dest, const void *const src, const size_t size)
A wrapper for a built-in memcpy, redefine to your own if required.
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_STATIC_ASSERT_DESC(X, DESC)
Compile-time assertion with a custom error description. Produces a compilation error if X is false.
#define STK_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Implementation of synchronization primitive: stk::sync::ConditionVariable.
Namespace of STK package.
constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
int32_t Timeout
Timeout time (ticks).
constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Synchronization primitives for task coordination and resource protection.
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Condition Variable primitive for signaling between tasks based on specific predicates.
bool TryPeek(void *msg_ptr)
Attempt to peek at the next message without blocking.
bool TryPeekFront(void *msg_ptr)
Attempt to peek at the front message without blocking.
bool IsEmpty() const
Check whether the queue is currently empty.
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.
size_t GetCapacity() const
Get the maximum number of messages the queue can hold.
static const size_t CAPACITY_MAX
Max capacity supported (number of messages).
bool TryPutFront(const void *msg_ptr)
Attempt to put a message into the front of the queue without blocking.
size_t m_tail
read index (next slot to be read by Get())
size_t Next(size_t idx) const
size_t GetMsgSize() const
Get the size of each message in bytes.
bool TryPut(const void *msg_ptr)
Attempt to put a message into the back of the queue without blocking.
~MessageQueue()=default
Destructor.
size_t GetSpace() const
Get the number of free slots currently available.
uint8_t * Slot(size_t idx) const
const size_t m_capacity
maximum number of messages stored in the queue
size_t Prev(size_t idx) const
size_t GetCount() const
Get the current number of messages in the queue.
bool TryGet(void *msg_ptr)
Attempt to get a message from the queue without blocking.
uint8_t * GetBuffer()
Get pointer to the message buffer.
size_t m_head
write index (next slot to be written by Put())
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.
uint8_t * m_buffer
flat byte ring-buffer: capacity slots of msg_size bytes each
ConditionVariable m_cv_not_full
signaled by Get()/Reset() when the queue is no longer full
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 IsStorageValid() const
Verify that the backing storage is valid and the pool is ready for use.
size_t m_count
current number of messages stored in the queue
bool Put(const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Put a message into the back of the queue (FIFO order).
bool Get(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Get a message from the queue.
bool IsFull() const
Check whether the queue is currently full.
ConditionVariable m_cv_not_empty
signaled by Put() when the queue transitions from empty
void Reset()
Discard all messages and reset the queue to the empty state.
MessageQueue(uint8_t *buf, size_t capacity, size_t msg_size)
Constructor.
const size_t m_msg_size
size of each message in bytes
uint8_t m_storage[N *MSG]
flat byte ring-buffer: N slots of MSG bytes each
~MessageQueueT()=default
Destructor.
MessageQueueT()
Constructor.