Skip to content

Commit

Permalink
Reuse pipe2 polyfill code
Browse files Browse the repository at this point in the history
  • Loading branch information
ecnelises authored and Thomasdezeeuw committed Aug 21, 2023
1 parent 808dbb0 commit e7a0685
Showing 1 changed file with 4 additions and 29 deletions.
33 changes: 4 additions & 29 deletions src/sys/unix/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ pub use self::kqueue::Waker;
target_os = "redox",
))]
mod pipe {
use crate::unix::pipe::new as new_pipe;
use std::fs::File;
use std::io::{self, Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
Expand All @@ -223,9 +224,9 @@ mod pipe {

impl WakerInternal {
pub fn new() -> io::Result<WakerInternal> {
let (receiver, sender) = Self::pipe2()?;
let receiver = unsafe { File::from_raw_fd(receiver) };
let sender = unsafe { File::from_raw_fd(sender) };
let (sender, receiver) = new_pipe()?;
let sender = unsafe { File::from_raw_fd(sender.as_raw_fd()) };
let receiver = unsafe { File::from_raw_fd(receiver.as_raw_fd()) };
Ok(WakerInternal { sender, receiver })
}

Expand Down Expand Up @@ -265,32 +266,6 @@ mod pipe {
}
}
}

#[cfg(target_os = "aix")]
fn pipe2() -> io::Result<(RawFd, RawFd)> {
let mut fds = [-1; 2];
syscall!(pipe(fds.as_mut_ptr()))?;
unsafe {
if libc::fcntl(fds[0], libc::F_SETFL, libc::O_NONBLOCK) != 0
|| libc::fcntl(fds[0], libc::F_SETFD, libc::O_CLOEXEC) != 0
|| libc::fcntl(fds[1], libc::F_SETFL, libc::O_NONBLOCK) != 0
|| libc::fcntl(fds[1], libc::F_SETFD, libc::O_CLOEXEC) != 0
{
let err = io::Error::last_os_error();
let _ = libc::close(fds[0]);
let _ = libc::close(fds[1]);
return Err(err);
}
}
Ok((fds[0], fds[1]))
}

#[cfg(not(target_os = "aix"))]
fn pipe2() -> io::Result<(RawFd, RawFd)> {
let mut fds = [-1; 2];
syscall!(pipe2(fds.as_mut_ptr(), libc::O_NONBLOCK | libc::O_CLOEXEC))?;
Ok((fds[0], fds[1]))
}
}

impl AsRawFd for WakerInternal {
Expand Down

0 comments on commit e7a0685

Please sign in to comment.