]> git.ozlabs.org Git - ccan/blob - ccan/io/poll.c
ccan/io: simplify do_ready.
[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_closing = 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 void backend_plan_changed(struct io_conn *conn)
81 {
82         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
83
84         if (pfd->events)
85                 num_waiting--;
86
87         pfd->events = conn->plan.pollflag;
88         if (conn->duplex) {
89                 int mask = conn->duplex->plan.pollflag;
90                 /* You can't *both* read/write. */
91                 assert(!mask || pfd->events != mask);
92                 pfd->events |= mask;
93         }
94         if (pfd->events)
95                 num_waiting++;
96
97         if (!conn->plan.next)
98                 num_closing++;
99 }
100
101 bool add_conn(struct io_conn *c)
102 {
103         if (!add_fd(&c->fd, c->plan.pollflag))
104                 return false;
105         /* Immediate close is allowed. */
106         if (!c->plan.next)
107                 num_closing++;
108         return true;
109 }
110
111 bool add_duplex(struct io_conn *c)
112 {
113         c->fd.backend_info = c->duplex->fd.backend_info;
114         backend_plan_changed(c);
115         return true;
116 }
117
118 static void del_conn(struct io_conn *conn)
119 {
120         if (conn->finish)
121                 conn->finish(conn, conn->finish_arg);
122         if (timeout_active(conn))
123                 backend_del_timeout(conn);
124         free(conn->timeout);
125         if (conn->duplex) {
126                 /* In case fds[] pointed to the other one. */
127                 fds[conn->fd.backend_info] = &conn->duplex->fd;
128                 conn->duplex->duplex = NULL;
129         } else
130                 del_fd(&conn->fd);
131         num_closing--;
132 }
133
134 void del_listener(struct io_listener *l)
135 {
136         del_fd(&l->fd);
137 }
138
139 static void set_plan(struct io_conn *conn, struct io_plan plan)
140 {
141         conn->plan = plan;
142         backend_plan_changed(conn);
143 }
144
145 static void accept_conn(struct io_listener *l)
146 {
147         int fd = accept(l->fd.fd, NULL, NULL);
148
149         /* FIXME: What to do here? */
150         if (fd < 0)
151                 return;
152         l->init(fd, l->arg);
153 }
154
155 /* It's OK to miss some, as long as we make progress. */
156 static void finish_conns(void)
157 {
158         unsigned int i;
159
160         for (i = 0; !io_loop_return && i < num_fds; i++) {
161                 struct io_conn *c, *duplex;
162
163                 if (!num_closing)
164                         break;
165
166                 if (fds[i]->listener)
167                         continue;
168                 c = (void *)fds[i];
169                 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
170                         if (!c->plan.next) {
171                                 del_conn(c);
172                                 free(c);
173                                 i--;
174                         }
175                 }
176         }
177 }
178
179 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
180 {
181         if (!timeouts.base)
182                 timers_init(&timeouts, time_now());
183         timer_add(&timeouts, &conn->timeout->timer,
184                   time_add(time_now(), duration));
185         conn->timeout->conn = conn;
186 }
187
188 void backend_del_timeout(struct io_conn *conn)
189 {
190         assert(conn->timeout->conn == conn);
191         timer_del(&timeouts, &conn->timeout->timer);
192         conn->timeout->conn = NULL;
193 }
194
195 /* This is the main loop. */
196 void *io_loop(void)
197 {
198         void *ret;
199
200         while (!io_loop_return) {
201                 int i, r, timeout = INT_MAX;
202                 struct timespec now;
203
204                 if (timeouts.base) {
205                         struct timespec first;
206                         struct list_head expired;
207                         struct io_timeout *t;
208
209                         now = time_now();
210
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 */
216                                 t->conn = NULL;
217                                 set_plan(conn, t->next(conn, t->next_arg));
218                         }
219
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));
223                                 if (f < INT_MAX)
224                                         timeout = f;
225                         }
226                 }
227
228                 if (num_closing) {
229                         finish_conns();
230                         /* Could have started/finished more. */
231                         continue;
232                 }
233
234                 if (num_fds == 0)
235                         break;
236
237                 /* You can't tell them all to go to sleep! */
238                 assert(num_waiting);
239
240                 r = poll(pollfds, num_fds, timeout);
241                 if (r < 0)
242                         break;
243
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;
247
248                         if (r == 0)
249                                 break;
250
251                         if (fds[i]->listener) {
252                                 if (events & POLLIN) {
253                                         accept_conn((void *)c);
254                                         r--;
255                                 }
256                         } else if (events & (POLLIN|POLLOUT)) {
257                                 r--;
258                                 if (c->duplex) {
259                                         int mask = c->duplex->plan.pollflag;
260                                         if (events & mask) {
261                                                 io_ready(c->duplex);
262                                                 events &= ~mask;
263                                                 if (!(events&(POLLIN|POLLOUT)))
264                                                         continue;
265                                         }
266                                 }
267                                 io_ready(c);
268                         } else if (events & POLLHUP) {
269                                 r--;
270                                 set_plan(c, io_close(c, NULL));
271                                 if (c->duplex)
272                                         set_plan(c->duplex,
273                                                  io_close(c->duplex, NULL));
274                         }
275                 }
276         }
277
278         while (num_closing)
279                 finish_conns();
280
281         ret = io_loop_return;
282         io_loop_return = NULL;
283         return ret;
284 }