Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize runtime::id::Id #7125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 23 additions & 32 deletions tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(tokio_unstable)]
use crate::runtime;
use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics};

/// Handle to the runtime.
Expand Down Expand Up @@ -404,36 +402,29 @@ impl Handle {
}
}

cfg_unstable! {
/// Returns the [`Id`] of the current `Runtime`.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// println!("Current runtime id: {}", Handle::current().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.
///
/// [unstable]: crate#unstable-features
/// [`Id`]: struct@crate::runtime::Id
pub fn id(&self) -> runtime::Id {
let owned_id = match &self.inner {
scheduler::Handle::CurrentThread(handle) => handle.owned_id(),
#[cfg(feature = "rt-multi-thread")]
scheduler::Handle::MultiThread(handle) => handle.owned_id(),
#[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
scheduler::Handle::MultiThreadAlt(handle) => handle.owned_id(),
};
owned_id.into()
}
/// Returns the [`Id`] of the current `Runtime`.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// println!("Current runtime id: {}", Handle::current().id());
/// }
/// ```
///
/// [`Id`]: crate::runtime::Id
pub fn id(&self) -> crate::runtime::Id {
let owned_id = match &self.inner {
scheduler::Handle::CurrentThread(handle) => handle.owned_id(),
#[cfg(feature = "rt-multi-thread")]
scheduler::Handle::MultiThread(handle) => handle.owned_id(),
#[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
scheduler::Handle::MultiThreadAlt(handle) => handle.owned_id(),
};
crate::runtime::Id::new(owned_id)
}

/// Returns a view that lets you get information about how the runtime
Expand Down
21 changes: 4 additions & 17 deletions tokio/src/runtime/id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use std::num::{NonZeroU32, NonZeroU64};
use std::num::NonZeroU64;

/// An opaque ID that uniquely identifies a runtime relative to all other currently
/// running runtimes.
Expand All @@ -23,25 +23,12 @@ use std::num::{NonZeroU32, NonZeroU64};
/// println!("Current runtime id: {}", Handle::current().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.
///
/// [unstable]: crate#unstable-features
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct Id(NonZeroU64);

impl From<NonZeroU64> for Id {
fn from(value: NonZeroU64) -> Self {
Id(value)
}
}

impl From<NonZeroU32> for Id {
fn from(value: NonZeroU32) -> Self {
Id(value.into())
impl Id {
pub(crate) fn new(integer: impl Into<NonZeroU64>) -> Self {
Self(integer.into())
}
}

Expand Down
8 changes: 4 additions & 4 deletions tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ cfg_rt! {
mod builder;
pub use self::builder::Builder;
cfg_unstable! {
mod id;
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
pub use id::Id;

pub use self::builder::UnhandledPanic;
pub use crate::util::rand::RngSeed;

Expand All @@ -396,6 +392,10 @@ cfg_rt! {
mod runtime;
pub use runtime::{Runtime, RuntimeFlavor};

mod id;
pub use id::Id;


/// Boundary value to prevent stack overflow caused by a large-sized
/// Future being placed in the stack.
pub(crate) const BOX_FUTURE_THRESHOLD: usize = if cfg!(debug_assertions) {
Expand Down
10 changes: 4 additions & 6 deletions tokio/src/runtime/scheduler/current_thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,11 @@ cfg_unstable_metrics! {
}
}

cfg_unstable! {
use std::num::NonZeroU64;
use std::num::NonZeroU64;

impl Handle {
pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id
}
impl Handle {
pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id
}
}

Expand Down
10 changes: 4 additions & 6 deletions tokio/src/runtime/scheduler/multi_thread/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ impl Handle {
}
}

cfg_unstable! {
use std::num::NonZeroU64;
use std::num::NonZeroU64;

impl Handle {
pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id
}
impl Handle {
pub(crate) fn owned_id(&self) -> NonZeroU64 {
self.shared.owned.id
}
}

Expand Down
2 changes: 1 addition & 1 deletion tokio/src/task/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ cfg_unstable! {
/// [unstable]: crate#unstable-features
/// [`Id`]: struct@crate::runtime::Id
pub fn id(&self) -> runtime::Id {
self.context.shared.local_state.owned.id.into()
runtime::Id::new(self.context.shared.local_state.owned.id)
}
}
}
Expand Down
29 changes: 12 additions & 17 deletions tokio/tests/rt_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,22 @@ fn drop_tasks_with_reference_cycle() {
});
}

#[cfg(tokio_unstable)]
mod unstable {
use super::*;

#[test]
fn runtime_id_is_same() {
let rt = rt();
#[test]
fn runtime_id_is_same() {
let rt = rt();

let handle1 = rt.handle();
let handle2 = rt.handle();
let handle1 = rt.handle();
let handle2 = rt.handle();

assert_eq!(handle1.id(), handle2.id());
}
assert_eq!(handle1.id(), handle2.id());
}

#[test]
fn runtime_ids_different() {
let rt1 = rt();
let rt2 = rt();
#[test]
fn runtime_ids_different() {
let rt1 = rt();
let rt2 = rt();

assert_ne!(rt1.handle().id(), rt2.handle().id());
}
assert_ne!(rt1.handle().id(), rt2.handle().id());
}

fn rt() -> Runtime {
Expand Down
Loading