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) |
45 | { |
69 | { |
|
|
70 | #if IOM_CHECK |
|
|
71 | queue.activity = true; |
|
|
72 | #endif |
46 | queue.push_back (w); |
73 | queue.push_back (w); |
47 | w->active = queue.size (); |
74 | w->active = queue.size (); |
48 | } |
75 | } |
49 | } |
76 | } |
50 | |
77 | |
51 | template<class watcher> |
78 | template<class watcher> |
52 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
79 | void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
53 | { |
80 | { |
54 | if (!iom_valid) |
81 | if (!iom_valid) |
55 | return; |
82 | return; |
56 | |
83 | |
57 | if (w->active) |
84 | if (w->active) |
… | |
… | |
142 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
169 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
143 | to = &tval; |
170 | to = &tval; |
144 | } |
171 | } |
145 | break; |
172 | break; |
146 | } |
173 | } |
147 | else if (next->at >= 0) |
174 | else |
148 | { |
175 | { |
149 | unreg (next); |
176 | unreg (next); |
150 | next->call (*next); |
177 | next->call (*next); |
151 | } |
178 | } |
152 | } |
179 | } |
153 | #endif |
180 | #endif |
154 | } |
181 | } |
155 | |
182 | |
156 | #if IOM_CHECK |
183 | #if IOM_CHECK |
|
|
184 | tw.activity = false; |
|
|
185 | |
157 | for (int i = cw.size (); i--; ) |
186 | for (int i = cw.size (); i--; ) |
158 | if (!cw[i]) |
187 | if (!cw[i]) |
159 | cw.erase_unordered (i); |
188 | cw.erase_unordered (i); |
160 | else |
189 | else |
161 | cw[i]->call (*cw[i]); |
190 | cw[i]->call (*cw[i]); |
|
|
191 | |
|
|
192 | if (tw.activity) |
|
|
193 | { |
|
|
194 | tval.tv_sec = 0; |
|
|
195 | tval.tv_usec = 0; |
|
|
196 | to = &tval; |
|
|
197 | } |
162 | #endif |
198 | #endif |
163 | |
199 | |
164 | #if IOM_IO |
200 | #if IOM_IO |
165 | fd_set rfd, wfd, efd; |
201 | fd_set rfd, wfd, efd; |
166 | |
202 | |
167 | FD_ZERO (&rfd); |
203 | FD_ZERO (&rfd); |
168 | FD_ZERO (&wfd); |
204 | FD_ZERO (&wfd); |
169 | |
205 | |
170 | int fds = 0; |
206 | int fds = 0; |
171 | |
207 | |
172 | for (io_watcher **i = iow.end (); i-- > iow.begin (); ) |
208 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
173 | if (*i) |
209 | if (*i) |
174 | { |
210 | { |
175 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
211 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
176 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
212 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
177 | |
213 | |
… | |
… | |
224 | break; |
260 | break; |
225 | #endif |
261 | #endif |
226 | } |
262 | } |
227 | } |
263 | } |
228 | |
264 | |
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 () |
265 | io_manager::io_manager () |
246 | { |
266 | { |
247 | iom_valid = true; |
267 | iom_valid = true; |
248 | |
268 | |
249 | #if IOM_TIME |
269 | #if IOM_TIME |