select.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * 2024-04-08 TroyMitchell Add all function comments
  10. */
  11. #include <rtthread.h>
  12. #include <poll.h>
  13. #include <sys/select.h>
  14. /**
  15. * @brief Initialize the file descriptor set to have zero bits for all file descriptors.
  16. * @param set Pointer to the file descriptor set to be initialized.
  17. * @param nfds The maximum file descriptor in the set plus one.
  18. * @note The actual size of the 'fd_set' is determined based on the parameter 'nfds'.
  19. */
  20. static void fdszero(fd_set *set, int nfds)
  21. {
  22. fd_mask *m;
  23. int n;
  24. /*
  25. The 'sizeof(fd_set)' of the system space may differ from user space,
  26. so the actual size of the 'fd_set' is determined here with the parameter 'nfds'
  27. */
  28. m = (fd_mask *)set;
  29. for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8))
  30. {
  31. rt_memset(m, 0, sizeof(fd_mask));
  32. m ++;
  33. }
  34. }
  35. /**
  36. * @brief Synchronous I/O multiplexing: multiplex input/output over a set of file descriptors.
  37. * @param nfds The highest-numbered file descriptor in any of the three sets, plus 1.
  38. * @param readfds A pointer to a set of file descriptors to be checked for read readiness.
  39. * @param writefds A pointer to a set of file descriptors to be checked for write readiness.
  40. * @param exceptfds A pointer to a set of file descriptors to be checked for exceptions.
  41. * @param timeout The maximum time to wait for any of the specified file descriptors to become ready.
  42. * @return Upon successful completion, the total number of file descriptors in all the sets that are ready for the requested operation is returned; otherwise, -1 is returned on error.
  43. */
  44. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
  45. {
  46. int fd;
  47. int npfds;
  48. int msec;
  49. int ndx;
  50. int ret;
  51. struct pollfd *pollset = RT_NULL;
  52. /* How many pollfd structures do we need to allocate? */
  53. for (fd = 0, npfds = 0; fd < nfds; fd++)
  54. {
  55. /* Check if any monitor operation is requested on this fd */
  56. if ((readfds && FD_ISSET(fd, readfds)) ||
  57. (writefds && FD_ISSET(fd, writefds)) ||
  58. (exceptfds && FD_ISSET(fd, exceptfds)))
  59. {
  60. npfds++;
  61. }
  62. }
  63. /* Allocate the descriptor list for poll() */
  64. if (npfds > 0)
  65. {
  66. pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd));
  67. if (!pollset)
  68. {
  69. return -1;
  70. }
  71. }
  72. /* Initialize the descriptor list for poll() */
  73. for (fd = 0, ndx = 0; fd < nfds; fd++)
  74. {
  75. int incr = 0;
  76. /* The readfs set holds the set of FDs that the caller can be assured
  77. * of reading from without blocking. Note that POLLHUP is included as
  78. * a read-able condition. POLLHUP will be reported at the end-of-file
  79. * or when a connection is lost. In either case, the read() can then
  80. * be performed without blocking.
  81. */
  82. if (readfds && FD_ISSET(fd, readfds))
  83. {
  84. pollset[ndx].fd = fd;
  85. pollset[ndx].events |= POLLIN;
  86. incr = 1;
  87. }
  88. if (writefds && FD_ISSET(fd, writefds))
  89. {
  90. pollset[ndx].fd = fd;
  91. pollset[ndx].events |= POLLOUT;
  92. incr = 1;
  93. }
  94. if (exceptfds && FD_ISSET(fd, exceptfds))
  95. {
  96. pollset[ndx].fd = fd;
  97. incr = 1;
  98. }
  99. ndx += incr;
  100. }
  101. RT_ASSERT(ndx == npfds);
  102. /* Convert the timeout to milliseconds */
  103. if (timeout)
  104. {
  105. msec = (int)timeout->tv_sec * 1000 + (int)timeout->tv_usec / 1000;
  106. }
  107. else
  108. {
  109. msec = -1;
  110. }
  111. /* Then let poll do all of the real work. */
  112. ret = poll(pollset, npfds, msec);
  113. /* Now set up the return values */
  114. if (readfds)
  115. {
  116. fdszero(readfds, nfds);
  117. }
  118. if (writefds)
  119. {
  120. fdszero(writefds, nfds);
  121. }
  122. if (exceptfds)
  123. {
  124. fdszero(exceptfds, nfds);
  125. }
  126. /* Convert the poll descriptor list back into selects 3 bitsets */
  127. if (ret > 0)
  128. {
  129. ret = 0;
  130. for (ndx = 0; ndx < npfds; ndx++)
  131. {
  132. /* Check for read conditions. Note that POLLHUP is included as a
  133. * read condition. POLLHUP will be reported when no more data will
  134. * be available (such as when a connection is lost). In either
  135. * case, the read() can then be performed without blocking.
  136. */
  137. if (readfds)
  138. {
  139. if (pollset[ndx].revents & (POLLIN | POLLHUP))
  140. {
  141. FD_SET(pollset[ndx].fd, readfds);
  142. ret++;
  143. }
  144. }
  145. /* Check for write conditions */
  146. if (writefds)
  147. {
  148. if (pollset[ndx].revents & POLLOUT)
  149. {
  150. FD_SET(pollset[ndx].fd, writefds);
  151. ret++;
  152. }
  153. }
  154. /* Check for exceptions */
  155. if (exceptfds)
  156. {
  157. if (pollset[ndx].revents & POLLERR)
  158. {
  159. FD_SET(pollset[ndx].fd, exceptfds);
  160. ret++;
  161. }
  162. }
  163. }
  164. }
  165. if (pollset) rt_free(pollset);
  166. return ret;
  167. }