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