select.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-12-28 Bernard first version
  9. */
  10. #include <rtthread.h>
  11. #include <poll.h>
  12. #include <sys/select.h>
  13. static void fdszero(fd_set *set, int nfds)
  14. {
  15. fd_mask *m;
  16. int n;
  17. /*
  18. The 'sizeof(fd_set)' of the system space may differ from user space,
  19. so the actual size of the 'fd_set' is determined here with the parameter 'nfds'
  20. */
  21. m = (fd_mask *)set;
  22. for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8))
  23. {
  24. rt_memset(m, 0, sizeof(fd_mask));
  25. m ++;
  26. }
  27. }
  28. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
  29. {
  30. int fd;
  31. int npfds;
  32. int msec;
  33. int ndx;
  34. int ret;
  35. struct pollfd *pollset = RT_NULL;
  36. /* How many pollfd structures do we need to allocate? */
  37. for (fd = 0, npfds = 0; fd < nfds; fd++)
  38. {
  39. /* Check if any monitor operation is requested on this fd */
  40. if ((readfds && FD_ISSET(fd, readfds)) ||
  41. (writefds && FD_ISSET(fd, writefds)) ||
  42. (exceptfds && FD_ISSET(fd, exceptfds)))
  43. {
  44. npfds++;
  45. }
  46. }
  47. /* Allocate the descriptor list for poll() */
  48. if (npfds > 0)
  49. {
  50. pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd));
  51. if (!pollset)
  52. {
  53. return -1;
  54. }
  55. }
  56. /* Initialize the descriptor list for poll() */
  57. for (fd = 0, ndx = 0; fd < nfds; fd++)
  58. {
  59. int incr = 0;
  60. /* The readfs set holds the set of FDs that the caller can be assured
  61. * of reading from without blocking. Note that POLLHUP is included as
  62. * a read-able condition. POLLHUP will be reported at the end-of-file
  63. * or when a connection is lost. In either case, the read() can then
  64. * be performed without blocking.
  65. */
  66. if (readfds && FD_ISSET(fd, readfds))
  67. {
  68. pollset[ndx].fd = fd;
  69. pollset[ndx].events |= POLLIN;
  70. incr = 1;
  71. }
  72. if (writefds && FD_ISSET(fd, writefds))
  73. {
  74. pollset[ndx].fd = fd;
  75. pollset[ndx].events |= POLLOUT;
  76. incr = 1;
  77. }
  78. if (exceptfds && FD_ISSET(fd, exceptfds))
  79. {
  80. pollset[ndx].fd = fd;
  81. incr = 1;
  82. }
  83. ndx += incr;
  84. }
  85. RT_ASSERT(ndx == npfds);
  86. /* Convert the timeout to milliseconds */
  87. if (timeout)
  88. {
  89. msec = (int)timeout->tv_sec * 1000 + (int)timeout->tv_usec / 1000;
  90. }
  91. else
  92. {
  93. msec = -1;
  94. }
  95. /* Then let poll do all of the real work. */
  96. ret = poll(pollset, npfds, msec);
  97. /* Now set up the return values */
  98. if (readfds)
  99. {
  100. fdszero(readfds, nfds);
  101. }
  102. if (writefds)
  103. {
  104. fdszero(writefds, nfds);
  105. }
  106. if (exceptfds)
  107. {
  108. fdszero(exceptfds, nfds);
  109. }
  110. /* Convert the poll descriptor list back into selects 3 bitsets */
  111. if (ret > 0)
  112. {
  113. ret = 0;
  114. for (ndx = 0; ndx < npfds; ndx++)
  115. {
  116. /* Check for read conditions. Note that POLLHUP is included as a
  117. * read condition. POLLHUP will be reported when no more data will
  118. * be available (such as when a connection is lost). In either
  119. * case, the read() can then be performed without blocking.
  120. */
  121. if (readfds)
  122. {
  123. if (pollset[ndx].revents & (POLLIN | POLLHUP))
  124. {
  125. FD_SET(pollset[ndx].fd, readfds);
  126. ret++;
  127. }
  128. }
  129. /* Check for write conditions */
  130. if (writefds)
  131. {
  132. if (pollset[ndx].revents & POLLOUT)
  133. {
  134. FD_SET(pollset[ndx].fd, writefds);
  135. ret++;
  136. }
  137. }
  138. /* Check for exceptions */
  139. if (exceptfds)
  140. {
  141. if (pollset[ndx].revents & POLLERR)
  142. {
  143. FD_SET(pollset[ndx].fd, exceptfds);
  144. ret++;
  145. }
  146. }
  147. }
  148. }
  149. if (pollset) rt_free(pollset);
  150. return ret;
  151. }