1use crate::handle::Handle;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[repr(transparent)]
17pub struct TaskHandle(pub Handle);
18
19impl TaskHandle {
20 #[inline]
22 #[must_use]
23 pub const fn new(id: u32, generation: u32) -> Self {
24 Self(Handle::new(id, generation))
25 }
26
27 #[inline]
29 #[must_use]
30 pub const fn null() -> Self {
31 Self(Handle::null())
32 }
33
34 #[inline]
36 #[must_use]
37 pub const fn is_null(&self) -> bool {
38 self.0.is_null()
39 }
40
41 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
60#[repr(u8)]
61pub enum TaskPriority {
62 Idle = 0,
64
65 Normal = 1,
67
68 High = 2,
70
71 RealTime = 3,
73
74 Critical = 4,
76}
77
78impl TaskPriority {
79 #[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 #[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}