proka_kernel/process/
task.rs1extern crate alloc;
2use alloc::vec::Vec;
3use lazy_static::lazy_static;
4use spin::Mutex;
5
6lazy_static! {
7 pub static ref TASK_MANAGER: Mutex<TaskManager> = Mutex::new(TaskManager::new());
8}
9
10pub enum TaskState {
12 Ready,
15
16 Running,
18
19 Terminated,
22}
23
24#[allow(unused)]
26pub struct Task {
27 id: u16,
32
33 state: TaskState,
35
36 priority: u8,
38}
39
40impl Task {
41 pub fn new(id: u16, priority: u8) -> Self {
43 Self {
44 id,
45 state: TaskState::Ready,
46 priority,
47 }
48 }
49
50 pub fn update_stat(&mut self, new_state: TaskState) {
52 self.state = new_state
53 }
54}
55
56pub struct TaskManager {
58 tasks: Vec<Task>,
60
61 allocated_tid: Vec<u16>,
63
64 next_tid: u16,
66}
67
68impl TaskManager {
69 pub const fn new() -> Self {
70 Self {
71 tasks: Vec::new(),
72 allocated_tid: Vec::new(),
73 next_tid: 0,
74 }
75 }
76
77 pub fn create_task(&mut self, priority: u8) {
78 let mut task_id = self.next_tid;
80
81 if self.allocated_tid.contains(&task_id) {
83 task_id += 1;
84 }
85
86 self.tasks.push(Task::new(task_id, priority));
88
89 self.allocated_tid.push(task_id);
91
92 self.next_tid = self.next_tid.wrapping_add(1);
94 }
95
96 pub fn delete_task(&mut self, task_id: u16) -> Result<(), &'static str> {
97 if !self.allocated_tid.contains(&task_id) {
99 return Err("The task ID is unable to discovor.");
100 }
101
102 Ok(())
104 }
105}
106
107impl Default for TaskManager {
108 fn default() -> Self {
109 Self::new()
110 }
111}