]> git.ozlabs.org Git - ccan/blob - ccan/io/poll.c
ccan/io: replace backend_set_state with backend_wakeup()
[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_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;
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         return true;
41 }
42
43 static void del_fd(struct fd *fd)
44 {
45         size_t n = fd->backend_info;
46
47         assert(n != -1);
48         assert(n < num_fds);
49         if (n != num_fds - 1) {
50                 /* Move last one over us. */
51                 pollfds[n] = pollfds[num_fds-1];
52                 fds[n] = fds[num_fds-1];
53                 assert(fds[n]->backend_info == num_fds-1);
54                 fds[n]->backend_info = n;
55         } else if (num_fds == 1) {
56                 /* Free everything when no more fds. */
57                 free(pollfds);
58                 free(fds);
59                 pollfds = NULL;
60                 fds = NULL;
61                 max_fds = 0;
62         }
63         num_fds--;
64         fd->backend_info = -1;
65         close(fd->fd);
66 }
67
68 bool add_listener(struct io_listener *l)
69 {
70         if (!add_fd(&l->fd, POLLIN))
71                 return false;
72         num_waiting++;
73         return true;
74 }
75
76 bool add_conn(struct io_conn *c)
77 {
78         if (!add_fd(&c->fd, 0))
79                 return false;
80         num_next++;
81         return true;
82 }
83
84 bool add_duplex(struct io_conn *c)
85 {
86         c->fd.backend_info = c->duplex->fd.backend_info;
87         num_next++;
88         return true;
89 }
90
91 static void del_conn(struct io_conn *conn)
92 {
93         if (conn->finish)
94                 conn->finish(conn, conn->finish_arg);
95         if (timeout_active(conn))
96                 backend_del_timeout(conn);
97         free(conn->timeout);
98         if (conn->duplex) {
99                 /* In case fds[] pointed to the other one. */
100                 fds[conn->fd.backend_info] = &conn->duplex->fd;
101                 conn->duplex->duplex = NULL;
102         } else
103                 del_fd(&conn->fd);
104         if (conn->state == IO_FINISHED)
105                 num_finished--;
106         else if (conn->state == IO_NEXT)
107                 num_next--;
108 }
109
110 void del_listener(struct io_listener *l)
111 {
112         del_fd(&l->fd);
113 }
114
115 static void backend_set_state(struct io_conn *conn, struct io_plan *plan)
116 {
117         enum io_state state = from_ioplan(plan);
118         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
119
120         if (pfd->events)
121                 num_waiting--;
122
123         pfd->events = conn->pollflag;
124         if (conn->duplex) {
125                 int mask = conn->duplex->pollflag;
126                 /* You can't *both* read/write. */
127                 assert(!mask || pfd->events != mask);
128                 pfd->events |= mask;
129         }
130         if (pfd->events)
131                 num_waiting++;
132
133         if (state == IO_NEXT)
134                 num_next++;
135         else if (state == IO_FINISHED)
136                 num_finished++;
137
138         conn->state = state;
139 }
140
141 void backend_wakeup(struct io_conn *conn)
142 {
143         num_next++;
144 }
145
146 static void accept_conn(struct io_listener *l)
147 {
148         struct io_conn *c;
149         int fd = accept(l->fd.fd, NULL, NULL);
150
151         /* FIXME: What to do here? */
152         if (fd < 0)
153                 return;
154         c = io_new_conn(fd, l->next, l->finish, l->conn_arg);
155         if (!c) {
156                 close(fd);
157                 return;
158         }
159 }
160
161 /* It's OK to miss some, as long as we make progress. */
162 static void finish_and_next(bool finished_only)
163 {
164         unsigned int i;
165
166         for (i = 0; !io_loop_return && i < num_fds; i++) {
167                 struct io_conn *c, *duplex;
168
169                 if (!num_finished) {
170                         if (finished_only || num_next == 0)
171                                 break;
172                 }
173                 if (fds[i]->listener)
174                         continue;
175                 c = (void *)fds[i];
176                 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
177                         if (c->state == IO_FINISHED) {
178                                 del_conn(c);
179                                 free(c);
180                                 i--;
181                         } else if (!finished_only && c->state == IO_NEXT) {
182                                 backend_set_state(c, c->next(c, c->next_arg));
183                                 num_next--;
184                         }
185                 }
186         }
187 }
188
189 static void ready(struct io_conn *c)
190 {
191         backend_set_state(c, do_ready(c));
192 }
193
194 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
195 {
196         if (!timeouts.base)
197                 timers_init(&timeouts, time_now());
198         timer_add(&timeouts, &conn->timeout->timer,
199                   time_add(time_now(), duration));
200         conn->timeout->conn = conn;
201 }
202
203 void backend_del_timeout(struct io_conn *conn)
204 {
205         assert(conn->timeout->conn == conn);
206         timer_del(&timeouts, &conn->timeout->timer);
207         conn->timeout->conn = NULL;
208 }
209
210 /* This is the main loop. */
211 void *io_loop(void)
212 {
213         void *ret;
214
215         while (!io_loop_return) {
216                 int i, r, timeout = INT_MAX;
217                 struct timespec now;
218
219                 if (timeouts.base) {
220                         struct timespec first;
221                         struct list_head expired;
222                         struct io_timeout *t;
223
224                         now = time_now();
225
226                         /* Call functions for expired timers. */
227                         timers_expire(&timeouts, now, &expired);
228                         while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
229                                 struct io_conn *conn = t->conn;
230                                 /* Clear, in case timer re-adds */
231                                 t->conn = NULL;
232                                 backend_set_state(conn, t->next(conn, t->next_arg));
233                         }
234
235                         /* Now figure out how long to wait for the next one. */
236                         if (timer_earliest(&timeouts, &first)) {
237                                 uint64_t f = time_to_msec(time_sub(first, now));
238                                 if (f < INT_MAX)
239                                         timeout = f;
240                         }
241                 }
242
243                 if (num_finished || num_next) {
244                         finish_and_next(false);
245                         /* Could have started/finished more. */
246                         continue;
247                 }
248
249                 if (num_fds == 0)
250                         break;
251
252                 /* You can't tell them all to go to sleep! */
253                 assert(num_waiting);
254
255                 r = poll(pollfds, num_fds, timeout);
256                 if (r < 0)
257                         break;
258
259                 for (i = 0; i < num_fds && !io_loop_return; i++) {
260                         struct io_conn *c = (void *)fds[i];
261                         int events = pollfds[i].revents;
262
263                         if (r == 0)
264                                 break;
265
266                         if (fds[i]->listener) {
267                                 if (events & POLLIN) {
268                                         accept_conn((void *)c);
269                                         r--;
270                                 }
271                         } else if (events & (POLLIN|POLLOUT)) {
272                                 r--;
273                                 if (c->duplex) {
274                                         int mask = c->duplex->pollflag;
275                                         if (events & mask) {
276                                                 ready(c->duplex);
277                                                 events &= ~mask;
278                                                 if (!(events&(POLLIN|POLLOUT)))
279                                                         continue;
280                                         }
281                                 }
282                                 ready(c);
283                         } else if (events & POLLHUP) {
284                                 r--;
285                                 backend_set_state(c, io_close(c, NULL));
286                                 if (c->duplex)
287                                         backend_set_state(c->duplex,
288                                                           io_close(c->duplex,
289                                                                    NULL));
290                         }
291                 }
292         }
293
294         while (num_finished)
295                 finish_and_next(true);
296
297         ret = io_loop_return;
298         io_loop_return = NULL;
299         return ret;
300 }