1 | /* |
1 | /* |
2 | iom.C -- generic I/O multiplexor |
2 | iom.C -- generic I/O multiplexor |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
15 | You should have received a copy of the GNU General Public License |
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 |
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 |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include "../config.h" |
|
|
21 | |
|
|
22 | #include <cstdio> |
20 | #include <cstdio> |
|
|
21 | #include <cstdlib> |
|
|
22 | #include <cerrno> |
23 | |
23 | |
|
|
24 | #include <sys/time.h> |
|
|
25 | |
|
|
26 | #include <assert.h> |
|
|
27 | |
|
|
28 | #if 1 // older unices need these includes for select (2) |
|
|
29 | # include <unistd.h> |
|
|
30 | # include <sys/types.h> |
|
|
31 | #endif |
|
|
32 | |
|
|
33 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
|
|
34 | // until that happens, sys/select.h must come last |
24 | #include <sys/select.h> |
35 | #include <sys/select.h> |
|
|
36 | |
|
|
37 | // for IOM_SIG |
25 | #include <sys/time.h> |
38 | #include <signal.h> |
26 | |
39 | |
27 | #include "iom.h" |
40 | #include "iom.h" |
28 | |
41 | |
|
|
42 | // TSTAMP_MAX must still fit into a positive struct timeval |
|
|
43 | #define TSTAMP_MAX (double)(1UL<<31) |
|
|
44 | |
|
|
45 | // this is a dummy time watcher to ensure that the first |
|
|
46 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
47 | // of null-pointer-checks |
|
|
48 | // (must come _before_ iom is being defined) |
|
|
49 | static struct tw0 : time_watcher |
|
|
50 | { |
|
|
51 | void cb (time_watcher &w) |
|
|
52 | { |
|
|
53 | // should never get called |
|
|
54 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
55 | // or compiler initialisation order broken, or something else :) |
|
|
56 | abort (); |
|
|
57 | } |
|
|
58 | |
|
|
59 | tw0 () |
|
|
60 | : time_watcher (this, &tw0::cb) |
|
|
61 | { } |
|
|
62 | } tw0; |
|
|
63 | |
29 | tstamp NOW; |
64 | tstamp NOW; |
30 | bool iom_valid; |
65 | static bool iom_valid; |
31 | io_manager iom; |
66 | io_manager iom; |
32 | |
67 | |
|
|
68 | template<class watcher> |
|
|
69 | void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) |
|
|
70 | { |
|
|
71 | if (!iom_valid) |
|
|
72 | abort (); |
|
|
73 | |
|
|
74 | if (!w->active) |
|
|
75 | { |
|
|
76 | #if IOM_CHECK |
|
|
77 | queue.activity = true; |
|
|
78 | #endif |
|
|
79 | queue.push_back (w); |
|
|
80 | w->active = queue.size (); |
|
|
81 | } |
|
|
82 | } |
|
|
83 | |
|
|
84 | template<class watcher> |
|
|
85 | void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
|
|
86 | { |
|
|
87 | if (!iom_valid) |
|
|
88 | return; |
|
|
89 | |
|
|
90 | if (w->active) |
|
|
91 | { |
|
|
92 | queue [w->active - 1] = 0; |
|
|
93 | w->active = 0; |
|
|
94 | } |
|
|
95 | } |
|
|
96 | |
|
|
97 | #if IOM_TIME |
33 | void time_watcher::trigger () |
98 | void time_watcher::trigger () |
34 | { |
99 | { |
35 | call (*this); |
100 | call (*this); |
36 | |
101 | |
37 | iom.reg (this); |
102 | iom.reg (this); |
38 | } |
103 | } |
39 | |
104 | |
40 | time_watcher::~time_watcher () |
|
|
41 | { |
|
|
42 | if (iom_valid) |
|
|
43 | iom.unreg (this); |
|
|
44 | } |
|
|
45 | |
|
|
46 | io_watcher::~io_watcher () |
|
|
47 | { |
|
|
48 | if (iom_valid) |
|
|
49 | iom.unreg (this); |
|
|
50 | } |
|
|
51 | |
|
|
52 | void io_manager::reg (io_watcher *w) |
|
|
53 | { |
|
|
54 | if (find (iow.begin (), iow.end (), w) == iow.end ()) |
|
|
55 | iow.push_back (w); |
|
|
56 | } |
|
|
57 | |
|
|
58 | void io_manager::unreg (io_watcher *w) |
|
|
59 | { |
|
|
60 | iow.erase (find (iow.begin (), iow.end (), w)); |
|
|
61 | } |
|
|
62 | |
|
|
63 | void io_manager::reg (time_watcher *w) |
105 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
64 | { |
|
|
65 | if (find (tw.begin (), tw.end (), w) == tw.end ()) |
|
|
66 | tw.push_back (w); |
|
|
67 | } |
|
|
68 | |
|
|
69 | void io_manager::unreg (time_watcher *w) |
106 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
70 | { |
107 | #endif |
71 | tw.erase (find (tw.begin (), tw.end (), w)); |
|
|
72 | } |
|
|
73 | |
108 | |
|
|
109 | #if IOM_IO |
|
|
110 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
|
|
111 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
|
|
112 | #endif |
|
|
113 | |
|
|
114 | #if IOM_CHECK |
|
|
115 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
|
|
116 | void io_manager::unreg (check_watcher *w) { unreg (w, cw); } |
|
|
117 | #endif |
|
|
118 | |
|
|
119 | #if IOM_IDLE |
|
|
120 | void io_manager::reg (idle_watcher *w) { reg (w, iw); } |
|
|
121 | void io_manager::unreg (idle_watcher *w) { unreg (w, iw); } |
|
|
122 | #endif |
|
|
123 | |
|
|
124 | #if IOM_TIME |
74 | inline void set_now (void) |
125 | inline void set_now (void) |
75 | { |
126 | { |
76 | struct timeval tv; |
127 | struct timeval tv; |
77 | |
128 | |
78 | gettimeofday (&tv, 0); |
129 | gettimeofday (&tv, 0); |
79 | |
130 | |
80 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
131 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
81 | } |
132 | } |
|
|
133 | #endif |
82 | |
134 | |
|
|
135 | #if IOM_SIG |
|
|
136 | // race conditions galore |
|
|
137 | |
|
|
138 | void io_manager::sighandler (int signum) |
|
|
139 | { |
|
|
140 | assert (0 < signum && signum <= iom.sw.size ()); |
|
|
141 | |
|
|
142 | sig_vec &sv = *iom.sw [signum - 1]; |
|
|
143 | |
|
|
144 | for (int i = sv.size (); i--; ) |
|
|
145 | if (!sv[i]) |
|
|
146 | sv.erase_unordered (i); |
|
|
147 | else |
|
|
148 | sv[i]->call (*sv[i]); |
|
|
149 | } |
|
|
150 | |
|
|
151 | void io_manager::reg (sig_watcher *w) |
|
|
152 | { |
|
|
153 | assert (0 < w->signum); |
|
|
154 | |
|
|
155 | sw.reserve (w->signum); |
|
|
156 | |
|
|
157 | sig_vec *&sv = sw [w->signum - 1]; |
|
|
158 | |
|
|
159 | if (!sv) |
|
|
160 | { |
|
|
161 | sv = new sig_vec; |
|
|
162 | |
|
|
163 | struct sigaction sa; |
|
|
164 | sa.sa_handler = io_manager::sighandler; |
|
|
165 | sigfillset (&sa.sa_mask); |
|
|
166 | sa.sa_flags = 0; |
|
|
167 | |
|
|
168 | if (sigaction (w->signum, &sa, 0)) |
|
|
169 | { |
|
|
170 | perror ("Error while installing signal handler"); |
|
|
171 | abort (); |
|
|
172 | } |
|
|
173 | } |
|
|
174 | |
|
|
175 | reg (w, *sv); |
|
|
176 | } |
|
|
177 | |
|
|
178 | void io_manager::unreg (sig_watcher *w) |
|
|
179 | { |
|
|
180 | assert (0 < w->signum && w->signum <= sw.size ()); |
|
|
181 | |
|
|
182 | unreg (w, *sw [w->signum - 1]); |
|
|
183 | } |
|
|
184 | |
|
|
185 | void sig_watcher::start (int signum) |
|
|
186 | { |
|
|
187 | stop (); |
|
|
188 | this->signum = signum; |
|
|
189 | iom.reg (this); |
|
|
190 | } |
|
|
191 | #endif |
|
|
192 | |
83 | void io_manager::loop () |
193 | void io_manager::loop () |
84 | { |
194 | { |
|
|
195 | #if IOM_TIME |
85 | set_now (); |
196 | set_now (); |
|
|
197 | #endif |
86 | |
198 | |
87 | for (;;) |
199 | for (;;) |
88 | { |
200 | { |
89 | time_watcher *w; |
201 | struct timeval *to = 0; |
90 | |
|
|
91 | for (;;) |
|
|
92 | { |
|
|
93 | w = tw[0]; |
|
|
94 | |
|
|
95 | for (time_watcher **i = tw.begin (); i != tw.end (); ++i) |
|
|
96 | if ((*i)->at < w->at) |
|
|
97 | w = *i; |
|
|
98 | |
|
|
99 | if (w->at > NOW) |
|
|
100 | break; |
|
|
101 | |
|
|
102 | // call it |
|
|
103 | w->call (*w); |
|
|
104 | |
|
|
105 | // re-add it if necessary |
|
|
106 | if (w->at >= 0) |
|
|
107 | reg (w); |
|
|
108 | } |
|
|
109 | |
|
|
110 | struct timeval to; |
202 | struct timeval tval; |
111 | double diff = w->at - NOW; |
203 | |
112 | to.tv_sec = (int)diff; |
204 | #if IOM_IDLE |
|
|
205 | if (iw.size ()) |
|
|
206 | { |
|
|
207 | tval.tv_sec = 0; |
|
|
208 | tval.tv_usec = 0; |
|
|
209 | to = &tval; |
|
|
210 | } |
|
|
211 | else |
|
|
212 | #endif |
|
|
213 | |
|
|
214 | { |
|
|
215 | #if IOM_TIME |
|
|
216 | time_watcher *next; |
|
|
217 | |
|
|
218 | for (;;) |
|
|
219 | { |
|
|
220 | next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
221 | |
|
|
222 | for (int i = tw.size (); i--; ) |
|
|
223 | if (!tw[i]) |
|
|
224 | tw.erase_unordered (i); |
|
|
225 | else if (tw[i]->at < next->at) |
|
|
226 | next = tw[i]; |
|
|
227 | |
|
|
228 | if (next->at > NOW) |
|
|
229 | { |
|
|
230 | if (next != tw[0]) |
|
|
231 | { |
|
|
232 | double diff = next->at - NOW; |
|
|
233 | tval.tv_sec = (int)diff; |
113 | to.tv_usec = (int)((diff - to.tv_sec) * 1000000); |
234 | tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); |
|
|
235 | to = &tval; |
|
|
236 | } |
|
|
237 | break; |
|
|
238 | } |
|
|
239 | else |
|
|
240 | { |
|
|
241 | unreg (next); |
|
|
242 | next->call (*next); |
|
|
243 | } |
|
|
244 | } |
|
|
245 | #endif |
114 | |
246 | |
|
|
247 | } |
|
|
248 | |
|
|
249 | #if IOM_CHECK |
|
|
250 | tw.activity = false; |
|
|
251 | |
|
|
252 | for (int i = cw.size (); i--; ) |
|
|
253 | if (!cw[i]) |
|
|
254 | cw.erase_unordered (i); |
|
|
255 | else |
|
|
256 | cw[i]->call (*cw[i]); |
|
|
257 | |
|
|
258 | if (tw.activity) |
|
|
259 | { |
|
|
260 | tval.tv_sec = 0; |
|
|
261 | tval.tv_usec = 0; |
|
|
262 | to = &tval; |
|
|
263 | } |
|
|
264 | #endif |
|
|
265 | |
|
|
266 | #if IOM_IO |
115 | fd_set rfd, wfd; |
267 | fd_set rfd, wfd; |
116 | |
268 | |
117 | FD_ZERO (&rfd); |
269 | FD_ZERO (&rfd); |
118 | FD_ZERO (&wfd); |
270 | FD_ZERO (&wfd); |
119 | |
271 | |
120 | int fds = 0; |
272 | int fds = 0; |
121 | |
273 | |
122 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
274 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
|
|
275 | if (*i) |
123 | { |
276 | { |
124 | if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); |
277 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
125 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
278 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
126 | |
279 | |
127 | if ((*w)->fd > fds) fds = (*w)->fd; |
280 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
128 | } |
281 | } |
129 | |
282 | |
|
|
283 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
|
|
284 | break; // no events |
|
|
285 | |
130 | fds = select (fds + 1, &rfd, &wfd, 0, &to); |
286 | fds = select (fds, &rfd, &wfd, NULL, to); |
131 | |
287 | # if IOM_TIME |
132 | set_now (); |
288 | set_now (); |
|
|
289 | # endif |
133 | |
290 | |
134 | if (fds > 0) |
291 | if (fds > 0) |
135 | for (io_watcher **w = iow.begin (); w < iow.end (); w++) |
292 | for (int i = iow.size (); i--; ) |
|
|
293 | if (!iow[i]) |
|
|
294 | iow.erase_unordered (i); |
|
|
295 | else |
136 | { |
296 | { |
137 | short revents = (*w)->events; |
297 | short revents = iow[i]->events; |
138 | |
298 | |
139 | if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; |
299 | if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; |
140 | if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; |
300 | if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; |
141 | |
301 | |
142 | if (revents) |
302 | if (revents) |
143 | (*w)->call (**w, revents); |
303 | iow[i]->call (*iow[i], revents); |
144 | } |
304 | } |
145 | } |
305 | else if (fds < 0 && errno != EINTR) |
146 | } |
306 | { |
|
|
307 | perror ("Error while waiting for I/O or time event"); |
|
|
308 | abort (); |
|
|
309 | } |
|
|
310 | #if IOM_IDLE |
|
|
311 | else |
|
|
312 | for (int i = iw.size (); i--; ) |
|
|
313 | if (!iw[i]) |
|
|
314 | iw.erase_unordered (i); |
|
|
315 | else |
|
|
316 | iw[i]->call (*iw[i]); |
|
|
317 | #endif |
147 | |
318 | |
148 | void io_manager::idle_cb (time_watcher &w) |
319 | #elif IOM_TIME |
149 | { |
320 | if (!to) |
150 | w.at = NOW + 1000000000; |
321 | break; |
|
|
322 | |
|
|
323 | select (0, 0, 0, 0, &to); |
|
|
324 | set_now (); |
|
|
325 | #else |
|
|
326 | break; |
|
|
327 | #endif |
|
|
328 | |
|
|
329 | } |
151 | } |
330 | } |
152 | |
331 | |
153 | io_manager::io_manager () |
332 | io_manager::io_manager () |
154 | { |
333 | { |
|
|
334 | iom_valid = true; |
|
|
335 | |
|
|
336 | #if IOM_TIME |
155 | set_now (); |
337 | set_now (); |
156 | |
338 | |
157 | iom_valid = true; |
339 | tw0.start (TSTAMP_MAX); |
158 | |
340 | #endif |
159 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
160 | idle->start (0); |
|
|
161 | } |
341 | } |
162 | |
342 | |
163 | io_manager::~io_manager () |
343 | io_manager::~io_manager () |
164 | { |
344 | { |
165 | iom_valid = false; |
345 | iom_valid = false; |