diff options
author | Pavel Labath <labath@google.com> | 2017-06-21 10:55:34 +0000 |
---|---|---|
committer | Pavel Labath <labath@google.com> | 2017-06-21 10:55:34 +0000 |
commit | 1f6aea2eb3cee9adad6c6975f674957dabd37c7b (patch) | |
tree | b87b8b2663a2ee1aba7268f3e7233525670905ad /llvm/unittests/Support/ErrnoTest.cpp | |
parent | 71d72135b05c85c81745f25801fb137d02a3e796 (diff) | |
download | bcm5719-llvm-1f6aea2eb3cee9adad6c6975f674957dabd37c7b.tar.gz bcm5719-llvm-1f6aea2eb3cee9adad6c6975f674957dabd37c7b.zip |
[Support] Add RetryAfterSignal helper function
Summary:
This function retries an operation if it was interrupted by a signal
(failed with EINTR). It's inspired by the TEMP_FAILURE_RETRY macro in
glibc, but I've turned that into a template function. I've also added a
fail-value argument, to enable the function to be used with e.g.
fopen(3), which is documented to fail for any reason that open(2) can
fail (which includes EINTR).
The main user of this function will be lldb, but there were also a
couple of uses within llvm that I could simplify using this function.
Reviewers: zturner, silvas, joerg
Subscribers: mgorny, llvm-commits
Differential Revision: https://reviews.llvm.org/D33895
llvm-svn: 305892
Diffstat (limited to 'llvm/unittests/Support/ErrnoTest.cpp')
-rw-r--r-- | llvm/unittests/Support/ErrnoTest.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/llvm/unittests/Support/ErrnoTest.cpp b/llvm/unittests/Support/ErrnoTest.cpp new file mode 100644 index 00000000000..72b52c01ee3 --- /dev/null +++ b/llvm/unittests/Support/ErrnoTest.cpp @@ -0,0 +1,33 @@ +//===- 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; + +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)); +} |