1 |
/* |
2 |
iom.C -- generic I/O multiplexor |
3 |
Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.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 2 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, write to the Free Software |
17 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
*/ |
19 |
|
20 |
#include <cstdio> |
21 |
#include <cstdlib> |
22 |
#include <cerrno> |
23 |
|
24 |
#include <sys/time.h> |
25 |
|
26 |
#if 1 // older unices need these includes for select(2) |
27 |
# include <unistd.h> |
28 |
# include <sys/types.h> |
29 |
#endif |
30 |
|
31 |
// if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
32 |
// until that happens, sys/select.h must come last |
33 |
#include <sys/select.h> |
34 |
|
35 |
#include "iom.h" |
36 |
|
37 |
// TSTAMP_MAX must still fit into a positive struct timeval |
38 |
#define TSTAMP_MAX (double)(1UL<<31) |
39 |
|
40 |
// this is a dummy time watcher to ensure that the first |
41 |
// time watcher is _always_ valid, this gets rid of a lot |
42 |
// of null-pointer-checks |
43 |
// (must come _before_ iom is being defined) |
44 |
static struct tw0 : time_watcher { |
45 |
void cb (time_watcher &w) |
46 |
{ |
47 |
// should never get called |
48 |
// reached end-of-time, or tstamp has a bogus definition, |
49 |
// or compiler initilization order broken, or somethine else :) |
50 |
abort (); |
51 |
} |
52 |
|
53 |
tw0() |
54 |
: time_watcher (this, &tw0::cb) |
55 |
{ } |
56 |
} tw0; |
57 |
|
58 |
tstamp NOW; |
59 |
static bool iom_valid; |
60 |
io_manager iom; |
61 |
|
62 |
template<class watcher> |
63 |
void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) |
64 |
{ |
65 |
if (!iom_valid) |
66 |
abort (); |
67 |
|
68 |
if (!w->active) |
69 |
{ |
70 |
queue.push_back (w); |
71 |
w->active = queue.size (); |
72 |
} |
73 |
} |
74 |
|
75 |
template<class watcher> |
76 |
void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
77 |
{ |
78 |
if (!iom_valid) |
79 |
return; |
80 |
|
81 |
if (w->active) |
82 |
{ |
83 |
queue [w->active - 1] = 0; |
84 |
w->active = 0; |
85 |
} |
86 |
} |
87 |
|
88 |
#if IOM_TIME |
89 |
void time_watcher::trigger () |
90 |
{ |
91 |
call (*this); |
92 |
|
93 |
iom.reg (this); |
94 |
} |
95 |
|
96 |
void io_manager::reg (time_watcher *w) { reg (w, tw); } |
97 |
void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
98 |
#endif |
99 |
|
100 |
#if IOM_IO |
101 |
void io_manager::reg (io_watcher *w) { reg (w, iow); } |
102 |
void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
103 |
#endif |
104 |
|
105 |
#if IOM_CHECK |
106 |
void io_manager::reg (check_watcher *w) { reg (w, cw); } |
107 |
void io_manager::unreg (check_watcher *w) { unreg (w, cw); } |
108 |
#endif |
109 |
|
110 |
#if IOM_IDLE |
111 |
void io_manager::reg (idle_watcher *w) { reg (w, iw); } |
112 |
void io_manager::unreg (idle_watcher *w) { unreg (w, iw); } |
113 |
#endif |
114 |
|
115 |
#if IOM_TIME |
116 |
inline void set_now (void) |
117 |
{ |
118 |
struct timeval tv; |
119 |
|
120 |
gettimeofday (&tv, 0); |
121 |
|
122 |
NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
123 |
#endif |
124 |
} |
125 |
|
126 |
void io_manager::loop () |
127 |
{ |
128 |
#if IOM_TIME |
129 |
set_now (); |
130 |
#endif |
131 |
|
132 |
for (;;) |
133 |
{ |
134 |
struct timeval *to = 0; |
135 |
struct timeval tval; |
136 |
|
137 |
#if IOM_IDLE |
138 |
if (iw.size ()) |
139 |
{ |
140 |
tval.tv_sec = 0; |
141 |
tval.tv_usec = 0; |
142 |
to = &tval; |
143 |
} |
144 |
else |
145 |
#endif |
146 |
{ |
147 |
#if IOM_TIME |
148 |
time_watcher *next; |
149 |
|
150 |
for (;;) |
151 |
{ |
152 |
next = tw[0]; // the first time-watcher must exist at ALL times |
153 |
|
154 |
for (int i = tw.size (); i--; ) |
155 |
if (!tw[i]) |
156 |
tw.erase_unordered (i); |
157 |
else if (tw[i]->at < next->at) |
158 |
next = tw[i]; |
159 |
|
160 |
if (next->at > NOW) |
161 |
{ |
162 |
if (next != tw[0]) |
163 |
{ |
164 |
double diff = next->at - NOW; |
165 |
tval.tv_sec = (int)diff; |
166 |
tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
167 |
to = &tval; |
168 |
} |
169 |
break; |
170 |
} |
171 |
else |
172 |
{ |
173 |
unreg (next); |
174 |
next->call (*next); |
175 |
} |
176 |
} |
177 |
#endif |
178 |
} |
179 |
|
180 |
#if IOM_CHECK |
181 |
for (int i = cw.size (); i--; ) |
182 |
if (!cw[i]) |
183 |
cw.erase_unordered (i); |
184 |
else |
185 |
cw[i]->call (*cw[i]); |
186 |
#endif |
187 |
|
188 |
#if IOM_IO |
189 |
fd_set rfd, wfd, efd; |
190 |
|
191 |
FD_ZERO (&rfd); |
192 |
FD_ZERO (&wfd); |
193 |
|
194 |
int fds = 0; |
195 |
|
196 |
for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
197 |
if (*i) |
198 |
{ |
199 |
if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
200 |
if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
201 |
|
202 |
if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
203 |
} |
204 |
|
205 |
if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
206 |
break; // no events |
207 |
|
208 |
fds = select (fds, &rfd, &wfd, &efd, to); |
209 |
# if IOM_TIME |
210 |
set_now (); |
211 |
# endif |
212 |
|
213 |
if (fds > 0) |
214 |
for (int i = iow.size (); i--; ) |
215 |
if (!iow[i]) |
216 |
iow.erase_unordered (i); |
217 |
else |
218 |
{ |
219 |
short revents = iow[i]->events; |
220 |
|
221 |
if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; |
222 |
if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; |
223 |
|
224 |
if (revents) |
225 |
iow[i]->call (*iow[i], revents); |
226 |
} |
227 |
else if (fds < 0 && errno != EINTR) |
228 |
{ |
229 |
perror ("Error while waiting for I/O or time event"); |
230 |
abort (); |
231 |
} |
232 |
#if IOM_IDLE |
233 |
else |
234 |
for (int i = iw.size (); i--; ) |
235 |
if (!iw[i]) |
236 |
iw.erase_unordered (i); |
237 |
else |
238 |
iw[i]->call (*iw[i]); |
239 |
#endif |
240 |
|
241 |
#elif IOM_TIME |
242 |
if (!to) |
243 |
break; |
244 |
|
245 |
select (0, 0, 0, 0, &to); |
246 |
set_now (); |
247 |
#else |
248 |
break; |
249 |
#endif |
250 |
} |
251 |
} |
252 |
|
253 |
io_manager::io_manager () |
254 |
{ |
255 |
iom_valid = true; |
256 |
|
257 |
#if IOM_TIME |
258 |
set_now (); |
259 |
|
260 |
tw0.start (TSTAMP_MAX); |
261 |
#endif |
262 |
} |
263 |
|
264 |
io_manager::~io_manager () |
265 |
{ |
266 |
iom_valid = false; |
267 |
} |
268 |
|