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].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 void backend_plan_changed(struct io_conn *conn)
82 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
87 pfd->events = conn->plan.pollflag;
89 int mask = conn->duplex->plan.pollflag;
90 /* You can't *both* read/write. */
91 assert(!mask || pfd->events != mask);
101 bool add_conn(struct io_conn *c)
103 if (!add_fd(&c->fd, c->plan.pollflag))
105 /* Immediate close is allowed. */
111 bool add_duplex(struct io_conn *c)
113 c->fd.backend_info = c->duplex->fd.backend_info;
114 backend_plan_changed(c);
118 static void del_conn(struct io_conn *conn)
121 conn->finish(conn, conn->finish_arg);
122 if (timeout_active(conn))
123 backend_del_timeout(conn);
126 /* In case fds[] pointed to the other one. */
127 fds[conn->fd.backend_info] = &conn->duplex->fd;
128 conn->duplex->duplex = NULL;
134 void del_listener(struct io_listener *l)
139 static void set_plan(struct io_conn *conn, struct io_plan plan)
142 backend_plan_changed(conn);
145 static void accept_conn(struct io_listener *l)
147 int fd = accept(l->fd.fd, NULL, NULL);
149 /* FIXME: What to do here? */
155 /* It's OK to miss some, as long as we make progress. */
156 static void finish_conns(void)
160 for (i = 0; !io_loop_return && i < num_fds; i++) {
161 struct io_conn *c, *duplex;
166 if (fds[i]->listener)
169 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
179 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
182 timers_init(&timeouts, time_now());
183 timer_add(&timeouts, &conn->timeout->timer,
184 time_add(time_now(), duration));
185 conn->timeout->conn = conn;
188 void backend_del_timeout(struct io_conn *conn)
190 assert(conn->timeout->conn == conn);
191 timer_del(&timeouts, &conn->timeout->timer);
192 conn->timeout->conn = NULL;
195 /* This is the main loop. */
200 while (!io_loop_return) {
201 int i, r, timeout = INT_MAX;
205 struct timespec first;
206 struct list_head expired;
207 struct io_timeout *t;
211 /* Call functions for expired timers. */
212 timers_expire(&timeouts, now, &expired);
213 while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
214 struct io_conn *conn = t->conn;
215 /* Clear, in case timer re-adds */
217 set_plan(conn, t->next(conn, t->next_arg));
220 /* Now figure out how long to wait for the next one. */
221 if (timer_earliest(&timeouts, &first)) {
222 uint64_t f = time_to_msec(time_sub(first, now));
230 /* Could have started/finished more. */
237 /* You can't tell them all to go to sleep! */
240 r = poll(pollfds, num_fds, timeout);
244 for (i = 0; i < num_fds && !io_loop_return; i++) {
245 struct io_conn *c = (void *)fds[i];
246 int events = pollfds[i].revents;
251 if (fds[i]->listener) {
252 if (events & POLLIN) {
253 accept_conn((void *)c);
256 } else if (events & (POLLIN|POLLOUT)) {
259 int mask = c->duplex->plan.pollflag;
263 if (!(events&(POLLIN|POLLOUT)))
268 } else if (events & POLLHUP) {
270 set_plan(c, io_close(c, NULL));
273 io_close(c->duplex, NULL));
281 ret = io_loop_return;
282 io_loop_return = NULL;