Skip to content

Commit 80f4f98

Browse files
committed
refactor poll/select/recvfrom syscall
1 parent 59eaacf commit 80f4f98

File tree

10 files changed

+256
-372
lines changed

10 files changed

+256
-372
lines changed

open-coroutine-core/src/syscall/facade.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::syscall::LinuxSyscall;
66
use crate::syscall::UnixSyscall;
77
#[cfg(target_os = "linux")]
88
use libc::epoll_event;
9-
use libc::{
10-
fd_set, iovec, msghdr, nfds_t, off_t, pollfd, size_t, sockaddr, socklen_t, ssize_t, timeval,
11-
};
9+
use libc::{iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
1210
use once_cell::sync::Lazy;
1311
use std::ffi::{c_int, c_void};
1412

@@ -23,49 +21,8 @@ cfg_if::cfg_if! {
2321
}
2422
}
2523

26-
/// poll
27-
28-
#[must_use]
29-
pub extern "C" fn poll(
30-
fn_ptr: Option<&extern "C" fn(*mut pollfd, nfds_t, c_int) -> c_int>,
31-
fds: *mut pollfd,
32-
nfds: nfds_t,
33-
timeout: c_int,
34-
) -> c_int {
35-
CHAIN.poll(fn_ptr, fds, nfds, timeout)
36-
}
37-
38-
#[must_use]
39-
pub extern "C" fn select(
40-
fn_ptr: Option<
41-
&extern "C" fn(c_int, *mut fd_set, *mut fd_set, *mut fd_set, *mut timeval) -> c_int,
42-
>,
43-
nfds: c_int,
44-
readfds: *mut fd_set,
45-
writefds: *mut fd_set,
46-
errorfds: *mut fd_set,
47-
timeout: *mut timeval,
48-
) -> c_int {
49-
CHAIN.select(fn_ptr, nfds, readfds, writefds, errorfds, timeout)
50-
}
51-
5224
/// read
5325
54-
#[must_use]
55-
pub extern "C" fn recvfrom(
56-
fn_ptr: Option<
57-
&extern "C" fn(c_int, *mut c_void, size_t, c_int, *mut sockaddr, *mut socklen_t) -> ssize_t,
58-
>,
59-
socket: c_int,
60-
buf: *mut c_void,
61-
len: size_t,
62-
flags: c_int,
63-
addr: *mut sockaddr,
64-
addrlen: *mut socklen_t,
65-
) -> ssize_t {
66-
CHAIN.recvfrom(fn_ptr, socket, buf, len, flags, addr, addrlen)
67-
}
68-
6926
#[must_use]
7027
pub extern "C" fn read(
7128
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,

open-coroutine-core/src/syscall/io_uring.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::syscall::LinuxSyscall;
22
use crate::syscall::UnixSyscall;
33
use libc::epoll_event;
4-
use libc::{
5-
fd_set, iovec, msghdr, nfds_t, off_t, pollfd, size_t, sockaddr, socklen_t, ssize_t, timeval,
6-
};
4+
use libc::{iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
75
use std::ffi::{c_int, c_void};
86

97
#[derive(Debug, Default)]
@@ -27,52 +25,6 @@ macro_rules! impl_io_uring {
2725
}
2826

2927
impl<I: UnixSyscall> UnixSyscall for IoUringLinuxSyscall<I> {
30-
extern "C" fn poll(
31-
&self,
32-
fn_ptr: Option<&extern "C" fn(*mut pollfd, nfds_t, c_int) -> c_int>,
33-
fds: *mut pollfd,
34-
nfds: nfds_t,
35-
timeout: c_int,
36-
) -> c_int {
37-
unsupported!(self, poll, fn_ptr, fds, nfds, timeout)
38-
}
39-
40-
extern "C" fn select(
41-
&self,
42-
fn_ptr: Option<
43-
&extern "C" fn(c_int, *mut fd_set, *mut fd_set, *mut fd_set, *mut timeval) -> c_int,
44-
>,
45-
nfds: c_int,
46-
readfds: *mut fd_set,
47-
writefds: *mut fd_set,
48-
errorfds: *mut fd_set,
49-
timeout: *mut timeval,
50-
) -> c_int {
51-
unsupported!(self, select, fn_ptr, nfds, readfds, writefds, errorfds, timeout)
52-
}
53-
54-
extern "C" fn recvfrom(
55-
&self,
56-
fn_ptr: Option<
57-
&extern "C" fn(
58-
c_int,
59-
*mut c_void,
60-
size_t,
61-
c_int,
62-
*mut sockaddr,
63-
*mut socklen_t,
64-
) -> ssize_t,
65-
>,
66-
socket: c_int,
67-
buf: *mut c_void,
68-
len: size_t,
69-
flags: c_int,
70-
addr: *mut sockaddr,
71-
addrlen: *mut socklen_t,
72-
) -> ssize_t {
73-
unsupported!(self, recvfrom, fn_ptr, socket, buf, len, flags, addr, addrlen)
74-
}
75-
7628
extern "C" fn read(
7729
&self,
7830
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,

open-coroutine-core/src/syscall/mod.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#[cfg(target_os = "linux")]
22
use libc::epoll_event;
33
#[cfg(unix)]
4-
use libc::{
5-
fd_set, iovec, msghdr, nfds_t, off_t, pollfd, size_t, sockaddr, socklen_t, ssize_t, timeval,
6-
};
4+
use libc::{iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
75
#[cfg(unix)]
86
use std::ffi::{c_int, c_void};
97

@@ -30,50 +28,8 @@ pub use facade::*;
3028

3129
#[cfg(unix)]
3230
pub trait UnixSyscall {
33-
/// poll
34-
35-
extern "C" fn poll(
36-
&self,
37-
fn_ptr: Option<&extern "C" fn(*mut pollfd, nfds_t, c_int) -> c_int>,
38-
fds: *mut pollfd,
39-
nfds: nfds_t,
40-
timeout: c_int,
41-
) -> c_int;
42-
43-
extern "C" fn select(
44-
&self,
45-
fn_ptr: Option<
46-
&extern "C" fn(c_int, *mut fd_set, *mut fd_set, *mut fd_set, *mut timeval) -> c_int,
47-
>,
48-
nfds: c_int,
49-
readfds: *mut fd_set,
50-
writefds: *mut fd_set,
51-
errorfds: *mut fd_set,
52-
timeout: *mut timeval,
53-
) -> c_int;
54-
5531
/// read
5632
57-
extern "C" fn recvfrom(
58-
&self,
59-
fn_ptr: Option<
60-
&extern "C" fn(
61-
c_int,
62-
*mut c_void,
63-
size_t,
64-
c_int,
65-
*mut sockaddr,
66-
*mut socklen_t,
67-
) -> ssize_t,
68-
>,
69-
fd: c_int,
70-
buf: *mut c_void,
71-
len: size_t,
72-
flags: c_int,
73-
addr: *mut sockaddr,
74-
addrlen: *mut socklen_t,
75-
) -> ssize_t;
76-
7733
extern "C" fn read(
7834
&self,
7935
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,

open-coroutine-core/src/syscall/nio.rs

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ use crate::syscall::LinuxSyscall;
55
use crate::syscall::UnixSyscall;
66
#[cfg(target_os = "linux")]
77
use libc::epoll_event;
8-
use libc::{
9-
fd_set, iovec, msghdr, nfds_t, off_t, pollfd, size_t, sockaddr, socklen_t, ssize_t, timeval,
10-
};
11-
use std::ffi::{c_int, c_uint, c_void};
8+
use libc::{iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
9+
use std::ffi::{c_int, c_void};
1210
use std::time::Duration;
1311

1412
#[derive(Debug, Default)]
@@ -259,123 +257,6 @@ macro_rules! impl_expected_batch_write_hook {
259257
}
260258

261259
impl<I: UnixSyscall> UnixSyscall for NioLinuxSyscall<I> {
262-
extern "C" fn poll(
263-
&self,
264-
fn_ptr: Option<&extern "C" fn(*mut pollfd, nfds_t, c_int) -> c_int>,
265-
fds: *mut pollfd,
266-
nfds: nfds_t,
267-
timeout: c_int,
268-
) -> c_int {
269-
let mut t = if timeout < 0 { c_int::MAX } else { timeout };
270-
let mut x = 1;
271-
let mut r;
272-
// just check select every x ms
273-
loop {
274-
r = self.inner.poll(fn_ptr, fds, nfds, 0);
275-
if r != 0 || t == 0 {
276-
break;
277-
}
278-
_ = EventLoops::wait_just(Some(Duration::from_millis(t.min(x) as u64)));
279-
if t != c_int::MAX {
280-
t = if t > x { t - x } else { 0 };
281-
}
282-
if x < 16 {
283-
x <<= 1;
284-
}
285-
}
286-
r
287-
}
288-
289-
extern "C" fn select(
290-
&self,
291-
fn_ptr: Option<
292-
&extern "C" fn(c_int, *mut fd_set, *mut fd_set, *mut fd_set, *mut timeval) -> c_int,
293-
>,
294-
nfds: c_int,
295-
readfds: *mut fd_set,
296-
writefds: *mut fd_set,
297-
errorfds: *mut fd_set,
298-
timeout: *mut timeval,
299-
) -> c_int {
300-
let mut t = if timeout.is_null() {
301-
c_uint::MAX
302-
} else {
303-
unsafe { ((*timeout).tv_sec as c_uint) * 1_000_000 + (*timeout).tv_usec as c_uint }
304-
};
305-
let mut o = timeval {
306-
tv_sec: 0,
307-
tv_usec: 0,
308-
};
309-
let mut s: [fd_set; 3] = unsafe { std::mem::zeroed() };
310-
unsafe {
311-
if !readfds.is_null() {
312-
s[0] = *readfds;
313-
}
314-
if !writefds.is_null() {
315-
s[1] = *writefds;
316-
}
317-
if !errorfds.is_null() {
318-
s[2] = *errorfds;
319-
}
320-
}
321-
let mut x = 1;
322-
let mut r;
323-
// just check poll every x ms
324-
loop {
325-
r = self
326-
.inner
327-
.select(fn_ptr, nfds, readfds, writefds, errorfds, &mut o);
328-
if r != 0 || t == 0 {
329-
break;
330-
}
331-
_ = EventLoops::wait_just(Some(Duration::from_millis(u64::from(t.min(x)))));
332-
if t != c_uint::MAX {
333-
t = if t > x { t - x } else { 0 };
334-
}
335-
if x < 16 {
336-
x <<= 1;
337-
}
338-
unsafe {
339-
if !readfds.is_null() {
340-
*readfds = s[0];
341-
}
342-
if !writefds.is_null() {
343-
*writefds = s[1];
344-
}
345-
if !errorfds.is_null() {
346-
*errorfds = s[2];
347-
}
348-
}
349-
o.tv_sec = 0;
350-
o.tv_usec = 0;
351-
}
352-
r
353-
}
354-
355-
extern "C" fn recvfrom(
356-
&self,
357-
fn_ptr: Option<
358-
&extern "C" fn(
359-
c_int,
360-
*mut c_void,
361-
size_t,
362-
c_int,
363-
*mut sockaddr,
364-
*mut socklen_t,
365-
) -> ssize_t,
366-
>,
367-
socket: c_int,
368-
buf: *mut c_void,
369-
len: size_t,
370-
flags: c_int,
371-
addr: *mut sockaddr,
372-
addrlen: *mut socklen_t,
373-
) -> ssize_t {
374-
impl_expected_read_hook!(
375-
self.inner, recvfrom, fn_ptr, socket, buf, len, flags, addr, addrlen
376-
)
377-
}
378-
379260
extern "C" fn read(
380261
&self,
381262
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t) -> ssize_t>,

0 commit comments

Comments
 (0)