10b57cec5SDimitry Andric //===- llvm/Support/Errno.h - Portable+convenient errno handling -*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file declares some portable and convenient functions to deal with errno. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_SUPPORT_ERRNO_H 140b57cec5SDimitry Andric #define LLVM_SUPPORT_ERRNO_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include <cerrno> 170b57cec5SDimitry Andric #include <string> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace llvm { 200b57cec5SDimitry Andric namespace sys { 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric /// Returns a string representation of the errno value, using whatever 230b57cec5SDimitry Andric /// thread-safe variant of strerror() is available. Be sure to call this 240b57cec5SDimitry Andric /// immediately after the function that set errno, or errno may have been 250b57cec5SDimitry Andric /// overwritten by an intervening call. 260b57cec5SDimitry Andric std::string StrError(); 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric /// Like the no-argument version above, but uses \p errnum instead of errno. 290b57cec5SDimitry Andric std::string StrError(int errnum); 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric template <typename FailT, typename Fun, typename... Args> decltype(auto)325ffd83dbSDimitry Andricinline decltype(auto) RetryAfterSignal(const FailT &Fail, const Fun &F, 335ffd83dbSDimitry Andric const Args &... As) { 340b57cec5SDimitry Andric decltype(F(As...)) Res; 350b57cec5SDimitry Andric do { 360b57cec5SDimitry Andric errno = 0; 370b57cec5SDimitry Andric Res = F(As...); 380b57cec5SDimitry Andric } while (Res == Fail && errno == EINTR); 390b57cec5SDimitry Andric return Res; 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric } // namespace sys 430b57cec5SDimitry Andric } // namespace llvm 440b57cec5SDimitry Andric 45*fe6060f1SDimitry Andric #endif // LLVM_SUPPORT_ERRNO_H 46