Added the `ratequeue' program.
[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
46static int epfd = -1, fdln = 0;
47static int exitstatus;
48static struct blocker **fdlist;
49static typedbuf(struct blocker *) timeheap;
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 }
131 bl->reg = 0;
132}
133
134static void thraise(struct blocker *bl, int n)
135{
136 int p;
137
138 while(n > 0) {
139 p = (n - 1) >> 1;
140 if(timeheap.b[p]->to <= bl->to)
141 break;
142 timeheap.b[n] = timeheap.b[p];
143 timeheap.b[n]->thpos = n;
144 n = p;
145 }
146 timeheap.b[n] = bl;
147 bl->thpos = n;
148}
149
150static void thlower(struct blocker *bl, int n)
151{
152 int c;
153
154 while(1) {
155 c = (n << 1) + 1;
156 if(c >= timeheap.d)
157 break;
158 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
159 c = c + 1;
160 if(timeheap.b[c]->to > bl->to)
161 break;
162 timeheap.b[n] = timeheap.b[c];
163 timeheap.b[n]->thpos = n;
164 n = c;
165 }
166 timeheap.b[n] = bl;
167 bl->thpos = n;
168}
169
170static void addtimeout(struct blocker *bl, time_t to)
171{
172 sizebuf(timeheap, ++timeheap.d);
173 thraise(bl, timeheap.d - 1);
174}
175
176static void deltimeout(struct blocker *bl)
177{
178 int n;
179
180 if(bl->thpos == timeheap.d - 1) {
181 timeheap.d--;
182 return;
183 }
184 n = bl->thpos;
185 bl = timeheap.b[--timeheap.d];
186 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
187 thraise(bl, n);
188 else
189 thlower(bl, n);
190}
191
192static int addblock(struct blocker *bl)
193{
194 if((epfd >= 0) && regfd(bl))
195 return(-1);
196 bl->n = blockers;
197 if(blockers)
198 blockers->p = bl;
199 blockers = bl;
200 if(bl->to > 0)
201 addtimeout(bl, bl->to);
202 return(0);
203}
204
205static void remblock(struct blocker *bl)
206{
207 if(bl->to > 0)
208 deltimeout(bl);
209 if(bl->n)
210 bl->n->p = bl->p;
211 if(bl->p)
212 bl->p->n = bl->n;
213 if(blockers == bl)
214 blockers = bl->n;
215 remfd(bl);
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])) {
233 for(i--; i >= 0; i--)
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);
262 return(rv);
263}
264
265int ioloop(void)
266{
267 struct blocker *bl, *nbl;
268 struct epoll_event evr[16];
269 int i, fd, nev, ev, toval;
270 time_t now;
271
272 exitstatus = 0;
273 epfd = epoll_create(128);
274 fcntl(epfd, F_SETFD, FD_CLOEXEC);
275 for(bl = blockers; bl; bl = nbl) {
276 nbl = bl->n;
277 if(regfd(bl))
278 resume(bl->th, -1);
279 }
280 while(blockers != NULL) {
281 now = time(NULL);
282 if(timeheap.d == 0)
283 toval = -1;
284 else if(timeheap.b[0]->to > now)
285 toval = (timeheap.b[0]->to - now) * 1000;
286 else
287 toval = 1000;
288 if(exitstatus)
289 break;
290 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
291 if(nev < 0) {
292 if(errno != EINTR) {
293 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
294 /* To avoid CPU hogging in case it's bad, which it
295 * probably is. */
296 sleep(1);
297 }
298 continue;
299 }
300 for(i = 0; i < nev; i++) {
301 fd = evr[i].data.fd;
302 ev = 0;
303 if(evr[i].events & EPOLLIN)
304 ev |= EV_READ;
305 if(evr[i].events & EPOLLOUT)
306 ev |= EV_WRITE;
307 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
308 ev = -1;
309 for(bl = fdlist[fd]; bl; bl = nbl) {
310 nbl = bl->n2;
311 if((ev < 0) || (ev & bl->ev)) {
312 if(bl->id < 0) {
313 resume(bl->th, ev);
314 } else {
315 bl->rev = ev;
316 resume(bl->th, bl->id);
317 }
318 }
319 }
320 }
321 now = time(NULL);
322 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
323 if(bl->id < 0) {
324 resume(bl->th, 0);
325 } else {
326 bl->rev = 0;
327 resume(bl->th, bl->id);
328 }
329 }
330 }
331 for(bl = blockers; bl; bl = bl->n)
332 remfd(bl);
333 close(epfd);
334 epfd = -1;
335 return(exitstatus);
336}
337
338void exitioloop(int status)
339{
340 exitstatus = status;
341}