mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
sync: add drop guard for cancellation token (#3839)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
//! An asynchronously awaitable `CancellationToken`.
|
//! An asynchronously awaitable `CancellationToken`.
|
||||||
//! The token allows to signal a cancellation request to one or more tasks.
|
//! The token allows to signal a cancellation request to one or more tasks.
|
||||||
|
pub(crate) mod guard;
|
||||||
|
|
||||||
use crate::loom::sync::atomic::AtomicUsize;
|
use crate::loom::sync::atomic::AtomicUsize;
|
||||||
use crate::loom::sync::Mutex;
|
use crate::loom::sync::Mutex;
|
||||||
@@ -11,6 +12,8 @@ use core::ptr::NonNull;
|
|||||||
use core::sync::atomic::Ordering;
|
use core::sync::atomic::Ordering;
|
||||||
use core::task::{Context, Poll, Waker};
|
use core::task::{Context, Poll, Waker};
|
||||||
|
|
||||||
|
use guard::DropGuard;
|
||||||
|
|
||||||
/// A token which can be used to signal a cancellation request to one or more
|
/// A token which can be used to signal a cancellation request to one or more
|
||||||
/// tasks.
|
/// tasks.
|
||||||
///
|
///
|
||||||
@@ -275,6 +278,14 @@ impl CancellationToken {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a `DropGuard` for this token.
|
||||||
|
///
|
||||||
|
/// Returned guard will cancel this token (and all its children) on drop
|
||||||
|
/// unless disarmed.
|
||||||
|
pub fn drop_guard(self) -> DropGuard {
|
||||||
|
DropGuard { inner: Some(self) }
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn register(
|
unsafe fn register(
|
||||||
&self,
|
&self,
|
||||||
wait_node: &mut ListNode<WaitQueueEntry>,
|
wait_node: &mut ListNode<WaitQueueEntry>,
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
use crate::sync::CancellationToken;
|
||||||
|
|
||||||
|
/// A wrapper for cancellation token which automatically cancels
|
||||||
|
/// it on drop. It is created using `drop_guard` method on the `CancellationToken`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DropGuard {
|
||||||
|
pub(super) inner: Option<CancellationToken>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DropGuard {
|
||||||
|
/// Returns stored cancellation token and removes this drop guard instance
|
||||||
|
/// (i.e. it will no longer cancel token). Other guards for this token
|
||||||
|
/// are not affected.
|
||||||
|
pub fn disarm(mut self) -> CancellationToken {
|
||||||
|
self.inner
|
||||||
|
.take()
|
||||||
|
.expect("`inner` can be only None in a destructor")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for DropGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if let Some(inner) = &self.inner {
|
||||||
|
inner.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Synchronization primitives
|
//! Synchronization primitives
|
||||||
|
|
||||||
mod cancellation_token;
|
mod cancellation_token;
|
||||||
pub use cancellation_token::{CancellationToken, WaitForCancellationFuture};
|
pub use cancellation_token::{guard::DropGuard, CancellationToken, WaitForCancellationFuture};
|
||||||
|
|
||||||
mod intrusive_double_linked_list;
|
mod intrusive_double_linked_list;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user