@@ -15,22 +15,25 @@ pub struct Tasking{
15
15
current_task_id : u8 ,
16
16
eflags_register : u32 ,
17
17
cr3_register : u32 ,
18
+ initialized : bool ,
18
19
}
19
20
20
21
impl Tasking {
21
22
const MAX_TASKS : usize = 10 ;
22
23
23
- pub fn init ( ) -> Self {
24
+ pub fn init ( & mut self ) {
25
+ if self . task_list . is_some ( ) {
26
+ self . task_list = None ;
27
+ }
24
28
let ( eflags, cr3) = Self :: get_eflags_and_cr3_registers ( ) ;
25
29
26
30
// Create main task
27
- let mut tasking: Self = Tasking {
28
- task_list : Some ( VecDeque :: new ( ) ) ,
29
- current_task_id : 0 ,
30
- eflags_register : eflags,
31
- cr3_register : cr3,
32
- } ;
33
- tasking. task_list . as_mut ( ) . unwrap ( ) . push_back ( Task {
31
+ self . task_list = Some ( VecDeque :: with_capacity ( Self :: MAX_TASKS ) ) ;
32
+ self . current_task_id = 0 ;
33
+ self . eflags_register = eflags;
34
+ self . cr3_register = cr3;
35
+ self . initialized = true ;
36
+ self . task_list . as_mut ( ) . unwrap ( ) . push_back ( Task {
34
37
registers : Registers {
35
38
eax : 0 ,
36
39
ebx : 0 ,
@@ -45,10 +48,12 @@ impl Tasking {
45
48
cr3,
46
49
} ,
47
50
} ) ;
48
- tasking
49
51
}
50
52
51
53
pub fn add_task ( & mut self , main_function : * mut fn ( ) ) -> Result < ( ) , & ' static str > {
54
+ if !self . initialized {
55
+ return Err ( "Cooperative tasking manager is not initialized" ) ;
56
+ }
52
57
if self . task_list . as_ref ( ) . unwrap ( ) . len ( ) >= Self :: MAX_TASKS {
53
58
return Err ( "Maximum number of tasks reached" ) ;
54
59
}
@@ -58,6 +63,10 @@ impl Tasking {
58
63
}
59
64
60
65
pub fn yield_task ( & mut self ) {
66
+ if !self . initialized {
67
+ panic ! ( "Cooperative tasking manager is not initialized" ) ;
68
+ }
69
+
61
70
let task_list = self . task_list . as_mut ( ) . unwrap ( ) ;
62
71
63
72
let current_task_registers_ptr = & mut task_list[ self . current_task_id as usize ] . registers as * mut Registers ;
@@ -92,6 +101,7 @@ pub static mut TASKING: Tasking = Tasking {
92
101
current_task_id : 0 ,
93
102
eflags_register : 0 ,
94
103
cr3_register : 0 ,
104
+ initialized : false ,
95
105
} ;
96
106
97
107
#[ macro_export]
0 commit comments