pub struct Queue<S> { /* private fields */ }Expand description
A queue of tasks that take &mut S, which the thread that runs the queue
owns. Cheap to clone: clones are handles to the same queue.
Tasks run in the order they were posted, and a timed task once its deadline passed, before the tasks of the same turn. A task that a closed queue gets drops at once, so tasks that hold handles to their own queue can’t keep it alive once its owner closed it.
Implementations§
Source§impl<S> Queue<S>
impl<S> Queue<S>
pub fn new() -> Self
Sourcepub fn post(&self, task: impl FnOnce(&mut S) + Send + 'static)
pub fn post(&self, task: impl FnOnce(&mut S) + Send + 'static)
Runs task on the queue’s thread, after the tasks posted before it.
Sourcepub fn post_at(
&self,
deadline: Instant,
task: impl FnOnce(&mut S) + Send + 'static,
) -> TimedTask
pub fn post_at( &self, deadline: Instant, task: impl FnOnce(&mut S) + Send + 'static, ) -> TimedTask
Runs task on the queue’s thread in its first turn once deadline
passed, before the turn’s other tasks, unless it’s
cancelled first.
Sourcepub fn post_after(
&self,
delay: Duration,
task: impl FnOnce(&mut S) + Send + 'static,
) -> TimedTask
pub fn post_after( &self, delay: Duration, task: impl FnOnce(&mut S) + Send + 'static, ) -> TimedTask
post_at delay from now. A delay past what an
Instant can hold never comes.
Sourcepub fn cancel(&self, task: TimedTask)
pub fn cancel(&self, task: TimedTask)
Drops the timed task that task names, on this thread, unless it
has begun to run or dropped already: it never runs, and from now on
next_deadline and the queue’s parking no
longer wait for it. A host that armed a timeout for its deadline
before still sees that timeout end.
Sourcepub fn set_waker(&self, waker: Option<Waker>)
pub fn set_waker(&self, waker: Option<Waker>)
Calls waker, from the posting thread, whenever a task arrives or
the queue is woken, so that a host that runs the queue with
run_once knows to run it. A timed task calls it
when it’s posted, not when its deadline passes.
Sourcepub fn run_once(&self, state: &mut S) -> bool
pub fn run_once(&self, state: &mut S) -> bool
Runs the timed tasks whose deadline passed, then the tasks waiting
now and those they post, with state, until none waits. Returns
whether any task ran.
Sourcepub fn has_pending(&self) -> bool
pub fn has_pending(&self) -> bool
Whether a task waits for run_once, or a timed
task’s deadline passed.
Sourcepub fn next_deadline(&self) -> Option<Instant>
pub fn next_deadline(&self) -> Option<Instant>
The deadline of the earliest timed task that waits, which a cancelled one no longer does.
Sourcepub fn park(&self)
pub fn park(&self)
Sleeps until a task arrives or the queue is woken, or the next timed task is due. It returns at once when that happened since it last returned, as while the queue was running.
Sourcepub fn park_until(&self, deadline: Option<Instant>)
pub fn park_until(&self, deadline: Option<Instant>)
park, but returns at deadline at the latest.
Sourcepub fn run(&self, state: &mut S)
pub fn run(&self, state: &mut S)
Runs the queue with state on this thread until a stop task runs,
parking while it has nothing to do: what a thread of its own does.
Sourcepub fn run_with(&self, state: &mut S, each_turn: impl FnMut(&mut S))
pub fn run_with(&self, state: &mut S, each_turn: impl FnMut(&mut S))
run, and each_turn after each turn, before the
queue parks, for an owner with work of its own on this thread, which
wakes the queue when it has some.
Sourcepub fn stop(&self)
pub fn stop(&self)
Ends the run in progress, or the next one, at the end
of the turn that runs this stop task: once the tasks posted before it
ran, and those that wait in the same turn after it.