subetha-cxc 0.1.12

MMF-backed cross-process IPC primitives for SubEtha: SharedRing, SharedHashMap, SharedRWLock, SharedSemaphore, SharedLRUCache, OwnerLease, HeartbeatTable, plus 30+ more. One byte layout serves cross-thread, cross-process, and disk-persistent.
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! `BlockingMpmcRing`: composed-SPSC MPMC grid with cross-process
//! futex-shaped `send_blocking` / `recv_blocking`.
//!
//! Wraps [`crate::mpmc_ring::SharedRingMpmc`] (N independent Lamport
//! SPSC rings, statically partitioned across M consumers
//! round-robin: consumer `m` owns rings `m, m + M, m + 2M, ...`).
//!
//! Wake routing:
//! - Each producer parks on its own ring's `producer_waker[i]` when
//!   the ring is full. The owning consumer wakes that specific
//!   waker after popping from ring `i`.
//! - Each consumer parks on its own `consumer_waker[m]` when every
//!   ring in its subset is empty. A producer publishing to ring `i`
//!   wakes `consumer_waker[i % M]` only.
//!
//! Per-subset shared `total_published[m]` counter drives the wake
//! seq so consumer parks at `total_published[m] + 1`.
//!
//! See [`crate::cross_process_waker`] for the wake protocol and
//! [`crate::blocking_spsc_ring::BlockingSpscRing`] /
//! [`crate::blocking_mpsc_ring::BlockingMpscRing`] for the simpler
//! shapes.

use std::cell::Cell;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::{Duration, Instant};

use crate::blocking_spsc_ring::BlockingError;
use crate::cross_process_waker::{
    CrossProcessWaker, MAX_WAITERS_DEFAULT, WakerError,
};
use crate::shared_ring::RingError;
use crate::spsc_ring::SpscRingCore;

const PRE_PARK_SPIN: u32 = 32;

/// Factory for an MPMC grid of N SPSC rings partitioned across M
/// consumers, every blocking call backed by a cross-process waker.
pub struct BlockingMpmcRing;

/// One producer handle. Sole writer to one underlying SPSC ring.
/// `!Sync + !Clone + Send`: one producer per thread.
pub struct BlockingMpmcProducer {
    ring: Arc<SpscRingCore>,
    own_waker: Arc<CrossProcessWaker>,
    /// Consumer waker for the subset that owns this ring
    /// (`subset = ring_index % n_consumers`).
    consumer_waker: Arc<CrossProcessWaker>,
    /// Shared total-published counter for the subset that owns this
    /// ring (drives the consumer-side wake seq).
    subset_total_published: Arc<AtomicU64>,
    _not_sync: PhantomData<Cell<()>>,
}

/// One consumer handle. Sole drainer of an assigned subset of
/// producer rings (round-robin from the factory).
/// `!Sync + !Clone + Send`.
pub struct BlockingMpmcConsumer {
    /// Producer rings in this consumer's subset.
    rings: Vec<Arc<SpscRingCore>>,
    /// Per-ring producer-side wakers (parallel to `rings`).
    producer_wakers: Vec<Arc<CrossProcessWaker>>,
    /// This consumer's own waker.
    own_waker: Arc<CrossProcessWaker>,
    /// Counter shared with all producers in this consumer's subset.
    subset_total_published: Arc<AtomicU64>,
    next_drain: AtomicUsize,
    _not_sync: PhantomData<Cell<()>>,
}

impl BlockingMpmcRing {
    /// In-process grid: N rings, M consumer subsets, anon-mapped.
    pub fn create_anon_grid(
        n_producers: usize,
        n_consumers: usize,
        capacity: usize,
    ) -> Result<(Vec<BlockingMpmcProducer>, Vec<BlockingMpmcConsumer>), BlockingError>
    {
        assert!(n_consumers >= 1, "n_consumers must be >= 1");
        assert!(
            n_producers >= n_consumers,
            "n_producers ({n_producers}) must be >= n_consumers ({n_consumers})",
        );
        let mut rings = Vec::with_capacity(n_producers);
        let mut producer_wakers = Vec::with_capacity(n_producers);
        for _ in 0..n_producers {
            rings.push(Arc::new(
                SpscRingCore::create_anon(capacity).map_err(BlockingError::from)?,
            ));
            producer_wakers.push(Arc::new(
                CrossProcessWaker::create_anon(MAX_WAITERS_DEFAULT)
                    .map_err(BlockingError::from)?,
            ));
        }
        let mut consumer_wakers = Vec::with_capacity(n_consumers);
        let mut subset_total_published = Vec::with_capacity(n_consumers);
        for _ in 0..n_consumers {
            consumer_wakers.push(Arc::new(
                CrossProcessWaker::create_anon(MAX_WAITERS_DEFAULT)
                    .map_err(BlockingError::from)?,
            ));
            subset_total_published.push(Arc::new(AtomicU64::new(0)));
        }
        Ok(build_grid(
            rings,
            producer_wakers,
            consumer_wakers,
            subset_total_published,
            n_consumers,
        ))
    }

    /// File-backed grid. Path layout:
    ///   `<prefix>.ring.{i}.bin`  - SPSC ring for producer `i`
    ///   `<prefix>.pw.{i}.bin`    - producer-side waker for ring `i`
    ///   `<prefix>.cw.{m}.bin`    - consumer-side waker for subset `m`
    pub fn create_grid(
        path_prefix: impl AsRef<Path>,
        n_producers: usize,
        n_consumers: usize,
        capacity: usize,
    ) -> Result<(Vec<BlockingMpmcProducer>, Vec<BlockingMpmcConsumer>), BlockingError>
    {
        assert!(n_consumers >= 1, "n_consumers must be >= 1");
        assert!(n_producers >= n_consumers, "n_producers must be >= n_consumers");
        let base = path_prefix.as_ref().to_path_buf();
        let mut rings = Vec::with_capacity(n_producers);
        let mut producer_wakers = Vec::with_capacity(n_producers);
        for i in 0..n_producers {
            rings.push(Arc::new(
                SpscRingCore::create(ring_path(&base, i), capacity)
                    .map_err(BlockingError::from)?,
            ));
            producer_wakers.push(Arc::new(
                CrossProcessWaker::create(pw_path(&base, i), MAX_WAITERS_DEFAULT)
                    .map_err(BlockingError::from)?,
            ));
        }
        let mut consumer_wakers = Vec::with_capacity(n_consumers);
        let mut subset_total_published = Vec::with_capacity(n_consumers);
        for m in 0..n_consumers {
            consumer_wakers.push(Arc::new(
                CrossProcessWaker::create(cw_path(&base, m), MAX_WAITERS_DEFAULT)
                    .map_err(BlockingError::from)?,
            ));
            subset_total_published.push(Arc::new(AtomicU64::new(0)));
        }
        Ok(build_grid(
            rings,
            producer_wakers,
            consumer_wakers,
            subset_total_published,
            n_consumers,
        ))
    }

    /// Open an existing file-backed grid.
    pub fn open_grid(
        path_prefix: impl AsRef<Path>,
        n_producers: usize,
        n_consumers: usize,
        expected_capacity: usize,
    ) -> Result<(Vec<BlockingMpmcProducer>, Vec<BlockingMpmcConsumer>), BlockingError>
    {
        assert!(n_consumers >= 1, "n_consumers must be >= 1");
        assert!(n_producers >= n_consumers, "n_producers must be >= n_consumers");
        let base = path_prefix.as_ref().to_path_buf();
        let mut rings = Vec::with_capacity(n_producers);
        let mut producer_wakers = Vec::with_capacity(n_producers);
        for i in 0..n_producers {
            rings.push(Arc::new(
                SpscRingCore::open(ring_path(&base, i), expected_capacity)
                    .map_err(BlockingError::from)?,
            ));
            producer_wakers.push(Arc::new(
                CrossProcessWaker::open(pw_path(&base, i), MAX_WAITERS_DEFAULT)
                    .map_err(BlockingError::from)?,
            ));
        }
        let mut consumer_wakers = Vec::with_capacity(n_consumers);
        let mut subset_total_published = Vec::with_capacity(n_consumers);
        for m in 0..n_consumers {
            consumer_wakers.push(Arc::new(
                CrossProcessWaker::open(cw_path(&base, m), MAX_WAITERS_DEFAULT)
                    .map_err(BlockingError::from)?,
            ));
            subset_total_published.push(Arc::new(AtomicU64::new(0)));
        }
        Ok(build_grid(
            rings,
            producer_wakers,
            consumer_wakers,
            subset_total_published,
            n_consumers,
        ))
    }
}

fn ring_path(base: &Path, i: usize) -> PathBuf {
    let mut s = base.as_os_str().to_owned();
    s.push(format!(".ring.{i}.bin"));
    PathBuf::from(s)
}

fn pw_path(base: &Path, i: usize) -> PathBuf {
    let mut s = base.as_os_str().to_owned();
    s.push(format!(".pw.{i}.bin"));
    PathBuf::from(s)
}

fn cw_path(base: &Path, m: usize) -> PathBuf {
    let mut s = base.as_os_str().to_owned();
    s.push(format!(".cw.{m}.bin"));
    PathBuf::from(s)
}

fn build_grid(
    rings: Vec<Arc<SpscRingCore>>,
    producer_wakers: Vec<Arc<CrossProcessWaker>>,
    consumer_wakers: Vec<Arc<CrossProcessWaker>>,
    subset_total_published: Vec<Arc<AtomicU64>>,
    n_consumers: usize,
) -> (Vec<BlockingMpmcProducer>, Vec<BlockingMpmcConsumer>) {
    let producers: Vec<BlockingMpmcProducer> = rings
        .iter()
        .zip(producer_wakers.iter())
        .enumerate()
        .map(|(i, (r, pw))| {
            let subset = i % n_consumers;
            BlockingMpmcProducer {
                ring: Arc::clone(r),
                own_waker: Arc::clone(pw),
                consumer_waker: Arc::clone(&consumer_wakers[subset]),
                subset_total_published: Arc::clone(&subset_total_published[subset]),
                _not_sync: PhantomData,
            }
        })
        .collect();

    let mut consumer_rings: Vec<Vec<Arc<SpscRingCore>>> =
        (0..n_consumers).map(|_| Vec::new()).collect();
    let mut consumer_pwakers: Vec<Vec<Arc<CrossProcessWaker>>> =
        (0..n_consumers).map(|_| Vec::new()).collect();
    for (i, (r, pw)) in rings.iter().zip(producer_wakers.iter()).enumerate() {
        let subset = i % n_consumers;
        consumer_rings[subset].push(Arc::clone(r));
        consumer_pwakers[subset].push(Arc::clone(pw));
    }
    let consumers: Vec<BlockingMpmcConsumer> = consumer_rings
        .into_iter()
        .zip(consumer_pwakers)
        .zip(consumer_wakers)
        .zip(subset_total_published)
        .map(|(((subset_rings, subset_pwakers), own_waker), total)| {
            BlockingMpmcConsumer {
                rings: subset_rings,
                producer_wakers: subset_pwakers,
                own_waker,
                subset_total_published: total,
                next_drain: AtomicUsize::new(0),
                _not_sync: PhantomData,
            }
        })
        .collect();

    (producers, consumers)
}

impl BlockingMpmcProducer {
    /// Non-blocking push. On success, advances subset-shared
    /// counter + fires a wake at the consumer waker.
    #[inline]
    pub fn try_push(&self, payload: &[u8]) -> Result<(), RingError> {
        let r = self.ring.try_push(payload);
        if r.is_ok() {
            let new_seq =
                self.subset_total_published.fetch_add(1, Ordering::Release) + 1;
            self.consumer_waker.wake_up_to(new_seq);
        }
        r
    }

    /// Block until either push succeeds or `timeout` elapses.
    pub fn send_blocking(
        &self,
        payload: &[u8],
        timeout: Option<Duration>,
    ) -> Result<(), BlockingError> {
        let deadline = timeout.map(|d| Instant::now() + d);
        loop {
            match self.try_push(payload) {
                Ok(()) => return Ok(()),
                Err(RingError::Full) => {}
                Err(e) => return Err(BlockingError::Ring(e)),
            }
            for _ in 0..PRE_PARK_SPIN {
                if self.ring.try_push(payload).is_ok() {
                    let new_seq = self
                        .subset_total_published
                        .fetch_add(1, Ordering::Release)
                        + 1;
                    self.consumer_waker.wake_up_to(new_seq);
                    return Ok(());
                }
                std::hint::spin_loop();
            }
            let target = self.ring.tail() + 1;
            let token = self.own_waker.try_park(target)?;
            if self.ring.try_push(payload).is_ok() {
                self.own_waker.release(token);
                let new_seq = self
                    .subset_total_published
                    .fetch_add(1, Ordering::Release)
                    + 1;
                self.consumer_waker.wake_up_to(new_seq);
                return Ok(());
            }
            let remaining = match deadline {
                None => None,
                Some(d) => {
                    let now = Instant::now();
                    if now >= d {
                        self.own_waker.release(token);
                        return Err(BlockingError::Timeout);
                    }
                    Some(d - now)
                }
            };
            match self.own_waker.wait(token, remaining) {
                Ok(()) => continue,
                Err(WakerError::Timeout) => return Err(BlockingError::Timeout),
                Err(e) => return Err(BlockingError::from(e)),
            }
        }
    }

    pub fn capacity(&self) -> usize { self.ring.capacity() }
    pub fn head(&self) -> u64 { self.ring.head() }
}

impl BlockingMpmcConsumer {
    /// Non-blocking pop, round-robin across this consumer's subset.
    pub fn try_pop(&self, out: &mut [u8]) -> Result<usize, RingError> {
        self.try_pop_inner(out)
    }

    /// Block until either a pop succeeds or `timeout` elapses.
    pub fn recv_blocking(
        &self,
        out: &mut [u8],
        timeout: Option<Duration>,
    ) -> Result<usize, BlockingError> {
        let deadline = timeout.map(|d| Instant::now() + d);
        loop {
            match self.try_pop_inner(out) {
                Ok(n) => return Ok(n),
                Err(RingError::Empty) => {}
                Err(e) => return Err(BlockingError::Ring(e)),
            }
            for _ in 0..PRE_PARK_SPIN {
                if let Ok(n) = self.try_pop_inner(out) {
                    return Ok(n);
                }
                std::hint::spin_loop();
            }
            let target = self.subset_total_published.load(Ordering::Acquire) + 1;
            let token = self.own_waker.try_park(target)?;
            if let Ok(n) = self.try_pop_inner(out) {
                self.own_waker.release(token);
                return Ok(n);
            }
            let remaining = match deadline {
                None => None,
                Some(d) => {
                    let now = Instant::now();
                    if now >= d {
                        self.own_waker.release(token);
                        return Err(BlockingError::Timeout);
                    }
                    Some(d - now)
                }
            };
            match self.own_waker.wait(token, remaining) {
                Ok(()) => continue,
                Err(WakerError::Timeout) => return Err(BlockingError::Timeout),
                Err(e) => return Err(BlockingError::from(e)),
            }
        }
    }

    #[inline]
    fn try_pop_inner(&self, out: &mut [u8]) -> Result<usize, RingError> {
        let n = self.rings.len();
        let start = self.next_drain.load(Ordering::Relaxed);
        for i in 0..n {
            let idx = (start + i) % n;
            if let Ok(bytes) = self.rings[idx].try_pop(out) {
                self.next_drain.store((idx + 1) % n, Ordering::Relaxed);
                let tail = self.rings[idx].tail();
                self.producer_wakers[idx].wake_up_to(tail);
                return Ok(bytes);
            }
        }
        Err(RingError::Empty)
    }

    /// Number of rings in this consumer's subset.
    pub fn n_rings(&self) -> usize { self.rings.len() }

    /// Approximate pending items across this consumer's subset.
    pub fn approx_subset_len(&self) -> usize {
        self.rings.iter().map(|r| r.approx_len()).sum()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;

    #[test]
    fn round_trip_4p_2c_anon() {
        let (producers, consumers) =
            BlockingMpmcRing::create_anon_grid(4, 2, 8).expect("create");
        const PER_PROD: u64 = 25;
        let total: u64 = PER_PROD * 4;

        let prod_handles: Vec<_> = producers
            .into_iter()
            .enumerate()
            .map(|(pid, p)| {
                thread::spawn(move || {
                    for i in 0..PER_PROD {
                        let val = (pid as u64) * 1_000_000 + i;
                        let mut payload = [0u8; 56];
                        payload[..8].copy_from_slice(&val.to_le_bytes());
                        p.send_blocking(&payload, Some(Duration::from_secs(5)))
                            .expect("send");
                    }
                })
            })
            .collect();

        let cons_handles: Vec<_> = consumers
            .into_iter()
            .map(|c| {
                let per_consumer = (total / 2) as usize;
                thread::spawn(move || {
                    let mut buf = [0u8; 64];
                    let mut got: Vec<u64> = Vec::with_capacity(per_consumer);
                    for _ in 0..per_consumer {
                        c.recv_blocking(&mut buf, Some(Duration::from_secs(5)))
                            .expect("recv");
                        got.push(u64::from_le_bytes(buf[..8].try_into().unwrap()));
                    }
                    got
                })
            })
            .collect();

        for h in prod_handles {
            h.join().unwrap();
        }
        let mut all: Vec<u64> = cons_handles
            .into_iter()
            .flat_map(|h| h.join().unwrap())
            .collect();
        all.sort_unstable();

        let mut expected: Vec<u64> = Vec::with_capacity(total as usize);
        for pid in 0..4u64 {
            for i in 0..PER_PROD {
                expected.push(pid * 1_000_000 + i);
            }
        }
        expected.sort_unstable();
        assert_eq!(all, expected, "every item delivered exactly once");
    }

    #[test]
    fn recv_blocking_returns_timeout() {
        let (_producers, consumers) =
            BlockingMpmcRing::create_anon_grid(2, 2, 4).expect("create");
        let mut buf = [0u8; 64];
        let t0 = Instant::now();
        let err = consumers[0].recv_blocking(&mut buf, Some(Duration::from_millis(60)));
        assert_eq!(err, Err(BlockingError::Timeout));
        assert!(t0.elapsed() >= Duration::from_millis(50));
    }
}