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 * @stability stable
4 * Use of this source code is governed by the ISC License.
5 *
6 * src/inc/xtc_preempt.h
7 * Per-worker preemption timer seam.
8 *
9 * Phase 0 (this): a per-thread CPU-time interval timer whose handler
10 * records a tick. No preemption yet -- the seam later phases build
11 * on. OFF by default (nothing arms it unless xtc_preempt_arm is
12 * called); the cooperative fast path is unchanged when unused.
13 */
14
15#ifndef XTC_PREEMPT_H
16#define XTC_PREEMPT_H
17
18#include "xtc_export.h"
19
20#include <stdint.h>
21
22#include "xtc.h"
23
24/*
25 * Arm a per-thread preemption timer that fires every `interval_ns` of
26 * THIS thread's CPU time (not wall time), delivering SIGVTALRM to this
27 * thread. Idempotent re-arm re-sets the interval. Returns XTC_OK, or
28 * XTC_E_NOSYS where POSIX per-thread CPU-time timers are unavailable, or
29 * XTC_E_INVAL for interval_ns <= 0. Call from the worker thread it
30 * should time.
31 *
32 * PUBLIC: int xtc_preempt_arm __P((int64_t));
33 * PUBLIC: int xtc_preempt_disarm __P((void));
34 * PUBLIC: int xtc_preempt_supported __P((void));
35 * PUBLIC: uint64_t xtc_preempt_ticks __P((void));
36 * PUBLIC: int xtc_preempt_tick_pending __P((void));
37 */
38XTC_API int xtc_preempt_arm(int64_t interval_ns);
39
40/* Stop + delete this thread's preemption timer. Safe if not armed. */
41XTC_API int xtc_preempt_disarm(void);
42
43/* Involuntary (signal-context) preemption is EXPERIMENTAL and lives in
44 * "xtc_preempt_involuntary.h". */
45
46/* 1 if per-thread CPU-time preemption timers are available on this
47 * platform, 0 otherwise (arm returns NOSYS then). */
48XTC_API int xtc_preempt_supported(void);
49
50/* Total timer ticks this thread has observed since arming (Phase 0
51 * seam-works metric / telemetry). */
52XTC_API uint64_t xtc_preempt_ticks(void);
53
54/* 1 if a timer tick fired and is unconsumed; consumes (clears) it.
55 * Phase 1 consults this at safe points to decide whether to yield. */
56XTC_API int xtc_preempt_tick_pending(void);
57
58/*
59 * The internal async-signal-unsafe bracket (__xtc_unsafe_enter/leave/
60 * depth) and the preemption-safe raw-pthread mutex wrappers
61 * (__xtc_mtx_lock/unlock) are library-internal and live in
62 * "preempt_int.h" (the __ prefix marks them internal, so they must not
63 * appear in this installed public header).
64 */
65
66#endif /* XTC_PREEMPT_H */