pub struct CancellationObserver { /* private fields */ }Expand description
Observes one CancellationToken without taking its lock.
CancellationToken::is_cancelled locks the token’s tree node, and every wait on the token
locks it on each poll. When many tasks on different threads check one token, such as all
requests of a connection, that lock moves between cores on every check. An observer reads an
atomic flag instead, and its waits register in per-thread waiter lists.
The flag is set while the token is being cancelled: the observer registers a waker with the
token, which CancellationToken::cancel calls before it returns, also when the cancellation
comes from a parent token. A thread that knows a cancellation has returned therefore sees
is_cancelled return true, as it would on the token itself.
Cloning an observer shares its flag and waiter lists.
Implementations§
Source§impl CancellationObserver
impl CancellationObserver
Sourcepub fn new(token: CancellationToken) -> Self
pub fn new(token: CancellationToken) -> Self
Creates an observer of token.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns whether the token is cancelled, with one atomic load.
Sourcepub fn cancelled(&self) -> impl Future<Output = ()> + Send + '_
pub fn cancelled(&self) -> impl Future<Output = ()> + Send + '_
Waits until the token is cancelled.
Sourcepub fn token(&self) -> &CancellationToken
pub fn token(&self) -> &CancellationToken
Returns the observed token.