scheduler done
This commit is contained in:
parent
adce79aaa8
commit
e3e217c57f
@ -1,3 +1,3 @@
|
|||||||
mod process;
|
mod process;
|
||||||
mod thread;
|
pub mod thread;
|
||||||
mod scheduler;
|
mod scheduler;
|
@ -1,10 +1,11 @@
|
|||||||
use crate::utility::list::List;
|
use crate::utility::list::List;
|
||||||
use crate::kernel::thread::Thread;
|
use crate::kernel::thread::Thread;
|
||||||
|
use crate::utility::system::{g_current_thread, g_thread_to_be_destroyed};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
||||||
struct Scheduler<> {
|
struct Scheduler<> {
|
||||||
ready_list: List<Rc<Thread>>
|
ready_list: List<Thread>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scheduler {
|
impl Scheduler {
|
||||||
@ -24,9 +25,48 @@ impl Scheduler {
|
|||||||
///
|
///
|
||||||
/// ## Pamameter
|
/// ## Pamameter
|
||||||
///
|
///
|
||||||
/// **thread**: 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: Rc<Thread>) {
|
pub fn ready_to_run(&mut self, thread: Thread) {
|
||||||
self.ready_list.push_back(thread);
|
self.ready_list.push_back(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<Thread> {
|
||||||
|
self.ready_list.pop_back()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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) {
|
||||||
|
let old_thread = Box::clone(&g_current_thread).unwrap();
|
||||||
|
|
||||||
|
g_current_thread.check_overflow();
|
||||||
|
|
||||||
|
g_current_thread = Box::new(Option::Some(next_thread));
|
||||||
|
|
||||||
|
old_thread.save_processor_state();
|
||||||
|
old_thread.save_simulator_state();
|
||||||
|
|
||||||
|
if(old_thread != g_current_thread) {
|
||||||
|
next_thread.restore_processor_state();
|
||||||
|
next_thread.restore_simulator_state();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(g_thread_to_be_destroyed.is_some()) {
|
||||||
|
drop(g_thread_to_be_destroyed.take());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,3 +1,8 @@
|
|||||||
|
use crate::kernel::thread::Thread;
|
||||||
|
|
||||||
|
|
||||||
|
pub static g_current_thread: Box<Option<Thread>> = Box::new(Option::None);
|
||||||
|
pub static g_thread_to_be_destroyed: Box<Option<Thread>> = Box::new(Option::None);
|
||||||
|
|
||||||
pub enum ObjectType {
|
pub enum ObjectType {
|
||||||
SEMAPHORE_TYPE,
|
SEMAPHORE_TYPE,
|
||||||
|
Loading…
Reference in New Issue
Block a user