poll.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/numbers.h"
  26. #include "libguile/error.h"
  27. #include "libguile/validate.h"
  28. #include "libguile/poll.h"
  29. /* {Poll}
  30. */
  31. /* Poll a set of file descriptors, waiting until one or more of them is
  32. ready to perform input or output.
  33. This is a low-level interface. See the `(ice-9 poll)' module for a more
  34. usable wrapper.
  35. `pollfds' is expected to be a bytevector, laid out in contiguous blocks of 64
  36. bits. Each block has the format of one `struct pollfd': a 32-bit int file
  37. descriptor, a 16-bit int events mask, and a 16-bit int revents mask.
  38. The number of pollfd structures in `pollfds' is specified in
  39. `nfds'. `pollfds' must be at least long enough to support that number of
  40. structures. It may be longer, in which case the trailing entries are left
  41. untouched.
  42. The pollfds bytevector is modified directly, setting the returned events in
  43. the final two bytes (the revents member).
  44. Since Scheme ports can buffer input or output in userspace, a Scheme
  45. poll interface needs to take that into account as well. The `ports'
  46. argument, a vector big enough for `nfds' elements, is given for this
  47. purpose. If a pollfd entry has a corresponding open port, that port
  48. is scanned for available input or output before dropping into the
  49. poll. If any port has buffered I/O available, the poll syscall is
  50. still issued, but with a timeout of 0 milliseconds, and a full port
  51. scan occurs after the poll returns.
  52. If timeout is given and is non-negative, the poll will return after that
  53. number of milliseconds if no fd became active.
  54. */
  55. static SCM
  56. scm_primitive_poll (SCM pollfds, SCM nfds, SCM ports, SCM timeout)
  57. #define FUNC_NAME "primitive-poll"
  58. {
  59. int rv = 0;
  60. nfds_t i;
  61. nfds_t c_nfds;
  62. int c_timeout;
  63. int have_buffered_io = 0;
  64. struct pollfd *fds;
  65. SCM_VALIDATE_BYTEVECTOR (SCM_ARG1, pollfds);
  66. c_nfds = scm_to_uint32 (nfds);
  67. c_timeout = scm_to_int (timeout);
  68. if (SCM_UNLIKELY (SCM_BYTEVECTOR_LENGTH (pollfds)
  69. < c_nfds * sizeof(struct pollfd)))
  70. SCM_OUT_OF_RANGE (SCM_ARG2, nfds);
  71. SCM_VALIDATE_VECTOR (SCM_ARG3, ports);
  72. if (SCM_UNLIKELY (SCM_SIMPLE_VECTOR_LENGTH (ports) < c_nfds))
  73. SCM_OUT_OF_RANGE (SCM_ARG3, ports);
  74. fds = (struct pollfd*)SCM_BYTEVECTOR_CONTENTS (pollfds);
  75. for (i = 0; i < c_nfds; i++)
  76. {
  77. SCM port = SCM_SIMPLE_VECTOR_REF (ports, i);
  78. short int revents = 0;
  79. if (SCM_PORTP (port))
  80. {
  81. if (SCM_CLOSEDP (port))
  82. revents |= POLLERR;
  83. else
  84. {
  85. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  86. if (pt->read_pos < pt->read_end)
  87. /* Buffered input waiting to be read. */
  88. revents |= POLLIN;
  89. if (pt->write_pos < pt->write_end)
  90. /* Buffered output possible. */
  91. revents |= POLLOUT;
  92. }
  93. }
  94. if (revents & fds[i].events)
  95. {
  96. have_buffered_io = 1;
  97. c_timeout = 0;
  98. break;
  99. }
  100. }
  101. SCM_SYSCALL (rv = poll (fds, c_nfds, c_timeout));
  102. if (rv == -1)
  103. SCM_SYSERROR;
  104. if (have_buffered_io)
  105. for (i = 0; i < c_nfds; i++)
  106. {
  107. SCM port = SCM_SIMPLE_VECTOR_REF (ports, i);
  108. short int revents = 0;
  109. if (SCM_PORTP (port))
  110. {
  111. if (SCM_CLOSEDP (port))
  112. revents |= POLLERR;
  113. else
  114. {
  115. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  116. if (pt->read_pos < pt->read_end)
  117. /* Buffered input waiting to be read. */
  118. revents |= POLLIN;
  119. if (SCM_OUTPUT_PORT_P (port) && pt->write_pos < pt->write_end)
  120. /* Buffered output possible. */
  121. revents |= POLLOUT;
  122. }
  123. }
  124. /* Mask in the events we are interested, and test if any are
  125. interesting. */
  126. if ((revents &= fds[i].events))
  127. {
  128. /* Could be the underlying fd is also ready for reading. */
  129. if (!fds[i].revents)
  130. rv++;
  131. /* In any case, add these events to whatever the syscall
  132. set. */
  133. fds[i].revents |= revents;
  134. }
  135. }
  136. return scm_from_int (rv);
  137. }
  138. #undef FUNC_NAME
  139. static void
  140. scm_init_poll (void)
  141. {
  142. scm_c_define_gsubr ("primitive-poll", 4, 0, 0, scm_primitive_poll);
  143. scm_c_define ("%sizeof-struct-pollfd", scm_from_size_t (sizeof (struct pollfd)));
  144. #ifdef POLLIN
  145. scm_c_define ("POLLIN", scm_from_int (POLLIN));
  146. #endif
  147. #ifdef POLLPRI
  148. scm_c_define ("POLLPRI", scm_from_int (POLLPRI));
  149. #endif
  150. #ifdef POLLOUT
  151. scm_c_define ("POLLOUT", scm_from_int (POLLOUT));
  152. #endif
  153. #ifdef POLLRDHUP
  154. scm_c_define ("POLLRDHUP", scm_from_int (POLLRDHUP));
  155. #endif
  156. #ifdef POLLERR
  157. scm_c_define ("POLLERR", scm_from_int (POLLERR));
  158. #endif
  159. #ifdef POLLHUP
  160. scm_c_define ("POLLHUP", scm_from_int (POLLHUP));
  161. #endif
  162. #ifdef POLLNVAL
  163. scm_c_define ("POLLNVAL", scm_from_int (POLLNVAL));
  164. #endif
  165. }
  166. void
  167. scm_register_poll (void)
  168. {
  169. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  170. "scm_init_poll",
  171. (scm_t_extension_init_func) scm_init_poll,
  172. NULL);
  173. }
  174. /*
  175. Local Variables:
  176. c-file-style: "gnu"
  177. End:
  178. */