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:
@ -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::simulator::machine::Machine;
|
||||
use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS, Machine}, interrupt::InterruptStatus}};
|
||||
|
||||
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;
|
||||
|
||||
@ -18,10 +17,8 @@ pub struct ThreadManager {
|
||||
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
|
||||
/// The list of alive threads
|
||||
pub g_alive: List<Rc<RefCell<Thread>>>,
|
||||
/// The thread scheduler
|
||||
pub g_scheduler: Scheduler,
|
||||
/// The system owning the thread manager
|
||||
pub system: Option<Rc<RefCell<System>>>
|
||||
/// Thread in ready state waiting to become active
|
||||
ready_list: List<Rc<RefCell<Thread>>>,
|
||||
}
|
||||
|
||||
impl ThreadManager {
|
||||
@ -32,8 +29,57 @@ impl ThreadManager {
|
||||
g_current_thread: Option::None,
|
||||
g_thread_to_be_destroyed: Option::None,
|
||||
g_alive: List::new(),
|
||||
g_scheduler: Scheduler::new(),
|
||||
system: Option::None
|
||||
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, 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.process.as_mut().unwrap().num_thread += 1;
|
||||
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(())
|
||||
}
|
||||
|
||||
/// 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)) {
|
||||
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.
|
||||
///
|
||||
/// 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>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
||||
pub fn thread_yield(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
|
||||
|
||||
assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread);
|
||||
let next_thread = self.g_scheduler.find_next_to_run();
|
||||
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
||||
let next_thread = self.find_next_to_run();
|
||||
if let Some(next_thread) = next_thread {
|
||||
let scheduler = &mut self.g_scheduler;
|
||||
scheduler.ready_to_run(thread);
|
||||
scheduler.switch_to(machine, thread_manager, next_thread);
|
||||
self.ready_to_run(thread);
|
||||
self.switch_to(machine, next_thread);
|
||||
}
|
||||
machine.interrupt.set_status(old_status);
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
/// Put the thread to sleep and relinquish the processor
|
||||
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);
|
||||
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() {
|
||||
eprintln!("Nobody to run => 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
|
||||
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);
|
||||
self.g_thread_to_be_destroyed = Option::Some(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>>) {
|
||||
let mut t: RefMut<_> = thread.borrow_mut();
|
||||
let mut t = thread.borrow_mut();
|
||||
for i in 0..NUM_INT_REGS {
|
||||
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>>) {
|
||||
|
||||
let t: Ref<_> = thread.borrow();
|
||||
for i in 0..NUM_INT_REGS {
|
||||
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
||||
@ -117,15 +163,15 @@ impl ThreadManager {
|
||||
}
|
||||
|
||||
/// Currently running thread
|
||||
pub fn get_g_current_thread(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
|
||||
&mut self.g_current_thread
|
||||
pub fn get_g_current_thread(&mut self) -> &Option<Rc<RefCell<Thread>>> {
|
||||
&self.g_current_thread
|
||||
}
|
||||
|
||||
/// Thread to be destroyed by [...]
|
||||
///
|
||||
/// TODO: Finish the comment with the relevant value
|
||||
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
|
||||
&mut self.g_thread_to_be_destroyed
|
||||
pub fn get_g_thread_to_be_destroyed(&mut self) -> &Option<Rc<RefCell<Thread>>> {
|
||||
&self.g_thread_to_be_destroyed
|
||||
}
|
||||
|
||||
/// List of alive threads
|
||||
|
Reference in New Issue
Block a user