poll.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright 2010,2013,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define _GNU_SOURCE
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <errno.h>
  20. #include <poll.h>
  21. #include "async.h"
  22. #include "bytevectors.h"
  23. #include "error.h"
  24. #include "extensions.h"
  25. #include "gsubr.h"
  26. #include "modules.h"
  27. #include "numbers.h"
  28. #include "ports-internal.h"
  29. #include "syscalls.h"
  30. #include "vectors.h"
  31. #include "version.h"
  32. #include "poll.h"
  33. /* {Poll}
  34. */
  35. /* Poll a set of file descriptors, waiting until one or more of them is
  36. ready to perform input or output.
  37. This is a low-level interface. See the `(ice-9 poll)' module for a more
  38. usable wrapper.
  39. `pollfds' is expected to be a bytevector, laid out in contiguous blocks of 64
  40. bits. Each block has the format of one `struct pollfd': a 32-bit int file
  41. descriptor, a 16-bit int events mask, and a 16-bit int revents mask.
  42. The number of pollfd structures in `pollfds' is specified in
  43. `nfds'. `pollfds' must be at least long enough to support that number of
  44. structures. It may be longer, in which case the trailing entries are left
  45. untouched.
  46. The pollfds bytevector is modified directly, setting the returned events in
  47. the final two bytes (the revents member).
  48. Since Scheme ports can buffer input or output in userspace, a Scheme
  49. poll interface needs to take that into account as well. The `ports'
  50. argument, a vector big enough for `nfds' elements, is given for this
  51. purpose. If a pollfd entry has a corresponding open port, that port
  52. is scanned for available input or output before dropping into the
  53. poll. If any port has buffered I/O available, the poll syscall is
  54. still issued, but with a timeout of 0 milliseconds, and a full port
  55. scan occurs after the poll returns.
  56. If timeout is given and is non-negative, the poll will return after that
  57. number of milliseconds if no fd became active.
  58. */
  59. static SCM
  60. scm_primitive_poll (SCM pollfds, SCM nfds, SCM ports, SCM timeout)
  61. #define FUNC_NAME "primitive-poll"
  62. {
  63. int rv = 0;
  64. nfds_t i;
  65. nfds_t c_nfds;
  66. int c_timeout;
  67. int have_buffered_io = 0;
  68. struct pollfd *fds;
  69. SCM_VALIDATE_BYTEVECTOR (SCM_ARG1, pollfds);
  70. c_nfds = scm_to_uint32 (nfds);
  71. c_timeout = scm_to_int (timeout);
  72. if (SCM_UNLIKELY (SCM_BYTEVECTOR_LENGTH (pollfds)
  73. < c_nfds * sizeof(struct pollfd)))
  74. SCM_OUT_OF_RANGE (SCM_ARG2, nfds);
  75. SCM_VALIDATE_VECTOR (SCM_ARG3, ports);
  76. if (SCM_UNLIKELY (SCM_SIMPLE_VECTOR_LENGTH (ports) < c_nfds))
  77. SCM_OUT_OF_RANGE (SCM_ARG3, ports);
  78. fds = (struct pollfd*)SCM_BYTEVECTOR_CONTENTS (pollfds);
  79. for (i = 0; i < c_nfds; i++)
  80. {
  81. SCM port = SCM_SIMPLE_VECTOR_REF (ports, i);
  82. short int revents = 0;
  83. if (SCM_PORTP (port))
  84. {
  85. if (SCM_CLOSEDP (port))
  86. revents |= POLLERR;
  87. else
  88. {
  89. scm_t_port *pt = SCM_PORT (port);
  90. size_t tmp;
  91. if (scm_port_buffer_can_take (pt->read_buf, &tmp) > 0)
  92. /* Buffered input waiting to be read. */
  93. revents |= POLLIN;
  94. if (SCM_OUTPUT_PORT_P (port)
  95. && scm_port_buffer_can_put (pt->write_buf, &tmp) > 1)
  96. /* Buffered output possible. The "> 1" is because
  97. writing the last byte would flush the port. */
  98. revents |= POLLOUT;
  99. }
  100. }
  101. if (revents & fds[i].events)
  102. {
  103. have_buffered_io = 1;
  104. c_timeout = 0;
  105. break;
  106. }
  107. }
  108. SCM_SYSCALL (rv = poll (fds, c_nfds, c_timeout));
  109. if (rv == -1)
  110. SCM_SYSERROR;
  111. if (have_buffered_io)
  112. for (i = 0; i < c_nfds; i++)
  113. {
  114. SCM port = SCM_SIMPLE_VECTOR_REF (ports, i);
  115. short int revents = 0;
  116. if (SCM_PORTP (port))
  117. {
  118. if (SCM_CLOSEDP (port))
  119. revents |= POLLERR;
  120. else
  121. {
  122. scm_t_port *pt = SCM_PORT (port);
  123. size_t tmp;
  124. if (scm_port_buffer_can_take (pt->read_buf, &tmp) > 0)
  125. /* Buffered input waiting to be read. */
  126. revents |= POLLIN;
  127. if (SCM_OUTPUT_PORT_P (port)
  128. && scm_port_buffer_can_put (pt->write_buf, &tmp) > 1)
  129. /* Buffered output possible. The "> 1" is because
  130. writing the last byte would flush the port. */
  131. revents |= POLLOUT;
  132. }
  133. }
  134. /* Mask in the events we are interested, and test if any are
  135. interesting. */
  136. if ((revents &= fds[i].events))
  137. {
  138. /* Could be the underlying fd is also ready for reading. */
  139. if (!fds[i].revents)
  140. rv++;
  141. /* In any case, add these events to whatever the syscall
  142. set. */
  143. fds[i].revents |= revents;
  144. }
  145. }
  146. return scm_from_int (rv);
  147. }
  148. #undef FUNC_NAME
  149. static void
  150. scm_init_poll (void)
  151. {
  152. scm_c_define_gsubr ("primitive-poll", 4, 0, 0, scm_primitive_poll);
  153. scm_c_define ("%sizeof-struct-pollfd", scm_from_size_t (sizeof (struct pollfd)));
  154. #ifdef POLLIN
  155. scm_c_define ("POLLIN", scm_from_int (POLLIN));
  156. #endif
  157. #ifdef POLLPRI
  158. scm_c_define ("POLLPRI", scm_from_int (POLLPRI));
  159. #endif
  160. #ifdef POLLOUT
  161. scm_c_define ("POLLOUT", scm_from_int (POLLOUT));
  162. #endif
  163. #ifdef POLLRDHUP
  164. scm_c_define ("POLLRDHUP", scm_from_int (POLLRDHUP));
  165. #endif
  166. #ifdef POLLERR
  167. scm_c_define ("POLLERR", scm_from_int (POLLERR));
  168. #endif
  169. #ifdef POLLHUP
  170. scm_c_define ("POLLHUP", scm_from_int (POLLHUP));
  171. #endif
  172. #ifdef POLLNVAL
  173. scm_c_define ("POLLNVAL", scm_from_int (POLLNVAL));
  174. #endif
  175. }
  176. void
  177. scm_register_poll (void)
  178. {
  179. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  180. "scm_init_poll",
  181. (scm_t_extension_init_func) scm_init_poll,
  182. NULL);
  183. }