]> git.ozlabs.org Git - ccan/blob - ccan/io/poll.c
io: update for new timer API.
[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 #include <errno.h>
11 #include <ccan/time/time.h>
12 #include <ccan/timer/timer.h>
13
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
20 static bool add_fd(struct fd *fd, short events)
21 {
22         if (!max_fds) {
23                 assert(num_fds == 0);
24                 pollfds = tal_arr(NULL, struct pollfd, 8);
25                 if (!pollfds)
26                         return false;
27                 fds = tal_arr(pollfds, struct fd *, 8);
28                 if (!fds)
29                         return false;
30                 max_fds = 8;
31         }
32
33         if (num_fds + 1 > max_fds) {
34                 size_t num = max_fds * 2;
35
36                 if (!tal_resize(&pollfds, num))
37                         return false;
38                 if (!tal_resize(&fds, num))
39                         return false;
40                 max_fds = num;
41         }
42
43         pollfds[num_fds].events = events;
44         /* In case it's idle. */
45         if (!events)
46                 pollfds[num_fds].fd = -fd->fd;
47         else
48                 pollfds[num_fds].fd = fd->fd;
49         pollfds[num_fds].revents = 0; /* In case we're iterating now */
50         fds[num_fds] = fd;
51         fd->backend_info = num_fds;
52         num_fds++;
53         if (events)
54                 num_waiting++;
55
56         return true;
57 }
58
59 static void del_fd(struct fd *fd)
60 {
61         size_t n = fd->backend_info;
62
63         assert(n != -1);
64         assert(n < num_fds);
65         if (pollfds[n].events)
66                 num_waiting--;
67         if (n != num_fds - 1) {
68                 /* Move last one over us. */
69                 pollfds[n] = pollfds[num_fds-1];
70                 fds[n] = fds[num_fds-1];
71                 assert(fds[n]->backend_info == num_fds-1);
72                 fds[n]->backend_info = n;
73         } else if (num_fds == 1) {
74                 /* Free everything when no more fds. */
75                 pollfds = tal_free(pollfds);
76                 fds = NULL;
77                 max_fds = 0;
78         }
79         num_fds--;
80         fd->backend_info = -1;
81
82         /* Closing a local socket doesn't wake poll() because other end
83          * has them open.  See 2.6.  When should I use shutdown()?
84          * in http://www.faqs.org/faqs/unix-faq/socket/ */
85         shutdown(fd->fd, SHUT_RDWR);
86
87         close(fd->fd);
88 }
89
90 bool add_listener(struct io_listener *l)
91 {
92         if (!add_fd(&l->fd, POLLIN))
93                 return false;
94         return true;
95 }
96
97 void remove_from_always(struct io_conn *conn)
98 {
99         list_del_init(&conn->always);
100 }
101
102 void backend_new_closing(struct io_conn *conn)
103 {
104         /* In case it's on always list, remove it. */
105         list_del_init(&conn->always);
106         list_add_tail(&closing, &conn->closing);
107 }
108
109 void backend_new_always(struct io_conn *conn)
110 {
111         /* In case it's already in always list. */
112         list_del(&conn->always);
113         list_add_tail(&always, &conn->always);
114 }
115
116 void backend_new_plan(struct io_conn *conn)
117 {
118         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
119
120         if (pfd->events)
121                 num_waiting--;
122
123         pfd->events = 0;
124         if (conn->plan[IO_IN].status == IO_POLLING)
125                 pfd->events |= POLLIN;
126         if (conn->plan[IO_OUT].status == IO_POLLING)
127                 pfd->events |= POLLOUT;
128
129         if (pfd->events) {
130                 num_waiting++;
131                 pfd->fd = conn->fd.fd;
132         } else {
133                 pfd->fd = -conn->fd.fd;
134         }
135 }
136
137 void backend_wake(const void *wait)
138 {
139         unsigned int i;
140
141         for (i = 0; i < num_fds; i++) {
142                 struct io_conn *c;
143
144                 /* Ignore listeners */
145                 if (fds[i]->listener)
146                         continue;
147
148                 c = (void *)fds[i];
149                 if (c->plan[IO_IN].status == IO_WAITING
150                     && c->plan[IO_IN].arg.u1.const_vp == wait)
151                         io_do_wakeup(c, IO_IN);
152
153                 if (c->plan[IO_OUT].status == IO_WAITING
154                     && c->plan[IO_OUT].arg.u1.const_vp == wait)
155                         io_do_wakeup(c, IO_OUT);
156         }
157 }
158
159 bool add_conn(struct io_conn *c)
160 {
161         return add_fd(&c->fd, 0);
162 }
163
164 static void del_conn(struct io_conn *conn)
165 {
166         del_fd(&conn->fd);
167         if (conn->finish) {
168                 /* Saved by io_close */
169                 errno = conn->plan[IO_IN].arg.u1.s;
170                 conn->finish(conn, conn->finish_arg);
171         }
172         tal_free(conn);
173 }
174
175 void del_listener(struct io_listener *l)
176 {
177         del_fd(&l->fd);
178 }
179
180 static void accept_conn(struct io_listener *l)
181 {
182         int fd = accept(l->fd.fd, NULL, NULL);
183
184         /* FIXME: What to do here? */
185         if (fd < 0)
186                 return;
187
188         io_new_conn(l->ctx, fd, l->init, l->arg);
189 }
190
191 /* It's OK to miss some, as long as we make progress. */
192 static bool close_conns(void)
193 {
194         bool ret = false;
195         struct io_conn *conn;
196
197         while ((conn = list_pop(&closing, struct io_conn, closing)) != NULL) {
198                 assert(conn->plan[IO_IN].status == IO_CLOSING);
199                 assert(conn->plan[IO_OUT].status == IO_CLOSING);
200
201                 del_conn(conn);
202                 ret = true;
203         }
204         return ret;
205 }
206
207 static bool handle_always(void)
208 {
209         bool ret = false;
210         struct io_conn *conn;
211
212         while ((conn = list_pop(&always, struct io_conn, always)) != NULL) {
213                 assert(conn->plan[IO_IN].status == IO_ALWAYS
214                        || conn->plan[IO_OUT].status == IO_ALWAYS);
215
216                 /* Re-initialize, for next time. */
217                 list_node_init(&conn->always);
218                 io_do_always(conn);
219                 ret = true;
220         }
221         return ret;
222 }
223
224 /* This is the main loop. */
225 void *io_loop(struct timers *timers, struct timer **expired)
226 {
227         void *ret;
228
229         /* if timers is NULL, expired must be.  If not, not. */
230         assert(!timers == !expired);
231
232         /* Make sure this is NULL if we exit for some other reason. */
233         if (expired)
234                 *expired = NULL;
235
236         while (!io_loop_return) {
237                 int i, r, ms_timeout = -1;
238
239                 if (close_conns()) {
240                         /* Could have started/finished more. */
241                         continue;
242                 }
243
244                 if (handle_always()) {
245                         /* Could have started/finished more. */
246                         continue;
247                 }
248
249                 /* Everything closed? */
250                 if (num_fds == 0)
251                         break;
252
253                 /* You can't tell them all to go to sleep! */
254                 assert(num_waiting);
255
256                 if (timers) {
257                         struct timeabs now, first;
258
259                         now = time_now();
260
261                         /* Call functions for expired timers. */
262                         *expired = timers_expire(timers, now);
263                         if (*expired)
264                                 break;
265
266                         /* Now figure out how long to wait for the next one. */
267                         if (timer_earliest(timers, &first)) {
268                                 uint64_t next;
269                                 next = time_to_msec(time_between(first, now));
270                                 if (next < INT_MAX)
271                                         ms_timeout = next;
272                                 else
273                                         ms_timeout = INT_MAX;
274                         }
275                 }
276
277                 r = poll(pollfds, num_fds, ms_timeout);
278                 if (r < 0)
279                         break;
280
281                 for (i = 0; i < num_fds && !io_loop_return; i++) {
282                         struct io_conn *c = (void *)fds[i];
283                         int events = pollfds[i].revents;
284
285                         if (r == 0)
286                                 break;
287
288                         if (fds[i]->listener) {
289                                 if (events & POLLIN) {
290                                         accept_conn((void *)c);
291                                         r--;
292                                 }
293                         } else if (events & (POLLIN|POLLOUT)) {
294                                 r--;
295                                 io_ready(c, events);
296                         } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
297                                 r--;
298                                 errno = EBADF;
299                                 io_close(c);
300                         }
301                 }
302         }
303
304         close_conns();
305
306         ret = io_loop_return;
307         io_loop_return = NULL;
308
309         return ret;
310 }