]> git.ozlabs.org Git - ccan/blob - ccan/io/poll.c
fdff271c64f26e09f060b9e52e468dcf80d97696
[ccan] / ccan / io / poll.c
1 /* Licensed under BSD-MIT - 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
10 static size_t num_fds = 0, max_fds = 0, num_next = 0, num_finished = 0;
11 static struct pollfd *pollfds = NULL;
12 static struct fd **fds = NULL;
13
14 static bool add_fd(struct fd *fd, short events)
15 {
16         if (num_fds + 1 > max_fds) {
17                 struct pollfd *newpollfds;
18                 struct fd **newfds;
19                 size_t num = max_fds ? max_fds * 2 : 8;
20
21                 newpollfds = realloc(pollfds, sizeof(*newpollfds) * num);
22                 if (!newpollfds)
23                         return false;
24                 pollfds = newpollfds;
25                 newfds = realloc(fds, sizeof(*newfds) * num);
26                 if (!newfds)
27                         return false;
28                 fds = newfds;
29                 max_fds = num;
30         }
31
32         pollfds[num_fds].fd = fd->fd;
33         pollfds[num_fds].events = events;
34         pollfds[num_fds].revents = 0; /* In case we're iterating now */
35         fds[num_fds] = fd;
36         fd->backend_info = num_fds;
37         num_fds++;
38         return true;
39 }
40
41 static void del_fd(struct fd *fd)
42 {
43         size_t n = fd->backend_info;
44
45         assert(n != -1);
46         assert(n < num_fds);
47         if (n != num_fds - 1) {
48                 /* Move last one over us. */
49                 pollfds[n] = pollfds[num_fds-1];
50                 fds[n] = fds[num_fds-1];
51                 assert(fds[n]->backend_info == num_fds-1);
52                 fds[n]->backend_info = n;
53         } else if (num_fds == 1) {
54                 /* Free everything when no more fds. */
55                 free(pollfds);
56                 free(fds);
57                 pollfds = NULL;
58                 fds = NULL;
59                 max_fds = 0;
60         }
61         num_fds--;
62         fd->backend_info = -1;
63         close(fd->fd);
64 }
65
66 bool add_listener(struct io_listener *l)
67 {
68         return add_fd(&l->fd, POLLIN);
69 }
70
71 bool add_conn(struct io_conn *c)
72 {
73         if (!add_fd(&c->fd, 0))
74                 return false;
75         num_next++;
76         return true;
77 }
78
79 bool add_duplex(struct io_conn *c)
80 {
81         c->fd.backend_info = c->duplex->fd.backend_info;
82         num_next++;
83         return true;
84 }
85
86 static void del_conn(struct io_conn *conn)
87 {
88         if (conn->fd.finish)
89                 conn->fd.finish(conn, conn->fd.finish_arg);
90         if (conn->duplex) {
91                 /* In case fds[] pointed to the other one. */
92                 fds[conn->fd.backend_info] = &conn->duplex->fd;
93                 conn->duplex->duplex = NULL;
94         } else
95                 del_fd(&conn->fd);
96         if (conn->state == FINISHED)
97                 num_finished--;
98         else if (conn->state == NEXT)
99                 num_next--;
100 }
101
102 void del_listener(struct io_listener *l)
103 {
104         del_fd(&l->fd);
105 }
106
107 static int pollmask(enum io_state state)
108 {
109         switch (state) {
110         case READ:
111         case READPART:
112                 return POLLIN;
113         case WRITE:
114         case WRITEPART:
115                 return POLLOUT;
116         default:
117                 return 0;
118         }
119 }
120
121 void backend_set_state(struct io_conn *conn, struct io_op *op)
122 {
123         enum io_state state = from_ioop(op);
124         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
125
126         pfd->events = pollmask(state);
127         if (conn->duplex) {
128                 int mask = pollmask(conn->duplex->state);
129                 /* You can't *both* read/write. */
130                 assert(!mask || pfd->events != mask);
131                 pfd->events |= mask;
132         }
133
134         if (state == NEXT)
135                 num_next++;
136         else if (state == FINISHED)
137                 num_finished++;
138
139         conn->state = state;
140 }
141
142 static void accept_conn(struct io_listener *l)
143 {
144         struct io_conn *c;
145         int fd = accept(l->fd.fd, NULL, NULL);
146
147         /* FIXME: What to do here? */
148         if (fd < 0)
149                 return;
150         c = io_new_conn(fd, l->fd.next, l->fd.finish, l->fd.next_arg);
151         if (!c) {
152                 close(fd);
153                 return;
154         }
155 }
156
157 /* It's OK to miss some, as long as we make progress. */
158 static void finish_and_next(bool finished_only)
159 {
160         unsigned int i;
161
162         for (i = 0; !io_loop_return && i < num_fds; i++) {
163                 struct io_conn *c, *duplex;
164
165                 if (!num_finished) {
166                         if (finished_only || num_next == 0)
167                                 break;
168                 }
169                 if (fds[i]->listener)
170                         continue;
171                 c = (void *)fds[i];
172                 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
173                         if (c->state == FINISHED) {
174                                 del_conn(c);
175                                 free(c);
176                                 i--;
177                         } else if (!finished_only && c->state == NEXT) {
178                                 backend_set_state(c,
179                                                   c->fd.next(c,
180                                                              c->fd.next_arg));
181                                 num_next--;
182                         }
183                 }
184         }
185 }
186
187 static void ready(struct io_conn *c)
188 {
189         backend_set_state(c, do_ready(c));
190 }
191
192 /* This is the main loop. */
193 void *io_loop(void)
194 {
195         void *ret;
196
197         while (!io_loop_return) {
198                 int i, r;
199
200                 if (num_finished || num_next) {
201                         finish_and_next(false);
202                         /* Could have started/finished more. */
203                         continue;
204                 }
205
206                 if (num_fds == 0)
207                         break;
208
209                 r = poll(pollfds, num_fds, -1);
210                 if (r < 0)
211                         break;
212
213                 for (i = 0; i < num_fds && !io_loop_return; i++) {
214                         struct io_conn *c = (void *)fds[i];
215                         int events = pollfds[i].revents;
216
217                         if (fds[i]->listener) {
218                                 if (events & POLLIN)
219                                         accept_conn((void *)c);
220                         } else if (events & (POLLIN|POLLOUT)) {
221                                 if (c->duplex) {
222                                         int mask = pollmask(c->duplex->state);
223                                         if (events & mask) {
224                                                 ready(c->duplex);
225                                                 events &= ~mask;
226                                                 if (!(events&(POLLIN|POLLOUT)))
227                                                         continue;
228                                         }
229                                 }
230                                 ready(c);
231                         } else if (events & POLLHUP) {
232                                 backend_set_state(c, io_close(c, NULL));
233                                 if (c->duplex)
234                                         backend_set_state(c->duplex,
235                                                           io_close(c->duplex,
236                                                                    NULL));
237                         }
238
239                 }
240         }
241
242         while (num_finished)
243                 finish_and_next(true);
244
245         ret = io_loop_return;
246         io_loop_return = NULL;
247         return ret;
248 }