diff --git a/lib/wasix/src/lib.rs b/lib/wasix/src/lib.rs index e039ea7b0bd..668f828b064 100644 --- a/lib/wasix/src/lib.rs +++ b/lib/wasix/src/lib.rs @@ -786,3 +786,18 @@ fn mem_error_to_wasi(err: MemoryAccessError) -> Errno { _ => Errno::Unknown, } } + +/// Run a synchronous function that would normally be blocking. +/// +/// When the `sys-thread` feature is enabled, this will call +/// [`tokio::task::block_in_place()`]. Otherwise, it calls the function +/// immediately. +pub(crate) fn block_in_place(thunk: impl FnOnce() -> Ret) -> Ret { + cfg_if::cfg_if! { + if #[cfg(feature = "sys-thread")] { + tokio::task::block_in_place(thunk) + } else { + thunk() + } + } +} diff --git a/lib/wasix/src/runtime/package_loader/builtin_loader.rs b/lib/wasix/src/runtime/package_loader/builtin_loader.rs index 34ffd58e210..92b2c6ca449 100644 --- a/lib/wasix/src/runtime/package_loader/builtin_loader.rs +++ b/lib/wasix/src/runtime/package_loader/builtin_loader.rs @@ -271,10 +271,7 @@ impl FileSystemCache { async fn lookup(&self, hash: &WebcHash) -> Result, Error> { let path = self.path(hash); - #[cfg(target_arch = "wasm32")] - let container = Container::from_disk(&path); - #[cfg(not(target_arch = "wasm32"))] - let container = tokio::task::block_in_place(|| Container::from_disk(&path)); + let container = crate::block_in_place(|| Container::from_disk(&path)); match container { Ok(c) => Ok(Some(c)), Err(ContainerError::Open { error, .. }) diff --git a/lib/wasix/src/runtime/resolver/filesystem_source.rs b/lib/wasix/src/runtime/resolver/filesystem_source.rs index c6c0f44d522..c20ceff665c 100644 --- a/lib/wasix/src/runtime/resolver/filesystem_source.rs +++ b/lib/wasix/src/runtime/resolver/filesystem_source.rs @@ -23,17 +23,9 @@ impl Source for FileSystemSource { _ => return Err(QueryError::Unsupported), }; - #[cfg(target_arch = "wasm32")] - let webc_sha256 = WebcHash::for_file(&path) + let webc_sha256 = crate::block_in_place(|| WebcHash::for_file(&path)) .with_context(|| format!("Unable to hash \"{}\"", path.display()))?; - #[cfg(not(target_arch = "wasm32"))] - let webc_sha256 = tokio::task::block_in_place(|| WebcHash::for_file(&path)) - .with_context(|| format!("Unable to hash \"{}\"", path.display()))?; - #[cfg(target_arch = "wasm32")] - let container = Container::from_disk(&path) - .with_context(|| format!("Unable to parse \"{}\"", path.display()))?; - #[cfg(not(target_arch = "wasm32"))] - let container = tokio::task::block_in_place(|| Container::from_disk(&path)) + let container = crate::block_in_place(|| Container::from_disk(&path)) .with_context(|| format!("Unable to parse \"{}\"", path.display()))?; let url = crate::runtime::resolver::utils::url_from_file_path(&path) diff --git a/lib/wasix/src/runtime/resolver/web_source.rs b/lib/wasix/src/runtime/resolver/web_source.rs index dbac364402e..e20f01db7b7 100644 --- a/lib/wasix/src/runtime/resolver/web_source.rs +++ b/lib/wasix/src/runtime/resolver/web_source.rs @@ -240,20 +240,12 @@ impl Source for WebSource { .await .context("Unable to get the locally cached file")?; - #[cfg(target_arch = "wasm32")] - let webc_sha256 = WebcHash::for_file(&local_path) - .with_context(|| format!("Unable to hash \"{}\"", local_path.display()))?; - #[cfg(not(target_arch = "wasm32"))] - let webc_sha256 = tokio::task::block_in_place(|| WebcHash::for_file(&local_path)) + let webc_sha256 = crate::block_in_place(|| WebcHash::for_file(&local_path)) .with_context(|| format!("Unable to hash \"{}\"", local_path.display()))?; // Note: We want to use Container::from_disk() rather than the bytes // our HTTP client gave us because then we can use memory-mapped files - #[cfg(target_arch = "wasm32")] - let container = Container::from_disk(&local_path) - .with_context(|| format!("Unable to load \"{}\"", local_path.display()))?; - #[cfg(not(target_arch = "wasm32"))] - let container = tokio::task::block_in_place(|| Container::from_disk(&local_path)) + let container = crate::block_in_place(|| Container::from_disk(&local_path)) .with_context(|| format!("Unable to load \"{}\"", local_path.display()))?; let pkg = PackageInfo::from_manifest(container.manifest()) .context("Unable to determine the package's metadata")?; diff --git a/lib/wasix/src/state/builder.rs b/lib/wasix/src/state/builder.rs index ef241e4da8b..3b382536f4c 100644 --- a/lib/wasix/src/state/builder.rs +++ b/lib/wasix/src/state/builder.rs @@ -13,8 +13,6 @@ use wasmer::{AsStoreMut, Instance, Module, Store}; #[cfg(feature = "journal")] use crate::journal::{DynJournal, SnapshotTrigger}; -#[cfg(feature = "sys")] -use crate::PluggableRuntime; use crate::{ bin_factory::{BinFactory, BinaryPackage}, capabilities::Capabilities, @@ -800,7 +798,7 @@ impl WasiEnvBuilder { #[cfg(feature = "sys-thread")] { #[allow(unused_mut)] - let mut runtime = PluggableRuntime::new(Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::default())); + let mut runtime = crate::runtime::PluggableRuntime::new(Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::default())); #[cfg(feature = "journal")] for journal in self.journals.clone() { runtime.add_journal(journal);