Skip to main content

sectorsync_core/
barrier.rs

1//! Full runtime barrier state for pause, snapshot, upgrade, and resume.
2
3use crate::ids::{BarrierId, InstanceId, StationId, Tick};
4
5/// Runtime barrier scope.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum BarrierScope {
8    /// Barrier applies to a full world instance.
9    Instance(InstanceId),
10    /// Barrier applies to a single station.
11    Station(StationId),
12}
13
14/// Strategy for commands received while a barrier is active.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum CommandQueueMode {
17    /// Buffer commands until resume.
18    Buffer,
19    /// Reject commands until resume.
20    Reject,
21    /// Drain already queued commands and reject new commands.
22    Drain,
23}
24
25/// Barrier lifecycle state.
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum BarrierState {
28    /// Normal running state.
29    Running,
30    /// Barrier was requested but not aligned.
31    Requested,
32    /// Waiting for a tick boundary.
33    WaitingTickBoundary,
34    /// Runtime state is frozen.
35    Frozen,
36    /// Runtime is resuming.
37    Resuming,
38}
39
40/// Full runtime barrier descriptor.
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub struct RuntimeBarrier {
43    /// Barrier id.
44    pub id: BarrierId,
45    /// Barrier scope.
46    pub scope: BarrierScope,
47    /// Tick observed when requested.
48    pub requested_at: Tick,
49    /// Tick boundary selected for freezing.
50    pub target_tick: Tick,
51    /// Command behavior during the barrier.
52    pub command_mode: CommandQueueMode,
53    /// Current barrier state.
54    pub state: BarrierState,
55}
56
57impl RuntimeBarrier {
58    /// Creates a requested barrier.
59    pub const fn requested(
60        id: BarrierId,
61        scope: BarrierScope,
62        requested_at: Tick,
63        target_tick: Tick,
64        command_mode: CommandQueueMode,
65    ) -> Self {
66        Self {
67            id,
68            scope,
69            requested_at,
70            target_tick,
71            command_mode,
72            state: BarrierState::Requested,
73        }
74    }
75
76    /// Marks this barrier as waiting for the target tick boundary.
77    pub fn wait_for_tick_boundary(&mut self) {
78        self.state = BarrierState::WaitingTickBoundary;
79    }
80
81    /// Marks this barrier as frozen.
82    pub fn freeze(&mut self) {
83        self.state = BarrierState::Frozen;
84    }
85
86    /// Marks this barrier as resuming.
87    pub fn resume(&mut self) {
88        self.state = BarrierState::Resuming;
89    }
90
91    /// Clears this barrier back to running state.
92    pub fn finish(&mut self) {
93        self.state = BarrierState::Running;
94    }
95}