From 088e32c107bb4e928fe9c048e87bd80a5148f2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aurimas=20Bla=C5=BEulionis?= <0x60@pm.me> Date: Wed, 6 Dec 2023 02:02:28 +0200 Subject: [PATCH] Add experimental async-io windows support --- Cargo.toml | 2 ++ mfio-rt/src/native/mod.rs | 2 +- mfio/Cargo.toml | 2 ++ mfio/src/backend/integrations/async_io.rs | 22 +++++++++++++++++++--- mfio/src/backend/integrations/mod.rs | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8209d33..85ea524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,5 @@ default-members = [ "mfio-netfs", ] +[patch.crates-io] +async-io = { git = "https://github.com/smol-rs/async-io", branch = "notgull/waitable" } diff --git a/mfio-rt/src/native/mod.rs b/mfio-rt/src/native/mod.rs index a164bf8..20593d6 100644 --- a/mfio-rt/src/native/mod.rs +++ b/mfio-rt/src/native/mod.rs @@ -1166,7 +1166,7 @@ mod suite_tests { }); // Test with different async runtimes - #[cfg(all(unix, not(miri)))] + #[cfg(all(any(unix, windows), not(miri)))] mod smol { use super::*; diff --git a/mfio/Cargo.toml b/mfio/Cargo.toml index 04e9bc4..0413617 100644 --- a/mfio/Cargo.toml +++ b/mfio/Cargo.toml @@ -34,6 +34,8 @@ spin = "0.9" [target.'cfg(unix)'.dependencies] nix = { version = "0.26", features = ["poll"] } + +[target.'cfg(any(unix, windows))'.dependencies] async-io = { version = "2", optional = true } [target.'cfg(all(unix, not(miri)))'.dependencies] diff --git a/mfio/src/backend/integrations/async_io.rs b/mfio/src/backend/integrations/async_io.rs index 1bc40f1..3c3d3e2 100644 --- a/mfio/src/backend/integrations/async_io.rs +++ b/mfio/src/backend/integrations/async_io.rs @@ -4,8 +4,15 @@ //! [limitation](https://github.com/smol-rs/async-io/issues/132) that was only resolved in version //! 2. +#[cfg(windows)] +use async_io::os::windows::Waitable as Async; +#[cfg(unix)] use async_io::Async; -use std::os::fd::BorrowedFd; + +#[cfg(unix)] +type BorrowedHandle<'a> = std::os::fd::BorrowedFd<'a>; +#[cfg(windows)] +type BorrowedHandle<'a> = std::os::windows::io::BorrowedHandle<'a>; use super::super::*; use super::{BorrowingFn, Integration}; @@ -32,7 +39,7 @@ enum AsyncIoState<'a, B: IoBackend + ?Sized + 'a, Func, F> { Initial(Func), Loaded( WithBackend<'a, B::Backend, F>, - Option<(Async>, &'a PollingFlags, Waker)>, + Option<(Async>, &'a PollingFlags, Waker)>, ), Finished, } @@ -82,10 +89,13 @@ impl<'a, B: LinksIoBackend + 'a, Func: BorrowingFn> Future waker, .. }| { - let handle = unsafe { BorrowedFd::borrow_raw(handle) }; + let handle = unsafe { BorrowedHandle::borrow_raw(handle) }; ( + #[cfg(unix)] Async::new_nonblocking(handle) .expect("Could not register the IO resource"), + #[cfg(windows)] + Async::new(handle).expect("Could not register the IO resource"), cur_flags, waker, ) @@ -99,6 +109,7 @@ impl<'a, B: LinksIoBackend + 'a, Func: BorrowingFn> Future break Poll::Ready(v); } + #[cfg(unix)] if let Some((fd, p, _)) = fd { let (read, write) = p.get(); // TODO: what to do when read = write = false? @@ -113,6 +124,11 @@ impl<'a, B: LinksIoBackend + 'a, Func: BorrowingFn> Future break ret; } } + + #[cfg(windows)] + if let Some((fd, _, _)) = fd { + let _ = core::task::ready!(fd.poll_ready(cx)); + } }; } AsyncIoState::Finished => unreachable!(), diff --git a/mfio/src/backend/integrations/mod.rs b/mfio/src/backend/integrations/mod.rs index 044ac21..8f8016e 100644 --- a/mfio/src/backend/integrations/mod.rs +++ b/mfio/src/backend/integrations/mod.rs @@ -6,7 +6,7 @@ //! require unix platforms, since async equivalents for windows raw handle polling is not exposed //! at the moment. -#[cfg(all(unix, feature = "async-io"))] +#[cfg(all(any(unix, windows), feature = "async-io"))] pub mod async_io; pub mod null; #[cfg(all(unix, not(miri), feature = "tokio"))]