libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
coro_select.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_select.h
6 * Which coroutine substrate this build uses. Defines XTC_CORO_FCTX
7 * when the hand-written fcontext substrate (coro_fctx.c) is live;
8 * otherwise ucontext (coro_uctx.c) is, or Win32 fibers on Windows.
9 * Every site that depends on the choice (coro_fctx.c, coro_uctx.c,
10 * coro_int.h's struct xtc_coro) tests this one macro, so they cannot
11 * disagree and struct xtc_coro's layout cannot drift between
12 * translation units. Include after xtc_int.h (xtc_config.h).
13 *
14 * fcontext is live when not Windows and:
15 * - the libc has no ucontext (musl), or
16 * - forced: -DXTC_CORO_FORCE_FCTX / configure --with-coro=fctx, or
17 * - by default on Linux x86-64 and aarch64 with glibc, and on
18 * Apple arm64. ucontext's swapcontext makes a rt_sigprocmask
19 * system call on every switch (two per round trip), and on Linux
20 * that call takes the PROCESS-wide sighand lock: with many loops
21 * every fiber switch in the process serializes on one kernel
22 * spinlock. Measured on a 64-vCPU x86-64 host: 16 loops of
23 * cross-loop request/reply 2.2M round trips/s on ucontext (56% of
24 * cycles spinning on that lock), 12.1M on fcontext; one fiber
25 * switch 550 -> 145 ns. On a 16-vCPU aarch64 host (Neoverse-V1):
26 * one switch 1091 -> 136 ns, 16 loops 2.08M -> 8.91M round
27 * trips/s (ucontext fell from 2.45M at 8 loops). fcontext has
28 * the same involuntary-preemption redirect on both
29 * (coro_preempt.h).
30 * ucontext is kept by -DXTC_CORO_FORCE_UCONTEXT / --with-coro=ucontext
31 * and in the single-file amalgamation (it cannot carry the .S files).
32 */
33
34#ifndef XTC_CORO_SELECT_H
35#define XTC_CORO_SELECT_H
36
37#include "xtc_int.h" /* xtc_config.h: XTC_HAVE_UCONTEXT */
38
39#if !defined(_WIN32) && \
40 (!defined(XTC_HAVE_UCONTEXT) || defined(XTC_CORO_FORCE_FCTX) || \
41 (!defined(XTC_CORO_FORCE_UCONTEXT) && !defined(XTC_AMALGAMATION) && \
42 ((defined(__APPLE__) && defined(__aarch64__)) || \
43 (defined(__linux__) && \
44 (defined(__x86_64__) || defined(__aarch64__)) && \
45 defined(__GLIBC__)))))
46# define XTC_CORO_FCTX 1
47#endif
48
49#endif /* XTC_CORO_SELECT_H */