… | |
… | |
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 | #include <assert.h> |
|
|
27 | |
|
|
28 | #if 1 // older unices need these includes for select (2) |
|
|
29 | # include <unistd.h> |
|
|
30 | # include <sys/types.h> |
|
|
31 | #endif |
|
|
32 | |
|
|
33 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
|
|
34 | // until that happens, sys/select.h must come last |
24 | #include <sys/select.h> |
35 | #include <sys/select.h> |
|
|
36 | |
|
|
37 | // for IOM_SIG |
25 | #include <sys/time.h> |
38 | #include <signal.h> |
26 | |
39 | |
27 | #include "iom.h" |
40 | #include "iom.h" |
28 | |
41 | |
29 | // TSTAMP_MAX must still fit into a positive struct timeval |
42 | // TSTAMP_MAX must still fit into a positive struct timeval |
30 | #define TSTAMP_MAX (double)(1UL<<31) |
43 | #define TSTAMP_MAX (double)(1UL<<31) |
|
|
44 | |
|
|
45 | // this is a dummy time watcher to ensure that the first |
|
|
46 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
47 | // of null-pointer-checks |
|
|
48 | // (must come _before_ iom is being defined) |
|
|
49 | static struct tw0 : time_watcher |
|
|
50 | { |
|
|
51 | void cb (time_watcher &w) |
|
|
52 | { |
|
|
53 | // should never get called |
|
|
54 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
55 | // or compiler initilization order broken, or somethine else :) |
|
|
56 | abort (); |
|
|
57 | } |
|
|
58 | |
|
|
59 | tw0 () |
|
|
60 | : time_watcher (this, &tw0::cb) |
|
|
61 | { } |
|
|
62 | } tw0; |
31 | |
63 | |
32 | tstamp NOW; |
64 | tstamp NOW; |
33 | static bool iom_valid; |
65 | static bool iom_valid; |
34 | io_manager iom; |
66 | io_manager iom; |
35 | |
67 | |
… | |
… | |
39 | if (!iom_valid) |
71 | if (!iom_valid) |
40 | abort (); |
72 | abort (); |
41 | |
73 | |
42 | if (!w->active) |
74 | if (!w->active) |
43 | { |
75 | { |
|
|
76 | #if IOM_CHECK |
|
|
77 | queue.activity = true; |
|
|
78 | #endif |
44 | queue.push_back (w); |
79 | queue.push_back (w); |
45 | w->active = queue.size (); |
80 | w->active = queue.size (); |
46 | } |
81 | } |
47 | } |
82 | } |
48 | |
83 | |
… | |
… | |
70 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
105 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
71 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
106 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
72 | #endif |
107 | #endif |
73 | |
108 | |
74 | #if IOM_IO |
109 | #if IOM_IO |
75 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
110 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
76 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
111 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
77 | #endif |
112 | #endif |
78 | |
113 | |
79 | #if IOM_CHECK |
114 | #if IOM_CHECK |
80 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
115 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
… | |
… | |
92 | struct timeval tv; |
127 | struct timeval tv; |
93 | |
128 | |
94 | gettimeofday (&tv, 0); |
129 | gettimeofday (&tv, 0); |
95 | |
130 | |
96 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
131 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
97 | #endif |
|
|
98 | } |
132 | } |
|
|
133 | #endif |
99 | |
134 | |
|
|
135 | #if IOM_SIG |
|
|
136 | // race conditions galore |
|
|
137 | |
|
|
138 | void io_manager::sighandler (int signum) |
|
|
139 | { |
|
|
140 | assert (0 < signum && signum <= iom.sw.size ()); |
|
|
141 | |
|
|
142 | sig_vec &sv = *iom.sw [signum - 1]; |
|
|
143 | |
|
|
144 | for (int i = sv.size (); i--; ) |
|
|
145 | if (!sv[i]) |
|
|
146 | sv.erase_unordered (i); |
|
|
147 | else |
|
|
148 | sv[i]->call (*sv[i]); |
|
|
149 | } |
|
|
150 | |
|
|
151 | void io_manager::reg (sig_watcher *w) |
|
|
152 | { |
|
|
153 | assert (0 < w->signum); |
|
|
154 | |
|
|
155 | sw.reserve (w->signum); |
|
|
156 | |
|
|
157 | sig_vec *&sv = sw [w->signum - 1]; |
|
|
158 | |
|
|
159 | if (!sv) |
|
|
160 | { |
|
|
161 | sv = new sig_vec; |
|
|
162 | |
|
|
163 | struct sigaction sa; |
|
|
164 | sa.sa_handler = io_manager::sighandler; |
|
|
165 | sigfillset (&sa.sa_mask); |
|
|
166 | sa.sa_flags = 0; |
|
|
167 | |
|
|
168 | if (sigaction (w->signum, &sa, 0)) |
|
|
169 | { |
|
|
170 | perror ("Error while installing signal handler"); |
|
|
171 | abort (); |
|
|
172 | } |
|
|
173 | } |
|
|
174 | |
|
|
175 | reg (w, *sv); |
|
|
176 | } |
|
|
177 | |
|
|
178 | void io_manager::unreg (sig_watcher *w) |
|
|
179 | { |
|
|
180 | assert (0 < w->signum && w->signum <= sw.size ()); |
|
|
181 | |
|
|
182 | unreg (w, *sw [w->signum - 1]); |
|
|
183 | } |
|
|
184 | |
|
|
185 | void sig_watcher::start (int signum) |
|
|
186 | { |
|
|
187 | stop (); |
|
|
188 | this->signum = signum; |
|
|
189 | iom.reg (this); |
|
|
190 | } |
|
|
191 | #endif |
|
|
192 | |
100 | void io_manager::loop () |
193 | void io_manager::loop () |
101 | { |
194 | { |
102 | #if IOM_TIME |
195 | #if IOM_TIME |
103 | set_now (); |
196 | set_now (); |
104 | #endif |
197 | #endif |
… | |
… | |
115 | tval.tv_usec = 0; |
208 | tval.tv_usec = 0; |
116 | to = &tval; |
209 | to = &tval; |
117 | } |
210 | } |
118 | else |
211 | else |
119 | #endif |
212 | #endif |
|
|
213 | |
120 | { |
214 | { |
121 | #if IOM_TIME |
215 | #if IOM_TIME |
122 | time_watcher *next; |
216 | time_watcher *next; |
123 | |
217 | |
124 | for (;;) |
218 | for (;;) |
… | |
… | |
135 | { |
229 | { |
136 | if (next != tw[0]) |
230 | if (next != tw[0]) |
137 | { |
231 | { |
138 | double diff = next->at - NOW; |
232 | double diff = next->at - NOW; |
139 | tval.tv_sec = (int)diff; |
233 | tval.tv_sec = (int)diff; |
140 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
234 | tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); |
141 | to = &tval; |
235 | to = &tval; |
142 | } |
236 | } |
143 | break; |
237 | break; |
144 | } |
238 | } |
145 | else |
239 | else |
… | |
… | |
147 | unreg (next); |
241 | unreg (next); |
148 | next->call (*next); |
242 | next->call (*next); |
149 | } |
243 | } |
150 | } |
244 | } |
151 | #endif |
245 | #endif |
|
|
246 | |
152 | } |
247 | } |
153 | |
248 | |
154 | #if IOM_CHECK |
249 | #if IOM_CHECK |
|
|
250 | tw.activity = false; |
|
|
251 | |
155 | for (int i = cw.size (); i--; ) |
252 | for (int i = cw.size (); i--; ) |
156 | if (!cw[i]) |
253 | if (!cw[i]) |
157 | cw.erase_unordered (i); |
254 | cw.erase_unordered (i); |
158 | else |
255 | else |
159 | cw[i]->call (*cw[i]); |
256 | cw[i]->call (*cw[i]); |
|
|
257 | |
|
|
258 | if (tw.activity) |
|
|
259 | { |
|
|
260 | tval.tv_sec = 0; |
|
|
261 | tval.tv_usec = 0; |
|
|
262 | to = &tval; |
|
|
263 | } |
160 | #endif |
264 | #endif |
161 | |
265 | |
162 | #if IOM_IO |
266 | #if IOM_IO |
163 | fd_set rfd, wfd, efd; |
267 | fd_set rfd, wfd; |
164 | |
268 | |
165 | FD_ZERO (&rfd); |
269 | FD_ZERO (&rfd); |
166 | FD_ZERO (&wfd); |
270 | FD_ZERO (&wfd); |
167 | |
271 | |
168 | int fds = 0; |
272 | int fds = 0; |
… | |
… | |
177 | } |
281 | } |
178 | |
282 | |
179 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
283 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
180 | break; // no events |
284 | break; // no events |
181 | |
285 | |
182 | fds = select (fds, &rfd, &wfd, &efd, to); |
286 | fds = select (fds, &rfd, &wfd, NULL, to); |
183 | # if IOM_TIME |
287 | # if IOM_TIME |
184 | set_now (); |
288 | set_now (); |
185 | # endif |
289 | # endif |
186 | |
290 | |
187 | if (fds > 0) |
291 | if (fds > 0) |
… | |
… | |
219 | select (0, 0, 0, 0, &to); |
323 | select (0, 0, 0, 0, &to); |
220 | set_now (); |
324 | set_now (); |
221 | #else |
325 | #else |
222 | break; |
326 | break; |
223 | #endif |
327 | #endif |
224 | } |
|
|
225 | } |
|
|
226 | |
328 | |
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 | { } |
329 | } |
241 | } tw0; |
330 | } |
242 | |
331 | |
243 | io_manager::io_manager () |
332 | io_manager::io_manager () |
244 | { |
333 | { |
245 | iom_valid = true; |
334 | iom_valid = true; |
246 | |
335 | |