From a09e2c119adf1c7d790d3dcf2ccea2923d74ba6a Mon Sep 17 00:00:00 2001 From: dragon-zhang Date: Sun, 26 Jan 2025 09:23:18 +0800 Subject: [PATCH] simplify code --- core/src/net/operator/windows/mod.rs | 10 ++- core/src/syscall/unix/accept.rs | 25 +----- core/src/syscall/unix/accept4.rs | 26 +------ core/src/syscall/unix/close.rs | 20 +---- core/src/syscall/unix/connect.rs | 25 +----- core/src/syscall/unix/fsync.rs | 19 +---- core/src/syscall/unix/link.rs | 15 +--- core/src/syscall/unix/listen.rs | 13 +--- core/src/syscall/unix/lseek.rs | 16 +--- core/src/syscall/unix/mkdir.rs | 15 +--- core/src/syscall/unix/mkdirat.rs | 23 +----- core/src/syscall/unix/mod.rs | 78 +++++++++++++++++++ core/src/syscall/unix/nanosleep.rs | 15 +--- core/src/syscall/unix/poll.rs | 17 +--- core/src/syscall/unix/pread.rs | 26 +------ core/src/syscall/unix/preadv.rs | 26 +------ .../syscall/unix/pthread_cond_timedwait.rs | 27 +++---- core/src/syscall/unix/pthread_mutex_lock.rs | 16 +--- .../src/syscall/unix/pthread_mutex_trylock.rs | 15 +--- core/src/syscall/unix/pthread_mutex_unlock.rs | 15 +--- core/src/syscall/unix/pwrite.rs | 26 +------ core/src/syscall/unix/pwritev.rs | 26 +------ core/src/syscall/unix/read.rs | 25 +----- core/src/syscall/unix/readv.rs | 25 +----- core/src/syscall/unix/recv.rs | 26 +------ core/src/syscall/unix/recvfrom.rs | 29 +++---- core/src/syscall/unix/recvmsg.rs | 25 +----- core/src/syscall/unix/renameat.rs | 24 +----- core/src/syscall/unix/renameat2.rs | 31 +++----- core/src/syscall/unix/rmdir.rs | 14 +--- core/src/syscall/unix/select.rs | 27 +++---- core/src/syscall/unix/send.rs | 26 +------ core/src/syscall/unix/sendmsg.rs | 25 +----- core/src/syscall/unix/sendto.rs | 77 ++++++++++-------- core/src/syscall/unix/setsockopt.rs | 19 +---- core/src/syscall/unix/shutdown.rs | 24 +----- core/src/syscall/unix/sleep.rs | 9 +-- core/src/syscall/unix/socket.rs | 25 +----- core/src/syscall/unix/unlink.rs | 14 +--- core/src/syscall/unix/usleep.rs | 14 +--- core/src/syscall/unix/write.rs | 25 +----- core/src/syscall/unix/writev.rs | 25 +----- core/src/syscall/windows/CreateFileW.rs | 48 +++--------- core/src/syscall/windows/SetFilePointerEx.rs | 23 +++--- core/src/syscall/windows/Sleep.rs | 8 +- core/src/syscall/windows/WSAAccept.rs | 33 +++----- core/src/syscall/windows/WSAPoll.rs | 17 +--- core/src/syscall/windows/WSARecv.rs | 48 +++--------- core/src/syscall/windows/WSASend.rs | 56 ++++--------- core/src/syscall/windows/WSASocketW.rs | 35 +++------ core/src/syscall/windows/WaitOnAddress.rs | 23 +++--- core/src/syscall/windows/accept.rs | 27 ++----- core/src/syscall/windows/connect.rs | 17 +--- core/src/syscall/windows/ioctlsocket.rs | 17 +--- core/src/syscall/windows/listen.rs | 12 +-- core/src/syscall/windows/mod.rs | 53 +++++++++++++ core/src/syscall/windows/recv.rs | 18 +---- core/src/syscall/windows/select.rs | 27 +++---- core/src/syscall/windows/send.rs | 18 +---- core/src/syscall/windows/setsockopt.rs | 19 +---- core/src/syscall/windows/shutdown.rs | 16 +--- core/src/syscall/windows/socket.rs | 16 +--- 62 files changed, 486 insertions(+), 1048 deletions(-) diff --git a/core/src/net/operator/windows/mod.rs b/core/src/net/operator/windows/mod.rs index 998cdbc1..0a28a4f5 100644 --- a/core/src/net/operator/windows/mod.rs +++ b/core/src/net/operator/windows/mod.rs @@ -14,7 +14,7 @@ use windows_sys::Win32::Networking::WinSock::{ getsockopt, setsockopt, AcceptEx, WSAGetLastError, WSARecv, WSASend, WSASocketW, INVALID_SOCKET, LPCONDITIONPROC, LPWSAOVERLAPPED_COMPLETION_ROUTINE, SEND_RECV_FLAGS, SOCKADDR, SOCKADDR_IN, SOCKET, SOCKET_ERROR, SOL_SOCKET, SO_PROTOCOL_INFO, SO_UPDATE_ACCEPT_CONTEXT, - WSABUF, WSAPROTOCOL_INFOW, WSA_FLAG_OVERLAPPED, WSA_IO_PENDING, + WSABUF, WSAEINPROGRESS, WSAENETDOWN, WSAPROTOCOL_INFOW, WSA_FLAG_OVERLAPPED, WSA_IO_PENDING, }; use windows_sys::Win32::Storage::FileSystem::SetFileCompletionNotificationModes; use windows_sys::Win32::System::WindowsProgramming::FILE_SKIP_SET_EVENT_ON_HANDLE; @@ -159,7 +159,6 @@ impl<'o> Operator<'o> { for entry in entries { let mut cqe = *Box::from_raw(entry.lpOverlapped.cast::()); // resolve completed read/write tasks - // todo refactor IOCP impl cqe.result = match cqe.syscall_name { SyscallName::accept => { if setsockopt( @@ -172,7 +171,7 @@ impl<'o> Operator<'o> { { cqe.socket.try_into().expect("result overflow") } else { - -c_longlong::from(windows_sys::Win32::Foundation::GetLastError()) + -c_longlong::from(WSAENETDOWN) } } SyscallName::recv @@ -183,7 +182,7 @@ impl<'o> Operator<'o> { if r > 0 { r } else { - -c_longlong::from(windows_sys::Win32::Foundation::GetLastError()) + -c_longlong::from(WSAEINPROGRESS) } } _ => panic!("unsupported"), @@ -273,6 +272,7 @@ impl<'o> Operator<'o> { overlapped.token = user_data; overlapped.syscall_name = syscall_name; overlapped.socket = socket; + overlapped.result = -c_longlong::from(WSAENETDOWN); let mut buf: Vec = Vec::with_capacity(size as usize * 2); while AcceptEx( fd, @@ -364,6 +364,7 @@ impl<'o> Operator<'o> { overlapped.from_fd = fd; overlapped.token = user_data; overlapped.syscall_name = syscall_name; + overlapped.result = -c_longlong::from(WSAEINPROGRESS); if WSARecv( fd, buf, @@ -457,6 +458,7 @@ impl<'o> Operator<'o> { overlapped.from_fd = fd; overlapped.token = user_data; overlapped.syscall_name = syscall_name; + overlapped.result = -c_longlong::from(WSAEINPROGRESS); if WSASend( fd, buf, diff --git a/core/src/syscall/unix/accept.rs b/core/src/syscall/unix/accept.rs index a39afead..cfbd12e0 100644 --- a/core/src/syscall/unix/accept.rs +++ b/core/src/syscall/unix/accept.rs @@ -1,27 +1,6 @@ use libc::{sockaddr, socklen_t}; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn accept( - fn_ptr: Option<&extern "C" fn(c_int, *mut sockaddr, *mut socklen_t) -> c_int>, - fd: c_int, - address: *mut sockaddr, - address_len: *mut socklen_t, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - AcceptSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.accept(fn_ptr, fd, address, address_len) -} - trait AcceptSyscall { extern "C" fn accept( &self, @@ -32,6 +11,10 @@ trait AcceptSyscall { ) -> c_int; } +impl_syscall!(AcceptSyscallFacade, IoUringAcceptSyscall, NioAcceptSyscall, RawAcceptSyscall, + accept(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int +); + impl_facade!(AcceptSyscallFacade, AcceptSyscall, accept(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int ); diff --git a/core/src/syscall/unix/accept4.rs b/core/src/syscall/unix/accept4.rs index 42e9fa68..599123ba 100644 --- a/core/src/syscall/unix/accept4.rs +++ b/core/src/syscall/unix/accept4.rs @@ -1,28 +1,6 @@ use libc::{sockaddr, socklen_t}; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn accept4( - fn_ptr: Option<&extern "C" fn(c_int, *mut sockaddr, *mut socklen_t, c_int) -> c_int>, - fd: c_int, - addr: *mut sockaddr, - len: *mut socklen_t, - flg: c_int, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - Accept4SyscallFacade>>, - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.accept4(fn_ptr, fd, addr, len, flg) -} - trait Accept4Syscall { extern "C" fn accept4( &self, @@ -34,6 +12,10 @@ trait Accept4Syscall { ) -> c_int; } +impl_syscall!(Accept4SyscallFacade, IoUringAccept4Syscall, NioAccept4Syscall, RawAccept4Syscall, + accept4(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t, flg: c_int) -> c_int +); + impl_facade!(Accept4SyscallFacade, Accept4Syscall, accept4(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t, flg: c_int) -> c_int ); diff --git a/core/src/syscall/unix/close.rs b/core/src/syscall/unix/close.rs index 251553b7..edf68f14 100644 --- a/core/src/syscall/unix/close.rs +++ b/core/src/syscall/unix/close.rs @@ -1,26 +1,14 @@ use crate::net::EventLoops; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn close(fn_ptr: Option<&extern "C" fn(c_int) -> c_int>, fd: c_int) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - CloseSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.close(fn_ptr, fd) -} - trait CloseSyscall { extern "C" fn close(&self, fn_ptr: Option<&extern "C" fn(c_int) -> c_int>, fd: c_int) -> c_int; } +impl_syscall!(CloseSyscallFacade, IoUringCloseSyscall, NioCloseSyscall, RawCloseSyscall, + close(fd: c_int) -> c_int +); + impl_facade!(CloseSyscallFacade, CloseSyscall, close(fd: c_int) -> c_int); impl_io_uring!(IoUringCloseSyscall, CloseSyscall, close(fd: c_int) -> c_int); diff --git a/core/src/syscall/unix/connect.rs b/core/src/syscall/unix/connect.rs index ece25287..14479e7f 100644 --- a/core/src/syscall/unix/connect.rs +++ b/core/src/syscall/unix/connect.rs @@ -2,30 +2,9 @@ use crate::common::now; use crate::net::EventLoops; use crate::syscall::{is_blocking, reset_errno, send_time_limit, set_blocking, set_errno, set_non_blocking}; use libc::{sockaddr, socklen_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; use std::io::Error; -#[must_use] -pub extern "C" fn connect( - fn_ptr: Option<&extern "C" fn(c_int, *const sockaddr, socklen_t) -> c_int>, - socket: c_int, - address: *const sockaddr, - len: socklen_t, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - ConnectSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.connect(fn_ptr, socket, address, len) -} - trait ConnectSyscall { extern "C" fn connect( &self, @@ -36,6 +15,10 @@ trait ConnectSyscall { ) -> c_int; } +impl_syscall!(ConnectSyscallFacade, IoUringConnectSyscall, NioConnectSyscall, RawConnectSyscall, + connect(fd: c_int, address: *const sockaddr, len: socklen_t) -> c_int +); + impl_facade!(ConnectSyscallFacade, ConnectSyscall, connect(fd: c_int, address: *const sockaddr, len: socklen_t) -> c_int ); diff --git a/core/src/syscall/unix/fsync.rs b/core/src/syscall/unix/fsync.rs index c8fec3ee..46241132 100644 --- a/core/src/syscall/unix/fsync.rs +++ b/core/src/syscall/unix/fsync.rs @@ -1,22 +1,5 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn fsync( - fn_ptr: Option<&extern "C" fn(c_int) -> c_int>, - fd: c_int, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } else { - static CHAIN: Lazy> = Lazy::new(Default::default); - } - } - CHAIN.fsync(fn_ptr, fd) -} - trait FsyncSyscall { extern "C" fn fsync( &self, @@ -25,6 +8,8 @@ trait FsyncSyscall { ) -> c_int; } +impl_syscall2!(FsyncSyscallFacade, IoUringFsyncSyscall, RawFsyncSyscall, fsync(fd: c_int) -> c_int); + impl_facade!(FsyncSyscallFacade, FsyncSyscall, fsync(fd: c_int) -> c_int); impl_io_uring!(IoUringFsyncSyscall, FsyncSyscall, fsync(fd: c_int) -> c_int); diff --git a/core/src/syscall/unix/link.rs b/core/src/syscall/unix/link.rs index 12fea515..dfe4af39 100644 --- a/core/src/syscall/unix/link.rs +++ b/core/src/syscall/unix/link.rs @@ -1,15 +1,4 @@ use std::ffi::{c_char, c_int}; -use once_cell::sync::Lazy; - -#[must_use] -pub extern "C" fn link( - fn_ptr: Option<&extern "C" fn(*const c_char, *const c_char) -> c_int>, - src: *const c_char, - dst: *const c_char -) -> c_int{ - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.link(fn_ptr, src, dst) -} trait LinkSyscall { extern "C" fn link( @@ -20,6 +9,10 @@ trait LinkSyscall { ) -> c_int; } +impl_syscall!(LinkSyscallFacade, RawLinkSyscall, + link(src: *const c_char, dst: *const c_char) -> c_int +); + impl_facade!(LinkSyscallFacade, LinkSyscall, link(src: *const c_char, dst: *const c_char) -> c_int ); diff --git a/core/src/syscall/unix/listen.rs b/core/src/syscall/unix/listen.rs index 6d9e8607..4270e433 100644 --- a/core/src/syscall/unix/listen.rs +++ b/core/src/syscall/unix/listen.rs @@ -1,16 +1,5 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn listen( - fn_ptr: Option<&extern "C" fn(c_int, c_int) -> c_int>, - fd: c_int, - backlog: c_int, -) -> c_int { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.listen(fn_ptr, fd, backlog) -} - trait ListenSyscall { extern "C" fn listen( &self, @@ -20,6 +9,8 @@ trait ListenSyscall { ) -> c_int; } +impl_syscall!(ListenSyscallFacade, RawListenSyscall, listen(fd: c_int, backlog: c_int) -> c_int); + impl_facade!(ListenSyscallFacade, ListenSyscall, listen(fd: c_int, backlog: c_int) -> c_int); impl_raw!(RawListenSyscall, ListenSyscall, listen(fd: c_int, backlog: c_int) -> c_int); diff --git a/core/src/syscall/unix/lseek.rs b/core/src/syscall/unix/lseek.rs index 1f7374fe..88363a1e 100644 --- a/core/src/syscall/unix/lseek.rs +++ b/core/src/syscall/unix/lseek.rs @@ -1,17 +1,5 @@ use libc::off_t; use std::ffi::c_int; -use once_cell::sync::Lazy; - -#[must_use] -pub extern "C" fn lseek( - fn_ptr: Option<&extern "C" fn(c_int, off_t, c_int) -> off_t>, - fd: c_int, - offset: off_t, - whence: c_int, -) -> off_t{ - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.lseek(fn_ptr, fd, offset,whence) -} trait LseekSyscall { extern "C" fn lseek( @@ -23,6 +11,10 @@ trait LseekSyscall { ) -> off_t; } +impl_syscall!(LseekSyscallFacade, RawLseekSyscall, + lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t +); + impl_facade!(LseekSyscallFacade, LseekSyscall, lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t ); diff --git a/core/src/syscall/unix/mkdir.rs b/core/src/syscall/unix/mkdir.rs index 89968b70..b1b003c3 100644 --- a/core/src/syscall/unix/mkdir.rs +++ b/core/src/syscall/unix/mkdir.rs @@ -1,17 +1,6 @@ use libc::mode_t; -use once_cell::sync::Lazy; use std::ffi::{c_char, c_int}; -#[must_use] -pub extern "C" fn mkdir( - fn_ptr: Option<&extern "C" fn(*const c_char, mode_t) -> c_int>, - path: *const c_char, - mode: mode_t, -) -> c_int { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.mkdir(fn_ptr, path, mode) -} - trait MkdirSyscall { extern "C" fn mkdir( &self, @@ -21,6 +10,10 @@ trait MkdirSyscall { ) -> c_int; } +impl_syscall!(MkdirSyscallFacade, RawMkdirSyscall, + mkdir(path: *const c_char, mode: mode_t) -> c_int +); + impl_facade!(MkdirSyscallFacade, MkdirSyscall, mkdir(path: *const c_char, mode: mode_t) -> c_int ); diff --git a/core/src/syscall/unix/mkdirat.rs b/core/src/syscall/unix/mkdirat.rs index 86aa47e8..53000303 100644 --- a/core/src/syscall/unix/mkdirat.rs +++ b/core/src/syscall/unix/mkdirat.rs @@ -1,25 +1,6 @@ use libc::mode_t; -use once_cell::sync::Lazy; use std::ffi::{c_char, c_int}; -#[must_use] -pub extern "C" fn mkdirat( - fn_ptr: Option<&extern "C" fn(c_int, *const c_char, mode_t) -> c_int>, - dirfd: c_int, - pathname: *const c_char, - mode: mode_t, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } else { - static CHAIN: Lazy> = Lazy::new(Default::default); - } - } - CHAIN.mkdirat(fn_ptr, dirfd, pathname, mode) -} - trait MkdiratSyscall { extern "C" fn mkdirat( &self, @@ -30,6 +11,10 @@ trait MkdiratSyscall { ) -> c_int; } +impl_syscall2!(MkdiratSyscallFacade, IoUringMkdiratSyscall, RawMkdiratSyscall, + mkdirat(dirfd: c_int, pathname: *const c_char, mode: mode_t) -> c_int +); + impl_facade!(MkdiratSyscallFacade, MkdiratSyscall, mkdirat(dirfd: c_int, pathname: *const c_char, mode: mode_t) -> c_int ); diff --git a/core/src/syscall/unix/mod.rs b/core/src/syscall/unix/mod.rs index 324f3ed8..0e9d882f 100644 --- a/core/src/syscall/unix/mod.rs +++ b/core/src/syscall/unix/mod.rs @@ -2,6 +2,84 @@ use dashmap::DashMap; use once_cell::sync::Lazy; use std::ffi::c_int; +macro_rules! impl_syscall { + ( + $facade_struct_name:ident, $iocp_struct_name: ident, $nio_struct_name: ident, $raw_struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "C" fn $syscall( + fn_ptr: Option<&extern "C" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + cfg_if::cfg_if! { + if #[cfg(all(target_os = "linux", feature = "io_uring"))] { + static CHAIN: once_cell::sync::Lazy< + $facade_struct_name<$iocp_struct_name<$nio_struct_name<$raw_struct_name>>> + > = once_cell::sync::Lazy::new(Default::default); + } else { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$nio_struct_name<$raw_struct_name>>> = + once_cell::sync::Lazy::new(Default::default); + } + } + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; + ( + $facade_struct_name:ident, $nio_struct_name: ident, $raw_struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "C" fn $syscall( + fn_ptr: Option<&extern "C" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$nio_struct_name<$raw_struct_name>>> = + once_cell::sync::Lazy::new(Default::default); + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; + ( + $facade_struct_name:ident, $struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "C" fn $syscall( + fn_ptr: Option<&extern "C" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$struct_name >> = + once_cell::sync::Lazy::new(Default::default); + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; +} + +macro_rules! impl_syscall2 { + ( + $facade_struct_name:ident, $iocp_struct_name: ident, $raw_struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "C" fn $syscall( + fn_ptr: Option<&extern "C" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + cfg_if::cfg_if! { + if #[cfg(all(target_os = "linux", feature = "io_uring"))] { + static CHAIN: once_cell::sync::Lazy< + $facade_struct_name<$iocp_struct_name<$raw_struct_name>> + > = once_cell::sync::Lazy::new(Default::default); + } else { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$raw_struct_name>> = + once_cell::sync::Lazy::new(Default::default); + } + } + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; +} + macro_rules! impl_facade { ( $struct_name:ident, $trait_name: ident, diff --git a/core/src/syscall/unix/nanosleep.rs b/core/src/syscall/unix/nanosleep.rs index 0902723d..a696732b 100644 --- a/core/src/syscall/unix/nanosleep.rs +++ b/core/src/syscall/unix/nanosleep.rs @@ -1,20 +1,9 @@ use crate::net::EventLoops; use crate::syscall::{reset_errno, set_errno}; use libc::timespec; -use once_cell::sync::Lazy; use std::ffi::c_int; use std::time::Duration; -#[must_use] -pub extern "C" fn nanosleep( - fn_ptr: Option<&extern "C" fn(*const timespec, *mut timespec) -> c_int>, - rqtp: *const timespec, - rmtp: *mut timespec, -) -> c_int { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.nanosleep(fn_ptr, rqtp, rmtp) -} - trait NanosleepSyscall { extern "C" fn nanosleep( &self, @@ -24,6 +13,10 @@ trait NanosleepSyscall { ) -> c_int; } +impl_syscall!(NanosleepSyscallFacade, NioNanosleepSyscall, + nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int +); + impl_facade!(NanosleepSyscallFacade, NanosleepSyscall, nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int ); diff --git a/core/src/syscall/unix/poll.rs b/core/src/syscall/unix/poll.rs index 3b51d5be..25348ad3 100644 --- a/core/src/syscall/unix/poll.rs +++ b/core/src/syscall/unix/poll.rs @@ -1,21 +1,8 @@ use crate::net::EventLoops; use libc::{nfds_t, pollfd}; -use once_cell::sync::Lazy; use std::ffi::c_int; use std::time::Duration; -#[must_use] -pub extern "C" fn poll( - fn_ptr: Option<&extern "C" fn(*mut pollfd, nfds_t, c_int) -> c_int>, - fds: *mut pollfd, - nfds: nfds_t, - timeout: c_int, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.poll(fn_ptr, fds, nfds, timeout) -} - trait PollSyscall { extern "C" fn poll( &self, @@ -26,6 +13,10 @@ trait PollSyscall { ) -> c_int; } +impl_syscall!(PollSyscallFacade, NioPollSyscall, RawPollSyscall, + poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int +); + impl_facade!(PollSyscallFacade, PollSyscall, poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int ); diff --git a/core/src/syscall/unix/pread.rs b/core/src/syscall/unix/pread.rs index 9f14cf2e..78f0d8fd 100644 --- a/core/src/syscall/unix/pread.rs +++ b/core/src/syscall/unix/pread.rs @@ -1,28 +1,6 @@ use libc::{off_t, size_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn pread( - fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, off_t) -> ssize_t>, - fd: c_int, - buf: *mut c_void, - len: size_t, - offset: off_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - PreadSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.pread(fn_ptr, fd, buf, len, offset) -} - trait PreadSyscall { extern "C" fn pread( &self, @@ -34,6 +12,10 @@ trait PreadSyscall { ) -> ssize_t; } +impl_syscall!(PreadSyscallFacade, IoUringPreadSyscall, NioPreadSyscall, RawPreadSyscall, + pread(fd: c_int, buf: *mut c_void, len: size_t, offset: off_t) -> ssize_t +); + impl_facade!(PreadSyscallFacade, PreadSyscall, pread(fd: c_int, buf: *mut c_void, len: size_t, offset: off_t) -> ssize_t ); diff --git a/core/src/syscall/unix/preadv.rs b/core/src/syscall/unix/preadv.rs index 339625f4..664bc1fa 100644 --- a/core/src/syscall/unix/preadv.rs +++ b/core/src/syscall/unix/preadv.rs @@ -1,28 +1,6 @@ use libc::{iovec, off_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn preadv( - fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>, - fd: c_int, - iov: *const iovec, - iovcnt: c_int, - offset: off_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - PreadvSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.preadv(fn_ptr, fd, iov, iovcnt, offset) -} - trait PreadvSyscall { extern "C" fn preadv( &self, @@ -34,6 +12,10 @@ trait PreadvSyscall { ) -> ssize_t; } +impl_syscall!(PreadvSyscallFacade, IoUringPreadvSyscall, NioPreadvSyscall, RawPreadvSyscall, + preadv(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t +); + impl_facade!(PreadvSyscallFacade, PreadvSyscall, preadv(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t ); diff --git a/core/src/syscall/unix/pthread_cond_timedwait.rs b/core/src/syscall/unix/pthread_cond_timedwait.rs index d228028b..d40e16c5 100644 --- a/core/src/syscall/unix/pthread_cond_timedwait.rs +++ b/core/src/syscall/unix/pthread_cond_timedwait.rs @@ -1,27 +1,9 @@ use crate::common::now; use crate::net::EventLoops; use libc::{pthread_cond_t, pthread_mutex_t, timespec}; -use once_cell::sync::Lazy; use std::ffi::c_int; use std::time::Duration; -#[must_use] -pub extern "C" fn pthread_cond_timedwait( - fn_ptr: Option< - &extern "C" fn(*mut pthread_cond_t, *mut pthread_mutex_t, *const timespec) -> c_int, - >, - cond: *mut pthread_cond_t, - lock: *mut pthread_mutex_t, - abstime: *const timespec, -) -> c_int { - static CHAIN: Lazy< - PthreadCondTimedwaitSyscallFacade< - NioPthreadCondTimedwaitSyscall, - >, - > = Lazy::new(Default::default); - CHAIN.pthread_cond_timedwait(fn_ptr, cond, lock, abstime) -} - trait PthreadCondTimedwaitSyscall { extern "C" fn pthread_cond_timedwait( &self, @@ -34,6 +16,15 @@ trait PthreadCondTimedwaitSyscall { ) -> c_int; } +impl_syscall!( + PthreadCondTimedwaitSyscallFacade, NioPthreadCondTimedwaitSyscall, RawPthreadCondTimedwaitSyscall, + pthread_cond_timedwait( + cond: *mut pthread_cond_t, + lock: *mut pthread_mutex_t, + abstime: *const timespec + ) -> c_int +); + impl_facade!(PthreadCondTimedwaitSyscallFacade, PthreadCondTimedwaitSyscall, pthread_cond_timedwait( cond: *mut pthread_cond_t, diff --git a/core/src/syscall/unix/pthread_mutex_lock.rs b/core/src/syscall/unix/pthread_mutex_lock.rs index 8862d522..5d103579 100644 --- a/core/src/syscall/unix/pthread_mutex_lock.rs +++ b/core/src/syscall/unix/pthread_mutex_lock.rs @@ -1,20 +1,8 @@ use crate::net::EventLoops; use crate::scheduler::SchedulableCoroutine; use libc::pthread_mutex_t; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn pthread_mutex_lock( - fn_ptr: Option<&extern "C" fn(*mut pthread_mutex_t) -> c_int>, - lock: *mut pthread_mutex_t, -) -> c_int { - static CHAIN: Lazy< - PthreadMutexLockSyscallFacade>, - > = Lazy::new(Default::default); - CHAIN.pthread_mutex_lock(fn_ptr, lock) -} - trait PthreadMutexLockSyscall { extern "C" fn pthread_mutex_lock( &self, @@ -23,6 +11,10 @@ trait PthreadMutexLockSyscall { ) -> c_int; } +impl_syscall!(PthreadMutexLockSyscallFacade, NioPthreadMutexLockSyscall, RawPthreadMutexLockSyscall, + pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int +); + impl_facade!(PthreadMutexLockSyscallFacade, PthreadMutexLockSyscall, pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int ); diff --git a/core/src/syscall/unix/pthread_mutex_trylock.rs b/core/src/syscall/unix/pthread_mutex_trylock.rs index cde1958d..e81be58c 100644 --- a/core/src/syscall/unix/pthread_mutex_trylock.rs +++ b/core/src/syscall/unix/pthread_mutex_trylock.rs @@ -1,17 +1,6 @@ use libc::pthread_mutex_t; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn pthread_mutex_trylock( - fn_ptr: Option<&extern "C" fn(*mut pthread_mutex_t) -> c_int>, - lock: *mut pthread_mutex_t, -) -> c_int { - static CHAIN: Lazy> = - Lazy::new(Default::default); - CHAIN.pthread_mutex_trylock(fn_ptr, lock) -} - trait PthreadMutexTrylockSyscall { extern "C" fn pthread_mutex_trylock( &self, @@ -20,6 +9,10 @@ trait PthreadMutexTrylockSyscall { ) -> c_int; } +impl_syscall!(PthreadMutexTrylockSyscallFacade, RawPthreadMutexTrylockSyscall, + pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> c_int +); + impl_facade!(PthreadMutexTrylockSyscallFacade, PthreadMutexTrylockSyscall, pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> c_int ); diff --git a/core/src/syscall/unix/pthread_mutex_unlock.rs b/core/src/syscall/unix/pthread_mutex_unlock.rs index 6308b2c7..3551611d 100644 --- a/core/src/syscall/unix/pthread_mutex_unlock.rs +++ b/core/src/syscall/unix/pthread_mutex_unlock.rs @@ -1,17 +1,6 @@ use libc::pthread_mutex_t; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn pthread_mutex_unlock( - fn_ptr: Option<&extern "C" fn(*mut pthread_mutex_t) -> c_int>, - lock: *mut pthread_mutex_t, -) -> c_int { - static CHAIN: Lazy> = - Lazy::new(Default::default); - CHAIN.pthread_mutex_unlock(fn_ptr, lock) -} - trait PthreadMutexUnlockSyscall { extern "C" fn pthread_mutex_unlock( &self, @@ -20,6 +9,10 @@ trait PthreadMutexUnlockSyscall { ) -> c_int; } +impl_syscall!(PthreadMutexUnlockSyscallFacade, RawPthreadMutexUnlockSyscall, + pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int +); + impl_facade!(PthreadMutexUnlockSyscallFacade, PthreadMutexUnlockSyscall, pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int ); diff --git a/core/src/syscall/unix/pwrite.rs b/core/src/syscall/unix/pwrite.rs index 3673564b..4ccfa973 100644 --- a/core/src/syscall/unix/pwrite.rs +++ b/core/src/syscall/unix/pwrite.rs @@ -1,28 +1,6 @@ use libc::{off_t, size_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn pwrite( - fn_ptr: Option<&extern "C" fn(c_int, *const c_void, size_t, off_t) -> ssize_t>, - fd: c_int, - buf: *const c_void, - count: size_t, - offset: off_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - PwriteSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.pwrite(fn_ptr, fd, buf, count, offset) -} - trait PwriteSyscall { extern "C" fn pwrite( &self, @@ -34,6 +12,10 @@ trait PwriteSyscall { ) -> ssize_t; } +impl_syscall!(PwriteSyscallFacade, IoUringPwriteSyscall, NioPwriteSyscall, RawPwriteSyscall, + pwrite(fd: c_int, buf: *const c_void, len: size_t, offset: off_t) -> ssize_t +); + impl_facade!(PwriteSyscallFacade, PwriteSyscall, pwrite(fd: c_int, buf: *const c_void, len: size_t, offset: off_t) -> ssize_t ); diff --git a/core/src/syscall/unix/pwritev.rs b/core/src/syscall/unix/pwritev.rs index d1014894..50867ade 100644 --- a/core/src/syscall/unix/pwritev.rs +++ b/core/src/syscall/unix/pwritev.rs @@ -1,28 +1,6 @@ use libc::{iovec, off_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn pwritev( - fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>, - fd: c_int, - iov: *const iovec, - iovcnt: c_int, - offset: off_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - PwritevSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.pwritev(fn_ptr, fd, iov, iovcnt, offset) -} - trait PwritevSyscall { extern "C" fn pwritev( &self, @@ -34,6 +12,10 @@ trait PwritevSyscall { ) -> ssize_t; } +impl_syscall!(PwritevSyscallFacade, IoUringPwritevSyscall, NioPwritevSyscall, RawPwritevSyscall, + pwritev(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t +); + impl_facade!(PwritevSyscallFacade, PwritevSyscall, pwritev(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t ); diff --git a/core/src/syscall/unix/read.rs b/core/src/syscall/unix/read.rs index 364c5372..fca0bd1e 100644 --- a/core/src/syscall/unix/read.rs +++ b/core/src/syscall/unix/read.rs @@ -1,27 +1,6 @@ use libc::{size_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn read( - fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>, - fd: c_int, - buf: *mut c_void, - len: size_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - ReadSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.read(fn_ptr, fd, buf, len) -} - trait ReadSyscall { extern "C" fn read( &self, @@ -32,6 +11,10 @@ trait ReadSyscall { ) -> ssize_t; } +impl_syscall!(ReadSyscallFacade, IoUringReadSyscall, NioReadSyscall, RawReadSyscall, + read(fd: c_int, buf: *mut c_void, len: size_t) -> ssize_t +); + impl_facade!(ReadSyscallFacade, ReadSyscall, read(fd: c_int, buf: *mut c_void, len: size_t) -> ssize_t ); diff --git a/core/src/syscall/unix/readv.rs b/core/src/syscall/unix/readv.rs index 3913221f..f5f88794 100644 --- a/core/src/syscall/unix/readv.rs +++ b/core/src/syscall/unix/readv.rs @@ -1,27 +1,6 @@ use libc::{iovec, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn readv( - fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>, - fd: c_int, - iov: *const iovec, - iovcnt: c_int, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - ReadvSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.readv(fn_ptr, fd, iov, iovcnt) -} - trait ReadvSyscall { extern "C" fn readv( &self, @@ -32,6 +11,10 @@ trait ReadvSyscall { ) -> ssize_t; } +impl_syscall!(ReadvSyscallFacade, IoUringReadvSyscall, NioReadvSyscall, RawReadvSyscall, + readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t +); + impl_facade!(ReadvSyscallFacade, ReadvSyscall, readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t ); diff --git a/core/src/syscall/unix/recv.rs b/core/src/syscall/unix/recv.rs index b6f6454d..9be41440 100644 --- a/core/src/syscall/unix/recv.rs +++ b/core/src/syscall/unix/recv.rs @@ -1,28 +1,6 @@ use libc::{size_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn recv( - fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>, - socket: c_int, - buf: *mut c_void, - len: size_t, - flags: c_int, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - RecvSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.recv(fn_ptr, socket, buf, len, flags) -} - trait RecvSyscall { extern "C" fn recv( &self, @@ -34,6 +12,10 @@ trait RecvSyscall { ) -> ssize_t; } +impl_syscall!(RecvSyscallFacade, IoUringRecvSyscall, NioRecvSyscall, RawRecvSyscall, + recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t +); + impl_facade!(RecvSyscallFacade, RecvSyscall, recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t ); diff --git a/core/src/syscall/unix/recvfrom.rs b/core/src/syscall/unix/recvfrom.rs index 180b1114..d9872a80 100644 --- a/core/src/syscall/unix/recvfrom.rs +++ b/core/src/syscall/unix/recvfrom.rs @@ -1,24 +1,6 @@ use libc::{size_t, sockaddr, socklen_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn recvfrom( - fn_ptr: Option< - &extern "C" fn(c_int, *mut c_void, size_t, c_int, *mut sockaddr, *mut socklen_t) -> ssize_t, - >, - fd: c_int, - buf: *mut c_void, - len: size_t, - flags: c_int, - addr: *mut sockaddr, - addrlen: *mut socklen_t, -) -> ssize_t { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.recvfrom(fn_ptr, fd, buf, len, flags, addr, addrlen) -} - trait RecvfromSyscall { extern "C" fn recvfrom( &self, @@ -41,6 +23,17 @@ trait RecvfromSyscall { ) -> ssize_t; } +impl_syscall!(RecvfromSyscallFacade, NioRecvfromSyscall, RawRecvfromSyscall, + recvfrom( + fd: c_int, + buf: *mut c_void, + len: size_t, + flags: c_int, + addr: *mut sockaddr, + addrlen: *mut socklen_t + ) -> ssize_t +); + impl_facade!(RecvfromSyscallFacade, RecvfromSyscall, recvfrom( fd: c_int, diff --git a/core/src/syscall/unix/recvmsg.rs b/core/src/syscall/unix/recvmsg.rs index 72e6cadf..08a6c64c 100644 --- a/core/src/syscall/unix/recvmsg.rs +++ b/core/src/syscall/unix/recvmsg.rs @@ -2,30 +2,9 @@ use crate::common::now; use crate::net::EventLoops; use crate::syscall::{is_blocking, reset_errno, set_blocking, set_non_blocking, recv_time_limit}; use libc::{msghdr, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; use std::io::{Error, ErrorKind}; -#[must_use] -pub extern "C" fn recvmsg( - fn_ptr: Option<&extern "C" fn(c_int, *mut msghdr, c_int) -> ssize_t>, - fd: c_int, - msg: *mut msghdr, - flags: c_int, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - RecvmsgSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.recvmsg(fn_ptr, fd, msg, flags) -} - trait RecvmsgSyscall { extern "C" fn recvmsg( &self, @@ -36,6 +15,10 @@ trait RecvmsgSyscall { ) -> ssize_t; } +impl_syscall!(RecvmsgSyscallFacade, IoUringRecvmsgSyscall, NioRecvmsgSyscall, RawRecvmsgSyscall, + recvmsg(fd: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t +); + impl_facade!(RecvmsgSyscallFacade, RecvmsgSyscall, recvmsg(fd: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t ); diff --git a/core/src/syscall/unix/renameat.rs b/core/src/syscall/unix/renameat.rs index 01c4ea76..ec6b2d0a 100644 --- a/core/src/syscall/unix/renameat.rs +++ b/core/src/syscall/unix/renameat.rs @@ -1,25 +1,5 @@ -use once_cell::sync::Lazy; use std::ffi::{c_char, c_int}; -#[must_use] -pub extern "C" fn renameat( - fn_ptr: Option<&extern "C" fn(c_int, *const c_char, c_int, *const c_char) -> c_int>, - olddirfd: c_int, - oldpath: *const c_char, - newdirfd: c_int, - newpath: *const c_char, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } else { - static CHAIN: Lazy> = Lazy::new(Default::default); - } - } - CHAIN.renameat(fn_ptr, olddirfd, oldpath, newdirfd, newpath) -} - trait RenameatSyscall { extern "C" fn renameat( &self, @@ -31,6 +11,10 @@ trait RenameatSyscall { ) -> c_int; } +impl_syscall2!(RenameatSyscallFacade, IoUringRenameatSyscall, RawRenameatSyscall, + renameat(olddirfd: c_int, oldpath: *const c_char, newdirfd: c_int, newpath: *const c_char) -> c_int +); + impl_facade!(RenameatSyscallFacade, RenameatSyscall, renameat(olddirfd: c_int, oldpath: *const c_char, newdirfd: c_int, newpath: *const c_char) -> c_int ); diff --git a/core/src/syscall/unix/renameat2.rs b/core/src/syscall/unix/renameat2.rs index e6083b9d..ed46680a 100644 --- a/core/src/syscall/unix/renameat2.rs +++ b/core/src/syscall/unix/renameat2.rs @@ -1,26 +1,5 @@ -use once_cell::sync::Lazy; use std::ffi::{c_char, c_int, c_uint}; -#[must_use] -pub extern "C" fn renameat2( - fn_ptr: Option<&extern "C" fn(c_int, *const c_char, c_int, *const c_char, c_uint) -> c_int>, - olddirfd: c_int, - oldpath: *const c_char, - newdirfd: c_int, - newpath: *const c_char, - flags: c_uint, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } else { - static CHAIN: Lazy> = Lazy::new(Default::default); - } - } - CHAIN.renameat2(fn_ptr, olddirfd, oldpath, newdirfd, newpath, flags) -} - trait Renameat2Syscall { extern "C" fn renameat2( &self, @@ -33,6 +12,16 @@ trait Renameat2Syscall { ) -> c_int; } +impl_syscall2!(Renameat2SyscallFacade, IoUringRenameat2Syscall, RawRenameat2Syscall, + renameat2( + olddirfd: c_int, + oldpath: *const c_char, + newdirfd: c_int, + newpath: *const c_char, + flags: c_uint, + ) -> c_int +); + impl_facade!(Renameat2SyscallFacade, Renameat2Syscall, renameat2( olddirfd: c_int, diff --git a/core/src/syscall/unix/rmdir.rs b/core/src/syscall/unix/rmdir.rs index 602a7123..9e9f36ec 100644 --- a/core/src/syscall/unix/rmdir.rs +++ b/core/src/syscall/unix/rmdir.rs @@ -1,15 +1,5 @@ -use once_cell::sync::Lazy; use std::ffi::{c_char, c_int}; -#[must_use] -pub extern "C" fn rmdir( - fn_ptr: Option<&extern "C" fn(*const c_char) -> c_int>, - path: *const c_char, -) -> c_int { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.rmdir(fn_ptr, path) -} - trait RmdirSyscall { extern "C" fn rmdir( &self, @@ -18,6 +8,10 @@ trait RmdirSyscall { ) -> c_int; } +impl_syscall!(RmdirSyscallFacade, RawRmdirSyscall, + rmdir(path: *const c_char) -> c_int +); + impl_facade!(RmdirSyscallFacade, RmdirSyscall, rmdir(path: *const c_char) -> c_int ); diff --git a/core/src/syscall/unix/select.rs b/core/src/syscall/unix/select.rs index 806f5d4b..d56f9913 100644 --- a/core/src/syscall/unix/select.rs +++ b/core/src/syscall/unix/select.rs @@ -1,25 +1,8 @@ use crate::net::EventLoops; use libc::{fd_set, timeval}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use std::time::Duration; -#[must_use] -pub extern "C" fn select( - fn_ptr: Option< - &extern "C" fn(c_int, *mut fd_set, *mut fd_set, *mut fd_set, *mut timeval) -> c_int, - >, - nfds: c_int, - readfds: *mut fd_set, - writefds: *mut fd_set, - errorfds: *mut fd_set, - timeout: *mut timeval, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.select(fn_ptr, nfds, readfds, writefds, errorfds, timeout) -} - trait SelectSyscall { extern "C" fn select( &self, @@ -34,6 +17,16 @@ trait SelectSyscall { ) -> c_int; } +impl_syscall!(SelectSyscallFacade, NioSelectSyscall, RawSelectSyscall, + select( + nfds: c_int, + readfds: *mut fd_set, + writefds: *mut fd_set, + errorfds: *mut fd_set, + timeout: *mut timeval + ) -> c_int +); + impl_facade!(SelectSyscallFacade, SelectSyscall, select( nfds: c_int, diff --git a/core/src/syscall/unix/send.rs b/core/src/syscall/unix/send.rs index 5a7d16cf..3e9a3f9c 100644 --- a/core/src/syscall/unix/send.rs +++ b/core/src/syscall/unix/send.rs @@ -1,28 +1,6 @@ use libc::{size_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn send( - fn_ptr: Option<&extern "C" fn(c_int, *const c_void, size_t, c_int) -> ssize_t>, - fd: c_int, - buf: *const c_void, - len: size_t, - flags: c_int, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - SendSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.send(fn_ptr, fd, buf, len, flags) -} - trait SendSyscall { extern "C" fn send( &self, @@ -34,6 +12,10 @@ trait SendSyscall { ) -> ssize_t; } +impl_syscall!(SendSyscallFacade, IoUringSendSyscall, NioSendSyscall, RawSendSyscall, + send(fd: c_int, buf: *const c_void, len: size_t, flags: c_int) -> ssize_t +); + impl_facade!(SendSyscallFacade, SendSyscall, send(fd: c_int, buf: *const c_void, len: size_t, flags: c_int) -> ssize_t ); diff --git a/core/src/syscall/unix/sendmsg.rs b/core/src/syscall/unix/sendmsg.rs index ff6bae5e..e8786fcf 100644 --- a/core/src/syscall/unix/sendmsg.rs +++ b/core/src/syscall/unix/sendmsg.rs @@ -2,30 +2,9 @@ use crate::common::now; use crate::net::EventLoops; use crate::syscall::{is_blocking, reset_errno, set_blocking, set_non_blocking, send_time_limit}; use libc::{msghdr, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; use std::io::{Error, ErrorKind}; -#[must_use] -pub extern "C" fn sendmsg( - fn_ptr: Option<&extern "C" fn(c_int, *const msghdr, c_int) -> ssize_t>, - fd: c_int, - msg: *const msghdr, - flags: c_int, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - SendmsgSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.sendmsg(fn_ptr, fd, msg, flags) -} - trait SendmsgSyscall { extern "C" fn sendmsg( &self, @@ -36,6 +15,10 @@ trait SendmsgSyscall { ) -> ssize_t; } +impl_syscall!(SendmsgSyscallFacade, IoUringSendmsgSyscall, NioSendmsgSyscall, RawSendmsgSyscall, + sendmsg(fd: c_int, msg: *const msghdr, flags: c_int) -> ssize_t +); + impl_facade!(SendmsgSyscallFacade, SendmsgSyscall, sendmsg(fd: c_int, msg: *const msghdr, flags: c_int) -> ssize_t ); diff --git a/core/src/syscall/unix/sendto.rs b/core/src/syscall/unix/sendto.rs index 1563ece0..01074096 100644 --- a/core/src/syscall/unix/sendto.rs +++ b/core/src/syscall/unix/sendto.rs @@ -1,32 +1,6 @@ use libc::{size_t, sockaddr, socklen_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn sendto( - fn_ptr: Option< - &extern "C" fn(c_int, *const c_void, size_t, c_int, *const sockaddr, socklen_t) -> ssize_t, - >, - fd: c_int, - buf: *const c_void, - len: size_t, - flags: c_int, - addr: *const sockaddr, - addrlen: socklen_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - SendtoSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.sendto(fn_ptr, fd, buf, len, flags, addr, addrlen) -} - trait SendtoSyscall { extern "C" fn sendto( &self, @@ -49,22 +23,57 @@ trait SendtoSyscall { ) -> ssize_t; } +impl_syscall!(SendtoSyscallFacade, IoUringSendtoSyscall, NioSendtoSyscall, RawSendtoSyscall, + sendto( + fd: c_int, + buf: *const c_void, + len: size_t, + flags: c_int, + addr: *const sockaddr, + addrlen: socklen_t + ) -> ssize_t +); + impl_facade!(SendtoSyscallFacade, SendtoSyscall, - sendto(fd: c_int, buf: *const c_void, len: size_t, flags: c_int, - addr: *const sockaddr, addrlen: socklen_t) -> ssize_t + sendto( + fd: c_int, + buf: *const c_void, + len: size_t, + flags: c_int, + addr: *const sockaddr, + addrlen: socklen_t + ) -> ssize_t ); impl_io_uring_write!(IoUringSendtoSyscall, SendtoSyscall, - sendto(fd: c_int, buf: *const c_void, len: size_t, flags: c_int, - addr: *const sockaddr, addrlen: socklen_t) -> ssize_t + sendto( + fd: c_int, + buf: *const c_void, + len: size_t, + flags: c_int, + addr: *const sockaddr, + addrlen: socklen_t + ) -> ssize_t ); impl_nio_write_buf!(NioSendtoSyscall, SendtoSyscall, - sendto(fd: c_int, buf: *const c_void, len: size_t, flags: c_int, - addr: *const sockaddr, addrlen: socklen_t) -> ssize_t + sendto( + fd: c_int, + buf: *const c_void, + len: size_t, + flags: c_int, + addr: *const sockaddr, + addrlen: socklen_t + ) -> ssize_t ); impl_raw!(RawSendtoSyscall, SendtoSyscall, - sendto(fd: c_int, buf: *const c_void, len: size_t, flags: c_int, - addr: *const sockaddr, addrlen: socklen_t) -> ssize_t + sendto( + fd: c_int, + buf: *const c_void, + len: size_t, + flags: c_int, + addr: *const sockaddr, + addrlen: socklen_t + ) -> ssize_t ); diff --git a/core/src/syscall/unix/setsockopt.rs b/core/src/syscall/unix/setsockopt.rs index c069f66b..4dfdc2f1 100644 --- a/core/src/syscall/unix/setsockopt.rs +++ b/core/src/syscall/unix/setsockopt.rs @@ -1,23 +1,8 @@ use std::ffi::{c_int, c_void}; use libc::{socklen_t, timeval}; -use once_cell::sync::Lazy; use crate::syscall::get_time_limit; use crate::syscall::unix::{RECV_TIME_LIMIT, SEND_TIME_LIMIT}; -#[must_use] -pub extern "C" fn setsockopt( - fn_ptr: Option<&extern "C" fn(c_int, c_int, c_int, *const c_void, socklen_t) -> c_int>, - socket: c_int, - level: c_int, - name: c_int, - value: *const c_void, - option_len: socklen_t -) -> c_int{ - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.setsockopt(fn_ptr, socket, level, name, value, option_len) -} - trait SetsockoptSyscall { extern "C" fn setsockopt( &self, @@ -30,6 +15,10 @@ trait SetsockoptSyscall { ) -> c_int; } +impl_syscall!(SetsockoptSyscallFacade, NioSetsockoptSyscall, RawSetsockoptSyscall, + setsockopt(socket: c_int, level: c_int, name: c_int, value: *const c_void, option_len: socklen_t) -> c_int +); + impl_facade!(SetsockoptSyscallFacade, SetsockoptSyscall, setsockopt(socket: c_int, level: c_int, name: c_int, value: *const c_void, option_len: socklen_t) -> c_int ); diff --git a/core/src/syscall/unix/shutdown.rs b/core/src/syscall/unix/shutdown.rs index 47a188f4..b3b70faf 100644 --- a/core/src/syscall/unix/shutdown.rs +++ b/core/src/syscall/unix/shutdown.rs @@ -1,27 +1,7 @@ use crate::net::EventLoops; use crate::syscall::set_errno; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn shutdown( - fn_ptr: Option<&extern "C" fn(c_int, c_int) -> c_int>, - socket: c_int, - how: c_int, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - ShutdownSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.shutdown(fn_ptr, socket, how) -} - trait ShutdownSyscall { extern "C" fn shutdown( &self, @@ -31,6 +11,10 @@ trait ShutdownSyscall { ) -> c_int; } +impl_syscall!(ShutdownSyscallFacade, IoUringShutdownSyscall, NioShutdownSyscall, RawShutdownSyscall, + shutdown(fd: c_int, how: c_int) -> c_int +); + impl_facade!(ShutdownSyscallFacade, ShutdownSyscall, shutdown(fd: c_int, how: c_int) -> c_int); impl_io_uring!(IoUringShutdownSyscall, ShutdownSyscall, shutdown(fd: c_int, how: c_int) -> c_int); diff --git a/core/src/syscall/unix/sleep.rs b/core/src/syscall/unix/sleep.rs index 1ea06be8..025b60a8 100644 --- a/core/src/syscall/unix/sleep.rs +++ b/core/src/syscall/unix/sleep.rs @@ -1,15 +1,8 @@ use crate::net::EventLoops; use crate::syscall::reset_errno; -use once_cell::sync::Lazy; use std::ffi::c_uint; use std::time::Duration; -#[must_use] -pub extern "C" fn sleep(fn_ptr: Option<&extern "C" fn(c_uint) -> c_uint>, secs: c_uint) -> c_uint { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.sleep(fn_ptr, secs) -} - trait SleepSyscall { extern "C" fn sleep( &self, @@ -18,6 +11,8 @@ trait SleepSyscall { ) -> c_uint; } +impl_syscall!(SleepSyscallFacade, NioSleepSyscall, sleep(secs: c_uint) -> c_uint); + impl_facade!(SleepSyscallFacade, SleepSyscall, sleep(secs: c_uint) -> c_uint); #[repr(C)] diff --git a/core/src/syscall/unix/socket.rs b/core/src/syscall/unix/socket.rs index 4c7c48fd..9bb0caca 100644 --- a/core/src/syscall/unix/socket.rs +++ b/core/src/syscall/unix/socket.rs @@ -1,26 +1,5 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn socket( - fn_ptr: Option<&extern "C" fn(c_int, c_int, c_int) -> c_int>, - domain: c_int, - ty: c_int, - protocol: c_int, -) -> c_int { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - SocketSyscallFacade> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy> = - Lazy::new(Default::default); - } - } - CHAIN.socket(fn_ptr, domain, ty, protocol) -} - trait SocketSyscall { extern "C" fn socket( &self, @@ -31,6 +10,10 @@ trait SocketSyscall { ) -> c_int; } +impl_syscall2!(SocketSyscallFacade, IoUringSocketSyscall, RawSocketSyscall, + socket(domain: c_int, ty: c_int, protocol: c_int) -> c_int +); + impl_facade!(SocketSyscallFacade, SocketSyscall, socket(domain: c_int, ty: c_int, protocol: c_int) -> c_int ); diff --git a/core/src/syscall/unix/unlink.rs b/core/src/syscall/unix/unlink.rs index ac96c7ac..9981d410 100644 --- a/core/src/syscall/unix/unlink.rs +++ b/core/src/syscall/unix/unlink.rs @@ -1,14 +1,4 @@ use std::ffi::{c_char, c_int}; -use once_cell::sync::Lazy; - -#[must_use] -pub extern "C" fn unlink( - fn_ptr: Option<&extern "C" fn(*const c_char) -> c_int>, - src: *const c_char, -) -> c_int{ - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.unlink(fn_ptr, src) -} trait LinkSyscall { extern "C" fn unlink( @@ -18,6 +8,10 @@ trait LinkSyscall { ) -> c_int; } +impl_syscall!(UnlinkSyscallFacade, RawUnlinkSyscall, + unlink(src: *const c_char) -> c_int +); + impl_facade!(UnlinkSyscallFacade, LinkSyscall, unlink(src: *const c_char) -> c_int ); diff --git a/core/src/syscall/unix/usleep.rs b/core/src/syscall/unix/usleep.rs index d8763921..5bf76921 100644 --- a/core/src/syscall/unix/usleep.rs +++ b/core/src/syscall/unix/usleep.rs @@ -1,18 +1,8 @@ use crate::net::EventLoops; use crate::syscall::reset_errno; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use std::time::Duration; -#[must_use] -pub extern "C" fn usleep( - fn_ptr: Option<&extern "C" fn(c_uint) -> c_int>, - microseconds: c_uint, -) -> c_int { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.usleep(fn_ptr, microseconds) -} - trait UsleepSyscall { extern "C" fn usleep( &self, @@ -21,6 +11,10 @@ trait UsleepSyscall { ) -> c_int; } +impl_syscall!(UsleepSyscallFacade, NioUsleepSyscall, + usleep(microseconds: c_uint) -> c_int +); + impl_facade!(UsleepSyscallFacade, UsleepSyscall, usleep(microseconds: c_uint) -> c_int ); diff --git a/core/src/syscall/unix/write.rs b/core/src/syscall/unix/write.rs index 5935f679..9449a855 100644 --- a/core/src/syscall/unix/write.rs +++ b/core/src/syscall/unix/write.rs @@ -1,27 +1,6 @@ use libc::{size_t, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_void}; -#[must_use] -pub extern "C" fn write( - fn_ptr: Option<&extern "C" fn(c_int, *const c_void, size_t) -> ssize_t>, - fd: c_int, - buf: *const c_void, - len: size_t, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - WriteSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.write(fn_ptr, fd, buf, len) -} - trait WriteSyscall { extern "C" fn write( &self, @@ -32,6 +11,10 @@ trait WriteSyscall { ) -> ssize_t; } +impl_syscall!(WriteSyscallFacade, IoUringWriteSyscall, NioWriteSyscall, RawWriteSyscall, + write(fd: c_int, buf: *const c_void, len: size_t) -> ssize_t +); + impl_facade!(WriteSyscallFacade, WriteSyscall, write(fd: c_int, buf: *const c_void, len: size_t) -> ssize_t ); diff --git a/core/src/syscall/unix/writev.rs b/core/src/syscall/unix/writev.rs index 5aeea9f7..7e3887e6 100644 --- a/core/src/syscall/unix/writev.rs +++ b/core/src/syscall/unix/writev.rs @@ -1,27 +1,6 @@ use libc::{iovec, ssize_t}; -use once_cell::sync::Lazy; use std::ffi::c_int; -#[must_use] -pub extern "C" fn writev( - fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>, - fd: c_int, - iov: *const iovec, - iovcnt: c_int, -) -> ssize_t { - cfg_if::cfg_if! { - if #[cfg(all(target_os = "linux", feature = "io_uring"))] { - static CHAIN: Lazy< - WritevSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.writev(fn_ptr, fd, iov, iovcnt) -} - trait WritevSyscall { extern "C" fn writev( &self, @@ -32,6 +11,10 @@ trait WritevSyscall { ) -> ssize_t; } +impl_syscall!(WritevSyscallFacade, IoUringWritevSyscall, NioWritevSyscall, RawWritevSyscall, + writev(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t +); + impl_facade!(WritevSyscallFacade, WritevSyscall, writev(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t ); diff --git a/core/src/syscall/windows/CreateFileW.rs b/core/src/syscall/windows/CreateFileW.rs index 40774f81..8015dd94 100644 --- a/core/src/syscall/windows/CreateFileW.rs +++ b/core/src/syscall/windows/CreateFileW.rs @@ -1,5 +1,4 @@ use std::ffi::c_uint; -use once_cell::sync::Lazy; use windows_sys::core::PCWSTR; use windows_sys::Win32::Foundation::HANDLE; use windows_sys::Win32::Security::SECURITY_ATTRIBUTES; @@ -7,41 +6,6 @@ use windows_sys::Win32::Storage::FileSystem::{ FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE, }; -#[must_use] -pub extern "system" fn CreateFileW( - fn_ptr: Option< - &extern "system" fn( - PCWSTR, - c_uint, - FILE_SHARE_MODE, - *const SECURITY_ATTRIBUTES, - FILE_CREATION_DISPOSITION, - FILE_FLAGS_AND_ATTRIBUTES, - HANDLE, - ) -> HANDLE, - >, - lpfilename: PCWSTR, - dwdesiredaccess: c_uint, - dwsharemode: FILE_SHARE_MODE, - lpsecurityattributes: *const SECURITY_ATTRIBUTES, - dwcreationdisposition: FILE_CREATION_DISPOSITION, - dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES, - htemplatefile: HANDLE, -) -> HANDLE { - static CHAIN: Lazy> = - Lazy::new(Default::default); - CHAIN.CreateFileW( - fn_ptr, - lpfilename, - dwdesiredaccess, - dwsharemode, - lpsecurityattributes, - dwcreationdisposition, - dwflagsandattributes, - htemplatefile - ) -} - trait CreateFileWSyscall { extern "system" fn CreateFileW( &self, @@ -66,6 +30,18 @@ trait CreateFileWSyscall { ) -> HANDLE; } +impl_syscall!(CreateFileWSyscallFacade, RawCreateFileWSyscall, + CreateFileW( + lpfilename: PCWSTR, + dwdesiredaccess: c_uint, + dwsharemode: FILE_SHARE_MODE, + lpsecurityattributes: *const SECURITY_ATTRIBUTES, + dwcreationdisposition: FILE_CREATION_DISPOSITION, + dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES, + htemplatefile: HANDLE + ) -> HANDLE +); + impl_facade!(CreateFileWSyscallFacade, CreateFileWSyscall, CreateFileW( lpfilename: PCWSTR, diff --git a/core/src/syscall/windows/SetFilePointerEx.rs b/core/src/syscall/windows/SetFilePointerEx.rs index 833c172c..a3ff7fec 100644 --- a/core/src/syscall/windows/SetFilePointerEx.rs +++ b/core/src/syscall/windows/SetFilePointerEx.rs @@ -1,21 +1,7 @@ use std::ffi::c_longlong; -use once_cell::sync::Lazy; use windows_sys::Win32::Foundation::{BOOL, HANDLE}; use windows_sys::Win32::Storage::FileSystem::SET_FILE_POINTER_MOVE_METHOD; -#[must_use] -pub extern "system" fn SetFilePointerEx( - fn_ptr: Option<&extern "system" fn(HANDLE, c_longlong, *mut c_longlong, SET_FILE_POINTER_MOVE_METHOD) -> BOOL>, - hfile: HANDLE, - lidistancetomove: c_longlong, - lpnewfilepointer: *mut c_longlong, - dwmovemethod: SET_FILE_POINTER_MOVE_METHOD -) -> BOOL { - static CHAIN: Lazy> = - Lazy::new(Default::default); - CHAIN.SetFilePointerEx(fn_ptr, hfile, lidistancetomove, lpnewfilepointer, dwmovemethod) -} - trait SetFilePointerExSyscall { extern "system" fn SetFilePointerEx( &self, @@ -27,6 +13,15 @@ trait SetFilePointerExSyscall { ) -> BOOL; } +impl_syscall!(SetFilePointerExSyscallFacade, RawSetFilePointerExSyscall, + SetFilePointerEx( + hfile: HANDLE, + lidistancetomove: c_longlong, + lpnewfilepointer: *mut c_longlong, + dwmovemethod: SET_FILE_POINTER_MOVE_METHOD + ) -> BOOL +); + impl_facade!(SetFilePointerExSyscallFacade, SetFilePointerExSyscall, SetFilePointerEx( hfile: HANDLE, diff --git a/core/src/syscall/windows/Sleep.rs b/core/src/syscall/windows/Sleep.rs index 1158b3b0..dd3f9c6c 100644 --- a/core/src/syscall/windows/Sleep.rs +++ b/core/src/syscall/windows/Sleep.rs @@ -2,15 +2,15 @@ use crate::net::EventLoops; use once_cell::sync::Lazy; use std::time::Duration; +trait SleepSyscall { + extern "system" fn Sleep(&self, fn_ptr: Option<&extern "system" fn(u32)>, dw_milliseconds: u32); +} + pub extern "system" fn Sleep(fn_ptr: Option<&extern "system" fn(u32)>, dw_milliseconds: u32) { static CHAIN: Lazy> = Lazy::new(Default::default); CHAIN.Sleep(fn_ptr, dw_milliseconds); } -trait SleepSyscall { - extern "system" fn Sleep(&self, fn_ptr: Option<&extern "system" fn(u32)>, dw_milliseconds: u32); -} - impl_facade!(SleepSyscallFacade, SleepSyscall, Sleep(dw_milliseconds: u32) -> () ); diff --git a/core/src/syscall/windows/WSAAccept.rs b/core/src/syscall/windows/WSAAccept.rs index d864bf58..4cb3f71a 100644 --- a/core/src/syscall/windows/WSAAccept.rs +++ b/core/src/syscall/windows/WSAAccept.rs @@ -1,29 +1,6 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::Win32::Networking::WinSock::{LPCONDITIONPROC, SOCKADDR, SOCKET}; -#[must_use] -pub extern "system" fn WSAAccept( - fn_ptr: Option< - &extern "system" fn( - SOCKET, - *mut SOCKADDR, - *mut c_int, - LPCONDITIONPROC, - usize - ) -> SOCKET - >, - fd: SOCKET, - address: *mut SOCKADDR, - address_len: *mut c_int, - lpfncondition: LPCONDITIONPROC, - dwcallbackdata: usize, -) -> SOCKET { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.WSAAccept(fn_ptr, fd, address, address_len, lpfncondition, dwcallbackdata) -} - trait WSAAcceptSyscall { extern "system" fn WSAAccept( &self, @@ -44,6 +21,16 @@ trait WSAAcceptSyscall { ) -> SOCKET; } +impl_syscall!(WSAAcceptSyscallFacade, NioWSAAcceptSyscall, RawWSAAcceptSyscall, + WSAAccept( + fd: SOCKET, + address: *mut SOCKADDR, + address_len: *mut c_int, + lpfncondition: LPCONDITIONPROC, + dwcallbackdata: usize + ) -> SOCKET +); + impl_facade!(WSAAcceptSyscallFacade, WSAAcceptSyscall, WSAAccept( fd: SOCKET, diff --git a/core/src/syscall/windows/WSAPoll.rs b/core/src/syscall/windows/WSAPoll.rs index 4490f7a8..b5aa14ec 100644 --- a/core/src/syscall/windows/WSAPoll.rs +++ b/core/src/syscall/windows/WSAPoll.rs @@ -1,22 +1,9 @@ use std::convert::TryInto; use crate::net::EventLoops; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use std::time::Duration; use windows_sys::Win32::Networking::WinSock::WSAPOLLFD; -#[must_use] -pub extern "system" fn WSAPoll( - fn_ptr: Option<&extern "system" fn(*mut WSAPOLLFD, c_uint, c_int) -> c_int>, - fds: *mut WSAPOLLFD, - nfds: c_uint, - timeout: c_int, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.WSAPoll(fn_ptr, fds, nfds, timeout) -} - trait PollSyscall { extern "system" fn WSAPoll( &self, @@ -27,6 +14,10 @@ trait PollSyscall { ) -> c_int; } +impl_syscall!(PollSyscallFacade, NioPollSyscall, RawPollSyscall, + WSAPoll(fds: *mut WSAPOLLFD, nfds: c_uint, timeout: c_int) -> c_int +); + impl_facade!(PollSyscallFacade, PollSyscall, WSAPoll(fds: *mut WSAPOLLFD, nfds: c_uint, timeout: c_int) -> c_int ); diff --git a/core/src/syscall/windows/WSARecv.rs b/core/src/syscall/windows/WSARecv.rs index f4a467bc..24a4ca26 100644 --- a/core/src/syscall/windows/WSARecv.rs +++ b/core/src/syscall/windows/WSARecv.rs @@ -1,43 +1,7 @@ -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use windows_sys::Win32::Networking::WinSock::{LPWSAOVERLAPPED_COMPLETION_ROUTINE, SOCKET, WSABUF}; use windows_sys::Win32::System::IO::OVERLAPPED; -#[must_use] -pub extern "system" fn WSARecv( - fn_ptr: Option< - &extern "system" fn( - SOCKET, - *const WSABUF, - c_uint, - *mut c_uint, - *mut c_uint, - *mut OVERLAPPED, - LPWSAOVERLAPPED_COMPLETION_ROUTINE, - ) -> c_int, - >, - fd: SOCKET, - buf: *const WSABUF, - dwbuffercount: c_uint, - lpnumberofbytesrecvd: *mut c_uint, - lpflags: *mut c_uint, - lpoverlapped: *mut OVERLAPPED, - lpcompletionroutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.WSARecv( - fn_ptr, - fd, - buf, - dwbuffercount, - lpnumberofbytesrecvd, - lpflags, - lpoverlapped, - lpcompletionroutine, - ) -} - trait WSARecvSyscall { extern "system" fn WSARecv( &self, @@ -62,6 +26,18 @@ trait WSARecvSyscall { ) -> c_int; } +impl_syscall!(WSARecvSyscallFacade, NioWSARecvSyscall, RawWSARecvSyscall, + WSARecv( + fd: SOCKET, + buf: *const WSABUF, + dwbuffercount: c_uint, + lpnumberofbytesrecvd: *mut c_uint, + lpflags: *mut c_uint, + lpoverlapped: *mut OVERLAPPED, + lpcompletionroutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE + ) -> c_int +); + impl_facade!(WSARecvSyscallFacade, WSARecvSyscall, WSARecv( fd: SOCKET, diff --git a/core/src/syscall/windows/WSASend.rs b/core/src/syscall/windows/WSASend.rs index 34d05de3..2753b7c0 100644 --- a/core/src/syscall/windows/WSASend.rs +++ b/core/src/syscall/windows/WSASend.rs @@ -1,44 +1,8 @@ -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use windows_sys::Win32::Networking::WinSock::{LPWSAOVERLAPPED_COMPLETION_ROUTINE, SOCKET, WSABUF}; use windows_sys::Win32::System::IO::OVERLAPPED; -#[must_use] -pub extern "system" fn WSASend( - fn_ptr: Option< - &extern "system" fn( - SOCKET, - *const WSABUF, - c_uint, - *mut c_uint, - c_uint, - *mut OVERLAPPED, - LPWSAOVERLAPPED_COMPLETION_ROUTINE, - ) -> c_int, - >, - fd: SOCKET, - buf: *const WSABUF, - dwbuffercount: c_uint, - lpnumberofbytessent: *mut c_uint, - dwflags: c_uint, - lpoverlapped: *mut OVERLAPPED, - lpcompletionroutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.WSASend( - fn_ptr, - fd, - buf, - dwbuffercount, - lpnumberofbytessent, - dwflags, - lpoverlapped, - lpcompletionroutine, - ) -} - -trait WSARecvSyscall { +trait WSASendSyscall { extern "system" fn WSASend( &self, fn_ptr: Option< @@ -62,7 +26,19 @@ trait WSARecvSyscall { ) -> c_int; } -impl_facade!(WSARecvSyscallFacade, WSARecvSyscall, +impl_syscall!(WSASendSyscallFacade, NioWSASendSyscall, RawWSASendSyscall, + WSASend( + fd: SOCKET, + buf: *const WSABUF, + dwbuffercount: c_uint, + lpnumberofbytessent: *mut c_uint, + dwflags: c_uint, + lpoverlapped: *mut OVERLAPPED, + lpcompletionroutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE + ) -> c_int +); + +impl_facade!(WSASendSyscallFacade, WSASendSyscall, WSASend( fd: SOCKET, buf: *const WSABUF, @@ -74,7 +50,7 @@ impl_facade!(WSARecvSyscallFacade, WSARecvSyscall, ) -> c_int ); -impl_nio_write_iovec!(NioWSARecvSyscall, WSARecvSyscall, +impl_nio_write_iovec!(NioWSASendSyscall, WSASendSyscall, WSASend( fd: SOCKET, buf: *const WSABUF, @@ -86,7 +62,7 @@ impl_nio_write_iovec!(NioWSARecvSyscall, WSARecvSyscall, ) -> c_int ); -impl_raw!(RawWSARecvSyscall, WSARecvSyscall, windows_sys::Win32::Networking::WinSock, +impl_raw!(RawWSASendSyscall, WSASendSyscall, windows_sys::Win32::Networking::WinSock, WSASend( fd: SOCKET, buf: *const WSABUF, diff --git a/core/src/syscall/windows/WSASocketW.rs b/core/src/syscall/windows/WSASocketW.rs index fe0022ef..45658698 100644 --- a/core/src/syscall/windows/WSASocketW.rs +++ b/core/src/syscall/windows/WSASocketW.rs @@ -1,32 +1,8 @@ -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use windows_sys::Win32::Networking::WinSock::{ IPPROTO, SOCKET, WINSOCK_SOCKET_TYPE, WSAPROTOCOL_INFOW, }; -#[must_use] -pub extern "system" fn WSASocketW( - fn_ptr: Option< - &extern "system" fn( - c_int, - WINSOCK_SOCKET_TYPE, - IPPROTO, - *const WSAPROTOCOL_INFOW, - c_uint, - c_uint, - ) -> SOCKET, - >, - domain: c_int, - ty: WINSOCK_SOCKET_TYPE, - protocol: IPPROTO, - lpprotocolinfo: *const WSAPROTOCOL_INFOW, - g: c_uint, - dw_flags: c_uint, -) -> SOCKET { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.WSASocketW(fn_ptr, domain, ty, protocol, lpprotocolinfo, g, dw_flags) -} - trait WSASocketWSyscall { extern "system" fn WSASocketW( &self, @@ -49,6 +25,17 @@ trait WSASocketWSyscall { ) -> SOCKET; } +impl_syscall!(WSASocketWSyscallFacade, RawWSASocketWSyscall, + WSASocketW( + domain: c_int, + ty: WINSOCK_SOCKET_TYPE, + protocol: IPPROTO, + lpprotocolinfo: *const WSAPROTOCOL_INFOW, + g: c_uint, + dw_flags: c_uint + ) -> SOCKET +); + impl_facade!(WSASocketWSyscallFacade, WSASocketWSyscall, WSASocketW( domain: c_int, diff --git a/core/src/syscall/windows/WaitOnAddress.rs b/core/src/syscall/windows/WaitOnAddress.rs index f998926d..f93cdbae 100644 --- a/core/src/syscall/windows/WaitOnAddress.rs +++ b/core/src/syscall/windows/WaitOnAddress.rs @@ -1,25 +1,11 @@ use std::ffi::{c_uint, c_void}; use std::time::Duration; -use once_cell::sync::Lazy; use windows_sys::Win32::Foundation::{BOOL, ERROR_TIMEOUT, FALSE, TRUE}; use crate::common::{get_timeout_time, now}; use crate::net::EventLoops; use crate::syscall::reset_errno; use crate::syscall::set_errno; -#[must_use] -pub extern "system" fn WaitOnAddress( - fn_ptr: Option<&extern "system" fn(*const c_void, *const c_void, usize, c_uint) -> BOOL>, - address: *const c_void, - compareaddress: *const c_void, - addresssize: usize, - dwmilliseconds: c_uint -) -> BOOL { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.WaitOnAddress(fn_ptr, address, compareaddress, addresssize, dwmilliseconds) -} - trait WaitOnAddressSyscall { extern "system" fn WaitOnAddress( &self, @@ -31,6 +17,15 @@ trait WaitOnAddressSyscall { ) -> BOOL; } +impl_syscall!(WaitOnAddressSyscallFacade, NioWaitOnAddressSyscall, RawWaitOnAddressSyscall, + WaitOnAddress( + address: *const c_void, + compareaddress: *const c_void, + addresssize: usize, + dwmilliseconds: c_uint + ) -> BOOL +); + impl_facade!(WaitOnAddressSyscallFacade, WaitOnAddressSyscall, WaitOnAddress( address: *const c_void, diff --git a/core/src/syscall/windows/accept.rs b/core/src/syscall/windows/accept.rs index 304927cd..f9cfdeb4 100644 --- a/core/src/syscall/windows/accept.rs +++ b/core/src/syscall/windows/accept.rs @@ -1,28 +1,7 @@ use std::convert::TryInto; -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::Win32::Networking::WinSock::{SOCKADDR, SOCKET}; -#[must_use] -pub extern "system" fn accept( - fn_ptr: Option<&extern "system" fn(SOCKET, *mut SOCKADDR, *mut c_int) -> SOCKET>, - fd: SOCKET, - address: *mut SOCKADDR, - address_len: *mut c_int, -) -> SOCKET { - cfg_if::cfg_if! { - if #[cfg(feature = "iocp")] { - static CHAIN: Lazy< - AcceptSyscallFacade>> - > = Lazy::new(Default::default); - } else { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - } - } - CHAIN.accept(fn_ptr, fd, address, address_len) -} - trait AcceptSyscall { extern "system" fn accept( &self, @@ -33,6 +12,10 @@ trait AcceptSyscall { ) -> SOCKET; } +impl_syscall!(AcceptSyscallFacade, IocpAcceptSyscall, NioAcceptSyscall, RawAcceptSyscall, + accept(fd: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET +); + impl_facade!(AcceptSyscallFacade, AcceptSyscall, accept(fd: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET ); @@ -89,7 +72,7 @@ impl AcceptSyscall for IocpAcceptSyscall { let (lock, cvar) = &*arc; let syscall_result = cvar .wait_while(lock.lock().expect("lock failed"), - |&mut result| result.is_none() + |&mut result| result.is_none() ) .expect("lock failed") .expect("no syscall result"); diff --git a/core/src/syscall/windows/connect.rs b/core/src/syscall/windows/connect.rs index 5ac6ffc9..4711c61b 100644 --- a/core/src/syscall/windows/connect.rs +++ b/core/src/syscall/windows/connect.rs @@ -1,23 +1,10 @@ use crate::common::now; use crate::net::EventLoops; use crate::syscall::{is_blocking, reset_errno, set_blocking, set_errno, set_non_blocking, send_time_limit}; -use once_cell::sync::Lazy; use std::ffi::c_int; use std::io::Error; use windows_sys::Win32::Networking::WinSock::{getpeername, getsockopt, SO_ERROR, SOCKADDR, SOCKET, SOL_SOCKET, WSAEALREADY, WSAEINPROGRESS, WSAEINTR, WSAETIMEDOUT, WSAEWOULDBLOCK}; -#[must_use] -pub extern "system" fn connect( - fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>, - socket: SOCKET, - address: *const SOCKADDR, - len: c_int, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.connect(fn_ptr, socket, address, len) -} - trait ConnectSyscall { extern "system" fn connect( &self, @@ -28,6 +15,10 @@ trait ConnectSyscall { ) -> c_int; } +impl_syscall!(ConnectSyscallFacade, NioConnectSyscall, RawConnectSyscall, + connect(fd: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int +); + impl_facade!(ConnectSyscallFacade, ConnectSyscall, connect(fd: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int ); diff --git a/core/src/syscall/windows/ioctlsocket.rs b/core/src/syscall/windows/ioctlsocket.rs index 856b7754..a8c70c30 100644 --- a/core/src/syscall/windows/ioctlsocket.rs +++ b/core/src/syscall/windows/ioctlsocket.rs @@ -2,22 +2,9 @@ use crate::common::constants::{CoroutineState, SyscallName, SyscallState}; use crate::scheduler::SchedulableCoroutine; use crate::syscall::windows::NON_BLOCKING; use crate::{error, info}; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use windows_sys::Win32::Networking::WinSock::SOCKET; -#[must_use] -pub extern "system" fn ioctlsocket( - fn_ptr: Option<&extern "system" fn(SOCKET, c_int, *mut c_uint) -> c_int>, - fd: SOCKET, - cmd: c_int, - argp: *mut c_uint, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.ioctlsocket(fn_ptr, fd, cmd, argp) -} - trait IoctlsocketSyscall { extern "system" fn ioctlsocket( &self, @@ -28,6 +15,10 @@ trait IoctlsocketSyscall { ) -> c_int; } +impl_syscall!(IoctlsocketSyscallFacade, NioIoctlsocketSyscall, RawIoctlsocketSyscall, + ioctlsocket(fd: SOCKET, cmd: c_int, argp: *mut c_uint) -> c_int +); + #[repr(C)] #[derive(Debug, Default)] struct IoctlsocketSyscallFacade { diff --git a/core/src/syscall/windows/listen.rs b/core/src/syscall/windows/listen.rs index ec52b2d1..92c171ca 100644 --- a/core/src/syscall/windows/listen.rs +++ b/core/src/syscall/windows/listen.rs @@ -1,16 +1,6 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::Win32::Networking::WinSock::SOCKET; -pub extern "system" fn listen( - fn_ptr: Option<&extern "system" fn(SOCKET, c_int) -> c_int>, - fd: SOCKET, - backlog: c_int, -) -> c_int { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.listen(fn_ptr, fd, backlog) -} - trait ListenSyscall { extern "system" fn listen( &self, @@ -20,6 +10,8 @@ trait ListenSyscall { ) -> c_int; } +impl_syscall!(ListenSyscallFacade, RawListenSyscall, listen(fd: SOCKET, backlog: c_int) -> c_int); + impl_facade!(ListenSyscallFacade, ListenSyscall, listen(fd: SOCKET, backlog: c_int) -> c_int); impl_raw!(RawListenSyscall, ListenSyscall, windows_sys::Win32::Networking::WinSock, diff --git a/core/src/syscall/windows/mod.rs b/core/src/syscall/windows/mod.rs index b10e766b..1bee4491 100644 --- a/core/src/syscall/windows/mod.rs +++ b/core/src/syscall/windows/mod.rs @@ -6,6 +6,59 @@ use windows_sys::Win32::Networking::WinSock::{ getsockopt, SOCKET, SOL_SOCKET, SO_RCVTIMEO, SO_SNDTIMEO, WSAENOTSOCK, }; +macro_rules! impl_syscall { + ( + $facade_struct_name:ident, $iocp_struct_name: ident, $nio_struct_name: ident, $raw_struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "system" fn $syscall( + fn_ptr: Option<&extern "system" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + cfg_if::cfg_if! { + if #[cfg(all(windows, feature = "iocp"))] { + static CHAIN: once_cell::sync::Lazy< + $facade_struct_name<$iocp_struct_name<$nio_struct_name<$raw_struct_name>>> + > = once_cell::sync::Lazy::new(Default::default); + } else { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$nio_struct_name<$raw_struct_name>>> = + once_cell::sync::Lazy::new(Default::default); + } + } + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; + ( + $facade_struct_name:ident, $nio_struct_name: ident, $raw_struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "system" fn $syscall( + fn_ptr: Option<&extern "system" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$nio_struct_name<$raw_struct_name>>> = + once_cell::sync::Lazy::new(Default::default); + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; + ( + $facade_struct_name:ident, $struct_name: ident, + $syscall: ident($($arg: ident : $arg_type: ty),*$(,)?) -> $result: ty + ) => { + #[must_use] + pub extern "system" fn $syscall( + fn_ptr: Option<&extern "system" fn($($arg_type),*) -> $result>, + $($arg: $arg_type),* + ) -> $result { + static CHAIN: once_cell::sync::Lazy<$facade_struct_name<$struct_name >> = + once_cell::sync::Lazy::new(Default::default); + CHAIN.$syscall(fn_ptr, $($arg, )*) + } + }; +} + macro_rules! impl_facade { ( $struct_name:ident, $trait_name: ident, diff --git a/core/src/syscall/windows/recv.rs b/core/src/syscall/windows/recv.rs index b9d822f7..7a044283 100644 --- a/core/src/syscall/windows/recv.rs +++ b/core/src/syscall/windows/recv.rs @@ -1,21 +1,7 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::core::PSTR; use windows_sys::Win32::Networking::WinSock::{SEND_RECV_FLAGS, SOCKET}; -#[must_use] -pub extern "system" fn recv( - fn_ptr: Option<&extern "system" fn(SOCKET, PSTR, c_int, SEND_RECV_FLAGS) -> c_int>, - fd: SOCKET, - buf: PSTR, - len: c_int, - flags: SEND_RECV_FLAGS, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.recv(fn_ptr, fd, buf, len, flags) -} - trait RecvSyscall { extern "system" fn recv( &self, @@ -27,6 +13,10 @@ trait RecvSyscall { ) -> c_int; } +impl_syscall!(RecvSyscallFacade, NioRecvSyscall, RawRecvSyscall, + recv(fd: SOCKET, buf: PSTR, len: c_int, flags: SEND_RECV_FLAGS) -> c_int +); + impl_facade!(RecvSyscallFacade, RecvSyscall, recv(fd: SOCKET, buf: PSTR, len: c_int, flags: SEND_RECV_FLAGS) -> c_int ); diff --git a/core/src/syscall/windows/select.rs b/core/src/syscall/windows/select.rs index 39ac9eca..f37bd0cc 100644 --- a/core/src/syscall/windows/select.rs +++ b/core/src/syscall/windows/select.rs @@ -1,25 +1,8 @@ use crate::net::EventLoops; -use once_cell::sync::Lazy; use std::ffi::{c_int, c_uint}; use std::time::Duration; use windows_sys::Win32::Networking::WinSock::{FD_SET, TIMEVAL}; -#[must_use] -pub extern "system" fn select( - fn_ptr: Option< - &extern "system" fn(c_int, *mut FD_SET, *mut FD_SET, *mut FD_SET, *mut TIMEVAL) -> c_int, - >, - nfds: c_int, - readfds: *mut FD_SET, - writefds: *mut FD_SET, - errorfds: *mut FD_SET, - timeout: *mut TIMEVAL, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.select(fn_ptr, nfds, readfds, writefds, errorfds, timeout) -} - trait SelectSyscall { extern "system" fn select( &self, @@ -34,6 +17,16 @@ trait SelectSyscall { ) -> c_int; } +impl_syscall!(SelectSyscallFacade, NioSelectSyscall, RawSelectSyscall, + select( + nfds: c_int, + readfds: *mut FD_SET, + writefds: *mut FD_SET, + errorfds: *mut FD_SET, + timeout: *mut TIMEVAL + ) -> c_int +); + impl_facade!(SelectSyscallFacade, SelectSyscall, select( nfds: c_int, diff --git a/core/src/syscall/windows/send.rs b/core/src/syscall/windows/send.rs index 8b87be11..68b0f1b0 100644 --- a/core/src/syscall/windows/send.rs +++ b/core/src/syscall/windows/send.rs @@ -1,21 +1,7 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::core::PCSTR; use windows_sys::Win32::Networking::WinSock::{SEND_RECV_FLAGS, SOCKET}; -#[must_use] -pub extern "system" fn send( - fn_ptr: Option<&extern "system" fn(SOCKET, PCSTR, c_int, SEND_RECV_FLAGS) -> c_int>, - fd: SOCKET, - buf: PCSTR, - len: c_int, - flags: SEND_RECV_FLAGS, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.send(fn_ptr, fd, buf, len, flags) -} - trait SendSyscall { extern "system" fn send( &self, @@ -27,6 +13,10 @@ trait SendSyscall { ) -> c_int; } +impl_syscall!(SendSyscallFacade, NioSendSyscall, RawSendSyscall, + send(fd: SOCKET, buf: PCSTR, len: c_int, flags: SEND_RECV_FLAGS) -> c_int +); + impl_facade!(SendSyscallFacade, SendSyscall, send(fd: SOCKET, buf: PCSTR, len: c_int, flags: SEND_RECV_FLAGS) -> c_int ); diff --git a/core/src/syscall/windows/setsockopt.rs b/core/src/syscall/windows/setsockopt.rs index 586f9c5a..46b665fc 100644 --- a/core/src/syscall/windows/setsockopt.rs +++ b/core/src/syscall/windows/setsockopt.rs @@ -1,23 +1,8 @@ use std::ffi::c_int; -use once_cell::sync::Lazy; use windows_sys::core::PSTR; use crate::syscall::windows::{RECV_TIME_LIMIT, SEND_TIME_LIMIT}; use windows_sys::Win32::Networking::WinSock::{SO_RCVTIMEO, SO_SNDTIMEO, SOCKET, SOL_SOCKET}; -#[must_use] -pub extern "system" fn setsockopt( - fn_ptr: Option<&extern "system" fn(SOCKET, c_int, c_int, PSTR, c_int) -> c_int>, - socket: SOCKET, - level: c_int, - name: c_int, - value: PSTR, - option_len: c_int -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.setsockopt(fn_ptr, socket, level, name, value, option_len) -} - trait SetsockoptSyscall { extern "system" fn setsockopt( &self, @@ -30,6 +15,10 @@ trait SetsockoptSyscall { ) -> c_int; } +impl_syscall!(SetsockoptSyscallFacade, NioSetsockoptSyscall, RawSetsockoptSyscall, + setsockopt(socket: SOCKET, level: c_int, name: c_int, value: PSTR, option_len: c_int) -> c_int +); + impl_facade!(SetsockoptSyscallFacade, SetsockoptSyscall, setsockopt(socket: SOCKET, level: c_int, name: c_int, value: PSTR, option_len: c_int) -> c_int ); diff --git a/core/src/syscall/windows/shutdown.rs b/core/src/syscall/windows/shutdown.rs index 53f2f8e9..125bb3e8 100644 --- a/core/src/syscall/windows/shutdown.rs +++ b/core/src/syscall/windows/shutdown.rs @@ -1,20 +1,8 @@ use crate::net::EventLoops; use crate::syscall::set_errno; -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::Win32::Networking::WinSock::{SOCKET, WINSOCK_SHUTDOWN_HOW, SD_RECEIVE, SD_SEND, SD_BOTH, WSAEINVAL}; -#[must_use] -pub extern "system" fn shutdown( - fn_ptr: Option<&extern "system" fn(SOCKET, WINSOCK_SHUTDOWN_HOW) -> c_int>, - fd: SOCKET, - how: WINSOCK_SHUTDOWN_HOW, -) -> c_int { - static CHAIN: Lazy>> = - Lazy::new(Default::default); - CHAIN.shutdown(fn_ptr, fd, how) -} - trait ShutdownSyscall { extern "system" fn shutdown( &self, @@ -24,6 +12,10 @@ trait ShutdownSyscall { ) -> c_int; } +impl_syscall!(ShutdownSyscallFacade, NioShutdownSyscall, RawShutdownSyscall, + shutdown(fd: SOCKET, how: WINSOCK_SHUTDOWN_HOW) -> c_int +); + impl_facade!(ShutdownSyscallFacade, ShutdownSyscall, shutdown(fd: SOCKET, how: WINSOCK_SHUTDOWN_HOW) -> c_int); #[repr(C)] diff --git a/core/src/syscall/windows/socket.rs b/core/src/syscall/windows/socket.rs index aff69705..817ab0a9 100644 --- a/core/src/syscall/windows/socket.rs +++ b/core/src/syscall/windows/socket.rs @@ -1,18 +1,6 @@ -use once_cell::sync::Lazy; use std::ffi::c_int; use windows_sys::Win32::Networking::WinSock::{IPPROTO, SOCKET, WINSOCK_SOCKET_TYPE}; -#[must_use] -pub extern "system" fn socket( - fn_ptr: Option<&extern "system" fn(c_int, WINSOCK_SOCKET_TYPE, IPPROTO) -> SOCKET>, - domain: c_int, - ty: WINSOCK_SOCKET_TYPE, - protocol: IPPROTO, -) -> SOCKET { - static CHAIN: Lazy> = Lazy::new(Default::default); - CHAIN.socket(fn_ptr, domain, ty, protocol) -} - trait SocketSyscall { extern "system" fn socket( &self, @@ -23,6 +11,10 @@ trait SocketSyscall { ) -> SOCKET; } +impl_syscall!(SocketSyscallFacade, RawSocketSyscall, + socket(domain: c_int, ty: WINSOCK_SOCKET_TYPE, protocol: IPPROTO) -> SOCKET +); + impl_facade!(SocketSyscallFacade, SocketSyscall, socket(domain: c_int, ty: WINSOCK_SOCKET_TYPE, protocol: IPPROTO) -> SOCKET );