Skip to content

Commit

Permalink
Add event extra information
Browse files Browse the repository at this point in the history
Foundation for more details later on.

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Aug 9, 2023
1 parent 20e04fc commit 74a162a
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 23 deletions.
34 changes: 19 additions & 15 deletions src/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ impl Poller {

poller.add(
poller.event_fd.as_raw_fd(),
Event {
key: crate::NOTIFY_KEY,
readable: true,
writable: false,
},
Event::readable(crate::NOTIFY_KEY),
PollMode::Oneshot,
)?;
}
Expand Down Expand Up @@ -177,11 +173,7 @@ impl Poller {
// Set interest in timerfd.
self.modify(
timer_fd.as_fd(),
Event {
key: crate::NOTIFY_KEY,
readable: true,
writable: false,
},
Event::readable(crate::NOTIFY_KEY),
PollMode::Oneshot,
)?;
}
Expand Down Expand Up @@ -213,11 +205,7 @@ impl Poller {
let _ = read(&self.event_fd, &mut buf);
self.modify(
self.event_fd.as_fd(),
Event {
key: crate::NOTIFY_KEY,
readable: true,
writable: false,
},
Event::readable(crate::NOTIFY_KEY),
PollMode::Oneshot,
)?;
Ok(())
Expand Down Expand Up @@ -322,6 +310,7 @@ impl Events {
key: ev.data.u64() as usize,
readable: flags.intersects(read_flags()),
writable: flags.intersects(write_flags()),
extra: EventExtra { flags },
}
})
}
Expand All @@ -336,3 +325,18 @@ impl Events {
self.list.capacity()
}
}

/// Extra information about this event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EventExtra {
flags: epoll::EventFlags,
}

impl EventExtra {
/// Create an empty version of the data.
pub fn empty() -> EventExtra {
EventExtra {
flags: epoll::EventFlags::empty(),
}
}
}
31 changes: 30 additions & 1 deletion src/iocp/afd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl AfdPollInfo {
}
}

#[derive(Default, Copy, Clone)]
#[derive(Default, Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub(super) struct AfdPollMask(u32);

Expand All @@ -91,6 +91,35 @@ impl AfdPollMask {
}
}

impl fmt::Debug for AfdPollMask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
const FLAGS: &[(&str, AfdPollMask)] = &[
("RECEIVE", AfdPollMask::RECEIVE),
("RECEIVE_EXPEDITED", AfdPollMask::RECEIVE_EXPEDITED),
("SEND", AfdPollMask::SEND),
("DISCONNECT", AfdPollMask::DISCONNECT),
("ABORT", AfdPollMask::ABORT),
("LOCAL_CLOSE", AfdPollMask::LOCAL_CLOSE),
("ACCEPT", AfdPollMask::ACCEPT),
("CONNECT_FAIL", AfdPollMask::CONNECT_FAIL),
];

let mut first = true;
for (name, value) in FLAGS {
if self.intersects(*value) {
if !first {
write!(f, " | ")?;
}

first = false;
write!(f, "{}", name)?;
}
}

Ok(())
}
}

impl ops::BitOr for AfdPollMask {
type Output = Self;

Expand Down
21 changes: 21 additions & 0 deletions src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,27 @@ impl Events {
pub(super) fn clear(&mut self) {
self.packets.clear();
}

/// The capacity of the list of events.
pub(super) fn capacity(&self) -> usize {
self.packets.capacity()
}
}

/// Extra information about an event.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct EventExtra {
/// Flags associated with this event.
flags: AfdPollMask,
}

impl EventExtra {
/// Create a new, empty version of this struct.
pub fn empty() -> EventExtra {
EventExtra {
flags: AfdPollMask::empty(),
}
}
}

/// A packet used to wake up the poller with an event.
Expand Down
12 changes: 12 additions & 0 deletions src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl Events {
writable: matches!(ev.filter(), kqueue::EventFilter::Write(..))
|| (matches!(ev.filter(), kqueue::EventFilter::Read(..))
&& (ev.flags().intersects(kqueue::EventFlags::EOF))),
extra: EventExtra,
})
}

Expand All @@ -261,6 +262,17 @@ impl Events {
}
}

/// Extra information associated with an event.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct EventExtra;

impl EventExtra {
/// Create a new, empty version of this struct.
pub fn empty() -> EventExtra {
EventExtra
}
}

pub(crate) fn mode_to_flags(mode: PollMode) -> kqueue::EventFlags {
use kqueue::EventFlags as EV;

Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub struct Event {
pub readable: bool,
/// Can it do a write operation without blocking?
pub writable: bool,
/// System-specific event data.
extra: sys::EventExtra,
}

/// The mode in which the poller waits for I/O events.
Expand Down Expand Up @@ -187,6 +189,7 @@ impl Event {
key,
readable: true,
writable: true,
extra: sys::EventExtra::empty(),
}
}

Expand All @@ -198,6 +201,7 @@ impl Event {
key,
readable: true,
writable: false,
extra: sys::EventExtra::empty(),
}
}

Expand All @@ -209,6 +213,7 @@ impl Event {
key,
readable: false,
writable: true,
extra: sys::EventExtra::empty(),
}
}

Expand All @@ -220,6 +225,7 @@ impl Event {
key,
readable: false,
writable: false,
extra: sys::EventExtra::empty(),
}
}
}
Expand Down
26 changes: 23 additions & 3 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ impl Poller {
let poll_fd = &mut fds.poll_fds[fd_data.poll_fds_index];
if !poll_fd.revents().is_empty() {
// Store event
let revents = poll_fd.revents();
events.inner.push(Event {
key: fd_data.key,
readable: poll_fd.revents().intersects(read_events()),
writable: poll_fd.revents().intersects(write_events()),
readable: revents.intersects(read_events()),
writable: revents.intersects(write_events()),
extra: EventExtra { flags: revents },
});
// Remove interest if necessary
if fd_data.remove {
Expand Down Expand Up @@ -365,7 +367,9 @@ pub struct Events {
impl Events {
/// Creates an empty list.
pub fn with_capacity(cap: usize) -> Events {
Self { inner: Vec::with_capacity(cap) }
Self {
inner: Vec::with_capacity(cap),
}
}

/// Iterates over I/O events.
Expand All @@ -384,6 +388,22 @@ impl Events {
}
}

/// Extra information associated with an event.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct EventExtra {
/// Flags associated with this event.
flags: PollFlags,
}

impl EventExtra {
/// Creates an empty set of extra information.
pub fn empty() -> Self {
Self {
flags: PollFlags::empty(),
}
}
}

fn cvt_mode_as_remove(mode: PollMode) -> io::Result<bool> {
match mode {
PollMode::Oneshot => Ok(true),
Expand Down
28 changes: 24 additions & 4 deletions src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,14 @@ impl Events {

/// Iterates over I/O events.
pub fn iter(&self) -> impl Iterator<Item = Event> + '_ {
self.list.iter().map(|ev| Event {
key: ev.userdata() as usize,
readable: PollFlags::from_bits_truncate(ev.events() as _).intersects(read_flags()),
writable: PollFlags::from_bits_truncate(ev.events() as _).intersects(write_flags()),
self.list.iter().map(|ev| {
let flags = PollFlags::from_bits_truncate(ev.events() as _);
Event {
key: ev.userdata() as usize,
readable: flags.intersects(read_flags()),
writable: flags.intersects(write_flags()),
extra: EventExtra { flags },
}
})
}

Expand All @@ -206,3 +210,19 @@ impl Events {
self.list.capacity()
}
}

/// Extra information associated with an event.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct EventExtra {
/// Flags associated with this event.
flags: PollFlags,
}

impl EventExtra {
/// Create a new, empty version of this struct.
pub fn empty() -> EventExtra {
EventExtra {
flags: PollFlags::empty(),
}
}
}

0 comments on commit 74a162a

Please sign in to comment.