1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
8 #include <sys/socket.h>
11 #include <ccan/time/time.h>
12 #include <ccan/timer/timer.h>
14 static size_t num_fds = 0, max_fds = 0, num_waiting = 0;
15 static struct pollfd *pollfds = NULL;
16 static struct fd **fds = NULL;
17 static LIST_HEAD(closing);
18 static LIST_HEAD(always);
19 static struct timemono (*nowfn)(void) = time_mono;
21 struct timemono (*io_time_override(struct timemono (*now)(void)))(void)
23 struct timemono (*old)(void) = nowfn;
28 static bool add_fd(struct fd *fd, short events)
32 pollfds = tal_arr(NULL, struct pollfd, 8);
35 fds = tal_arr(pollfds, struct fd *, 8);
41 if (num_fds + 1 > max_fds) {
42 size_t num = max_fds * 2;
44 if (!tal_resize(&pollfds, num))
46 if (!tal_resize(&fds, num))
51 pollfds[num_fds].events = events;
52 /* In case it's idle. */
54 pollfds[num_fds].fd = -fd->fd;
56 pollfds[num_fds].fd = fd->fd;
57 pollfds[num_fds].revents = 0; /* In case we're iterating now */
59 fd->backend_info = num_fds;
67 static void del_fd(struct fd *fd)
69 size_t n = fd->backend_info;
73 if (pollfds[n].events)
75 if (n != num_fds - 1) {
76 /* Move last one over us. */
77 pollfds[n] = pollfds[num_fds-1];
78 fds[n] = fds[num_fds-1];
79 assert(fds[n]->backend_info == num_fds-1);
80 fds[n]->backend_info = n;
81 } else if (num_fds == 1) {
82 /* Free everything when no more fds. */
83 pollfds = tal_free(pollfds);
88 fd->backend_info = -1;
91 static void destroy_listener(struct io_listener *l)
97 bool add_listener(struct io_listener *l)
99 if (!add_fd(&l->fd, POLLIN))
101 tal_add_destructor(l, destroy_listener);
105 void remove_from_always(struct io_conn *conn)
107 list_del_init(&conn->always);
110 void backend_new_always(struct io_conn *conn)
112 /* In case it's already in always list. */
113 list_del(&conn->always);
114 list_add_tail(&always, &conn->always);
117 void backend_new_plan(struct io_conn *conn)
119 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
125 if (conn->plan[IO_IN].status == IO_POLLING)
126 pfd->events |= POLLIN;
127 if (conn->plan[IO_OUT].status == IO_POLLING)
128 pfd->events |= POLLOUT;
132 pfd->fd = conn->fd.fd;
134 pfd->fd = -conn->fd.fd;
138 void backend_wake(const void *wait)
142 for (i = 0; i < num_fds; i++) {
145 /* Ignore listeners */
146 if (fds[i]->listener)
150 if (c->plan[IO_IN].status == IO_WAITING
151 && c->plan[IO_IN].arg.u1.const_vp == wait)
152 io_do_wakeup(c, IO_IN);
154 if (c->plan[IO_OUT].status == IO_WAITING
155 && c->plan[IO_OUT].arg.u1.const_vp == wait)
156 io_do_wakeup(c, IO_OUT);
160 static void destroy_conn(struct io_conn *conn, bool close_fd)
162 int saved_errno = errno;
167 /* In case it's on always list, remove it. */
168 list_del_init(&conn->always);
170 /* errno saved/restored by tal_free itself. */
173 conn->finish(conn, conn->finish_arg);
177 static void destroy_conn_close_fd(struct io_conn *conn)
179 destroy_conn(conn, true);
182 bool add_conn(struct io_conn *c)
184 if (!add_fd(&c->fd, 0))
186 tal_add_destructor(c, destroy_conn_close_fd);
190 struct io_plan *io_close_taken_fd(struct io_conn *conn)
192 tal_del_destructor(conn, destroy_conn_close_fd);
193 destroy_conn(conn, false);
194 return io_close(conn);
197 static void accept_conn(struct io_listener *l)
199 int fd = accept(l->fd.fd, NULL, NULL);
201 /* FIXME: What to do here? */
205 io_new_conn(l->ctx, fd, l->init, l->arg);
208 static bool handle_always(void)
211 struct io_conn *conn;
213 while ((conn = list_pop(&always, struct io_conn, always)) != NULL) {
214 assert(conn->plan[IO_IN].status == IO_ALWAYS
215 || conn->plan[IO_OUT].status == IO_ALWAYS);
217 /* Re-initialize, for next time. */
218 list_node_init(&conn->always);
225 /* This is the main loop. */
226 void *io_loop(struct timers *timers, struct timer **expired)
230 /* if timers is NULL, expired must be. If not, not. */
231 assert(!timers == !expired);
233 /* Make sure this is NULL if we exit for some other reason. */
237 while (!io_loop_return) {
238 int i, r, ms_timeout = -1;
240 if (handle_always()) {
241 /* Could have started/finished more. */
245 /* Everything closed? */
249 /* You can't tell them all to go to sleep! */
253 struct timemono now, first;
257 /* Call functions for expired timers. */
258 *expired = timers_expire(timers, now);
262 /* Now figure out how long to wait for the next one. */
263 if (timer_earliest(timers, &first)) {
265 next = time_to_msec(timemono_between(first, now));
269 ms_timeout = INT_MAX;
273 r = poll(pollfds, num_fds, ms_timeout);
277 for (i = 0; i < num_fds && !io_loop_return; i++) {
278 struct io_conn *c = (void *)fds[i];
279 int events = pollfds[i].revents;
284 if (fds[i]->listener) {
285 struct io_listener *l = (void *)fds[i];
286 if (events & POLLIN) {
289 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
292 io_close_listener(l);
294 } else if (events & (POLLIN|POLLOUT)) {
297 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
305 ret = io_loop_return;
306 io_loop_return = NULL;