Skip to main content

Scheduler

Struct Scheduler 

Source
pub struct Scheduler { /* private fields */ }
Expand description

The three-lane scheduler.

Uses binary heaps for O(log n) insertion instead of O(n) VecDeque insertion. Generation counters provide FIFO ordering within same priority/deadline.

Implementations§

Source§

impl Scheduler

Source

pub fn new() -> Self

Creates a new empty scheduler.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a scheduler with pre-allocated capacity for lanes and dedup set.

The capacity is applied per lane to reduce heap growth on bursty workloads.

Source

pub fn len(&self) -> usize

Returns the total number of scheduled tasks.

Source

pub fn is_empty(&self) -> bool

Returns true if no tasks are scheduled.

Source

pub fn has_runnable_work(&self, now: Time) -> bool

Returns true if there is work that can be executed immediately.

Returns true if:

  • Cancel lane is not empty
  • Ready lane is not empty
  • Timed lane has a task with deadline <= now
Source

pub fn next_deadline(&self) -> Option<Time>

Returns the earliest deadline from the timed lane, if any.

Source

pub fn schedule(&mut self, task: TaskId, priority: u8)

Schedules a task in the ready lane.

Does nothing if the task is already scheduled. O(log n) insertion via binary heap.

Source

pub fn schedule_cancel(&mut self, task: TaskId, priority: u8)

Schedules or promotes a task into the cancel lane.

If the task is already scheduled, it is moved to the cancel lane to ensure cancellation preempts timed/ready work. O(log n) insertion for new tasks; O(n) for promotions.

Source

pub fn schedule_timed(&mut self, task: TaskId, deadline: Time)

Schedules a task in the timed lane.

Does nothing if the task is already scheduled. O(log n) insertion via binary heap.

Source

pub fn pop(&mut self) -> Option<TaskId>

Pops the next task to run.

Order: cancel lane > timed lane > ready lane. O(log n) pop via binary heap.

Source

pub fn pop_with_rng_hint(&mut self, rng_hint: u64) -> Option<TaskId>

Pops the next task to run, using rng_hint for tie-breaking among equal-priority tasks.

Order: cancel lane > timed lane > ready lane. O(log n) pop via binary heap.

Source

pub fn pop_with_lane(&mut self, rng_hint: u64) -> Option<(TaskId, DispatchLane)>

Pop the highest-priority task across all three lanes, returning both the task and the lane it was dispatched from.

Lane priority: Cancel > Timed > Ready (same as pop_with_rng_hint).

This method is deadline-agnostic for timed tasks. If your caller keeps future timed tasks in the scheduler, use Self::pop_with_lane_if_due instead to prevent dispatch before deadline.

Source

pub fn pop_with_lane_if_due( &mut self, rng_hint: u64, now: Time, ) -> Option<(TaskId, DispatchLane)>

Pop across all three lanes while enforcing timed deadline readiness.

Lane priority remains Cancel > Timed > Ready, but timed tasks are dispatched only when deadline <= now.

Source

pub fn pop_cancel_with_rng( &mut self, rng_hint: u64, ) -> Option<(TaskId, DispatchLane)>

Pop a task from the cancel lane using deterministic RNG tie-breaking.

Source

pub fn pop_non_cancel_with_rng( &mut self, rng_hint: u64, ) -> Option<(TaskId, DispatchLane)>

Pop a task from timed or ready lanes (excluding cancel lane).

Timed lane has priority over ready lane.

This method is deadline-agnostic for timed tasks. If your caller keeps future timed tasks in the scheduler, use Self::pop_non_cancel_with_rng_if_due to prevent early dispatch.

Source

pub fn pop_non_cancel_with_rng_if_due( &mut self, rng_hint: u64, now: Time, ) -> Option<(TaskId, DispatchLane)>

Pop from timed or ready lanes while enforcing timed deadline readiness.

Timed lane retains priority over ready lane, but timed tasks are dispatched only when deadline <= now.

Source

pub fn remove(&mut self, task: TaskId)

Removes a specific task from the scheduler.

O(n) rebuild of affected lane. This is acceptable since removal is rare compared to schedule/pop operations.

Source

pub fn move_to_cancel_lane(&mut self, task: TaskId, priority: u8)

Moves a task to the cancel lane (highest priority).

If the task is not currently scheduled, it will be added to the cancel lane. If the task is already in the cancel lane, its priority may be updated.

This is the key operation for ensuring cancelled tasks get priority: the cancel lane is always drained before timed and ready lanes.

O(n) for finding and removing from other lanes, O(log n) for insertion.

Source

pub fn is_in_cancel_lane(&self, task: TaskId) -> bool

Returns true if a task is in the cancel lane.

Source

pub fn pop_cancel_only(&mut self) -> Option<TaskId>

Pops only from the cancel lane.

Use this for strict cancel-first processing in multi-worker scenarios. O(log n) pop via binary heap.

Source

pub fn pop_cancel_only_with_hint(&mut self, rng_hint: u64) -> Option<TaskId>

Pops only from the cancel lane with RNG tie-breaking.

Source

pub fn pop_timed_only(&mut self, now: Time) -> Option<TaskId>

Pops only from the timed lane if the earliest deadline is due.

Returns None if no timed tasks exist or the earliest deadline has not yet been reached. This prevents timed tasks from firing before their deadline when in the local scheduler.

O(log n) pop via binary heap.

Source

pub fn pop_timed_only_with_hint( &mut self, rng_hint: u64, now: Time, ) -> Option<TaskId>

Pops only from the timed lane if the earliest deadline is due, with RNG tie-breaking among tasks sharing the earliest deadline.

Source

pub fn pop_ready_only(&mut self) -> Option<TaskId>

Pops only from the ready lane.

Use this for strict lane ordering in multi-worker scenarios. O(log n) pop via binary heap.

Source

pub fn pop_ready_only_with_hint(&mut self, rng_hint: u64) -> Option<TaskId>

Pops only from the ready lane with RNG tie-breaking among equal priorities.

Source

pub fn pop_any_lane_with_hint( &mut self, rng_hint: u64, now: Time, ) -> Option<(u8, TaskId)>

Checks all local lanes in priority order (cancel > timed > ready) in a single call, avoiding repeated lock acquisitions when the caller would check each lane sequentially.

Returns (lane_tag, task_id) where lane_tag is 0=cancel, 1=timed, 2=ready.

Source

pub fn steal_ready_batch(&mut self, max_steal: usize) -> Vec<(TaskId, u8)>

Steals a batch of ready tasks for another worker.

Only steals from the ready lane to preserve cancel/timed priority semantics. Returns the stolen tasks with their priorities.

O(k log n) where k is the number of tasks stolen.

Source

pub fn steal_ready_batch_into( &mut self, max_steal: usize, out: &mut Vec<(TaskId, u8)>, ) -> usize

Steals ready tasks into a caller-provided buffer.

Returns the number of tasks stolen.

Source

pub fn has_cancel_work(&self) -> bool

Returns true if the cancel lane has pending tasks.

Source

pub fn has_timed_work(&self) -> bool

Returns true if the timed lane has pending tasks.

Source

pub fn has_ready_work(&self) -> bool

Returns true if the ready lane has pending tasks.

Source

pub fn clear(&mut self)

Clears all scheduled tasks.

Trait Implementations§

Source§

impl Debug for Scheduler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Scheduler

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more