kernel now build

I commented out semaphore code too cause it need to be updated
and having some error cause the compiler to not check for borrow errors
This commit is contained in:
Quentin Legot 2023-03-16 14:52:53 +01:00
parent fa64d4314d
commit 977cb2bf96
5 changed files with 206 additions and 241 deletions

View File

@ -1,6 +1,5 @@
mod process; mod process;
pub mod thread; pub mod thread;
pub mod scheduler;
pub mod mgerror; pub mod mgerror;
pub mod system; pub mod system;
mod ucontext; mod ucontext;

View File

@ -1,69 +0,0 @@
use std::cell::RefCell;
use std::rc::Rc;
use crate::utility::list::List;
use crate::kernel::thread::Thread;
use crate::simulator::machine::Machine;
use super::system::System;
use super::thread_manager::ThreadManager;
#[derive(PartialEq)]
pub struct Scheduler {
ready_list: List<Rc<RefCell<Thread>>>,
}
impl Scheduler {
/// Constructor
///
/// Initilize the list of ready thread
pub fn new() -> Self {
Self {
ready_list: List::new(),
}
}
/// Mark a thread as aready, but not necessarily running yet.
///
/// Put it in the ready list, for later scheduling onto the CPU.
///
/// ## Pamameter
///
/// **thread** is the thread to be put on the read list
pub fn ready_to_run(&mut self, thread: Rc<RefCell<Thread>>) {
self.ready_list.push(thread);
}
/// Return the next thread to be scheduled onto the CPU.
/// If there are no ready threads, return Option::None
///
/// Thread is removed from the ready list.
///
/// **return** Thread thread to be scheduled
pub fn find_next_to_run(&mut self) -> Option<Rc<RefCell<Thread>>> {
self.ready_list.pop()
}
/// Dispatch the CPU to next_thread. Save the state of the old thread
/// and load the state of the new thread.
///
/// We assume the state of the previously running thread has already been changed from running to blocked or ready.
///
/// Global variable g_current_thread become next_thread
///
/// ## Parameter
///
/// **next_thread** thread to dispatch to the CPU
pub fn switch_to(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, next_thread: Rc<RefCell<Thread>>) {
let old_thread = thread_manager.get_g_current_thread().as_ref().unwrap();
thread_manager.thread_save_processor_state(machine, Rc::clone(&old_thread));
// old_thread.save_simulator_state();
if old_thread != &next_thread {
thread_manager.thread_restore_processor_state(machine, Rc::clone(&next_thread));
// next_thread.restore_simulator_state();
thread_manager.set_g_current_thread(Some(next_thread));
}
}
}

View File

@ -5,8 +5,6 @@ use crate::simulator::machine::Machine;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use super::scheduler::Scheduler;
use super::system::System; use super::system::System;
use super::thread_manager::ThreadManager; use super::thread_manager::ThreadManager;
@ -47,8 +45,10 @@ impl Semaphore {
if self.counter < 0 { if self.counter < 0 {
match thread_manager.get_g_current_thread() { match thread_manager.get_g_current_thread() {
Some(thread) => { Some(thread) => {
self.waiting_queue.push(Rc::clone(thread)); let rc1_thread = Rc::clone(thread);
thread_manager.thread_sleep(machine, Rc::clone(thread)); let rc2_thread = Rc::clone(thread);
self.waiting_queue.push(rc1_thread);
thread_manager.thread_sleep(machine, rc2_thread);
}, },
None => unreachable!("Current thread should not be None") None => unreachable!("Current thread should not be None")
} }
@ -70,7 +70,7 @@ impl Semaphore {
let old_status = system.get_machine().interrupt.set_status(InterruptOff); let old_status = system.get_machine().interrupt.set_status(InterruptOff);
self.counter += 1; self.counter += 1;
if self.waiting_queue.peek() != None { if self.waiting_queue.peek() != None {
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); system.get_thread_manager().ready_to_run(self.waiting_queue.pop().unwrap());
} }
system.get_machine().interrupt.set_status(old_status); system.get_machine().interrupt.set_status(old_status);
} }
@ -112,24 +112,21 @@ impl Lock {
/// ### Parameters /// ### Parameters
/// - **current_thread** the current thread /// - **current_thread** the current thread
/// - **machine** the machine where the threads are executed /// - **machine** the machine where the threads are executed
pub fn acquire(&mut self, thread_manager: &mut ThreadManager, machine: &mut Machine) { pub fn acquire(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
if self.free { if self.free {
self.free = false; self.free = false;
self.owner = Option::Some(match thread_manager.get_g_current_thread() { self.owner = Option::Some(match thread_manager.get_g_current_thread() {
Some(th) => { Some(th) => {
Rc::clone(th) Rc::clone(&th)
}, },
None => unreachable!() None => unreachable!()
}); });
} else { } else {
let t = thread_manager.get_g_current_thread(); match thread_manager.get_g_current_thread() {
match t {
Some(x) => { Some(x) => {
let x = Rc::clone(x); let x = Rc::clone(&x);
self.waiting_queue.push(Rc::clone(&x)); self.waiting_queue.push(Rc::clone(&x));
thread_manager.thread_sleep(machine, Rc::clone(&x)); thread_manager.thread_sleep(machine, Rc::clone(&x));
}, },
None => unreachable!("Current thread should not be None") None => unreachable!("Current thread should not be None")
@ -157,7 +154,7 @@ impl Lock {
if self.waiting_queue.peek() != None { if self.waiting_queue.peek() != None {
self.owner = Some(self.waiting_queue.pop().unwrap()); self.owner = Some(self.waiting_queue.pop().unwrap());
match &self.owner { match &self.owner {
Some(x) => system.get_thread_manager().g_scheduler.ready_to_run(Rc::clone(&x)), Some(x) => system.get_thread_manager().ready_to_run(Rc::clone(&x)),
None => () None => ()
} }
} else { } else {
@ -176,7 +173,7 @@ impl Lock {
match &self.owner { match &self.owner {
Some(x) => Some(x) =>
match system.get_thread_manager().get_g_current_thread() { match system.get_thread_manager().get_g_current_thread() {
Some(thread) => Rc::ptr_eq(&x, thread), Some(thread) => Rc::ptr_eq(&x, &thread),
None => false None => false
} }
None => false None => false
@ -208,18 +205,19 @@ impl Condition {
/// ### Parameters /// ### Parameters
/// - **current_thread** the current thread /// - **current_thread** the current thread
/// - **machine** the machine where threads are executed /// - **machine** the machine where threads are executed
pub fn wait(&mut self, system: &mut System) { pub fn wait(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
match thread_manager.get_g_current_thread() {
match system.get_thread_manager().get_g_current_thread() {
Some(thread) => { Some(thread) => {
self.waiting_queue.push(Rc::clone(thread)); let rc1 = Rc::clone(thread);
system.thread_sleep(Rc::clone(thread)); let rc2 = Rc::clone(thread);
self.waiting_queue.push(rc1);
thread_manager.thread_sleep(machine, rc2);
}, },
None => unreachable!() None => unreachable!()
} }
system.get_machine().interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
/// Wake up the first thread of the wait queue (if any). /// Wake up the first thread of the wait queue (if any).
@ -232,7 +230,7 @@ impl Condition {
let old_status = system.get_machine().interrupt.set_status(InterruptOff); let old_status = system.get_machine().interrupt.set_status(InterruptOff);
if self.waiting_queue.peek() != None { if self.waiting_queue.peek() != None {
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); system.get_thread_manager().ready_to_run(self.waiting_queue.pop().unwrap());
} }
system.get_machine().interrupt.set_status(old_status); system.get_machine().interrupt.set_status(old_status);
@ -249,7 +247,7 @@ impl Condition {
let old_status = system.get_machine().interrupt.set_status(InterruptOff); let old_status = system.get_machine().interrupt.set_status(InterruptOff);
while self.waiting_queue.peek() != None { while self.waiting_queue.peek() != None {
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); system.get_thread_manager().ready_to_run(self.waiting_queue.pop().unwrap());
} }
system.get_machine().interrupt.set_status(old_status); system.get_machine().interrupt.set_status(old_status);
@ -257,132 +255,132 @@ impl Condition {
} }
#[cfg(test)] // #[cfg(test)]
mod test { // mod test {
use std::{rc::Rc, cell::RefCell}; // use std::{rc::Rc, cell::RefCell};
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine}; // use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine};
#[test] // #[test]
fn test_semaphore_single() { // fn test_semaphore_single() {
// Init // // Init
let system = init_system!(); // let system = init_system!();
let mut semaphore = Semaphore::new(1); // let mut semaphore = Semaphore::new(1);
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore"))); // let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
// P // // P
semaphore.p(system, thread); // semaphore.p(system, thread);
assert_eq!(semaphore.counter, 0); // assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
// V // // V
semaphore.v(Rc::clone(&system)); // semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 1); // assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
} // }
#[test] // #[test]
#[ignore] // #[ignore]
fn test_semaphore_multiple() { // fn test_semaphore_multiple() {
// Init // // Init
let system = init_system!(); // let system = init_system!();
let tm = system.borrow_mut().get_thread_manager(); // let tm = system.borrow_mut().get_thread_manager();
let mut semaphore = Semaphore::new(2, Rc::clone(&tm)); // let mut semaphore = Semaphore::new(2, Rc::clone(&tm));
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1"))); // let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2"))); // let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3"))); // let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3")));
let mut borrow_tm = tm.borrow_mut(); // let mut borrow_tm = tm.borrow_mut();
let scheduler = &mut borrow_tm.g_scheduler; // let scheduler = &mut borrow_tm.g_scheduler;
scheduler.ready_to_run(Rc::clone(&thread1)); // scheduler.ready_to_run(Rc::clone(&thread1));
scheduler.ready_to_run(Rc::clone(&thread2)); // scheduler.ready_to_run(Rc::clone(&thread2));
scheduler.ready_to_run(Rc::clone(&thread3)); // scheduler.ready_to_run(Rc::clone(&thread3));
// P // // P
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread1))); // borrow_tm.set_g_current_thread(Some(Rc::clone(&thread1)));
semaphore.p(thread1, Rc::clone(&system)); // semaphore.p(thread1, Rc::clone(&system));
assert_eq!(semaphore.counter, 1); // assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread2))); // borrow_tm.set_g_current_thread(Some(Rc::clone(&thread2)));
semaphore.p(thread2, Rc::clone(&system)); // semaphore.p(thread2, Rc::clone(&system));
assert_eq!(semaphore.counter, 0); // assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread3))); // borrow_tm.set_g_current_thread(Some(Rc::clone(&thread3)));
semaphore.p(thread3, Rc::clone(&system)); // semaphore.p(thread3, Rc::clone(&system));
assert_eq!(semaphore.counter, -1); // assert_eq!(semaphore.counter, -1);
assert!(semaphore.waiting_queue.iter().count() == 1); // assert!(semaphore.waiting_queue.iter().count() == 1);
// V // // V
semaphore.v(Rc::clone(&system)); // semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 0); // assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
semaphore.v(Rc::clone(&system)); // semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 1); // assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
semaphore.v(Rc::clone(&system)); // semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 2); // assert_eq!(semaphore.counter, 2);
assert!(semaphore.waiting_queue.is_empty()); // assert!(semaphore.waiting_queue.is_empty());
} // }
#[test] // #[test]
#[ignore] // #[ignore]
fn test_lock_simple() { // fn test_lock_simple() {
let system = init_system!(); // let system = init_system!();
let sys = system.borrow_mut(); // let sys = system.borrow_mut();
let tm = sys.get_thread_manager(); // let tm = sys.get_thread_manager();
let thread = Rc::new(RefCell::new(Thread::new("test_lock"))); // let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread))); // tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread)));
let mut lock = Lock::new(Rc::clone(&tm)); // let mut lock = Lock::new(Rc::clone(&tm));
assert!(lock.free); // assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system)); // lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread))); // assert!(lock.held_by_current_thread(Rc::clone(&thread)));
assert!(!lock.free); // assert!(!lock.free);
lock.release(Rc::clone(&system), Rc::clone(&thread)); // lock.release(Rc::clone(&system), Rc::clone(&thread));
assert!(!lock.held_by_current_thread(thread)); // assert!(!lock.held_by_current_thread(thread));
assert!(lock.free); // assert!(lock.free);
} // }
#[test] // #[test]
#[ignore] // #[ignore]
fn test_lock_multiple() { // fn test_lock_multiple() {
let system = init_system!(); // let system = init_system!();
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1"))); // let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2"))); // let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3"))); // let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3")));
let tm = system.borrow_mut().get_thread_manager(); // let tm = system.borrow_mut().get_thread_manager();
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1))); // tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
let mut lock = Lock::new(Rc::clone(&tm)); // let mut lock = Lock::new(Rc::clone(&tm));
assert!(lock.free); // assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system)); // lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread1))); // assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(!lock.free); // assert!(!lock.free);
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2))); // tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
lock.acquire(Some(Rc::clone(&thread2)), Rc::clone(&system)); // lock.acquire(Some(Rc::clone(&thread2)), Rc::clone(&system));
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1))); // tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
assert!(lock.held_by_current_thread(Rc::clone(&thread1))); // assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(lock.waiting_queue.iter().count() == 1); // assert!(lock.waiting_queue.iter().count() == 1);
assert!(!lock.free); // assert!(!lock.free);
lock.release(Rc::clone(&system), Rc::clone(&thread1)); // lock.release(Rc::clone(&system), Rc::clone(&thread1));
assert!(!lock.held_by_current_thread(thread1)); // assert!(!lock.held_by_current_thread(thread1));
assert!(lock.held_by_current_thread(Rc::clone(&thread2))); // assert!(lock.held_by_current_thread(Rc::clone(&thread2)));
assert!(!lock.free); // assert!(!lock.free);
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2))); // tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
lock.release(Rc::clone(&system), Rc::clone(&thread2)); // lock.release(Rc::clone(&system), Rc::clone(&thread2));
assert!(!lock.held_by_current_thread(thread2)); // assert!(!lock.held_by_current_thread(thread2));
assert!(lock.free); // assert!(lock.free);
} // }
} // }

View File

@ -2,11 +2,9 @@
//! //!
//! Module containing structs and methods pertaining to the state of the operating system //! Module containing structs and methods pertaining to the state of the operating system
use std::{cell::RefCell, rc::Rc};
use crate::simulator::machine::Machine; use crate::simulator::machine::Machine;
use super::{thread_manager::ThreadManager, thread::Thread}; use super::{thread_manager::ThreadManager};
/// This macro properly initializes the system /// This macro properly initializes the system
#[macro_export] #[macro_export]
@ -46,13 +44,6 @@ impl System {
} }
} }
/// Sets a thread asleep
///
pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
let machine = self.get_machine();
self.thread_manager.thread_sleep(machine, thread);
}
// GETTERS // GETTERS
/// Returns the Machine /// Returns the Machine
@ -88,7 +79,7 @@ pub enum ObjectType {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{System, Machine}; use crate::Machine;
#[test] #[test]
fn test_init_system() { fn test_init_system() {

View File

@ -1,9 +1,8 @@
use std::{rc::Rc, cell::{RefCell, RefMut, Ref}}; use std::{rc::Rc, cell::{RefCell, Ref}};
use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS}, interrupt::InterruptStatus}}; use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS, Machine}, interrupt::InterruptStatus}};
use crate::simulator::machine::Machine;
use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process}; use super::{thread::Thread, mgerror::ErrorCode, process::Process};
pub const SIMULATORSTACKSIZE: usize = 32 * 1024; pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
@ -18,10 +17,8 @@ pub struct ThreadManager {
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>, pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
/// The list of alive threads /// The list of alive threads
pub g_alive: List<Rc<RefCell<Thread>>>, pub g_alive: List<Rc<RefCell<Thread>>>,
/// The thread scheduler /// Thread in ready state waiting to become active
pub g_scheduler: Scheduler, ready_list: List<Rc<RefCell<Thread>>>,
/// The system owning the thread manager
pub system: Option<Rc<RefCell<System>>>
} }
impl ThreadManager { impl ThreadManager {
@ -32,8 +29,57 @@ impl ThreadManager {
g_current_thread: Option::None, g_current_thread: Option::None,
g_thread_to_be_destroyed: Option::None, g_thread_to_be_destroyed: Option::None,
g_alive: List::new(), g_alive: List::new(),
g_scheduler: Scheduler::new(), ready_list: List::new(),
system: Option::None }
}
/// Mark a thread as aready, but not necessarily running yet.
///
/// Put it in the ready list, for later scheduling onto the CPU.
///
/// ## Pamameter
///
/// **thread** is the thread to be put on the read list
pub fn ready_to_run(&mut self, thread: Rc<RefCell<Thread>>) {
self.ready_list.push(thread);
}
/// Return the next thread to be scheduled onto the CPU.
/// If there are no ready threads, return Option::None
///
/// Thread is removed from the ready list.
///
/// **return** Thread thread to be scheduled
pub fn find_next_to_run(&mut self) -> Option<Rc<RefCell<Thread>>> {
self.ready_list.pop()
}
/// Dispatch the CPU to next_thread. Save the state of the old thread
/// and load the state of the new thread.
///
/// We assume the state of the previously running thread has already been changed from running to blocked or ready.
///
/// Global variable g_current_thread become next_thread
///
/// ## Parameter
///
/// **next_thread** thread to dispatch to the CPU
pub fn switch_to(&mut self, machine: &mut Machine, next_thread: Rc<RefCell<Thread>>) {
match self.get_g_current_thread() {
Some(old) => {
let old1 = Rc::clone(old);
let old2 = Rc::clone(old);
self.thread_save_processor_state(machine, old1);
// old_thread.save_simulator_state();
if old2 != next_thread {
self.thread_restore_processor_state(machine, Rc::clone(&next_thread));
// next_thread.restore_simulator_state();
self.set_g_current_thread(Some(next_thread));
}
},
None => {
}
} }
} }
@ -48,48 +94,49 @@ impl ThreadManager {
thread_m.init_simulator_context(base_stack_addr); thread_m.init_simulator_context(base_stack_addr);
thread_m.process.as_mut().unwrap().num_thread += 1; thread_m.process.as_mut().unwrap().num_thread += 1;
self.get_g_alive().push(Rc::clone(&thread)); self.get_g_alive().push(Rc::clone(&thread));
self.g_scheduler.ready_to_run(Rc::clone(&thread)); self.ready_to_run(Rc::clone(&thread));
Result::Ok(()) Result::Ok(())
} }
/// Wait for another thread to finish its execution /// Wait for another thread to finish its execution
pub fn thread_join(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, id_thread: Rc<RefCell<Thread>>) { pub fn thread_join(&mut self, machine: &mut Machine, id_thread: Rc<RefCell<Thread>>) {
while self.get_g_alive().contains(&Rc::clone(&id_thread)) { while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
self.thread_yield(machine, thread_manager, Rc::clone(&id_thread)); self.thread_yield(machine, Rc::clone(&id_thread));
} }
} }
/// Relinquish the CPU if any other thread is runnable. /// Relinquish the CPU if any other thread is runnable.
/// ///
/// Cannot use yield as a function name -> reserved name in rust /// Cannot use yield as a function name -> reserved name in rust
pub fn thread_yield(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, thread: Rc<RefCell<Thread>>) { pub fn thread_yield(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread); assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
let next_thread = self.g_scheduler.find_next_to_run(); let next_thread = self.find_next_to_run();
if let Some(next_thread) = next_thread { if let Some(next_thread) = next_thread {
let scheduler = &mut self.g_scheduler; self.ready_to_run(thread);
scheduler.ready_to_run(thread); self.switch_to(machine, next_thread);
scheduler.switch_to(machine, thread_manager, next_thread);
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
/// Put the thread to sleep and relinquish the processor /// Put the thread to sleep and relinquish the processor
pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) { pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread); assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff); assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
let mut next_thread = self.g_scheduler.find_next_to_run();
let mut next_thread = self.find_next_to_run();
while next_thread.is_none() { while next_thread.is_none() {
eprintln!("Nobody to run => idle"); eprintln!("Nobody to run => idle");
machine.interrupt.idle(); machine.interrupt.idle();
next_thread = self.g_scheduler.find_next_to_run(); next_thread = self.find_next_to_run();
} }
self.g_scheduler.switch_to(machine, self, Rc::clone(&next_thread.unwrap())); self.switch_to(machine, Rc::clone(&next_thread.unwrap()));
} }
/// Finish the execution of the thread and prepare its deallocation /// Finish the execution of the thread and prepare its deallocation
pub fn thread_finish(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, thread: Rc<RefCell<Thread>>) { pub fn thread_finish(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread)); self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
self.g_alive.remove(Rc::clone(&thread)); self.g_alive.remove(Rc::clone(&thread));
@ -99,7 +146,7 @@ impl ThreadManager {
} }
pub fn thread_save_processor_state(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) { pub fn thread_save_processor_state(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let mut t: RefMut<_> = thread.borrow_mut(); let mut t = thread.borrow_mut();
for i in 0..NUM_INT_REGS { for i in 0..NUM_INT_REGS {
t.thread_context.int_registers[i] = machine.read_int_register(i); t.thread_context.int_registers[i] = machine.read_int_register(i);
} }
@ -109,7 +156,6 @@ impl ThreadManager {
} }
pub fn thread_restore_processor_state(&self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) { pub fn thread_restore_processor_state(&self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let t: Ref<_> = thread.borrow(); let t: Ref<_> = thread.borrow();
for i in 0..NUM_INT_REGS { for i in 0..NUM_INT_REGS {
machine.write_int_register(i, t.thread_context.int_registers[i]); machine.write_int_register(i, t.thread_context.int_registers[i]);
@ -117,15 +163,15 @@ impl ThreadManager {
} }
/// Currently running thread /// Currently running thread
pub fn get_g_current_thread(&mut self) -> &mut Option<Rc<RefCell<Thread>>> { pub fn get_g_current_thread(&mut self) -> &Option<Rc<RefCell<Thread>>> {
&mut self.g_current_thread &self.g_current_thread
} }
/// Thread to be destroyed by [...] /// Thread to be destroyed by [...]
/// ///
/// TODO: Finish the comment with the relevant value /// TODO: Finish the comment with the relevant value
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Rc<RefCell<Thread>>> { pub fn get_g_thread_to_be_destroyed(&mut self) -> &Option<Rc<RefCell<Thread>>> {
&mut self.g_thread_to_be_destroyed &self.g_thread_to_be_destroyed
} }
/// List of alive threads /// List of alive threads