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 struct pollfd *pollfds = NULL;
14 static struct fd **fds = NULL;
15 static struct timers timeouts;
17 static unsigned int io_loop_level;
18 static struct io_conn *free_later;
19 static void io_loop_enter(void)
23 static void io_loop_exit(void)
26 if (io_loop_level == 0) {
29 struct io_conn *c = free_later;
30 free_later = c->finish_arg;
35 static void free_conn(struct io_conn *conn)
37 /* Only free on final exit: chain via finish. */
38 if (io_loop_level > 1) {
40 for (c = free_later; c; c = c->finish_arg)
42 conn->finish_arg = free_later;
48 static void io_loop_enter(void)
51 static void io_loop_exit(void)
54 static void free_conn(struct io_conn *conn)
60 static bool add_fd(struct fd *fd, short events)
62 if (num_fds + 1 > max_fds) {
63 struct pollfd *newpollfds;
65 size_t num = max_fds ? max_fds * 2 : 8;
67 newpollfds = io_alloc.realloc(pollfds, sizeof(*newpollfds)*num);
71 newfds = io_alloc.realloc(fds, sizeof(*newfds) * num);
78 pollfds[num_fds].events = events;
79 /* In case it's idle. */
81 pollfds[num_fds].fd = -fd->fd;
83 pollfds[num_fds].fd = fd->fd;
84 pollfds[num_fds].revents = 0; /* In case we're iterating now */
86 fd->backend_info = num_fds;
94 static void del_fd(struct fd *fd)
96 size_t n = fd->backend_info;
100 if (pollfds[n].events)
102 if (n != num_fds - 1) {
103 /* Move last one over us. */
104 pollfds[n] = pollfds[num_fds-1];
105 fds[n] = fds[num_fds-1];
106 assert(fds[n]->backend_info == num_fds-1);
107 fds[n]->backend_info = n;
108 } else if (num_fds == 1) {
109 /* Free everything when no more fds. */
110 io_alloc.free(pollfds);
117 fd->backend_info = -1;
121 bool add_listener(struct io_listener *l)
123 if (!add_fd(&l->fd, POLLIN))
128 void backend_plan_changed(struct io_conn *conn)
132 /* This can happen with debugging and delayed free... */
133 if (conn->fd.backend_info == -1)
136 pfd = &pollfds[conn->fd.backend_info];
141 pfd->events = conn->plan.pollflag;
143 int mask = conn->duplex->plan.pollflag;
144 /* You can't *both* read/write. */
145 assert(!mask || pfd->events != mask);
150 pfd->fd = conn->fd.fd;
152 pfd->fd = -conn->fd.fd;
154 if (!conn->plan.next)
158 bool add_conn(struct io_conn *c)
160 if (!add_fd(&c->fd, c->plan.pollflag))
162 /* Immediate close is allowed. */
168 bool add_duplex(struct io_conn *c)
170 c->fd.backend_info = c->duplex->fd.backend_info;
171 backend_plan_changed(c);
175 void backend_del_conn(struct io_conn *conn)
178 /* Saved by io_close */
179 errno = conn->plan.u1.s;
180 conn->finish(conn, conn->finish_arg);
182 if (timeout_active(conn))
183 backend_del_timeout(conn);
184 io_alloc.free(conn->timeout);
186 /* In case fds[] pointed to the other one. */
187 fds[conn->fd.backend_info] = &conn->duplex->fd;
188 conn->duplex->duplex = NULL;
189 conn->fd.backend_info = -1;
196 void del_listener(struct io_listener *l)
201 static void set_plan(struct io_conn *conn, struct io_plan plan)
204 backend_plan_changed(conn);
207 static void accept_conn(struct io_listener *l)
209 int fd = accept(l->fd.fd, NULL, NULL);
211 /* FIXME: What to do here? */
217 /* It's OK to miss some, as long as we make progress. */
218 static bool finish_conns(struct io_conn **ready)
222 for (i = 0; !io_loop_return && i < num_fds; i++) {
223 struct io_conn *c, *duplex;
228 if (fds[i]->listener)
231 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
233 if (doing_debug_on(c) && ready) {
245 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
248 timers_init(&timeouts, time_now());
249 timer_add(&timeouts, &conn->timeout->timer,
250 time_add(time_now(), duration));
251 conn->timeout->conn = conn;
254 void backend_del_timeout(struct io_conn *conn)
256 assert(conn->timeout->conn == conn);
257 timer_del(&timeouts, &conn->timeout->timer);
258 conn->timeout->conn = NULL;
261 /* This is the main loop. */
262 void *do_io_loop(struct io_conn **ready)
268 while (!io_loop_return) {
269 int i, r, timeout = INT_MAX;
271 bool some_timeouts = false;
274 struct timespec first;
275 struct list_head expired;
276 struct io_timeout *t;
280 /* Call functions for expired timers. */
281 timers_expire(&timeouts, now, &expired);
282 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
283 struct io_conn *conn = t->conn;
284 /* Clear, in case timer re-adds */
287 set_plan(conn, t->next(conn, t->next_arg));
288 some_timeouts = true;
291 /* Now figure out how long to wait for the next one. */
292 if (timer_earliest(&timeouts, &first)) {
293 uint64_t f = time_to_msec(time_sub(first, now));
300 /* If this finishes a debugging con, return now. */
301 if (finish_conns(ready))
303 /* Could have started/finished more. */
307 /* debug can recurse on io_loop; anything can change. */
308 if (doing_debug() && some_timeouts)
314 /* You can't tell them all to go to sleep! */
317 r = poll(pollfds, num_fds, timeout);
321 for (i = 0; i < num_fds && !io_loop_return; i++) {
322 struct io_conn *c = (void *)fds[i];
323 int events = pollfds[i].revents;
328 if (fds[i]->listener) {
329 if (events & POLLIN) {
330 accept_conn((void *)c);
333 } else if (events & (POLLIN|POLLOUT)) {
336 int mask = c->duplex->plan.pollflag;
338 if (doing_debug_on(c->duplex)
345 /* debug can recurse;
346 * anything can change. */
349 if (!(events&(POLLIN|POLLOUT)))
353 if (doing_debug_on(c) && ready) {
358 /* debug can recurse; anything can change. */
361 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
365 set_plan(c, io_close());
367 set_current(c->duplex);
368 set_plan(c->duplex, io_close());
374 while (num_closing && !io_loop_return) {
375 if (finish_conns(ready))
379 ret = io_loop_return;
380 io_loop_return = NULL;
388 return do_io_loop(NULL);