poll.c 6.5 KB

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