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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! [`PinnedRunner`] — a thread-per-core driver for [`ShardedRegion<T>`]
//! (Phase 7c, `pinning`), plus the OS-affinity helpers it wraps.
//!
//! The locality apex of the sharded tier. The idea: pin worker thread *i* to
//! core *i* and explicitly [`bind`](ShardedRegion::bind_current_thread_to_shard)
//! it to shard *i*, so each thread deterministically owns the shard whose data
//! lives in its own core's cache. The hot path then has **no lock** and **no
//! cross-shard contention** — writers in different shards never meet — which is
//! also why this composes with async runtimes without "lock across `.await`"
//! hazards (there is no lock to hold).
//!
//! [`core_affinity`](::core_affinity) is a safe wrapper over the OS affinity
//! syscalls (`sched_setaffinity` on Linux, `SetThreadAffinityMask` on Windows,
//! `thread_policy_set` on macOS). This module adds **zero `unsafe`** of its own.
//!
//! ## Integrating with current-thread-per-core async runtimes
//!
//! The pattern is identical for [`glommio`], [`monoio`], and
//! [`tokio-rt-multi-thread`] pinned runtimes, and for a hand-rolled
//! `thread-per-core` executor:
//!
//! 1. At runtime startup, pin the worker thread to its core via
//! [`PinnedRunner::pin_current_thread_to_core`] (best-effort — some OSes /
//! sandboxes refuse affinity; the runner runs correctly either way).
//! 2. Bind that worker to the matching shard once via
//! [`ShardedRegion::bind_current_thread_to_shard`] (deterministic routing —
//! the auto round-robin claim cannot guarantee a specific id).
//! 3. Spawn tasks on that worker; every `insert`/`get_with`/`remove` from the
//! worker routes to its bound shard with no lock on the hot path.
//!
//! ```text
//! core 0 ── worker 0 ── shard 0 (cache-local: data + compute on core 0)
//! core 1 ── worker 1 ── shard 1
//! core 2 ── worker 2 ── shard 2
//! ...
//! ```
//!
//! Cross-shard reads (`get_with`) and cross-thread removes (`remote_evict`)
//! still work — they route by `handle.shard` and are lock-free — they just are
//! not the hot path you are optimizing.
//!
//! ## Honest verdict (workload-dependent)
//!
//! Pinning is **not** a guaranteed win. It helps when:
//! - the per-shard working set is cache-hot (repeated reads/writes to the same
//! slots benefit from L1/L2 residency), and
//! - shard access is truly thread-local (a worker mostly touches its own shard).
//!
//! It does little (and may add spawn cost) when:
//! - the workload is read-heavy with random cross-shard handles (every read is
//! already lock-free; locality is already good), or
//! - the working set far exceeds per-core cache (memory-bandwidth-bound), or
//! - the OS scheduler already places threads well on an idle machine.
//!
//! Measure with `benches/pinned_write.rs` for your own workload before
//! committing to the pinned topology.
//!
//! [`glommio`]: https://docs.rs/glommio
//! [`monoio`]: https://docs.rs/monoio
//! [`tokio-rt-multi-thread`]: https://docs.rs/tokio
//! [`ShardedRegion<T>`]: crate::concurrent::ShardedRegion
use Arc;
use scope;
use CoreId;
use crateShardedRegion;
/// A thread-per-core runner for [`ShardedRegion<T>`]: spawns one worker thread
/// per core, pins thread *i* to core *i*, binds it to shard *i*, and runs a
/// per-worker closure. Zero `unsafe` — [`core_affinity`] is a safe wrapper.
///
/// Construct with [`PinnedRunner::new`] (which probes the host's available
/// cores); the runner's worker count is `min(cores, region.shard_count())` so a
/// region with fewer shards than cores is not over-subscribed. Use
/// [`PinnedRunner::with_workers`] to cap the worker count explicitly.
///
/// ## Example
///
/// ```no_run
/// # #[cfg(feature = "pinning")] {
/// use sefer_alloc::{PinnedRunner, ShardedRegion};
/// use std::sync::Arc;
///
/// let region = Arc::new(ShardedRegion::<u64>::new());
/// let runner = PinnedRunner::new(®ion).expect("no cores available");
/// runner.run(®ion, |shard_id, region: &ShardedRegion<u64>| {
/// let h = region.insert(u64::from(shard_id)).unwrap();
/// assert_eq!(h.shard(), shard_id); // deterministic: shard == worker == core
/// });
/// # }
/// ```
///
/// ## Pinning is best-effort
///
/// On some OSes / sandboxes the affinity syscall is refused (containers
/// without `CAP_SYS_NICE`, some CI runners, etc.). The runner STILL runs
/// correctly in that case — it just cannot guarantee the thread stayed on the
/// chosen core. The shard BINDING (which is what makes routing deterministic)
/// is always honored, so tests assert routing, not affinity.