Errno.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===- llvm/Support/Errno.h - Portable+convenient errno handling -*- C++ -*-===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares some portable and convenient functions to deal with errno.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_SUPPORT_ERRNO_H
  13. #define LLVM_SUPPORT_ERRNO_H
  14. #include <cerrno>
  15. #include <string>
  16. #include <type_traits>
  17. namespace llvm {
  18. namespace sys {
  19. /// Returns a string representation of the errno value, using whatever
  20. /// thread-safe variant of strerror() is available. Be sure to call this
  21. /// immediately after the function that set errno, or errno may have been
  22. /// overwritten by an intervening call.
  23. std::string StrError();
  24. /// Like the no-argument version above, but uses \p errnum instead of errno.
  25. std::string StrError(int errnum);
  26. template <typename FailT, typename Fun, typename... Args>
  27. inline decltype(auto) RetryAfterSignal(const FailT &Fail, const Fun &F,
  28. const Args &... As) {
  29. decltype(F(As...)) Res;
  30. do {
  31. errno = 0;
  32. Res = F(As...);
  33. } while (Res == Fail && errno == EINTR);
  34. return Res;
  35. }
  36. } // namespace sys
  37. } // namespace llvm
  38. #endif // LLVM_SUPPORT_ERRNO_H