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

rt: add runtime Id #5864

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,12 @@ impl Handle {
/// [unstable]: crate#unstable-features
/// [`Id`]: struct@crate::runtime::Id
pub fn id(&self) -> runtime::Id {
match &self.inner {
scheduler::Handle::CurrentThread(handle) => handle.runtime_id,
let owned_id = match &self.inner {
scheduler::Handle::CurrentThread(handle) => handle.owned_id(),
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
scheduler::Handle::MultiThread(handle) => handle.runtime_id,
}
scheduler::Handle::MultiThread(handle) => handle.owned_id(),
};
runtime::Id::from_u64(owned_id)
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions tokio/src/runtime/id.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether merge all taskid/threadid/runtimeid as one type id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping a separate type because we don't want users to be able to compare (for example) a task ID to a runtime ID. Also, the runtime::Id implementation is now pretty different as we're using the value from the (Local)OwnedTasks ID.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt;
/// # Notes
///
/// - Runtime IDs are unique relative to other *currently running* runtimes.
/// When a task completes, the same ID may be used for another task.
/// When a runtime completes, the same ID may be used for another runtime.
/// - Runtime IDs are *not* sequential, and do not indicate the order in which
/// runtimes are started or any other data.
/// - The runtime ID of the currently running task can be obtained from the
Expand All @@ -32,18 +32,14 @@ use std::fmt;
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct Id(u64);
hds marked this conversation as resolved.
Show resolved Hide resolved

impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
impl Id {
pub(crate) fn from_u64(val: u64) -> Self {
Id(val)
}
}

impl Id {
pub(crate) fn next() -> Self {
use crate::loom::sync::atomic::{Ordering::Relaxed, StaticAtomicU64};

static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1);

Self(NEXT_ID.fetch_add(1, Relaxed))
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
13 changes: 6 additions & 7 deletions tokio/src/runtime/scheduler/current_thread.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::future::poll_fn;
use crate::loom::sync::atomic::AtomicBool;
use crate::loom::sync::Arc;
#[cfg(tokio_unstable)]
use crate::runtime;
use crate::runtime::driver::{self, Driver};
use crate::runtime::scheduler::{self, Defer, Inject};
use crate::runtime::task::{self, JoinHandle, OwnedTasks, Schedule, Task};
Expand Down Expand Up @@ -43,9 +41,6 @@ pub(crate) struct Handle {

/// Current random number generator seed
pub(crate) seed_generator: RngSeedGenerator,

#[cfg(tokio_unstable)]
pub(crate) runtime_id: runtime::Id,
}

/// Data required for executing the scheduler. The struct is passed around to
Expand Down Expand Up @@ -146,8 +141,6 @@ impl CurrentThread {
driver: driver_handle,
blocking_spawner,
seed_generator,
#[cfg(tokio_unstable)]
runtime_id: runtime::Id::next(),
});

let core = AtomicCell::new(Some(Box::new(Core {
Expand Down Expand Up @@ -548,6 +541,12 @@ cfg_metrics! {
}
}

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

impl fmt::Debug for Handle {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("current_thread::Handle { ... }").finish()
Expand Down
9 changes: 4 additions & 5 deletions tokio/src/runtime/scheduler/multi_thread/handle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::future::Future;
use crate::loom::sync::Arc;
#[cfg(tokio_unstable)]
use crate::runtime;
use crate::runtime::scheduler::multi_thread::worker;
use crate::runtime::{
blocking, driver,
Expand Down Expand Up @@ -32,9 +30,6 @@ pub(crate) struct Handle {

/// Current random number generator seed
pub(crate) seed_generator: RngSeedGenerator,

#[cfg(tokio_unstable)]
pub(crate) runtime_id: runtime::Id,
}

impl Handle {
Expand Down Expand Up @@ -64,6 +59,10 @@ impl Handle {

handle
}

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

impl fmt::Debug for Handle {
Expand Down
4 changes: 1 addition & 3 deletions tokio/src/runtime/scheduler/multi_thread/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub(crate) struct Shared {
idle: Idle,

/// Collection of all active tasks spawned onto this executor.
pub(super) owned: OwnedTasks<Arc<Handle>>,
pub(crate) owned: OwnedTasks<Arc<Handle>>,

/// Data synchronized by the scheduler mutex
pub(super) synced: Mutex<Synced>,
Expand Down Expand Up @@ -302,8 +302,6 @@ pub(super) fn create(
driver: driver_handle,
blocking_spawner,
seed_generator,
#[cfg(tokio_unstable)]
runtime_id: runtime::Id::next(),
});

let mut launch = Launch(vec![]);
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/task/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ cfg_not_has_atomic_u64! {

pub(crate) struct OwnedTasks<S: 'static> {
inner: Mutex<CountedOwnedTasksInner<S>>,
id: u64,
pub(crate) id: u64,
}
struct CountedOwnedTasksInner<S: 'static> {
list: CountedLinkedList<Task<S>, <Task<S> as Link>::Target>,
closed: bool,
}
pub(crate) struct LocalOwnedTasks<S: 'static> {
inner: UnsafeCell<OwnedTasksInner<S>>,
id: u64,
pub(crate) id: u64,
_not_send_or_sync: PhantomData<*const ()>,
}
struct OwnedTasksInner<S: 'static> {
Expand Down
26 changes: 26 additions & 0 deletions tokio/src/task/local.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Runs `!Send` futures on the current thread.
use crate::loom::cell::UnsafeCell;
use crate::loom::sync::{Arc, Mutex};
#[cfg(tokio_unstable)]
use crate::runtime;
use crate::runtime::task::{self, JoinHandle, LocalOwnedTasks, Task};
use crate::runtime::{context, ThreadId};
use crate::sync::AtomicWaker;
Expand Down Expand Up @@ -785,6 +787,30 @@ cfg_unstable! {
.unhandled_panic = behavior;
self
}

/// Returns the [`Id`] of the current `LocalSet` runtime.
///
/// # Examples
///
/// ```rust
/// use tokio::task;
///
/// #[tokio::main]
/// async fn main() {
/// let local_set = task::LocalSet::new();
/// println!("Local set id: {}", local_set.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 {
runtime::Id::from_u64(self.context.shared.local_state.owned.id)
}
}
}

Expand Down