indexbus_kit/lanes/sequencer/
wait.rs

1use std::time::Duration;
2
3use crate::errors::{Error, Result};
4
5use indexbus_core::{SpinWait, StdBackoff};
6use indexbus_seq::SequencerBarrier;
7
8#[derive(Copy, Clone, Debug)]
9/// Wait strategy used by `wait_for` when observing sequencer progress.
10pub enum WaitMode {
11    /// Busy-wait with a spin loop.
12    Spin,
13    /// Poll with a standard backoff.
14    Backoff,
15    /// Use the OS wake-based blocking path (requires wake sections).
16    Blocking,
17}
18
19/// Wait until the sequencer barrier observes `seq`.
20///
21/// This helper is intended for application-side orchestration; it does not assert fairness.
22pub fn wait_for<const N: usize>(
23    barrier: &SequencerBarrier<'_, N>,
24    seq: u64,
25    mode: WaitMode,
26    timeout: Option<Duration>,
27) -> Result<()> {
28    match mode {
29        WaitMode::Spin => {
30            let mut w = SpinWait::default();
31            barrier.wait_for(seq, &mut w);
32            Ok(())
33        }
34        WaitMode::Backoff => {
35            let mut w = StdBackoff::default();
36            barrier.wait_for(seq, &mut w);
37            Ok(())
38        }
39        WaitMode::Blocking => {
40            let ok = barrier
41                .wait_for_blocking(seq, timeout)
42                .map_err(Error::from)?;
43            if ok {
44                Ok(())
45            } else {
46                Err(Error::msg("timed out waiting for seq"))
47            }
48        }
49    }
50}