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