… | |
… | |
19 | |
19 | |
20 | #include <cstdio> |
20 | #include <cstdio> |
21 | #include <cstdlib> |
21 | #include <cstdlib> |
22 | #include <cerrno> |
22 | #include <cerrno> |
23 | |
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 |
24 | #include <sys/select.h> |
33 | #include <sys/select.h> |
25 | #include <sys/time.h> |
|
|
26 | |
34 | |
27 | #include "iom.h" |
35 | #include "iom.h" |
28 | |
36 | |
29 | // TSTAMP_MAX must still fit into a positive struct timeval |
37 | // TSTAMP_MAX must still fit into a positive struct timeval |
30 | #define TSTAMP_MAX (double)(1UL<<31) |
38 | #define TSTAMP_MAX (double)(1UL<<31) |
31 | |
39 | |
32 | // this is a dummy time watcher to ensure that the first |
40 | // this is a dummy time watcher to ensure that the first |
33 | // time watcher is _always_ valid, this gets rid of a lot |
41 | // time watcher is _always_ valid, this gets rid of a lot |
34 | // of null-pointer-checks |
42 | // of null-pointer-checks |
35 | // (must come _before_ iom is being defined) |
43 | // (must come _before_ iom is being defined) |
36 | static struct tw0 : time_watcher { |
44 | static struct tw0 : time_watcher |
37 | void cb (time_watcher &w) |
|
|
38 | { |
45 | { |
|
|
46 | void cb (time_watcher &w) |
|
|
47 | { |
39 | // should never get called |
48 | // should never get called |
40 | // reached end-of-time, or tstamp has a bogus definition, |
49 | // reached end-of-time, or tstamp has a bogus definition, |
41 | // or compiler initilization order broken, or somethine else :) |
50 | // or compiler initilization order broken, or somethine else :) |
42 | abort (); |
51 | abort (); |
43 | } |
52 | } |
44 | |
53 | |
45 | tw0() |
54 | tw0() |
46 | : time_watcher (this, &tw0::cb) |
55 | : time_watcher (this, &tw0::cb) |
47 | { } |
56 | { }} |
48 | } tw0; |
57 | tw0; |
49 | |
58 | |
50 | tstamp NOW; |
59 | tstamp NOW; |
51 | static bool iom_valid; |
60 | static bool iom_valid; |
52 | io_manager iom; |
61 | io_manager iom; |
53 | |
62 | |
… | |
… | |
57 | if (!iom_valid) |
66 | if (!iom_valid) |
58 | abort (); |
67 | abort (); |
59 | |
68 | |
60 | if (!w->active) |
69 | if (!w->active) |
61 | { |
70 | { |
|
|
71 | #if IOM_CHECK |
|
|
72 | queue.activity = true; |
|
|
73 | #endif |
62 | queue.push_back (w); |
74 | queue.push_back (w); |
63 | w->active = queue.size (); |
75 | w->active = queue.size (); |
64 | } |
76 | } |
65 | } |
77 | } |
66 | |
78 | |
… | |
… | |
88 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
100 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
89 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
101 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
90 | #endif |
102 | #endif |
91 | |
103 | |
92 | #if IOM_IO |
104 | #if IOM_IO |
93 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
105 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
94 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
106 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
95 | #endif |
107 | #endif |
96 | |
108 | |
97 | #if IOM_CHECK |
109 | #if IOM_CHECK |
98 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
110 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
… | |
… | |
133 | tval.tv_usec = 0; |
145 | tval.tv_usec = 0; |
134 | to = &tval; |
146 | to = &tval; |
135 | } |
147 | } |
136 | else |
148 | else |
137 | #endif |
149 | #endif |
|
|
150 | |
138 | { |
151 | { |
139 | #if IOM_TIME |
152 | #if IOM_TIME |
140 | time_watcher *next; |
153 | time_watcher *next; |
141 | |
154 | |
142 | for (;;) |
155 | for (;;) |
… | |
… | |
165 | unreg (next); |
178 | unreg (next); |
166 | next->call (*next); |
179 | next->call (*next); |
167 | } |
180 | } |
168 | } |
181 | } |
169 | #endif |
182 | #endif |
|
|
183 | |
170 | } |
184 | } |
171 | |
185 | |
172 | #if IOM_CHECK |
186 | #if IOM_CHECK |
|
|
187 | tw.activity = false; |
|
|
188 | |
173 | for (int i = cw.size (); i--; ) |
189 | for (int i = cw.size (); i--; ) |
174 | if (!cw[i]) |
190 | if (!cw[i]) |
175 | cw.erase_unordered (i); |
191 | cw.erase_unordered (i); |
176 | else |
192 | else |
177 | 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 | } |
178 | #endif |
201 | #endif |
179 | |
202 | |
180 | #if IOM_IO |
203 | #if IOM_IO |
181 | fd_set rfd, wfd, efd; |
204 | fd_set rfd, wfd, efd; |
182 | |
205 | |
… | |
… | |
237 | select (0, 0, 0, 0, &to); |
260 | select (0, 0, 0, 0, &to); |
238 | set_now (); |
261 | set_now (); |
239 | #else |
262 | #else |
240 | break; |
263 | break; |
241 | #endif |
264 | #endif |
|
|
265 | |
242 | } |
266 | } |
243 | } |
267 | } |
244 | |
268 | |
245 | io_manager::io_manager () |
269 | io_manager::io_manager () |
246 | { |
270 | { |
… | |
… | |
248 | |
272 | |
249 | #if IOM_TIME |
273 | #if IOM_TIME |
250 | set_now (); |
274 | set_now (); |
251 | |
275 | |
252 | tw0.start (TSTAMP_MAX); |
276 | tw0.start (TSTAMP_MAX); |
253 | printf ("abort, %f but inly on %f\n", NOW, tw0.at); |
|
|
254 | #endif |
277 | #endif |
255 | } |
278 | } |
256 | |
279 | |
257 | io_manager::~io_manager () |
280 | io_manager::~io_manager () |
258 | { |
281 | { |