Browse Source

better name and comment

pull/2832/head
Pat Hickey 4 years ago
parent
commit
e50f1b24a9
  1. 33
      crates/wasi-common/tokio/src/dir.rs
  2. 36
      crates/wasi-common/tokio/src/file.rs
  3. 13
      crates/wasi-common/tokio/src/lib.rs
  4. 4
      crates/wasi-common/tokio/src/sched/windows.rs

33
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<Box<dyn WasiFile>, 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<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)))
}
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<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> {
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<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(
&self,
@ -93,7 +94,9 @@ impl WasiDir for Dir {
.as_any()
.downcast_ref::<Self>()
.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::<Self>()
.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<wasi_common::SystemTimeSpec>,
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))
}
}

36
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<FileType, Error> {
asyncify(|| self.0.get_filetype())
block_on_dummy_executor(|| self.0.get_filetype())
}
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> {
asyncify(|| self.0.set_fdflags(fdflags))
block_on_dummy_executor(|| self.0.set_fdflags(fdflags))
}
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> {
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<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>(
&self,
bufs: &mut [io::IoSliceMut<'a>],
offset: u64,
) -> 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> {
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<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> {
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> {
asyncify(move || self.0.peek(buf))
block_on_dummy_executor(move || self.0.peek(buf))
}
async fn set_times(
&self,
atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>,
) -> 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> {
asyncify(|| self.0.num_ready_bytes())
block_on_dummy_executor(|| self.0.num_ready_bytes())
}
#[cfg(not(windows))]

13
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<T, Error>
// 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<T, Error>
where
F: FnOnce() -> Fut + Send + 'a,
Fut: Future<Output = Result<T, Error>>,

4
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> {

Loading…
Cancel
Save