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