eintr_wrapper.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This provides a wrapper around system calls which may be interrupted by a
  5. // signal and return EINTR. See man 7 signal.
  6. // To prevent long-lasting loops (which would likely be a bug, such as a signal
  7. // that should be masked) to go unnoticed, there is a limit after which the
  8. // caller will nonetheless see an EINTR in Debug builds.
  9. //
  10. // On Windows and Fuchsia, this wrapper macro does nothing because there are no
  11. // signals.
  12. //
  13. // Don't wrap close calls in HANDLE_EINTR. Use IGNORE_EINTR if the return
  14. // value of close is significant. See http://crbug.com/269623.
  15. #ifndef BASE_POSIX_EINTR_WRAPPER_H_
  16. #define BASE_POSIX_EINTR_WRAPPER_H_
  17. #include "build/build_config.h"
  18. #if defined(OS_POSIX)
  19. #include <errno.h>
  20. #if defined(NDEBUG)
  21. #define HANDLE_EINTR(x) ({ \
  22. decltype(x) eintr_wrapper_result; \
  23. do { \
  24. eintr_wrapper_result = (x); \
  25. } while (eintr_wrapper_result == -1 && errno == EINTR); \
  26. eintr_wrapper_result; \
  27. })
  28. #else
  29. #define HANDLE_EINTR(x) ({ \
  30. int eintr_wrapper_counter = 0; \
  31. decltype(x) eintr_wrapper_result; \
  32. do { \
  33. eintr_wrapper_result = (x); \
  34. } while (eintr_wrapper_result == -1 && errno == EINTR && \
  35. eintr_wrapper_counter++ < 100); \
  36. eintr_wrapper_result; \
  37. })
  38. #endif // NDEBUG
  39. #define IGNORE_EINTR(x) ({ \
  40. decltype(x) eintr_wrapper_result; \
  41. do { \
  42. eintr_wrapper_result = (x); \
  43. if (eintr_wrapper_result == -1 && errno == EINTR) { \
  44. eintr_wrapper_result = 0; \
  45. } \
  46. } while (0); \
  47. eintr_wrapper_result; \
  48. })
  49. #else // !OS_POSIX
  50. #define HANDLE_EINTR(x) (x)
  51. #define IGNORE_EINTR(x) (x)
  52. #endif // !OS_POSIX
  53. #endif // BASE_POSIX_EINTR_WRAPPER_H_