Skip to content

Commit

Permalink
more conditional compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Mar 21, 2023
1 parent 03d1758 commit a2a6f7e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
4 changes: 4 additions & 0 deletions tokio/src/io/async_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ pub struct AsyncFdReadyMutGuard<'a, T: AsRawFd> {
event: Option<ReadyEvent>,
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
const ALL_INTEREST: Interest = Interest::READABLE.add(Interest::WRITABLE);

#[cfg(any(target_os = "linux", target_os = "android"))]
const ALL_INTEREST: Interest = Interest::READABLE
.add(Interest::WRITABLE)
.add(Interest::PRIORITY);
Expand Down
3 changes: 3 additions & 0 deletions tokio/src/io/interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ impl Interest {
/// let both = Interest::READABLE | Interest::PRIORITY;
/// assert!(both.is_priority());
/// ```
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
pub const fn is_priority(self) -> bool {
self.0.is_priority()
}
Expand Down Expand Up @@ -126,6 +128,7 @@ impl Interest {
match self {
Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED,
Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED,
#[cfg(any(target_os = "linux", target_os = "android"))]
Interest::PRIORITY => Ready::PRIORITY,
_ => Ready::EMPTY,
}
Expand Down
13 changes: 9 additions & 4 deletions tokio/src/io/poll_evented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ cfg_io_driver! {

// ===== impl PollEvented =====

#[cfg(not(any(target_os = "linux", target_os = "android")))]
const ALL_INTEREST: Interest = Interest::READABLE.add(Interest::WRITABLE);

#[cfg(any(target_os = "linux", target_os = "android"))]
const ALL_INTEREST: Interest = Interest::READABLE
.add(Interest::WRITABLE)
.add(Interest::PRIORITY);

impl<E: Source> PollEvented<E> {
/// Creates a new `PollEvented` associated with the default reactor.
///
Expand All @@ -83,10 +91,7 @@ impl<E: Source> PollEvented<E> {
#[track_caller]
#[cfg_attr(feature = "signal", allow(unused))]
pub(crate) fn new(io: E) -> io::Result<Self> {
PollEvented::new_with_interest(
io,
Interest::READABLE | Interest::WRITABLE | Interest::PRIORITY,
)
PollEvented::new_with_interest(io, ALL_INTEREST)
}

/// Creates a new `PollEvented` associated with the default reactor, for
Expand Down
43 changes: 34 additions & 9 deletions tokio/src/io/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const READABLE: usize = 0b0_01;
const WRITABLE: usize = 0b0_10;
const READ_CLOSED: usize = 0b0_0100;
const WRITE_CLOSED: usize = 0b0_1000;
#[cfg(any(target_os = "linux", target_os = "android"))]
const PRIORITY: usize = 0b1_0000;

/// Describes the readiness state of an I/O resources.
Expand All @@ -33,11 +34,18 @@ impl Ready {
pub const WRITE_CLOSED: Ready = Ready(WRITE_CLOSED);

/// Returns a `Ready` representing priority readiness.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
pub const PRIORITY: Ready = Ready(PRIORITY);

/// Returns a `Ready` representing readiness for all operations.
#[cfg(any(target_os = "linux", target_os = "android"))]
pub const ALL: Ready = Ready(READABLE | WRITABLE | READ_CLOSED | WRITE_CLOSED | PRIORITY);

/// Returns a `Ready` representing readiness for all operations.
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub const ALL: Ready = Ready(READABLE | WRITABLE | READ_CLOSED | WRITE_CLOSED);

// Must remain crate-private to avoid adding a public dependency on Mio.
pub(crate) fn from_mio(event: &mio::event::Event) -> Ready {
let mut ready = Ready::EMPTY;
Expand Down Expand Up @@ -69,8 +77,11 @@ impl Ready {
ready |= Ready::WRITE_CLOSED;
}

if event.is_priority() {
ready |= Ready::PRIORITY;
#[cfg(any(target_os = "linux", target_os = "android"))]
{
if event.is_priority() {
ready |= Ready::PRIORITY;
}
}

ready
Expand Down Expand Up @@ -216,6 +227,7 @@ cfg_io_readiness! {
ready |= Ready::WRITE_CLOSED;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if interest.is_priority() {
ready |= Ready::PRIORITY;
}
Expand Down Expand Up @@ -269,12 +281,25 @@ impl ops::Sub<Ready> for Ready {

impl fmt::Debug for Ready {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Ready")
.field("is_readable", &self.is_readable())
.field("is_writable", &self.is_writable())
.field("is_read_closed", &self.is_read_closed())
.field("is_write_closed", &self.is_write_closed())
.field("is_priority", &self.is_priority())
.finish()
#[cfg(any(target_os = "linux", target_os = "android"))]
{
fmt.debug_struct("Ready")
.field("is_readable", &self.is_readable())
.field("is_writable", &self.is_writable())
.field("is_read_closed", &self.is_read_closed())
.field("is_write_closed", &self.is_write_closed())
.field("is_priority", &self.is_priority())
.finish()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
{
fmt.debug_struct("Ready")
.field("is_readable", &self.is_readable())
.field("is_writable", &self.is_writable())
.field("is_read_closed", &self.is_read_closed())
.field("is_write_closed", &self.is_write_closed())
.finish()
}
}
}
4 changes: 1 addition & 3 deletions tokio/tests/tcp_priority.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", not(tokio_wasi)))] // Wasi doesn't support bind
#![cfg(all(feature = "full", any(target_os = "linux", target_os = "android")))]

use std::os::unix::io::AsRawFd;
use tokio::io::{self, Interest};
use tokio::net::{TcpListener, TcpStream};

#[tokio::test]
#[cfg(any(target_os = "linux", target_os = "android"))]
async fn await_priority() {
use std::net::SocketAddr;

Expand All @@ -25,7 +24,6 @@ async fn await_priority() {
let _ = client.ready(Interest::PRIORITY).await.unwrap();
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn send_oob_data<S: AsRawFd>(stream: &S, data: &[u8]) -> io::Result<usize> {
unsafe {
let res = libc::send(
Expand Down

0 comments on commit a2a6f7e

Please sign in to comment.