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