poll.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * poll function
  3. *
  4. * Copyright 2008 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #ifndef HAVE_POLL
  23. #ifdef HAVE_SYS_TIME_H
  24. #include <sys/time.h>
  25. #endif
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. /* we can't include winsock2.h here so we have to duplicate the definitions for Windows */
  30. #if defined(__MINGW32__) || defined(_MSC_VER)
  31. #define FD_SETSIZE 64
  32. struct __ms_timeval
  33. {
  34. long tv_sec;
  35. long tv_usec;
  36. };
  37. #define timeval __ms_timeval
  38. typedef struct
  39. {
  40. unsigned int fd_count;
  41. int fd_array[FD_SETSIZE]; /* an array of SOCKETs */
  42. } fd_set;
  43. #define FD_ZERO(set) ((set)->fd_count = 0)
  44. #define FD_ISSET(fd, set) __WSAFDIsSet((fd), (set))
  45. #define FD_SET(fd, set) do { if ((set)->fd_count < FD_SETSIZE) (set)->fd_array[(set)->fd_count++]=(fd); } while(0)
  46. int __stdcall select(int,fd_set*,fd_set*,fd_set*,const struct timeval*);
  47. int __stdcall __WSAFDIsSet(int,fd_set*);
  48. #endif
  49. int poll( struct pollfd *fds, unsigned int count, int timeout )
  50. {
  51. fd_set read_set, write_set, except_set;
  52. unsigned int i;
  53. int maxfd = -1, ret;
  54. FD_ZERO( &read_set );
  55. FD_ZERO( &write_set );
  56. FD_ZERO( &except_set );
  57. for (i = 0; i < count; i++)
  58. {
  59. if (fds[i].fd == -1) continue;
  60. if (fds[i].events & (POLLIN|POLLPRI)) FD_SET( fds[i].fd, &read_set );
  61. if (fds[i].events & POLLOUT) FD_SET( fds[i].fd, &write_set );
  62. FD_SET( fds[i].fd, &except_set ); /* POLLERR etc. are always selected */
  63. if (fds[i].fd > maxfd) maxfd = fds[i].fd;
  64. }
  65. if (timeout != -1)
  66. {
  67. struct timeval tv;
  68. tv.tv_sec = timeout / 1000;
  69. tv.tv_usec = timeout % 1000;
  70. ret = select( maxfd + 1, &read_set, &write_set, &except_set, &tv );
  71. }
  72. else ret = select( maxfd + 1, &read_set, &write_set, &except_set, NULL );
  73. if (ret >= 0)
  74. {
  75. for (i = 0; i < count; i++)
  76. {
  77. fds[i].revents = 0;
  78. if (fds[i].fd == -1) continue;
  79. if (FD_ISSET( fds[i].fd, &read_set )) fds[i].revents |= POLLIN;
  80. if (FD_ISSET( fds[i].fd, &write_set )) fds[i].revents |= POLLOUT;
  81. if (FD_ISSET( fds[i].fd, &except_set )) fds[i].revents |= POLLERR;
  82. }
  83. }
  84. return ret;
  85. }
  86. #endif /* HAVE_POLL */