1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
8 #include <sys/socket.h>
12 static size_t num_fds = 0, max_fds = 0, num_closing = 0, num_waiting = 0;
13 static bool some_always = false;
14 static struct pollfd *pollfds = NULL;
15 static struct fd **fds = NULL;
16 static struct timers timeouts;
18 static unsigned int io_loop_level;
19 static struct io_conn *free_later;
20 static void io_loop_enter(void)
24 static void io_loop_exit(void)
27 if (io_loop_level == 0) {
30 struct io_conn *c = free_later;
31 free_later = c->finish_arg;
36 static void free_conn(struct io_conn *conn)
38 /* Only free on final exit: chain via finish. */
39 if (io_loop_level > 1) {
41 for (c = free_later; c; c = c->finish_arg)
43 conn->finish_arg = free_later;
49 static void io_loop_enter(void)
52 static void io_loop_exit(void)
55 static void free_conn(struct io_conn *conn)
61 static bool add_fd(struct fd *fd, short events)
63 if (num_fds + 1 > max_fds) {
64 struct pollfd *newpollfds;
66 size_t num = max_fds ? max_fds * 2 : 8;
68 newpollfds = io_alloc.realloc(pollfds, sizeof(*newpollfds)*num);
72 newfds = io_alloc.realloc(fds, sizeof(*newfds) * num);
79 pollfds[num_fds].events = events;
80 /* In case it's idle. */
82 pollfds[num_fds].fd = -fd->fd;
84 pollfds[num_fds].fd = fd->fd;
85 pollfds[num_fds].revents = 0; /* In case we're iterating now */
87 fd->backend_info = num_fds;
95 static void del_fd(struct fd *fd)
97 size_t n = fd->backend_info;
101 if (pollfds[n].events)
103 if (n != num_fds - 1) {
104 /* Move last one over us. */
105 pollfds[n] = pollfds[num_fds-1];
106 fds[n] = fds[num_fds-1];
107 assert(fds[n]->backend_info == num_fds-1);
108 fds[n]->backend_info = n;
109 /* If that happens to be a duplex, move that too. */
110 if (!fds[n]->listener) {
111 struct io_conn *c = (void *)fds[n];
113 assert(c->duplex->fd.backend_info == num_fds-1);
114 c->duplex->fd.backend_info = n;
117 } else if (num_fds == 1) {
118 /* Free everything when no more fds. */
119 io_alloc.free(pollfds);
126 fd->backend_info = -1;
130 bool add_listener(struct io_listener *l)
132 if (!add_fd(&l->fd, POLLIN))
137 void backend_plan_changed(struct io_conn *conn)
141 /* This can happen with debugging and delayed free... */
142 if (conn->fd.backend_info == -1)
145 pfd = &pollfds[conn->fd.backend_info];
150 pfd->events = conn->plan.pollflag & (POLLIN|POLLOUT);
152 int mask = conn->duplex->plan.pollflag & (POLLIN|POLLOUT);
153 /* You can't *both* read/write. */
154 assert(!mask || pfd->events != mask);
159 pfd->fd = conn->fd.fd;
161 pfd->fd = -conn->fd.fd;
163 if (!conn->plan.next)
166 if (conn->plan.pollflag == POLLALWAYS)
170 bool add_conn(struct io_conn *c)
172 if (!add_fd(&c->fd, c->plan.pollflag & (POLLIN|POLLOUT)))
174 /* Immediate close is allowed. */
177 if (c->plan.pollflag == POLLALWAYS)
182 bool add_duplex(struct io_conn *c)
184 c->fd.backend_info = c->duplex->fd.backend_info;
185 backend_plan_changed(c);
189 void backend_del_conn(struct io_conn *conn)
192 /* Saved by io_close */
193 errno = conn->plan.u1.s;
194 conn->finish(conn, conn->finish_arg);
196 if (timeout_active(conn))
197 backend_del_timeout(conn);
198 io_alloc.free(conn->timeout);
200 /* In case fds[] pointed to the other one. */
201 assert(conn->duplex->fd.backend_info == conn->fd.backend_info);
202 fds[conn->fd.backend_info] = &conn->duplex->fd;
203 conn->duplex->duplex = NULL;
204 conn->fd.backend_info = -1;
211 void del_listener(struct io_listener *l)
216 static void set_plan(struct io_conn *conn, struct io_plan plan)
219 backend_plan_changed(conn);
222 static void accept_conn(struct io_listener *l)
224 int fd = accept(l->fd.fd, NULL, NULL);
226 /* FIXME: What to do here? */
232 /* It's OK to miss some, as long as we make progress. */
233 static bool finish_conns(struct io_conn **ready)
237 for (i = 0; !io_loop_return && i < num_fds; i++) {
238 struct io_conn *c, *duplex;
243 if (fds[i]->listener)
246 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
248 if (doing_debug_on(c) && ready) {
260 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
263 timers_init(&timeouts, time_now());
264 timer_add(&timeouts, &conn->timeout->timer,
265 time_add(time_now(), duration));
266 conn->timeout->conn = conn;
269 void backend_del_timeout(struct io_conn *conn)
271 assert(conn->timeout->conn == conn);
272 timer_del(&timeouts, &conn->timeout->timer);
273 conn->timeout->conn = NULL;
276 static void handle_always(void)
282 for (i = 0; i < num_fds && !io_loop_return; i++) {
283 struct io_conn *c = (void *)fds[i];
285 if (fds[i]->listener)
288 if (c->plan.pollflag == POLLALWAYS)
291 if (c->duplex && c->duplex->plan.pollflag == POLLALWAYS)
296 /* This is the main loop. */
297 void *do_io_loop(struct io_conn **ready)
303 while (!io_loop_return) {
304 int i, r, timeout = INT_MAX;
306 bool some_timeouts = false;
309 struct timespec first;
310 struct list_head expired;
311 struct io_timeout *t;
315 /* Call functions for expired timers. */
316 timers_expire(&timeouts, now, &expired);
317 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
318 struct io_conn *conn = t->conn;
319 /* Clear, in case timer re-adds */
322 set_plan(conn, t->next(conn, t->next_arg));
323 some_timeouts = true;
326 /* Now figure out how long to wait for the next one. */
327 if (timer_earliest(&timeouts, &first)) {
328 uint64_t f = time_to_msec(time_sub(first, now));
335 /* If this finishes a debugging con, return now. */
336 if (finish_conns(ready))
338 /* Could have started/finished more. */
342 /* debug can recurse on io_loop; anything can change. */
343 if (doing_debug() && some_timeouts)
354 /* You can't tell them all to go to sleep! */
357 r = poll(pollfds, num_fds, timeout);
361 for (i = 0; i < num_fds && !io_loop_return; i++) {
362 struct io_conn *c = (void *)fds[i];
363 int events = pollfds[i].revents;
368 if (fds[i]->listener) {
369 if (events & POLLIN) {
370 accept_conn((void *)c);
373 } else if (events & (POLLIN|POLLOUT)) {
376 int mask = c->duplex->plan.pollflag;
378 if (doing_debug_on(c->duplex)
385 /* debug can recurse;
386 * anything can change. */
390 /* If no events, or it closed
391 * the duplex, continue. */
392 if (!(events&(POLLIN|POLLOUT))
397 if (doing_debug_on(c) && ready) {
402 /* debug can recurse; anything can change. */
405 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
409 set_plan(c, io_close());
411 set_current(c->duplex);
412 set_plan(c->duplex, io_close());
418 while (num_closing && !io_loop_return) {
419 if (finish_conns(ready))
423 ret = io_loop_return;
424 io_loop_return = NULL;
432 return do_io_loop(NULL);