File size: 2,546 Bytes
433dc73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | // ==============================================================================
// VYRON Micro-OS - Task Abstraction & State Machine
// Layer 3: Task Representation
// ==============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskState {
Ready,
Running,
Blocked,
Terminated,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TaskPriority {
Low = 1,
Normal = 2,
High = 3,
}
#[derive(Debug, Clone)]
pub struct TaskContext {
pub instruction_pointer: usize,
pub stack_pointer: usize,
pub registers: [u64; 8],
}
impl TaskContext {
pub fn new(entry_point: usize, stack_top: usize) -> Self {
Self {
instruction_pointer: entry_point,
stack_pointer: stack_top,
registers: [0; 8],
}
}
}
#[derive(Debug, Clone)]
pub struct Task {
pub id: u32,
pub name: String,
pub state: TaskState,
pub priority: TaskPriority,
pub total_burst: u32,
pub remaining_burst: u32,
pub waiting_time: u32,
pub turnaround_time: u32,
pub arrival_time: u64,
pub completion_time: Option<u64>,
pub context: TaskContext,
}
impl Task {
pub fn new(id: u32, name: &str, burst_ticks: u32, priority: TaskPriority, arrival: u64) -> Self {
Self {
id,
name: name.to_string(),
state: TaskState::Ready,
priority,
total_burst: burst_ticks,
remaining_burst: burst_ticks,
waiting_time: 0,
turnaround_time: 0,
arrival_time: arrival,
completion_time: None,
context: TaskContext::new(0x1000 + (id as usize * 0x100), 0x8000),
}
}
pub fn execute_step(&mut self, current_tick: u64) -> bool {
if self.state != TaskState::Running {
self.state = TaskState::Running;
}
if self.remaining_burst > 0 {
self.remaining_burst -= 1;
self.context.instruction_pointer += 4;
}
if self.remaining_burst == 0 {
self.state = TaskState::Terminated;
self.completion_time = Some(current_tick);
self.turnaround_time = (current_tick - self.arrival_time) as u32;
true // Task completed
} else {
false // Still has work
}
}
pub fn increment_wait(&mut self) {
if self.state == TaskState::Ready {
self.waiting_time += 1;
}
}
}
|