MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
praet_ordo.h
Go to the documentation of this file.
1/* MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
3 *
4 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
5 * negotiated commercial licensing contract or an educator's license issued to you personally.
6 */
7/**
8 * @file praet_ordo.h
9 * @brief The schedule context: one flag word per channel, the two volatiles that coordinate the
10 * interrupt, and the reader/setter. The reader/setter is the only thing that touches either.
11 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
12 * @date 2026-09-01
13 *
14 * @note A context exists only where DMA has to schedule internally. A channel is not a context.
15 * @note Two separate concerns live here and must stay apart. The quaternary core in the flag word is
16 * the channel's DMA state, where busy means the engine moves bytes. The busy volatile below
17 * is the reader/setter's lock, and means it is mid-update.
18 * @note Time is microseconds throughout. DMA timing is tight enough that a millisecond is not a unit
19 * for anything here.
20 */
21#ifndef MMGR_PRAET_ORDO_H
22#define MMGR_PRAET_ORDO_H
23
24/** @addtogroup mod_praet
25 * @{ */
26
27#include "mmgr.h"
28
29// PRAET_CHANNELS, PRAET_SETTLE_MICROS, PRAET_KEEPALIVE_MICROS and PRAET_RECOVERY come from here. None
30// has a value that is right for every part. An unset one takes a default and raises a warning
31// that names itself. What stops the build is praet_iudex.h, below, once every one of them has its say
33
34// Every bit of the flag word, and the assertions that retain the map from drift. Retained apart from the
35// entries because the map is one thing and it is the same on every build. A status a build never
36// sets still owns its bit
38
39// Where the microseconds come from, and what a tick is worth against them. Every deadline below is
40// microseconds, and so nothing here means anything without it
41#include "embed_clock.h"
42
43// Last, and after every knob reported. This is the single one place a configuration stops the build. So
44// a build that misses several knobs hears about all of them instead of the first
46
47// The examination arm's recorder. Expands to nothing unless a build asked for it, and the one
48// function that writes a flag word is what calls into it
50
51EMBED_BEGIN_DECLS
52
53#if PRAET_RECOVERY
54
55/**
56 * @brief Recovery that backs the transfer out and leaves the bytes as they are.
57 *
58 * @note What you want where the destination is about to be overwritten anyway, or where a partial
59 * result is still worth reading.
60 */
61#define PRAET_RESTITUERE_ET_REDINTEGRARE 0u
62
63/**
64 * @brief Recovery that treats the transfer's bytes as unsafe.
65 *
66 * @note What you want where a half-written buffer would read as a whole one. Which of the two is
67 * right depends on where in a program the stall happened. So it is the caller's call and not
68 * this library's.
69 */
70#define PRAET_RESTITUERE_ET_AD_NIHILUM_REDIGERE 1u
71
72#endif
73
74/**
75 * @brief The two answers a declaration may give about the boundary word check.
76 *
77 * @note Written at length deliberately. This is not a knob that rides along with recovery on. It is
78 * its own deliberate yes or no, made once per context, and the name is hard to skim past in a
79 * declaration.
80 * @note Real enumerators. A mistyped token is an undeclared identifier at the
81 * declaration instead of quietly reading as the off arm.
82 */
83typedef enum
84{
85 AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE = 0,
86 AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE = 1
88
89/**
90 * @brief Marks a declaration so that every use of it reports, with a message.
91 *
92 * @param[in] message_ Text the diagnostic reports, as a string literal.
93 * @note Aliased under this module's prefix and written once, as
94 * embed_compiler_directives.h asks a consumer to do with an attribute.
95 * @note The deprecated attribute, where the error and warning ones fall short. Those report at a call
96 * that survives compilation, and a declaration at file scope has no call in it: both stay
97 * silent. This one reports at any use, and a typedef that names the type is a use.
98 * @note Nothing marked with this is out of date. It is the only attribute that reports from a
99 * declarator, and its job here is to make a deliberate answer visible. Nothing goes away. The
100 * message says which answer, and so the line reads correctly whatever GCC prefixes it with.
101 * @warning Expands to nothing where EMBED_HAS_ATTRIBUTE(deprecated) is 0. The answer is then silent
102 * and only a wrong token still fails, the half nobody can miss.
103 */
104#if EMBED_HAS_ATTRIBUTE(deprecated)
105#define PRAET_DENUNTIATIO_ATTR(message_) __attribute__((deprecated(message_)))
106#else
107#define PRAET_DENUNTIATIO_ATTR(message_)
108#endif
109
110/**
111 * @brief A type that exists only for the AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE token.
112 *
113 * @note The declarator pastes the token onto this and typedefs the result. A token that is neither
114 * of the two fails on an unknown type name that has the bad token in it. A paste onto a
115 * macro instead gives a syntax error several lines down that names nothing, on the mistyped
116 * token this exists to catch.
117 * @note That same typedef makes the answer report. Both arms carry the attribute. A choice that
118 * only speaks up one way trains everyone to read its silence as the safe answer. Neither
119 * answer here is safe by default.
120 */
122 "AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE - this context measures no boundary word. A "
123 "recovery is exact to the word and no "
124 "finer") unsigned char PraetCrcResponsum_AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE;
125
126/**
127 * @brief A type that exists only for the AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE token.
128 */
130 "AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE - this context checksums the word a stalled "
131 "transfer was inside, one word, bit at a time, on the recovery path "
132 "only") unsigned char PraetCrcResponsum_AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE;
133
134/**
135 * @brief Pastes @p token_ onto the answer type prefix.
136 *
137 * @param[in] token_ One of the two AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC tokens.
138 * @return The answer type for that token.
139 */
140#define PRAET_CRC_RESPONSUM_IN_LOCO_FIGERE(token_) PraetCrcResponsum_##token_
141
142/**
143 * @brief The answer type @p token_ names, after its expansion.
144 *
145 * @param[in] token_ One of the two AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC tokens, or a macro that holds one.
146 * @return That token's answer type.
147 * @note This is what refuses a plain 1 or TRUE in the answer's place. Those are values, and the two
148 * tokens are the only things that name a type here.
149 */
150#define PRAET_CRC_RESPONSUM(token_) PRAET_CRC_RESPONSUM_IN_LOCO_FIGERE(token_)
151
152/**
153 * @brief The value the AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE token is for.
154 *
155 * @note Pasted onto, and so the declarator gets the answer before the compiler proper runs and can
156 * assert on it.
157 */
158#define PRAET_CRC_VALUE_AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE 0
159
160/**
161 * @brief The value the AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE token is for.
162 */
163#define PRAET_CRC_VALUE_AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE 1
164
165/**
166 * @brief Pastes @p token_ onto the value prefix.
167 *
168 * @param[in] token_ One of the two AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC tokens.
169 * @return The value macro for that token.
170 */
171#define PRAET_CRC_VALUE_IN_LOCO_FIGERE(token_) PRAET_CRC_VALUE_##token_
172
173/**
174 * @brief The value @p token_ is for, after its expansion.
175 *
176 * @param[in] token_ One of the two AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC tokens, or a macro that holds one.
177 * @return 1 for the ENABLE token, 0 for the DISABLE token.
178 * @note Usable in an #if, as lets a caller compile one thing or another around their own
179 * answer. No second macro has to stay in step with it.
180 */
181#define PRAET_CRC_VALUE(token_) PRAET_CRC_VALUE_IN_LOCO_FIGERE(token_)
182
183#if PRAET_RECOVERY
184
185/**
186 * @brief Storage the boundary word check needs, where a declaration asked for it.
187 *
188 * @param crc_choice_ One of the two AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC tokens.
189 * @return An initializer for the members that carry the choice.
190 */
191#define PRAET_ORDINEM_INCIPE(crc_choice_) {.word_boundary_crc = PRAET_CRC_VALUE_##crc_choice_}
192
193#else
194
195/**
196 * @brief An empty context, for a build with no recovery machinery to configure.
197 *
198 * @param crc_choice_ One of the two AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC tokens, unused here.
199 * @return A zero initializer.
200 * @note The token is still demanded by the declarator and still reports. What changes is that there
201 * is no member to put it in, and the assertion there refuses the ENABLE arm outright.
202 */
203#define PRAET_ORDINEM_INCIPE(crc_choice_) {0}
204
205#endif
206
207/**
208 * @brief Declares one schedule context and answers the boundary word question for it.
209 *
210 * @param name_ Name of the context this declares.
211 * @param crc_choice_ AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE or
212 * AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_DISABLE.
213 * @note The instantiation is the declaration. This emits data with its initial value and the answer in
214 * it. So nothing runs to make a context usable, and nothing can change the answer afterwards.
215 * @note Reports whichever token it received. A caller who wants a quieter build takes the message
216 * away with no context declared, the only honest way out of it.
217 * @warning A token that is neither of the two fails on the typedef, with the token it received in
218 * the message. A check on a build with PRAET_RECOVERY off fails the assertion below, which
219 * names what to do about it.
220 */
221#define PraetOrdoContext(name_, crc_choice_) \
222 typedef PRAET_CRC_RESPONSUM(crc_choice_) name_##_boundary_word_answer; \
223 EMBED_STATIC_ASSERT( \
224 PRAET_RECOVERY || (PRAET_CRC_VALUE(crc_choice_) == 0), \
225 "AD_VERBI_CONFINIUM_RESTITUE_PAULATIM_CRC_ENABLE needs PRAET_RECOVERY set to 1, since the word it " \
226 "measures is the one a recovery is deciding about"); \
227 static PraetOrdo name_ = PRAET_ORDINEM_INCIPE(crc_choice_)
228
229/**
230 * @brief Everything one context schedules over.
231 *
232 * @param flags One uint32 per channel: quaternary core at the bottom, statuses above it,
233 * region descriptor in the byte at PRAET_REGION_SHIFT. Plain, because the
234 * two volatiles below are what coordinate against the interrupt. Fixed at
235 * 32 bits whatever embed_word is, because the layout is a fixed set of bits.
236 * An environment that builds at a 16-bit word has nowhere to put the
237 * region byte.
238 * @param bound First byte of the memory each attached channel moves, as the pool named
239 * at its attach [BORROWS]. Present only where PRAET_RECOVERY is on.
240 * @param bound_bytes How many bytes are at that address, as the pool's own extent said.
241 * Present only where PRAET_RECOVERY is on.
242 * @param start First byte of each active channel's transfer, which is bound plus the
243 * offset a submit named. The caller's storage, and the caller's to write -
244 * nothing here writes through it [BORROWS]. Present only where
245 * PRAET_RECOVERY is on.
246 * @param length Bytes each active channel has to move. Present only where
247 * PRAET_RECOVERY is on.
248 * @param position Bytes each active channel moved, as of its last kick. Present only
249 * where PRAET_RECOVERY is on.
250 * @param boundary_crc Checksum of the word a recovery found the engine inside, where the
251 * declaration asked for the check. Meaningful only where the channel reads
252 * PRAET_MEASURED.
253 * @param word_boundary_crc What the declaration answered about the boundary word check. Written once
254 * by PraetOrdoContext and never again, as lets the one
255 * branch reading it fold away where the answer was no.
256 * @param keepalive_deadline Microsecond mark before which each active channel needs a kick.
257 * @param settle_deadline Microsecond mark the settle of the engine ends at. One timer, because the
258 * settle is the engine on its way up and not a property of a channel.
259 * @param praet_set_bitflag Raised by the interrupt, and by anything that changes what a service call
260 * would find. Volatile, and so every read after the preemption goes to memory.
261 * @param praet_busy_bitflag The reader/setter's lock. Volatile, and set while it updates, and that
262 * makes it ignore the interrupt for that span.
263 * @param elapsed_micros Microseconds elapsed, as the caller advances them.
264 * @note Both volatiles carry the module prefix. `set` and `busy` are words a caller may well take for
265 * a macro of their own. A macro over a member name breaks the struct at every use with a
266 * diagnostic that points nowhere near the cause.
267 * @warning The context sits at an address the linker resolves, and every write goes by offset from it.
268 * Nothing computes where a member lives, and nothing can move or copy it: its address
269 * is its identity.
270 */
271typedef struct
272{
273 uint32_t flags[PRAET_CHANNELS];
274 embed_word keepalive_deadline[PRAET_CHANNELS];
275#if PRAET_RECOVERY
276 uint8_t *bound[PRAET_CHANNELS];
277 embed_word bound_bytes[PRAET_CHANNELS];
278 uint8_t *start[PRAET_CHANNELS];
279 embed_word length[PRAET_CHANNELS];
280 embed_word position[PRAET_CHANNELS];
281 uint32_t boundary_crc[PRAET_CHANNELS];
282 embed_bool word_boundary_crc;
283#endif
284 embed_word settle_deadline;
285 volatile embed_word praet_set_bitflag;
286 volatile embed_word praet_busy_bitflag;
287 embed_word elapsed_micros;
288} PraetOrdo;
289
290/**
291 * @brief Returns how far the port says @p channel has got, in bytes.
292 *
293 * @param[in] channel Channel to read.
294 * @return Bytes moved so far. Zero from a port with nothing to say.
295 * @note The fifth port hook, and the one the four in memoriam_praetereo.h do not have. Every
296 * controller this library targets exposes a count of bytes left. The orchestrator needs it to
297 * feed a watchdog with something a caller can act on.
298 * @note Reached only from praet_ordo_poll. Nothing above it sees a byte count that did not go
299 * through the flag word first. That retains how the module tracks progress out of a caller's
300 * business.
301 * @note Carries a weak default that refuses, the same as the four hooks in memoriam_praetereo.c. It
302 * returns zero, and praet_ordo_take_progress treats zero as no movement. A build that supplies
303 * no port walks every channel and still marks a stalled one. An unkicked keepalive window is
304 * what marks a channel stopped.
305 * @note The default sits in praet_ordo.c, the unit that calls it. GCC on PE-COFF emits a weak
306 * definition as a weak external that ld does not resolve from another unit. A weak definition
307 * beside its call resolves on every linker this library builds with.
308 */
309EMBED_HOST_DEVICE uint16_t praet_hw_progress(embed_word channel);
310
311/**
312 * @brief Clears a context back to every channel detached.
313 *
314 * @param[out] context Context to clear [BORROWS].
315 * @note A declaration emits this as initialized data. The entry exists so one context can serve again.
316 * @warning Leaves the boundary word answer alone. That came from the declaration and is not state a
317 * reset has any business with.
318 */
319EMBED_HOST_DEVICE void praet_ordo_reset(PraetOrdo *context);
320
321/**
322 * @brief Attaches a channel over a block of bytes, claims its vector, and starts the settle timer.
323 *
324 * @param[in,out] context Context the channel belongs to [BORROWS].
325 * @param[in] channel Channel to attach.
326 * @param[in] bound First byte of the memory this channel moves. The caller's [BORROWS].
327 * @param[in] bytes How many bytes are there.
328 * @param[in] region Region descriptor for this channel, as a byte.
329 * @return EMBED_TRUE where the channel went from detached to attached.
330 * @note Attach and detach are the only entry and exit, and configuration takes no part in either.
331 * How long the engine takes to come up is a property of the part. So it arrives as
332 * PRAET_SETTLE_MICROS at compile time and not as an argument here.
333 * @note Reach this through PraetAttach, which takes the pool by name. A direct call means an address
334 * and an extent stated apart, and nothing here can tell whether the two go together.
335 * @note An attach does not claim the pool. A cellblock and a ring both write their own records into
336 * the bytes they dress. So MMGR_PARS_CLAIMED_ONCE exists to retain two of those off one pool.
337 * This writes no records into the bytes. An engine that fills ring segments while a
338 * consumer drains them is the arrangement the module is for.
339 * @warning Fails closed. A channel in any state but detached stays exactly as it is.
340 * @warning Two channels may attach over one block, and nothing here reports it. Which of them
341 * runs when is the caller's plan, and this library does not have it.
342 */
343EMBED_HOST_DEVICE embed_bool praet_ordo_adnectere(PraetOrdo *context, embed_word channel, uint8_t *bound,
344 embed_word bytes, embed_word region);
345
346/**
347 * @brief Attaches a channel over a declared pool, by name.
348 *
349 * @param context_ Context the channel belongs to.
350 * @param channel_ Channel to attach.
351 * @param pool_ Pool declared by ParsMemoriaeInternae or ParsMemoriaeExternum.
352 * @param region_ Region descriptor for this channel, as a byte.
353 * @return What praet_ordo_adnectere answered.
354 * @note The attach surface on a buffer, and how a caller should reach a channel. The pool's name
355 * makes the address and the extent come from one place. mmgr_pars_storage_##pool_ and
356 * pool_##_bytes both come out of the declaration, and neither exists for anything that is
357 * not a declared pool.
358 * @note Not a question of who owns the bytes. The test is whether whoever passes them over has every
359 * answer about them: the address, the extent, and which channel is over them. A caller with
360 * all three said everything this needs, and where the bytes came from is their business.
361 * @note Both names have internal linkage. A translation unit that did not declare the pool cannot
362 * reach either and does not compile. That leaves the answers first hand, and a caller that
363 * gives all of them makes these bytes legal to touch. The caller said so.
364 * @warning The region descriptor is still stated here. Which of the two declarations wrote a pool
365 * settles which memory it sits in, and neither emits a marker this could read. So a
366 * derivation needs something added at the pool's declaration.
367 */
368#define PraetAttach(context_, channel_, pool_, region_) \
369 (PRAET_ALVEUS_SUPERARE(context_, channel_, pool_), \
370 praet_ordo_adnectere(&(context_), (channel_), mmgr_pars_storage_##pool_, (embed_word)pool_##_bytes, \
371 (embed_word)(region_)))
372
373/**
374 * @brief Declares which pool a channel of a context is over.
375 *
376 * @param context_ Context the channel belongs to.
377 * @param channel_ Channel number, as a literal, since it goes into the symbol this emits by paste.
378 * @param pool_ Pool declared by ParsMemoriaeInternae or ParsMemoriaeExternum.
379 * @note Written once per channel, at file scope, beside the context's own declaration. It emits an
380 * enumerator whose name carries the context, the channel and the pool, and that name is the
381 * binding. PraetAttach and PraetSubmit both name it. So a call to either with a pool outside
382 * the channel's declaration is an undeclared identifier, and the error prints the triple as
383 * written.
384 * @note The same shape locus_carcerum uses. MMGR_CARCER_BODY pastes the site and the pool into
385 * prisonsite_##_##name_##_ctx and MMGR_CARCER_MEM makes the pool the member's name. A
386 * cellblock's entries cannot take another cellblock's bytes. Here the channel joins the
387 * paste, because a context has several and they may be over different pools.
388 * @note Costs nothing. An enumerator emits no storage and settles while the unit compiles.
389 * @warning Channel numbers settle at compile time, as lets this paste one. A channel chosen at
390 * run time has no binding this way and reaches praet_ordo_adnectere directly, where
391 * nothing relates the address to the extent.
392 */
393#define PraetChannel(context_, channel_, pool_) \
394 EMBED_STATIC_ASSERT((channel_) < PRAET_CHANNELS, \
395 #context_ " has no channel " #channel_ ", because PRAET_CHANNELS says how many it carries"); \
396 EMBED_STATIC_ASSERT(sizeof(mmgr_pars_storage_##pool_) > 0u, \
397 #pool_ " has no bytes for " #context_ " channel " #channel_ " to move"); \
398 enum \
399 { \
400 context_##_channel##channel_##_is_over_##pool_ = 1 \
401 }
402
403/**
404 * @brief Names the binding of a channel's declaration, and fails where there is none.
405 *
406 * @param context_ Context the channel belongs to.
407 * @param channel_ Channel number, as a literal.
408 * @param pool_ Pool the caller believes the channel is over.
409 * @note Reads the enumerator PraetChannel emitted. Nothing computes from it, and the reference is
410 * the purpose: a triple nobody declared names an identifier that does not exist.
411 */
412#define PRAET_ALVEUS_SUPERARE(context_, channel_, pool_) ((void)context_##_channel##channel_##_is_over_##pool_)
413
414/**
415 * @brief Asks for the detach of a channel.
416 *
417 * @param[in,out] context Context the channel belongs to [BORROWS].
418 * @param[in] channel Channel to detach.
419 * @note Writes the request and reports nothing. The caller reads the result out of the flag word,
420 * as gives a void teardown something to report through.
421 * @note A second request against a channel already in detach changes nothing. Hardware teardown
422 * writes the disable and polls the busy bit, and so a second call is the normal path.
423 */
424EMBED_HOST_DEVICE void praet_ordo_separare(PraetOrdo *context, embed_word channel);
425
426#if PRAET_RECOVERY
427
428/**
429 * @brief Marks a channel busy over @p length bytes at @p offset into what it attached over.
430 *
431 * @param[in,out] context Context the channel belongs to [BORROWS].
432 * @param[in] channel Channel to run on.
433 * @param[in] offset Bytes into the attached memory the transfer starts at.
434 * @param[in] length Bytes it has to move.
435 * @return EMBED_TRUE where the channel took it.
436 * @note Takes no pointer. The address came from the pool named at the attach. An offset and a
437 * length are all a transfer adds. An address here would let a caller state one that has
438 * nothing to do with what the channel attached over.
439 * @note Start, length and position are what make a backout possible. Without the three, a stalled
440 * transfer leaves a buffer nobody can say anything about. So this is the form PRAET_RECOVERY
441 * selects, and why it is not optional once that is on.
442 * @note Takes no duration. How long a transfer runs is not something this library can know, and so
443 * nothing here predicts it - the port reports completion when it happens.
444 * @warning Refuses a channel in its settle, already busy, in detach, or not attached. Fails
445 * closed: a refused submit changes no state.
446 * @warning Does not test the offset and the length against the attached extent. PraetSubmit settles
447 * that before anything runs, with the pool's own extent to compare against. A direct call
448 * here with numbers nobody checked is how a transfer runs off the end.
449 */
450EMBED_HOST_DEVICE embed_bool praet_ordo_relatio(PraetOrdo *context, embed_word channel, embed_word offset,
451 embed_word length);
452
453/**
454 * @brief Fails the build when a span runs past the pool it names.
455 *
456 * @param pool_ Pool declared by ParsMemoriaeInternae or ParsMemoriaeExternum.
457 * @param offset_ Bytes into that pool the span starts at.
458 * @param length_ Bytes in the span.
459 * @note The bound, and it settles before anything runs. A span that does not fit gives a negative
460 * bitfield width. The member's name is what the compiler prints, the same way
461 * MMGR_PARS_CLAIMED_ONCE says what it means.
462 * @note Usable in an expression, which a static assertion is not. That lets the check sit in
463 * front of a call whose answer the caller reads.
464 * @note At -O0 and at -O2 alike, a span that fits reports nothing and one that does not fails.
465 * Nothing here needs the optimizer, unlike a check that rides on a call the optimizer has to
466 * fold away.
467 * @warning Reads pool_##_bytes, and so it holds for a span whose offset and length are at compile
468 * time. A length computed at run time has no bound from this and no bound anywhere else
469 * either.
470 */
471#define PRAET_SPAN_FITS(pool_, offset_, length_) \
472 ((void)sizeof(struct { \
473 int this_span_runs_past_the_pool_the_channel_was_attached_over \
474 : (((offset_) + (length_)) <= sizeof(mmgr_pars_storage_##pool_)) \
475 ? 1 \
476 : -1; \
477 }))
478
479/**
480 * @brief Feeds the watchdog for one channel and records how far it has got.
481 *
482 * @param[in,out] context Context the channel belongs to [BORROWS].
483 * @param[in] channel Channel that showed a sign of life.
484 * @param[in] position Bytes moved so far.
485 * @note What the port calls when the engine moved. A sign of life and how far it got are the
486 * same event, and so one call carries both.
487 * @note Pushes the keepalive window out and clears a recorded stall, because a channel that moves
488 * again has no stall.
489 * @note Says nothing about the end of the transfer. praet_ordo_completed reports that.
490 */
491EMBED_HOST_DEVICE void praet_ordo_efficere(PraetOrdo *context, embed_word channel, embed_word position);
492
493/**
494 * @brief Returns how far a channel's transfer has got.
495 *
496 * @param[in] context Context the channel belongs to [BORROWS].
497 * @param[in] channel Channel to read.
498 * @return Bytes moved so far.
499 * @note With the start and length the caller submitted, this is the whole picture. The bytes at
500 * [start, start + position) hold writes and the rest do not. A caller needs exactly this to
501 * back a stalled transfer out, or to zero exactly the touched bytes instead of the whole buffer.
502 */
503EMBED_HOST_DEVICE embed_word praet_ordo_situs(const PraetOrdo *context, embed_word channel);
504
505/**
506 * @brief Returns the max bytes a stalled transfer wrote, rounded up to a whole word.
507 *
508 * @param[in] context Context the channel belongs to [BORROWS].
509 * @param[in] channel Channel to read.
510 * @return Bytes at start to treat as written, never above the length.
511 * @note The poll samples the position every few ticks, and so it is exact to word granularity. It can
512 * be up to one word behind what the engine wrote. A caller that zeroes only as far as the
513 * position would leave a partial word unscrubbed. So the round lives here instead of in every
514 * caller that has to remember it.
515 * @note This is the number to scrub. praet_ordo_situs is the sample itself.
516 */
517EMBED_HOST_DEVICE embed_word praet_ordo_commotus_est(const PraetOrdo *context, embed_word channel);
518
519/**
520 * @brief Returns the checksum of the word a recovery found the engine inside.
521 *
522 * @param[in] context Context the channel belongs to [BORROWS].
523 * @param[in] channel Channel to read.
524 * @return The checksum, where the channel reads PRAET_MEASURED. Zero otherwise.
525 * @note Read PRAET_MEASURED first. Zero is a legal checksum, and so the value alone does not say whether
526 * a measure of a word happened.
527 * @note The caller makes this useful. This library holds the destination and never the source. So
528 * it can say what the boundary word now contains and not whether that matches the intent. The
529 * compare of the two is the caller's, the only place both are at hand.
530 */
531EMBED_HOST_DEVICE uint32_t praet_ordo_boundary_crc(const PraetOrdo *context, embed_word channel);
532
533#else
534
535/**
536 * @brief Marks a channel busy and starts its keepalive window.
537 *
538 * @param[in,out] context Context the channel belongs to [BORROWS].
539 * @param[in] channel Channel to run on.
540 * @return EMBED_TRUE where the channel took it.
541 * @note Takes no bytes. Where PRAET_RECOVERY is off nothing records where a transfer pointed.
542 * A start and a length would land in storage and never get a read.
543 * @warning Refuses a channel in its settle, already busy, in detach, or not attached. Fails
544 * closed: a refused submit changes no state.
545 */
546EMBED_HOST_DEVICE embed_bool praet_ordo_relatio(PraetOrdo *context, embed_word channel);
547
548/**
549 * @brief Fails the build when a span runs past the pool it names.
550 *
551 * @param pool_ Pool declared by ParsMemoriaeInternae or ParsMemoriaeExternum.
552 * @param offset_ Bytes into that pool the span starts at.
553 * @param length_ Bytes in the span.
554 * @note The same check as on the other arm, and present here for the same reason it is there. A build
555 * with no recovery records nothing about a transfer. A span that runs off the end of the
556 * pool is still a span that runs off the end of the pool.
557 */
558#define PRAET_SPAN_FITS(pool_, offset_, length_) \
559 ((void)sizeof(struct { \
560 int this_span_runs_past_the_pool_the_channel_was_attached_over \
561 : (((offset_) + (length_)) <= sizeof(mmgr_pars_storage_##pool_)) \
562 ? 1 \
563 : -1; \
564 }))
565
566/**
567 * @brief Feeds the watchdog for one channel.
568 *
569 * @param[in,out] context Context the channel belongs to [BORROWS].
570 * @param[in] channel Channel that showed a sign of life.
571 * @note A sign of life, alone. How far the engine got is what PRAET_RECOVERY buys, and this
572 * build does not carry it.
573 * @note Pushes the keepalive window out and clears a recorded stall, because a channel that moves
574 * again has no stall.
575 */
576EMBED_HOST_DEVICE void praet_ordo_efficere(PraetOrdo *context, embed_word channel);
577
578#endif
579
580/**
581 * @brief Submits a span of a declared pool on a channel attached over that pool.
582 *
583 * @param context_ Context the channel belongs to.
584 * @param channel_ Channel to run on.
585 * @param pool_ Pool declared by ParsMemoriaeInternae or ParsMemoriaeExternum.
586 * @param offset_ Bytes into that pool the transfer starts at.
587 * @param length_ Bytes it moves.
588 * @return What praet_ordo_relatio answered.
589 * @note The submit surface on a buffer, and the counterpart of PraetAttach. The pool's name again
590 * gives the bound something to compare against, since the extent lives with the
591 * declaration and not with the channel.
592 * @note The check of the bound is here and never inside the entry. Sizes and spans settle at compile
593 * time. A test at run time would pay for a question with an answer already.
594 * @note Names the binding PraetChannel declared. So a submit of a pool this channel is not over fails.
595 * The identifier carries the context, the channel and the pool as written. That is the
596 * same guarantee locus_carcerum gets from the paste of the site and the pool into one symbol.
597 */
598#if PRAET_RECOVERY
599#define PraetSubmit(context_, channel_, pool_, offset_, length_) \
600 (PRAET_ALVEUS_SUPERARE(context_, channel_, pool_), PRAET_SPAN_FITS(pool_, offset_, length_), \
601 praet_ordo_relatio(&(context_), (channel_), (embed_word)(offset_), (embed_word)(length_)))
602#else
603#define PraetSubmit(context_, channel_, pool_, offset_, length_) \
604 (PRAET_ALVEUS_SUPERARE(context_, channel_, pool_), PRAET_SPAN_FITS(pool_, offset_, length_), \
605 praet_ordo_relatio(&(context_), (channel_)))
606#endif
607
608/**
609 * @brief Reports that a channel's transfer finished.
610 *
611 * @param[in,out] context Context the channel belongs to [BORROWS].
612 * @param[in] channel Channel that finished.
613 * @param[in] failed EMBED_TRUE where it ended in an error, EMBED_FALSE on a completion.
614 * @note Completion arrives from the port and never from a timer. A timer that decides a transfer
615 * ended would be this library's guess at hardware.
616 */
617EMBED_HOST_DEVICE void praet_ordo_completed(PraetOrdo *context, embed_word channel, embed_bool failed);
618
619#if PRAET_RECOVERY
620
621/**
622 * @brief Resolves a stalled channel by the recovery the caller picks.
623 *
624 * @param[in,out] context Context the channel belongs to [BORROWS].
625 * @param[in] channel Channel to resolve.
626 * @param[in] recovery PRAET_RESTITUERE_ET_REDINTEGRARE or PRAET_RESTITUERE_ET_AD_NIHILUM_REDIGERE.
627 * @return EMBED_TRUE where this resolved a stalled channel.
628 * @note Returns the channel to attached and records which recovery it took. A later reader can
629 * tell a backed-out transfer from one whose bytes counted as unsafe.
630 * @note Nothing here writes the caller's storage. This context holds no pointer to it. A caller
631 * that chooses PRAET_RESTITUERE_ET_AD_NIHILUM_REDIGERE zeroes its own bytes and this records that it did.
632 * @warning Refuses a channel with no stall. Fails closed: state is only ever unknown after the
633 * watchdog said so, and a resolution of a healthy channel would throw a live transfer away.
634 */
635EMBED_HOST_DEVICE embed_bool praet_ordo_resolve(PraetOrdo *context, embed_word channel, embed_word recovery);
636
637#endif
638
639/**
640 * @brief Advances the clock by @p micros.
641 *
642 * @param[in,out] context Context to advance [BORROWS].
643 * @param[in] micros Microseconds to add.
644 * @note Raises set, because the passage of time is one of the things that changes what a service
645 * call would find. A keepalive window can elapse with nothing else at all.
646 */
647EMBED_HOST_DEVICE void praet_ordo_advance(PraetOrdo *context, embed_word micros);
648
649/**
650 * @brief Advances the clock by @p ticks, scaled against the declared frequency.
651 *
652 * @param[in,out] context Context to advance [BORROWS].
653 * @param[in] ticks Ticks read off the clock this build declared.
654 * @note What a port calls, because a counter reads in ticks and every deadline here is microseconds.
655 * The scale is one compile-time constant. A build whose clock runs at a whole megahertz
656 * pays a multiply and a shift for it.
657 * @warning Ticks short of a whole microsecond drop, and nothing carries them over. A port that feeds
658 * this one tick at a time on a fast clock never advances anything. For that reason a port
659 * reads the counter and passes the difference instead of a count of calls.
660 */
661EMBED_HOST_DEVICE void praet_ordo_advance_ticks(PraetOrdo *context, embed_word ticks);
662
663/**
664 * @brief Is in for the interrupt that raises the set volatile.
665 *
666 * @param[in,out] context Context the interrupt belongs to [BORROWS].
667 * @note All an interrupt does. It may run any number of times and the raises collapse. There is
668 * exactly one reader and a raise of a raised flag is a no-op. Nothing counts and nothing
669 * waits in line.
670 */
671EMBED_HOST_DEVICE void praet_ordo_raise(PraetOrdo *context);
672
673/**
674 * @brief Drives the port and brings every flag word up to date. The caller's poll.
675 *
676 * @param[in,out] context Context to poll [BORROWS].
677 * @note What a caller calls, and the only thing they have to. Everything a channel does between an
678 * attach and a completion happens here. The poll asks the port how far each active channel got
679 * and feeds the watchdog the answer. It clears a settle that elapsed, marks a channel nothing
680 * reported on as stalled, and completes a detach whose transfer ended.
681 * @note Reports nothing. What a caller wants is in the flag word, which this leaves current, and one
682 * load reads it. A return value here would be a second way to learn the same thing, and the two
683 * would eventually disagree.
684 * @note Also the reader/setter, and the first call on interrupt exit. It reads set, takes busy, does
685 * the above, then unsets both volatiles. With busy taken it ignores the interrupt for that
686 * span. The interrupt may retain set raised throughout and nothing gets lost, because this
687 * recomputes every channel from what it can see and consumes no event.
688 * @note Does nothing where set was clear, and where busy was already up. A nested call while the
689 * lock is out is the re-entry case, and it declines and leaves the outer call's half-done
690 * state as it is.
691 * @note Every state change a channel undergoes goes through here, as puts all of the
692 * access control in one function. That fell out of the lock: one reader means one place.
693 */
694EMBED_HOST_DEVICE void praet_ordo_poll(PraetOrdo *context);
695
696/**
697 * @brief Returns one channel's flag word.
698 *
699 * @param[in] context Context the channel belongs to [BORROWS].
700 * @param[in] channel Channel to read.
701 * @return The whole word: quaternary core at the bottom, statuses and region above.
702 * @note One load. To know a channel is idle needs no wait for a callback.
703 */
704EMBED_HOST_DEVICE uint32_t praet_ordo_flags(const PraetOrdo *context, embed_word channel);
705
706EMBED_END_DECLS
707
708/** @} */
709
710#endif
EMBED_HOST_DEVICE void praet_ordo_reset(PraetOrdo *context)
Clears a context back to every channel detached.
EMBED_HOST_DEVICE void praet_ordo_separare(PraetOrdo *context, embed_word channel)
Asks for the detach of a channel.
EMBED_HOST_DEVICE embed_bool praet_ordo_adnectere(PraetOrdo *context, embed_word channel, uint8_t *bound, embed_word bytes, embed_word region)
Attaches a channel over a block of bytes, claims its vector, and starts the settle timer.
EMBED_HOST_DEVICE void praet_ordo_completed(PraetOrdo *context, embed_word channel, embed_bool failed)
Reports that a channel's transfer finished.
#define PRAET_DENUNTIATIO_ATTR(message_)
Marks a declaration so that every use of it reports, with a message.
Definition praet_ordo.h:107
EMBED_HOST_DEVICE void praet_ordo_advance_ticks(PraetOrdo *context, embed_word ticks)
Advances the clock by ticks, scaled against the declared frequency.
EMBED_HOST_DEVICE void praet_ordo_poll(PraetOrdo *context)
Drives the port and brings every flag word up to date. The caller's poll.
EMBED_HOST_DEVICE void praet_ordo_efficere(PraetOrdo *context, embed_word channel)
Feeds the watchdog for one channel.
PraetRecoveryWordBoundaryBitwiseCrc
The two answers a declaration may give about the boundary word check.
Definition praet_ordo.h:84
#define PRAET_CHANNELS
Logical channels one context carries.
EMBED_HOST_DEVICE void praet_ordo_advance(PraetOrdo *context, embed_word micros)
Advances the clock by micros.
EMBED_HOST_DEVICE embed_bool praet_ordo_relatio(PraetOrdo *context, embed_word channel)
Marks a channel busy and starts its keepalive window.
EMBED_HOST_DEVICE uint32_t praet_ordo_flags(const PraetOrdo *context, embed_word channel)
Returns one channel's flag word.
EMBED_HOST_DEVICE uint16_t praet_hw_progress(embed_word channel)
Returns how far the port says channel has got, in bytes.
EMBED_HOST_DEVICE void praet_ordo_raise(PraetOrdo *context)
Is in for the interrupt that raises the set volatile.
The embedded_types headers every module builds on.
Read after every knob reported, and the single place a configuration stops the build.
Every knob this module reads, what it takes when the caller did not set it, and the warning that says...
Which states and transitions a run actually reached.
Every bit of the channel flag word, and the assertions that retain the map from drift.
Everything one context schedules over.
Definition praet_ordo.h:272