SDL_poll.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_poll.h"
  20. #ifdef HAVE_POLL
  21. #include <poll.h>
  22. #else
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #endif
  27. #include <errno.h>
  28. int SDL_IOReady(int fd, int flags, Sint64 timeoutNS)
  29. {
  30. int result;
  31. SDL_assert(flags & (SDL_IOR_READ | SDL_IOR_WRITE));
  32. // Note: We don't bother to account for elapsed time if we get EINTR
  33. do {
  34. #ifdef HAVE_POLL
  35. struct pollfd info;
  36. int timeoutMS;
  37. info.fd = fd;
  38. info.events = 0;
  39. if (flags & SDL_IOR_READ) {
  40. info.events |= POLLIN | POLLPRI;
  41. }
  42. if (flags & SDL_IOR_WRITE) {
  43. info.events |= POLLOUT;
  44. }
  45. // FIXME: Add support for ppoll() for nanosecond precision
  46. if (timeoutNS > 0) {
  47. timeoutMS = (int)SDL_NS_TO_MS(timeoutNS + (SDL_NS_PER_MS - 1));
  48. } else if (timeoutNS == 0) {
  49. timeoutMS = 0;
  50. } else {
  51. timeoutMS = -1;
  52. }
  53. result = poll(&info, 1, timeoutMS);
  54. #else
  55. fd_set rfdset, *rfdp = NULL;
  56. fd_set wfdset, *wfdp = NULL;
  57. struct timeval tv, *tvp = NULL;
  58. // If this assert triggers we'll corrupt memory here
  59. SDL_assert(fd >= 0 && fd < FD_SETSIZE);
  60. if (flags & SDL_IOR_READ) {
  61. FD_ZERO(&rfdset);
  62. FD_SET(fd, &rfdset);
  63. rfdp = &rfdset;
  64. }
  65. if (flags & SDL_IOR_WRITE) {
  66. FD_ZERO(&wfdset);
  67. FD_SET(fd, &wfdset);
  68. wfdp = &wfdset;
  69. }
  70. if (timeoutNS >= 0) {
  71. tv.tv_sec = (timeoutNS / SDL_NS_PER_SECOND);
  72. tv.tv_usec = SDL_NS_TO_US((timeoutNS % SDL_NS_PER_SECOND) + (SDL_NS_PER_US - 1));
  73. tvp = &tv;
  74. }
  75. result = select(fd + 1, rfdp, wfdp, NULL, tvp);
  76. #endif // HAVE_POLL
  77. } while (result < 0 && errno == EINTR && !(flags & SDL_IOR_NO_RETRY));
  78. return result;
  79. }