Skip to content

Commit c35e0c4

Browse files
committed
Add cooperative multitasking support
1 parent 6d5ce22 commit c35e0c4

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/dos/cooperative_multitasking/mod.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ pub struct Tasking{
1515
current_task_id: u8,
1616
eflags_register: u32,
1717
cr3_register: u32,
18+
initialized: bool,
1819
}
1920

2021
impl Tasking {
2122
const MAX_TASKS: usize = 10;
2223

23-
pub fn init() -> Self {
24+
pub fn init(&mut self) {
25+
if self.task_list.is_some() {
26+
self.task_list = None;
27+
}
2428
let (eflags, cr3) = Self::get_eflags_and_cr3_registers();
2529

2630
// 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 {
3437
registers: Registers {
3538
eax: 0,
3639
ebx: 0,
@@ -45,10 +48,12 @@ impl Tasking {
4548
cr3,
4649
},
4750
});
48-
tasking
4951
}
5052

5153
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+
}
5257
if self.task_list.as_ref().unwrap().len() >= Self::MAX_TASKS {
5358
return Err("Maximum number of tasks reached");
5459
}
@@ -58,6 +63,10 @@ impl Tasking {
5863
}
5964

6065
pub fn yield_task(&mut self) {
66+
if !self.initialized {
67+
panic!("Cooperative tasking manager is not initialized");
68+
}
69+
6170
let task_list = self.task_list.as_mut().unwrap();
6271

6372
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 {
92101
current_task_id: 0,
93102
eflags_register: 0,
94103
cr3_register: 0,
104+
initialized: false,
95105
};
96106

97107
#[macro_export]

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ extern crate rlibc;
88
extern crate alloc;
99

1010
use crate::dos::allocator::GLOBAL_ALLOCATOR;
11-
use crate::dos::cooperative_multitasking::{Tasking, TASKING};
11+
use crate::dos::cooperative_multitasking::TASKING;
1212

1313
#[link_section = ".startup"]
1414
#[no_mangle]
1515
fn _start() -> ! {
1616
unsafe {
1717
GLOBAL_ALLOCATOR.init();
18-
TASKING = Tasking::init();
18+
TASKING.init(); // Relies on the allocator
1919
}
2020
extern "Rust" {
2121
fn main() -> ();

0 commit comments

Comments
 (0)