#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux;
#[cfg(ring)]
pub(crate) mod ring;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(crate) use self::linux::{Notifier, Poller, Registrar};
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))]
mod kqueue;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))]
pub(crate) use self::kqueue::{Notifier, Poller, Registrar};
#[cfg(windows)]
pub(crate) mod iocp;
#[cfg(windows)]
pub(crate) use self::iocp::{Notifier, Poller};
#[cfg(all(test, not(any(target_os = "linux", target_os = "android"))))]
thread_local! {
static FAIL_NOTIFIES: core::cell::Cell<u32> = const { core::cell::Cell::new(0) };
}
#[cfg(all(test, not(any(target_os = "linux", target_os = "android"))))]
pub(crate) fn fail_notifies(n: u32) -> u32 {
FAIL_NOTIFIES.replace(n)
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
const NOTIFY_TRIES: u32 = 64;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn notify_until_taken(post: impl Fn() -> std::io::Result<()>) {
let mut tries = 1;
while let Err(err) = post_notification(&post) {
if tries == NOTIFY_TRIES {
log::error!(
"a sleeping worker could not be woken ({err}): it runs its new work at its next timer or IO"
);
return;
}
tries += 1;
std::thread::yield_now();
}
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[inline(always)]
fn post_notification(post: impl FnOnce() -> std::io::Result<()>) -> std::io::Result<()> {
#[cfg(test)]
if FAIL_NOTIFIES.with(|left| {
let fails = left.get() > 0;
left.set(left.get().saturating_sub(1));
fails
}) {
return Err(std::io::ErrorKind::Interrupted.into());
}
post()
}
#[cfg(readiness)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Readiness {
pub(crate) readable: bool,
pub(crate) writable: bool,
pub(crate) urgent: bool,
pub(crate) hangup: bool,
}
#[cfg(all(test, readiness))]
mod tests;