better name and comment

This commit is contained in:
Pat Hickey
2021-05-06 11:24:48 -07:00
parent 76be1959c4
commit e50f1b24a9
4 changed files with 48 additions and 38 deletions

View File

@@ -1,4 +1,4 @@
use crate::{asyncify, file::File}; use crate::{block_on_dummy_executor, file::File};
use std::any::Any; use std::any::Any;
use std::path::PathBuf; use std::path::PathBuf;
use wasi_common::{ use wasi_common::{
@@ -29,7 +29,7 @@ impl WasiDir for Dir {
write: bool, write: bool,
fdflags: FdFlags, fdflags: FdFlags,
) -> Result<Box<dyn WasiFile>, Error> { ) -> Result<Box<dyn WasiFile>, Error> {
let f = asyncify(move || async move { let f = block_on_dummy_executor(move || async move {
self.0 self.0
.open_file_(symlink_follow, path, oflags, read, write, fdflags) .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<Box<dyn WasiDir>, Error> { async fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, 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))) Ok(Box::new(Dir(d)))
} }
async fn create_dir(&self, path: &str) -> Result<(), Error> { 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( async fn readdir(
&self, &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))) Ok(Box::new(I(inner)))
} }
async fn symlink(&self, src_path: &str, dest_path: &str) -> Result<(), Error> { 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> { 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> { 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<PathBuf, Error> { async fn read_link(&self, path: &str) -> Result<PathBuf, Error> {
asyncify(move || self.0.read_link(path)) block_on_dummy_executor(move || self.0.read_link(path))
} }
async fn get_filestat(&self) -> Result<Filestat, Error> { async fn get_filestat(&self) -> Result<Filestat, Error> {
asyncify(|| self.0.get_filestat()) block_on_dummy_executor(|| self.0.get_filestat())
} }
async fn get_path_filestat( async fn get_path_filestat(
&self, &self,
path: &str, path: &str,
follow_symlinks: bool, follow_symlinks: bool,
) -> Result<Filestat, Error> { ) -> Result<Filestat, Error> {
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( async fn rename(
&self, &self,
@@ -93,7 +94,9 @@ impl WasiDir for Dir {
.as_any() .as_any()
.downcast_ref::<Self>() .downcast_ref::<Self>()
.ok_or(Error::badf().context("failed downcast to tokio Dir"))?; .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( async fn hard_link(
&self, &self,
@@ -105,7 +108,9 @@ impl WasiDir for Dir {
.as_any() .as_any()
.downcast_ref::<Self>() .downcast_ref::<Self>()
.ok_or(Error::badf().context("failed downcast to tokio Dir"))?; .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( async fn set_times(
&self, &self,
@@ -114,7 +119,7 @@ impl WasiDir for Dir {
mtime: Option<wasi_common::SystemTimeSpec>, mtime: Option<wasi_common::SystemTimeSpec>,
follow_symlinks: bool, follow_symlinks: bool,
) -> Result<(), Error> { ) -> 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))
} }
} }

View File

@@ -1,4 +1,4 @@
use crate::asyncify; use crate::block_on_dummy_executor;
use std::any::Any; use std::any::Any;
use std::io; use std::io;
#[cfg(windows)] #[cfg(windows)]
@@ -45,70 +45,70 @@ macro_rules! wasi_file_impl {
self self
} }
async fn datasync(&self) -> Result<(), Error> { async fn datasync(&self) -> Result<(), Error> {
asyncify(|| self.0.datasync()) block_on_dummy_executor(|| self.0.datasync())
} }
async fn sync(&self) -> Result<(), Error> { async fn sync(&self) -> Result<(), Error> {
asyncify(|| self.0.sync()) block_on_dummy_executor(|| self.0.sync())
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&self) -> Result<FileType, Error> {
asyncify(|| self.0.get_filetype()) block_on_dummy_executor(|| self.0.get_filetype())
} }
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&self) -> Result<FdFlags, Error> {
asyncify(|| self.0.get_fdflags()) block_on_dummy_executor(|| self.0.get_fdflags())
} }
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> { 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<Filestat, Error> { async fn get_filestat(&self) -> Result<Filestat, Error> {
asyncify(|| self.0.get_filestat()) block_on_dummy_executor(|| self.0.get_filestat())
} }
async fn set_filestat_size(&self, size: u64) -> Result<(), Error> { 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> { 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> { 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>( async fn read_vectored<'a>(
&self, &self,
bufs: &mut [io::IoSliceMut<'a>], bufs: &mut [io::IoSliceMut<'a>],
) -> Result<u64, Error> { ) -> Result<u64, Error> {
asyncify(move || self.0.read_vectored(bufs)) block_on_dummy_executor(move || self.0.read_vectored(bufs))
} }
async fn read_vectored_at<'a>( async fn read_vectored_at<'a>(
&self, &self,
bufs: &mut [io::IoSliceMut<'a>], bufs: &mut [io::IoSliceMut<'a>],
offset: u64, offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
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<u64, Error> { async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
asyncify(move || self.0.write_vectored(bufs)) block_on_dummy_executor(move || self.0.write_vectored(bufs))
} }
async fn write_vectored_at<'a>( async fn write_vectored_at<'a>(
&self, &self,
bufs: &[io::IoSlice<'a>], bufs: &[io::IoSlice<'a>],
offset: u64, offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
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<u64, Error> { async fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
asyncify(move || self.0.seek(pos)) block_on_dummy_executor(move || self.0.seek(pos))
} }
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> { async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
asyncify(move || self.0.peek(buf)) block_on_dummy_executor(move || self.0.peek(buf))
} }
async fn set_times( async fn set_times(
&self, &self,
atime: Option<wasi_common::SystemTimeSpec>, atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>, mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> { ) -> 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<u64, Error> { async fn num_ready_bytes(&self) -> Result<u64, Error> {
asyncify(|| self.0.num_ready_bytes()) block_on_dummy_executor(|| self.0.num_ready_bytes())
} }
#[cfg(not(windows))] #[cfg(not(windows))]

View File

@@ -96,10 +96,15 @@ impl WasiCtxBuilder {
} }
} }
// This function takes the "async" code which is in fact blocking // Much of this crate is implemented in terms of `async` methods from the
// but always returns Poll::Ready, and executes it in a dummy executor // wasi-cap-std-sync crate. These methods may be async in signature, however,
// on a blocking thread in the tokio thread pool. // they are synchronous in implementation (always Poll::Ready on first poll)
pub(crate) fn asyncify<'a, F, Fut, T>(f: F) -> Result<T, Error> // 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<T, Error>
where where
F: FnOnce() -> Fut + Send + 'a, F: FnOnce() -> Fut + Send + 'a,
Fut: Future<Output = Result<T, Error>>, Fut: Future<Output = Result<T, Error>>,

View File

@@ -1,4 +1,4 @@
use crate::asyncify; use crate::block_on_dummy_executor;
use anyhow::Context; use anyhow::Context;
use std::ops::Deref; use std::ops::Deref;
use std::os::windows::io::{AsRawHandle, RawHandle}; 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> { 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> { async fn poll_oneoff_<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {