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