2021-12-24 04:05:23 -06:00
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
kmain::THREAD_LIST,
|
|
|
|
log::{self, Log},
|
|
|
|
},
|
|
|
|
alloc::{vec, vec::Vec},
|
|
|
|
core::cmp::Ordering,
|
|
|
|
};
|
2021-12-24 03:30:27 -06:00
|
|
|
|
|
|
|
pub type Pointer = fn();
|
|
|
|
pub type ThreadID = u64;
|
|
|
|
pub type TaskID = u64;
|
|
|
|
pub type ThreadList = Vec<Thread>;
|
|
|
|
|
|
|
|
#[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 {
|
2021-12-24 04:05:23 -06:00
|
|
|
debug!(
|
|
|
|
"New task with TaskID: {} | Parent ThreadID: {}",
|
|
|
|
task_id, parent_thread
|
|
|
|
);
|
|
|
|
|
2021-12-24 03:30:27 -06:00
|
|
|
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 => {}
|
|
|
|
}
|
2021-12-24 04:05:23 -06:00
|
|
|
debug!("New thread with ThreadID: {}", final_threadid);
|
2021-12-24 03:30:27 -06:00
|
|
|
Self {
|
|
|
|
id: final_threadid,
|
|
|
|
tasks: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn sort_tasks(&mut self) {
|
|
|
|
self.tasks.sort();
|
|
|
|
}
|
|
|
|
pub fn new_task_id(&self) -> TaskID {
|
2021-12-24 04:05:23 -06:00
|
|
|
let task_id = self.tasks.len().try_into().unwrap();
|
|
|
|
trace!("New TaskID: {}", task_id);
|
|
|
|
task_id
|
2021-12-24 03:30:27 -06:00
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|