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 "xtc_export.h"
34
35#include <stdint.h>
36
37#include "xtc.h"
38#include "xtc_loop.h"
39#include "xtc_proc.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/* The launched function. Runs on its own fiber; its intptr_t return is
46 * delivered to the launcher on a clean finish. */
47typedef intptr_t (*xtc_launch_fn)(void *arg);
48
49/* The exit reason xtc_launch uses when it cancels a timed-out child, so
50 * the child's DOWN is distinguishable from a fault. */
51#define XTC_LAUNCH_CANCEL_REASON 0x4c43 /* 'LC' */
52
53typedef struct xtc_launch_opts {
54 const char *name; /* child proc label (logs); may be NULL */
55 size_t mailbox_cap; /* child mailbox cap; 0 = default */
56 int loop; /* exec loop index when launched on an exec
57 * loop (via xtc_exec_loop); 0 default */
59
60/*
61 * Launch fn(arg) on `loop` with a `timeout_ns` deadline (< 0 = no
62 * deadline, i.e. a plain awaited spawn). A NULL `loop` means the loop
63 * the caller is currently running on (the natural default when
64 * launching from inside a fiber). Blocks (parks) the caller until fn
65 * finishes or the deadline fires.
66 *
67 * On a clean finish within the deadline: returns XTC_OK and, if result
68 * != NULL, stores fn's return value.
69 *
70 * On deadline: cancels the child (xtc_exit_pid -> its recovery/at-exit
71 * cleanup releases resources) and returns XTC_E_AGAIN (the runtime's
72 * timeout/try-again code, as xtc_recv / xtc_svr_call use).
73 *
74 * On a fault inside fn (contained by the per-fiber recovery): returns
75 * XTC_E_ABORTED.
76 *
77 * Other returns: XTC_E_INVAL (bad args), XTC_E_NOMEM / spawn failure.
78 *
79 * PUBLIC: int xtc_launch __P((xtc_loop_t *, xtc_launch_fn, void *, int64_t, const xtc_launch_opts_t *, intptr_t *));
80 */
81XTC_API int xtc_launch(xtc_loop_t *loop, xtc_launch_fn fn, void *arg,
82 int64_t timeout_ns, const xtc_launch_opts_t *opts,
83 intptr_t *result);
84
85#ifdef __cplusplus
86}
87#endif
88
89#endif /* XTC_LAUNCH_H */