Skip to content

Commit

Permalink
feat: implement poll op on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
CarrotzRule123 committed Jul 24, 2023
1 parent cf719a1 commit 6342593
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Monoio 就是这样一个 Runtime:它并不像 Tokio 那样通过公平调度

同时,如果你想使用 io_uring,你需要确保你当前的内核版本是较新的([5.6+](docs/zh/platform-support.md));并且 memlock 是一个[合适的配置](docs/zh/memlock.md)。如果你的内核版本不满足需求,可以尝试使用 legacy driver 启动([参考这里](/docs/zh/use-legacy-driver.md)),当前支持 Linux 和 macOS。

🚧实验性的 windows 系统支持正在开发中,你需要确保你的 windows 版本支持 ([Windows Build 22000](https://docs.microsoft.com/zh-CN/windows/win32/api/ioringapi/ns-ioringapi-ioring_capabilities))
🚧实验性的 windows 系统支持正在开发中。

这是一个非常简单的例子,基于 Monoio 实现一个简单的 echo 服务。运行起来之后你可以通过 `nc 127.0.0.1 50002` 来连接它。

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To force using nightly, create a file named `rust-toolchain` and write `nightly`

Also, if you want to use io_uring, you must make sure your kernel supports it([5.6+](docs/en/platform-support.md)). And, memlock is [configured as a proper number](docs/en/memlock.md). If your kernel version does not meet the requirements, you can try to use the legacy driver to start, currently supports Linux and macOS([ref here](/docs/en/use-legacy-driver.md)).

🚧Experimental windows support is on the way, if you want to use windows you must make sure your windows supports it([Windows Build 22000](https://docs.microsoft.com/en-us/windows/win32/api/ioringapi/ns-ioringapi-ioring_capabilities)).
🚧Experimental windows support is on the way.

Here is a basic example of how to use Monoio.

Expand Down
44 changes: 39 additions & 5 deletions monoio/src/driver/op/poll.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use std::io;
#[cfg(windows)]
use std::{
io::{Error, ErrorKind},
os::windows::prelude::AsRawSocket,
};

#[cfg(all(target_os = "linux", feature = "iouring"))]
use io_uring::{opcode, types};
#[cfg(windows)]
use windows_sys::Win32::Networking::WinSock::{
WSAGetLastError, WSAPoll, POLLIN, POLLOUT, SOCKET_ERROR, WSAPOLLFD,
};

use super::{super::shared_fd::SharedFd, Op, OpAble};
#[cfg(all(unix, feature = "legacy"))]
#[cfg(feature = "legacy")]
use crate::driver::legacy::ready::Direction;

pub(crate) struct PollAdd {
Expand All @@ -14,7 +23,7 @@ pub(crate) struct PollAdd {
fd: SharedFd,
// true: read; false: write
is_read: bool,
#[cfg(all(unix, feature = "legacy"))]
#[cfg(feature = "legacy")]
relaxed: bool,
}

Expand All @@ -23,7 +32,7 @@ impl Op<PollAdd> {
Op::submit_with(PollAdd {
fd: fd.clone(),
is_read: true,
#[cfg(all(unix, feature = "legacy"))]
#[cfg(feature = "legacy")]
relaxed: _relaxed,
})
}
Expand All @@ -32,7 +41,7 @@ impl Op<PollAdd> {
Op::submit_with(PollAdd {
fd: fd.clone(),
is_read: false,
#[cfg(all(unix, feature = "legacy"))]
#[cfg(feature = "legacy")]
relaxed: _relaxed,
})
}
Expand All @@ -57,7 +66,7 @@ impl OpAble for PollAdd {
.build()
}

#[cfg(all(unix, feature = "legacy"))]
#[cfg(feature = "legacy")]
fn legacy_interest(&self) -> Option<(Direction, usize)> {
self.fd.registered_index().map(|idx| {
(
Expand Down Expand Up @@ -92,4 +101,29 @@ impl OpAble for PollAdd {
}
Ok(0)
}

#[cfg(windows)]
fn legacy_call(&mut self) -> io::Result<u32> {
if !self.relaxed {
let mut pollfd = WSAPOLLFD {
fd: self.fd.as_raw_socket(),
events: if self.is_read {
POLLIN as _
} else {
POLLOUT as _
},
revents: 0,
};
let ret = unsafe { WSAPoll(&mut pollfd as *mut _, 1, 0) };
match ret {
0 => return Err(ErrorKind::WouldBlock.into()),
SOCKET_ERROR => {
let error = unsafe { WSAGetLastError() };
return Err(Error::from_raw_os_error(error));
}
_ => (),
}
}
Ok(0)
}
}

0 comments on commit 6342593

Please sign in to comment.