From 282edd32c7459d14cd4efc0ecafd652ab806f30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20P=C3=B6schel?= Date: Mon, 30 Jan 2023 14:09:07 +0100 Subject: [PATCH] Add Interest::PRIORITY This adds a PRIORITY interest, that is supported on linux. This makes priority events work on TcpStream again. --- src/interest.rs | 17 +++++++++++++++++ src/sys/unix/selector/epoll.rs | 6 +++++- tests/tcp_stream.rs | 8 ++++++-- tests/util/mod.rs | 3 +++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/interest.rs b/src/interest.rs index 0aa0bda89..84d25b6bd 100644 --- a/src/interest.rs +++ b/src/interest.rs @@ -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) }); @@ -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 @@ -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 { @@ -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(()) } diff --git a/src/sys/unix/selector/epoll.rs b/src/sys/unix/selector/epoll.rs index 8bad3830d..1809a2bb1 100644 --- a/src/sys/unix/selector/epoll.rs +++ b/src/sys/unix/selector/epoll.rs @@ -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)] @@ -177,6 +177,10 @@ fn interests_to_epoll(interests: Interest) -> u32 { kind |= EPOLLOUT; } + if interests.is_priority() { + kind |= EPOLLPRI; + } + kind as u32 } diff --git a/tests/tcp_stream.rs b/tests/tcp_stream.rs index b8a845c7c..a9af46729 100644 --- a/tests/tcp_stream.rs +++ b/tests/tcp_stream.rs @@ -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 @@ -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)], ); } diff --git a/tests/util/mod.rs b/tests/util/mod.rs index db667c04d..1a1e1c421 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -130,6 +130,9 @@ impl From for Readiness { if interests.is_lio() { readiness.0 |= LIO; } + if interests.is_priority() { + readiness.0 |= PRIORITY; + } readiness } }