-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are a number of cases in which being able to identify a runtime is useful. When instrumenting an application, this is particularly true. For example, we would like to be able to add traces for runtimes so that tasks can be differentiated (#5792). It would also allow a way to differentiate runtimes which are have their tasks dumped. Outside of instrumentation, it may be useful to check whether 2 runtime handles are pointing to the same runtime. This change adds an opaque `runtime::Id` struct which serves this purpose, initially behind the `tokio_unstable` cfg flag. The inner value of the ID is taken from the `OwnedTasks` or `LocalOwnedTasks` struct which every runtime and local set already has. This will mean that any use of the ID will align with the task dump feature. The ID is added within the scope of working towards closing #5545.
- Loading branch information
Showing
10 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::fmt; | ||
use std::num::NonZeroU64; | ||
|
||
/// An opaque ID that uniquely identifies a runtime relative to all other currently | ||
/// running runtimes. | ||
/// | ||
/// # Notes | ||
/// | ||
/// - Runtime IDs are unique relative to other *currently running* runtimes. | ||
/// 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 | ||
/// Handle. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use tokio::runtime::Handle; | ||
/// | ||
/// #[tokio::main(flavor = "multi_thread", worker_threads = 4)] | ||
/// 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 | ||
#[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 fmt::Display for Id { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters