runtime: stabilize LocalRuntime (#7557)

This commit is contained in:
Daksh
2026-04-02 13:13:24 +00:00
committed by GitHub
parent 324218f9bb
commit 1fc450aefb
6 changed files with 13 additions and 54 deletions
-22
View File
@@ -476,12 +476,7 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
},
};
let mut checks = vec![];
let mut errors = vec![];
let build = if let RuntimeFlavor::Local = config.flavor {
checks.push(quote! { tokio_unstable });
errors.push("The local runtime flavor is only available when `tokio_unstable` is set.");
quote_spanned! {last_stmt_start_span=> build_local(Default::default())}
} else {
quote_spanned! {last_stmt_start_span=> build()}
@@ -509,23 +504,10 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
quote! {}
};
let do_checks: TokenStream = checks
.iter()
.zip(&errors)
.map(|(check, error)| {
quote! {
#[cfg(not(#check))]
compile_error!(#error);
}
})
.collect();
let body_ident = quote! { body };
// This explicit `return` is intentional. See tokio-rs/tokio#4636
let last_block = quote_spanned! {last_stmt_end_span=>
#do_checks
#[cfg(all(#(#checks),*))]
#[allow(clippy::expect_used, clippy::diverging_sub_expression, clippy::needless_return, clippy::unwrap_in_result)]
{
#use_builder
@@ -537,10 +519,6 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
.block_on(#body_ident);
}
#[cfg(not(all(#(#checks),*)))]
{
panic!("fell through checks")
}
};
let body = input.body();
-13
View File
@@ -73,16 +73,11 @@ use proc_macro::TokenStream;
///
/// ## Local
///
/// [Unstable API][unstable] only.
///
/// To use the [local runtime], the macro can be configured using
///
/// ```rust
/// # #[cfg(tokio_unstable)]
/// #[tokio::main(flavor = "local")]
/// # async fn main() {}
/// # #[cfg(not(tokio_unstable))]
/// # fn main() {}
/// ```
///
/// # Function arguments
@@ -165,25 +160,19 @@ use proc_macro::TokenStream;
///
/// ## Using the local runtime
///
/// Available in the [unstable API][unstable] only.
///
/// The [local runtime] is similar to the current-thread runtime but
/// supports [`task::spawn_local`](../tokio/task/fn.spawn_local.html).
///
/// ```rust
/// # #[cfg(tokio_unstable)]
/// #[tokio::main(flavor = "local")]
/// async fn main() {
/// println!("Hello world");
/// }
/// # #[cfg(not(tokio_unstable))]
/// # fn main() {}
/// ```
///
/// Equivalent code not using `#[tokio::main]`
///
/// ```rust
/// # #[cfg(tokio_unstable)]
/// fn main() {
/// tokio::runtime::Builder::new_current_thread()
/// .enable_all()
@@ -193,8 +182,6 @@ use proc_macro::TokenStream;
/// println!("Hello world");
/// })
/// }
/// # #[cfg(not(tokio_unstable))]
/// # fn main() {}
/// ```
///
///
+4 -5
View File
@@ -5,7 +5,9 @@ use crate::runtime::{
blocking, driver, Callback, HistogramBuilder, Runtime, TaskCallback, TimerFlavor,
};
#[cfg(tokio_unstable)]
use crate::runtime::{metrics::HistogramConfiguration, LocalOptions, LocalRuntime, TaskMeta};
use crate::runtime::{metrics::HistogramConfiguration, TaskMeta};
use crate::runtime::{LocalOptions, LocalRuntime};
use crate::util::rand::{RngSeed, RngSeedGenerator};
use crate::runtime::blocking::BlockingPool;
@@ -241,7 +243,7 @@ impl Builder {
/// Configuration methods can be chained on the return value.
///
/// To spawn non-`Send` tasks on the resulting runtime, combine it with a
/// [`LocalSet`], or call [`build_local`] to create a [`LocalRuntime`] (unstable).
/// [`LocalSet`], or call [`build_local`] to create a [`LocalRuntime`].
///
/// [`LocalSet`]: crate::task::LocalSet
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
@@ -1048,8 +1050,6 @@ impl Builder {
/// });
/// ```
#[allow(unused_variables, unreachable_patterns)]
#[cfg(tokio_unstable)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
pub fn build_local(&mut self, options: LocalOptions) -> io::Result<LocalRuntime> {
match &self.kind {
Kind::CurrentThread => self.build_current_thread_local_runtime(),
@@ -1605,7 +1605,6 @@ impl Builder {
))
}
#[cfg(tokio_unstable)]
fn build_current_thread_local_runtime(&mut self) -> io::Result<LocalRuntime> {
use crate::runtime::local_runtime::LocalRuntimeScheduler;
@@ -29,7 +29,6 @@ use std::time::Duration;
/// [runtime]: crate::runtime::Runtime
/// [module]: crate::runtime
#[derive(Debug)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
pub struct LocalRuntime {
/// Task scheduler
scheduler: LocalRuntimeScheduler,
+6 -6
View File
@@ -36,9 +36,9 @@
//! | Yes | No
//! | |
//! V |
//! +--------------------------+ |
//! | Local Runtime (unstable) | |
//! +--------------------------+ |
//! +---------------+ |
//! | Local Runtime | |
//! +---------------+ |
//! |
//! v
//! +------------------------+
@@ -575,9 +575,6 @@ cfg_rt! {
pub use self::builder::UnhandledPanic;
pub use crate::util::rand::RngSeed;
mod local_runtime;
pub use local_runtime::{LocalRuntime, LocalOptions};
/// Returns the index of the current worker thread, if called from a
/// runtime worker thread.
///
@@ -635,6 +632,9 @@ cfg_rt! {
mod runtime;
pub use runtime::{Runtime, RuntimeFlavor, is_rt_shutdown_err};
mod local_runtime;
pub use local_runtime::{LocalRuntime, LocalOptions};
mod id;
pub use id::Id;
+3 -7
View File
@@ -328,9 +328,8 @@ impl<'a> Drop for LocalDataEnterGuard<'a> {
cfg_rt! {
/// Spawns a `!Send` future on the current [`LocalSet`] or [`LocalRuntime`].
///
/// This is possible when either using one of these types
/// explicitly, or (with `tokio_unstable`) by opting to use the
/// `"local"` runtime flavor in `tokio::main`:
/// This is possible when either using one of these types explicitly, or by
/// opting to use the `"local"` runtime flavor in `tokio::main`:
///
/// ```ignore
/// #[tokio::main(flavor = "local")]
@@ -374,10 +373,9 @@ cfg_rt! {
/// }).await;
/// # }
/// ```
/// With local runtime flavor ([Unstable API][unstable] only).
/// With local runtime flavor.
///
/// ```rust
/// # #[cfg(tokio_unstable)]
/// #[tokio::main(flavor = "local")]
/// async fn main() {
/// let join = tokio::task::spawn_local(async {
@@ -386,8 +384,6 @@ cfg_rt! {
///
/// join.await.unwrap()
/// }
/// # #[cfg(not(tokio_unstable))]
/// # fn main() {}
///
/// ```
///