Minor thread and scheduler refactoring
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2022-12-31 12:23:27 +01:00
parent 3a8167b6ad
commit af4ab10505
2 changed files with 24 additions and 12 deletions

@ -56,4 +56,12 @@ impl Scheduler {
.push(thread_id) .push(thread_id)
.expect("Thread queue full"); .expect("Thread queue full");
} }
pub fn get_thread(&mut self, id: ThreadId) -> Option<Threadt> {
if let Some(thread) = self.threads.get_mut(&id) {
Some(thread.clone())
} else {
None
}
}
} }

@ -27,14 +27,18 @@ impl ThreadId {
pub fn exit() { pub fn exit() {
println!("Exiting thread"); println!("Exiting thread");
let mut scheduler = SCHEDULER.try_lock().unwrap(); let thread: *mut Thread;
let mut thread = scheduler {
.threads let mut scheduler = SCHEDULER.try_lock().unwrap();
.get_mut(&ThreadId(0)) thread = scheduler
.unwrap() .get_thread(ThreadId(0))
.borrow_mut(); .unwrap()
SCHEDULER.force_unlock(); .as_ptr();
thread.run(); } // Drop scheduler mutex guard
unsafe {
(&mut* thread).run();
}
} }
pub struct Thread { pub struct Thread {
@ -51,7 +55,7 @@ impl Thread {
id: ThreadId::new(), id: ThreadId::new(),
entry_point: entry_point, entry_point: entry_point,
started: false, started: false,
rsp: alloc(Layout::new::<[u8; STACK_SIZE]>()) as u64 + STACK_SIZE as u64 - 0x80, rsp: alloc(Layout::new::<[u8; STACK_SIZE]>()) as u64 + STACK_SIZE as u64,
} }
} }
} }
@ -69,7 +73,7 @@ impl Thread {
); );
let mut scheduler = SCHEDULER.try_lock().unwrap(); let mut scheduler = SCHEDULER.try_lock().unwrap();
let current_thread = scheduler.threads.get_mut(&*current_thread_guard).unwrap(); let current_thread = scheduler.get_thread(*current_thread_guard).unwrap();
current_thread.borrow_mut().rsp = current_rsp; current_thread.borrow_mut().rsp = current_rsp;
*current_thread_guard = self.id; // change running thread *current_thread_guard = self.id; // change running thread
@ -109,9 +113,9 @@ impl Thread {
"push rsi", "push rsi",
"push rdi", "push rdi",
"push {rsp}", "push {rsp}", // Set stack pointer to the new thread
"pop rsp", "pop rsp",
"jmp {rip}", "jmp {rip}", // Jump to thread routine
rsp = in(reg) self.rsp, rsp = in(reg) self.rsp,
rip = in(reg) self.entry_point, rip = in(reg) self.entry_point,
); );