From e50f1b24a92dbe334945ce3ae084def84aaf2818 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 6 May 2021 11:24:48 -0700 Subject: [PATCH] better name and comment --- crates/wasi-common/tokio/src/dir.rs | 33 +++++++++-------- crates/wasi-common/tokio/src/file.rs | 36 +++++++++---------- crates/wasi-common/tokio/src/lib.rs | 13 ++++--- crates/wasi-common/tokio/src/sched/windows.rs | 4 +-- 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/crates/wasi-common/tokio/src/dir.rs b/crates/wasi-common/tokio/src/dir.rs index 5ba7d985ed..a70b44e182 100644 --- a/crates/wasi-common/tokio/src/dir.rs +++ b/crates/wasi-common/tokio/src/dir.rs @@ -1,4 +1,4 @@ -use crate::{asyncify, file::File}; +use crate::{block_on_dummy_executor, file::File}; use std::any::Any; use std::path::PathBuf; use wasi_common::{ @@ -29,7 +29,7 @@ impl WasiDir for Dir { write: bool, fdflags: FdFlags, ) -> Result, Error> { - let f = asyncify(move || async move { + let f = block_on_dummy_executor(move || async move { self.0 .open_file_(symlink_follow, path, oflags, read, write, fdflags) })?; @@ -37,12 +37,13 @@ impl WasiDir for Dir { } async fn open_dir(&self, symlink_follow: bool, path: &str) -> Result, Error> { - let d = asyncify(move || async move { self.0.open_dir_(symlink_follow, path) })?; + let d = + block_on_dummy_executor(move || async move { self.0.open_dir_(symlink_follow, path) })?; Ok(Box::new(Dir(d))) } async fn create_dir(&self, path: &str) -> Result<(), Error> { - asyncify(|| self.0.create_dir(path)) + block_on_dummy_executor(|| self.0.create_dir(path)) } async fn readdir( &self, @@ -56,32 +57,32 @@ impl WasiDir for Dir { } } - let inner = asyncify(move || self.0.readdir(cursor))?; + let inner = block_on_dummy_executor(move || self.0.readdir(cursor))?; Ok(Box::new(I(inner))) } async fn symlink(&self, src_path: &str, dest_path: &str) -> Result<(), Error> { - asyncify(move || self.0.symlink(src_path, dest_path)) + block_on_dummy_executor(move || self.0.symlink(src_path, dest_path)) } async fn remove_dir(&self, path: &str) -> Result<(), Error> { - asyncify(move || self.0.remove_dir(path)) + block_on_dummy_executor(move || self.0.remove_dir(path)) } async fn unlink_file(&self, path: &str) -> Result<(), Error> { - asyncify(move || self.0.unlink_file(path)) + block_on_dummy_executor(move || self.0.unlink_file(path)) } async fn read_link(&self, path: &str) -> Result { - asyncify(move || self.0.read_link(path)) + block_on_dummy_executor(move || self.0.read_link(path)) } async fn get_filestat(&self) -> Result { - asyncify(|| self.0.get_filestat()) + block_on_dummy_executor(|| self.0.get_filestat()) } async fn get_path_filestat( &self, path: &str, follow_symlinks: bool, ) -> Result { - asyncify(move || self.0.get_path_filestat(path, follow_symlinks)) + block_on_dummy_executor(move || self.0.get_path_filestat(path, follow_symlinks)) } async fn rename( &self, @@ -93,7 +94,9 @@ impl WasiDir for Dir { .as_any() .downcast_ref::() .ok_or(Error::badf().context("failed downcast to tokio Dir"))?; - asyncify(move || async move { self.0.rename_(src_path, &dest_dir.0, dest_path) }) + block_on_dummy_executor( + move || async move { self.0.rename_(src_path, &dest_dir.0, dest_path) }, + ) } async fn hard_link( &self, @@ -105,7 +108,9 @@ impl WasiDir for Dir { .as_any() .downcast_ref::() .ok_or(Error::badf().context("failed downcast to tokio Dir"))?; - asyncify(move || async move { self.0.hard_link_(src_path, &target_dir.0, target_path) }) + block_on_dummy_executor(move || async move { + self.0.hard_link_(src_path, &target_dir.0, target_path) + }) } async fn set_times( &self, @@ -114,7 +119,7 @@ impl WasiDir for Dir { mtime: Option, follow_symlinks: bool, ) -> Result<(), Error> { - asyncify(move || self.0.set_times(path, atime, mtime, follow_symlinks)) + block_on_dummy_executor(move || self.0.set_times(path, atime, mtime, follow_symlinks)) } } diff --git a/crates/wasi-common/tokio/src/file.rs b/crates/wasi-common/tokio/src/file.rs index 82c8d965ac..5907aa7f53 100644 --- a/crates/wasi-common/tokio/src/file.rs +++ b/crates/wasi-common/tokio/src/file.rs @@ -1,4 +1,4 @@ -use crate::asyncify; +use crate::block_on_dummy_executor; use std::any::Any; use std::io; #[cfg(windows)] @@ -45,70 +45,70 @@ macro_rules! wasi_file_impl { self } async fn datasync(&self) -> Result<(), Error> { - asyncify(|| self.0.datasync()) + block_on_dummy_executor(|| self.0.datasync()) } async fn sync(&self) -> Result<(), Error> { - asyncify(|| self.0.sync()) + block_on_dummy_executor(|| self.0.sync()) } async fn get_filetype(&self) -> Result { - asyncify(|| self.0.get_filetype()) + block_on_dummy_executor(|| self.0.get_filetype()) } async fn get_fdflags(&self) -> Result { - asyncify(|| self.0.get_fdflags()) + block_on_dummy_executor(|| self.0.get_fdflags()) } async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> { - asyncify(|| self.0.set_fdflags(fdflags)) + block_on_dummy_executor(|| self.0.set_fdflags(fdflags)) } async fn get_filestat(&self) -> Result { - asyncify(|| self.0.get_filestat()) + block_on_dummy_executor(|| self.0.get_filestat()) } async fn set_filestat_size(&self, size: u64) -> Result<(), Error> { - asyncify(move || self.0.set_filestat_size(size)) + block_on_dummy_executor(move || self.0.set_filestat_size(size)) } async fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { - asyncify(move || self.0.advise(offset, len, advice)) + block_on_dummy_executor(move || self.0.advise(offset, len, advice)) } async fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { - asyncify(move || self.0.allocate(offset, len)) + block_on_dummy_executor(move || self.0.allocate(offset, len)) } async fn read_vectored<'a>( &self, bufs: &mut [io::IoSliceMut<'a>], ) -> Result { - asyncify(move || self.0.read_vectored(bufs)) + block_on_dummy_executor(move || self.0.read_vectored(bufs)) } async fn read_vectored_at<'a>( &self, bufs: &mut [io::IoSliceMut<'a>], offset: u64, ) -> Result { - asyncify(move || self.0.read_vectored_at(bufs, offset)) + block_on_dummy_executor(move || self.0.read_vectored_at(bufs, offset)) } async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result { - asyncify(move || self.0.write_vectored(bufs)) + block_on_dummy_executor(move || self.0.write_vectored(bufs)) } async fn write_vectored_at<'a>( &self, bufs: &[io::IoSlice<'a>], offset: u64, ) -> Result { - asyncify(move || self.0.write_vectored_at(bufs, offset)) + block_on_dummy_executor(move || self.0.write_vectored_at(bufs, offset)) } async fn seek(&self, pos: std::io::SeekFrom) -> Result { - asyncify(move || self.0.seek(pos)) + block_on_dummy_executor(move || self.0.seek(pos)) } async fn peek(&self, buf: &mut [u8]) -> Result { - asyncify(move || self.0.peek(buf)) + block_on_dummy_executor(move || self.0.peek(buf)) } async fn set_times( &self, atime: Option, mtime: Option, ) -> Result<(), Error> { - asyncify(move || self.0.set_times(atime, mtime)) + block_on_dummy_executor(move || self.0.set_times(atime, mtime)) } async fn num_ready_bytes(&self) -> Result { - asyncify(|| self.0.num_ready_bytes()) + block_on_dummy_executor(|| self.0.num_ready_bytes()) } #[cfg(not(windows))] diff --git a/crates/wasi-common/tokio/src/lib.rs b/crates/wasi-common/tokio/src/lib.rs index bbe09dfe79..865b99445a 100644 --- a/crates/wasi-common/tokio/src/lib.rs +++ b/crates/wasi-common/tokio/src/lib.rs @@ -96,10 +96,15 @@ impl WasiCtxBuilder { } } -// This function takes the "async" code which is in fact blocking -// but always returns Poll::Ready, and executes it in a dummy executor -// on a blocking thread in the tokio thread pool. -pub(crate) fn asyncify<'a, F, Fut, T>(f: F) -> Result +// Much of this crate is implemented in terms of `async` methods from the +// wasi-cap-std-sync crate. These methods may be async in signature, however, +// they are synchronous in implementation (always Poll::Ready on first poll) +// and perform blocking syscalls. +// +// This function takes this blocking code and executes it using a dummy executor +// to assert its immediate readiness. We tell tokio this is a blocking operation +// with the block_in_place function. +pub(crate) fn block_on_dummy_executor<'a, F, Fut, T>(f: F) -> Result where F: FnOnce() -> Fut + Send + 'a, Fut: Future>, diff --git a/crates/wasi-common/tokio/src/sched/windows.rs b/crates/wasi-common/tokio/src/sched/windows.rs index 7228d4e395..f484056e8d 100644 --- a/crates/wasi-common/tokio/src/sched/windows.rs +++ b/crates/wasi-common/tokio/src/sched/windows.rs @@ -1,4 +1,4 @@ -use crate::asyncify; +use crate::block_on_dummy_executor; use anyhow::Context; use std::ops::Deref; use std::os::windows::io::{AsRawHandle, RawHandle}; @@ -16,7 +16,7 @@ use wasi_common::{ }; pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> { - asyncify(move || poll_oneoff_(poll)) + block_on_dummy_executor(move || poll_oneoff_(poll)) } async fn poll_oneoff_<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {