use super::super::c;
use super::super::conv::{ret, ret_owned_fd, ret_u32};
use crate::fd::{AsFd, AsRawFd, OwnedFd};
use crate::io;
use alloc::vec::Vec;
use bitflags::bitflags;
use core::convert::TryInto;
use core::ptr::null_mut;
bitflags! {
pub struct CreateFlags: c::c_int {
const CLOEXEC = c::EPOLL_CLOEXEC;
}
}
bitflags! {
#[derive(Default)]
pub struct EventFlags: u32 {
const IN = c::EPOLLIN as u32;
const OUT = c::EPOLLOUT as u32;
const PRI = c::EPOLLPRI as u32;
const ERR = c::EPOLLERR as u32;
const HUP = c::EPOLLHUP as u32;
const RDNORM = c::EPOLLRDNORM as u32;
const RDBAND = c::EPOLLRDBAND as u32;
const WRNORM = c::EPOLLWRNORM as u32;
const WRBAND = c::EPOLLWRBAND as u32;
const MSG = c::EPOLLMSG as u32;
const RDHUP = c::EPOLLRDHUP as u32;
const ET = c::EPOLLET as u32;
const ONESHOT = c::EPOLLONESHOT as u32;
const WAKEUP = c::EPOLLWAKEUP as u32;
#[cfg(not(target_os = "android"))]
const EXCLUSIVE = c::EPOLLEXCLUSIVE as u32;
}
}
#[inline]
#[doc(alias = "epoll_create1")]
pub fn epoll_create(flags: CreateFlags) -> io::Result<OwnedFd> {
unsafe { ret_owned_fd(c::epoll_create1(flags.bits())) }
}
#[doc(alias = "epoll_ctl")]
pub fn epoll_add(
epoll: impl AsFd,
source: impl AsFd,
data: u64,
event_flags: EventFlags,
) -> io::Result<()> {
unsafe {
let raw_fd = source.as_fd().as_raw_fd();
ret(c::epoll_ctl(
epoll.as_fd().as_raw_fd(),
c::EPOLL_CTL_ADD,
raw_fd,
&mut c::epoll_event {
events: event_flags.bits(),
r#u64: data,
},
))
}
}
#[doc(alias = "epoll_ctl")]
pub fn epoll_mod(
epoll: impl AsFd,
source: impl AsFd,
data: u64,
event_flags: EventFlags,
) -> io::Result<()> {
let raw_fd = source.as_fd().as_raw_fd();
unsafe {
ret(c::epoll_ctl(
epoll.as_fd().as_raw_fd(),
c::EPOLL_CTL_MOD,
raw_fd,
&mut c::epoll_event {
events: event_flags.bits(),
r#u64: data,
},
))
}
}
#[doc(alias = "epoll_ctl")]
pub fn epoll_del(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
unsafe {
let raw_fd = source.as_fd().as_raw_fd();
ret(c::epoll_ctl(
epoll.as_fd().as_raw_fd(),
c::EPOLL_CTL_DEL,
raw_fd,
null_mut(),
))
}
}
pub fn epoll_wait(
epoll: impl AsFd,
event_list: &mut EventVec,
timeout: c::c_int,
) -> io::Result<()> {
unsafe {
event_list.events.set_len(0);
let nfds = ret_u32(c::epoll_wait(
epoll.as_fd().as_raw_fd(),
event_list.events.as_mut_ptr().cast::<c::epoll_event>(),
event_list.events.capacity().try_into().unwrap_or(i32::MAX),
timeout,
))?;
event_list.events.set_len(nfds as usize);
}
Ok(())
}
pub struct Iter<'a> {
iter: core::slice::Iter<'a, Event>,
}
impl<'a> Iterator for Iter<'a> {
type Item = (EventFlags, u64);
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|event| (event.event_flags, event.data))
}
}
#[repr(C)]
#[cfg_attr(
any(
all(
target_arch = "x86",
not(target_env = "musl"),
not(target_os = "android"),
),
target_arch = "x86_64",
),
repr(packed)
)]
struct Event {
event_flags: EventFlags,
data: u64,
}
pub struct EventVec {
events: Vec<Event>,
}
impl EventVec {
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self {
events: Vec::with_capacity(capacity),
}
}
#[inline]
pub fn capacity(&self) -> usize {
self.events.capacity()
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.events.reserve(additional);
}
#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.events.reserve_exact(additional);
}
#[inline]
pub fn clear(&mut self) {
self.events.clear();
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.events.shrink_to_fit();
}
#[inline]
pub fn iter(&self) -> Iter<'_> {
Iter {
iter: self.events.iter(),
}
}
#[inline]
pub fn len(&mut self) -> usize {
self.events.len()
}
#[inline]
pub fn is_empty(&mut self) -> bool {
self.events.is_empty()
}
}
impl<'a> IntoIterator for &'a EventVec {
type IntoIter = Iter<'a>;
type Item = (EventFlags, u64);
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}