2023-03-01 16:55:17 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-02-28 16:39:40 +01:00
|
|
|
use crate::utility::list::List;
|
|
|
|
use crate::kernel::thread::Thread;
|
2023-03-01 11:16:21 +01:00
|
|
|
use crate::utility::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED};
|
2023-02-28 16:39:40 +01:00
|
|
|
|
|
|
|
|
2023-03-01 16:55:17 +01:00
|
|
|
pub struct Scheduler {
|
|
|
|
ready_list: List<Arc<Thread>>
|
2023-02-28 16:39:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
///
|
2023-03-01 10:11:19 +01:00
|
|
|
/// **thread** is the thread to be put on the read list
|
2023-03-01 16:55:17 +01:00
|
|
|
pub fn ready_to_run(&mut self, thread: Arc<Thread>) {
|
|
|
|
self.ready_list.push(thread);
|
2023-02-28 16:39:40 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 10:11:19 +01:00
|
|
|
/// 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
|
2023-03-01 16:55:17 +01:00
|
|
|
pub fn find_next_to_run(&mut self) -> Option<Arc<Thread>> {
|
|
|
|
self.ready_list.pop()
|
2023-03-01 10:11:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(&self, next_thread: Thread) {
|
2023-03-01 11:16:21 +01:00
|
|
|
match G_CURRENT_THREAD.write() {
|
2023-03-01 11:10:15 +01:00
|
|
|
Ok(mut current_thread) => {
|
|
|
|
let old_thread = current_thread.as_mut().unwrap();
|
2023-03-01 10:11:19 +01:00
|
|
|
|
2023-03-01 11:10:15 +01:00
|
|
|
old_thread.save_processor_state();
|
|
|
|
old_thread.save_simulator_state();
|
2023-03-01 10:11:19 +01:00
|
|
|
|
2023-03-01 11:10:15 +01:00
|
|
|
if old_thread != &next_thread {
|
|
|
|
next_thread.restore_processor_state();
|
|
|
|
next_thread.restore_simulator_state();
|
|
|
|
current_thread.replace(next_thread);
|
|
|
|
}
|
2023-03-01 10:11:19 +01:00
|
|
|
|
2023-03-01 11:16:21 +01:00
|
|
|
match G_THREAD_TO_BE_DESTROYED.write() {
|
2023-03-01 11:10:15 +01:00
|
|
|
Ok(mut thread_to_be_destroyed) => {
|
|
|
|
if thread_to_be_destroyed.is_some() {
|
|
|
|
drop(thread_to_be_destroyed.take());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
panic!("RwLock is poisonned: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
panic!("RwLock is poisonned: {}", err);
|
|
|
|
}
|
2023-03-01 10:11:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-28 16:39:40 +01:00
|
|
|
}
|