select.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * File : select.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016-12-28 Bernard first version
  23. */
  24. #include <dfs.h>
  25. #include <dfs_fs.h>
  26. #include <dfs_posix.h>
  27. #include <dfs_poll.h>
  28. #include <dfs_select.h>
  29. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
  30. {
  31. int fd;
  32. int npfds;
  33. int msec;
  34. int ndx;
  35. int ret;
  36. struct pollfd *pollset = RT_NULL;
  37. /* How many pollfd structures do we need to allocate? */
  38. for (fd = 0, npfds = 0; fd < nfds; fd++)
  39. {
  40. /* Check if any monitor operation is requested on this fd */
  41. if ((readfds && FD_ISSET(fd, readfds)) ||
  42. (writefds && FD_ISSET(fd, writefds)) ||
  43. (exceptfds && FD_ISSET(fd, exceptfds)))
  44. {
  45. npfds++;
  46. }
  47. }
  48. /* Allocate the descriptor list for poll() */
  49. if (npfds > 0)
  50. {
  51. pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd));
  52. if (!pollset)
  53. {
  54. return -1;
  55. }
  56. }
  57. /* Initialize the descriptor list for poll() */
  58. for (fd = 0, ndx = 0; fd < nfds; fd++)
  59. {
  60. int incr = 0;
  61. /* The readfs set holds the set of FDs that the caller can be assured
  62. * of reading from without blocking. Note that POLLHUP is included as
  63. * a read-able condition. POLLHUP will be reported at the end-of-file
  64. * or when a connection is lost. In either case, the read() can then
  65. * be performed without blocking.
  66. */
  67. if (readfds && FD_ISSET(fd, readfds))
  68. {
  69. pollset[ndx].fd = fd;
  70. pollset[ndx].events |= POLLIN;
  71. incr = 1;
  72. }
  73. if (writefds && FD_ISSET(fd, writefds))
  74. {
  75. pollset[ndx].fd = fd;
  76. pollset[ndx].events |= POLLOUT;
  77. incr = 1;
  78. }
  79. if (exceptfds && FD_ISSET(fd, exceptfds))
  80. {
  81. pollset[ndx].fd = fd;
  82. incr = 1;
  83. }
  84. ndx += incr;
  85. }
  86. RT_ASSERT(ndx == npfds);
  87. /* Convert the timeout to milliseconds */
  88. if (timeout)
  89. {
  90. msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  91. }
  92. else
  93. {
  94. msec = -1;
  95. }
  96. /* Then let poll do all of the real work. */
  97. ret = poll(pollset, npfds, msec);
  98. /* Now set up the return values */
  99. if (readfds)
  100. {
  101. memset(readfds, 0, sizeof(fd_set));
  102. }
  103. if (writefds)
  104. {
  105. memset(writefds, 0, sizeof(fd_set));
  106. }
  107. if (exceptfds)
  108. {
  109. memset(exceptfds, 0, sizeof(fd_set));
  110. }
  111. /* Convert the poll descriptor list back into selects 3 bitsets */
  112. if (ret > 0)
  113. {
  114. ret = 0;
  115. for (ndx = 0; ndx < npfds; ndx++)
  116. {
  117. /* Check for read conditions. Note that POLLHUP is included as a
  118. * read condition. POLLHUP will be reported when no more data will
  119. * be available (such as when a connection is lost). In either
  120. * case, the read() can then be performed without blocking.
  121. */
  122. if (readfds)
  123. {
  124. if (pollset[ndx].revents & (POLLIN | POLLHUP))
  125. {
  126. FD_SET(pollset[ndx].fd, readfds);
  127. ret++;
  128. }
  129. }
  130. /* Check for write conditions */
  131. if (writefds)
  132. {
  133. if (pollset[ndx].revents & POLLOUT)
  134. {
  135. FD_SET(pollset[ndx].fd, writefds);
  136. ret++;
  137. }
  138. }
  139. /* Check for exceptions */
  140. if (exceptfds)
  141. {
  142. if (pollset[ndx].revents & POLLERR)
  143. {
  144. FD_SET(pollset[ndx].fd, exceptfds);
  145. ret++;
  146. }
  147. }
  148. }
  149. }
  150. if (pollset) rt_free(pollset);
  151. return ret;
  152. }