libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_app.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_app.h
6 * The L4 application container: a root supervisor + a process
7 * registry + lifecycle plumbing. Models OTP's `application`
8 * concept. An xtc_app owns a loop (creating one if not given
9 * or borrowing one passed in), starts a top-level supervisor
10 * with the configured children, and exposes the registry that
11 * those children can use to find each other by name.
12 *
13 * Typical usage:
14 *
15 * xtc_app_t *app;
16 * xtc_app_opts_t opts = XTC_APP_OPTS_DEFAULT;
17 * xtc_app_create(&opts, &app);
18 * xtc_app_start(app, child_specs, n_children);
19 * xtc_app_run(app); // blocks until app stops
20 * xtc_app_destroy(app);
21 */
22
23#ifndef XTC_APP_H
24#define XTC_APP_H
25
26#include <stddef.h>
27
28#include "xtc.h"
29#include "xtc_loop.h"
30#include "xtc_exec.h"
31#include "xtc_orc.h"
32#include "xtc_reg.h"
33
34typedef struct xtc_app xtc_app_t;
35
36typedef struct xtc_app_opts {
37 const char *name; /* optional, for logs */
38 xtc_loop_t *loop; /* optional; NULL = create one */
39 int n_loops; /* >1 = own an N-loop executor and run
40 * the root supervisor on loop 0 (the
41 * other loops host children placed via
42 * xtc_child_spec.loop, and any work the
43 * children spawn via xtc_app_exec); 0/1
44 * = single loop. Ignored when `loop`
45 * is supplied. */
46 xtc_sup_opts_t sup; /* root-supervisor settings */
48
49#define XTC_APP_OPTS_DEFAULT { \
50 .name = NULL, \
51 .loop = NULL, \
52 .n_loops = 1, \
53 .sup = XTC_SUP_OPTS_DEFAULT \
54}
55
56/*
57 * PUBLIC: int xtc_app_create __P((const xtc_app_opts_t *, xtc_app_t **));
58 * PUBLIC: void xtc_app_destroy __P((xtc_app_t *));
59 * PUBLIC: int xtc_app_start __P((xtc_app_t *, const xtc_child_spec_t *, int));
60 * PUBLIC: int xtc_app_run __P((xtc_app_t *));
61 * PUBLIC: int xtc_app_stop __P((xtc_app_t *));
62 * PUBLIC: xtc_loop_t *xtc_app_loop __P((const xtc_app_t *));
63 * PUBLIC: xtc_exec_t *xtc_app_exec __P((const xtc_app_t *));
64 * PUBLIC: xtc_reg_t *xtc_app_registry __P((const xtc_app_t *));
65 */
66
67int xtc_app_create(const xtc_app_opts_t *opts, xtc_app_t **out);
68void xtc_app_destroy(xtc_app_t *app);
69
70/* Start the root supervisor with `n_children` initial children. */
71int xtc_app_start(xtc_app_t *app,
72 const xtc_child_spec_t *children,
73 int n_children);
74
75/* Run the loop until the supervisor exits or xtc_app_stop is called.
76 * On return, xtc_app_destroy may be called. */
77int xtc_app_run(xtc_app_t *app);
78
79/* Asynchronously request the app to stop (kicks the root sup). */
80int xtc_app_stop(xtc_app_t *app);
81
82xtc_loop_t *xtc_app_loop(const xtc_app_t *app);
83
84/* The executor backing a multi-loop app (n_loops > 1), or NULL for a
85 * single-loop app. Children use it to spawn work across loops -- e.g.
86 * a listener spawning a proc per connection round-robin. */
87xtc_exec_t *xtc_app_exec(const xtc_app_t *app);
88
89xtc_reg_t *xtc_app_registry(const xtc_app_t *app);
90
91#endif /* XTC_APP_H */