From f15d782916931d99e25d915a02ee0c1093a52cf1 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 8 Mar 2023 15:54:10 +0100 Subject: [PATCH] Fix thread --- src/kernel/scheduler.rs | 10 ++++------ src/kernel/thread.rs | 33 ++++++--------------------------- 2 files changed, 10 insertions(+), 33 deletions(-) diff --git a/src/kernel/scheduler.rs b/src/kernel/scheduler.rs index 9753cdf..670f0fa 100644 --- a/src/kernel/scheduler.rs +++ b/src/kernel/scheduler.rs @@ -1,13 +1,11 @@ -use std::sync::Arc; +use std::{sync::Arc, rc::Rc}; use crate::utility::list::List; use crate::kernel::thread::Thread; -use super::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED}; - #[derive(PartialEq)] pub struct Scheduler { - ready_list: List> + ready_list: List> } impl Scheduler { @@ -28,7 +26,7 @@ impl Scheduler { /// ## Pamameter /// /// **thread** is the thread to be put on the read list - pub fn ready_to_run(&mut self, thread: Arc) { + pub fn ready_to_run(&mut self, thread: Rc) { self.ready_list.push(thread); } @@ -38,7 +36,7 @@ impl Scheduler { /// Thread is removed from the ready list. /// /// **return** Thread thread to be scheduled - pub fn find_next_to_run(&mut self) -> Option> { + pub fn find_next_to_run(&mut self) -> Option> { self.ready_list.pop() } diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index ce4aec8..3ffbba6 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -51,23 +51,9 @@ impl Thread { let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray self.init_simulator_context(base_stack_addr); self.process.as_mut().unwrap().num_thread += 1; - match G_ALIVE.write() { - Ok(mut alive) => { - let this = Arc::new(self); - alive.push(Arc::clone(&this)); - match G_SCHEDULER.write() { - Ok(mut scheduler) => { - scheduler.ready_to_run(Arc::clone(&this)); - }, - Err(err) => { - panic!("RwLock poisonned, {}", err); - } - } - }, - Err(err) => { - panic!("RwLock poisonned, {}", err); - } - } + let this = Rc::new(self); + self.system.get_g_alive().push(Rc::clone(&this)); + self.system.g_scheduler().ready_to_run(Rc::clone(&this)); Result::Ok(()) } @@ -93,16 +79,9 @@ impl Thread { } /// Wait for another thread to finish its execution - pub fn join(&self, id_thread: Arc) { - match G_ALIVE.write() { - Ok(alive) => { - while alive.contains(&Arc::clone(&id_thread)) { - self.t_yield(); - } - }, - Err(err) => { - panic!("RwLock poisonned, {}", err) - } + pub fn join(&self, id_thread: Rc) { + while self.system.get_g_alive().contains(&Rc::clone(&id_thread)) { + self.t_yield(); } }