[llvm-commits] [llvm] r103153 - /llvm/trunk/lib/Support/raw_ostream.cpp

Dan Gohman gohman at apple.com
Wed May 5 18:27:36 PDT 2010


Author: djg
Date: Wed May  5 20:27:36 2010
New Revision: 103153

URL: http://llvm.org/viewvc/llvm-project?rev=103153&view=rev
Log:
Handle EWOULDBLOCK as EAGAIN. And add a comment explaining why
EAGAIN and EWOULDBLOCK are used here.

Also, handle the case where a write call is interrupted after
some data has already been written.

Modified:
    llvm/trunk/lib/Support/raw_ostream.cpp

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=103153&r1=103152&r2=103153&view=diff
==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Wed May  5 20:27:36 2010
@@ -422,11 +422,30 @@
   assert(FD >= 0 && "File already closed.");
   pos += Size;
   ssize_t ret;
+
   do {
     ret = ::write(FD, Ptr, Size);
-  } while (ret < 0 && (errno == EAGAIN || errno == EINTR));
-  if (ret != (ssize_t) Size)
-    error_detected();
+
+    if (ret < 0) {
+      // If it's a recoverable error, swallow it and retry the write.
+      // EAGAIN and EWOULDBLOCK are not unambiguously recoverable, but
+      // some programs, such as bjam, assume that their child processes
+      // will treat them as if they are. If you don't want this code to
+      // spin, don't use O_NONBLOCK file descriptors with raw_ostream.
+      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
+        continue;
+
+      // Otherwise it's a non-recoverable error. Note it and quit.
+      error_detected();
+      break;
+    }
+
+    // The write may have written some or all of the data. Update the
+    // size and buffer pointer to reflect the remainder that needs
+    // to be written. If there are no bytes left, we're done.
+    Ptr += ret;
+    Size -= ret;
+  } while (Size > 0);
 }
 
 void raw_fd_ostream::close() {





More information about the llvm-commits mailing list