Defines event queue functionality for producer/consumer patterns. More...
![]() |
Data Structures | |
struct | sys_event_queue_t |
Event queue structure for producer/consumer patterns. More... | |
Typedefs | |
typedef void * | sys_event_t |
Event type for queue items. More... | |
Functions | |
sys_event_queue_t | sys_event_queue_init (size_t capacity) |
Initialize a new event queue. More... | |
void | sys_event_queue_finalize (sys_event_queue_t *queue) |
Finalize and cleanup an event queue. More... | |
bool | sys_event_queue_push (sys_event_queue_t *queue, sys_event_t event) |
Push an event to the queue (always succeeds) More... | |
bool | sys_event_queue_try_push (sys_event_queue_t *queue, sys_event_t event) |
Try to push an event without overwriting. More... | |
sys_event_t | sys_event_queue_peek (sys_event_queue_t *queue) |
Peek at the next event without removing it. More... | |
sys_event_t | sys_event_queue_pop (sys_event_queue_t *queue) |
Remove and return the next event (blocking) More... | |
sys_event_t | sys_event_queue_try_pop (sys_event_queue_t *queue) |
Try to remove and return the next event (non-blocking) More... | |
sys_event_t | sys_event_queue_timed_pop (sys_event_queue_t *queue, uint32_t timeout_ms) |
Remove and return the next event with timeout. More... | |
size_t | sys_event_queue_size (sys_event_queue_t *queue) |
Get the current number of events in the queue. More... | |
bool | sys_event_queue_empty (sys_event_queue_t *queue) |
Check if the queue is empty. More... | |
void | sys_event_queue_shutdown (sys_event_queue_t *queue) |
Shut down the queue gracefully. More... | |
bool | sys_event_queue_lock (sys_event_queue_t *queue) |
Lock the queue for manual synchronization. More... | |
bool | sys_event_queue_unlock (sys_event_queue_t *queue) |
Unlock the queue after manual synchronization. More... | |
static bool | sys_event_queue_valid (sys_event_queue_t *queue) |
Check if the queue is valid and usable for operations. More... | |
Defines event queue functionality for producer/consumer patterns.
This file declares types and functions for thread-safe event queues that support multiple producers and consumers with peek-before-acquire semantics.
It is intended to support event channels, for a single producer and single consumer, all the way to multiple producers and consumers.
The event type sys_event_t is a simple pointer type that can be used to hold any data, but that data is not managed by the queue itself; If you want to manage the data, you must do so outside the queue. The queue supports operations to push events, pop events, and peek at events without removing them.
If you peek events you also need to acquire the queue lock before doing so, and release it after you are done. This is to ensure that the queue state is not modified while you are inspecting it.
You set a queue capacity when you initialize it, and the queue will use a circular buffer to store events. When the buffer is full, new events will overwrite the oldest events if you use the sys_event_queue_push() function. If you want to avoid overwriting, you can use sys_event_queue_try_push() function instead, which will return false if the queue is full.
When you wish to shutdown the queue, you can call sys_event_queue_finalize(). Consumers will be woken up and any further attempts to push events will fail, but existing events can still be popped, until the queue is empty.
typedef void* sys_event_t |
bool sys_event_queue_empty | ( | sys_event_queue_t * | queue | ) |
Check if the queue is empty.
queue | Pointer to the event queue |
Returns true if the queue contains no events. This is a snapshot value that may change immediately after the call returns in a multi-threaded environment.
void sys_event_queue_finalize | ( | sys_event_queue_t * | queue | ) |
Finalize and cleanup an event queue.
queue | Pointer to the queue to finalize |
Finalizes the event queue and releases all associated resources. Any threads waiting on the queue will be woken up. The queue should not be used after this call.
sys_event_queue_t sys_event_queue_init | ( | size_t | capacity | ) |
Initialize a new event queue.
capacity | Maximum number of events the queue can hold |
Creates a new thread-safe event queue with the specified capacity. The queue uses circular buffer semantics - when full, new items will overwrite the oldest items. The returned queue must be finalized with sys_event_queue_finalize().
bool sys_event_queue_lock | ( | sys_event_queue_t * | queue | ) |
Lock the queue for manual synchronization.
queue | Pointer to the event queue |
Manually locks the queue mutex. This allows for atomic sequences of peek/pop operations. Must be paired with sys_event_queue_unlock().
sys_event_t sys_event_queue_peek | ( | sys_event_queue_t * | queue | ) |
Peek at the next event without removing it.
queue | Pointer to the event queue |
Returns the next event that would be returned by sys_event_queue_pop() without removing it from the queue. This allows consumers to inspect events before deciding whether to acquire them. The queue must be locked by the caller.
sys_event_t sys_event_queue_pop | ( | sys_event_queue_t * | queue | ) |
Remove and return the next event (blocking)
queue | Pointer to the event queue |
Removes and returns the next event from the queue. If the queue is empty, this function blocks until an event becomes available or the queue is shut down.
bool sys_event_queue_push | ( | sys_event_queue_t * | queue, |
sys_event_t | event | ||
) |
Push an event to the queue (always succeeds)
queue | Pointer to the event queue |
event | Event to add to the queue |
Adds an event to the queue. This operation always succeeds - if the queue is full, the oldest event is overwritten. This ensures producers are never blocked.
void sys_event_queue_shutdown | ( | sys_event_queue_t * | queue | ) |
Shut down the queue gracefully.
queue | Pointer to the event queue |
Sets the shutdown flag and wakes up all waiting consumers. After shutdown, push operations may fail and pop operations will return NULL once the queue is empty.
size_t sys_event_queue_size | ( | sys_event_queue_t * | queue | ) |
Get the current number of events in the queue.
queue | Pointer to the event queue |
Returns the current number of events in the queue. This is a snapshot value that may change immediately after the call returns in a multi-threaded environment.
sys_event_t sys_event_queue_timed_pop | ( | sys_event_queue_t * | queue, |
uint32_t | timeout_ms | ||
) |
Remove and return the next event with timeout.
queue | Pointer to the event queue |
timeout_ms | Timeout in milliseconds (0 = no timeout) |
Removes and returns the next event from the queue. If the queue is empty, waits up to timeout_ms milliseconds for an event to become available.
sys_event_t sys_event_queue_try_pop | ( | sys_event_queue_t * | queue | ) |
Try to remove and return the next event (non-blocking)
queue | Pointer to the event queue |
Attempts to remove and return the next event from the queue. Returns NULL immediately if the queue is empty.
bool sys_event_queue_try_push | ( | sys_event_queue_t * | queue, |
sys_event_t | event | ||
) |
Try to push an event without overwriting.
queue | Pointer to the event queue |
event | Event to add to the queue |
Attempts to add an event to the queue. Returns false if the queue is full, ensuring no existing events are overwritten.
bool sys_event_queue_unlock | ( | sys_event_queue_t * | queue | ) |
Unlock the queue after manual synchronization.
queue | Pointer to the event queue |
Unlocks the queue mutex. Must be called after sys_event_queue_lock().
|
inlinestatic |
Check if the queue is valid and usable for operations.
queue | Pointer to the event queue |
Definition at line 238 of file event.h.