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;
46 static void del_fd(struct fd *fd)
48 size_t n = fd->backend_info;
52 if (pollfds[n].events)
54 if (n != num_fds - 1) {
55 /* Move last one over us. */
56 pollfds[n] = pollfds[num_fds-1];
57 fds[n] = fds[num_fds-1];
58 assert(fds[n]->backend_info == num_fds-1);
59 fds[n]->backend_info = n;
60 } else if (num_fds == 1) {
61 /* Free everything when no more fds. */
69 fd->backend_info = -1;
73 bool add_listener(struct io_listener *l)
75 if (!add_fd(&l->fd, POLLIN))
80 static void adjust_counts(enum io_state state)
84 else if (state == IO_FINISHED)
88 static void update_pollevents(struct io_conn *conn)
90 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
95 pfd->events = conn->plan.pollflag;
97 int mask = conn->duplex->plan.pollflag;
98 /* You can't *both* read/write. */
99 assert(!mask || pfd->events != mask);
105 adjust_counts(conn->plan.state);
108 bool add_conn(struct io_conn *c)
110 if (!add_fd(&c->fd, c->plan.pollflag))
112 adjust_counts(c->plan.state);
116 bool add_duplex(struct io_conn *c)
118 c->fd.backend_info = c->duplex->fd.backend_info;
119 update_pollevents(c);
123 static void del_conn(struct io_conn *conn)
126 conn->finish(conn, conn->finish_arg);
127 if (timeout_active(conn))
128 backend_del_timeout(conn);
131 /* In case fds[] pointed to the other one. */
132 fds[conn->fd.backend_info] = &conn->duplex->fd;
133 conn->duplex->duplex = NULL;
136 if (conn->plan.state == IO_FINISHED)
138 else if (conn->plan.state == IO_NEXT)
142 void del_listener(struct io_listener *l)
147 static void backend_set_state(struct io_conn *conn, struct io_plan plan)
150 update_pollevents(conn);
153 void backend_wakeup(struct io_conn *conn)
155 update_pollevents(conn);
158 static void accept_conn(struct io_listener *l)
160 int fd = accept(l->fd.fd, NULL, NULL);
162 /* FIXME: What to do here? */
168 /* It's OK to miss some, as long as we make progress. */
169 static void finish_and_next(bool finished_only)
173 for (i = 0; !io_loop_return && i < num_fds; i++) {
174 struct io_conn *c, *duplex;
177 if (finished_only || num_next == 0)
180 if (fds[i]->listener)
183 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
184 if (c->plan.state == IO_FINISHED) {
188 } else if (!finished_only && c->plan.state == IO_NEXT) {
189 backend_set_state(c, c->plan.next(c, c->plan.next_arg));
196 static void ready(struct io_conn *c)
198 backend_set_state(c, do_ready(c));
201 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
204 timers_init(&timeouts, time_now());
205 timer_add(&timeouts, &conn->timeout->timer,
206 time_add(time_now(), duration));
207 conn->timeout->conn = conn;
210 void backend_del_timeout(struct io_conn *conn)
212 assert(conn->timeout->conn == conn);
213 timer_del(&timeouts, &conn->timeout->timer);
214 conn->timeout->conn = NULL;
217 /* This is the main loop. */
222 while (!io_loop_return) {
223 int i, r, timeout = INT_MAX;
227 struct timespec first;
228 struct list_head expired;
229 struct io_timeout *t;
233 /* Call functions for expired timers. */
234 timers_expire(&timeouts, now, &expired);
235 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
236 struct io_conn *conn = t->conn;
237 /* Clear, in case timer re-adds */
239 backend_set_state(conn, t->next(conn, t->next_arg));
242 /* Now figure out how long to wait for the next one. */
243 if (timer_earliest(&timeouts, &first)) {
244 uint64_t f = time_to_msec(time_sub(first, now));
250 if (num_finished || num_next) {
251 finish_and_next(false);
252 /* Could have started/finished more. */
259 /* You can't tell them all to go to sleep! */
262 r = poll(pollfds, num_fds, timeout);
266 for (i = 0; i < num_fds && !io_loop_return; i++) {
267 struct io_conn *c = (void *)fds[i];
268 int events = pollfds[i].revents;
273 if (fds[i]->listener) {
274 if (events & POLLIN) {
275 accept_conn((void *)c);
278 } else if (events & (POLLIN|POLLOUT)) {
281 int mask = c->duplex->plan.pollflag;
285 if (!(events&(POLLIN|POLLOUT)))
290 } else if (events & POLLHUP) {
292 backend_set_state(c, io_close(c, NULL));
294 backend_set_state(c->duplex,
302 finish_and_next(true);
304 ret = io_loop_return;
305 io_loop_return = NULL;