Skip to content

Commit

Permalink
Add new_unregistered function to fd based wakers
Browse files Browse the repository at this point in the history
To be used in the poll implementation.
  • Loading branch information
Thomasdezeeuw committed Jun 24, 2024
1 parent ee86c2c commit f3863f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/sys/unix/waker/eventfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,14 +22,19 @@ pub(crate) struct Waker {
}

impl Waker {
pub(crate) fn new() -> io::Result<Waker> {
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)?;
Ok(waker)
}

pub(crate) fn new_unregistered() -> io::Result<Waker> {
#[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 })
}
Expand Down
10 changes: 9 additions & 1 deletion src/sys/unix/waker/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -20,7 +22,13 @@ pub(crate) struct Waker {
}

impl Waker {
pub(crate) fn new() -> io::Result<Waker> {
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)?;
Ok(waker)
}

pub(crate) fn new_unregistered() -> io::Result<Waker> {
let [receiver, sender] = pipe::new_raw()?;
let sender = unsafe { File::from_raw_fd(sender) };
let receiver = unsafe { File::from_raw_fd(receiver) };
Expand Down

0 comments on commit f3863f1

Please sign in to comment.