select.c 4.3 KB

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