[llvm] r306005 - Revert "[Support] Add RetryAfterSignal helper function" and subsequent fix

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 22 07:18:55 PDT 2017


Author: labath
Date: Thu Jun 22 09:18:55 2017
New Revision: 306005

URL: http://llvm.org/viewvc/llvm-project?rev=306005&view=rev
Log:
Revert "[Support] Add RetryAfterSignal helper function" and subsequent fix

The fix in r306003 uncovered a pretty fundamental problem that libc++
implementation of std::result_of does not handle the prototype of
open(2) correctly (presumably because it contains ...). This makes the
whole function unusable in its current form, so I am also reverting the
original commit (r305892), which introduced the function, at least until
I figure out a way to solve the libc++ issue.

Removed:
    llvm/trunk/unittests/Support/ErrnoTest.cpp
Modified:
    llvm/trunk/include/llvm/Support/Errno.h
    llvm/trunk/lib/Support/MemoryBuffer.cpp
    llvm/trunk/lib/Support/Unix/Path.inc
    llvm/trunk/lib/Support/Unix/Process.inc
    llvm/trunk/unittests/Support/CMakeLists.txt

Modified: llvm/trunk/include/llvm/Support/Errno.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Errno.h?rev=306005&r1=306004&r2=306005&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Errno.h (original)
+++ llvm/trunk/include/llvm/Support/Errno.h Thu Jun 22 09:18:55 2017
@@ -16,7 +16,6 @@
 
 #include <cerrno>
 #include <string>
-#include <type_traits>
 
 namespace llvm {
 namespace sys {
@@ -30,18 +29,6 @@ std::string StrError();
 /// Like the no-argument version above, but uses \p errnum instead of errno.
 std::string StrError(int errnum);
 
-template <typename Fun, typename... Args,
-          typename ResultT = std::result_of<Fun const &(const Args &...)>>
-inline typename ResultT::type RetryAfterSignal(typename ResultT::type Fail,
-                                               const Fun &F,
-                                               const Args &... As) {
-  typename ResultT::type Res;
-  do
-    Res = F(As...);
-  while (Res == Fail && errno == EINTR);
-  return Res;
-}
-
 }  // namespace sys
 }  // namespace llvm
 

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=306005&r1=306004&r2=306005&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Thu Jun 22 09:18:55 2017
@@ -240,9 +240,11 @@ getMemoryBufferForStream(int FD, const T
   // Read into Buffer until we hit EOF.
   do {
     Buffer.reserve(Buffer.size() + ChunkSize);
-    ReadBytes = sys::RetryAfterSignal(-1, read, FD, Buffer.end(), ChunkSize);
-    if (ReadBytes == -1)
+    ReadBytes = read(FD, Buffer.end(), ChunkSize);
+    if (ReadBytes == -1) {
+      if (errno == EINTR) continue;
       return std::error_code(errno, std::generic_category());
+    }
     Buffer.set_size(Buffer.size() + ReadBytes);
   } while (ReadBytes != 0);
 
@@ -389,12 +391,13 @@ getOpenFileImpl(int FD, const Twine &Fil
 
   while (BytesLeft) {
 #ifdef HAVE_PREAD
-    ssize_t NumRead = sys::RetryAfterSignal(-1, ::pread, FD, BufPtr, BytesLeft,
-                                            MapSize - BytesLeft + Offset);
+    ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
 #else
-    ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD, BufPtr, BytesLeft);
+    ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
 #endif
     if (NumRead == -1) {
+      if (errno == EINTR)
+        continue;
       // Error while reading.
       return std::error_code(errno, std::generic_category());
     }

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=306005&r1=306004&r2=306005&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Thu Jun 22 09:18:55 2017
@@ -737,8 +737,10 @@ std::error_code openFileForRead(const Tw
 #ifdef O_CLOEXEC
   OpenFlags |= O_CLOEXEC;
 #endif
-  if ((ResultFD = sys::RetryAfterSignal(-1, open, P.begin(), OpenFlags)) < 0)
-    return std::error_code(errno, std::generic_category());
+  while ((ResultFD = open(P.begin(), OpenFlags)) < 0) {
+    if (errno != EINTR)
+      return std::error_code(errno, std::generic_category());
+  }
 #ifndef O_CLOEXEC
   int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
   (void)r;
@@ -798,8 +800,10 @@ std::error_code openFileForWrite(const T
 
   SmallString<128> Storage;
   StringRef P = Name.toNullTerminatedStringRef(Storage);
-  if ((ResultFD = sys::RetryAfterSignal(-1, open, P.begin(), OpenFlags, Mode)) < 0)
-    return std::error_code(errno, std::generic_category());
+  while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) {
+    if (errno != EINTR)
+      return std::error_code(errno, std::generic_category());
+  }
 #ifndef O_CLOEXEC
   int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
   (void)r;

Modified: llvm/trunk/lib/Support/Unix/Process.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Process.inc?rev=306005&r1=306004&r2=306005&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Process.inc (original)
+++ llvm/trunk/lib/Support/Unix/Process.inc Thu Jun 22 09:18:55 2017
@@ -207,10 +207,13 @@ std::error_code Process::FixupStandardFi
   for (int StandardFD : StandardFDs) {
     struct stat st;
     errno = 0;
-    if (RetryAfterSignal(-1, fstat, StandardFD, &st) < 0) {
+    while (fstat(StandardFD, &st) < 0) {
       assert(errno && "expected errno to be set if fstat failed!");
       // fstat should return EBADF if the file descriptor is closed.
-      if (errno != EBADF)
+      if (errno == EBADF)
+        break;
+      // retry fstat if we got EINTR, otherwise bubble up the failure.
+      if (errno != EINTR)
         return std::error_code(errno, std::generic_category());
     }
     // if fstat succeeds, move on to the next FD.
@@ -219,8 +222,11 @@ std::error_code Process::FixupStandardFi
     assert(errno == EBADF && "expected errno to have EBADF at this point!");
 
     if (NullFD < 0) {
-      if ((NullFD = RetryAfterSignal(-1, open, "/dev/null", O_RDWR)) < 0)
+      while ((NullFD = open("/dev/null", O_RDWR)) < 0) {
+        if (errno == EINTR)
+          continue;
         return std::error_code(errno, std::generic_category());
+      }
     }
 
     if (NullFD == StandardFD)

Modified: llvm/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=306005&r1=306004&r2=306005&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Support/CMakeLists.txt Thu Jun 22 09:18:55 2017
@@ -21,7 +21,6 @@ add_llvm_unittest(SupportTests
   DebugTest.cpp
   EndianStreamTest.cpp
   EndianTest.cpp
-  ErrnoTest.cpp
   ErrorOrTest.cpp
   ErrorTest.cpp
   FileOutputBufferTest.cpp

Removed: llvm/trunk/unittests/Support/ErrnoTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrnoTest.cpp?rev=306004&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/ErrnoTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrnoTest.cpp (removed)
@@ -1,38 +0,0 @@
-//===- ErrnoTest.cpp - Error handling unit tests --------------------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Support/Errno.h"
-#include "gtest/gtest.h"
-
-using namespace llvm::sys;
-
-static int *ReturnPointer() { return new int(47); }
-
-TEST(ErrnoTest, RetryAfterSignal) {
-  EXPECT_EQ(1, RetryAfterSignal(-1, [] { return 1; }));
-
-  EXPECT_EQ(-1, RetryAfterSignal(-1, [] {
-    errno = EAGAIN;
-    return -1;
-  }));
-  EXPECT_EQ(EAGAIN, errno);
-
-  unsigned calls = 0;
-  EXPECT_EQ(1, RetryAfterSignal(-1, [&calls] {
-              errno = EINTR;
-              ++calls;
-              return calls == 1 ? -1 : 1;
-            }));
-  EXPECT_EQ(2u, calls);
-
-  EXPECT_EQ(1, RetryAfterSignal(-1, [](int x) { return x; }, 1));
-
-  std::unique_ptr<int> P{RetryAfterSignal(nullptr, ReturnPointer)};
-  EXPECT_EQ(47, *P);
-}




More information about the llvm-commits mailing list