poll.c 6.3 KB

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