Skip to content

Commit b914827

Browse files
authored
feat: implement poll op on windows (#198)
1 parent cf719a1 commit b914827

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

README-zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Monoio 就是这样一个 Runtime:它并不像 Tokio 那样通过公平调度
3535

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

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

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

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ To force using nightly, create a file named `rust-toolchain` and write `nightly`
3232

3333
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)).
3434

35-
🚧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)).
35+
🚧Experimental windows support is on the way.
3636

3737
Here is a basic example of how to use Monoio.
3838

monoio/src/driver/op/poll.rs

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
use std::io;
2+
#[cfg(windows)]
3+
use std::{
4+
io::{Error, ErrorKind},
5+
os::windows::prelude::AsRawSocket,
6+
};
27

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

615
use super::{super::shared_fd::SharedFd, Op, OpAble};
7-
#[cfg(all(unix, feature = "legacy"))]
16+
#[cfg(feature = "legacy")]
817
use crate::driver::legacy::ready::Direction;
918

1019
pub(crate) struct PollAdd {
@@ -14,7 +23,7 @@ pub(crate) struct PollAdd {
1423
fd: SharedFd,
1524
// true: read; false: write
1625
is_read: bool,
17-
#[cfg(all(unix, feature = "legacy"))]
26+
#[cfg(feature = "legacy")]
1827
relaxed: bool,
1928
}
2029

@@ -23,7 +32,7 @@ impl Op<PollAdd> {
2332
Op::submit_with(PollAdd {
2433
fd: fd.clone(),
2534
is_read: true,
26-
#[cfg(all(unix, feature = "legacy"))]
35+
#[cfg(feature = "legacy")]
2736
relaxed: _relaxed,
2837
})
2938
}
@@ -32,7 +41,7 @@ impl Op<PollAdd> {
3241
Op::submit_with(PollAdd {
3342
fd: fd.clone(),
3443
is_read: false,
35-
#[cfg(all(unix, feature = "legacy"))]
44+
#[cfg(feature = "legacy")]
3645
relaxed: _relaxed,
3746
})
3847
}
@@ -57,7 +66,7 @@ impl OpAble for PollAdd {
5766
.build()
5867
}
5968

60-
#[cfg(all(unix, feature = "legacy"))]
69+
#[cfg(feature = "legacy")]
6170
fn legacy_interest(&self) -> Option<(Direction, usize)> {
6271
self.fd.registered_index().map(|idx| {
6372
(
@@ -92,4 +101,29 @@ impl OpAble for PollAdd {
92101
}
93102
Ok(0)
94103
}
104+
105+
#[cfg(windows)]
106+
fn legacy_call(&mut self) -> io::Result<u32> {
107+
if !self.relaxed {
108+
let mut pollfd = WSAPOLLFD {
109+
fd: self.fd.as_raw_socket(),
110+
events: if self.is_read {
111+
POLLIN as _
112+
} else {
113+
POLLOUT as _
114+
},
115+
revents: 0,
116+
};
117+
let ret = unsafe { WSAPoll(&mut pollfd as *mut _, 1, 0) };
118+
match ret {
119+
0 => return Err(ErrorKind::WouldBlock.into()),
120+
SOCKET_ERROR => {
121+
let error = unsafe { WSAGetLastError() };
122+
return Err(Error::from_raw_os_error(error));
123+
}
124+
_ => (),
125+
}
126+
}
127+
Ok(0)
128+
}
95129
}

0 commit comments

Comments
 (0)