libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
coro_int.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/coro_int.h
6 * Internal types for the L2 coroutine substrate.
7 */
8
9#ifndef XTC_CORO_INT_H
10#define XTC_CORO_INT_H
11
12#if defined(_WIN32)
13# include <windows.h>
14#elif defined(XTC_HAVE_UCONTEXT) && !defined(XTC_CORO_FORCE_FCTX)
15# include <ucontext.h>
16#endif
17
18#include "xtc_async.h"
19#include "loop_int.h"
20
21/*
22 * The fiber context attached to an xtc_task_t when it was spawned
23 * via xtc_async. Lives in the per-task arena (currently malloc).
24 */
25struct xtc_coro {
26#if defined(_WIN32)
27 LPVOID fiber; /* the coroutine's own Win32 fiber */
28 LPVOID loop_fiber; /* return-to-loop fiber pointer */
29#elif defined(XTC_HAVE_UCONTEXT) && !defined(XTC_CORO_FORCE_FCTX)
30 ucontext_t ctx; /* the coroutine's own machine state */
31 ucontext_t loop_ctx; /* return-to-loop context (set on resume) */
32#else
33 /* fcontext substrate (coro_fctx.c): a single saved stack pointer.
34 * On make_fcontext it is the fresh entry point; each yield/await
35 * overwrites it with the coroutine's current resume point. The
36 * scheduler's return point is a per-thread cursor in coro_fctx.c,
37 * not stored here, because coroutines always return to the
38 * scheduler, never directly to one another. */
39 void *fctx;
40#endif
41 void *stack;
42 size_t stack_sz;
43 xtc_coro_fn fn;
44 void *arg;
45 intptr_t result;
46 int done; /* 1 once fn has returned */
47
48 xtc_task_t *self; /* back-pointer to our task */
49 xtc_task_t *waiter; /* task awaiting this one (or NULL) */
50
51 /*
52 * When non-NULL, this coroutine has just registered itself as
53 * the `waiter` of another and is yielding into the loop with
54 * the intent of staying parked rather than rescheduling. The
55 * step function reads and clears this flag to decide between
56 * RESCHED and PENDING.
57 */
58 struct xtc_coro *_parked_on;
59};
60
61/* Shared by loop.c -- the currently-running coroutine on this loop. */
62extern XTC_THREAD_LOCAL struct xtc_coro *__xtc_current_coro;
63
64/* Forward declarations for the dispatch glue. */
65int __xtc_coro_step(xtc_task_t *self, void *user);
66
67/* The task wrapping the currently-running coroutine on this thread,
68 * or NULL when not running inside a coroutine. Lets lower-level
69 * primitives (e.g. xtc_amutex) find the current task to park it. */
70xtc_task_t *__xtc_current_task(void);
71
72#endif /* XTC_CORO_INT_H */