From 3fb29e3c92a6c9905e52ba344fdec247017ef161 Mon Sep 17 00:00:00 2001 From: Noah Kennedy Date: Wed, 28 Aug 2024 10:41:29 +0100 Subject: [PATCH] rt: expose io driver fd This change exposes the IO driver fd, used by our epoll, kqueue, uring, etc instance. This is exposed so that users can watch it from another epoll/kqueue/io_uring/poll setup, and for no other reason. It is not valid to do anything other than watch this for readiness. --- tokio/src/runtime/io/driver.rs | 6 ++++++ tokio/src/runtime/runtime.rs | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/tokio/src/runtime/io/driver.rs b/tokio/src/runtime/io/driver.rs index 0f7b1e57a..1dd93427f 100644 --- a/tokio/src/runtime/io/driver.rs +++ b/tokio/src/runtime/io/driver.rs @@ -13,6 +13,7 @@ use crate::runtime::io::{IoDriverMetrics, RegistrationSet, ScheduledIo}; use mio::event::Source; use std::fmt; use std::io; +use std::os::fd::AsRawFd; use std::sync::Arc; use std::time::Duration; @@ -209,6 +210,11 @@ impl Handle { self.waker.wake().expect("failed to wake I/O driver"); } + #[cfg(unix)] + pub(crate) fn get_raw_poll_fd(&self) -> std::os::fd::RawFd { + self.registry.as_raw_fd() + } + /// Registers an I/O resource with the reactor for a given `mio::Ready` state. /// /// The registration token is returned. diff --git a/tokio/src/runtime/runtime.rs b/tokio/src/runtime/runtime.rs index 16f8718ab..5ac7632d8 100644 --- a/tokio/src/runtime/runtime.rs +++ b/tokio/src/runtime/runtime.rs @@ -475,6 +475,17 @@ impl Runtime { pub fn metrics(&self) -> crate::runtime::RuntimeMetrics { self.handle.metrics() } + + /// Gets a raw fd for the IO driver which can be polled. + /// + /// Do NOT use this for anything other than watching it with epoll/kqueue/poll. + /// + /// # Safety + /// Don't use this after the runtime goes away. + #[cfg(unix)] + pub unsafe fn get_raw_poll_fd(&self) -> Option { + Some(self.handle.inner.driver().io.as_ref()?.get_raw_poll_fd()) + } } #[allow(clippy::single_match)] // there are comments in the error branch, so we don't want if-let