blob: aedb5fb292b84d81d0e4b490070a33ed93325dca [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Support/Errno.h - Portable+convenient errno handling -*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares some portable and convenient functions to deal with errno.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_ERRNO_H
14#define LLVM_SUPPORT_ERRNO_H
15
16#include <cerrno>
17#include <string>
18#include <type_traits>
19
20namespace llvm {
21namespace sys {
22
23/// Returns a string representation of the errno value, using whatever
24/// thread-safe variant of strerror() is available. Be sure to call this
25/// immediately after the function that set errno, or errno may have been
26/// overwritten by an intervening call.
27std::string StrError();
28
29/// Like the no-argument version above, but uses \p errnum instead of errno.
30std::string StrError(int errnum);
31
32template <typename FailT, typename Fun, typename... Args>
33inline auto RetryAfterSignal(const FailT &Fail, const Fun &F,
34 const Args &... As) -> decltype(F(As...)) {
35 decltype(F(As...)) Res;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010036 do {
37 errno = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 Res = F(As...);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039 } while (Res == Fail && errno == EINTR);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040 return Res;
41}
42
43} // namespace sys
44} // namespace llvm
45
46#endif // LLVM_SYSTEM_ERRNO_H