Merge branch 'timeheap'
[ashd.git] / lib / mtio-epoll.c
CommitLineData
a6cda4dd
FT
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>
4f6d772e
FT
20#include <unistd.h>
21#include <time.h>
be078ac9 22#include <fcntl.h>
a6cda4dd
FT
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;
205ee933 40 int ev, rev, id;
589987f8 41 int thpos;
a6cda4dd
FT
42 time_t to;
43 struct muth *th;
44};
45
46static int epfd = -1, fdln = 0;
9d32586e 47static int exitstatus;
a6cda4dd 48static struct blocker **fdlist;
bb073004 49static typedbuf(struct blocker *) timeheap;
a6cda4dd
FT
50
51static int regfd(struct blocker *bl)
52{
53 struct blocker *o;
54 struct epoll_event evd;
55
56 memset(&evd, 0, sizeof(evd));
57 evd.events = 0;
58 if(bl->ev & EV_READ)
59 evd.events |= EPOLLIN;
60 if(bl->ev & EV_WRITE)
61 evd.events |= EPOLLOUT;
62 evd.data.fd = bl->fd;
63 if(bl->fd >= fdln) {
64 if(fdlist) {
65 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
66 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
67 fdln = bl->fd + 1;
68 } else {
69 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
70 }
71 }
72 if(fdlist[bl->fd] == NULL) {
73 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
74 /* XXX?! Whatever to do, really? */
75 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
76 return(-1);
77 }
78 } else {
79 for(o = fdlist[bl->fd]; o; o = o->n2) {
80 if(o->ev & EV_READ)
81 evd.events |= EPOLLIN;
82 if(o->ev & EV_WRITE)
83 evd.events |= EPOLLOUT;
84 }
85 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
86 /* XXX?! Whatever to do, really? */
87 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
88 return(-1);
89 }
90 }
91 bl->n2 = fdlist[bl->fd];
92 bl->p2 = NULL;
93 if(fdlist[bl->fd] != NULL)
94 fdlist[bl->fd]->p2 = bl;
95 fdlist[bl->fd] = bl;
96 bl->reg = 1;
97 return(0);
98}
99
100static void remfd(struct blocker *bl)
101{
102 struct blocker *o;
103 struct epoll_event evd;
104
105 if(!bl->reg)
106 return;
107 if(bl->n2)
108 bl->n2->p2 = bl->p2;
109 if(bl->p2)
110 bl->p2->n2 = bl->n2;
111 if(bl == fdlist[bl->fd])
112 fdlist[bl->fd] = bl->n2;
113 if(fdlist[bl->fd] == NULL) {
114 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
115 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
116 } else {
117 memset(&evd, 0, sizeof(evd));
118 evd.events = 0;
119 evd.data.fd = bl->fd;
120 for(o = fdlist[bl->fd]; o; o = o->n2) {
121 if(o->ev & EV_READ)
122 evd.events |= EPOLLIN;
123 if(o->ev & EV_WRITE)
124 evd.events |= EPOLLOUT;
125 }
126 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
127 /* XXX?! Whatever to do, really? */
128 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
129 }
130 }
9d32586e 131 bl->reg = 0;
a6cda4dd
FT
132}
133
bb073004 134static void thraise(struct blocker *bl, int n)
589987f8
FT
135{
136 int p;
137
138 while(n > 0) {
139 p = (n - 1) >> 1;
bb073004 140 if(timeheap.b[p]->to <= bl->to)
589987f8
FT
141 break;
142 timeheap.b[n] = timeheap.b[p];
bb073004 143 timeheap.b[n]->thpos = n;
589987f8
FT
144 n = p;
145 }
bb073004
FT
146 timeheap.b[n] = bl;
147 bl->thpos = n;
589987f8
FT
148}
149
bb073004 150static void thlower(struct blocker *bl, int n)
589987f8
FT
151{
152 int c;
153
154 while(1) {
155 c = (n << 1) + 1;
156 if(c >= timeheap.d)
157 break;
bb073004 158 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
589987f8 159 c = c + 1;
bb073004 160 if(timeheap.b[c]->to > bl->to)
589987f8
FT
161 break;
162 timeheap.b[n] = timeheap.b[c];
bb073004 163 timeheap.b[n]->thpos = n;
589987f8
FT
164 n = c;
165 }
bb073004
FT
166 timeheap.b[n] = bl;
167 bl->thpos = n;
589987f8
FT
168}
169
170static void addtimeout(struct blocker *bl, time_t to)
171{
172 sizebuf(timeheap, ++timeheap.d);
bb073004 173 thraise(bl, timeheap.d - 1);
589987f8
FT
174}
175
176static void deltimeout(struct blocker *bl)
177{
589987f8
FT
178 int n;
179
180 if(bl->thpos == timeheap.d - 1) {
181 timeheap.d--;
182 return;
183 }
184 n = bl->thpos;
bb073004
FT
185 bl = timeheap.b[--timeheap.d];
186 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
187 thraise(bl, n);
589987f8 188 else
bb073004 189 thlower(bl, n);
589987f8
FT
190}
191
205ee933 192static int addblock(struct blocker *bl)
a6cda4dd 193{
205ee933 194 if((epfd >= 0) && regfd(bl))
a6cda4dd 195 return(-1);
a6cda4dd
FT
196 bl->n = blockers;
197 if(blockers)
198 blockers->p = bl;
199 blockers = bl;
bcad6b0c
FT
200 if(bl->to > 0)
201 addtimeout(bl, bl->to);
205ee933
FT
202 return(0);
203}
204
205static void remblock(struct blocker *bl)
206{
589987f8
FT
207 if(bl->to > 0)
208 deltimeout(bl);
a6cda4dd
FT
209 if(bl->n)
210 bl->n->p = bl->p;
211 if(bl->p)
212 bl->p->n = bl->n;
205ee933 213 if(blockers == bl)
a6cda4dd
FT
214 blockers = bl->n;
215 remfd(bl);
205ee933
FT
216}
217
218struct selected mblock(time_t to, int n, struct selected *spec)
219{
220 int i, id;
221 struct blocker bls[n];
222
223 to = (to > 0)?(time(NULL) + to):0;
224 for(i = 0; i < n; i++) {
225 bls[i] = (struct blocker) {
226 .fd = spec[i].fd,
227 .ev = spec[i].ev,
228 .id = i,
229 .to = to,
230 .th = current,
231 };
232 if(addblock(&bls[i])) {
4f5bec84 233 for(i--; i >= 0; i--)
205ee933
FT
234 remblock(&bls[i]);
235 return((struct selected){.fd = -1, .ev = -1});
236 }
237 }
238 id = yield();
239 for(i = 0; i < n; i++)
240 remblock(&bls[i]);
241 if(id < 0)
242 return((struct selected){.fd = -1, .ev = -1});
243 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
244}
245
246int block(int fd, int ev, time_t to)
247{
248 struct blocker bl;
249 int rv;
250
251 bl = (struct blocker) {
252 .fd = fd,
253 .ev = ev,
254 .id = -1,
255 .to = (to > 0)?(time(NULL) + to):0,
256 .th = current,
257 };
258 if(addblock(&bl))
259 return(-1);
260 rv = yield();
261 remblock(&bl);
a6cda4dd
FT
262 return(rv);
263}
264
9d32586e 265int ioloop(void)
a6cda4dd
FT
266{
267 struct blocker *bl, *nbl;
268 struct epoll_event evr[16];
269 int i, fd, nev, ev, toval;
589987f8 270 time_t now;
a6cda4dd 271
9d32586e 272 exitstatus = 0;
a6cda4dd 273 epfd = epoll_create(128);
be078ac9 274 fcntl(epfd, F_SETFD, FD_CLOEXEC);
589987f8 275 bufinit(timeheap);
a6cda4dd
FT
276 for(bl = blockers; bl; bl = nbl) {
277 nbl = bl->n;
278 if(regfd(bl))
279 resume(bl->th, -1);
280 }
281 while(blockers != NULL) {
a6cda4dd 282 now = time(NULL);
589987f8 283 if(timeheap.d == 0)
a6cda4dd 284 toval = -1;
bb073004
FT
285 else if(timeheap.b[0]->to > now)
286 toval = (timeheap.b[0]->to - now) * 1000;
a6cda4dd
FT
287 else
288 toval = 1000;
9d32586e
FT
289 if(exitstatus)
290 break;
a6cda4dd
FT
291 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
292 if(nev < 0) {
293 if(errno != EINTR) {
f43decce 294 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
a6cda4dd
FT
295 /* To avoid CPU hogging in case it's bad, which it
296 * probably is. */
297 sleep(1);
298 }
299 continue;
300 }
301 for(i = 0; i < nev; i++) {
302 fd = evr[i].data.fd;
303 ev = 0;
304 if(evr[i].events & EPOLLIN)
305 ev |= EV_READ;
306 if(evr[i].events & EPOLLOUT)
307 ev |= EV_WRITE;
308 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
309 ev = -1;
310 for(bl = fdlist[fd]; bl; bl = nbl) {
311 nbl = bl->n2;
205ee933
FT
312 if((ev < 0) || (ev & bl->ev)) {
313 if(bl->id < 0) {
314 resume(bl->th, ev);
315 } else {
316 bl->rev = ev;
317 resume(bl->th, bl->id);
318 }
319 }
a6cda4dd
FT
320 }
321 }
322 now = time(NULL);
bb073004 323 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
bcad6b0c 324 if(bl->id < 0) {
bb073004 325 resume(bl->th, 0);
bcad6b0c
FT
326 } else {
327 bl->rev = 0;
328 resume(bl->th, bl->id);
205ee933 329 }
a6cda4dd
FT
330 }
331 }
9d32586e
FT
332 for(bl = blockers; bl; bl = bl->n)
333 remfd(bl);
589987f8 334 buffree(timeheap);
a6cda4dd
FT
335 close(epfd);
336 epfd = -1;
9d32586e
FT
337 return(exitstatus);
338}
339
340void exitioloop(int status)
341{
342 exitstatus = status;
a6cda4dd 343}