Skip to content

Commit

Permalink
task: make JoinHandle::abort_handle public
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkyPotato committed Mar 13, 2023
1 parent bfc4379 commit dd3a5e9
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion tokio/src/runtime/task/join.rs
Expand Up @@ -252,7 +252,41 @@ impl<T> JoinHandle<T> {
}

/// Returns a new `AbortHandle` that can be used to remotely abort this task.
pub(crate) fn abort_handle(&self) -> super::AbortHandle {
///
/// Awaiting a task cancelled by the `AbortHandle` might complete as usual if the task was
/// already completed at the time it was cancelled, but most likely it
/// will fail with a [cancelled] `JoinError`.
///
/// ```rust
/// use tokio::{time, task};
///
/// #[tokio::main]
/// async fn main() {
/// let mut handles = Vec::new();
///
/// handles.push(tokio::spawn(async {
/// time::sleep(time::Duration::from_secs(10)).await;
/// true
/// }));
///
/// handles.push(tokio::spawn(async {
/// time::sleep(time::Duration::from_secs(10)).await;
/// false
/// }));
///
/// let abort_handles: Vec<task::AbortHandle> = handles.iter().map(|h| h.abort_handle()).collect();
///
/// for handle in abort_handles {
/// handle.abort();
/// }
///
/// for handle in handles {
/// assert!(handle.await.unwrap_err().is_cancelled());
/// }
/// }
/// ```
/// [cancelled]: method@super::error::JoinError::is_cancelled
pub fn abort_handle(&self) -> super::AbortHandle {
self.raw.ref_inc();
super::AbortHandle::new(self.raw)
}
Expand Down

0 comments on commit dd3a5e9

Please sign in to comment.