Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 13 additions & 10 deletions tokio/src/runtime/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand Down
8 changes: 7 additions & 1 deletion tokio/src/runtime/local_runtime/options.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/local_runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tokio/tests/async_send_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
24 changes: 22 additions & 2 deletions tokio/tests/rt_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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