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

Add spawn_blocking() api #4

Merged
merged 1 commit into from
Sep 16, 2022
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
14 changes: 13 additions & 1 deletion msim-tokio/src/sim/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
use msim::runtime as ms_runtime;
use msim::task::JoinHandle;

use tracing::debug;
use tracing::{debug, warn};

#[derive(Clone)]
pub struct Handle {
Expand Down Expand Up @@ -48,6 +48,18 @@ impl Handle {
{
ms_runtime::NodeHandle::current().spawn(future)
}

pub fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
warn!(
"spawn_blocking() call in simulator may cause deadlocks if spawned task \
attempts to do I/O"
);
ms_runtime::NodeHandle::current().spawn_blocking(f)
}
}

pub struct EnterGuard<'a>(ms_runtime::EnterGuard, std::marker::PhantomData<&'a Handle>);
Expand Down
9 changes: 9 additions & 0 deletions msim/src/sim/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ impl NodeHandle {
self.task.spawn(future)
}

/// Spawn a blocking task.
pub fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
self.task.spawn(async move { f() })
}

/// Join the node.
/// TODO: unimplemented
pub fn join(self) -> Result<(), ()> {
Expand Down