akern-gkgoat-fork/ableos/src/scheduler.rs

86 lines
1.9 KiB
Rust

use {
crate::kmain::THREAD_LIST,
alloc::{vec, vec::Vec},
core::cmp::Ordering,
};
pub type Pointer = fn();
pub type ThreadID = u64;
pub type TaskID = u64;
pub type ThreadList = Vec<Thread>;
#[allow(dead_code)]
#[derive(Eq, Debug)]
pub struct Task {
id: TaskID,
parent_thread: ThreadID,
fn_pointer: Pointer,
}
impl Task {
fn new(parent_thread: ThreadID, task_id: u64, function_pointer: fn()) -> Self {
debug!(
"New task with TaskID: {} | Parent ThreadID: {}",
task_id, parent_thread
);
Self {
id: task_id,
parent_thread,
fn_pointer: function_pointer,
}
}
}
impl Ord for Task {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl PartialOrd for Task {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Task {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
#[derive(Debug)]
pub struct Thread {
pub id: ThreadID,
pub tasks: Vec<Task>,
}
impl Thread {
pub fn new() -> Self {
let threads = &*THREAD_LIST.lock();
let mut final_threadid = 0;
match threads.last() {
Some(last_thread) => final_threadid = last_thread.id + 1,
None => {}
}
debug!("New thread with ThreadID: {}", final_threadid);
Self {
id: final_threadid,
tasks: vec![],
}
}
pub fn sort_tasks(&mut self) {
self.tasks.sort();
}
pub fn new_task_id(&self) -> TaskID {
let task_id = self.tasks.len().try_into().unwrap();
trace!("New TaskID: {}", task_id);
task_id
}
pub fn new_task(&mut self, function_pointer: fn()) {
let x = Task::new(self.id, self.new_task_id(), function_pointer);
self.tasks.push(x);
}
}
pub fn test_fn() {
println!("Hello");
}