mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-24 00:00:11 +02:00
task: stabilize task ids (#6793)
Co-authored-by: Alice Ryhl <[email protected]>
This commit is contained in:
co-authored by
Alice Ryhl
parent
6c5dbfa08c
commit
b68f5c7f38
@@ -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()) }
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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")
|
||||
@@ -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()
|
||||
|
||||
@@ -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()) }
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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")]
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user