libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_launch.h
1/*-
2 * Copyright (c) 2026, The XTC Project -- All rights reserved.
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_launch.h
6 * Bounded-time / cancellable work: xtc_launch (M_PREEMPTION Phase 3,
7 * the libinger launch(f, timeout) model, reimplemented natively).
8 *
9 * Run fn(arg) on a child fiber with a one-shot DEADLINE. If fn
10 * finishes within the deadline, its return value is delivered; if it
11 * exceeds the deadline it is CANCELLED at the deadline (its recovery
12 * / at-exit cleanup runs, so locks, fds, and memory contexts it
13 * registered are released -- no leak) and xtc_launch returns
14 * XTC_E_TIMEDOUT. This is the statement-timeout / bounded-untrusted-
15 * work primitive.
16 *
17 * Precise-timeout on a RUNAWAY (a fn that never reaches a cooperative
18 * yield point) requires involuntary preemption to be enabled on the
19 * executor (xtc_exec_set_preempt + xtc_preempt_set_involuntary(1), on
20 * the arches where it is effective); otherwise a purely-uncooperative
21 * fn can only be cancelled once it next reaches a safe point. A
22 * cooperating fn (one that yields, recvs, or does I/O) is bounded at
23 * its next such point regardless.
24 *
25 * Composable: a launched fn may itself xtc_launch. Must be called
26 * from a fiber (it parks the caller waiting on the child); off a
27 * fiber it drives the loop.
28 */
29
30#ifndef XTC_LAUNCH_H
31#define XTC_LAUNCH_H
32
33#include <stdint.h>
34
35#include "xtc.h"
36#include "xtc_loop.h"
37#include "xtc_proc.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/* The launched function. Runs on its own fiber; its intptr_t return is
44 * delivered to the launcher on a clean finish. */
45typedef intptr_t (*xtc_launch_fn)(void *arg);
46
47/* The exit reason xtc_launch uses when it cancels a timed-out child, so
48 * the child's DOWN is distinguishable from a fault. */
49#define XTC_LAUNCH_CANCEL_REASON 0x4c43 /* 'LC' */
50
51typedef struct xtc_launch_opts {
52 const char *name; /* child proc label (logs); may be NULL */
53 size_t mailbox_cap; /* child mailbox cap; 0 = default */
54 int loop; /* exec loop index when launched on an exec
55 * loop (via xtc_exec_loop); 0 default */
57
58/*
59 * Launch fn(arg) on `loop` with a `timeout_ns` deadline (< 0 = no
60 * deadline, i.e. a plain awaited spawn). A NULL `loop` means the loop
61 * the caller is currently running on (the natural default when
62 * launching from inside a fiber). Blocks (parks) the caller until fn
63 * finishes or the deadline fires.
64 *
65 * On a clean finish within the deadline: returns XTC_OK and, if result
66 * != NULL, stores fn's return value.
67 *
68 * On deadline: cancels the child (xtc_exit_pid -> its recovery/at-exit
69 * cleanup releases resources) and returns XTC_E_AGAIN (the runtime's
70 * timeout/try-again code, as xtc_recv / xtc_svr_call use).
71 *
72 * On a fault inside fn (contained by the per-fiber recovery): returns
73 * XTC_E_ABORTED.
74 *
75 * Other returns: XTC_E_INVAL (bad args), XTC_E_NOMEM / spawn failure.
76 *
77 * PUBLIC: int xtc_launch __P((xtc_loop_t *, xtc_launch_fn, void *, int64_t, const xtc_launch_opts_t *, intptr_t *));
78 */
79int xtc_launch(xtc_loop_t *loop, xtc_launch_fn fn, void *arg,
80 int64_t timeout_ns, const xtc_launch_opts_t *opts,
81 intptr_t *result);
82
83#ifdef __cplusplus
84}
85#endif
86
87#endif /* XTC_LAUNCH_H */