1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
8 #include <sys/socket.h>
11 static size_t num_fds = 0, max_fds = 0, num_closing = 0, num_waiting = 0;
12 static struct pollfd *pollfds = NULL;
13 static struct fd **fds = NULL;
14 static struct timers timeouts;
16 static unsigned int io_loop_level;
17 static struct io_conn *free_later;
18 static void io_loop_enter(void)
22 static void io_loop_exit(void)
25 if (io_loop_level == 0) {
28 struct io_conn *c = free_later;
29 free_later = c->finish_arg;
34 static void free_conn(struct io_conn *conn)
36 /* Only free on final exit: chain via finish. */
37 if (io_loop_level > 1) {
39 for (c = free_later; c; c = c->finish_arg)
41 conn->finish_arg = free_later;
47 static void io_loop_enter(void)
50 static void io_loop_exit(void)
53 static void free_conn(struct io_conn *conn)
59 static bool add_fd(struct fd *fd, short events)
61 if (num_fds + 1 > max_fds) {
62 struct pollfd *newpollfds;
64 size_t num = max_fds ? max_fds * 2 : 8;
66 newpollfds = realloc(pollfds, sizeof(*newpollfds) * num);
70 newfds = realloc(fds, sizeof(*newfds) * num);
77 pollfds[num_fds].events = events;
78 /* In case it's idle. */
80 pollfds[num_fds].fd = -fd->fd;
82 pollfds[num_fds].fd = fd->fd;
83 pollfds[num_fds].revents = 0; /* In case we're iterating now */
85 fd->backend_info = num_fds;
93 static void del_fd(struct fd *fd)
95 size_t n = fd->backend_info;
99 if (pollfds[n].events)
101 if (n != num_fds - 1) {
102 /* Move last one over us. */
103 pollfds[n] = pollfds[num_fds-1];
104 fds[n] = fds[num_fds-1];
105 assert(fds[n]->backend_info == num_fds-1);
106 fds[n]->backend_info = n;
107 } else if (num_fds == 1) {
108 /* Free everything when no more fds. */
116 fd->backend_info = -1;
120 bool add_listener(struct io_listener *l)
122 if (!add_fd(&l->fd, POLLIN))
127 void backend_plan_changed(struct io_conn *conn)
131 /* This can happen with debugging and delayed free... */
132 if (conn->fd.backend_info == -1)
135 pfd = &pollfds[conn->fd.backend_info];
140 pfd->events = conn->plan.pollflag;
142 int mask = conn->duplex->plan.pollflag;
143 /* You can't *both* read/write. */
144 assert(!mask || pfd->events != mask);
149 pfd->fd = conn->fd.fd;
151 pfd->fd = -conn->fd.fd;
153 if (!conn->plan.next)
157 bool add_conn(struct io_conn *c)
159 if (!add_fd(&c->fd, c->plan.pollflag))
161 /* Immediate close is allowed. */
167 bool add_duplex(struct io_conn *c)
169 c->fd.backend_info = c->duplex->fd.backend_info;
170 backend_plan_changed(c);
174 static void del_conn(struct io_conn *conn)
177 conn->finish(conn, conn->finish_arg);
178 if (timeout_active(conn))
179 backend_del_timeout(conn);
182 /* In case fds[] pointed to the other one. */
183 fds[conn->fd.backend_info] = &conn->duplex->fd;
184 conn->duplex->duplex = NULL;
185 conn->fd.backend_info = -1;
191 void del_listener(struct io_listener *l)
196 static void set_plan(struct io_conn *conn, struct io_plan plan)
199 backend_plan_changed(conn);
202 static void accept_conn(struct io_listener *l)
204 int fd = accept(l->fd.fd, NULL, NULL);
206 /* FIXME: What to do here? */
212 /* It's OK to miss some, as long as we make progress. */
213 static void finish_conns(void)
217 for (i = 0; !io_loop_return && i < num_fds; i++) {
218 struct io_conn *c, *duplex;
223 if (fds[i]->listener)
226 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
236 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
239 timers_init(&timeouts, time_now());
240 timer_add(&timeouts, &conn->timeout->timer,
241 time_add(time_now(), duration));
242 conn->timeout->conn = conn;
245 void backend_del_timeout(struct io_conn *conn)
247 assert(conn->timeout->conn == conn);
248 timer_del(&timeouts, &conn->timeout->timer);
249 conn->timeout->conn = NULL;
252 /* This is the main loop. */
259 while (!io_loop_return) {
260 int i, r, timeout = INT_MAX;
262 bool some_timeouts = false;
265 struct timespec first;
266 struct list_head expired;
267 struct io_timeout *t;
271 /* Call functions for expired timers. */
272 timers_expire(&timeouts, now, &expired);
273 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
274 struct io_conn *conn = t->conn;
275 /* Clear, in case timer re-adds */
278 set_plan(conn, t->next(conn, t->next_arg));
279 some_timeouts = true;
282 /* Now figure out how long to wait for the next one. */
283 if (timer_earliest(&timeouts, &first)) {
284 uint64_t f = time_to_msec(time_sub(first, now));
292 /* Could have started/finished more. */
296 /* debug can recurse on io_loop; anything can change. */
297 if (doing_debug() && some_timeouts)
303 /* You can't tell them all to go to sleep! */
306 r = poll(pollfds, num_fds, timeout);
310 for (i = 0; i < num_fds && !io_loop_return; i++) {
311 struct io_conn *c = (void *)fds[i];
312 int events = pollfds[i].revents;
317 if (fds[i]->listener) {
318 if (events & POLLIN) {
319 accept_conn((void *)c);
322 } else if (events & (POLLIN|POLLOUT)) {
325 int mask = c->duplex->plan.pollflag;
329 /* debug can recurse;
330 * anything can change. */
333 if (!(events&(POLLIN|POLLOUT)))
338 /* debug can recurse; anything can change. */
341 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
344 set_plan(c, io_close());
346 set_current(c->duplex);
347 set_plan(c->duplex, io_close());
356 ret = io_loop_return;
357 io_loop_return = NULL;