Skip to content

Commit

Permalink
task: stabilize task ids (#6793)
Browse files Browse the repository at this point in the history
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
  • Loading branch information
evanrittenhouse and Darksonn authored Oct 6, 2024
1 parent 6c5dbfa commit b68f5c7
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 45 deletions.
7 changes: 0 additions & 7 deletions tokio/src/runtime/task/abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ impl AbortHandle {
/// Returns a [task ID] that uniquely identifies this task relative to other
/// currently spawned tasks.
///
/// **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.
///
/// [task ID]: crate::task::Id
/// [unstable]: crate#unstable-features
#[cfg(tokio_unstable)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
pub fn id(&self) -> super::Id {
// Safety: The header pointer is valid.
unsafe { Header::get_id(self.raw.header_ptr()) }
Expand Down
21 changes: 1 addition & 20 deletions tokio/src/runtime/task/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ use std::{fmt, num::NonZeroU64};
/// task via the [`task::try_id()`](crate::task::try_id()) and
/// [`task::id()`](crate::task::id()) functions and from outside the task via
/// the [`JoinHandle::id()`](crate::task::JoinHandle::id()) function.
///
/// **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_attr(docsrs, doc(cfg(all(feature = "rt", tokio_unstable))))]
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "rt"))))]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct Id(pub(crate) NonZeroU64);

Expand All @@ -35,13 +28,7 @@ pub struct Id(pub(crate) NonZeroU64);
/// within a call to `block_on`. For a version of this function that doesn't
/// panic, see [`task::try_id()`](crate::runtime::task::try_id()).
///
/// **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.
///
/// [task ID]: crate::task::Id
/// [unstable]: crate#unstable-features
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
#[track_caller]
pub fn id() -> Id {
context::current_task_id().expect("Can't get a task id when not inside a task")
Expand All @@ -54,13 +41,7 @@ pub fn id() -> Id {
/// that it returns `None` rather than panicking if called outside of a task
/// context.
///
/// **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.
///
/// [task ID]: crate::task::Id
/// [unstable]: crate#unstable-features
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
#[track_caller]
pub fn try_id() -> Option<Id> {
context::current_task_id()
Expand Down
7 changes: 0 additions & 7 deletions tokio/src/runtime/task/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,7 @@ impl<T> JoinHandle<T> {
/// Returns a [task ID] that uniquely identifies this task relative to other
/// currently spawned tasks.
///
/// **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.
///
/// [task ID]: crate::task::Id
/// [unstable]: crate#unstable-features
#[cfg(tokio_unstable)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
pub fn id(&self) -> super::Id {
// Safety: The header pointer is valid.
unsafe { Header::get_id(self.raw.header_ptr()) }
Expand Down
4 changes: 1 addition & 3 deletions tokio/src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,7 @@ cfg_rt! {
#[cfg(tokio_unstable)]
pub mod join_set;

cfg_unstable! {
pub use crate::runtime::task::{Id, id, try_id};
}
pub use crate::runtime::task::{Id, id, try_id};

cfg_trace! {
mod builder;
Expand Down
14 changes: 6 additions & 8 deletions tokio/tests/task_id.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
#![allow(unknown_lints, unexpected_cfgs)]
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", tokio_unstable))]
#![cfg(feature = "full")]

#[cfg(not(target_os = "wasi"))]
use std::error::Error;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#[cfg(not(target_os = "wasi"))]
use tokio::runtime::{Builder, Runtime};
use tokio::runtime::Runtime;
use tokio::sync::oneshot;
use tokio::task::{self, Id, LocalSet};

#[cfg(not(target_os = "wasi"))]
mod support {
pub mod panic;
}
#[cfg(not(target_os = "wasi"))]
use support::panic::test_panic;

#[tokio::test(flavor = "current_thread")]
Expand Down Expand Up @@ -256,6 +252,8 @@ async fn task_id_nested_spawn_local() {
#[cfg(not(target_os = "wasi"))]
#[tokio::test(flavor = "multi_thread")]
async fn task_id_block_in_place_block_on_spawn() {
use tokio::runtime::Builder;

task::spawn(async {
let parent_id = task::id();

Expand All @@ -273,8 +271,8 @@ async fn task_id_block_in_place_block_on_spawn() {
.unwrap();
}

#[cfg(not(target_os = "wasi"))]
#[test]
#[cfg_attr(not(panic = "unwind"), ignore)]
fn task_id_outside_task_panic_caller() -> Result<(), Box<dyn Error>> {
let panic_location_file = test_panic(|| {
let _ = task::id();
Expand All @@ -286,8 +284,8 @@ fn task_id_outside_task_panic_caller() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[cfg(not(target_os = "wasi"))]
#[test]
#[cfg_attr(not(panic = "unwind"), ignore)]
fn task_id_inside_block_on_panic_caller() -> Result<(), Box<dyn Error>> {
let panic_location_file = test_panic(|| {
let rt = Runtime::new().unwrap();
Expand Down

0 comments on commit b68f5c7

Please sign in to comment.