Skip to content

Commit

Permalink
Rollup merge of rust-lang#39514 - tbu-:pr_less_syscalls_fd, r=alexcri…
Browse files Browse the repository at this point in the history
…chton

Use less syscalls in `FileDesc::set_{nonblocking,cloexec}`

Only set the flags if they differ from what the OS reported, use
`FIONBIO` to atomically set the non-blocking IO flag on Linux.
  • Loading branch information
frewsxcv authored Feb 5, 2017
2 parents ae3aafa + efeb42b commit d194688
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/libstd/sys/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,24 @@ impl FileDesc {
pub fn set_cloexec(&self) -> io::Result<()> {
unsafe {
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
cvt(libc::fcntl(self.fd, libc::F_SETFD, previous | libc::FD_CLOEXEC))?;
let new = previous | libc::FD_CLOEXEC;
if new != previous {
cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
}
Ok(())
}
}

#[cfg(target_os = "linux")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
unsafe {
let v = nonblocking as c_int;
cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
Ok(())
}
}

#[cfg(not(target_os = "linux"))]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
unsafe {
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
Expand All @@ -157,7 +170,9 @@ impl FileDesc {
} else {
previous & !libc::O_NONBLOCK
};
cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
if new != previous {
cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
}
Ok(())
}
}
Expand Down

0 comments on commit d194688

Please sign in to comment.