Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions open-coroutine-core/src/syscall/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,6 @@ cfg_if::cfg_if! {
}
}

/// read

#[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,
count: size_t,
) -> ssize_t {
CHAIN.read(fn_ptr, fd, buf, count)
}

#[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,
count: size_t,
offset: off_t,
) -> ssize_t {
CHAIN.pread(fn_ptr, fd, buf, count, offset)
}

#[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 {
CHAIN.recvmsg(fn_ptr, fd, msg, flags)
}

/// write

#[must_use]
Expand Down
31 changes: 0 additions & 31 deletions open-coroutine-core/src/syscall/io_uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,6 @@ macro_rules! impl_io_uring {
}

impl<I: UnixSyscall> UnixSyscall for IoUringLinuxSyscall<I> {
extern "C" fn read(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
) -> ssize_t {
impl_io_uring!(self, read, fn_ptr, fd, buf, count)
}

extern "C" fn pread(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, off_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
offset: off_t,
) -> ssize_t {
impl_io_uring!(self, pread, fn_ptr, fd, buf, count, offset)
}

extern "C" fn recvmsg(
&self,
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 {
impl_io_uring!(self, recvmsg, fn_ptr, fd, msg, flags)
}

extern "C" fn sendto(
&self,
fn_ptr: Option<
Expand Down
27 changes: 0 additions & 27 deletions open-coroutine-core/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,6 @@ pub use facade::*;

#[cfg(unix)]
pub trait UnixSyscall {
/// read

extern "C" fn read(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
) -> ssize_t;

extern "C" fn pread(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, off_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
offset: off_t,
) -> ssize_t;

extern "C" fn recvmsg(
&self,
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;

/// write

extern "C" fn sendto(
Expand Down
166 changes: 0 additions & 166 deletions open-coroutine-core/src/syscall/nio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,6 @@ pub struct NioLinuxSyscall<I: UnixSyscall> {
inner: I,
}

macro_rules! impl_expected_read_hook {
( $invoker: expr, $syscall: ident, $fn_ptr: expr, $socket:expr, $buffer:expr, $length:expr, $($arg: expr),* $(,)* ) => {{
let socket = $socket;
let blocking = $crate::syscall::common::is_blocking(socket);
if blocking {
$crate::syscall::common::set_non_blocking(socket);
}
let mut received = 0;
let mut r = 0;
while received < $length {
r = $invoker.$syscall(
$fn_ptr,
$socket,
($buffer as usize + received) as *mut c_void,
$length - received,
$($arg, )*
);
if r != -1 {
$crate::syscall::common::reset_errno();
received += r as size_t;
if received >= $length || r == 0 {
r = received as ssize_t;
break;
}
}
let error_kind = std::io::Error::last_os_error().kind();
if error_kind == std::io::ErrorKind::WouldBlock {
//wait read event
if $crate::net::event_loop::EventLoops::wait_read_event(
socket,
Some(std::time::Duration::from_millis(10)),
)
.is_err()
{
break;
}
} else if error_kind != std::io::ErrorKind::Interrupted {
break;
}
}
if blocking {
$crate::syscall::common::set_blocking(socket);
}
r
}};
}

macro_rules! impl_expected_write_hook {
( $invoker: expr, $syscall: ident, $fn_ptr: expr, $socket:expr, $buffer:expr, $length:expr, $($arg: expr),* $(,)* ) => {{
let socket = $socket;
Expand Down Expand Up @@ -109,125 +62,6 @@ macro_rules! impl_expected_write_hook {
}

impl<I: UnixSyscall> UnixSyscall for NioLinuxSyscall<I> {
extern "C" fn read(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
) -> ssize_t {
impl_expected_read_hook!(self.inner, read, fn_ptr, fd, buf, count,)
}

extern "C" fn pread(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, off_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
offset: off_t,
) -> ssize_t {
impl_expected_read_hook!(self.inner, pread, fn_ptr, fd, buf, count, offset)
}

extern "C" fn recvmsg(
&self,
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 {
let blocking = is_blocking(fd);
if blocking {
set_non_blocking(fd);
}
let msghdr = unsafe { *msg };
let mut vec = std::collections::VecDeque::from(unsafe {
Vec::from_raw_parts(
msghdr.msg_iov,
msghdr.msg_iovlen as usize,
msghdr.msg_iovlen as usize,
)
});
let mut length = 0;
let mut pices = std::collections::VecDeque::new();
for iovec in &vec {
length += iovec.iov_len;
pices.push_back(length);
}
let mut received = 0;
let mut r = 0;
while received < length {
// find from-index
let mut from_index = 0;
for (i, v) in pices.iter().enumerate() {
if received < *v {
from_index = i;
break;
}
}
// calculate offset
let current_received_offset = if from_index > 0 {
received.saturating_sub(pices[from_index.saturating_sub(1)])
} else {
received
};
// remove already received
for _ in 0..from_index {
_ = vec.pop_front();
_ = pices.pop_front();
}
// build syscall args
vec[0] = iovec {
iov_base: (vec[0].iov_base as usize + current_received_offset) as *mut c_void,
iov_len: vec[0].iov_len - current_received_offset,
};
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "linux",
target_os = "l4re",
target_os = "android",
target_os = "emscripten"
))] {
let len = vec.len();
} else {
let len = c_int::try_from(vec.len()).unwrap();
}
}
let mut new_msg = msghdr {
msg_name: msghdr.msg_name,
msg_namelen: msghdr.msg_namelen,
msg_iov: vec.get_mut(0).unwrap(),
msg_iovlen: len,
msg_control: msghdr.msg_control,
msg_controllen: msghdr.msg_controllen,
msg_flags: msghdr.msg_flags,
};
r = self.inner.recvmsg(fn_ptr, fd, &mut new_msg, flags);
if r != -1 {
reset_errno();
received += r as usize;
if received >= length || r == 0 {
r = received as ssize_t;
break;
}
}
let error_kind = std::io::Error::last_os_error().kind();
if error_kind == std::io::ErrorKind::WouldBlock {
//wait read event
if EventLoops::wait_read_event(fd, Some(Duration::from_millis(10))).is_err() {
break;
}
} else if error_kind != std::io::ErrorKind::Interrupted {
break;
}
}
if blocking {
set_blocking(fd);
}
r
}

extern "C" fn sendto(
&self,
fn_ptr: Option<
Expand Down
45 changes: 0 additions & 45 deletions open-coroutine-core/src/syscall/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,6 @@ use std::ffi::{c_int, c_void};
pub struct RawLinuxSyscall {}

impl UnixSyscall for RawLinuxSyscall {
/// read

extern "C" fn read(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
) -> ssize_t {
if let Some(f) = fn_ptr {
(f)(fd, buf, count)
} else {
unsafe { libc::read(fd, buf, count) }
}
}

extern "C" fn pread(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, off_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
offset: off_t,
) -> ssize_t {
if let Some(f) = fn_ptr {
(f)(fd, buf, count, offset)
} else {
unsafe { libc::pread(fd, buf, count, offset) }
}
}

extern "C" fn recvmsg(
&self,
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 {
if let Some(f) = fn_ptr {
(f)(fd, msg, flags)
} else {
unsafe { libc::recvmsg(fd, msg, flags) }
}
}

/// write

extern "C" fn sendto(
Expand Down
31 changes: 0 additions & 31 deletions open-coroutine-core/src/syscall/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,6 @@ macro_rules! syscall_state {
}

impl<I: UnixSyscall> UnixSyscall for StateLinuxSyscall<I> {
extern "C" fn read(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
) -> ssize_t {
syscall_state!(self, read, fn_ptr, fd, buf, count)
}

extern "C" fn pread(
&self,
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, off_t) -> ssize_t>,
fd: c_int,
buf: *mut c_void,
count: size_t,
offset: off_t,
) -> ssize_t {
syscall_state!(self, pread, fn_ptr, fd, buf, count, offset)
}

extern "C" fn recvmsg(
&self,
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 {
syscall_state!(self, recvmsg, fn_ptr, fd, msg, flags)
}

extern "C" fn sendto(
&self,
fn_ptr: Option<
Expand Down
Loading