poll.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. size_t tmp;
  88. if (scm_port_buffer_can_take (pt->read_buf, &tmp) > 0)
  89. /* Buffered input waiting to be read. */
  90. revents |= POLLIN;
  91. if (SCM_OUTPUT_PORT_P (port)
  92. && scm_port_buffer_can_put (pt->write_buf, &tmp) > 1)
  93. /* Buffered output possible. The "> 1" is because
  94. writing the last byte would flush the port. */
  95. revents |= POLLOUT;
  96. }
  97. }
  98. if (revents & fds[i].events)
  99. {
  100. have_buffered_io = 1;
  101. c_timeout = 0;
  102. break;
  103. }
  104. }
  105. SCM_SYSCALL (rv = poll (fds, c_nfds, c_timeout));
  106. if (rv == -1)
  107. SCM_SYSERROR;
  108. if (have_buffered_io)
  109. for (i = 0; i < c_nfds; i++)
  110. {
  111. SCM port = SCM_SIMPLE_VECTOR_REF (ports, i);
  112. short int revents = 0;
  113. if (SCM_PORTP (port))
  114. {
  115. if (SCM_CLOSEDP (port))
  116. revents |= POLLERR;
  117. else
  118. {
  119. scm_t_port *pt = SCM_PORT (port);
  120. size_t tmp;
  121. if (scm_port_buffer_can_take (pt->read_buf, &tmp) > 0)
  122. /* Buffered input waiting to be read. */
  123. revents |= POLLIN;
  124. if (SCM_OUTPUT_PORT_P (port)
  125. && scm_port_buffer_can_put (pt->write_buf, &tmp) > 1)
  126. /* Buffered output possible. The "> 1" is because
  127. writing the last byte would flush the port. */
  128. revents |= POLLOUT;
  129. }
  130. }
  131. /* Mask in the events we are interested, and test if any are
  132. interesting. */
  133. if ((revents &= fds[i].events))
  134. {
  135. /* Could be the underlying fd is also ready for reading. */
  136. if (!fds[i].revents)
  137. rv++;
  138. /* In any case, add these events to whatever the syscall
  139. set. */
  140. fds[i].revents |= revents;
  141. }
  142. }
  143. return scm_from_int (rv);
  144. }
  145. #undef FUNC_NAME
  146. static void
  147. scm_init_poll (void)
  148. {
  149. scm_c_define_gsubr ("primitive-poll", 4, 0, 0, scm_primitive_poll);
  150. scm_c_define ("%sizeof-struct-pollfd", scm_from_size_t (sizeof (struct pollfd)));
  151. #ifdef POLLIN
  152. scm_c_define ("POLLIN", scm_from_int (POLLIN));
  153. #endif
  154. #ifdef POLLPRI
  155. scm_c_define ("POLLPRI", scm_from_int (POLLPRI));
  156. #endif
  157. #ifdef POLLOUT
  158. scm_c_define ("POLLOUT", scm_from_int (POLLOUT));
  159. #endif
  160. #ifdef POLLRDHUP
  161. scm_c_define ("POLLRDHUP", scm_from_int (POLLRDHUP));
  162. #endif
  163. #ifdef POLLERR
  164. scm_c_define ("POLLERR", scm_from_int (POLLERR));
  165. #endif
  166. #ifdef POLLHUP
  167. scm_c_define ("POLLHUP", scm_from_int (POLLHUP));
  168. #endif
  169. #ifdef POLLNVAL
  170. scm_c_define ("POLLNVAL", scm_from_int (POLLNVAL));
  171. #endif
  172. }
  173. void
  174. scm_register_poll (void)
  175. {
  176. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  177. "scm_init_poll",
  178. (scm_t_extension_init_func) scm_init_poll,
  179. NULL);
  180. }
  181. /*
  182. Local Variables:
  183. c-file-style: "gnu"
  184. End:
  185. */