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