1 /* Licensed under BSD-MIT - see LICENSE file for details */
8 #include <sys/socket.h>
10 static size_t num_fds = 0, max_fds = 0, num_next = 0, num_finished = 0;
11 static struct pollfd *pollfds = NULL;
12 static struct fd **fds = NULL;
14 static bool add_fd(struct fd *fd, short events)
16 if (num_fds + 1 > max_fds) {
17 struct pollfd *newpollfds;
19 size_t num = max_fds ? max_fds * 2 : 8;
21 newpollfds = realloc(pollfds, sizeof(*newpollfds) * num);
25 newfds = realloc(fds, sizeof(*newfds) * num);
32 pollfds[num_fds].fd = fd->fd;
33 pollfds[num_fds].events = events;
34 pollfds[num_fds].revents = 0; /* In case we're iterating now */
36 fd->backend_info = num_fds;
41 static void del_fd(struct fd *fd)
43 size_t n = fd->backend_info;
47 if (n != num_fds - 1) {
48 /* Move last one over us. */
49 pollfds[n] = pollfds[num_fds-1];
50 fds[n] = fds[num_fds-1];
51 assert(fds[n]->backend_info == num_fds-1);
52 fds[n]->backend_info = n;
53 } else if (num_fds == 1) {
54 /* Free everything when no more fds. */
62 fd->backend_info = -1;
66 bool add_listener(struct io_listener *l)
68 return add_fd(&l->fd, POLLIN);
71 bool add_conn(struct io_conn *c)
73 if (!add_fd(&c->fd, 0))
79 bool add_duplex(struct io_conn *c)
81 c->fd.backend_info = c->duplex->fd.backend_info;
86 static void del_conn(struct io_conn *conn)
89 conn->fd.finish(conn, conn->fd.finish_arg);
91 /* In case fds[] pointed to the other one. */
92 fds[conn->fd.backend_info] = &conn->duplex->fd;
93 conn->duplex->duplex = NULL;
96 if (conn->state == FINISHED)
98 else if (conn->state == NEXT)
102 void del_listener(struct io_listener *l)
107 static int pollmask(enum io_state state)
121 void backend_set_state(struct io_conn *conn, struct io_op *op)
123 enum io_state state = from_ioop(op);
124 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
126 pfd->events = pollmask(state);
128 int mask = pollmask(conn->duplex->state);
129 /* You can't *both* read/write. */
130 assert(!mask || pfd->events != mask);
136 else if (state == FINISHED)
142 static void accept_conn(struct io_listener *l)
145 int fd = accept(l->fd.fd, NULL, NULL);
147 /* FIXME: What to do here? */
150 c = io_new_conn(fd, l->fd.next, l->fd.finish, l->fd.next_arg);
157 /* It's OK to miss some, as long as we make progress. */
158 static void finish_and_next(bool finished_only)
162 for (i = 0; !io_loop_return && i < num_fds; i++) {
163 struct io_conn *c, *duplex;
166 if (finished_only || num_next == 0)
169 if (fds[i]->listener)
172 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
173 if (c->state == FINISHED) {
177 } else if (!finished_only && c->state == NEXT) {
187 static void ready(struct io_conn *c)
189 backend_set_state(c, do_ready(c));
192 /* This is the main loop. */
197 while (!io_loop_return) {
200 if (num_finished || num_next) {
201 finish_and_next(false);
202 /* Could have started/finished more. */
209 r = poll(pollfds, num_fds, -1);
213 for (i = 0; i < num_fds && !io_loop_return; i++) {
214 struct io_conn *c = (void *)fds[i];
215 int events = pollfds[i].revents;
217 if (fds[i]->listener) {
219 accept_conn((void *)c);
220 } else if (events & (POLLIN|POLLOUT)) {
222 int mask = pollmask(c->duplex->state);
226 if (!(events&(POLLIN|POLLOUT)))
231 } else if (events & POLLHUP) {
232 backend_set_state(c, io_close(c, NULL));
234 backend_set_state(c->duplex,
243 finish_and_next(true);
245 ret = io_loop_return;
246 io_loop_return = NULL;