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 | { |
|
|
46 | void cb (time_watcher &w) |
|
|
47 | { |
|
|
48 | // should never get called |
|
|
49 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
50 | // or compiler initilization order broken, or somethine else :) |
|
|
51 | abort (); |
|
|
52 | } |
|
|
53 | |
|
|
54 | tw0 () |
|
|
55 | : time_watcher (this, &tw0::cb) |
|
|
56 | { }} |
|
|
57 | tw0; |
33 | |
58 | |
34 | tstamp NOW; |
59 | tstamp NOW; |
35 | static bool iom_valid; |
60 | static bool iom_valid; |
36 | io_manager iom; |
61 | io_manager iom; |
37 | |
62 | |
38 | template<class watcher> |
63 | template<class watcher> |
39 | void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
64 | void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) |
40 | { |
65 | { |
41 | if (!iom_valid) |
66 | if (!iom_valid) |
42 | abort (); |
67 | abort (); |
43 | |
68 | |
44 | if (!w->active) |
69 | if (!w->active) |
45 | { |
70 | { |
|
|
71 | #if IOM_CHECK |
|
|
72 | queue.activity = true; |
|
|
73 | #endif |
46 | queue.push_back (w); |
74 | queue.push_back (w); |
47 | w->active = queue.size (); |
75 | w->active = queue.size (); |
48 | } |
76 | } |
49 | } |
77 | } |
50 | |
78 | |
51 | template<class watcher> |
79 | template<class watcher> |
52 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
80 | void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
53 | { |
81 | { |
54 | if (!iom_valid) |
82 | if (!iom_valid) |
55 | return; |
83 | return; |
56 | |
84 | |
57 | if (w->active) |
85 | if (w->active) |
… | |
… | |
72 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
100 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
73 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
101 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
74 | #endif |
102 | #endif |
75 | |
103 | |
76 | #if IOM_IO |
104 | #if IOM_IO |
77 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
105 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
78 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
106 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
79 | #endif |
107 | #endif |
80 | |
108 | |
81 | #if IOM_CHECK |
109 | #if IOM_CHECK |
82 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
110 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
… | |
… | |
117 | tval.tv_usec = 0; |
145 | tval.tv_usec = 0; |
118 | to = &tval; |
146 | to = &tval; |
119 | } |
147 | } |
120 | else |
148 | else |
121 | #endif |
149 | #endif |
|
|
150 | |
122 | { |
151 | { |
123 | #if IOM_TIME |
152 | #if IOM_TIME |
124 | time_watcher *next; |
153 | time_watcher *next; |
125 | |
154 | |
126 | for (;;) |
155 | for (;;) |
… | |
… | |
137 | { |
166 | { |
138 | if (next != tw[0]) |
167 | if (next != tw[0]) |
139 | { |
168 | { |
140 | double diff = next->at - NOW; |
169 | double diff = next->at - NOW; |
141 | tval.tv_sec = (int)diff; |
170 | tval.tv_sec = (int)diff; |
142 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
171 | tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); |
143 | to = &tval; |
172 | to = &tval; |
144 | } |
173 | } |
145 | break; |
174 | break; |
146 | } |
175 | } |
147 | else if (next->at >= 0) |
176 | else |
148 | { |
177 | { |
149 | unreg (next); |
178 | unreg (next); |
150 | next->call (*next); |
179 | next->call (*next); |
151 | } |
180 | } |
152 | } |
181 | } |
153 | #endif |
182 | #endif |
|
|
183 | |
154 | } |
184 | } |
155 | |
185 | |
156 | #if IOM_CHECK |
186 | #if IOM_CHECK |
|
|
187 | tw.activity = false; |
|
|
188 | |
157 | for (int i = cw.size (); i--; ) |
189 | for (int i = cw.size (); i--; ) |
158 | if (!cw[i]) |
190 | if (!cw[i]) |
159 | cw.erase_unordered (i); |
191 | cw.erase_unordered (i); |
160 | else |
192 | else |
161 | cw[i]->call (*cw[i]); |
193 | cw[i]->call (*cw[i]); |
|
|
194 | |
|
|
195 | if (tw.activity) |
|
|
196 | { |
|
|
197 | tval.tv_sec = 0; |
|
|
198 | tval.tv_usec = 0; |
|
|
199 | to = &tval; |
|
|
200 | } |
162 | #endif |
201 | #endif |
163 | |
202 | |
164 | #if IOM_IO |
203 | #if IOM_IO |
165 | fd_set rfd, wfd, efd; |
204 | fd_set rfd, wfd, efd; |
166 | |
205 | |
167 | FD_ZERO (&rfd); |
206 | FD_ZERO (&rfd); |
168 | FD_ZERO (&wfd); |
207 | FD_ZERO (&wfd); |
169 | |
208 | |
170 | int fds = 0; |
209 | int fds = 0; |
171 | |
210 | |
172 | for (io_watcher **i = iow.end (); i-- > iow.begin (); ) |
211 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
173 | if (*i) |
212 | if (*i) |
174 | { |
213 | { |
175 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
214 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
176 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
215 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
177 | |
216 | |
… | |
… | |
221 | select (0, 0, 0, 0, &to); |
260 | select (0, 0, 0, 0, &to); |
222 | set_now (); |
261 | set_now (); |
223 | #else |
262 | #else |
224 | break; |
263 | break; |
225 | #endif |
264 | #endif |
226 | } |
|
|
227 | } |
|
|
228 | |
265 | |
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 | { } |
266 | } |
243 | } tw0; |
267 | } |
244 | |
268 | |
245 | io_manager::io_manager () |
269 | io_manager::io_manager () |
246 | { |
270 | { |
247 | iom_valid = true; |
271 | iom_valid = true; |
248 | |
272 | |