-
-
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. It follows the same pattern as the `task::Id` struct. The Id can be compared for equality with another `runtime::Id` and implements `Debug` and `Display` so that it can be output as well. Internally the Id is a `u64`, but that is an implementation detail. There is a degree of code duplication, but that is necessary to ensure that the Ids are not used to compare against one another. The Id is added within the scope of working towards closing #5545.
- Loading branch information
Showing
8 changed files
with
138 additions
and
0 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,49 @@ | ||
use std::fmt; | ||
|
||
/// 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 task completes, the same ID may be used for another task. | ||
/// - 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(u64); | ||
|
||
impl fmt::Display for Id { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
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)) | ||
} | ||
} |
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