select.c 4.6 KB

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