1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Process-global per-CPU cache state: enable/disable toggles, the
//! backend-keying-invariant static, and the cached-current-CPU-id helpers.
use ;
use ;
pub const MAX_CACHED_BLOCKS: usize = 8;
pub const NUM_CPU_SLOTS: usize = 256;
/// Whether the per-CPU block cache is consulted on the alloc/free fast
/// path.
///
/// Disabled under `cfg(test)` so tests observe the thread-local path
/// deterministically: the per-CPU cache can otherwise satisfy a request
/// from another CPU's slot and make occupancy assertions flaky.
pub static PER_CPU_CACHE_ENABLED: AtomicBool = new;
/// Whether the per-CPU block cache is consulted on the alloc/free fast
/// path.
pub static PER_CPU_CACHE_ENABLED: AtomicBool = new;
/// The global per-CPU cache instance.
///
/// # Backend-keying invariant (latent hazard)
///
/// This is a single process-global array, **not** keyed by memory backend. The
/// slots store raw block addresses, so a block freed here under one backend and
/// re-handed out under another would cross backend ownership. `try_alloc_cpu` /
/// `try_free_cpu` only touch it when `B::ENABLE_CPU_CACHE` is `true`, and today
/// no backend sets that constant to `true` (the `MemoryBackend` trait default
/// is `false`, and neither the Unix nor Windows `DefaultBackend` overrides it),
/// so exactly one backend — none, currently — can ever populate the cache and
/// no mixing is possible.
///
/// The correctness of the shared static therefore rests on the invariant that
/// **at most one backend enables the CPU cache per process**. Keying the cache
/// per backend (an associated-type or generic buffer on `ComputeBackend`) is the
/// robust fix but is a `[minor]`-class change (new trait surface); until then this
/// invariant is documented rather than type-enforced. Any future backend that
/// sets `ENABLE_CPU_CACHE = true` alongside another such backend must first
/// introduce that per-backend keying.
pub static PER_CPU_CACHE: PerCpuCacheHandle = new;
const _: =
assert!;
pub static DISABLE_CPU_CACHE: AtomicBool = new;
/// Dynamically disables the per-CPU cache.
/// Dynamically enables the per-CPU cache.
/// Returns the current CPU ID.
thread_cached!
/// Returns the cached CPU ID, or queries the OS and caches it if uninitialized.
/// Force-refreshes the cached CPU ID from the OS.