ShutdownBarrier

Struct ShutdownBarrier 

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

Similar to tokio::sync::Barrier, but you don’t need to know how many waiters there will be up front. It starts with one waiter, and more can be added dynamically with spawn() (which will return an error if invoked after shutdown).

This barrier’s API handles two commonly-overlooked race conditions:

  • It is OK to listen for completion before the first worker starts execution. The wait() will not finish until all the workers that spawn complete.
  • It is OK to start listening for completion after the last worker exits. In this case, the wait() will immediately complete.

You can also invoke cancel(), which causes the wait result’s is_cancelled() method to return true for all waiters.

Implementations§

Source§

impl ShutdownBarrier

Source

pub fn spawn(&self) -> Result<(), ShutdownBarrierError>

Register another worker with the barrier.

Returns Error if the barrier has already been completed. The barrier starts with one worker (the one that is the parent of all work), so this will never happen if you are careful not to invoke spawn() after the parent task invokes done()

Source

pub fn cancel(&self) -> Result<(), ShutdownBarrierError>

Inform the barrier that whatever work all the workers are performing has been cancelled. This call causes wait() to return immediately with cancelled = true.

Source

pub fn done(&self) -> Result<ShutdownBarrierDoneResult, ShutdownBarrierError>

Inform the barrier that a single worker has completed.

Returns a ShutdownBarrierDoneResult, with is_cancelled = true if the pool of work protected by the barrier was cancelled, and shutdown_leader = true if this call to done() was the one that completed the pool of work. Workers can check for shutdown_leader = true to perform clean up logic outside the thread of control that invokes done().

Source

pub async fn wait( &self, ) -> Result<ShutdownBarrierWaitResult, ShutdownBarrierError>

Waits until the number of workers reaches zero. This can be called at any time and can be called multiple times.

Source

pub fn new() -> Self

Returns a new shutdown barrier with a single worker. The caller should spawn() all the work that needs to be done, then invoke done(). This makes sure the worker count doesn’t spuriously reach zero while work is being spawned.

Trait Implementations§

Source§

impl Default for ShutdownBarrier

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, 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, 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.