libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
os_cpu.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/os_cpu.h
6 * CPU + NUMA topology surface.
7 */
8
9#ifndef XTC_OS_CPU_H
10#define XTC_OS_CPU_H
11
12#include "xtc_export.h"
13
14#include <stdint.h>
15
16/*
17 * PUBLIC: int __os_ncpus __P((void));
18 * PUBLIC: int __os_ncpus_perf __P((void));
19 * PUBLIC: int __os_ncpus_effic __P((void));
20 * PUBLIC: int __os_numa_nnodes __P((void));
21 * PUBLIC: int __os_numa_node_of_cpu __P((int));
22 * PUBLIC: int __os_numa_current_node __P((void));
23 * PUBLIC: int64_t __os_mem_max __P((void));
24 */
25XTC_API int __os_ncpus(void);
26XTC_API int __os_ncpus_perf(void);
27XTC_API int __os_ncpus_effic(void);
28XTC_API int __os_numa_nnodes(void);
29XTC_API int __os_numa_node_of_cpu(int cpu);
30XTC_API int __os_numa_current_node(void);
31XTC_API int64_t __os_mem_max(void);
32
33/*
34 * Internal / test hooks (Linux only): point __os_ncpus / __os_mem_max
35 * at a fixture file instead of the real /sys/fs/cgroup/cpu.max or
36 * memory.max, so cgroup v2 parsing is unit-testable without root or a
37 * real cgroup. NULL restores the real path. Not part of the stable
38 * API. Linux-only: there is no cgroup path to override elsewhere.
39 */
40#if defined(__linux__)
41void __xtc_os_cgroup_cpu_path_override(const char *path);
42void __xtc_os_cgroup_mem_path_override(const char *path);
43#endif
44
45/*
46 * Spin-loop relaxation hint: tells the CPU we are in a busy-wait so it
47 * can save power and yield pipeline/SMT resources to the lock holder
48 * (x86 PAUSE, ARM YIELD). A no-op where unavailable. Header-only and
49 * always inlined -- it must be zero-cost in a tight CAS retry loop.
50 */
51static inline void
52__os_cpu_relax(void)
53{
54#if defined(__GNUC__) || defined(__clang__)
55# if defined(__i386__) || defined(__x86_64__)
56 __asm__ __volatile__("pause" ::: "memory");
57# elif defined(__aarch64__) || defined(__arm__)
58 __asm__ __volatile__("yield" ::: "memory");
59# else
60 __asm__ __volatile__("" ::: "memory"); /* compiler barrier only */
61# endif
62#else
63 /* MSVC / unknown compiler: no portable hint without <intrin.h>. */
64#endif
65}
66
67#endif /* XTC_OS_CPU_H */