From f3863f1b9bd2e961e33a4c5be75420f81de8bea2 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Fri, 21 Jun 2024 16:52:31 +0200 Subject: [PATCH] Add new_unregistered function to fd based wakers To be used in the poll implementation. --- src/sys/unix/waker/eventfd.rs | 12 ++++++++++-- src/sys/unix/waker/pipe.rs | 10 +++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sys/unix/waker/eventfd.rs b/src/sys/unix/waker/eventfd.rs index 2b53d1c56..66583d17b 100644 --- a/src/sys/unix/waker/eventfd.rs +++ b/src/sys/unix/waker/eventfd.rs @@ -7,6 +7,9 @@ use std::os::fd::{AsRawFd, FromRawFd, RawFd}; #[cfg(target_os = "hermit")] use std::os::hermit::io::{AsRawFd, FromRawFd, RawFd}; +use crate::sys::Selector; +use crate::{Interest, Token}; + /// Waker backed by `eventfd`. /// /// `eventfd` is effectively an 64 bit counter. All writes must be of 8 @@ -19,14 +22,19 @@ pub(crate) struct Waker { } impl Waker { - pub(crate) fn new() -> io::Result { + pub(crate) fn new(selector: &Selector, token: Token) -> io::Result { + let waker = Waker::new_unregistered()?; + selector.register(waker.fd.as_raw_fd(), token, Interest::READABLE)?; + Ok(waker) + } + + pub(crate) fn new_unregistered() -> io::Result { #[cfg(not(target_os = "espidf"))] let flags = libc::EFD_CLOEXEC | libc::EFD_NONBLOCK; // ESP-IDF is EFD_NONBLOCK by default and errors if you try to pass this flag. #[cfg(target_os = "espidf")] let flags = 0; let fd = syscall!(eventfd(0, flags))?; - let file = unsafe { File::from_raw_fd(fd) }; Ok(Waker { fd: file }) } diff --git a/src/sys/unix/waker/pipe.rs b/src/sys/unix/waker/pipe.rs index ab001849a..b0799016e 100644 --- a/src/sys/unix/waker/pipe.rs +++ b/src/sys/unix/waker/pipe.rs @@ -8,6 +8,8 @@ use std::os::fd::{AsRawFd, FromRawFd, RawFd}; use std::os::hermit::io::{AsRawFd, FromRawFd, RawFd}; use crate::sys::unix::pipe; +use crate::sys::Selector; +use crate::{Interest, Token}; /// Waker backed by a unix pipe. /// @@ -20,7 +22,13 @@ pub(crate) struct Waker { } impl Waker { - pub(crate) fn new() -> io::Result { + pub(crate) fn new(selector: &Selector, token: Token) -> io::Result { + let waker = Waker::new_unregistered()?; + selector.register(waker.fd.as_raw_fd(), token, Interest::READABLE)?; + Ok(waker) + } + + pub(crate) fn new_unregistered() -> io::Result { let [receiver, sender] = pipe::new_raw()?; let sender = unsafe { File::from_raw_fd(sender) }; let receiver = unsafe { File::from_raw_fd(receiver) };