ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
worker.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 worker.h
6 * @brief Core server - server worker identity.
7 *
8 * The server pipeline runs in one or more dedicated worker tasks (see
9 * PROTOCORE_WORKER_COUNT). Each worker owns a disjoint partition of connection slots
10 * (slot i -> worker i % count) and its own scratch arena, so per-worker state
11 * (the arena, work buffers) is selected by the caller's worker id. This header is
12 * the single source of that id.
13 *
14 * The id is per-task/per-thread: a worker binds itself once at task entry via
15 * protocore_worker_set_self(); any context that has not bound an id (the user's
16 * loop(), a unit test, the network stack's own thread) reads 0, which is also the only valid id
17 * in the default single-worker build, so PROTOCORE_WORKER_COUNT == 1 is byte-for-byte
18 * the original single-pipeline behavior.
19 *
20 * @author Douglas Quigg (dstroy0)
21 * @date 2026
22 */
23
24#ifndef PROTOCORE_WORKER_H
25#define PROTOCORE_WORKER_H
26
27#include "protocore_config.h"
28
29#include "locus_carcerum/locus_carcerum.h" // the cellblock guards each worker slot borrows through
30
31#if PROTOCORE_ENABLE_PREEMPT_QUEUE
32#include "server/core/preempt_queue/preempt_queue.h" // carried below as Session.workers->queue
33#endif
34#if PROTOCORE_ENABLE_IDEMIP
35#include "src/idemip_owners.h" // IDEMIP_OWNERS: the owner of each unit of the stack
36#endif
37
38EMBED_BEGIN_DECLS
39
40// ---------------------------------------------------------------------------
41// Worker identity
42// ---------------------------------------------------------------------------
43
44/** @brief Number of server worker tasks (PROTOCORE_WORKER_COUNT). */
46
47/**
48 * @brief Worker id [0, count) of the calling task; 0 by default / single-worker.
49 *
50 * With PROTOCORE_WORKER_COUNT == 1 (the default) there is exactly one worker, so the answer is 0 by
51 * construction and this is an inline constant - no lookup, no call. Every borrow asks, so the
52 * multi-worker lookup is paid only where there is more than one worker to tell apart.
53 */
54#if PROTOCORE_WORKER_COUNT == 1
55EMBED_INLINE int protocore_worker_self(void)
56{
57 return 0;
58}
59#else
61#endif
62
63/** @brief Bind the calling task/thread to worker id @p id (worker entry / tests). */
65
66// ---------------------------------------------------------------------------
67// Cellblocks - the memory each worker slot borrows from
68// ---------------------------------------------------------------------------
69//
70// Each slot borrows through two MMgr cellblock guards: a minimum-security one for plaintext, whose
71// bytes are left as they are on release, and a maximum-security one for key material, whose bytes
72// are zeroed on release. A slot has exactly one accessor, the worker that owns it, so a borrow is a
73// plain bump with no lock.
74//
75// The worker slots are the application's. It declares the pools and the LocusCarcerum over them in
76// its own translation unit and hands the guards in through protocore_cellblocks_bind(). The ghost
77// slot (PROTOCORE_GHOST_WORKER_SLOT) is the library's own, declared in worker.c. A slot nothing was
78// bound to, and any context outside [0, PROTOCORE_WORKER_COUNT), borrows from the ghost.
79
80/**
81 * @brief Hand worker slot @p worker the guards it borrows through.
82 *
83 * @param worker a worker id in [0, PROTOCORE_WORKER_COUNT); anything else is ignored.
84 * @param plain the minimum-security guard for plaintext, or NULL to leave the slot on the ghost's.
85 * @param secure the maximum-security guard for key material, or NULL to leave it on the ghost's.
86 */
87void protocore_cellblocks_bind(int worker, const MinimumSecurityGuard *plain, const MaxSecurityGuard *secure);
88
89/** @brief The plaintext guard the calling worker borrows through. Never NULL. */
90const MinimumSecurityGuard *protocore_plain_guard(void);
91
92/** @brief The key-material guard the calling worker borrows through. Never NULL. */
93const MaxSecurityGuard *protocore_secure_guard(void);
94
95/**
96 * @brief The owners of the library, as the list that MMGR_OWNERS takes.
97 *
98 * worker.c expands MMGR_OWNERS over this list one time for the program. Each entry is an identity
99 * and the name that a reader prints.
100 */
101#if PROTOCORE_ENABLE_IDEMIP
102#define PROTOCORE_OWNERS(owner_) \
103 owner_(protocore_owner_plain, "protocore_plain") owner_(protocore_owner_secure, "protocore_secure") \
104 IDEMIP_OWNERS(owner_)
105#else
106#define PROTOCORE_OWNERS(owner_) \
107 owner_(protocore_owner_plain, "protocore_plain") owner_(protocore_owner_secure, "protocore_secure")
108#endif
109
110PROTOCORE_OWNERS(MMGR_OWNER_EXTERN)
111
112/** @brief The owner that every plaintext take in the library names. Never NULL. */
113const CellOwner *protocore_plain_owner(void);
114
115/** @brief The owner that every key-material take in the library names. Never NULL. */
116const CellOwner *protocore_secure_owner(void);
117
118/**
119 * @brief @p n persistent plaintext bytes, zeroed, or NULL if the calling worker's cellblock is full.
120 *
121 * State that lasts across dispatches starts from zero, and a cellblock hands its cells back as
122 * they were, so this is the persistent borrow followed by the zeroing.
123 */
125
126/** @brief @p n persistent key-material bytes, zeroed, or NULL if the calling worker's cellblock is full. */
128
129// This header is also scheduling: starting, waking, stopping and deferring onto those workers.
130
131// ---------------------------------------------------------------------------
132// Worker tasks
133// ---------------------------------------------------------------------------
134//
135// Where the platform has a scheduler the server runs in dedicated worker tasks instead of the
136// user's loop(): protocore_workers_start() spawns PROTOCORE_WORKER_COUNT tasks, each
137// pinned to a core, each binding its worker id and repeatedly invoking the
138// app-supplied pump (so this layer stays free of any app dependency). On host
139// builds there are no tasks - the pipeline is driven inline by handle() / tests -
140// so these are no-ops and protocore_workers_running() is false.
141
142/** @brief Pump callback run by each worker task with its worker id. */
143typedef void (*protocore_worker_pump_fn)(int worker_id);
144
145// ---------------------------------------------------------------------------
146// Deferred work (thread-safe app -> worker submission)
147// ---------------------------------------------------------------------------
148//
149// HttpRoute a callback to a worker so it runs in that worker's single-thread context.
150// This is how application code on loop() (or any other task) safely pushes to a
151// connection - e.g. an SSE broadcast on a timer, or ws_send from a sensor task:
152// instead of calling the send API directly (which would race the worker that owns
153// the slot), wrap it in a small function and hand it to the owning worker. The
154// worker drains and runs deferred callbacks each service iteration.
155// (defer(slot, fn, arg) is the app-facing wrapper that resolves the
156// slot's owner; this layer stays free of the transport/conn_pool dependency.)
157//
158// @p arg must remain valid until the callback runs (point it at static/global
159// state, or data you keep alive). On host builds (no worker task) the callback
160// runs inline immediately, so tests and loop()-driven code behave identically.
161
162/** @brief Deferred callback signature. */
163typedef void (*protocore_deferred_fn)(void *arg);
164
165/** @brief One deferred call: what runs, and what it is given. */
166typedef struct
167{
168 protocore_deferred_fn fn; ///< what the worker runs
169 void *arg; ///< the opaque context it is given
171
172/**
173 * @brief The Workers module.
174 *
175 * A caller sets the members a call takes, invokes it through ::Workers, and reads the outcome off
176 * the same handle.
177 *
178 * @var WorkerNs::worker_id whose queue or task a call names
179 * @var WorkerNs::pump what each worker task runs each iteration
180 * @var WorkerNs::defer_args the call a defer hands to a worker; the arg must outlive it
181 * @var WorkerNs::ok a call's true/false outcome
182 * @var WorkerNs::run_deferred run every callback queued for worker_id, in its own context
183 * @var WorkerNs::running whether the worker tasks are up
184 * @var WorkerNs::start spawn the tasks and bind the pump
185 * @var WorkerNs::stop ask them to exit
186 * @var WorkerNs::wake nudge one so it services now rather than on the idle timeout
187 * @var WorkerNs::defer hand fn+arg to worker_id's queue and wake it
188 */
189typedef struct
190{
191 int worker_id; ///< the worker every call names
192 protocore_worker_pump_fn pump; ///< what a started worker runs each time it wakes
193 WorkerDeferArgs defer_args; ///< the call handed to a worker to run in its own context
194 embed_bool ok;
195#if PROTOCORE_ENABLE_PREEMPT_QUEUE
196 // The lane the workers jump. They run without it; it only changes what runs first.
197 const PreemptQueueNs *queue;
198#endif
200
201/** @brief The operands and the outcome. */
202extern WorkersVars WorkersV;
203
204/** @brief The entries. */
205typedef struct
206{
207 void (*const run_deferred)(uint8_t *work);
208 void (*const running)(uint8_t *work);
209 void (*const start)(uint8_t *work);
210 void (*const stop)(uint8_t *work);
211 void (*const wake)(uint8_t *work);
212 void (*const defer)(uint8_t *work);
213} WorkerNs;
214
215// What the table binds, defined once in the .c and taking one parameter each: everything
216// else an entry needs is an operand in WorkersV or a region of the borrow at a fixed offset.
218void protocore_workers_running(uint8_t *work);
219void protocore_workers_start(uint8_t *work);
220void protocore_workers_stop(uint8_t *work);
221void protocore_workers_wake(uint8_t *work);
222void protocore_workers_defer(uint8_t *work);
223#if PROTOCORE_ENABLE_PREEMPT_QUEUE
224#endif
225
226// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
227// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
228// `Workers.run_deferred(work)` resolves to a named function and becomes a DIRECT call. An extern table
229// leaves the call indirect and the symbol live at every level, -O2 -flto included.
230static const WorkerNs Workers __attribute__((unused)) = {
231 .run_deferred = protocore_workers_run_deferred,
232 .running = protocore_workers_running,
237#if PROTOCORE_ENABLE_PREEMPT_QUEUE
238#endif
239};
240
241/**
242 * @brief The PROTOCORE_WORKER_BORROW bytes this module's state lives in.
243 *
244 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
245 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
246 * walks, so the state lasts the life of the program.
247 *
248 * @return the span.
249 */
251
252EMBED_END_DECLS
253
254#endif // PROTOCORE_WORKER_H
User-configurable preempting work queues + high-priority processing tasks (PROTOCORE_ENABLE_PREEMPT_Q...
One deferred call: what runs, and what it is given.
Definition worker.h:167
protocore_deferred_fn fn
what the worker runs
Definition worker.h:168
void * arg
the opaque context it is given
Definition worker.h:169
The entries.
Definition worker.h:206
void(*const wake)(uint8_t *work)
Definition worker.h:211
void(*const run_deferred)(uint8_t *work)
Definition worker.h:207
void(*const running)(uint8_t *work)
Definition worker.h:208
void(*const stop)(uint8_t *work)
Definition worker.h:210
void(*const start)(uint8_t *work)
Definition worker.h:209
void(*const defer)(uint8_t *work)
Definition worker.h:212
WorkerDeferArgs defer_args
the call handed to a worker to run in its own context
Definition worker.h:193
embed_bool ok
Definition worker.h:194
protocore_worker_pump_fn pump
what a started worker runs each time it wakes
Definition worker.h:192
int worker_id
the worker every call names
Definition worker.h:191
void(* protocore_deferred_fn)(void *arg)
Deferred callback signature.
Definition worker.h:163
void protocore_workers_stop(uint8_t *work)
void protocore_workers_defer(uint8_t *work)
const MinimumSecurityGuard * protocore_plain_guard(void)
The plaintext guard the calling worker borrows through. Never NULL.
void protocore_workers_run_deferred(uint8_t *work)
void protocore_workers_running(uint8_t *work)
const CellOwner * protocore_secure_owner(void)
The owner that every key-material take in the library names. Never NULL.
void protocore_worker_set_self(int id)
Bind the calling task/thread to worker id id (worker entry / tests).
uint8_t * protocore_worker_span(void)
The PROTOCORE_WORKER_BORROW bytes this module's state lives in.
void protocore_workers_start(uint8_t *work)
void * protocore_secure_persist(size_t n)
n persistent key-material bytes, zeroed, or NULL if the calling worker's cellblock is full.
const MaxSecurityGuard * protocore_secure_guard(void)
The key-material guard the calling worker borrows through. Never NULL.
int protocore_worker_self(void)
Worker id [0, count) of the calling task; 0 by default / single-worker.
void * protocore_plain_persist(size_t n)
n persistent plaintext bytes, zeroed, or NULL if the calling worker's cellblock is full.
void protocore_cellblocks_bind(int worker, const MinimumSecurityGuard *plain, const MaxSecurityGuard *secure)
Hand worker slot worker the guards it borrows through.
#define PROTOCORE_OWNERS(owner_)
The owners of the library, as the list that MMGR_OWNERS takes.
Definition worker.h:106
WorkersVars WorkersV
The operands and the outcome.
const CellOwner * protocore_plain_owner(void)
The owner that every plaintext take in the library names. Never NULL.
void(* protocore_worker_pump_fn)(int worker_id)
Pump callback run by each worker task with its worker id.
Definition worker.h:143
void protocore_workers_wake(uint8_t *work)
EMBED_BEGIN_DECLS int protocore_worker_count(void)
Number of server worker tasks (PROTOCORE_WORKER_COUNT).