Skip to content

Commit

Permalink
Move waker cfg attributes to sys::unix module
Browse files Browse the repository at this point in the history
Same as what we did for the selector.

But for the waker it's a little more complicated due to the poll(2)
implementation. That needs to wrap the Waker type to use it's internal
waker, compared to kqueue and epoll which just reexport the Waker
directly.

This logical of what waker to use is now moved to the selector module.
For kqueue and epoll it's a simple export, for poll it's a special type
that call the internal waker. This special poll waker used to be the
fdbased waker.

All these changes (hopefully) add up to no changes at all. All the
supported targets should continue to work with all configurations.
However it should be easier to port to new targets without having to
deal with the clusterfuck that was the old sys::unix::waker module.
  • Loading branch information
Thomasdezeeuw committed Jun 24, 2024
1 parent f3863f1 commit 1133ed0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 207 deletions.
54 changes: 52 additions & 2 deletions src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ cfg_os_poll! {

#[cfg_attr(all(
not(mio_unsupported_force_waker_pipe),
any(
target_os = "android",
target_os = "espidf",
target_os = "fuchsia",
target_os = "hermit",
target_os = "illumos",
target_os = "linux",
)
), path = "waker/eventfd.rs")]
#[cfg_attr(all(
not(mio_unsupported_force_waker_pipe),
not(mio_unsupported_force_poll_poll), // `kqueue(2)` based waker doesn't work with `poll(2)`.
any(
target_os = "freebsd",
target_os = "ios",
Expand All @@ -62,8 +74,34 @@ cfg_os_poll! {
target_os = "watchos",
)
), path = "waker/kqueue.rs")]
#[cfg_attr(any(
// NOTE: also add to the list list for the `pipe` module below.
mio_unsupported_force_waker_pipe,
all(
// `kqueue(2)` based waker doesn't work with `poll(2)`.
mio_unsupported_force_poll_poll,
any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
),
),
target_os = "aix",
target_os = "dragonfly",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "vita",
), path = "waker/pipe.rs")]
mod waker;
pub(crate) use self::waker::Waker;
// NOTE: the `Waker` type is expected in the selector module as the
// `poll(2)` implementation needs to do some special stuff.

mod sourcefd;
#[cfg(feature = "os-ext")]
Expand All @@ -83,10 +121,22 @@ cfg_os_poll! {
all(feature = "os-ext", not(target_os = "hermit")),
// For the `Waker` type based on a pipe.
mio_unsupported_force_waker_pipe,
all(
// `kqueue(2)` based waker doesn't work with `poll(2)`.
mio_unsupported_force_poll_poll,
any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
),
),
// NOTE: also add to the list list for the `pipe` module below.
target_os = "aix",
target_os = "dragonfly",
target_os = "haiku",
target_os = "illumos",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd",
Expand Down
3 changes: 3 additions & 0 deletions src/sys/unix/selector/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ pub mod event {
}
}

// No special requirement from the implementation around waking.
pub(crate) use crate::sys::unix::waker::Waker;

cfg_io_source! {
mod stateless_io_source;
pub(crate) use stateless_io_source::IoSourceState;
Expand Down
3 changes: 3 additions & 0 deletions src/sys/unix/selector/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,9 @@ pub mod event {
}
}

// No special requirement from the implementation around waking.
pub(crate) use crate::sys::unix::waker::Waker;

cfg_io_source! {
mod stateless_io_source;
pub(crate) use stateless_io_source::IoSourceState;
Expand Down
23 changes: 21 additions & 2 deletions src/sys/unix/selector/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
use std::{cmp, fmt, io};

use crate::sys::unix::waker::WakerInternal;
use crate::sys::unix::waker::Waker as WakerInternal;
use crate::{Interest, Token};

/// Unique id for use as `SelectorId`.
Expand Down Expand Up @@ -161,7 +161,7 @@ struct FdData {

impl SelectorState {
pub fn new() -> io::Result<SelectorState> {
let notify_waker = WakerInternal::new()?;
let notify_waker = WakerInternal::new_unregistered()?;

Ok(Self {
fds: Mutex::new(Fds {
Expand Down Expand Up @@ -625,6 +625,25 @@ pub mod event {
}
}

#[derive(Debug)]
pub(crate) struct Waker {
selector: Selector,
token: Token,
}

impl Waker {
pub(crate) fn new(selector: &Selector, token: Token) -> io::Result<Waker> {
Ok(Waker {
selector: selector.try_clone()?,
token,
})
}

pub(crate) fn wake(&self) -> io::Result<()> {
self.selector.wake(self.token)
}
}

cfg_io_source! {
use crate::Registry;

Expand Down
188 changes: 0 additions & 188 deletions src/sys/unix/waker.rs

This file was deleted.

8 changes: 2 additions & 6 deletions src/sys/unix/waker/eventfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Waker {
}

impl Waker {
#[allow(dead_code)] // Not used by the `poll(2)` implementation.
pub(crate) fn new(selector: &Selector, token: Token) -> io::Result<Waker> {
let waker = Waker::new_unregistered()?;
selector.register(waker.fd.as_raw_fd(), token, Interest::READABLE)?;
Expand Down Expand Up @@ -54,12 +55,7 @@ impl Waker {
}
}

#[cfg(any(
mio_unsupported_force_poll_poll,
target_os = "espidf",
target_os = "fuchsia",
target_os = "hermit",
))]
#[allow(dead_code)] // Only used by the `poll(2)` implementation.
pub(crate) fn ack_and_reset(&self) {
let _ = self.reset();
}
Expand Down
12 changes: 3 additions & 9 deletions src/sys/unix/waker/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ pub(crate) struct Waker {
}

impl Waker {
#[allow(dead_code)] // Not used by the `poll(2)` implementation.
pub(crate) fn new(selector: &Selector, token: Token) -> io::Result<Waker> {
let waker = Waker::new_unregistered()?;
selector.register(waker.fd.as_raw_fd(), token, Interest::READABLE)?;
selector.register(waker.receiver.as_raw_fd(), token, Interest::READABLE)?;
Ok(waker)
}

Expand Down Expand Up @@ -55,14 +56,7 @@ impl Waker {
}
}

#[cfg(any(
mio_unsupported_force_poll_poll,
target_os = "espidf",
target_os = "haiku",
target_os = "nto",
target_os = "solaris",
target_os = "vita",
))]
#[allow(dead_code)] // Only used by the `poll(2)` implementation.
pub(crate) fn ack_and_reset(&self) {
self.empty();
}
Expand Down

0 comments on commit 1133ed0

Please sign in to comment.