Skip to main content

ScheduledWakerQueue

Struct ScheduledWakerQueue 

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

Priority queue of scheduled wakers to wake up processes at specific times

This struct represents a priority queue of scheduled wakers, where each waker is associated with a specific time at which a process should be woken up.

The queue is effectively an extension of WakerSet ordered by wake time, and is (currently) implemented as a pair of a BTreeSet and a HashMap to allow efficient insertion and deduplication of wakers. See the documentation of WakerSet for more details on the data structure of wakers and the rationale behind it.

Like WakerSet, wakers in this queue are compared by their pointer addresses, and dead wakers in the queue may be automatically removed as a side effect of other operations.

Implementations§

Source§

impl ScheduledWakerQueue

Source

pub fn new() -> Self

Creates a new empty ScheduledWakerQueue.

Source

pub fn len(&self) -> usize

Returns the number of scheduled wakers in the queue.

Wakers may become dead over time, so the actual number of valid wakers may be less than this count.

Source

pub fn is_empty(&self) -> bool

Checks if the queue is empty.

Wakers may become dead over time, so there may be no valid wakers even if this method returns false.

Source

pub fn clear(&mut self)

Clears all scheduled wakers from the queue.

Source

pub fn push( &mut self, wake_time: Instant, waker: Weak<Cell<Option<Waker>>>, ) -> bool

Pushes a new scheduled waker into the queue.

This method adds a new scheduled waker to the priority queue so that the associated process can be woken up at the specified time.

Returns true if the waker was successfully added to the queue, or false if it was already present or dead, in which case the passed weak reference is dropped.

The amortized time complexity of this method is O(log(n)). If the queue is full, it will first clean up dead wakers and possibly reallocate to optimize the capacity for future insertions, which will cost O(n log(n)) time. Because of this cleanup, the number of wakers in the queue may decrease after calling this method, regardless of whether the new waker was added or not.

Source

pub fn next_wake_time(&self) -> Option<Instant>

Returns the next scheduled wake time, if any.

This method peeks at the priority queue to find the scheduled waker with the earliest wake time. If the queue is not empty, it returns the wake time of that waker; otherwise, it returns None.

If you have a mutable reference to the queue, you can use trim_to_next_wake_time instead of this method to remove dead wakers as the queue is traversed to find the next wake time.

Source

pub fn trim_to_next_wake_time(&mut self) -> Option<Instant>

Trims dead wakers to find the next wake time.

This method removes dead wakers from the beginning of the priority queue until it finds a live waker or the queue becomes empty. The return value is the wake time of the first live waker in the queue after trimming, or None if the queue is empty.

This method is solely for optimization purposes and does not affect the correctness of the queue. Using this method instead of next_wake_time can help avoid unnecessary processing of dead wakers, particularly when next_wake_time is followed by wake that will remove dead wakers anyway.

The push method will also call this method to clean up dead wakers before inserting a new waker, so you don’t need to call this method manually in most cases.

Source

pub fn wake(&mut self, now: Instant)

Wakes up processes whose scheduled wake time has been reached.

This method checks the priority queue for any scheduled wakers whose wake time is less than or equal to the current time (now). For each such waker, it takes the waker from the Cell and calls wake() on it to wake up the associated process. After waking up the process, the item is removed from the queue.

Trait Implementations§

Source§

impl Clone for ScheduledWakerQueue

Source§

fn clone(&self) -> ScheduledWakerQueue

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ScheduledWakerQueue

Source§

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

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

impl Default for ScheduledWakerQueue

Source§

fn default() -> ScheduledWakerQueue

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.