Merge branch 'master' of git.dolda2000.com:/srv/git/r/ashd
[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 <fcntl.h>
21 #include <string.h>
22 #include <sys/epoll.h>
23 #include <errno.h>
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <log.h>
29 #include <utils.h>
30 #include <mt.h>
31 #include <mtio.h>
32
33 static struct blocker *blockers;
34
35 struct blocker {
36     struct blocker *n, *p, *n2, *p2;
37     int fd, reg;
38     int ev;
39     time_t to;
40     struct muth *th;
41 };
42
43 static int epfd = -1, fdln = 0;
44 static struct blocker **fdlist;
45
46 static int regfd(struct blocker *bl)
47 {
48     struct blocker *o;
49     struct epoll_event evd;
50     
51     memset(&evd, 0, sizeof(evd));
52     evd.events = 0;
53     if(bl->ev & EV_READ)
54         evd.events |= EPOLLIN;
55     if(bl->ev & EV_WRITE)
56         evd.events |= EPOLLOUT;
57     evd.data.fd = bl->fd;
58     if(bl->fd >= fdln) {
59         if(fdlist) {
60             fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
61             memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
62             fdln = bl->fd + 1;
63         } else {
64             fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
65         }
66     }
67     if(fdlist[bl->fd] == NULL) {
68         if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
69             /* XXX?! Whatever to do, really? */
70             flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
71             return(-1);
72         }
73     } else {
74         for(o = fdlist[bl->fd]; o; o = o->n2) {
75             if(o->ev & EV_READ)
76                 evd.events |= EPOLLIN;
77             if(o->ev & EV_WRITE)
78                 evd.events |= EPOLLOUT;
79         }
80         if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
81             /* XXX?! Whatever to do, really? */
82             flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
83             return(-1);
84         }
85     }
86     bl->n2 = fdlist[bl->fd];
87     bl->p2 = NULL;
88     if(fdlist[bl->fd] != NULL)
89         fdlist[bl->fd]->p2 = bl;
90     fdlist[bl->fd] = bl;
91     bl->reg = 1;
92     return(0);
93 }
94
95 static void remfd(struct blocker *bl)
96 {
97     struct blocker *o;
98     struct epoll_event evd;
99     
100     if(!bl->reg)
101         return;
102     if(bl->n2)
103         bl->n2->p2 = bl->p2;
104     if(bl->p2)
105         bl->p2->n2 = bl->n2;
106     if(bl == fdlist[bl->fd])
107         fdlist[bl->fd] = bl->n2;
108     if(fdlist[bl->fd] == NULL) {
109         if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
110             flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
111     } else {
112         memset(&evd, 0, sizeof(evd));
113         evd.events = 0;
114         evd.data.fd = bl->fd;
115         for(o = fdlist[bl->fd]; o; o = o->n2) {
116             if(o->ev & EV_READ)
117                 evd.events |= EPOLLIN;
118             if(o->ev & EV_WRITE)
119                 evd.events |= EPOLLOUT;
120         }
121         if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
122             /* XXX?! Whatever to do, really? */
123             flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
124         }
125     }
126 }
127
128 int block(int fd, int ev, time_t to)
129 {
130     struct blocker *bl;
131     int rv;
132     
133     omalloc(bl);
134     bl->fd = fd;
135     bl->ev = ev;
136     if(to > 0)
137         bl->to = time(NULL) + to;
138     bl->th = current;
139     if((epfd >= 0) && regfd(bl)) {
140         free(bl);
141         return(-1);
142     }
143     bl->n = blockers;
144     if(blockers)
145         blockers->p = bl;
146     blockers = bl;
147     rv = yield();
148     if(bl->n)
149         bl->n->p = bl->p;
150     if(bl->p)
151         bl->p->n = bl->n;
152     if(bl == blockers)
153         blockers = bl->n;
154     remfd(bl);
155     free(bl);
156     return(rv);
157 }
158
159 void ioloop(void)
160 {
161     struct blocker *bl, *nbl;
162     struct epoll_event evr[16];
163     int i, fd, nev, ev, toval;
164     time_t now, timeout;
165     
166     epfd = epoll_create(128);
167     fcntl(epfd, F_SETFD, FD_CLOEXEC);
168     for(bl = blockers; bl; bl = nbl) {
169         nbl = bl->n;
170         if(regfd(bl))
171             resume(bl->th, -1);
172     }
173     while(blockers != NULL) {
174         timeout = 0;
175         for(bl = blockers; bl; bl = bl->n) {
176             if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
177                 timeout = bl->to;
178         }
179         now = time(NULL);
180         if(timeout == 0)
181             toval = -1;
182         else if(timeout > now)
183             toval = (timeout - now) * 1000;
184         else
185             toval = 1000;
186         nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
187         if(nev < 0) {
188             if(errno != EINTR) {
189                 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
190                 /* To avoid CPU hogging in case it's bad, which it
191                  * probably is. */
192                 sleep(1);
193             }
194             continue;
195         }
196         for(i = 0; i < nev; i++) {
197             fd = evr[i].data.fd;
198             ev = 0;
199             if(evr[i].events & EPOLLIN)
200                 ev |= EV_READ;
201             if(evr[i].events & EPOLLOUT)
202                 ev |= EV_WRITE;
203             if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
204                 ev = -1;
205             for(bl = fdlist[fd]; bl; bl = nbl) {
206                 nbl = bl->n2;
207                 if((ev < 0) || (ev & bl->ev))
208                     resume(bl->th, ev);
209             }
210         }
211         now = time(NULL);
212         for(bl = blockers; bl; bl = nbl) {
213             nbl = bl->n;
214             if((bl->to != 0) && (bl->to <= now))
215                 resume(bl->th, 0);
216         }
217     }
218     close(epfd);
219     epfd = -1;
220 }