lib: Removed the surely redundant struct timeentry from the mtio-epoll.
[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, rev, id;
41     int thpos;
42     time_t to;
43     struct muth *th;
44 };
45
46 static int epfd = -1, fdln = 0;
47 static int exitstatus;
48 static struct blocker **fdlist;
49 static typedbuf(struct blocker *) timeheap;
50
51 static 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
100 static 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
134 static 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
150 static 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
170 static void addtimeout(struct blocker *bl, time_t to)
171 {
172     sizebuf(timeheap, ++timeheap.d);
173     thraise(bl, timeheap.d - 1);
174 }
175
176 static 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
192 static 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
205 static 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
218 struct 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
246 int 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
265 int 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     bufinit(timeheap);
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) {
282         now = time(NULL);
283         if(timeheap.d == 0)
284             toval = -1;
285         else if(timeheap.b[0]->to > now)
286             toval = (timeheap.b[0]->to - now) * 1000;
287         else
288             toval = 1000;
289         if(exitstatus)
290             break;
291         nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
292         if(nev < 0) {
293             if(errno != EINTR) {
294                 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
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;
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                 }
320             }
321         }
322         now = time(NULL);
323         while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
324             if(bl->id < 0) {
325                 resume(bl->th, 0);
326             } else {
327                 bl->rev = 0;
328                 resume(bl->th, bl->id);
329             }
330         }
331     }
332     for(bl = blockers; bl; bl = bl->n)
333         remfd(bl);
334     buffree(timeheap);
335     close(epfd);
336     epfd = -1;
337     return(exitstatus);
338 }
339
340 void exitioloop(int status)
341 {
342     exitstatus = status;
343 }