Moved htparser's ioloop to the library.
[ashd.git] / lib / mtio.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 <string.h>
22 #include <time.h>
23 #include <errno.h>
24 #include <sys/select.h>
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #include <log.h>
30 #include <utils.h>
31 #include <mt.h>
32 #include <mtio.h>
33
34 struct blocker {
35     struct blocker *n, *p;
36     int fd;
37     int ev;
38     time_t to;
39     struct muth *th;
40 };
41
42 static struct blocker *blockers;
43
44 int block(int fd, int ev, time_t to)
45 {
46     struct blocker *bl;
47     int rv;
48     
49     omalloc(bl);
50     bl->fd = fd;
51     bl->ev = ev;
52     if(to > 0)
53         bl->to = time(NULL) + to;
54     bl->th = current;
55     bl->n = blockers;
56     if(blockers)
57         blockers->p = bl;
58     blockers = bl;
59     rv = yield();
60     if(bl->n)
61         bl->n->p = bl->p;
62     if(bl->p)
63         bl->p->n = bl->n;
64     if(bl == blockers)
65         blockers = bl->n;
66     return(rv);
67 }
68
69 void ioloop(void)
70 {
71     int ret;
72     fd_set rfds, wfds, efds;
73     struct blocker *bl, *nbl;
74     struct timeval toval;
75     time_t now, timeout;
76     int maxfd;
77     int ev;
78     
79     while(blockers != NULL) {
80         FD_ZERO(&rfds);
81         FD_ZERO(&wfds);
82         FD_ZERO(&efds);
83         maxfd = 0;
84         now = time(NULL);
85         timeout = 0;
86         for(bl = blockers; bl; bl = bl->n) {
87             if(bl->ev & EV_READ)
88                 FD_SET(bl->fd, &rfds);
89             if(bl->ev & EV_WRITE)
90                 FD_SET(bl->fd, &wfds);
91             FD_SET(bl->fd, &efds);
92             if(bl->fd > maxfd)
93                 maxfd = bl->fd;
94             if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
95                 timeout = bl->to;
96         }
97         toval.tv_sec = timeout - now;
98         toval.tv_usec = 0;
99         ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
100         if(ret < 0) {
101             if(errno != EINTR) {
102                 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
103                 /* To avoid CPU hogging in case it's bad, which it
104                  * probably is. */
105                 sleep(1);
106             }
107         }
108         now = time(NULL);
109         for(bl = blockers; bl; bl = nbl) {
110             nbl = bl->n;
111             ev = 0;
112             if(FD_ISSET(bl->fd, &rfds))
113                 ev |= EV_READ;
114             if(FD_ISSET(bl->fd, &wfds))
115                 ev |= EV_WRITE;
116             if(FD_ISSET(bl->fd, &efds))
117                 ev = -1;
118             if(ev != 0)
119                 resume(bl->th, ev);
120             else if((bl->to != 0) && (bl->to <= now))
121                 resume(bl->th, 0);
122         }
123     }
124 }