Skip to content

Commit

Permalink
Add experimental async-io windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Dec 6, 2023
1 parent 09a8786 commit 088e32c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ default-members = [
"mfio-netfs",
]

[patch.crates-io]
async-io = { git = "https://github.com/smol-rs/async-io", branch = "notgull/waitable" }
2 changes: 1 addition & 1 deletion mfio-rt/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
2 changes: 2 additions & 0 deletions mfio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 19 additions & 3 deletions mfio/src/backend/integrations/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -32,7 +39,7 @@ enum AsyncIoState<'a, B: IoBackend + ?Sized + 'a, Func, F> {
Initial(Func),
Loaded(
WithBackend<'a, B::Backend, F>,
Option<(Async<BorrowedFd<'a>>, &'a PollingFlags, Waker)>,
Option<(Async<BorrowedHandle<'a>>, &'a PollingFlags, Waker)>,
),
Finished,
}
Expand Down Expand Up @@ -82,10 +89,13 @@ impl<'a, B: LinksIoBackend + 'a, Func: BorrowingFn<B::Link>> 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,
)
Expand All @@ -99,6 +109,7 @@ impl<'a, B: LinksIoBackend + 'a, Func: BorrowingFn<B::Link>> 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?
Expand All @@ -113,6 +124,11 @@ impl<'a, B: LinksIoBackend + 'a, Func: BorrowingFn<B::Link>> Future
break ret;
}
}

#[cfg(windows)]
if let Some((fd, _, _)) = fd {
let _ = core::task::ready!(fd.poll_ready(cx));
}
};
}
AsyncIoState::Finished => unreachable!(),
Expand Down
2 changes: 1 addition & 1 deletion mfio/src/backend/integrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down

0 comments on commit 088e32c

Please sign in to comment.