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