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 | |
26 | #include <sys/select.h> |
24 | #include <sys/select.h> |
… | |
… | |
29 | #include "iom.h" |
27 | #include "iom.h" |
30 | |
28 | |
31 | // TSTAMP_MAX must still fit into a positive struct timeval |
29 | // TSTAMP_MAX must still fit into a positive struct timeval |
32 | #define TSTAMP_MAX (double)(1UL<<31) |
30 | #define TSTAMP_MAX (double)(1UL<<31) |
33 | |
31 | |
|
|
32 | // this is a dummy time watcher to ensure that the first |
|
|
33 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
34 | // of null-pointer-checks |
|
|
35 | // (must come _before_ iom is being defined) |
|
|
36 | static struct tw0 : time_watcher { |
|
|
37 | void cb (time_watcher &w) |
|
|
38 | { |
|
|
39 | // should never get called |
|
|
40 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
41 | // or compiler initilization order broken, or somethine else :) |
|
|
42 | abort (); |
|
|
43 | } |
|
|
44 | |
|
|
45 | tw0() |
|
|
46 | : time_watcher (this, &tw0::cb) |
|
|
47 | { } |
|
|
48 | } tw0; |
|
|
49 | |
34 | tstamp NOW; |
50 | tstamp NOW; |
35 | static bool iom_valid; |
51 | static bool iom_valid; |
36 | io_manager iom; |
52 | io_manager iom; |
37 | |
53 | |
38 | template<class watcher> |
54 | template<class watcher> |
39 | void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
55 | void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) |
40 | { |
56 | { |
41 | if (!iom_valid) |
57 | if (!iom_valid) |
42 | abort (); |
58 | abort (); |
43 | |
59 | |
44 | if (!w->active) |
60 | if (!w->active) |
… | |
… | |
47 | w->active = queue.size (); |
63 | w->active = queue.size (); |
48 | } |
64 | } |
49 | } |
65 | } |
50 | |
66 | |
51 | template<class watcher> |
67 | template<class watcher> |
52 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
68 | void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
53 | { |
69 | { |
54 | if (!iom_valid) |
70 | if (!iom_valid) |
55 | return; |
71 | return; |
56 | |
72 | |
57 | if (w->active) |
73 | if (w->active) |
… | |
… | |
142 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
158 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
143 | to = &tval; |
159 | to = &tval; |
144 | } |
160 | } |
145 | break; |
161 | break; |
146 | } |
162 | } |
147 | else if (next->at >= 0) |
163 | else |
148 | { |
164 | { |
149 | unreg (next); |
165 | unreg (next); |
150 | next->call (*next); |
166 | next->call (*next); |
151 | } |
167 | } |
152 | } |
168 | } |
… | |
… | |
167 | FD_ZERO (&rfd); |
183 | FD_ZERO (&rfd); |
168 | FD_ZERO (&wfd); |
184 | FD_ZERO (&wfd); |
169 | |
185 | |
170 | int fds = 0; |
186 | int fds = 0; |
171 | |
187 | |
172 | for (io_watcher **i = iow.end (); i-- > iow.begin (); ) |
188 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
173 | if (*i) |
189 | if (*i) |
174 | { |
190 | { |
175 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
191 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
176 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
192 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
177 | |
193 | |
… | |
… | |
224 | break; |
240 | break; |
225 | #endif |
241 | #endif |
226 | } |
242 | } |
227 | } |
243 | } |
228 | |
244 | |
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 () |
245 | io_manager::io_manager () |
246 | { |
246 | { |
247 | iom_valid = true; |
247 | iom_valid = true; |
248 | |
248 | |
249 | #if IOM_TIME |
249 | #if IOM_TIME |
250 | set_now (); |
250 | set_now (); |
251 | |
251 | |
252 | tw0.start (TSTAMP_MAX); |
252 | tw0.start (TSTAMP_MAX); |
|
|
253 | printf ("abort, %f but inly on %f\n", NOW, tw0.at); |
253 | #endif |
254 | #endif |
254 | } |
255 | } |
255 | |
256 | |
256 | io_manager::~io_manager () |
257 | io_manager::~io_manager () |
257 | { |
258 | { |