![]() |
SuperTinyKernel™ RTOS 1.06.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Thread-safe FIFO communication pipe for inter-task data passing. More...
#include <stk_sync_pipe.h>
Public Member Functions | |
| Pipe (uint8_t *buf, size_t capacity, size_t element_size) | |
| Constructor. | |
| ~Pipe ()=default | |
| Destructor. | |
| bool | Write (const void *data, Timeout timeout_ticks=WAIT_INFINITE) |
| Write a single element to the pipe. | |
| bool | TryWrite (const void *data) |
| Attempt to write a single element to the pipe without blocking. | |
| size_t | WriteBulk (const void *src, size_t count, Timeout timeout_ticks=WAIT_INFINITE) |
| Write multiple elements to the pipe. | |
| size_t | TryWriteBulk (const void *src, size_t count) |
| Attempt to write multiple elements to the pipe without blocking. | |
| bool | Read (void *data, Timeout timeout_ticks=WAIT_INFINITE) |
| Read a single element from the pipe. | |
| bool | TryRead (void *data) |
| Attempt to read a single element from the pipe without blocking. | |
| size_t | ReadBulk (void *dst, size_t count, Timeout timeout_ticks=WAIT_INFINITE) |
| Read multiple elements from the pipe. | |
| size_t | TryReadBulk (void *dst, size_t count) |
| Attempt to read multiple elements from the pipe without blocking. | |
| size_t | ReadBulkTriggered (void *dst, size_t trigger, size_t max_count, Timeout timeout_ticks=WAIT_INFINITE) |
| Read at least trigger elements, then drain up to max_count without blocking. | |
| size_t | TryReadBulkTriggered (void *dst, size_t max_count) |
| Non-blocking variant of ReadBulkTriggered. | |
| void | Reset () |
| Discard all elements and reset the pipe to the empty state. | |
| size_t | GetCapacity () const |
| Get the maximum number of elements the pipe can hold. | |
| size_t | GetElementSize () const |
| Get the size of each element in bytes. | |
| size_t | GetCount () const |
| Get the current number of elements in the pipe. | |
| size_t | GetSpace () const |
| Get the number of free slots currently available. | |
| uint8_t * | GetBuffer () |
| Get a pointer to the raw backing buffer. | |
| bool | IsEmpty () const |
| Check if the pipe is currently empty. | |
| bool | IsFull () const |
| Check if the pipe is currently full. | |
| bool | IsStorageValid () const |
| Verify that the backing storage is valid and the pipe 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 elements). | |
Private Member Functions | |
| Pipe (const Pipe &)=delete | |
| Pipe & | operator= (const Pipe &)=delete |
| uint8_t * | Slot (size_t idx) const |
| size_t | Next (size_t idx) const |
| size_t | DrainLocked (uint8_t *dst_bytes, size_t count) |
Private Attributes | |
| uint8_t * | m_buffer |
| flat byte ring-buffer: capacity slots of element_size bytes each | |
| const size_t | m_capacity |
| maximum number of elements stored in the pipe | |
| const size_t | m_element_size |
| size of each element in bytes | |
| size_t | m_count |
| current number of elements stored in the pipe | |
| size_t | m_head |
| write index (next slot to be written by Write()) | |
| size_t | m_tail |
| read index (next slot to be read by Read()) | |
| ConditionVariable | m_cv_not_empty |
| signaled by Write() when the pipe transitions from empty | |
| ConditionVariable | m_cv_not_full |
| signaled by Read()/Reset() when the pipe is no longer full | |
Thread-safe FIFO communication pipe for inter-task data passing.
Pipe provides a synchronized ring-buffer mechanism that allows tasks to exchange data safely. It is parameterised on an element byte count (element_size) rather than a C++ type, making it suitable for passing heterogeneous or C-ABI structs without requiring the element type to be copyable via the C++ assignment operator. All element payloads are copied with memcpy.
It follows the following blocking semantics:
Write() blocks if the pipe is full until space becomes available or the timeout expires.Read() blocks if the pipe is empty until data is produced or the timeout expires.The caller is responsible for providing an appropriately sized external buffer and ensuring it remains valid for the entire lifetime of the Pipe object. For a self-contained, type-safe alternative with compile-time capacity and direct typed assignment, see stk::sync::PipeT.
Definition at line 71 of file stk_sync_pipe.h.
|
inlineexplicit |
Constructor.
| [in] | buf | Pointer to externally-allocated storage. Must be at least capacity * element_size bytes. |
| [in] | capacity | Maximum number of elements [1, CAPACITY_MAX]. |
| [in] | element_size | Size of each element in bytes (>= 1). |
Definition at line 330 of file stk_sync_pipe.h.
References CAPACITY_MAX, m_buffer, m_capacity, m_count, m_element_size, m_head, m_tail, and STK_ASSERT.
Referenced by operator=().
|
default |
Destructor.
References STK_VIRT_DTOR, and stk::WAIT_INFINITE.
|
privatedelete |
|
inlineprivate |
Definition at line 487 of file stk_sync_pipe.h.
References m_capacity, m_count, m_cv_not_full, m_element_size, m_tail, stk::Min(), Slot(), and STK_MEMCPY().
Referenced by ReadBulk(), and ReadBulkTriggered().
|
inline |
Get a pointer to the raw backing buffer.
Definition at line 277 of file stk_sync_pipe.h.
References m_buffer.
|
inline |
Get the maximum number of elements the pipe can hold.
Definition at line 252 of file stk_sync_pipe.h.
References m_capacity.
Referenced by stk_pipe_get_capacity(), and xStreamBufferSetTriggerLevel().
|
inline |
Get the current number of elements in the pipe.
Definition at line 265 of file stk_sync_pipe.h.
References m_count.
Referenced by stk_pipe_get_count().
|
inline |
Get the size of each element in bytes.
Definition at line 258 of file stk_sync_pipe.h.
References m_element_size.
Referenced by stk_pipe_get_element_size().
|
inline |
Get the number of free slots currently available.
Definition at line 271 of file stk_sync_pipe.h.
References m_capacity, and m_count.
Referenced by stk_pipe_get_space().
|
inlineinherited |
Get name.
NULL if not set or if STK_SYNC_DEBUG_NAMES is 0. Definition at line 416 of file stk_common.h.
|
inline |
Check if the pipe is currently empty.
true if empty, otherwise false. Definition at line 284 of file stk_sync_pipe.h.
References m_count.
Referenced by stk_pipe_is_empty().
|
inline |
Check if the pipe is currently full.
true if full, otherwise false. Definition at line 291 of file stk_sync_pipe.h.
References m_capacity, and m_count.
Referenced by stk_pipe_is_full().
|
inline |
Verify that the backing storage is valid and the pipe is ready for use.
Always true for pipes constructed with external storage. For heap-constructed pipes, false if operator new failed. Must be checked after the heap constructor when operating without exceptions (the typical embedded configuration).
true if the pipe is ready for use. Definition at line 301 of file stk_sync_pipe.h.
References m_buffer.
Referenced by stk_pipe_is_storage_valid().
|
inlineprivate |
Definition at line 310 of file stk_sync_pipe.h.
References m_capacity.
Referenced by Read(), and Write().
|
inline |
Read a single element from the pipe.
Copies element_size bytes from the oldest slot in the ring buffer into the buffer pointed to by data. If the pipe is empty, the calling task is suspended until data is produced or the timeout expires.
| [out] | data | Destination buffer for the retrieved element (must be at least element_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for data (ticks). Default: WAIT_INFINITE. |
true if data was successfully read, false if timeout expired before any data became available. Definition at line 454 of file stk_sync_pipe.h.
References m_count, m_cv_not_empty, m_cv_not_full, m_element_size, m_tail, Next(), Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_pipe_read(), and TryRead().
|
inline |
Read multiple elements from the pipe.
Attempts to retrieve count elements from the FIFO. If the pipe does not contain enough elements to satisfy the request, it will block until the full amount is read or the timeout expires.
| [out] | dst | Pointer to the destination array (must hold at least count elements of element_size bytes each). |
| [in] | count | Number of elements to read. |
| [in] | timeout_ticks | Maximum time to wait for data (ticks). Default: WAIT_INFINITE. |
count unless a timeout occurred. Definition at line 513 of file stk_sync_pipe.h.
References DrainLocked(), stk::GetTicks(), m_count, m_cv_not_empty, m_element_size, stk::NO_WAIT, and stk::WAIT_INFINITE.
Referenced by stk_pipe_read_bulk(), and TryReadBulk().
|
inline |
Read at least trigger elements, then drain up to max_count without blocking.
Blocks until at least trigger elements are simultaneously available in the pipe, or the timeout expires. Once the threshold is reached the call dequeues min(max_count, available) elements in a single critical-section pass — no busy-spin, no second call.
| [out] | dst | Destination buffer; must hold at least max_count elements of element_size bytes. |
| [in] | trigger | Minimum number of elements that must be available before any data is dequeued. Clamped to [1, max_count] internally. |
| [in] | max_count | Maximum number of elements to return in total. |
| [in] | timeout_ticks | Maximum time to wait for trigger elements. Default: WAIT_INFINITE. |
Definition at line 567 of file stk_sync_pipe.h.
References DrainLocked(), stk::GetTicks(), m_count, m_cv_not_empty, stk::NO_WAIT, and stk::WAIT_INFINITE.
Referenced by stk_pipe_read_bulk_triggered(), TryReadBulkTriggered(), and xStreamBufferReceive().
|
inline |
Discard all elements and reset the pipe to the empty state.
Resets head, tail and count to zero. Any tasks blocked in Write() are woken so they can re-evaluate and enqueue into the now-empty pipe.
Definition at line 614 of file stk_sync_pipe.h.
References m_count, m_cv_not_full, m_head, and m_tail.
Referenced by stk_pipe_reset().
|
inlineinherited |
Set name.
| [in] | name | Null-terminated string or NULL. |
Definition at line 404 of file stk_common.h.
References STK_UNUSED.
Referenced by stk::memory::BlockMemoryPool::BlockMemoryPool(), and stk::memory::BlockMemoryPool::BlockMemoryPool().
|
inlineprivate |
Definition at line 307 of file stk_sync_pipe.h.
References m_buffer, and m_element_size.
Referenced by DrainLocked(), Read(), Write(), and WriteBulk().
|
inline |
Attempt to read a single element from the pipe without blocking.
Dequeues an element only if one is immediately available. Returns false instantly if the pipe is empty.
| [out] | data | Destination buffer for the retrieved element. |
true if data was successfully read, false if the pipe was empty. Definition at line 166 of file stk_sync_pipe.h.
References stk::NO_WAIT, and Read().
Referenced by stk_pipe_tryread().
|
inline |
Attempt to read multiple elements from the pipe without blocking.
Reads as many elements as are currently available without blocking.
| [out] | dst | Pointer to the destination array. |
| [in] | count | Number of elements to read. |
Definition at line 199 of file stk_sync_pipe.h.
References stk::NO_WAIT, and ReadBulk().
Referenced by stk_pipe_tryread_bulk(), and xStreamBufferReceiveFromISR().
|
inline |
Non-blocking variant of ReadBulkTriggered.
Returns immediately with however many elements are available, up to max_count. The trigger threshold is not enforced (equivalent to trigger = 1 with NO_WAIT).
| [out] | dst | Destination buffer. |
| [in] | max_count | Maximum number of elements to read. |
Definition at line 234 of file stk_sync_pipe.h.
References stk::NO_WAIT, and ReadBulkTriggered().
Referenced by stk_pipe_tryread_bulk_triggered().
|
inline |
Attempt to write a single element to the pipe without blocking.
Enqueues the element only if a free slot is immediately available. Returns false instantly if the pipe is full.
| [in] | data | Pointer to the element payload. |
true if data was successfully written, false if no space is available. Definition at line 112 of file stk_sync_pipe.h.
References stk::NO_WAIT, and Write().
Referenced by stk_pipe_trywrite().
|
inline |
Attempt to write multiple elements to the pipe without blocking.
Copies as many elements as possible without blocking. Elements that do not fit are discarded.
| [in] | src | Pointer to the source array. |
| [in] | count | Number of elements to write. |
Definition at line 145 of file stk_sync_pipe.h.
References stk::NO_WAIT, and WriteBulk().
Referenced by stk_pipe_trywrite_bulk(), and xStreamBufferSendFromISR().
|
inline |
Write a single element to the pipe.
Copies element_size bytes from data into the next available slot in the ring buffer. If the pipe is full, the calling task is suspended until space becomes available or the timeout expires.
| [in] | data | Pointer to the element payload (must be at least element_size bytes). |
| [in] | timeout_ticks | Maximum time to wait for space (ticks). Default: WAIT_INFINITE. |
true if data was successfully written, false if timeout expired before space became available. Definition at line 348 of file stk_sync_pipe.h.
References CAPACITY_MAX, m_capacity, m_count, m_cv_not_empty, m_cv_not_full, m_element_size, m_head, Next(), Slot(), STK_ASSERT, and STK_MEMCPY().
Referenced by stk_pipe_write(), and TryWrite().
|
inline |
Write multiple elements to the pipe.
Copies a block of count elements into the FIFO. If the pipe does not have enough space for the entire block, it will block until the full amount can be written or the timeout expires.
| [in] | src | Pointer to the source array (must hold at least count elements of element_size bytes each). |
| [in] | count | Number of elements to write. |
| [in] | timeout_ticks | Maximum time to wait for sufficient space (ticks). Default: WAIT_INFINITE. |
count unless a timeout occurred. Definition at line 382 of file stk_sync_pipe.h.
References stk::GetTicks(), m_capacity, m_count, m_cv_not_empty, m_cv_not_full, m_element_size, m_head, stk::NO_WAIT, Slot(), STK_MEMCPY(), and stk::WAIT_INFINITE.
Referenced by stk_pipe_write_bulk(), TryWriteBulk(), and xStreamBufferSend().
|
static |
Max capacity supported (number of elements).
Definition at line 76 of file stk_sync_pipe.h.
|
private |
flat byte ring-buffer: capacity slots of element_size bytes each
Definition at line 316 of file stk_sync_pipe.h.
Referenced by GetBuffer(), IsStorageValid(), Pipe(), and Slot().
|
private |
maximum number of elements stored in the pipe
Definition at line 317 of file stk_sync_pipe.h.
Referenced by DrainLocked(), GetCapacity(), GetSpace(), IsFull(), Next(), Pipe(), Write(), and WriteBulk().
|
private |
current number of elements stored in the pipe
Definition at line 319 of file stk_sync_pipe.h.
Referenced by DrainLocked(), GetCount(), GetSpace(), IsEmpty(), IsFull(), Pipe(), Read(), ReadBulk(), ReadBulkTriggered(), Reset(), Write(), and WriteBulk().
|
private |
signaled by Write() when the pipe transitions from empty
Definition at line 322 of file stk_sync_pipe.h.
Referenced by Read(), ReadBulk(), ReadBulkTriggered(), Write(), and WriteBulk().
|
private |
signaled by Read()/Reset() when the pipe is no longer full
Definition at line 323 of file stk_sync_pipe.h.
Referenced by DrainLocked(), Read(), Reset(), Write(), and WriteBulk().
|
private |
size of each element in bytes
Definition at line 318 of file stk_sync_pipe.h.
Referenced by DrainLocked(), GetElementSize(), Pipe(), Read(), ReadBulk(), Slot(), Write(), and WriteBulk().
|
private |
write index (next slot to be written by Write())
Definition at line 320 of file stk_sync_pipe.h.
Referenced by Pipe(), Reset(), Write(), and WriteBulk().
|
private |
read index (next slot to be read by Read())
Definition at line 321 of file stk_sync_pipe.h.
Referenced by DrainLocked(), Pipe(), Read(), and Reset().