From fe8158f415ad9834c8c7bd3d1832b198c16588f5 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 24 Feb 2022 10:07:03 -0800 Subject: [PATCH] cleanup feature flags, add abort module Signed-off-by: Eliza Weisman --- tokio/src/runtime/task/abort.rs | 69 +++++++++++++++++++++++++++++++++ tokio/src/runtime/task/join.rs | 67 ++------------------------------ tokio/src/runtime/task/mod.rs | 12 +++--- 3 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 tokio/src/runtime/task/abort.rs diff --git a/tokio/src/runtime/task/abort.rs b/tokio/src/runtime/task/abort.rs new file mode 100644 index 00000000000..b56cb5cd81a --- /dev/null +++ b/tokio/src/runtime/task/abort.rs @@ -0,0 +1,69 @@ +use crate::runtime::task::RawTask; +use std::fmt; +use std::panic::{RefUnwindSafe, UnwindSafe}; + +/// An owned permission to abort a spawned task, without awaiting its completion. +/// +/// Unlike a [`JoinHandle`], an `AbortHandle` does *not* represent the +/// permission to await the task's completion, only to terminate it. +/// +/// The task may be aborted by calling the [`AbortHandle::abort`] method. +/// Dropping an `AbortHandle` releases the permission to terminate the task +/// --- it does *not* abort the task. +/// +/// **Note**: This is an [unstable API][unstable]. The public API of this type +/// may break in 1.x releases. See [the documentation on unstable +/// features][unstable] for details. +/// +/// [unstable]: crate#unstable-features +/// [`JoinHandle`]: crate::task::JoinHandle +#[cfg_attr(docsrs, doc(cfg(all(feature = "rt", tokio_unstable))))] +#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] +pub struct AbortHandle { + raw: Option, +} + +impl AbortHandle { + pub(super) fn new(raw: Option) -> Self { + Self { raw } + } + + /// Abort the task associated with the handle. + /// + /// Awaiting a cancelled task 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`. + /// + /// If the task was already cancelled, such as by [`JoinHandle::abort`], + /// this method will do nothing. + /// + /// [cancelled]: method@super::error::JoinError::is_cancelled + // the `AbortHandle` type is only publicly exposed when `tokio_unstable` is + // enabled, but it is still defined for testing purposes. + #[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] + pub fn abort(self) { + if let Some(raw) = self.raw { + raw.remote_abort(); + } + } +} + +unsafe impl Send for AbortHandle {} +unsafe impl Sync for AbortHandle {} + +impl UnwindSafe for AbortHandle {} +impl RefUnwindSafe for AbortHandle {} + +impl fmt::Debug for AbortHandle { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("AbortHandle").finish() + } +} + +impl Drop for AbortHandle { + fn drop(&mut self) { + if let Some(raw) = self.raw.take() { + raw.drop_abort_handle(); + } + } +} diff --git a/tokio/src/runtime/task/join.rs b/tokio/src/runtime/task/join.rs index 61d2abe1de7..b7846348e34 100644 --- a/tokio/src/runtime/task/join.rs +++ b/tokio/src/runtime/task/join.rs @@ -146,27 +146,6 @@ cfg_rt! { raw: Option, _p: PhantomData, } - - /// An owned permission to abort a spawned task, without awaiting its completion. - /// - /// Unlike a [`JoinHandle`], an `AbortHandle` does *not* represent the - /// permission to await the task's completion, only to terminate it. - /// - /// The task may be aborted by calling the [`AbortHandle::abort`] method. - /// Dropping an `AbortHandle` releases the permission to terminate the task - /// --- it does *not* abort the task. - /// - /// **Note**: This is an [unstable API][unstable]. The public API of this type - /// may break in 1.x releases. See [the documentation on unstable - /// features][unstable] for details. - /// - /// [unstable]: crate#unstable-features - #[cfg(any(tokio_unstable, test))] - #[cfg_attr(docsrs, doc(cfg(tokio_unstable)))] - pub struct AbortHandle { - raw: Option, - } - } unsafe impl Send for JoinHandle {} @@ -233,12 +212,13 @@ impl JoinHandle { } /// Returns a new `AbortHandle` that can be used to remotely abort this task. - pub(crate) fn abort_handle(&self) -> AbortHandle { + #[cfg(any(tokio_unstable, test))] + pub(crate) fn abort_handle(&self) -> super::AbortHandle { let raw = self.raw.map(|raw| { raw.ref_inc(); raw }); - AbortHandle { raw } + super::AbortHandle::new(raw) } } @@ -303,44 +283,3 @@ where fmt.debug_struct("JoinHandle").finish() } } - -impl AbortHandle { - /// Abort the task associated with the handle. - /// - /// Awaiting a cancelled task 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`. - /// - /// If the task was already cancelled, such as by [`JoinHandle::abort`], - /// this method will do nothing. - /// - /// [cancelled]: method@super::error::JoinError::is_cancelled - // the `AbortHandle` type is only publicly exposed when `tokio_unstable` is - // enabled, but it is still defined for testing purposes. - #[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] - pub fn abort(self) { - if let Some(raw) = self.raw { - raw.remote_abort(); - } - } -} - -unsafe impl Send for AbortHandle {} -unsafe impl Sync for AbortHandle {} - -impl UnwindSafe for AbortHandle {} -impl RefUnwindSafe for AbortHandle {} - -impl fmt::Debug for AbortHandle { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_struct("AbortHandle").finish() - } -} - -impl Drop for AbortHandle { - fn drop(&mut self) { - if let Some(raw) = self.raw.take() { - raw.drop_abort_handle(); - } - } -} diff --git a/tokio/src/runtime/task/mod.rs b/tokio/src/runtime/task/mod.rs index ee8f656aa7d..c7a85c5a173 100644 --- a/tokio/src/runtime/task/mod.rs +++ b/tokio/src/runtime/task/mod.rs @@ -155,15 +155,13 @@ cfg_rt_multi_thread! { pub(super) use self::inject::Inject; } +#[cfg(all(feature = "rt", any(tokio_unstable, test)))] +mod abort; mod join; -cfg_unstable! { - #[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 - pub use self::join::AbortHandle; -} -#[cfg(all(not(tokio_unstable), test))] -#[allow(unused_imports)] -pub(crate) use self::join::AbortHandle; +#[cfg(all(feature = "rt", any(tokio_unstable, test)))] +#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 +pub use self::abort::AbortHandle; #[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 pub use self::join::JoinHandle;