about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-02-05 12:45:10 -0500
committerGitHub <noreply@github.com>2017-02-05 12:45:10 -0500
commitd194688c20bbe2b40dbdbca14f608fd9e75822fa (patch)
treeec386d4075a7a7c3dfa7145872d55a6f082e38f5 /src/libstd/sys
parentae3aafacdfeb4a3a1133239a15ec9e3bb7750bd5 (diff)
parentefeb42be2837842d1beb47b51bb693c7474aba3d (diff)
downloadrust-d194688c20bbe2b40dbdbca14f608fd9e75822fa.tar.gz
rust-d194688c20bbe2b40dbdbca14f608fd9e75822fa.zip
Rollup merge of #39514 - tbu-:pr_less_syscalls_fd, r=alexcrichton
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.
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/fd.rs19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/libstd/sys/unix/fd.rs b/src/libstd/sys/unix/fd.rs
index dcab30aad83..c690fd467ee 100644
--- a/src/libstd/sys/unix/fd.rs
+++ b/src/libstd/sys/unix/fd.rs
@@ -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))?;
@@ -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(())
         }
     }