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> |
23 | #include <cstdlib> |
21 | #include <cstdlib> |
24 | #include <cerrno> |
22 | #include <cerrno> |
25 | |
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 |
26 | #include <sys/select.h> |
33 | #include <sys/select.h> |
27 | #include <sys/time.h> |
|
|
28 | |
34 | |
29 | #include "iom.h" |
35 | #include "iom.h" |
30 | |
36 | |
31 | // TSTAMP_MAX must still fit into a positive struct timeval |
37 | // TSTAMP_MAX must still fit into a positive struct timeval |
32 | #define TSTAMP_MAX (double)(1UL<<31) |
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; |
33 | |
57 | |
34 | tstamp NOW; |
58 | tstamp NOW; |
35 | static bool iom_valid; |
59 | static bool iom_valid; |
36 | io_manager iom; |
60 | io_manager iom; |
37 | |
61 | |
38 | template<class watcher> |
62 | template<class watcher> |
39 | void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
63 | void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) |
40 | { |
64 | { |
41 | if (!iom_valid) |
65 | if (!iom_valid) |
42 | abort (); |
66 | abort (); |
43 | |
67 | |
44 | if (!w->active) |
68 | if (!w->active) |
… | |
… | |
47 | w->active = queue.size (); |
71 | w->active = queue.size (); |
48 | } |
72 | } |
49 | } |
73 | } |
50 | |
74 | |
51 | template<class watcher> |
75 | template<class watcher> |
52 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
76 | void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
53 | { |
77 | { |
54 | if (!iom_valid) |
78 | if (!iom_valid) |
55 | return; |
79 | return; |
56 | |
80 | |
57 | if (w->active) |
81 | if (w->active) |
… | |
… | |
142 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
166 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
143 | to = &tval; |
167 | to = &tval; |
144 | } |
168 | } |
145 | break; |
169 | break; |
146 | } |
170 | } |
147 | else if (next->at >= 0) |
171 | else |
148 | { |
172 | { |
149 | unreg (next); |
173 | unreg (next); |
150 | next->call (*next); |
174 | next->call (*next); |
151 | } |
175 | } |
152 | } |
176 | } |
… | |
… | |
167 | FD_ZERO (&rfd); |
191 | FD_ZERO (&rfd); |
168 | FD_ZERO (&wfd); |
192 | FD_ZERO (&wfd); |
169 | |
193 | |
170 | int fds = 0; |
194 | int fds = 0; |
171 | |
195 | |
172 | for (io_watcher **i = iow.end (); i-- > iow.begin (); ) |
196 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
173 | if (*i) |
197 | if (*i) |
174 | { |
198 | { |
175 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
199 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
176 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
200 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
177 | |
201 | |
… | |
… | |
224 | break; |
248 | break; |
225 | #endif |
249 | #endif |
226 | } |
250 | } |
227 | } |
251 | } |
228 | |
252 | |
229 | // this is a dummy time watcher to ensure that the first |
|
|
230 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
231 | // of null-pointer-checks |
|
|
232 | static struct tw0 : time_watcher { |
|
|
233 | void cb (time_watcher &w) |
|
|
234 | { |
|
|
235 | // should never get called |
|
|
236 | // reached end-of-time, or tstamp has a bogus definition :) |
|
|
237 | abort (); |
|
|
238 | } |
|
|
239 | |
|
|
240 | tw0() |
|
|
241 | : time_watcher (this, &tw0::cb) |
|
|
242 | { } |
|
|
243 | } tw0; |
|
|
244 | |
|
|
245 | io_manager::io_manager () |
253 | io_manager::io_manager () |
246 | { |
254 | { |
247 | iom_valid = true; |
255 | iom_valid = true; |
248 | |
256 | |
249 | #if IOM_TIME |
257 | #if IOM_TIME |