Scheduler

Trait Scheduler 

Source
pub trait Scheduler: Send + Sync {
    // Required methods
    fn enqueue(&self, thread: ReadyRef);
    fn pick_next(&self, cpu_id: CpuId) -> Option<ReadyRef>;
    fn on_tick(&self, current: &RunningRef) -> Option<ReadyRef>;
    fn set_priority(&self, thread_id: ThreadId, priority: u8);

    // Provided methods
    fn on_yield(&self, current: RunningRef) { ... }
    fn on_block(&self, current: RunningRef) { ... }
    fn wake_up(&self, thread: ReadyRef) { ... }
    fn stats(&self) -> (usize, usize, usize) { ... }
}
Expand description

New scheduler trait for lock-free implementations.

This trait defines the interface that all scheduler implementations must provide. It’s designed to support lock-free operation and per-CPU scheduling.

Required Methods§

Source

fn enqueue(&self, thread: ReadyRef)

Enqueue a thread that is ready to run.

This is called when a thread becomes ready to run (either newly created, woken up from blocking, or preempted and should continue running).

§Arguments
  • thread - Ready thread to enqueue
Source

fn pick_next(&self, cpu_id: CpuId) -> Option<ReadyRef>

Pick the next thread to run on the given CPU.

This is called by the scheduler when a CPU needs a new thread to run. It should return the thread with the highest priority or best scheduling characteristics according to the algorithm.

§Arguments
  • cpu_id - ID of the CPU requesting the next thread
§Returns

The next thread to run, or None if no threads are ready.

Source

fn on_tick(&self, current: &RunningRef) -> Option<ReadyRef>

Handle a scheduler tick for the currently running thread.

This is called periodically from timer interrupts to allow the scheduler to make preemption decisions. The scheduler should check if the current thread should be preempted and handle time slice accounting.

§Arguments
  • current - Reference to the currently running thread
§Returns

Some(ready_thread) if the current thread should be preempted and replaced with the returned thread. None if the current thread should continue running.

Source

fn set_priority(&self, thread_id: ThreadId, priority: u8)

Set the priority of a thread.

This updates the scheduling priority of the given thread. The scheduler may need to reorder queues or adjust scheduling parameters.

§Arguments
  • thread_id - ID of the thread to modify
  • priority - New priority value (0-255, higher = more important)

Provided Methods§

Source

fn on_yield(&self, current: RunningRef)

Handle a thread yielding the CPU voluntarily.

This is called when a thread explicitly yields (e.g., via yield_now()). The scheduler may treat this differently from preemption.

§Arguments
  • current - The thread that is yielding
Source

fn on_block(&self, current: RunningRef)

Handle a thread blocking (going to sleep).

This is called when a thread blocks on I/O, synchronization primitives, or other blocking operations. The thread is removed from scheduling queues until it’s explicitly woken up.

§Arguments
  • current - The thread that is blocking
Source

fn wake_up(&self, thread: ReadyRef)

Wake up a blocked thread.

This is called when a blocked thread should become ready to run again (e.g., I/O completed, lock acquired, condition signaled).

§Arguments
  • thread - The thread to wake up
Source

fn stats(&self) -> (usize, usize, usize)

Get scheduler statistics.

Returns various metrics about the scheduler state for monitoring and debugging purposes.

§Returns

A tuple of (total_threads, runnable_threads, blocked_threads).

Implementors§