From 5ed4fd3a47366c01e599a57acba99601e2035d7f Mon Sep 17 00:00:00 2001 From: ihciah Date: Wed, 30 Oct 2024 14:03:33 +0000 Subject: [PATCH] fix feature --- monoio/src/driver/mod.rs | 2 +- monoio/src/driver/op.rs | 6 ++++++ monoio/src/driver/op/accept.rs | 6 +++--- monoio/src/driver/op/fsync.rs | 4 +++- monoio/src/driver/op/read.rs | 17 +++++++++++------ monoio/src/driver/op/recv.rs | 2 +- monoio/src/driver/op/send.rs | 6 +++--- monoio/src/driver/op/write.rs | 27 ++++++++++++++++++--------- monoio/src/fs/mod.rs | 4 ++-- 9 files changed, 48 insertions(+), 26 deletions(-) diff --git a/monoio/src/driver/mod.rs b/monoio/src/driver/mod.rs index 820e685c..41335f88 100644 --- a/monoio/src/driver/mod.rs +++ b/monoio/src/driver/mod.rs @@ -156,7 +156,7 @@ impl Inner { } } - #[allow(unused)] + #[cfg(all(target_os = "linux", feature = "iouring"))] #[inline] fn drop_op(&self, index: usize, data: &mut Option, skip_cancel: bool) { match self { diff --git a/monoio/src/driver/op.rs b/monoio/src/driver/op.rs index 163d6ed7..661fafb4 100644 --- a/monoio/src/driver/op.rs +++ b/monoio/src/driver/op.rs @@ -109,6 +109,11 @@ impl MaybeFd { fd: 0, } } + + #[inline] + pub(crate) fn fd(&self) -> u32 { + self.fd + } } impl Drop for MaybeFd { @@ -237,6 +242,7 @@ where } } +#[cfg(all(target_os = "linux", feature = "iouring"))] impl Drop for Op { #[inline] fn drop(&mut self) { diff --git a/monoio/src/driver/op/accept.rs b/monoio/src/driver/op/accept.rs index bec6fb42..89672a42 100644 --- a/monoio/src/driver/op/accept.rs +++ b/monoio/src/driver/op/accept.rs @@ -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) }; } diff --git a/monoio/src/driver/op/fsync.rs b/monoio/src/driver/op/fsync.rs index 799a7521..7bc4f729 100644 --- a/monoio/src/driver/op/fsync.rs +++ b/monoio/src/driver/op/fsync.rs @@ -52,8 +52,10 @@ impl OpAble for Fsync { fn legacy_call(&mut self) -> io::Result { 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 ) diff --git a/monoio/src/driver/op/read.rs b/monoio/src/driver/op/read.rs index 0dede746..2062edb0 100644 --- a/monoio/src/driver/op/read.rs +++ b/monoio/src/driver/op/read.rs @@ -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 { + pub(crate) fn read(handle: isize, buf: *mut u8, len: usize) -> io::Result { let mut bytes_read = 0; let ret = unsafe { ReadFile( @@ -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 { + pub(crate) fn read_at( + handle: isize, + buf: *mut u8, + len: usize, + offset: u64, + ) -> io::Result { let mut bytes_read = 0; let ret = unsafe { // see https://learn.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-readfile @@ -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 _)), } } diff --git a/monoio/src/driver/op/recv.rs b/monoio/src/driver/op/recv.rs index 6972832a..7efe8d58 100644 --- a/monoio/src/driver/op/recv.rs +++ b/monoio/src/driver/op/recv.rs @@ -110,7 +110,7 @@ impl OpAble for Recv { fn legacy_call(&mut self) -> io::Result { let fd = self.fd.as_raw_socket(); MaybeFd::new_non_fd_result(crate::syscall!( - recv( + recv@NON_FD( fd as _, self.buf.write_ptr(), self.buf.bytes_total().min(i32::MAX as usize) as _, diff --git a/monoio/src/driver/op/send.rs b/monoio/src/driver/op/send.rs index f4431b0d..d0da1db9 100644 --- a/monoio/src/driver/op/send.rs +++ b/monoio/src/driver/op/send.rs @@ -113,7 +113,7 @@ impl OpAble for Send { } #[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))] - fn legacy_call(&mut self) -> io::Result { + fn legacy_call(&mut self) -> io::Result { let fd = self.fd.as_raw_socket(); syscall!( send@NON_FD(fd as _, self.buf.read_ptr(), self.buf.bytes_init() as _, 0), @@ -219,7 +219,7 @@ impl OpAble for SendMsg { } #[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))] - fn legacy_call(&mut self) -> io::Result { + fn legacy_call(&mut self) -> io::Result { let fd = self.fd.as_raw_socket(); let mut nsent = 0; let ret = unsafe { @@ -235,7 +235,7 @@ impl OpAble for SendMsg { if ret == SOCKET_ERROR { Err(io::Error::last_os_error()) } else { - Ok(nsent) + Ok(MaybeFd::new_non_fd(nsent)) } } } diff --git a/monoio/src/driver/op/write.rs b/monoio/src/driver/op/write.rs index c88234f0..c3d973aa 100644 --- a/monoio/src/driver/op/write.rs +++ b/monoio/src/driver/op/write.rs @@ -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 { + pub(crate) fn write(fd: isize, buf: *const u8, len: usize) -> io::Result { 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 { + pub(crate) fn write_at( + fd: isize, + buf: *const u8, + len: usize, + offset: u64, + ) -> io::Result { let mut bytes_write = 0; let mut overlapped: OVERLAPPED = unsafe { std::mem::zeroed() }; @@ -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 { + pub(crate) fn write_vectored( + fd: usize, + buf_vec: *const WSABUF, + len: usize, + ) -> io::Result { use windows_sys::Win32::Networking::WinSock::{WSAGetLastError, WSASend, WSAESHUTDOWN}; let mut bytes_sent = 0; @@ -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)) } diff --git a/monoio/src/fs/mod.rs b/monoio/src/fs/mod.rs index 7d5fd621..3664a7db 100644 --- a/monoio/src/fs/mod.rs +++ b/monoio/src/fs/mod.rs @@ -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)) }; @@ -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)) };