reovim_kernel/sched/state.rs
1//! Runtime lifecycle state machine.
2//!
3//! Linux equivalent: Task states in `kernel/sched/sched.h`
4//!
5//! The runtime transitions through these states during its lifecycle:
6//! - `Booting` -> `Running` -> `Stopping`
7//! - Any state can transition to `Emergency` on panic
8
9/// Runtime lifecycle state.
10///
11/// Represents the current operating mode of the scheduler runtime.
12///
13/// # State Transitions
14///
15/// ```text
16/// ┌─────────┐ ┌─────────┐ ┌──────────┐
17/// │ Booting │────>│ Running │────>│ Stopping │
18/// └─────────┘ └─────────┘ └──────────┘
19/// │ │ │
20/// └───────────────┴───────────────┘
21/// │
22/// v
23/// ┌───────────┐
24/// │ Emergency │
25/// └───────────┘
26/// ```
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
28pub enum RuntimeState {
29 /// Initializing subsystems, loading modules.
30 ///
31 /// Linux equivalent: `TASK_NEW`
32 #[default]
33 Booting,
34
35 /// Normal operation, processing events.
36 ///
37 /// Linux equivalent: `TASK_RUNNING`
38 Running,
39
40 /// Graceful shutdown in progress.
41 ///
42 /// Remaining work is drained before full stop.
43 ///
44 /// Linux equivalent: `TASK_STOPPED`
45 Stopping,
46
47 /// Emergency shutdown (panic recovery mode).
48 ///
49 /// No new work accepted, cleanup only.
50 ///
51 /// Linux equivalent: Kernel oops handling
52 Emergency,
53}
54
55impl RuntimeState {
56 /// Check if runtime is in the running state.
57 ///
58 /// Only in `Running` state can new work be accepted.
59 #[inline]
60 #[must_use]
61 pub const fn is_running(&self) -> bool {
62 matches!(self, Self::Running)
63 }
64
65 /// Check if shutdown is in progress.
66 ///
67 /// Returns `true` for both `Stopping` and `Emergency` states.
68 #[inline]
69 #[must_use]
70 pub const fn is_shutting_down(&self) -> bool {
71 matches!(self, Self::Stopping | Self::Emergency)
72 }
73
74 /// Check if runtime can accept new work.
75 ///
76 /// Equivalent to `is_running()` - only running runtime accepts work.
77 #[inline]
78 #[must_use]
79 pub const fn can_accept_work(&self) -> bool {
80 self.is_running()
81 }
82
83 /// Check if this is a terminal state.
84 ///
85 /// Terminal states indicate the runtime should stop its main loop.
86 #[inline]
87 #[must_use]
88 pub const fn is_terminal(&self) -> bool {
89 matches!(self, Self::Stopping | Self::Emergency)
90 }
91}
92
93impl std::fmt::Display for RuntimeState {
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 match self {
96 Self::Booting => write!(f, "Booting"),
97 Self::Running => write!(f, "Running"),
98 Self::Stopping => write!(f, "Stopping"),
99 Self::Emergency => write!(f, "Emergency"),
100 }
101 }
102}