From 69346c7dcb5e4065511f833adedb21ce97e617fd Mon Sep 17 00:00:00 2001 From: Mark Logan Date: Fri, 16 Sep 2022 12:13:29 -0700 Subject: [PATCH] spawn_blocking() api --- msim-tokio/src/sim/runtime.rs | 14 +++++++++++++- msim/src/sim/runtime/mod.rs | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/msim-tokio/src/sim/runtime.rs b/msim-tokio/src/sim/runtime.rs index 85a435f..5e6a253 100644 --- a/msim-tokio/src/sim/runtime.rs +++ b/msim-tokio/src/sim/runtime.rs @@ -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 { @@ -48,6 +48,18 @@ impl Handle { { ms_runtime::NodeHandle::current().spawn(future) } + + pub fn spawn_blocking(&self, f: F) -> JoinHandle + 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>); diff --git a/msim/src/sim/runtime/mod.rs b/msim/src/sim/runtime/mod.rs index eec05c8..19a7a10 100644 --- a/msim/src/sim/runtime/mod.rs +++ b/msim/src/sim/runtime/mod.rs @@ -364,6 +364,15 @@ impl NodeHandle { self.task.spawn(future) } + /// Spawn a blocking task. + pub fn spawn_blocking(&self, f: F) -> JoinHandle + 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<(), ()> {