Fix thread

This commit is contained in:
Quentin Legot 2023-03-08 15:54:10 +01:00
parent f4edac230e
commit f15d782916
2 changed files with 10 additions and 33 deletions

View File

@ -1,13 +1,11 @@
use std::sync::Arc; use std::{sync::Arc, rc::Rc};
use crate::utility::list::List; use crate::utility::list::List;
use crate::kernel::thread::Thread; use crate::kernel::thread::Thread;
use super::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED};
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct Scheduler { pub struct Scheduler {
ready_list: List<Arc<Thread>> ready_list: List<Rc<Thread>>
} }
impl Scheduler { impl Scheduler {
@ -28,7 +26,7 @@ impl Scheduler {
/// ## Pamameter /// ## Pamameter
/// ///
/// **thread** is the thread to be put on the read list /// **thread** is the thread to be put on the read list
pub fn ready_to_run(&mut self, thread: Arc<Thread>) { pub fn ready_to_run(&mut self, thread: Rc<Thread>) {
self.ready_list.push(thread); self.ready_list.push(thread);
} }
@ -38,7 +36,7 @@ impl Scheduler {
/// Thread is removed from the ready list. /// Thread is removed from the ready list.
/// ///
/// **return** Thread thread to be scheduled /// **return** Thread thread to be scheduled
pub fn find_next_to_run(&mut self) -> Option<Arc<Thread>> { pub fn find_next_to_run(&mut self) -> Option<Rc<Thread>> {
self.ready_list.pop() self.ready_list.pop()
} }

View File

@ -51,23 +51,9 @@ impl Thread {
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
self.init_simulator_context(base_stack_addr); self.init_simulator_context(base_stack_addr);
self.process.as_mut().unwrap().num_thread += 1; self.process.as_mut().unwrap().num_thread += 1;
match G_ALIVE.write() { let this = Rc::new(self);
Ok(mut alive) => { self.system.get_g_alive().push(Rc::clone(&this));
let this = Arc::new(self); self.system.g_scheduler().ready_to_run(Rc::clone(&this));
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);
}
}
Result::Ok(()) Result::Ok(())
} }
@ -93,17 +79,10 @@ impl Thread {
} }
/// Wait for another thread to finish its execution /// Wait for another thread to finish its execution
pub fn join(&self, id_thread: Arc<Thread>) { pub fn join(&self, id_thread: Rc<Thread>) {
match G_ALIVE.write() { while self.system.get_g_alive().contains(&Rc::clone(&id_thread)) {
Ok(alive) => {
while alive.contains(&Arc::clone(&id_thread)) {
self.t_yield(); self.t_yield();
} }
},
Err(err) => {
panic!("RwLock poisonned, {}", err)
}
}
} }
/// Relinquish the CPU if any other thread is runnable. /// Relinquish the CPU if any other thread is runnable.