Files
looper/engine/src/queue.h
2026-05-13 17:57:41 +00:00

32 lines
978 B
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef QUEUE_H
#define QUEUE_H
#include "command.h"
#include <stdbool.h>
/* Fixedsize lockfree SPSC queue (single producer, single consumer).
* The queue is safe for one thread writing (producer) and one thread
* reading (consumer). No locks, no dynamic memory allocation.
* Must be initialised before first use. All operations are RTsafe. */
#define QUEUE_CAPACITY 256
typedef struct {
command_t buffer[QUEUE_CAPACITY];
/* head: index where next element will be written (producer only)
* tail: index of next element to read (consumer only) */
int head;
int tail;
} spsc_queue_t;
/* Initialise queue (must be called once before any push/pop). */
void queue_init(spsc_queue_t *q);
/* Push a command. Returns true on success, false if queue full. */
bool queue_push(spsc_queue_t *q, command_t cmd);
/* Pop a command. Returns true if a command was retrieved, false if empty. */
bool queue_pop(spsc_queue_t *q, command_t *cmd);
#endif