err.h 1.2 KB

12345678910111213141516171819202122232425
  1. #pragma once
  2. #include <cerrno>
  3. #include <system_error>
  4. // `errno` is only meaningful when it fails. E.g., a successful `fork()` sets
  5. // `errno` to `EINVAL` in child process on some macos
  6. // (https://stackoverflow.com/a/20295079), and thus `errno` should really only
  7. // be inspected if an error occurred.
  8. //
  9. // All functions used in `libshm` (so far) indicate error by returning `-1`. If
  10. // you want to use a function with a different error reporting mechanism, you
  11. // need to port `SYSCHECK` from `torch/lib/c10d/Utils.hpp`.
  12. #define SYSCHECK_ERR_RETURN_NEG1(expr) \
  13. while (true) { \
  14. if ((expr) == -1) { \
  15. if (errno == EINTR) { \
  16. continue; \
  17. } else { \
  18. throw std::system_error(errno, std::system_category()); \
  19. } \
  20. } else { \
  21. break; \
  22. } \
  23. }