Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver: add support for registering file descriptors with user-specified flags #6089

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
9 changes: 8 additions & 1 deletion tokio/src/io/async_fd.rs
Expand Up @@ -246,9 +246,16 @@ impl<T: AsRawFd> AsyncFd<T> {
/// Create a new AsyncFd with the provided raw epoll flags for registration.
///
/// These flags replace any epoll flags would normally set when registering the fd.
///
/// **Note**: This is an [unstable API][unstable]. The public API of this may break in 1.x
/// releases.
/// See [the documentation on unstable features][unstable] for details.
///
/// [unstable]: crate#unstable-features
#[track_caller]
#[cfg(all(target_os = "linux", tokio_unstable))]
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
pub fn with_flags(inner: T, flags: u32) -> io::Result<Self>
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)), doc(cfg(target_os = "linux")))]
Noah-Kennedy marked this conversation as resolved.
Show resolved Hide resolved
pub fn with_epoll_flags(inner: T, flags: u32) -> io::Result<Self>
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
where
T: AsRawFd,
{
Expand Down
12 changes: 11 additions & 1 deletion tokio/src/net/tcp/listener.rs
Expand Up @@ -243,9 +243,19 @@ impl TcpListener {
/// Create a new TcpListener with the provided raw epoll flags.
///
/// These flags replace any epoll flags would normally set when registering the fd.
///
/// **Note**: This is an [unstable API][unstable]. The public API of this may break in 1.x
/// releases.
/// See [the documentation on unstable features][unstable] for details.
///
/// [unstable]: crate#unstable-features
#[track_caller]
#[cfg(all(target_os = "linux", tokio_unstable))]
pub fn from_std_with_flags(listener: net::TcpListener, flags: u32) -> io::Result<TcpListener> {
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)), doc(cfg(target_os = "linux")))]
pub fn from_std_with_epoll_flags(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not have these methods on TcpListener. Consider adding them to TcpSocket instead as that is the socket builder API.

listener: net::TcpListener,
flags: u32,
) -> io::Result<TcpListener> {
let io = mio::net::TcpListener::from_std(listener);
let io = PollEvented::new_raw(io, flags)?;
Ok(TcpListener { io })
Expand Down
9 changes: 8 additions & 1 deletion tokio/src/net/unix/listener.rs
Expand Up @@ -117,9 +117,16 @@ impl UnixListener {
/// Create a new UnixListener with the provided raw epoll flags.
///
/// These flags replace any epoll flags would normally set when registering the fd.
///
/// **Note**: This is an [unstable API][unstable]. The public API of this may break in 1.x
/// releases.
/// See [the documentation on unstable features][unstable] for details.
///
/// [unstable]: crate#unstable-features
#[track_caller]
#[cfg(all(target_os = "linux", tokio_unstable))]
pub fn from_std_with_flags(
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)), doc(cfg(target_os = "linux")))]
pub fn from_std_with_epoll_flags(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the TCP method is added to TcpSocket, this will need to be removed and either skipped initially or we will need a UnixSocket API.

listener: net::UnixListener,
flags: u32,
) -> io::Result<UnixListener> {
Expand Down
2 changes: 1 addition & 1 deletion tokio/tests/epollexclusive.rs
Expand Up @@ -84,7 +84,7 @@ fn count_accepts(std: std::net::TcpListener, flags: u32, barrier: Arc<Barrier>)
rt.block_on(async {
std.set_nonblocking(true).unwrap();

let listener = tokio::net::TcpListener::from_std_with_flags(std, flags).unwrap();
let listener = tokio::net::TcpListener::from_std_with_epoll_flags(std, flags).unwrap();

barrier.wait().await;

Expand Down