libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_loop.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_loop.h
6 * The L2 single-thread event loop, run-queue, timer-heap, and
7 * task state machine. See M3_CLAIMS.md.
8 */
9
10#ifndef XTC_LOOP_H
11#define XTC_LOOP_H
12
13#include <stdint.h>
14
15#include "xtc_io.h"
16
17typedef struct xtc_loop xtc_loop_t;
18typedef struct xtc_task xtc_task_t;
19typedef struct xtc_timer xtc_timer_t;
20
21/*
22 * A task's main function. Called once per "step" by the loop.
23 * Returns one of the XTC_TASK_* codes below to tell the loop what to
24 * do next.
25 * XTC_TASK_DONE The task is finished; the loop reaps it.
26 * XTC_TASK_RESCHED Re-queue me at the back of the run queue.
27 * XTC_TASK_PENDING I am waiting on a waker; do not run me again
28 * until xtc_waker_wake (or a timer / fd that
29 * I parked on) fires.
30 */
31typedef int (*xtc_task_fn)(xtc_task_t *self, void *user);
32
33#define XTC_TASK_DONE 0
34#define XTC_TASK_RESCHED 1
35#define XTC_TASK_PENDING 2
36
37/*
38 * The waker is a value-type cookie (loop, task) used to re-schedule
39 * a parked task. In M3 wakers are valid only on the owning thread;
40 * cross-thread wake arrives in M5.
41 */
42typedef struct xtc_waker {
43 xtc_loop_t *loop;
44 xtc_task_t *task;
46
47/* Timer callback. */
48typedef void (*xtc_timer_fn)(void *user);
49
50/*
51 * PUBLIC: int xtc_loop_init __P((xtc_loop_t **));
52 * PUBLIC: int xtc_loop_fini __P((xtc_loop_t *));
53 * PUBLIC: int xtc_loop_run __P((xtc_loop_t *));
54 * PUBLIC: int xtc_loop_stop __P((xtc_loop_t *));
55 *
56 * PUBLIC: int xtc_task_spawn __P((xtc_loop_t *, xtc_task_fn, void *, xtc_task_t **));
57 * PUBLIC: int xtc_task_waker __P((xtc_task_t *, xtc_waker_t *));
58 * PUBLIC: int xtc_task_park_on_timer __P((xtc_task_t *, int64_t));
59 * PUBLIC: int xtc_task_park_on_fd __P((xtc_task_t *, int, uint32_t));
60 *
61 * PUBLIC: int xtc_waker_wake __P((const xtc_waker_t *));
62 *
63 * PUBLIC: int xtc_timer_set __P((xtc_loop_t *, int64_t, xtc_timer_fn, void *, xtc_timer_t **));
64 * PUBLIC: int xtc_timer_cancel __P((xtc_timer_t *));
65 */
66int xtc_loop_init(xtc_loop_t **out);
67int xtc_loop_fini(xtc_loop_t *loop);
68int xtc_loop_run(xtc_loop_t *loop);
69int xtc_loop_stop(xtc_loop_t *loop);
70
71/*
72 * Borrow the loop's resource accountant. Non-NULL after init.
73 * The pointer is owned by the loop and must not be freed by the
74 * caller; it is reset on xtc_loop_fini.
75 *
76 * PUBLIC: struct xtc_res *xtc_loop_res __P((xtc_loop_t *));
77 */
78struct xtc_res;
79struct xtc_res *xtc_loop_res(xtc_loop_t *loop);
80
81int xtc_task_spawn(xtc_loop_t *loop, xtc_task_fn fn, void *user,
82 xtc_task_t **out_task);
83int xtc_task_waker(xtc_task_t *task, xtc_waker_t *out);
84
85/*
86 * Park the calling task on a timer. The task's fn returned PENDING
87 * (or is about to); after delay_ns elapses, the loop will re-run fn.
88 */
89int xtc_task_park_on_timer(xtc_task_t *self, int64_t delay_ns);
90
91/*
92 * Park the calling task on fd readiness. When the fd becomes ready
93 * for any of the requested interest bits, the loop unregisters the
94 * fd and re-runs the task fn.
95 *
96 * A task may have at most one outstanding park (timer or fd) at any
97 * moment; a second park while the first is in flight returns
98 * XTC_E_INVAL.
99 */
100int xtc_task_park_on_fd(xtc_task_t *self, int fd, uint32_t interest);
101
102int xtc_waker_wake(const xtc_waker_t *w);
103
104int xtc_timer_set(xtc_loop_t *loop, int64_t delay_ns,
105 xtc_timer_fn fn, void *user, xtc_timer_t **out_timer);
106int xtc_timer_cancel(xtc_timer_t *timer);
107
108#endif /* XTC_LOOP_H */