skip to content
==> include/mesh/driver.h <==
1/* @ajax/mesh-driver: meshd's driver thread (driver.rs).
2 *
3 * The only publisher of every ring and the only writer of the program table.
4 * Each tick it collects completions, honours run requests and commands, and
5 * publishes the next tasks of every loaded program -- each its own resident
6 * kernel (or wasm lanes, or a native worker) with its own ring.
7 *
8 * Lifecycle (plan §3.1): start/stop launch or halt one program's kernel and
9 * touch nothing else (zero gap). Anything that changes the context's module
10 * set (load, upgrade, unload --now) is a *blink*, because module loads and
11 * unloads block while any kernel of the context runs: drain every ring, halt
12 * every kernel, do the module operations (images compiled beforehand on a
13 * worker thread), rewind each ring head to its published tail, launch
14 * everything that runs, then wait for all of it. Rollback and reset need no
15 * module operation: they blink only their slot. Isolated executors (mps, the
16 * CPU executors) blink only the slot a module change touches.
17 *
18 * Executors are per slot (mesh-abi PX_EXEC): the GPU backend and beside it the
19 * CPU executors (wasm lanes, native worker processes). A load runs on the
20 * first executor its manifest's `backends` lists that this meshd has. */
21#ifndef MESH_DRIVER_H
22#define MESH_DRIVER_H
23
24#include "mesh/backend.h"
25#include "mesh/shared.h"
26
27#include <stdbool.h>
28#include <stdint.h>
29
30typedef struct {
31    bool peered;
32    const char *state; /* --state FILE, or NULL */
33    bool autoload;
34    /* A fleet member with a committed desired state: no --state / --autoload loads. */
35    bool skip_startup;
36} mesh_driver_config;
37
38typedef struct mesh_driver mesh_driver;
39
40
41/* gpu: the GPU backend (or wasm itself: a CPU-only meshd); cpus: the CPU
42 * executors beside it. The driver owns the backends. */
43mesh_driver *mesh_driver_new(mesh_shared *sh, mesh_backend *gpu, mesh_backend **cpus, size_t ncpus, mesh_driver_config cfg);
44/* make_current, then the startup loads (--state, --autoload): false with *err. */
45bool mesh_driver_start(mesh_driver *d, char **err);
46/* The driver loop; returns the process exit code after SIGINT/SIGTERM (sh->quit). */
47int mesh_driver_run(mesh_driver *d);
48void mesh_driver_free(mesh_driver *d);
49
50/* Bit s set for every loaded name the manifest's `reads` lists ("*": READS_ALL). Exposed for tests. */
51uint32_t mesh_reads_mask(const mesh_manifest *m, const char *const *names, size_t n);
52
53/* A job's setup, one task at a time, in order (driver.rs SetupState). Exposed for tests. */
54typedef struct {
55    bool has_job;
56    uint64_t job;
57    size_t next;
58    bool busy;
59    char *err;
60} mesh_setup_state;
61
62typedef enum { MESH_SETUP_PUBLISH, MESH_SETUP_WAIT, MESH_SETUP_FAILED, MESH_SETUP_READY } mesh_setup_step_kind;
63
64typedef struct {
65    mesh_setup_step_kind k;
66    uint32_t op, arg;  /* PUBLISH */
67    const char *err;   /* FAILED (the state's) */
68} mesh_setup_step;
69
70mesh_setup_step mesh_setup_state_step(mesh_setup_state *s, uint64_t job, const mj_setup *setup, size_t n);
71void mesh_setup_state_published(mesh_setup_state *s);
72void mesh_setup_state_completed(mesh_setup_state *s, uint64_t job, const char *err);
73void mesh_setup_state_reset(mesh_setup_state *s);
74
75#endif