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 LocalRuntime #6808

Merged
merged 32 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5b87bc6
rt: add LocalRuntime
Noah-Kennedy Aug 30, 2024
1ba8bc0
fix oversite with tokio_unstable gates leading to compile failure
Noah-Kennedy Aug 30, 2024
e9ca7b8
appease clippy
Noah-Kennedy Aug 30, 2024
439c084
fix paths
Noah-Kennedy Aug 30, 2024
20e7bda
fix localset spawn
Noah-Kennedy Aug 30, 2024
5aa073b
no rustc, i actually want it this way to prevent this from accidental…
Noah-Kennedy Aug 30, 2024
169578f
useless lints
Noah-Kennedy Aug 30, 2024
2faea81
i dislike having spellcheck
Noah-Kennedy Aug 30, 2024
d7fa83e
i dislike having spellcheck even more
Noah-Kennedy Aug 30, 2024
29d72ba
fix typo
Noah-Kennedy Sep 10, 2024
0dcfcd9
get rid of spawn, improve docs for spawn_local
Noah-Kennedy Sep 10, 2024
662e37e
Update tokio/src/runtime/local_runtime/runtime.rs
Noah-Kennedy Sep 16, 2024
17a3862
Update tokio/src/runtime/local_runtime/runtime.rs
Noah-Kennedy Sep 16, 2024
11715ac
take LocalOptions by reference in build_local
Noah-Kennedy Oct 2, 2024
2339646
make LocalOptions !Send + !Sync
Noah-Kennedy Oct 2, 2024
073a049
fix safety docs
Noah-Kennedy Oct 2, 2024
fdb4976
cleanup spawn_blocking docs
Noah-Kennedy Oct 2, 2024
db4d554
cleanup runtime docs
Noah-Kennedy Oct 2, 2024
0e6b6a4
cleanup runtime docs
Noah-Kennedy Oct 2, 2024
30721a5
hopefully this fixes docs
Noah-Kennedy Oct 2, 2024
8c45993
add tests
Noah-Kennedy Oct 2, 2024
40e69ed
"spelling"
Noah-Kennedy Oct 2, 2024
4b58bdb
i am going to become the joker
Noah-Kennedy Oct 2, 2024
4979831
hah, spellcheck doesnt even catch my actual grammar issues
Noah-Kennedy Oct 2, 2024
6fc68fb
Update tokio/src/runtime/local_runtime/runtime.rs
Noah-Kennedy Oct 4, 2024
8e414e6
Update tokio/src/runtime/local_runtime/runtime.rs
Noah-Kennedy Oct 4, 2024
20b4c92
add panics section to build_local
Noah-Kennedy Oct 4, 2024
bf5e5f9
send big futures to the heap in release mode
Noah-Kennedy Oct 4, 2024
85f2c36
document that handle.enter() allows spawn_local with LocalRuntime
Noah-Kennedy Oct 4, 2024
017adc9
document that only blocking tasks leak
Noah-Kennedy Oct 4, 2024
613dcd6
update with tracing task size stuff
Noah-Kennedy Oct 12, 2024
92cafd6
we stabilized id!
Noah-Kennedy Oct 12, 2024
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
81 changes: 72 additions & 9 deletions tokio/src/runtime/builder.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#![cfg_attr(loom, allow(unused_imports))]

use crate::runtime::handle::Handle;
#[cfg(tokio_unstable)]
use crate::runtime::TaskMeta;
use crate::runtime::{blocking, driver, Callback, HistogramBuilder, Runtime, TaskCallback};
#[cfg(tokio_unstable)]
use crate::runtime::{LocalOptions, LocalRuntime, TaskMeta};
use crate::util::rand::{RngSeed, RngSeedGenerator};

use crate::runtime::blocking::BlockingPool;
use crate::runtime::scheduler::CurrentThread;
use std::fmt;
use std::io;
use std::thread::ThreadId;
use std::time::Duration;

/// Builds Tokio Runtime with custom configuration values.
Expand Down Expand Up @@ -800,6 +803,37 @@ impl Builder {
}
}

/// Creates the configured `LocalRuntime`.
///
/// The returned `LocalRuntime` instance is ready to spawn tasks.
///
/// # Panics
/// This will panic if `current_thread` is not the selected runtime flavor.
/// All other runtime flavors are unsupported by [`LocalRuntime`].
///
/// [`LocalRuntime`]: [crate::runtime::LocalRuntime]
///
/// # Examples
///
/// ```
/// use tokio::runtime::Builder;
///
/// let rt = Builder::new_current_thread().build_local(&mut Default::default()).unwrap();
///
/// rt.block_on(async {
/// println!("Hello from the Tokio runtime");
/// });
/// ```
#[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(),
_ => panic!("Only current_thread is supported when building a local runtime"),
}
}

fn get_cfg(&self, workers: usize) -> driver::Cfg {
driver::Cfg {
enable_pause_time: match self.kind {
Expand Down Expand Up @@ -1191,8 +1225,40 @@ impl Builder {
}

fn build_current_thread_runtime(&mut self) -> io::Result<Runtime> {
use crate::runtime::scheduler::{self, CurrentThread};
use crate::runtime::{runtime::Scheduler, Config};
use crate::runtime::runtime::Scheduler;

let (scheduler, handle, blocking_pool) =
self.build_current_thread_runtime_components(None)?;

Ok(Runtime::from_parts(
Scheduler::CurrentThread(scheduler),
handle,
blocking_pool,
))
}

#[cfg(tokio_unstable)]
fn build_current_thread_local_runtime(&mut self) -> io::Result<LocalRuntime> {
use crate::runtime::local_runtime::LocalRuntimeScheduler;

let tid = std::thread::current().id();

let (scheduler, handle, blocking_pool) =
self.build_current_thread_runtime_components(Some(tid))?;

Ok(LocalRuntime::from_parts(
LocalRuntimeScheduler::CurrentThread(scheduler),
handle,
blocking_pool,
))
}

fn build_current_thread_runtime_components(
&mut self,
local_tid: Option<ThreadId>,
) -> io::Result<(CurrentThread, Handle, BlockingPool)> {
use crate::runtime::scheduler;
use crate::runtime::Config;

let (driver, driver_handle) = driver::Driver::new(self.get_cfg(1))?;

Expand Down Expand Up @@ -1227,17 +1293,14 @@ impl Builder {
seed_generator: seed_generator_1,
metrics_poll_count_histogram: self.metrics_poll_count_histogram_builder(),
},
local_tid,
);

let handle = Handle {
inner: scheduler::Handle::CurrentThread(handle),
};

Ok(Runtime::from_parts(
Scheduler::CurrentThread(scheduler),
handle,
blocking_pool,
))
Ok((scheduler, handle, blocking_pool))
}

fn metrics_poll_count_histogram_builder(&self) -> Option<HistogramBuilder> {
Expand Down
29 changes: 27 additions & 2 deletions tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ impl Handle {
/// # Panics
///
/// This function panics if the provided future panics, if called within an
/// asynchronous execution context, or if a timer future is executed on a
/// runtime that has been shut down.
/// asynchronous execution context, or if a timer future is executed on a runtime that has been
/// shut down.
Noah-Kennedy marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
Expand Down Expand Up @@ -348,6 +348,31 @@ impl Handle {
self.inner.spawn(future, id)
}

#[track_caller]
#[allow(dead_code)]
pub(crate) unsafe fn spawn_local_named<F>(
&self,
future: F,
_meta: SpawnMeta<'_>,
) -> JoinHandle<F::Output>
where
F: Future + 'static,
F::Output: 'static,
{
let id = crate::runtime::task::Id::next();
#[cfg(all(
tokio_unstable,
tokio_taskdump,
feature = "rt",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
))]
let future = super::task::trace::Trace::root(future);
#[cfg(all(tokio_unstable, feature = "tracing"))]
let future = crate::util::trace::task(future, "task", _meta, id.as_u64());
self.inner.spawn_local(future, id)
}

/// Returns the flavor of the current `Runtime`.
///
/// # Examples
Expand Down
7 changes: 7 additions & 0 deletions tokio/src/runtime/local_runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod runtime;

mod options;

pub use options::LocalOptions;
pub use runtime::LocalRuntime;
pub(super) use runtime::LocalRuntimeScheduler;
12 changes: 12 additions & 0 deletions tokio/src/runtime/local_runtime/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::marker::PhantomData;

/// `LocalRuntime`-only config options
///
/// Currently, there are no such options, but in the future, things like `!Send + !Sync` hooks may
/// be added.
#[derive(Default, Debug)]
#[non_exhaustive]
pub struct LocalOptions {
/// Marker used to make this !Send and !Sync.
_phantom: PhantomData<*mut u8>,
}
Loading
Loading