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_next = 0, num_finished = 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].fd = fd->fd;
35 pollfds[num_fds].events = events;
36 pollfds[num_fds].revents = 0; /* In case we're iterating now */
38 fd->backend_info = num_fds;
43 static void del_fd(struct fd *fd)
45 size_t n = fd->backend_info;
49 if (n != num_fds - 1) {
50 /* Move last one over us. */
51 pollfds[n] = pollfds[num_fds-1];
52 fds[n] = fds[num_fds-1];
53 assert(fds[n]->backend_info == num_fds-1);
54 fds[n]->backend_info = n;
55 } else if (num_fds == 1) {
56 /* Free everything when no more fds. */
64 fd->backend_info = -1;
68 bool add_listener(struct io_listener *l)
70 if (!add_fd(&l->fd, POLLIN))
76 bool add_conn(struct io_conn *c)
78 if (!add_fd(&c->fd, 0))
84 bool add_duplex(struct io_conn *c)
86 c->fd.backend_info = c->duplex->fd.backend_info;
91 static void del_conn(struct io_conn *conn)
94 conn->finish(conn, conn->finish_arg);
95 if (timeout_active(conn))
96 backend_del_timeout(conn);
99 /* In case fds[] pointed to the other one. */
100 fds[conn->fd.backend_info] = &conn->duplex->fd;
101 conn->duplex->duplex = NULL;
104 if (conn->plan.state == IO_FINISHED)
106 else if (conn->plan.state == IO_NEXT)
110 void del_listener(struct io_listener *l)
115 static void backend_set_state(struct io_conn *conn, struct io_plan plan)
117 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
122 pfd->events = plan.pollflag;
124 int mask = conn->duplex->plan.pollflag;
125 /* You can't *both* read/write. */
126 assert(!mask || pfd->events != mask);
132 if (plan.state == IO_NEXT)
134 else if (plan.state == IO_FINISHED)
140 void backend_wakeup(struct io_conn *conn)
145 static void accept_conn(struct io_listener *l)
148 int fd = accept(l->fd.fd, NULL, NULL);
150 /* FIXME: What to do here? */
153 c = io_new_conn(fd, l->next, l->finish, l->conn_arg);
160 /* It's OK to miss some, as long as we make progress. */
161 static void finish_and_next(bool finished_only)
165 for (i = 0; !io_loop_return && i < num_fds; i++) {
166 struct io_conn *c, *duplex;
169 if (finished_only || num_next == 0)
172 if (fds[i]->listener)
175 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
176 if (c->plan.state == IO_FINISHED) {
180 } else if (!finished_only && c->plan.state == IO_NEXT) {
181 backend_set_state(c, c->plan.next(c, c->plan.next_arg));
188 static void ready(struct io_conn *c)
190 backend_set_state(c, do_ready(c));
193 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
196 timers_init(&timeouts, time_now());
197 timer_add(&timeouts, &conn->timeout->timer,
198 time_add(time_now(), duration));
199 conn->timeout->conn = conn;
202 void backend_del_timeout(struct io_conn *conn)
204 assert(conn->timeout->conn == conn);
205 timer_del(&timeouts, &conn->timeout->timer);
206 conn->timeout->conn = NULL;
209 /* This is the main loop. */
214 while (!io_loop_return) {
215 int i, r, timeout = INT_MAX;
219 struct timespec first;
220 struct list_head expired;
221 struct io_timeout *t;
225 /* Call functions for expired timers. */
226 timers_expire(&timeouts, now, &expired);
227 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
228 struct io_conn *conn = t->conn;
229 /* Clear, in case timer re-adds */
231 backend_set_state(conn, t->next(conn, t->next_arg));
234 /* Now figure out how long to wait for the next one. */
235 if (timer_earliest(&timeouts, &first)) {
236 uint64_t f = time_to_msec(time_sub(first, now));
242 if (num_finished || num_next) {
243 finish_and_next(false);
244 /* Could have started/finished more. */
251 /* You can't tell them all to go to sleep! */
254 r = poll(pollfds, num_fds, timeout);
258 for (i = 0; i < num_fds && !io_loop_return; i++) {
259 struct io_conn *c = (void *)fds[i];
260 int events = pollfds[i].revents;
265 if (fds[i]->listener) {
266 if (events & POLLIN) {
267 accept_conn((void *)c);
270 } else if (events & (POLLIN|POLLOUT)) {
273 int mask = c->duplex->plan.pollflag;
277 if (!(events&(POLLIN|POLLOUT)))
282 } else if (events & POLLHUP) {
284 backend_set_state(c, io_close(c, NULL));
286 backend_set_state(c->duplex,
294 finish_and_next(true);
296 ret = io_loop_return;
297 io_loop_return = NULL;