lib: Reimplemented mtio-epoll timeout checking as a bin-heap.
[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;
40 int ev;
589987f8 41 int thpos;
a6cda4dd
FT
42 time_t to;
43 struct muth *th;
44};
45
589987f8
FT
46struct timeentry {
47 time_t to;
48 struct blocker *bl;
49};
50
a6cda4dd 51static int epfd = -1, fdln = 0;
9d32586e 52static int exitstatus;
a6cda4dd 53static struct blocker **fdlist;
589987f8 54static typedbuf(struct timeentry) timeheap;
a6cda4dd
FT
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 }
9d32586e 136 bl->reg = 0;
a6cda4dd
FT
137}
138
589987f8
FT
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
a6cda4dd
FT
198int block(int fd, int ev, time_t to)
199{
200 struct blocker *bl;
201 int rv;
202
203 omalloc(bl);
204 bl->fd = fd;
205 bl->ev = ev;
a6cda4dd
FT
206 bl->th = current;
207 if((epfd >= 0) && regfd(bl)) {
208 free(bl);
209 return(-1);
210 }
211 bl->n = blockers;
212 if(blockers)
213 blockers->p = bl;
214 blockers = bl;
589987f8
FT
215 if(to > 0)
216 addtimeout(bl, bl->to = (time(NULL) + to));
a6cda4dd 217 rv = yield();
589987f8
FT
218 if(bl->to > 0)
219 deltimeout(bl);
a6cda4dd
FT
220 if(bl->n)
221 bl->n->p = bl->p;
222 if(bl->p)
223 bl->p->n = bl->n;
224 if(bl == blockers)
225 blockers = bl->n;
226 remfd(bl);
227 free(bl);
228 return(rv);
229}
230
9d32586e 231int ioloop(void)
a6cda4dd
FT
232{
233 struct blocker *bl, *nbl;
234 struct epoll_event evr[16];
235 int i, fd, nev, ev, toval;
589987f8 236 time_t now;
a6cda4dd 237
9d32586e 238 exitstatus = 0;
a6cda4dd 239 epfd = epoll_create(128);
be078ac9 240 fcntl(epfd, F_SETFD, FD_CLOEXEC);
589987f8 241 bufinit(timeheap);
a6cda4dd
FT
242 for(bl = blockers; bl; bl = nbl) {
243 nbl = bl->n;
244 if(regfd(bl))
245 resume(bl->th, -1);
246 }
247 while(blockers != NULL) {
a6cda4dd 248 now = time(NULL);
589987f8 249 if(timeheap.d == 0)
a6cda4dd 250 toval = -1;
589987f8
FT
251 else if(timeheap.b[0].to > now)
252 toval = (timeheap.b[0].to - now) * 1000;
a6cda4dd
FT
253 else
254 toval = 1000;
9d32586e
FT
255 if(exitstatus)
256 break;
a6cda4dd
FT
257 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
258 if(nev < 0) {
259 if(errno != EINTR) {
f43decce 260 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
a6cda4dd
FT
261 /* To avoid CPU hogging in case it's bad, which it
262 * probably is. */
263 sleep(1);
264 }
265 continue;
266 }
267 for(i = 0; i < nev; i++) {
268 fd = evr[i].data.fd;
269 ev = 0;
270 if(evr[i].events & EPOLLIN)
271 ev |= EV_READ;
272 if(evr[i].events & EPOLLOUT)
273 ev |= EV_WRITE;
274 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
275 ev = -1;
276 for(bl = fdlist[fd]; bl; bl = nbl) {
277 nbl = bl->n2;
278 if((ev < 0) || (ev & bl->ev))
279 resume(bl->th, ev);
280 }
281 }
282 now = time(NULL);
589987f8
FT
283 while((timeheap.d > 0) && (timeheap.b[0].to <= now))
284 resume(timeheap.b[0].bl->th, 0);
a6cda4dd 285 }
9d32586e
FT
286 for(bl = blockers; bl; bl = bl->n)
287 remfd(bl);
589987f8 288 buffree(timeheap);
a6cda4dd
FT
289 close(epfd);
290 epfd = -1;
9d32586e
FT
291 return(exitstatus);
292}
293
294void exitioloop(int status)
295{
296 exitstatus = status;
a6cda4dd 297}