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§
Sourcefn enqueue(&self, thread: ReadyRef)
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
Sourcefn pick_next(&self, cpu_id: CpuId) -> Option<ReadyRef>
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.
Sourcefn on_tick(&self, current: &RunningRef) -> Option<ReadyRef>
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.
Sourcefn set_priority(&self, thread_id: ThreadId, priority: u8)
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 modifypriority
- New priority value (0-255, higher = more important)
Provided Methods§
Sourcefn on_yield(&self, current: RunningRef)
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
Sourcefn on_block(&self, current: RunningRef)
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