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 bool add_fd(struct fd *fd, short events)
18 if (num_fds + 1 > max_fds) {
19 struct pollfd *newpollfds;
21 size_t num = max_fds ? max_fds * 2 : 8;
23 newpollfds = realloc(pollfds, sizeof(*newpollfds) * num);
27 newfds = realloc(fds, sizeof(*newfds) * num);
34 pollfds[num_fds].events = events;
35 /* In case it's idle. */
37 pollfds[num_fds].fd = -fd->fd;
39 pollfds[num_fds].fd = fd->fd;
40 pollfds[num_fds].revents = 0; /* In case we're iterating now */
42 fd->backend_info = num_fds;
50 static void del_fd(struct fd *fd)
52 size_t n = fd->backend_info;
56 if (pollfds[n].events)
58 if (n != num_fds - 1) {
59 /* Move last one over us. */
60 pollfds[n] = pollfds[num_fds-1];
61 fds[n] = fds[num_fds-1];
62 assert(fds[n]->backend_info == num_fds-1);
63 fds[n]->backend_info = n;
64 } else if (num_fds == 1) {
65 /* Free everything when no more fds. */
73 fd->backend_info = -1;
77 bool add_listener(struct io_listener *l)
79 if (!add_fd(&l->fd, POLLIN))
84 void backend_plan_changed(struct io_conn *conn)
86 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
91 pfd->events = conn->plan.pollflag;
93 int mask = conn->duplex->plan.pollflag;
94 /* You can't *both* read/write. */
95 assert(!mask || pfd->events != mask);
100 pfd->fd = conn->fd.fd;
102 pfd->fd = -conn->fd.fd;
104 if (!conn->plan.next)
108 bool add_conn(struct io_conn *c)
110 if (!add_fd(&c->fd, c->plan.pollflag))
112 /* Immediate close is allowed. */
118 bool add_duplex(struct io_conn *c)
120 c->fd.backend_info = c->duplex->fd.backend_info;
121 backend_plan_changed(c);
125 static void del_conn(struct io_conn *conn)
128 conn->finish(conn, conn->finish_arg);
129 if (timeout_active(conn))
130 backend_del_timeout(conn);
133 /* In case fds[] pointed to the other one. */
134 fds[conn->fd.backend_info] = &conn->duplex->fd;
135 conn->duplex->duplex = NULL;
141 void del_listener(struct io_listener *l)
146 static void set_plan(struct io_conn *conn, struct io_plan plan)
149 backend_plan_changed(conn);
152 static void accept_conn(struct io_listener *l)
154 int fd = accept(l->fd.fd, NULL, NULL);
156 /* FIXME: What to do here? */
162 /* It's OK to miss some, as long as we make progress. */
163 static void finish_conns(void)
167 for (i = 0; !io_loop_return && i < num_fds; i++) {
168 struct io_conn *c, *duplex;
173 if (fds[i]->listener)
176 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
186 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
189 timers_init(&timeouts, time_now());
190 timer_add(&timeouts, &conn->timeout->timer,
191 time_add(time_now(), duration));
192 conn->timeout->conn = conn;
195 void backend_del_timeout(struct io_conn *conn)
197 assert(conn->timeout->conn == conn);
198 timer_del(&timeouts, &conn->timeout->timer);
199 conn->timeout->conn = NULL;
202 /* This is the main loop. */
207 while (!io_loop_return) {
208 int i, r, timeout = INT_MAX;
212 struct timespec first;
213 struct list_head expired;
214 struct io_timeout *t;
218 /* Call functions for expired timers. */
219 timers_expire(&timeouts, now, &expired);
220 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
221 struct io_conn *conn = t->conn;
222 /* Clear, in case timer re-adds */
224 set_plan(conn, t->next(conn, t->next_arg));
227 /* Now figure out how long to wait for the next one. */
228 if (timer_earliest(&timeouts, &first)) {
229 uint64_t f = time_to_msec(time_sub(first, now));
237 /* Could have started/finished more. */
244 /* You can't tell them all to go to sleep! */
247 r = poll(pollfds, num_fds, timeout);
251 for (i = 0; i < num_fds && !io_loop_return; i++) {
252 struct io_conn *c = (void *)fds[i];
253 int events = pollfds[i].revents;
258 if (fds[i]->listener) {
259 if (events & POLLIN) {
260 accept_conn((void *)c);
263 } else if (events & (POLLIN|POLLOUT)) {
266 int mask = c->duplex->plan.pollflag;
270 if (!(events&(POLLIN|POLLOUT)))
275 } else if (events & POLLHUP) {
277 set_plan(c, io_close(c, NULL));
280 io_close(c->duplex, NULL));
288 ret = io_loop_return;
289 io_loop_return = NULL;