PosixHelpers.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "il2cpp-config.h"
  2. #if (IL2CPP_TARGET_POSIX || IL2CPP_SUPPORT_SOCKETS_POSIX_API) && !RUNTIME_TINY
  3. #if IL2CPP_TARGET_SWITCH || IL2CPP_TARGET_QNX
  4. #include <errno.h>
  5. #else
  6. #include <sys/errno.h>
  7. #endif
  8. #include "os/Posix/PosixHelpers.h"
  9. namespace il2cpp
  10. {
  11. namespace os
  12. {
  13. namespace posix
  14. {
  15. int Poll(pollfd* handles, int numHandles, int timeout)
  16. {
  17. int32_t ret = 0;
  18. time_t start = time(NULL);
  19. do
  20. {
  21. ret = poll(handles, numHandles, timeout);
  22. if (timeout > 0 && ret < 0)
  23. {
  24. const int32_t err = errno;
  25. const int32_t sec = time(NULL) - start;
  26. timeout -= sec * 1000;
  27. if (timeout < 0)
  28. timeout = 0;
  29. errno = err;
  30. }
  31. }
  32. #if IL2CPP_TARGET_LINUX
  33. while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  34. #else
  35. while (ret == -1 && errno == EINTR);
  36. #endif
  37. #if IL2CPP_TARGET_LINUX
  38. // On Linux, socket will report POLLERR if the other end has been closed, in addition to normal POLLHUP
  39. // From man page:
  40. // POLLERR
  41. // Error condition(only returned in revents; ignored in events).
  42. // This bit is also set for a file descriptor referring to the
  43. // write end of a pipe when the read end has been closed.
  44. //
  45. // Mac and Windows doesn't do it, so we zero out that bit if POLLHUP is set on Linux to get consistent behaviour
  46. for (int i = 0; i < numHandles; i++)
  47. {
  48. if ((handles[i].revents & POLLERR) && (handles[i].revents & POLLHUP))
  49. handles[i].revents &= ~POLLERR & handles[i].events;
  50. }
  51. #endif
  52. return ret;
  53. }
  54. }
  55. }
  56. }
  57. #endif // IL2CPP_TARGET_POSIX