lib: Added support for blocking on multiple file descriptors.
[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     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 static int addblock(struct blocker *bl)
133 {
134     if((epfd >= 0) && regfd(bl))
135         return(-1);
136     bl->n = blockers;
137     if(blockers)
138         blockers->p = bl;
139     blockers = bl;
140     return(0);
141 }
142
143 static void remblock(struct blocker *bl)
144 {
145     if(bl->n)
146         bl->n->p = bl->p;
147     if(bl->p)
148         bl->p->n = bl->n;
149     if(blockers == bl)
150         blockers = bl->n;
151     remfd(bl);
152 }
153
154 struct selected mblock(time_t to, int n, struct selected *spec)
155 {
156     int i, id;
157     struct blocker bls[n];
158     
159     to = (to > 0)?(time(NULL) + to):0;
160     for(i = 0; i < n; i++) {
161         bls[i] = (struct blocker) {
162             .fd = spec[i].fd,
163             .ev = spec[i].ev,
164             .id = i,
165             .to = to,
166             .th = current,
167         };
168         if(addblock(&bls[i])) {
169             for(i--; i >= 0; i++)
170                 remblock(&bls[i]);
171             return((struct selected){.fd = -1, .ev = -1});
172         }
173     }
174     id = yield();
175     for(i = 0; i < n; i++)
176         remblock(&bls[i]);
177     if(id < 0)
178         return((struct selected){.fd = -1, .ev = -1});
179     return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
180 }
181
182 int block(int fd, int ev, time_t to)
183 {
184     struct blocker bl;
185     int rv;
186     
187     bl = (struct blocker) {
188         .fd = fd,
189         .ev = ev,
190         .id = -1,
191         .to = (to > 0)?(time(NULL) + to):0,
192         .th = current,
193     };
194     if(addblock(&bl))
195         return(-1);
196     rv = yield();
197     remblock(&bl);
198     return(rv);
199 }
200
201 int ioloop(void)
202 {
203     struct blocker *bl, *nbl;
204     struct epoll_event evr[16];
205     int i, fd, nev, ev, toval;
206     time_t now, timeout;
207     
208     exitstatus = 0;
209     epfd = epoll_create(128);
210     fcntl(epfd, F_SETFD, FD_CLOEXEC);
211     for(bl = blockers; bl; bl = nbl) {
212         nbl = bl->n;
213         if(regfd(bl))
214             resume(bl->th, -1);
215     }
216     while(blockers != NULL) {
217         timeout = 0;
218         for(bl = blockers; bl; bl = bl->n) {
219             if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
220                 timeout = bl->to;
221         }
222         now = time(NULL);
223         if(timeout == 0)
224             toval = -1;
225         else if(timeout > now)
226             toval = (timeout - now) * 1000;
227         else
228             toval = 1000;
229         if(exitstatus)
230             break;
231         nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
232         if(nev < 0) {
233             if(errno != EINTR) {
234                 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
235                 /* To avoid CPU hogging in case it's bad, which it
236                  * probably is. */
237                 sleep(1);
238             }
239             continue;
240         }
241         for(i = 0; i < nev; i++) {
242             fd = evr[i].data.fd;
243             ev = 0;
244             if(evr[i].events & EPOLLIN)
245                 ev |= EV_READ;
246             if(evr[i].events & EPOLLOUT)
247                 ev |= EV_WRITE;
248             if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
249                 ev = -1;
250             for(bl = fdlist[fd]; bl; bl = nbl) {
251                 nbl = bl->n2;
252                 if((ev < 0) || (ev & bl->ev)) {
253                     if(bl->id < 0) {
254                         resume(bl->th, ev);
255                     } else {
256                         bl->rev = ev;
257                         resume(bl->th, bl->id);
258                     }
259                 }
260             }
261         }
262         now = time(NULL);
263         for(bl = blockers; bl; bl = nbl) {
264             nbl = bl->n;
265             if((bl->to != 0) && (bl->to <= now)) {
266                 if(bl->id < 0) {
267                     resume(bl->th, 0);
268                 } else {
269                     bl->rev = 0;
270                     resume(bl->th, bl->id);
271                 }
272             }
273         }
274     }
275     for(bl = blockers; bl; bl = bl->n)
276         remfd(bl);
277     close(epfd);
278     epfd = -1;
279     return(exitstatus);
280 }
281
282 void exitioloop(int status)
283 {
284     exitstatus = status;
285 }