Skip to content

Commit 768647d

Browse files
author
Stjepan Glavina
committed
Delete sys module
1 parent 7ecce7f commit 768647d

File tree

3 files changed

+72
-148
lines changed

3 files changed

+72
-148
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use socket2::{Domain, Protocol, Socket, Type};
3232
use crate::parking::{Reactor, Source};
3333

3434
pub mod parking;
35-
mod sys;
3635

3736
/// Fires at the chosen point in time.
3837
///

src/parking.rs

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,9 @@ mod sys {
758758
use std::convert::TryInto;
759759
use std::io;
760760
use std::os::unix::io::RawFd;
761+
use std::ptr;
761762
use std::time::Duration;
762763

763-
use crate::sys::epoll::{
764-
epoll_create1, epoll_ctl, epoll_wait, EpollEvent, EpollFlags, EpollOp,
765-
};
766-
767764
macro_rules! syscall {
768765
($fn:ident $args:tt) => {{
769766
let res = unsafe { libc::$fn $args };
@@ -781,7 +778,42 @@ mod sys {
781778
}
782779
impl Reactor {
783780
pub fn new() -> io::Result<Reactor> {
784-
let epoll_fd = epoll_create1()?;
781+
// According to libuv, `EPOLL_CLOEXEC` is not defined on Android API < 21.
782+
// But `EPOLL_CLOEXEC` is an alias for `O_CLOEXEC` on that platform, so we use it instead.
783+
#[cfg(target_os = "android")]
784+
const CLOEXEC: libc::c_int = libc::O_CLOEXEC;
785+
#[cfg(not(target_os = "android"))]
786+
const CLOEXEC: libc::c_int = libc::EPOLL_CLOEXEC;
787+
788+
let epoll_fd = unsafe {
789+
// Check if the `epoll_create1` symbol is available on this platform.
790+
let ptr = libc::dlsym(
791+
libc::RTLD_DEFAULT,
792+
"epoll_create1\0".as_ptr() as *const libc::c_char,
793+
);
794+
795+
if ptr.is_null() {
796+
// If not, use `epoll_create` and manually set `CLOEXEC`.
797+
let fd = match libc::epoll_create(1024) {
798+
-1 => return Err(io::Error::last_os_error()),
799+
fd => fd,
800+
};
801+
let flags = libc::fcntl(fd, libc::F_GETFD);
802+
libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC);
803+
fd
804+
} else {
805+
// Use `epoll_create1` with `CLOEXEC`.
806+
let epoll_create1 = std::mem::transmute::<
807+
*mut libc::c_void,
808+
unsafe extern "C" fn(libc::c_int) -> libc::c_int,
809+
>(ptr);
810+
match epoll_create1(CLOEXEC) {
811+
-1 => return Err(io::Error::last_os_error()),
812+
fd => fd,
813+
}
814+
}
815+
};
816+
785817
let event_fd = syscall!(eventfd(0, libc::EFD_CLOEXEC | libc::EFD_NONBLOCK))?;
786818
let reactor = Reactor { epoll_fd, event_fd };
787819
reactor.register(event_fd, !0)?;
@@ -791,8 +823,12 @@ mod sys {
791823
pub fn register(&self, fd: RawFd, key: usize) -> io::Result<()> {
792824
let flags = syscall!(fcntl(fd, libc::F_GETFL))?;
793825
syscall!(fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK))?;
794-
let ev = &mut EpollEvent::new(0, key as u64);
795-
epoll_ctl(self.epoll_fd, EpollOp::EpollCtlAdd, fd, Some(ev))
826+
let mut ev = libc::epoll_event {
827+
events: 0,
828+
u64: key as u64,
829+
};
830+
syscall!(epoll_ctl(self.epoll_fd, libc::EPOLL_CTL_ADD, fd, &mut ev))?;
831+
Ok(())
796832
}
797833
pub fn reregister(&self, fd: RawFd, key: usize, read: bool, write: bool) -> io::Result<()> {
798834
let mut flags = libc::EPOLLONESHOT;
@@ -802,11 +838,21 @@ mod sys {
802838
if write {
803839
flags |= write_flags();
804840
}
805-
let ev = &mut EpollEvent::new(flags, key as u64);
806-
epoll_ctl(self.epoll_fd, EpollOp::EpollCtlMod, fd, Some(ev))
841+
let mut ev = libc::epoll_event {
842+
events: flags as _,
843+
u64: key as u64,
844+
};
845+
syscall!(epoll_ctl(self.epoll_fd, libc::EPOLL_CTL_MOD, fd, &mut ev))?;
846+
Ok(())
807847
}
808848
pub fn deregister(&self, fd: RawFd) -> io::Result<()> {
809-
epoll_ctl(self.epoll_fd, EpollOp::EpollCtlDel, fd, None)
849+
syscall!(epoll_ctl(
850+
self.epoll_fd,
851+
libc::EPOLL_CTL_DEL,
852+
fd,
853+
ptr::null_mut()
854+
))?;
855+
Ok(())
810856
}
811857
pub fn wait(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<usize> {
812858
let timeout_ms = timeout
@@ -819,7 +865,14 @@ mod sys {
819865
})
820866
.and_then(|t| t.as_millis().try_into().ok())
821867
.unwrap_or(-1);
822-
events.len = epoll_wait(self.epoll_fd, &mut events.list, timeout_ms)?;
868+
869+
let res = syscall!(epoll_wait(
870+
self.epoll_fd,
871+
events.list.as_mut_ptr() as *mut libc::epoll_event,
872+
events.list.len() as libc::c_int,
873+
timeout_ms as libc::c_int,
874+
))?;
875+
events.len = res as usize;
823876

824877
let mut buf = [0u8; 8];
825878
let _ = syscall!(read(
@@ -841,28 +894,29 @@ mod sys {
841894
Ok(())
842895
}
843896
}
844-
fn read_flags() -> EpollFlags {
897+
fn read_flags() -> libc::c_int {
845898
libc::EPOLLIN | libc::EPOLLRDHUP | libc::EPOLLHUP | libc::EPOLLERR | libc::EPOLLPRI
846899
}
847-
fn write_flags() -> EpollFlags {
900+
fn write_flags() -> libc::c_int {
848901
libc::EPOLLOUT | libc::EPOLLHUP | libc::EPOLLERR
849902
}
850903

851904
pub struct Events {
852-
list: Box<[EpollEvent]>,
905+
list: Box<[libc::epoll_event]>,
853906
len: usize,
854907
}
855908
impl Events {
856909
pub fn new() -> Events {
857-
let list = vec![EpollEvent::empty(); 1000].into_boxed_slice();
910+
let ev = libc::epoll_event { events: 0, u64: 0 };
911+
let list = vec![ev; 1000].into_boxed_slice();
858912
let len = 0;
859913
Events { list, len }
860914
}
861915
pub fn iter(&self) -> impl Iterator<Item = Event> + '_ {
862916
self.list[..self.len].iter().map(|ev| Event {
863-
readable: (ev.events() & read_flags()) != 0,
864-
writable: (ev.events() & write_flags()) != 0,
865-
key: ev.data() as usize,
917+
readable: (ev.events as libc::c_int & read_flags()) != 0,
918+
writable: (ev.events as libc::c_int & write_flags()) != 0,
919+
key: ev.u64 as usize,
866920
})
867921
}
868922
}

src/sys.rs

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)