Merge branch 'master' into timeheap
[ashd.git] / lib / mtio-epoll.c
... / ...
CommitLineData
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <time.h>
22#include <fcntl.h>
23#include <string.h>
24#include <sys/epoll.h>
25#include <errno.h>
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <log.h>
31#include <utils.h>
32#include <mt.h>
33#include <mtio.h>
34
35static struct blocker *blockers;
36
37struct blocker {
38 struct blocker *n, *p, *n2, *p2;
39 int fd, reg;
40 int ev, rev, id;
41 int thpos;
42 time_t to;
43 struct muth *th;
44};
45
46struct timeentry {
47 time_t to;
48 struct blocker *bl;
49};
50
51static int epfd = -1, fdln = 0;
52static int exitstatus;
53static struct blocker **fdlist;
54static typedbuf(struct timeentry) timeheap;
55
56static int regfd(struct blocker *bl)
57{
58 struct blocker *o;
59 struct epoll_event evd;
60
61 memset(&evd, 0, sizeof(evd));
62 evd.events = 0;
63 if(bl->ev & EV_READ)
64 evd.events |= EPOLLIN;
65 if(bl->ev & EV_WRITE)
66 evd.events |= EPOLLOUT;
67 evd.data.fd = bl->fd;
68 if(bl->fd >= fdln) {
69 if(fdlist) {
70 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
71 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
72 fdln = bl->fd + 1;
73 } else {
74 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
75 }
76 }
77 if(fdlist[bl->fd] == NULL) {
78 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
79 /* XXX?! Whatever to do, really? */
80 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
81 return(-1);
82 }
83 } else {
84 for(o = fdlist[bl->fd]; o; o = o->n2) {
85 if(o->ev & EV_READ)
86 evd.events |= EPOLLIN;
87 if(o->ev & EV_WRITE)
88 evd.events |= EPOLLOUT;
89 }
90 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
91 /* XXX?! Whatever to do, really? */
92 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
93 return(-1);
94 }
95 }
96 bl->n2 = fdlist[bl->fd];
97 bl->p2 = NULL;
98 if(fdlist[bl->fd] != NULL)
99 fdlist[bl->fd]->p2 = bl;
100 fdlist[bl->fd] = bl;
101 bl->reg = 1;
102 return(0);
103}
104
105static void remfd(struct blocker *bl)
106{
107 struct blocker *o;
108 struct epoll_event evd;
109
110 if(!bl->reg)
111 return;
112 if(bl->n2)
113 bl->n2->p2 = bl->p2;
114 if(bl->p2)
115 bl->p2->n2 = bl->n2;
116 if(bl == fdlist[bl->fd])
117 fdlist[bl->fd] = bl->n2;
118 if(fdlist[bl->fd] == NULL) {
119 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
120 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
121 } else {
122 memset(&evd, 0, sizeof(evd));
123 evd.events = 0;
124 evd.data.fd = bl->fd;
125 for(o = fdlist[bl->fd]; o; o = o->n2) {
126 if(o->ev & EV_READ)
127 evd.events |= EPOLLIN;
128 if(o->ev & EV_WRITE)
129 evd.events |= EPOLLOUT;
130 }
131 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
132 /* XXX?! Whatever to do, really? */
133 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
134 }
135 }
136 bl->reg = 0;
137}
138
139static void thraise(struct timeentry ent, int n)
140{
141 int p;
142
143 while(n > 0) {
144 p = (n - 1) >> 1;
145 if(timeheap.b[p].to <= ent.to)
146 break;
147 timeheap.b[n] = timeheap.b[p];
148 timeheap.b[n].bl->thpos = n;
149 n = p;
150 }
151 timeheap.b[n] = ent;
152 ent.bl->thpos = n;
153}
154
155static void thlower(struct timeentry ent, int n)
156{
157 int c;
158
159 while(1) {
160 c = (n << 1) + 1;
161 if(c >= timeheap.d)
162 break;
163 if((c + 1 < timeheap.d) && (timeheap.b[c + 1].to < timeheap.b[c].to))
164 c = c + 1;
165 if(timeheap.b[c].to > ent.to)
166 break;
167 timeheap.b[n] = timeheap.b[c];
168 timeheap.b[n].bl->thpos = n;
169 n = c;
170 }
171 timeheap.b[n] = ent;
172 ent.bl->thpos = n;
173}
174
175static void addtimeout(struct blocker *bl, time_t to)
176{
177 sizebuf(timeheap, ++timeheap.d);
178 thraise((struct timeentry){.to = to, .bl = bl}, timeheap.d - 1);
179}
180
181static void deltimeout(struct blocker *bl)
182{
183 struct timeentry ent;
184 int n;
185
186 if(bl->thpos == timeheap.d - 1) {
187 timeheap.d--;
188 return;
189 }
190 n = bl->thpos;
191 ent = timeheap.b[--timeheap.d];
192 if((n > 0) && (timeheap.b[(n - 1) >> 1].to > ent.to))
193 thraise(ent, n);
194 else
195 thlower(ent, n);
196}
197
198static int addblock(struct blocker *bl)
199{
200 if((epfd >= 0) && regfd(bl))
201 return(-1);
202 bl->n = blockers;
203 if(blockers)
204 blockers->p = bl;
205 blockers = bl;
206 if(bl->to > 0)
207 addtimeout(bl, bl->to);
208 return(0);
209}
210
211static void remblock(struct blocker *bl)
212{
213 if(bl->to > 0)
214 deltimeout(bl);
215 if(bl->n)
216 bl->n->p = bl->p;
217 if(bl->p)
218 bl->p->n = bl->n;
219 if(blockers == bl)
220 blockers = bl->n;
221 remfd(bl);
222}
223
224struct selected mblock(time_t to, int n, struct selected *spec)
225{
226 int i, id;
227 struct blocker bls[n];
228
229 to = (to > 0)?(time(NULL) + to):0;
230 for(i = 0; i < n; i++) {
231 bls[i] = (struct blocker) {
232 .fd = spec[i].fd,
233 .ev = spec[i].ev,
234 .id = i,
235 .to = to,
236 .th = current,
237 };
238 if(addblock(&bls[i])) {
239 for(i--; i >= 0; i--)
240 remblock(&bls[i]);
241 return((struct selected){.fd = -1, .ev = -1});
242 }
243 }
244 id = yield();
245 for(i = 0; i < n; i++)
246 remblock(&bls[i]);
247 if(id < 0)
248 return((struct selected){.fd = -1, .ev = -1});
249 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
250}
251
252int block(int fd, int ev, time_t to)
253{
254 struct blocker bl;
255 int rv;
256
257 bl = (struct blocker) {
258 .fd = fd,
259 .ev = ev,
260 .id = -1,
261 .to = (to > 0)?(time(NULL) + to):0,
262 .th = current,
263 };
264 if(addblock(&bl))
265 return(-1);
266 rv = yield();
267 remblock(&bl);
268 return(rv);
269}
270
271int ioloop(void)
272{
273 struct blocker *bl, *nbl;
274 struct epoll_event evr[16];
275 int i, fd, nev, ev, toval;
276 time_t now;
277
278 exitstatus = 0;
279 epfd = epoll_create(128);
280 fcntl(epfd, F_SETFD, FD_CLOEXEC);
281 bufinit(timeheap);
282 for(bl = blockers; bl; bl = nbl) {
283 nbl = bl->n;
284 if(regfd(bl))
285 resume(bl->th, -1);
286 }
287 while(blockers != NULL) {
288 now = time(NULL);
289 if(timeheap.d == 0)
290 toval = -1;
291 else if(timeheap.b[0].to > now)
292 toval = (timeheap.b[0].to - now) * 1000;
293 else
294 toval = 1000;
295 if(exitstatus)
296 break;
297 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
298 if(nev < 0) {
299 if(errno != EINTR) {
300 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
301 /* To avoid CPU hogging in case it's bad, which it
302 * probably is. */
303 sleep(1);
304 }
305 continue;
306 }
307 for(i = 0; i < nev; i++) {
308 fd = evr[i].data.fd;
309 ev = 0;
310 if(evr[i].events & EPOLLIN)
311 ev |= EV_READ;
312 if(evr[i].events & EPOLLOUT)
313 ev |= EV_WRITE;
314 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
315 ev = -1;
316 for(bl = fdlist[fd]; bl; bl = nbl) {
317 nbl = bl->n2;
318 if((ev < 0) || (ev & bl->ev)) {
319 if(bl->id < 0) {
320 resume(bl->th, ev);
321 } else {
322 bl->rev = ev;
323 resume(bl->th, bl->id);
324 }
325 }
326 }
327 }
328 now = time(NULL);
329 while((timeheap.d > 0) && (timeheap.b[0].to <= now)) {
330 if(bl->id < 0) {
331 resume(timeheap.b[0].bl->th, 0);
332 } else {
333 bl->rev = 0;
334 resume(bl->th, bl->id);
335 }
336 }
337 }
338 for(bl = blockers; bl; bl = bl->n)
339 remfd(bl);
340 buffree(timeheap);
341 close(epfd);
342 epfd = -1;
343 return(exitstatus);
344}
345
346void exitioloop(int status)
347{
348 exitstatus = status;
349}