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 /* If that happens to be a duplex, move that too. */
109 if (!fds[n]->listener) {
110 struct io_conn *c = (void *)fds[n];
112 assert(c->duplex->fd.backend_info == num_fds-1);
113 c->duplex->fd.backend_info = n;
116 } else if (num_fds == 1) {
117 /* Free everything when no more fds. */
118 io_alloc.free(pollfds);
125 fd->backend_info = -1;
129 bool add_listener(struct io_listener *l)
131 if (!add_fd(&l->fd, POLLIN))
136 void backend_plan_changed(struct io_conn *conn)
140 /* This can happen with debugging and delayed free... */
141 if (conn->fd.backend_info == -1)
144 pfd = &pollfds[conn->fd.backend_info];
149 pfd->events = conn->plan.pollflag;
151 int mask = conn->duplex->plan.pollflag;
152 /* You can't *both* read/write. */
153 assert(!mask || pfd->events != mask);
158 pfd->fd = conn->fd.fd;
160 pfd->fd = -conn->fd.fd;
162 if (!conn->plan.next)
166 bool add_conn(struct io_conn *c)
168 if (!add_fd(&c->fd, c->plan.pollflag))
170 /* Immediate close is allowed. */
176 bool add_duplex(struct io_conn *c)
178 c->fd.backend_info = c->duplex->fd.backend_info;
179 backend_plan_changed(c);
183 void backend_del_conn(struct io_conn *conn)
186 /* Saved by io_close */
187 errno = conn->plan.u1.s;
188 conn->finish(conn, conn->finish_arg);
190 if (timeout_active(conn))
191 backend_del_timeout(conn);
192 io_alloc.free(conn->timeout);
194 /* In case fds[] pointed to the other one. */
195 assert(conn->duplex->fd.backend_info == conn->fd.backend_info);
196 fds[conn->fd.backend_info] = &conn->duplex->fd;
197 conn->duplex->duplex = NULL;
198 conn->fd.backend_info = -1;
205 void del_listener(struct io_listener *l)
210 static void set_plan(struct io_conn *conn, struct io_plan plan)
213 backend_plan_changed(conn);
216 static void accept_conn(struct io_listener *l)
218 int fd = accept(l->fd.fd, NULL, NULL);
220 /* FIXME: What to do here? */
226 /* It's OK to miss some, as long as we make progress. */
227 static bool finish_conns(struct io_conn **ready)
231 for (i = 0; !io_loop_return && i < num_fds; i++) {
232 struct io_conn *c, *duplex;
237 if (fds[i]->listener)
240 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
242 if (doing_debug_on(c) && ready) {
254 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
257 timers_init(&timeouts, time_now());
258 timer_add(&timeouts, &conn->timeout->timer,
259 time_add(time_now(), duration));
260 conn->timeout->conn = conn;
263 void backend_del_timeout(struct io_conn *conn)
265 assert(conn->timeout->conn == conn);
266 timer_del(&timeouts, &conn->timeout->timer);
267 conn->timeout->conn = NULL;
270 /* This is the main loop. */
271 void *do_io_loop(struct io_conn **ready)
277 while (!io_loop_return) {
278 int i, r, timeout = INT_MAX;
280 bool some_timeouts = false;
283 struct timespec first;
284 struct list_head expired;
285 struct io_timeout *t;
289 /* Call functions for expired timers. */
290 timers_expire(&timeouts, now, &expired);
291 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
292 struct io_conn *conn = t->conn;
293 /* Clear, in case timer re-adds */
296 set_plan(conn, t->next(conn, t->next_arg));
297 some_timeouts = true;
300 /* Now figure out how long to wait for the next one. */
301 if (timer_earliest(&timeouts, &first)) {
302 uint64_t f = time_to_msec(time_sub(first, now));
309 /* If this finishes a debugging con, return now. */
310 if (finish_conns(ready))
312 /* Could have started/finished more. */
316 /* debug can recurse on io_loop; anything can change. */
317 if (doing_debug() && some_timeouts)
323 /* You can't tell them all to go to sleep! */
326 r = poll(pollfds, num_fds, timeout);
330 for (i = 0; i < num_fds && !io_loop_return; i++) {
331 struct io_conn *c = (void *)fds[i];
332 int events = pollfds[i].revents;
337 if (fds[i]->listener) {
338 if (events & POLLIN) {
339 accept_conn((void *)c);
342 } else if (events & (POLLIN|POLLOUT)) {
345 int mask = c->duplex->plan.pollflag;
347 if (doing_debug_on(c->duplex)
354 /* debug can recurse;
355 * anything can change. */
359 /* If no events, or it closed
360 * the duplex, continue. */
361 if (!(events&(POLLIN|POLLOUT))
366 if (doing_debug_on(c) && ready) {
371 /* debug can recurse; anything can change. */
374 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
378 set_plan(c, io_close());
380 set_current(c->duplex);
381 set_plan(c->duplex, io_close());
387 while (num_closing && !io_loop_return) {
388 if (finish_conns(ready))
392 ret = io_loop_return;
393 io_loop_return = NULL;
401 return do_io_loop(NULL);