Scheduler

Struct Scheduler 

Source
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

Source

pub fn new() -> Self

Source

pub fn add_task(&mut self, task: Task, cpu_id: usize) -> usize

Source

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
Source

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.

Source

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
Source

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.

Source

pub fn get_current_task(&mut self, cpu_id: usize) -> Option<&Task>

Source

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
Source

pub fn get_current_task_id(&self, cpu_id: usize) -> Option<usize>

Source

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.

Source

pub fn wake_task(&mut self, task_id: usize) -> bool

Move a task from blocked queue to ready queue when it’s woken up

This method is called by Waker when a blocked task needs to be woken up.

§Arguments
  • task_id - The ID of the task to move to ready queue
§Returns

true if the task was found and moved, false otherwise

Source

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
Source

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.

Source

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 ID
  • from_task_id - Current task ID
  • to_task_id - Next task ID
Source

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 state
  • task - The task to setup for execution

Auto Trait Implementations§

§

impl Freeze for Scheduler

§

impl RefUnwindSafe for Scheduler

§

impl Send for Scheduler

§

impl Sync for Scheduler

§

impl Unpin for Scheduler

§

impl UnwindSafe for Scheduler

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.