mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-25 12:00:35 +00:00
runtime: various minor LocalRuntime
improvements (#7346)
This commit is contained in:
parent
327bec2caf
commit
17d8c2b29d
@ -895,34 +895,37 @@ impl Builder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates the configured `LocalRuntime`.
|
||||
/// Creates the configured [`LocalRuntime`].
|
||||
///
|
||||
/// The returned `LocalRuntime` instance is ready to spawn tasks.
|
||||
/// 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]
|
||||
/// This will panic if the runtime is configured with [`new_multi_thread()`].
|
||||
///
|
||||
/// [`new_multi_thread()`]: Builder::new_multi_thread
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tokio::runtime::Builder;
|
||||
/// use tokio::runtime::{Builder, LocalOptions};
|
||||
///
|
||||
/// let rt = Builder::new_current_thread().build_local(&mut Default::default()).unwrap();
|
||||
/// let rt = Builder::new_current_thread()
|
||||
/// .build_local(LocalOptions::default())
|
||||
/// .unwrap();
|
||||
///
|
||||
/// rt.block_on(async {
|
||||
/// rt.spawn_local(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> {
|
||||
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"),
|
||||
#[cfg(feature = "rt-multi-thread")]
|
||||
Kind::MultiThread => panic!("multi_thread is not supported for LocalRuntime"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,15 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// `LocalRuntime`-only config options
|
||||
/// [`LocalRuntime`]-only config options
|
||||
///
|
||||
/// Currently, there are no such options, but in the future, things like `!Send + !Sync` hooks may
|
||||
/// be added.
|
||||
///
|
||||
/// Use `LocalOptions::default()` to create the default set of options. This type is used with
|
||||
/// [`Builder::build_local`].
|
||||
///
|
||||
/// [`Builder::build_local`]: crate::runtime::Builder::build_local
|
||||
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
|
||||
#[derive(Default, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub struct LocalOptions {
|
||||
|
@ -92,7 +92,7 @@ impl LocalRuntime {
|
||||
pub fn new() -> std::io::Result<LocalRuntime> {
|
||||
Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build_local(&Default::default())
|
||||
.build_local(Default::default())
|
||||
}
|
||||
|
||||
/// Returns a handle to the runtime's spawner.
|
||||
|
@ -760,3 +760,11 @@ mod unix_asyncfd {
|
||||
async_assert_fn!(AsyncFd<ImplsFd<NN>>::writable(_): !Send & !Sync & !Unpin);
|
||||
async_assert_fn!(AsyncFd<ImplsFd<NN>>::writable_mut(_): !Send & !Sync & !Unpin);
|
||||
}
|
||||
|
||||
#[cfg(tokio_unstable)]
|
||||
mod unstable {
|
||||
use super::*;
|
||||
|
||||
assert_value!(tokio::runtime::LocalRuntime: !Send & !Sync & Unpin);
|
||||
assert_value!(tokio::runtime::LocalOptions: !Send & !Sync & Unpin);
|
||||
}
|
||||
|
@ -73,7 +73,27 @@ fn test_spawn_local_from_guard() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg_attr(target_family = "wasm", ignore)] // threads not supported
|
||||
fn test_spawn_from_guard_other_thread() {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let rt = rt();
|
||||
let handle = rt.handle().clone();
|
||||
|
||||
tx.send(handle).unwrap();
|
||||
});
|
||||
|
||||
let handle = rx.recv().unwrap();
|
||||
|
||||
let _guard = handle.enter();
|
||||
|
||||
tokio::spawn(async {});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "Local tasks can only be spawned on a LocalRuntime from the thread the runtime was created on"]
|
||||
#[cfg_attr(target_family = "wasm", ignore)] // threads not supported
|
||||
fn test_spawn_local_from_guard_other_thread() {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
|
||||
@ -94,6 +114,6 @@ fn test_spawn_local_from_guard_other_thread() {
|
||||
fn rt() -> tokio::runtime::LocalRuntime {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build_local(&LocalOptions::default())
|
||||
.build_local(LocalOptions::default())
|
||||
.unwrap()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user