Skip to content

Commit

Permalink
fix feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Oct 30, 2024
1 parent 3c899be commit 7f00dd4
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion monoio/src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Inner {
}
}

#[allow(unused)]
#[cfg(all(target_os = "linux", feature = "iouring"))]
#[inline]
fn drop_op<T: 'static>(&self, index: usize, data: &mut Option<T>, skip_cancel: bool) {
match self {
Expand Down
6 changes: 6 additions & 0 deletions monoio/src/driver/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ impl MaybeFd {
fd: 0,
}
}

#[inline]
pub(crate) fn fd(&self) -> u32 {
self.fd
}
}

impl Drop for MaybeFd {
Expand Down Expand Up @@ -237,6 +242,7 @@ where
}
}

#[cfg(all(target_os = "linux", feature = "iouring"))]
impl<T: OpAble> Drop for Op<T> {
#[inline]
fn drop(&mut self) {
Expand Down
6 changes: 3 additions & 3 deletions monoio/src/driver/op/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ impl OpAble for Accept {
))]
return {
let stream_fd = syscall_u32!(accept@FD(fd, addr, len))?;
let fd = stream_fd.get() as i32;
syscall_u32!(fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC))?;
syscall_u32!(fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK))?;
let fd = stream_fd.fd() as i32;
syscall_u32!(fcntl@RAW(fd, libc::F_SETFD, libc::FD_CLOEXEC))?;
syscall_u32!(fcntl@RAW(fd, libc::F_SETFL, libc::O_NONBLOCK))?;
Ok(stream_fd)
};
}
Expand Down
4 changes: 3 additions & 1 deletion monoio/src/driver/op/fsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ impl OpAble for Fsync {
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
use std::os::windows::prelude::AsRawHandle;

use windows_sys::Win32::Storage::FileSystem::FlushFileBuffers;

crate::syscall!(
windows_sys::Win32::Storage::FileSystem::FlushFileBuffers@NON_FD(self.fd.as_raw_handle() as _),
FlushFileBuffers@NON_FD(self.fd.as_raw_handle() as _),
PartialEq::eq,
0
)
Expand Down
17 changes: 11 additions & 6 deletions monoio/src/driver/op/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub(crate) mod impls {
use super::*;

/// A wrapper of [`windows_sys::Win32::Storage::FileSystem::ReadFile`]
pub(crate) fn read(handle: isize, buf: *mut u8, len: usize) -> io::Result<u32> {
pub(crate) fn read(handle: isize, buf: *mut u8, len: usize) -> io::Result<MaybeFd> {
let mut bytes_read = 0;
let ret = unsafe {
ReadFile(
Expand All @@ -373,18 +373,23 @@ pub(crate) mod impls {
};

if ret == TRUE {
return Ok(bytes_read);
return Ok(MaybeFd::new_non_fd(bytes_read));
}

match unsafe { GetLastError() } {
ERROR_HANDLE_EOF => Ok(bytes_read),
ERROR_HANDLE_EOF => Ok(MaybeFd::new_non_fd(bytes_read)),
error => Err(io::Error::from_raw_os_error(error as _)),
}
}

/// A wrapper of [`windows_sys::Win32::Storage::FileSystem::ReadFile`] and using the
/// [`windows_sys::Win32::System::IO::OVERLAPPED`] to read at specific position.
pub(crate) fn read_at(handle: isize, buf: *mut u8, len: usize, offset: u64) -> io::Result<u32> {
pub(crate) fn read_at(
handle: isize,
buf: *mut u8,
len: usize,
offset: u64,
) -> io::Result<MaybeFd> {
let mut bytes_read = 0;
let ret = unsafe {
// see https://learn.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-readfile
Expand All @@ -402,11 +407,11 @@ pub(crate) mod impls {
};

if ret == TRUE {
return Ok(bytes_read);
return Ok(MaybeFd::new_non_fd(bytes_read));
}

match unsafe { GetLastError() } {
ERROR_HANDLE_EOF => Ok(bytes_read),
ERROR_HANDLE_EOF => Ok(MaybeFd::new_non_fd(bytes_read)),
error => Err(io::Error::from_raw_os_error(error as _)),
}
}
Expand Down
6 changes: 3 additions & 3 deletions monoio/src/driver/op/recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ impl<T: IoBufMut> OpAble for Recv<T> {
#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_socket();
MaybeFd::new_non_fd_result(crate::syscall!(
recv(
crate::syscall!(
recv@NON_FD(
fd as _,
self.buf.write_ptr(),
self.buf.bytes_total().min(i32::MAX as usize) as _,
0
),
PartialOrd::lt,
0
))
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions monoio/src/driver/op/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<T: IoBuf> OpAble for Send<T> {
}

#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
fn legacy_call(&mut self) -> io::Result<u32> {
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_socket();
syscall!(
send@NON_FD(fd as _, self.buf.read_ptr(), self.buf.bytes_init() as _, 0),
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<T: IoBuf> OpAble for SendMsg<T> {
}

#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
fn legacy_call(&mut self) -> io::Result<u32> {
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_socket();
let mut nsent = 0;
let ret = unsafe {
Expand All @@ -235,7 +235,7 @@ impl<T: IoBuf> OpAble for SendMsg<T> {
if ret == SOCKET_ERROR {
Err(io::Error::last_os_error())
} else {
Ok(nsent)
Ok(MaybeFd::new_non_fd(nsent))
}
}
}
Expand Down
27 changes: 18 additions & 9 deletions monoio/src/driver/op/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,28 @@ pub(crate) mod impls {
use super::*;

/// A wrapper of [`windows_sys::Win32::Storage::FileSystem::WriteFile`]
pub(crate) fn write(fd: isize, buf: *const u8, len: usize) -> io::Result<u32> {
pub(crate) fn write(fd: isize, buf: *const u8, len: usize) -> io::Result<MaybeFd> {
let mut bytes_write = 0;

let ret = unsafe { WriteFile(fd, buf, len as _, &mut bytes_write, std::ptr::null_mut()) };
if ret == TRUE {
return Ok(bytes_write);
return Ok(MaybeFd::new_non_fd(bytes_write));
}

match unsafe { GetLastError() } {
ERROR_HANDLE_EOF => Ok(bytes_write),
ERROR_HANDLE_EOF => Ok(MaybeFd::new_non_fd(bytes_write)),
error => Err(io::Error::from_raw_os_error(error as _)),
}
}

/// A wrapper of [`windows_sys::Win32::Storage::FileSystem::WriteFile`],
/// using [`windows_sys::Win32::System::IO::OVERLAPPED`] to write at specific offset.
pub(crate) fn write_at(fd: isize, buf: *const u8, len: usize, offset: u64) -> io::Result<u32> {
pub(crate) fn write_at(
fd: isize,
buf: *const u8,
len: usize,
offset: u64,
) -> io::Result<MaybeFd> {
let mut bytes_write = 0;

let mut overlapped: OVERLAPPED = unsafe { std::mem::zeroed() };
Expand All @@ -340,18 +345,22 @@ pub(crate) mod impls {
};

if ret == TRUE {
return Ok(bytes_write);
return Ok(MaybeFd::new_non_fd(bytes_write));
}

match unsafe { GetLastError() } {
ERROR_HANDLE_EOF => Ok(bytes_write),
ERROR_HANDLE_EOF => Ok(MaybeFd::new_non_fd(bytes_write)),
error => Err(io::Error::from_raw_os_error(error as _)),
}
}

/// There is no `writev` like syscall of file on windows, but this will be used to send socket
/// message.
pub(crate) fn write_vectored(fd: usize, buf_vec: *const WSABUF, len: usize) -> io::Result<u32> {
pub(crate) fn write_vectored(
fd: usize,
buf_vec: *const WSABUF,
len: usize,
) -> io::Result<MaybeFd> {
use windows_sys::Win32::Networking::WinSock::{WSAGetLastError, WSASend, WSAESHUTDOWN};

let mut bytes_sent = 0;
Expand All @@ -369,11 +378,11 @@ pub(crate) mod impls {
};

match ret {
0 => Ok(bytes_sent),
0 => Ok(MaybeFd::new_non_fd(bytes_sent)),
_ => {
let error = unsafe { WSAGetLastError() };
if error == WSAESHUTDOWN {
Ok(0)
Ok(MaybeFd::zero())
} else {
Err(io::Error::from_raw_os_error(error))
}
Expand Down
4 changes: 2 additions & 2 deletions monoio/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ macro_rules! asyncify_op {

let res = $crate::fs::asyncify(move || $op(fd, buf_ptr as *mut _, len, $($extra_param)?))
.await
.map(|n| n as usize);
.map(|n| n.into_inner() as usize);

unsafe { buf.set_init(*res.as_ref().unwrap_or(&0)) };

Expand All @@ -150,7 +150,7 @@ macro_rules! asyncify_op {

let res = $crate::fs::asyncify(move || $op(fd, buf_ptr as *mut _, len, $($extra_param)?))
.await
.map(|n| n as usize);
.map(|n| n.into_inner() as usize);

// unsafe { buf.set_init(*res.as_ref().unwrap_or(&0)) };

Expand Down

0 comments on commit 7f00dd4

Please sign in to comment.