libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_preempt.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_preempt.h
6 * Per-worker preemption timer seam.
7 *
8 * Phase 0 (this): a per-thread CPU-time interval timer whose handler
9 * records a tick. No preemption yet -- the seam later phases build
10 * on. OFF by default (nothing arms it unless xtc_preempt_arm is
11 * called); the cooperative fast path is unchanged when unused.
12 */
13
14#ifndef XTC_PREEMPT_H
15#define XTC_PREEMPT_H
16
17#include "xtc_export.h"
18
19#include <stdint.h>
20
21#include "xtc.h"
22
23/*
24 * Arm a per-thread preemption timer that fires every `interval_ns` of
25 * THIS thread's CPU time (not wall time), delivering SIGVTALRM to this
26 * thread. Idempotent re-arm re-sets the interval. Returns XTC_OK, or
27 * XTC_E_NOSYS where POSIX per-thread CPU-time timers are unavailable, or
28 * XTC_E_INVAL for interval_ns <= 0. Call from the worker thread it
29 * should time.
30 *
31 * PUBLIC: int xtc_preempt_arm __P((int64_t));
32 * PUBLIC: int xtc_preempt_disarm __P((void));
33 * PUBLIC: int xtc_preempt_supported __P((void));
34 * PUBLIC: uint64_t xtc_preempt_ticks __P((void));
35 * PUBLIC: int xtc_preempt_tick_pending __P((void));
36 */
37XTC_API int xtc_preempt_arm(int64_t interval_ns);
38
39/* Stop + delete this thread's preemption timer. Safe if not armed. */
40XTC_API int xtc_preempt_disarm(void);
41
42/* Enable (on != 0) / disable signal-context involuntary yield (Phase
43 * 2). When on and the timer is armed, a tick preempts the running
44 * fiber in the handler -- resumably and only when safe (crit_depth ==
45 * 0, unsafe_depth == 0) -- on the ucontext substrate; the fctx/winfiber
46 * substrate declines and falls back to cooperative-assisted preemption.
47 * Off by default.
48 *
49 * PUBLIC: void xtc_preempt_set_involuntary __P((int));
50 */
51XTC_API void xtc_preempt_set_involuntary(int on);
52
53/* 1 if per-thread CPU-time preemption timers are available on this
54 * platform, 0 otherwise (arm returns NOSYS then). */
55XTC_API int xtc_preempt_supported(void);
56
57/* Total timer ticks this thread has observed since arming (Phase 0
58 * seam-works metric / telemetry). */
59XTC_API uint64_t xtc_preempt_ticks(void);
60
61/* 1 if a timer tick fired and is unconsumed; consumes (clears) it.
62 * Phase 1 consults this at safe points to decide whether to yield. */
63XTC_API int xtc_preempt_tick_pending(void);
64
65/*
66 * The internal async-signal-unsafe bracket (__xtc_unsafe_enter/leave/
67 * depth) and the preemption-safe raw-pthread mutex wrappers
68 * (__xtc_mtx_lock/unlock) are library-internal and live in
69 * "preempt_int.h" (the __ prefix marks them internal, so they must not
70 * appear in this installed public header).
71 */
72
73#endif /* XTC_PREEMPT_H */