Skip to content

Commit

Permalink
Add Interest::PRIORITY
Browse files Browse the repository at this point in the history
This adds a PRIORITY interest, that is supported on linux. This makes
priority events work on TcpStream again.
  • Loading branch information
poeschel committed Feb 2, 2023
1 parent db776d9 commit 282edd3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const AIO: u8 = 0b0100;
#[cfg_attr(not(target_os = "freebsd"), allow(dead_code))]
const LIO: u8 = 0b1000;

const PRIORITY: u8 = 0b10000;

impl Interest {
/// Returns a `Interest` set representing readable interests.
pub const READABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(READABLE) });
Expand All @@ -53,6 +55,9 @@ impl Interest {
#[cfg(target_os = "freebsd")]
pub const LIO: Interest = Interest(unsafe { NonZeroU8::new_unchecked(LIO) });

/// Returns a `Interest` set representing priority interests.
pub const PRIORITY: Interest = Interest(unsafe { NonZeroU8::new_unchecked(PRIORITY) });

/// Add together two `Interest`.
///
/// This does the same thing as the `BitOr` implementation, but is a
Expand Down Expand Up @@ -113,6 +118,11 @@ impl Interest {
pub const fn is_lio(self) -> bool {
(self.0.get() & LIO) != 0
}

/// Returns true if `Interest` contains PRIORITY readiness
pub const fn is_priority(self) -> bool {
(self.0.get() & PRIORITY) != 0
}
}

impl ops::BitOr for Interest {
Expand Down Expand Up @@ -173,6 +183,13 @@ impl fmt::Debug for Interest {
one = true
}
}
if self.is_priority() {
if one {
write!(fmt, " | ")?
}
write!(fmt, "PRIORITY")?;
one = true
}
debug_assert!(one, "printing empty interests");
Ok(())
}
Expand Down
6 changes: 5 additions & 1 deletion src/sys/unix/selector/epoll.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Interest, Token};

use libc::{EPOLLET, EPOLLIN, EPOLLOUT, EPOLLRDHUP};
use libc::{EPOLLET, EPOLLIN, EPOLLOUT, EPOLLPRI, EPOLLRDHUP};
use log::error;
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -177,6 +177,10 @@ fn interests_to_epoll(interests: Interest) -> u32 {
kind |= EPOLLOUT;
}

if interests.is_priority() {
kind |= EPOLLPRI;
}

kind as u32
}

Expand Down
8 changes: 6 additions & 2 deletions tests/tcp_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,11 @@ fn pollpri() {

let (mut sock, _) = listener.accept().unwrap();
poll.registry()
.register(&mut sock, Token(1), Interest::READABLE)
.register(
&mut sock,
Token(1),
Interest::READABLE.add(Interest::PRIORITY),
)
.unwrap();

// MsgFlags::MSG_OOB sends a tcp packet which is received by linux kernel with
Expand All @@ -841,6 +845,6 @@ fn pollpri() {
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(Token(1), Readiness::PRIORITY)],
vec![ExpectEvent::new(Token(1), Interest::PRIORITY)],
);
}
3 changes: 3 additions & 0 deletions tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ impl From<Interest> for Readiness {
if interests.is_lio() {
readiness.0 |= LIO;
}
if interests.is_priority() {
readiness.0 |= PRIORITY;
}
readiness
}
}
Expand Down

0 comments on commit 282edd3

Please sign in to comment.