ccan/io: remove IO_NEXT state.
[ccan] / ccan / io / poll.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "io.h"
3 #include "backend.h"
4 #include <assert.h>
5 #include <poll.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <limits.h>
10
11 static size_t num_fds = 0, max_fds = 0, num_finished = 0, num_waiting = 0;
12 static struct pollfd *pollfds = NULL;
13 static struct fd **fds = NULL;
14 static struct timers timeouts;
15
16 static bool add_fd(struct fd *fd, short events)
17 {
18         if (num_fds + 1 > max_fds) {
19                 struct pollfd *newpollfds;
20                 struct fd **newfds;
21                 size_t num = max_fds ? max_fds * 2 : 8;
22
23                 newpollfds = realloc(pollfds, sizeof(*newpollfds) * num);
24                 if (!newpollfds)
25                         return false;
26                 pollfds = newpollfds;
27                 newfds = realloc(fds, sizeof(*newfds) * num);
28                 if (!newfds)
29                         return false;
30                 fds = newfds;
31                 max_fds = num;
32         }
33
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 */
37         fds[num_fds] = fd;
38         fd->backend_info = num_fds;
39         num_fds++;
40         if (events)
41                 num_waiting++;
42
43         return true;
44 }
45
46 static void del_fd(struct fd *fd)
47 {
48         size_t n = fd->backend_info;
49
50         assert(n != -1);
51         assert(n < num_fds);
52         if (pollfds[n].events)
53                 num_waiting--;
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. */
62                 free(pollfds);
63                 free(fds);
64                 pollfds = NULL;
65                 fds = NULL;
66                 max_fds = 0;
67         }
68         num_fds--;
69         fd->backend_info = -1;
70         close(fd->fd);
71 }
72
73 bool add_listener(struct io_listener *l)
74 {
75         if (!add_fd(&l->fd, POLLIN))
76                 return false;
77         return true;
78 }
79
80 static void adjust_counts(enum io_state state)
81 {
82         if (state == IO_FINISHED)
83                 num_finished++;
84 }
85
86 static void update_pollevents(struct io_conn *conn)
87 {
88         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
89
90         if (pfd->events)
91                 num_waiting--;
92
93         pfd->events = conn->plan.pollflag;
94         if (conn->duplex) {
95                 int mask = conn->duplex->plan.pollflag;
96                 /* You can't *both* read/write. */
97                 assert(!mask || pfd->events != mask);
98                 pfd->events |= mask;
99         }
100         if (pfd->events)
101                 num_waiting++;
102
103         adjust_counts(conn->plan.state);
104 }
105
106 bool add_conn(struct io_conn *c)
107 {
108         if (!add_fd(&c->fd, c->plan.pollflag))
109                 return false;
110         adjust_counts(c->plan.state);
111         return true;
112 }
113
114 bool add_duplex(struct io_conn *c)
115 {
116         c->fd.backend_info = c->duplex->fd.backend_info;
117         update_pollevents(c);
118         return true;
119 }
120
121 static void del_conn(struct io_conn *conn)
122 {
123         assert(conn->plan.state == IO_FINISHED);
124         if (conn->finish)
125                 conn->finish(conn, conn->finish_arg);
126         if (timeout_active(conn))
127                 backend_del_timeout(conn);
128         free(conn->timeout);
129         if (conn->duplex) {
130                 /* In case fds[] pointed to the other one. */
131                 fds[conn->fd.backend_info] = &conn->duplex->fd;
132                 conn->duplex->duplex = NULL;
133         } else
134                 del_fd(&conn->fd);
135         num_finished--;
136 }
137
138 void del_listener(struct io_listener *l)
139 {
140         del_fd(&l->fd);
141 }
142
143 static void backend_set_state(struct io_conn *conn, struct io_plan plan)
144 {
145         conn->plan = plan;
146         update_pollevents(conn);
147 }
148
149 void backend_wakeup(struct io_conn *conn)
150 {
151         update_pollevents(conn);
152 }
153
154 static void accept_conn(struct io_listener *l)
155 {
156         int fd = accept(l->fd.fd, NULL, NULL);
157
158         /* FIXME: What to do here? */
159         if (fd < 0)
160                 return;
161         l->init(fd, l->arg);
162 }
163
164 /* It's OK to miss some, as long as we make progress. */
165 static void finish_conns(void)
166 {
167         unsigned int i;
168
169         for (i = 0; !io_loop_return && i < num_fds; i++) {
170                 struct io_conn *c, *duplex;
171
172                 if (!num_finished)
173                         break;
174
175                 if (fds[i]->listener)
176                         continue;
177                 c = (void *)fds[i];
178                 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
179                         if (c->plan.state == IO_FINISHED) {
180                                 del_conn(c);
181                                 free(c);
182                                 i--;
183                         }
184                 }
185         }
186 }
187
188 static void ready(struct io_conn *c)
189 {
190         backend_set_state(c, do_ready(c));
191 }
192
193 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
194 {
195         if (!timeouts.base)
196                 timers_init(&timeouts, time_now());
197         timer_add(&timeouts, &conn->timeout->timer,
198                   time_add(time_now(), duration));
199         conn->timeout->conn = conn;
200 }
201
202 void backend_del_timeout(struct io_conn *conn)
203 {
204         assert(conn->timeout->conn == conn);
205         timer_del(&timeouts, &conn->timeout->timer);
206         conn->timeout->conn = NULL;
207 }
208
209 /* This is the main loop. */
210 void *io_loop(void)
211 {
212         void *ret;
213
214         while (!io_loop_return) {
215                 int i, r, timeout = INT_MAX;
216                 struct timespec now;
217
218                 if (timeouts.base) {
219                         struct timespec first;
220                         struct list_head expired;
221                         struct io_timeout *t;
222
223                         now = time_now();
224
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 */
230                                 t->conn = NULL;
231                                 backend_set_state(conn, t->next(conn, t->next_arg));
232                         }
233
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));
237                                 if (f < INT_MAX)
238                                         timeout = f;
239                         }
240                 }
241
242                 if (num_finished) {
243                         finish_conns();
244                         /* Could have started/finished more. */
245                         continue;
246                 }
247
248                 if (num_fds == 0)
249                         break;
250
251                 /* You can't tell them all to go to sleep! */
252                 assert(num_waiting);
253
254                 r = poll(pollfds, num_fds, timeout);
255                 if (r < 0)
256                         break;
257
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;
261
262                         if (r == 0)
263                                 break;
264
265                         if (fds[i]->listener) {
266                                 if (events & POLLIN) {
267                                         accept_conn((void *)c);
268                                         r--;
269                                 }
270                         } else if (events & (POLLIN|POLLOUT)) {
271                                 r--;
272                                 if (c->duplex) {
273                                         int mask = c->duplex->plan.pollflag;
274                                         if (events & mask) {
275                                                 ready(c->duplex);
276                                                 events &= ~mask;
277                                                 if (!(events&(POLLIN|POLLOUT)))
278                                                         continue;
279                                         }
280                                 }
281                                 ready(c);
282                         } else if (events & POLLHUP) {
283                                 r--;
284                                 backend_set_state(c, io_close(c, NULL));
285                                 if (c->duplex)
286                                         backend_set_state(c->duplex,
287                                                           io_close(c->duplex,
288                                                                    NULL));
289                         }
290                 }
291         }
292
293         while (num_finished)
294                 finish_conns();
295
296         ret = io_loop_return;
297         io_loop_return = NULL;
298         return ret;
299 }