Skip to main content

ruvix_types/
task.rs

1//! Task management types.
2//!
3//! A Task is the unit of concurrent execution in RuVix. Each task has an
4//! explicit capability set and runs with a scheduling priority.
5
6use crate::handle::Handle;
7
8/// Handle to a task (unit of concurrent execution).
9///
10/// Tasks are analogous to seL4 TCBs (Thread Control Blocks). Each task has:
11/// - An entry point (RVF component)
12/// - A capability set (what resources it can access)
13/// - A scheduling priority
14/// - An optional hard deadline
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[repr(transparent)]
17pub struct TaskHandle(pub Handle);
18
19impl TaskHandle {
20    /// Creates a new task handle.
21    #[inline]
22    #[must_use]
23    pub const fn new(id: u32, generation: u32) -> Self {
24        Self(Handle::new(id, generation))
25    }
26
27    /// Creates a null (invalid) task handle.
28    #[inline]
29    #[must_use]
30    pub const fn null() -> Self {
31        Self(Handle::null())
32    }
33
34    /// Checks if this handle is null.
35    #[inline]
36    #[must_use]
37    pub const fn is_null(&self) -> bool {
38        self.0.is_null()
39    }
40
41    /// Returns the raw handle.
42    #[inline]
43    #[must_use]
44    pub const fn raw(&self) -> Handle {
45        self.0
46    }
47}
48
49impl Default for TaskHandle {
50    fn default() -> Self {
51        Self::null()
52    }
53}
54
55/// Task scheduling priority.
56///
57/// Priority is used by the coherence-aware scheduler in combination with
58/// deadline pressure, novelty signal, and structural risk.
59#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
60#[repr(u8)]
61pub enum TaskPriority {
62    /// Lowest priority. Background tasks, housekeeping.
63    Idle = 0,
64
65    /// Normal priority. Default for most tasks.
66    Normal = 1,
67
68    /// High priority. Time-sensitive operations.
69    High = 2,
70
71    /// Real-time priority. Hard deadline tasks.
72    RealTime = 3,
73
74    /// Critical priority. Kernel-level urgency.
75    Critical = 4,
76}
77
78impl TaskPriority {
79    /// Returns the priority as a numeric weight (0-100).
80    #[inline]
81    #[must_use]
82    pub const fn weight(&self) -> u8 {
83        match self {
84            Self::Idle => 0,
85            Self::Normal => 25,
86            Self::High => 50,
87            Self::RealTime => 75,
88            Self::Critical => 100,
89        }
90    }
91
92    /// Converts from a raw u8 value.
93    #[inline]
94    #[must_use]
95    pub const fn from_u8(value: u8) -> Option<Self> {
96        match value {
97            0 => Some(Self::Idle),
98            1 => Some(Self::Normal),
99            2 => Some(Self::High),
100            3 => Some(Self::RealTime),
101            4 => Some(Self::Critical),
102            _ => None,
103        }
104    }
105}
106
107impl Default for TaskPriority {
108    fn default() -> Self {
109        Self::Normal
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_task_handle() {
119        let h = TaskHandle::new(1, 2);
120        assert!(!h.is_null());
121        assert_eq!(h.raw().id, 1);
122        assert_eq!(h.raw().generation, 2);
123    }
124
125    #[test]
126    fn test_task_priority_ordering() {
127        assert!(TaskPriority::Idle < TaskPriority::Normal);
128        assert!(TaskPriority::Normal < TaskPriority::High);
129        assert!(TaskPriority::High < TaskPriority::RealTime);
130        assert!(TaskPriority::RealTime < TaskPriority::Critical);
131    }
132
133    #[test]
134    fn test_task_priority_weight() {
135        assert_eq!(TaskPriority::Critical.weight(), 100);
136        assert_eq!(TaskPriority::Idle.weight(), 0);
137    }
138}