pub struct Scheduler {
ready_queue: [VecDeque<usize>; 2],
blocked_queue: [VecDeque<usize>; 2],
zombie_queue: [VecDeque<usize>; 2],
current_task_id: [Option<usize>; 2],
}Fields§
§ready_queue: [VecDeque<usize>; 2]Queue for ready-to-run task IDs
blocked_queue: [VecDeque<usize>; 2]Queue for blocked task IDs (waiting for I/O, etc.)
zombie_queue: [VecDeque<usize>; 2]Queue for zombie task IDs (finished but not yet cleaned up)
current_task_id: [Option<usize>; 2]Implementations§
Source§impl Scheduler
impl Scheduler
pub fn new() -> Self
pub fn add_task(&mut self, task: Task, cpu_id: usize) -> usize
Sourcefn run(&mut self, cpu: &Arch) -> (Option<usize>, Option<usize>)
fn run(&mut self, cpu: &Arch) -> (Option<usize>, Option<usize>)
Determines the next task to run and returns current and next task IDs
This method performs the core scheduling algorithm and task state management without performing actual context switches or hardware setup.
§Arguments
cpu- The CPU architecture state (for CPU ID)
§Returns
(old_task_id, new_task_id)- Tuple of old and new task IDs
Sourcepub fn on_tick(&mut self, cpu_id: usize, trapframe: &mut Trapframe)
pub fn on_tick(&mut self, cpu_id: usize, trapframe: &mut Trapframe)
Called every timer tick. Decrements the current task’s time_slice. If time_slice reaches 0, triggers a reschedule.
Sourcepub fn schedule(&mut self, trapframe: &mut Trapframe)
pub fn schedule(&mut self, trapframe: &mut Trapframe)
Schedule tasks on the CPU with kernel context switching
This function performs cooperative scheduling by switching between task kernel contexts. It returns to the caller, allowing the trap handler to handle user space return.
§Arguments
cpu- The CPU architecture state
Sourcepub fn start_scheduler(&mut self) -> Option<usize>
pub fn start_scheduler(&mut self) -> Option<usize>
Start the scheduler and return the first runnable task ID (if any).
This function intentionally avoids performing the initial user-mode transition.
The very first switch is architecture-specific and should be performed by
crate::arch::first_switch_to_user() from the boot path.
pub fn get_current_task(&mut self, cpu_id: usize) -> Option<&Task>
Sourcepub unsafe fn get_current_task_mut(
&mut self,
cpu_id: usize,
) -> Option<&mut Task>
pub unsafe fn get_current_task_mut( &mut self, cpu_id: usize, ) -> Option<&mut Task>
Get a mutable reference to the current task on the specified CPU
§Safety
This function returns a mutable reference to the current task, which can lead to undefined behavior if misused. The caller must ensure:
- No other references (mutable or immutable) to the same task exist
- The task is not concurrently accessed from other contexts
- The scheduler’s invariants are maintained
pub fn get_current_task_id(&self, cpu_id: usize) -> Option<usize>
Sourcepub fn get_task_by_id(&mut self, task_id: usize) -> Option<&mut Task>
pub fn get_task_by_id(&mut self, task_id: usize) -> Option<&mut Task>
Returns a mutable reference to the task with the specified ID, if found.
This method searches the TaskPool to find the task with the specified ID. This is needed for Waker integration.
§Arguments
task_id- The ID of the task to search for.
§Returns
A mutable reference to the task if found, or None otherwise.
Sourcepub fn cleanup_zombie_task(&mut self, task_id: usize)
pub fn cleanup_zombie_task(&mut self, task_id: usize)
Clean up a zombie task after it has been waited on
This removes the task from zombie_queue and task_pool, freeing all resources. Should only be called from Task::wait() after confirming the task is a zombie.
§Arguments
task_id- The ID of the zombie task to clean up
Sourcepub fn get_all_task_ids(&self) -> Vec<usize>
pub fn get_all_task_ids(&self) -> Vec<usize>
Get IDs of all tasks across ready, blocked, and zombie queues
This helper is used by subsystems (e.g., event broadcast) that need to target every task in the system without holding a mutable reference to the scheduler during delivery.
Sourcefn kernel_context_switch(
&mut self,
cpu_id: usize,
from_task_id: usize,
to_task_id: usize,
)
fn kernel_context_switch( &mut self, cpu_id: usize, from_task_id: usize, to_task_id: usize, )
Perform kernel context switch between tasks
This function handles the low-level kernel context switching between the current task and the next selected task. It also saves/restores FPU/SIMD/Vector context for user-space tasks.
§Arguments
cpu_id- The CPU IDfrom_task_id- Current task IDto_task_id- Next task ID
Sourcepub fn setup_task_execution(cpu: &mut Arch, task: &mut Task)
pub fn setup_task_execution(cpu: &mut Arch, task: &mut Task)
Setup task execution by configuring hardware and user context
This replaces the old dispatcher functionality with a more direct approach.
§Arguments
cpu- The CPU architecture statetask- The task to setup for execution