ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
preempt_queue.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file preempt_queue.h
6 * @brief User-configurable preempting work queues + high-priority processing tasks
7 * (PROTOCORE_ENABLE_PREEMPT_QUEUE) - the v5 real-time ingest primitive.
8 *
9 * Fixed-capacity queues, each feeding one dedicated, core-pinned task. A producer posts
10 * a fixed-size item. The scheduler **preempts** the lower-priority producer the instant
11 * the item lands so it is processed immediately instead of on the next tick. Producers
12 * post from a task (back or front, with a wait timeout) or from an ISR (interrupt-safe,
13 * with an immediate context-switch request). Each processing task pops items in order
14 * and hands each to a user handler.
15 *
16 * **Named lanes.** There are several queues, addressed by @ref protocore_pq_lane:
17 * - `PROTOCORE_PQ_LANE_USER` - the single lane exposed to the application. The no-lane
18 * `protocore_pq_*` API drives it. Lowest priority.
19 * - `PROTOCORE_PQ_LANE_DMA` / `_FORWARD` / `_DEVICE` - internal lanes for the library's own
20 * real-time work (DMA peripheral transfers, interface forwarding, device access).
21 * They run **above** the user lane (base `PROTOCORE_PQ_INTERNAL_PRIORITY`, DMA highest),
22 * so internal ingest always preempts user work. And below the network stack's own
23 * tasks so networking is never starved.
24 *
25 * This is the single normalized pipe for "hardware event -> process now": a DMA-complete
26 * / GPIO / bus ISR posts a descriptor onto its lane, the lane's task drains it. Zero-heap
27 * queue storage (static, compile-time PROTOCORE_PQ_DEPTH x PROTOCORE_PQ_ITEM_SIZE per lane; a
28 * task's stack is created only when its lane starts, so unused lanes cost only their queue
29 * storage), fail-closed on a full queue, no hot-path locks - so latency stays bounded.
30 *
31 * A build with no task backend posts into the same fixed per-lane ring, and
32 * protocore_pq_drain[_lane]() runs the handler over what is queued, so the logic is
33 * host-testable and behaves identically to the device's draining task.
34 *
35 * @author Douglas Quigg (dstroy0)
36 * @date 2026
37 */
38
39#ifndef PROTOCORE_PREEMPT_QUEUE_H
40#define PROTOCORE_PREEMPT_QUEUE_H
41
42#include "protocore_config.h" // the entry point: protocore_types.h for the widths
43
44#if PROTOCORE_ENABLE_PREEMPT_QUEUE
45
46EMBED_BEGIN_DECLS
47
48/**
49 * @brief The preempting lanes, ordered by role. The USER lane is exposed to the
50 * application; the internal lanes run at a higher priority (DMA highest) so
51 * internal ingest preempts user work.
52 */
53EMBED_ENUM_BYTE(protocore_pq_lane,
54 PROTOCORE_PQ_LANE_USER = 0, ///< exposed to the app (no-lane API); lowest priority
55 PROTOCORE_PQ_LANE_DMA, ///< internal: DMA peripheral transfers (highest)
56 PROTOCORE_PQ_LANE_FORWARD, ///< internal: interface forwarding
57 PROTOCORE_PQ_LANE_DEVICE, ///< internal: device access
58 PROTOCORE_PQ_LANE_COUNT);
59
60/**
61 * @brief Handler a lane's processing task invokes for each dequeued item.
62 * @param item pointer to PROTOCORE_PQ_ITEM_SIZE bytes (the posted item).
63 * @param ctx the opaque pointer passed to protocore_pq_start[_lane]().
64 */
65typedef void (*protocore_pq_handler)(const void *item, void *ctx);
66
67/**
68 * @brief What a lane does with an item, and where it ranks.
69 *
70 * The priority is the QoS: it is what makes a post preempt lower-ranked work instead of waiting
71 * for it, so it belongs to the lane rather than to whichever task happens to drain it.
72 */
73typedef struct
74{
75 protocore_pq_handler handler; ///< Called once per dequeued item (required).
76 void *ctx; ///< Opaque, forwarded to @ref handler.
77 uint8_t priority; ///< Lane priority; 0 = the lane's default (internal ranks above user).
78 uint8_t core; ///< Core to pin the lane's worker to; ignored where there is one.
79 const char *name; ///< Lane name (debug); may be NULL.
80} protocore_pq_config;
81
82/** @brief What one post carries, and how long it may wait for room. */
83typedef struct
84{
85 const void *item; ///< PROTOCORE_PQ_ITEM_SIZE bytes to copy onto the lane
86 uint32_t timeout_ticks; ///< how long a post may block; 0 returns at once when the lane is full
87} PqPostArgs;
88
89/**
90 * @brief The preempting work queues.
91 *
92 * A caller names the lane, sets the members a call takes, invokes it through ::PreemptQueue, and
93 * reads the outcome off the same handle. The queue storage and the tasks are behind @ref internal.
94 *
95 * @var PreemptQueueNs::lane the lane every call names
96 * @var PreemptQueueNs::cfg what starting a lane installs
97 * @var PreemptQueueNs::post_args what a post carries
98 * @var PreemptQueueNs::ok a call's true/false outcome
99 * @var PreemptQueueNs::n the peak depth a high-water read reports
100 * @var PreemptQueueNs::u8 the priority a lookup reports
101 * @var PreemptQueueNs::post_from_isr post from an ISR, then hand the CPU to the lane if it outranks us
102 * @var PreemptQueueNs::post_urgent post to the front of the lane, ahead of what is already queued
103 * @var PreemptQueueNs::high_water the most items ever queued at once, as a sizing aid
104 * @var PreemptQueueNs::priority the lane's default task priority
105 * @var PreemptQueueNs::running the lane's task is up
106 * @var PreemptQueueNs::start create the queue and spawn the lane's task
107 * @var PreemptQueueNs::post post to the back of the lane
108 * @var PreemptQueueNs::drain run the handler over what is queued, where no task does it
109 * @var PreemptQueueNs::stop stop the lane's task
110 */
111typedef struct
112{
113 protocore_pq_lane lane;
114 const protocore_pq_config *cfg;
115 PqPostArgs post_args;
116 embed_bool ok;
117 size_t n;
118 uint8_t u8;
119} PreemptQueueVars;
120
121/** @brief The operands and the outcome. */
122extern PreemptQueueVars PreemptQueueV;
123
124/** @brief The entries. */
125typedef struct
126{
127 void (*const post_from_isr)(uint8_t *work);
128 void (*const post_urgent)(uint8_t *work);
129 void (*const high_water)(uint8_t *work);
130 void (*const priority)(const uint8_t *work);
131 void (*const running)(uint8_t *work);
132 void (*const start)(uint8_t *work);
133 void (*const post)(uint8_t *work);
134 void (*const drain)(const uint8_t *work);
135 void (*const stop)(uint8_t *work);
136} PreemptQueueNs;
137
138// What the table binds, defined once in the .c and taking one parameter each: everything
139// else an entry needs is an operand in PreemptQueueV or a region of the borrow at a fixed offset.
140void protocore_preempt_queue_post_from_isr(uint8_t *work);
141void protocore_preempt_queue_post_urgent(uint8_t *work);
142void protocore_preempt_queue_high_water(uint8_t *work);
143void protocore_preempt_queue_priority(const uint8_t *work);
144void protocore_preempt_queue_running(uint8_t *work);
145void protocore_preempt_queue_start(uint8_t *work);
146void protocore_preempt_queue_post(uint8_t *work);
147void protocore_preempt_queue_drain(const uint8_t *work);
148void protocore_preempt_queue_stop(uint8_t *work);
149
150// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
151// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
152// `PreemptQueue.post_from_isr(work)` resolves to a named function and becomes a DIRECT call. An extern table
153// leaves the call indirect and the symbol live at every level, -O2 -flto included.
154static const PreemptQueueNs PreemptQueue __attribute__((unused)) = {
155 .post_from_isr = protocore_preempt_queue_post_from_isr,
156 .post_urgent = protocore_preempt_queue_post_urgent,
157 .high_water = protocore_preempt_queue_high_water,
158 .priority = protocore_preempt_queue_priority,
159 .running = protocore_preempt_queue_running,
160 .start = protocore_preempt_queue_start,
161 .post = protocore_preempt_queue_post,
162 .drain = protocore_preempt_queue_drain,
163 .stop = protocore_preempt_queue_stop,
164};
165
166/**
167 * @brief The PROTOCORE_PREEMPT_QUEUE_BORROW bytes this module's state lives in.
168 *
169 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
170 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
171 * walks, so the state lasts the life of the program.
172 *
173 * @return the span.
174 */
175uint8_t *protocore_preempt_queue_span(void);
176
177// --- User-lane API (drives PROTOCORE_PQ_LANE_USER) --------------------------------------------
178
179/** @brief Start the USER lane. */
180EMBED_INLINE embed_bool protocore_pq_start(const protocore_pq_config *cfg)
181{
182 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
183 PreemptQueueV.cfg = cfg;
184 PreemptQueue.start(protocore_preempt_queue_span());
185 return PreemptQueueV.ok;
186}
187/** @brief Post to the back of the USER lane. */
188EMBED_INLINE embed_bool protocore_pq_post(const void *item, uint32_t timeout_ticks)
189{
190 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
191 PreemptQueueV.post_args.item = item;
192 PreemptQueueV.post_args.timeout_ticks = timeout_ticks;
193 PreemptQueue.post(protocore_preempt_queue_span());
194 return PreemptQueueV.ok;
195}
196/** @brief Post to the front of the USER lane (urgent). */
197EMBED_INLINE embed_bool protocore_pq_post_urgent(const void *item, uint32_t timeout_ticks)
198{
199 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
200 PreemptQueueV.post_args.item = item;
201 PreemptQueueV.post_args.timeout_ticks = timeout_ticks;
202 PreemptQueue.post_urgent(protocore_preempt_queue_span());
203 return PreemptQueueV.ok;
204}
205/** @brief Post to the USER lane from an ISR. */
206EMBED_INLINE embed_bool protocore_pq_post_from_isr(const void *item)
207{
208 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
209 PreemptQueueV.post_args.item = item;
210 PreemptQueue.post_from_isr(protocore_preempt_queue_span());
211 return PreemptQueueV.ok;
212}
213/** @brief Drain the USER lane (host / inline drive). */
214EMBED_INLINE void protocore_pq_drain(void)
215{
216 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
217 PreemptQueue.drain(protocore_preempt_queue_span());
218}
219/** @brief Stop the USER lane's task. */
220EMBED_INLINE void protocore_pq_stop(void)
221{
222 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
223 PreemptQueue.stop(protocore_preempt_queue_span());
224}
225/** @brief True while the USER lane's task is running. */
226EMBED_INLINE embed_bool protocore_pq_running(void)
227{
228 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
229 PreemptQueue.running(protocore_preempt_queue_span());
230 return PreemptQueueV.ok;
231}
232/** @brief Peak items ever queued on the USER lane. */
233EMBED_INLINE size_t protocore_pq_high_water(void)
234{
235 PreemptQueueV.lane = PROTOCORE_PQ_LANE_USER;
236 PreemptQueue.high_water(protocore_preempt_queue_span());
237 return PreemptQueueV.n;
238}
239
240EMBED_END_DECLS
241
242#endif // PROTOCORE_ENABLE_PREEMPT_QUEUE
243
244#endif // PROTOCORE_PREEMPT_QUEUE_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.