… | |
… | |
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) |
|
|
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; |
31 | |
58 | |
32 | tstamp NOW; |
59 | tstamp NOW; |
33 | static bool iom_valid; |
60 | static bool iom_valid; |
34 | io_manager iom; |
61 | io_manager iom; |
35 | |
62 | |
… | |
… | |
39 | if (!iom_valid) |
66 | if (!iom_valid) |
40 | abort (); |
67 | abort (); |
41 | |
68 | |
42 | if (!w->active) |
69 | if (!w->active) |
43 | { |
70 | { |
|
|
71 | #if IOM_CHECK |
|
|
72 | queue.activity = true; |
|
|
73 | #endif |
44 | queue.push_back (w); |
74 | queue.push_back (w); |
45 | w->active = queue.size (); |
75 | w->active = queue.size (); |
46 | } |
76 | } |
47 | } |
77 | } |
48 | |
78 | |
… | |
… | |
70 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
100 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
71 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
101 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
72 | #endif |
102 | #endif |
73 | |
103 | |
74 | #if IOM_IO |
104 | #if IOM_IO |
75 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
105 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
76 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
106 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
77 | #endif |
107 | #endif |
78 | |
108 | |
79 | #if IOM_CHECK |
109 | #if IOM_CHECK |
80 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
110 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
… | |
… | |
115 | tval.tv_usec = 0; |
145 | tval.tv_usec = 0; |
116 | to = &tval; |
146 | to = &tval; |
117 | } |
147 | } |
118 | else |
148 | else |
119 | #endif |
149 | #endif |
|
|
150 | |
120 | { |
151 | { |
121 | #if IOM_TIME |
152 | #if IOM_TIME |
122 | time_watcher *next; |
153 | time_watcher *next; |
123 | |
154 | |
124 | for (;;) |
155 | for (;;) |
… | |
… | |
135 | { |
166 | { |
136 | if (next != tw[0]) |
167 | if (next != tw[0]) |
137 | { |
168 | { |
138 | double diff = next->at - NOW; |
169 | double diff = next->at - NOW; |
139 | tval.tv_sec = (int)diff; |
170 | tval.tv_sec = (int)diff; |
140 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
171 | tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); |
141 | to = &tval; |
172 | to = &tval; |
142 | } |
173 | } |
143 | break; |
174 | break; |
144 | } |
175 | } |
145 | else |
176 | else |
… | |
… | |
147 | unreg (next); |
178 | unreg (next); |
148 | next->call (*next); |
179 | next->call (*next); |
149 | } |
180 | } |
150 | } |
181 | } |
151 | #endif |
182 | #endif |
|
|
183 | |
152 | } |
184 | } |
153 | |
185 | |
154 | #if IOM_CHECK |
186 | #if IOM_CHECK |
|
|
187 | tw.activity = false; |
|
|
188 | |
155 | for (int i = cw.size (); i--; ) |
189 | for (int i = cw.size (); i--; ) |
156 | if (!cw[i]) |
190 | if (!cw[i]) |
157 | cw.erase_unordered (i); |
191 | cw.erase_unordered (i); |
158 | else |
192 | else |
159 | 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 | } |
160 | #endif |
201 | #endif |
161 | |
202 | |
162 | #if IOM_IO |
203 | #if IOM_IO |
163 | fd_set rfd, wfd, efd; |
204 | fd_set rfd, wfd; |
164 | |
205 | |
165 | FD_ZERO (&rfd); |
206 | FD_ZERO (&rfd); |
166 | FD_ZERO (&wfd); |
207 | FD_ZERO (&wfd); |
167 | |
208 | |
168 | int fds = 0; |
209 | int fds = 0; |
… | |
… | |
177 | } |
218 | } |
178 | |
219 | |
179 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
220 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
180 | break; // no events |
221 | break; // no events |
181 | |
222 | |
182 | fds = select (fds, &rfd, &wfd, &efd, to); |
223 | fds = select (fds, &rfd, &wfd, NULL, to); |
183 | # if IOM_TIME |
224 | # if IOM_TIME |
184 | set_now (); |
225 | set_now (); |
185 | # endif |
226 | # endif |
186 | |
227 | |
187 | if (fds > 0) |
228 | if (fds > 0) |
… | |
… | |
219 | select (0, 0, 0, 0, &to); |
260 | select (0, 0, 0, 0, &to); |
220 | set_now (); |
261 | set_now (); |
221 | #else |
262 | #else |
222 | break; |
263 | break; |
223 | #endif |
264 | #endif |
224 | } |
|
|
225 | } |
|
|
226 | |
265 | |
227 | // this is a dummy time watcher to ensure that the first |
|
|
228 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
229 | // of null-pointer-checks |
|
|
230 | static struct tw0 : time_watcher { |
|
|
231 | void cb (time_watcher &w) |
|
|
232 | { |
|
|
233 | // should never get called |
|
|
234 | // reached end-of-time, or tstamp has a bogus definition :) |
|
|
235 | abort (); |
|
|
236 | } |
|
|
237 | |
|
|
238 | tw0() |
|
|
239 | : time_watcher (this, &tw0::cb) |
|
|
240 | { } |
266 | } |
241 | } tw0; |
267 | } |
242 | |
268 | |
243 | io_manager::io_manager () |
269 | io_manager::io_manager () |
244 | { |
270 | { |
245 | iom_valid = true; |
271 | iom_valid = true; |
246 | |
272 | |