eintr.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Utility for handling interrupted syscalls by signals.
  2. Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef GDBSUPPORT_EINTR_H
  15. #define GDBSUPPORT_EINTR_H
  16. #include <cerrno>
  17. namespace gdb
  18. {
  19. /* Repeat a system call interrupted with a signal.
  20. A utility for handling interrupted syscalls, which return with error
  21. and set the errno to EINTR. The interrupted syscalls can be repeated,
  22. until successful completion. This utility avoids wrapping code with
  23. manual checks for such errors which are highly repetitive.
  24. For example, with:
  25. ssize_t ret;
  26. do
  27. {
  28. errno = 0;
  29. ret = ::write (pipe[1], "+", 1);
  30. }
  31. while (ret == -1 && errno == EINTR);
  32. You could wrap it by writing the wrapped form:
  33. ssize_t ret = gdb::handle_eintr (-1, ::write, pipe[1], "+", 1);
  34. ERRVAL specifies the failure value indicating that the call to the
  35. F function with ARGS... arguments was possibly interrupted with a
  36. signal. */
  37. template<typename ErrorValType, typename Fun, typename... Args>
  38. inline auto
  39. handle_eintr (ErrorValType errval, const Fun &f, const Args &... args)
  40. -> decltype (f (args...))
  41. {
  42. decltype (f (args...)) ret;
  43. do
  44. {
  45. errno = 0;
  46. ret = f (args...);
  47. }
  48. while (ret == errval && errno == EINTR);
  49. return ret;
  50. }
  51. } /* namespace gdb */
  52. #endif /* GDBSUPPORT_EINTR_H */