2a2599218467813d38fa3d17a558b8d286015675
[ashd.git] / lib / mtio-epoll.c
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
35 static struct blocker *blockers;
36
37 struct blocker {
38     struct blocker *n, *p, *n2, *p2;
39     int fd, reg;
40     int ev;
41     int thpos;
42     time_t to;
43     struct muth *th;
44 };
45
46 struct timeentry {
47     time_t to;
48     struct blocker *bl;
49 };
50
51 static int epfd = -1, fdln = 0;
52 static int exitstatus;
53 static struct blocker **fdlist;
54 static typedbuf(struct timeentry) timeheap;
55
56 static 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
105 static 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
139 static 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
155 static 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
175 static 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
181 static 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
198 int 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;
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;
215     if(to > 0)
216         addtimeout(bl, bl->to = (time(NULL) + to));
217     rv = yield();
218     if(bl->to > 0)
219         deltimeout(bl);
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
231 int ioloop(void)
232 {
233     struct blocker *bl, *nbl;
234     struct epoll_event evr[16];
235     int i, fd, nev, ev, toval;
236     time_t now;
237     
238     exitstatus = 0;
239     epfd = epoll_create(128);
240     fcntl(epfd, F_SETFD, FD_CLOEXEC);
241     bufinit(timeheap);
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) {
248         now = time(NULL);
249         if(timeheap.d == 0)
250             toval = -1;
251         else if(timeheap.b[0].to > now)
252             toval = (timeheap.b[0].to - now) * 1000;
253         else
254             toval = 1000;
255         if(exitstatus)
256             break;
257         nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
258         if(nev < 0) {
259             if(errno != EINTR) {
260                 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
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);
283         while((timeheap.d > 0) && (timeheap.b[0].to <= now))
284             resume(timeheap.b[0].bl->th, 0);
285     }
286     for(bl = blockers; bl; bl = bl->n)
287         remfd(bl);
288     buffree(timeheap);
289     close(epfd);
290     epfd = -1;
291     return(exitstatus);
292 }
293
294 void exitioloop(int status)
295 {
296     exitstatus = status;
297 }