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
impl ShutdownBarrier
Sourcepub fn spawn(&self) -> Result<(), ShutdownBarrierError>
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()
Sourcepub fn cancel(&self) -> Result<(), ShutdownBarrierError>
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.
Sourcepub fn done(&self) -> Result<ShutdownBarrierDoneResult, ShutdownBarrierError>
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().
Sourcepub async fn wait(
&self,
) -> Result<ShutdownBarrierWaitResult, ShutdownBarrierError>
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.