skip to content
==> include/mesh/worker.h <==
1/* @ajax/mesh-worker: the SDK of native worker programs (mesh-worker lib.rs).
2 * A native program is an ordinary C program built for the machine it runs
3 * on, with full access to its hardware:
4 *
5 *     static void init(const mesh_worker_ctx *c, void *u) { mesh_ctx_set_u32(c, 0, 0x4f4c4548); }
6 *     static uint32_t task(const mesh_worker_ctx *c, uint32_t op, uint32_t arg, void *u) {
7 *         if (op != MESH_OP_TASK) return MESH_R_BAD_OP;
8 *         mesh_ctx_count(c, 0, 1);
9 *         return arg * 3 + 7;
10 *     }
11 *     int main(void) { mesh_worker_run(&(mesh_worker_program){init, task, NULL}); }
12 *
13 * meshd starts the process (@ajax/mesh-native), which maps meshd's mailbox
14 * and plays mesh-rt's node loop on its slot's ring: one thread per node.
15 * init and task run once per ticket on a node's thread, so they must be
16 * thread-safe.  A crash (mesh_worker_panic, or a signal) ends the process:
17 * meshd marks the program FAILED with the exit status and the output's tail;
18 * nothing else stops. */
19#ifndef MESH_WORKER_H
20#define MESH_WORKER_H
21
22#include "mesh/worker_node.h"
23
24/* The worker's main loop: checks the native ABI, maps meshd's mailbox
25 * (NATIVE_MAILBOX_FD), says `ready <abi>` on NATIVE_STATUS_FD, then runs a
26 * slot's nodes at each `run <gen> <slot>` until they halt, says `halted
27 * <gen>`, and exits when P_SHUTDOWN was SHUTDOWN_EXIT, on `exit`, or when
28 * meshd is gone (EOF on stdin).  Call it last in main, after the program's
29 * own setup: `ready` tells meshd that setup is done. */
30_Noreturn void mesh_worker_run(const mesh_worker_program *p);
31/* The program's bug: prints "mesh-worker: panic: <message>" and leaves at
32 * once with status 101 (Rust's panic status), no core dump. */
33_Noreturn void mesh_worker_panic(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
34
35#endif