1 | /* |
1 | /* |
2 | iom.C -- I/O multiplexor |
2 | iom.C -- generic I/O multiplexer |
|
|
3 | Copyright (C) 2003, 2004 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
10 | (at your option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
20 | */ |
18 | |
21 | |
19 | #include "config.h" |
22 | #include "iom.h" |
20 | |
23 | |
|
|
24 | #include <cstdio> |
|
|
25 | #include <cstdlib> |
|
|
26 | #include <cerrno> |
|
|
27 | #include <cassert> |
|
|
28 | |
|
|
29 | #include <sys/types.h> |
21 | #include <sys/time.h> |
30 | #include <sys/time.h> |
22 | |
31 | |
23 | #include <algorithm> |
32 | #if 1 // older unices need these includes for select (2) |
|
|
33 | # include <unistd.h> |
|
|
34 | # include <time.h> |
|
|
35 | #endif |
|
|
36 | |
|
|
37 | #if IOM_CHILD |
|
|
38 | # include <sys/wait.h> |
|
|
39 | #endif |
|
|
40 | |
|
|
41 | #if IOM_SIG |
24 | #include <functional> |
42 | # include <csignal> |
|
|
43 | # include <fcntl.h> |
|
|
44 | #endif |
25 | |
45 | |
26 | #include "slog.h" |
46 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
27 | #include "iom.h" |
47 | // until that happens, sys/select.h must come last |
|
|
48 | #include <sys/select.h> |
28 | |
49 | |
29 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
50 | #define TIMEVAL timeval |
|
|
51 | #define TV_FRAC tv_usec |
|
|
52 | #define TV_MULT 1000000L |
|
|
53 | |
|
|
54 | #if IOM_IO |
|
|
55 | static io_manager_vec<io_watcher> iow; |
|
|
56 | #endif |
|
|
57 | #if IOM_CHECK |
|
|
58 | static io_manager_vec<check_watcher> cw; |
|
|
59 | #endif |
|
|
60 | #if IOM_TIME |
|
|
61 | static io_manager_vec<time_watcher> tw; |
|
|
62 | #endif |
|
|
63 | #if IOM_IDLE |
|
|
64 | static io_manager_vec<idle_watcher> iw; |
|
|
65 | #endif |
|
|
66 | #if IOM_SIG |
|
|
67 | static int sigpipe[2]; // signal signalling pipe |
|
|
68 | static sigset_t sigs; |
|
|
69 | struct sig_vec : io_manager_vec<sig_watcher> { |
|
|
70 | int pending; |
|
|
71 | sig_vec () |
|
|
72 | : pending (false) |
|
|
73 | { } |
|
|
74 | }; |
|
|
75 | static vector<sig_vec *> sw; |
|
|
76 | #endif |
|
|
77 | #if IOM_CHILD |
|
|
78 | static io_manager_vec<child_watcher> pw; |
|
|
79 | #endif |
|
|
80 | |
|
|
81 | // this is a dummy time watcher to ensure that the first |
|
|
82 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
83 | // of null-pointer-checks |
|
|
84 | // (must come _before_ iom is being defined) |
|
|
85 | static struct tw0 : time_watcher |
30 | { |
86 | { |
31 | return a->at > b->at; |
87 | void cb (time_watcher &w) |
32 | } |
88 | { |
|
|
89 | // should never get called |
|
|
90 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
91 | // or compiler initialisation order broken, or something else :) |
|
|
92 | abort (); |
|
|
93 | } |
|
|
94 | |
|
|
95 | tw0 () |
|
|
96 | : time_watcher (this, &tw0::cb) |
|
|
97 | { } |
|
|
98 | } tw0; |
33 | |
99 | |
34 | tstamp NOW; |
100 | tstamp NOW; |
35 | |
101 | |
36 | io_manager iom; |
102 | #if IOM_CHILD |
37 | |
103 | // sig_watcher for child signal(s) |
38 | void time_watcher::set (tstamp when) |
104 | static struct sw0 : sig_watcher |
39 | { |
105 | { |
40 | iom.unreg (this); |
106 | void cb (sig_watcher &w) |
41 | at = when; |
107 | { |
42 | iom.reg (this); |
108 | // SIGCHLD, call corresponding watchera |
43 | } |
109 | pid_t pid; |
|
|
110 | int status; |
44 | |
111 | |
|
|
112 | while ((pid = waitpid (-1, &status, WNOHANG)) > 0) |
|
|
113 | for (int i = pw.size (); i--; ) |
|
|
114 | { |
|
|
115 | child_watcher *w = pw[i]; |
|
|
116 | |
|
|
117 | if (!w) |
|
|
118 | pw.erase_unordered (i); |
|
|
119 | else if (w->pid == pid) |
|
|
120 | { |
|
|
121 | w->stop (); |
|
|
122 | w->call (*w, status); |
|
|
123 | } |
|
|
124 | } |
|
|
125 | |
|
|
126 | } |
|
|
127 | |
|
|
128 | sw0 () |
|
|
129 | : sig_watcher (this, &sw0::cb) |
|
|
130 | { } |
|
|
131 | } sw0; |
|
|
132 | #endif |
|
|
133 | |
|
|
134 | #if IOM_TIME |
|
|
135 | tstamp io_manager::now () |
|
|
136 | { |
|
|
137 | struct timeval tv; |
|
|
138 | |
|
|
139 | gettimeofday (&tv, 0); |
|
|
140 | return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
|
|
141 | } |
|
|
142 | |
|
|
143 | void io_manager::set_now () |
|
|
144 | { |
|
|
145 | NOW = now (); |
|
|
146 | } |
|
|
147 | #endif |
|
|
148 | |
|
|
149 | static bool iom_valid; |
|
|
150 | |
|
|
151 | // used for initialisation only |
|
|
152 | static struct init { |
|
|
153 | init () |
|
|
154 | { |
|
|
155 | iom_valid = true; |
|
|
156 | |
|
|
157 | #if IOM_SIG |
|
|
158 | sigemptyset (&sigs); |
|
|
159 | |
|
|
160 | if (pipe (sigpipe)) |
|
|
161 | { |
|
|
162 | perror ("io_manager: unable to create signal pipe, aborting."); |
|
|
163 | abort (); |
|
|
164 | } |
|
|
165 | |
|
|
166 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); |
|
|
167 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); |
|
|
168 | #endif |
|
|
169 | |
|
|
170 | #if IOM_CHILD |
|
|
171 | sw0.start (SIGCHLD); |
|
|
172 | #endif |
|
|
173 | |
|
|
174 | #if IOM_TIME |
|
|
175 | io_manager::set_now (); |
|
|
176 | |
|
|
177 | tw0.start (TSTAMP_MAX); |
|
|
178 | #endif |
|
|
179 | } |
|
|
180 | |
|
|
181 | ~init () |
|
|
182 | { |
|
|
183 | iom_valid = false; |
|
|
184 | } |
|
|
185 | |
|
|
186 | static void required (); |
|
|
187 | } init; |
|
|
188 | |
|
|
189 | void |
|
|
190 | init::required () |
|
|
191 | { |
|
|
192 | if (!iom_valid) |
|
|
193 | { |
|
|
194 | write (2, "io_manager: early registration attempt, aborting.\n", |
|
|
195 | sizeof ("io_manager: early registration attempt, aborting.\n") - 1); |
|
|
196 | abort (); |
|
|
197 | } |
|
|
198 | } |
|
|
199 | |
|
|
200 | template<class watcher> |
|
|
201 | void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue) |
|
|
202 | { |
|
|
203 | init::required (); |
|
|
204 | |
|
|
205 | if (!w.active) |
|
|
206 | { |
|
|
207 | queue.push_back (&w); |
|
|
208 | w.active = queue.size (); |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | template<class watcher> |
|
|
213 | void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue) |
|
|
214 | { |
|
|
215 | if (!iom_valid) |
|
|
216 | return; |
|
|
217 | |
|
|
218 | if (w.active) |
|
|
219 | { |
|
|
220 | queue [w.active - 1] = 0; |
|
|
221 | w.active = 0; |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
|
|
225 | #if IOM_TIME |
45 | void time_watcher::trigger () |
226 | void time_watcher::trigger () |
46 | { |
227 | { |
47 | iom.unreg (this); |
|
|
48 | call (at); |
228 | call (*this); |
49 | iom.reg (this); |
|
|
50 | } |
|
|
51 | |
|
|
52 | void time_watcher::start () |
|
|
53 | { |
|
|
54 | iom.unreg (this); |
229 | io_manager::reg (*this); |
55 | iom.reg (this); |
|
|
56 | } |
230 | } |
57 | |
231 | |
58 | void io_manager::reg (int fd, short events, io_watcher *w) |
232 | void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } |
59 | { |
233 | void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } |
60 | pollfd pfd; |
234 | #endif |
61 | |
235 | |
62 | pfd.fd = fd; |
236 | #if IOM_IO |
63 | pfd.events = events; |
237 | void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } |
|
|
238 | void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } |
|
|
239 | #endif |
64 | |
240 | |
65 | pfs.push_back (pfd); |
241 | #if IOM_CHECK |
66 | iow.push_back (w); |
242 | void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } |
67 | } |
243 | void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } |
|
|
244 | #endif |
68 | |
245 | |
|
|
246 | #if IOM_IDLE |
|
|
247 | void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); } |
|
|
248 | void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); } |
|
|
249 | #endif |
|
|
250 | |
|
|
251 | #if IOM_SIG |
|
|
252 | static void |
|
|
253 | sighandler (int signum) |
|
|
254 | { |
|
|
255 | sw [signum - 1]->pending = true; |
|
|
256 | |
|
|
257 | // we use a pipe for signal notifications, as most current |
|
|
258 | // OSes (Linux...) do not implement pselect correctly. ugh. |
|
|
259 | char ch = signum; // actual content not used |
|
|
260 | write (sigpipe[1], &ch, 1); |
|
|
261 | } |
|
|
262 | |
69 | void io_manager::unreg (const io_watcher *w) |
263 | void io_manager::reg (sig_watcher &w) |
70 | { |
264 | { |
71 | unsigned int sz = iow.size (); |
265 | init::required (); |
72 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
|
|
73 | |
266 | |
|
|
267 | assert (0 < w.signum); |
|
|
268 | |
|
|
269 | sw.reserve (w.signum); |
|
|
270 | |
|
|
271 | while (sw.size () < w.signum) // pathetic |
|
|
272 | sw.push_back (0); |
|
|
273 | |
|
|
274 | sig_vec *&sv = sw[w.signum - 1]; |
|
|
275 | |
74 | if (i != sz) |
276 | if (!sv) |
75 | { |
277 | { |
76 | if (sz == 1) |
278 | sv = new sig_vec; |
|
|
279 | |
|
|
280 | sigaddset (&sigs, w.signum); |
|
|
281 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
282 | |
|
|
283 | struct sigaction sa; |
|
|
284 | sa.sa_handler = sighandler; |
|
|
285 | sigfillset (&sa.sa_mask); |
|
|
286 | sa.sa_flags = SA_RESTART; |
|
|
287 | |
|
|
288 | if (sigaction (w.signum, &sa, 0)) |
77 | { |
289 | { |
|
|
290 | perror ("io_manager: error while installing signal handler, ignoring."); |
78 | pfs.clear (); |
291 | abort (); |
79 | iow.clear (); |
|
|
80 | } |
292 | } |
81 | else if (i == sz - 1) |
293 | |
|
|
294 | } |
|
|
295 | |
|
|
296 | io_manager::reg (w, *sv); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void io_manager::unreg (sig_watcher &w) |
|
|
300 | { |
|
|
301 | if (!w.active || !iom_valid) |
|
|
302 | return; |
|
|
303 | |
|
|
304 | assert (0 < w.signum && w.signum <= sw.size ()); |
|
|
305 | |
|
|
306 | io_manager::unreg (w, *sw[w.signum - 1]); |
|
|
307 | } |
|
|
308 | |
|
|
309 | void sig_watcher::start (int signum) |
|
|
310 | { |
|
|
311 | stop (); |
|
|
312 | this->signum = signum; |
|
|
313 | io_manager::reg (*this); |
|
|
314 | } |
|
|
315 | #endif |
|
|
316 | |
|
|
317 | #if IOM_CHILD |
|
|
318 | void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); } |
|
|
319 | void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); } |
|
|
320 | #endif |
|
|
321 | |
|
|
322 | void io_manager::loop () |
|
|
323 | { |
|
|
324 | init::required (); |
|
|
325 | |
|
|
326 | #if IOM_TIME |
|
|
327 | set_now (); |
|
|
328 | #endif |
|
|
329 | |
|
|
330 | for (;;) |
|
|
331 | { |
|
|
332 | |
|
|
333 | #if IOM_TIME |
|
|
334 | // call pending time watchers |
|
|
335 | { |
|
|
336 | bool activity; |
|
|
337 | |
|
|
338 | do |
|
|
339 | { |
|
|
340 | activity = false; |
|
|
341 | |
|
|
342 | for (int i = tw.size (); i--; ) |
|
|
343 | if (!tw[i]) |
|
|
344 | tw.erase_unordered (i); |
|
|
345 | else if (tw[i]->at <= NOW) |
|
|
346 | { |
|
|
347 | time_watcher &w = *tw[i]; |
|
|
348 | |
|
|
349 | unreg (w); |
|
|
350 | w.call (w); |
|
|
351 | |
|
|
352 | activity = true; |
|
|
353 | } |
|
|
354 | } |
|
|
355 | while (activity); |
|
|
356 | } |
|
|
357 | #endif |
|
|
358 | |
|
|
359 | #if IOM_CHECK |
|
|
360 | // call all check watchers |
|
|
361 | for (int i = cw.size (); i--; ) |
|
|
362 | if (!cw[i]) |
|
|
363 | cw.erase_unordered (i); |
|
|
364 | else |
|
|
365 | cw[i]->call (*cw[i]); |
|
|
366 | #endif |
|
|
367 | |
|
|
368 | struct TIMEVAL *to = 0; |
|
|
369 | struct TIMEVAL tval; |
|
|
370 | |
|
|
371 | #if IOM_IDLE |
|
|
372 | if (iw.size ()) |
82 | { |
373 | { |
83 | iow.pop_back (); |
374 | tval.tv_sec = 0; |
84 | pfs.pop_back (); |
375 | tval.TV_FRAC = 0; |
|
|
376 | to = &tval; |
85 | } |
377 | } |
86 | else |
378 | else |
|
|
379 | #endif |
87 | { |
380 | { |
88 | iow[i] = iow[sz - 1]; iow.pop_back (); |
381 | #if IOM_TIME |
89 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
382 | // find earliest active watcher |
|
|
383 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
384 | |
|
|
385 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
386 | if (*i && (*i)->at < next->at) |
|
|
387 | next = *i; |
|
|
388 | |
|
|
389 | if (next->at > NOW && next != tw[0]) |
|
|
390 | { |
|
|
391 | double diff = next->at - NOW; |
|
|
392 | tval.tv_sec = (int)diff; |
|
|
393 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
|
|
394 | to = &tval; |
|
|
395 | } |
90 | } |
396 | } |
|
|
397 | #endif |
|
|
398 | |
|
|
399 | #if IOM_IO || IOM_SIG |
|
|
400 | fd_set rfd, wfd; |
|
|
401 | |
|
|
402 | FD_ZERO (&rfd); |
|
|
403 | FD_ZERO (&wfd); |
|
|
404 | |
|
|
405 | int fds = 0; |
|
|
406 | |
|
|
407 | # if IOM_IO |
|
|
408 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
|
|
409 | if (*i) |
|
|
410 | { |
|
|
411 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
|
|
412 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
|
|
413 | |
|
|
414 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
|
|
415 | } |
|
|
416 | # endif |
|
|
417 | |
|
|
418 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
|
|
419 | break; // no events |
|
|
420 | |
|
|
421 | # if IOM_SIG |
|
|
422 | FD_SET (sigpipe[0], &rfd); |
|
|
423 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
|
|
424 | # endif |
|
|
425 | |
|
|
426 | # if IOM_SIG |
|
|
427 | // there is no race, as we use a pipe for signals, so select |
|
|
428 | // will return if a signal is caught. |
|
|
429 | sigprocmask (SIG_UNBLOCK, &sigs, NULL); |
|
|
430 | # endif |
|
|
431 | fds = select (fds, &rfd, &wfd, NULL, to); |
|
|
432 | # if IOM_SIG |
|
|
433 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
434 | # endif |
|
|
435 | |
|
|
436 | # if IOM_TIME |
|
|
437 | { |
|
|
438 | // update time, try to compensate for gross non-monotonic time changes |
|
|
439 | tstamp diff = NOW; |
|
|
440 | set_now (); |
|
|
441 | diff = NOW - diff; |
|
|
442 | |
|
|
443 | if (diff < 0) |
|
|
444 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
445 | if (*i) |
|
|
446 | (*i)->at += diff; |
|
|
447 | } |
|
|
448 | # endif |
|
|
449 | |
|
|
450 | if (fds > 0) |
|
|
451 | { |
|
|
452 | # if IOM_SIG |
|
|
453 | if (FD_ISSET (sigpipe[0], &rfd)) |
|
|
454 | { |
|
|
455 | char ch; |
|
|
456 | |
|
|
457 | while (read (sigpipe[0], &ch, 1) > 0) |
|
|
458 | ; |
|
|
459 | |
|
|
460 | for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
|
|
461 | if (*svp && (*svp)->pending) |
|
|
462 | { |
|
|
463 | sig_vec &sv = **svp; |
|
|
464 | for (int i = sv.size (); i--; ) |
|
|
465 | if (!sv[i]) |
|
|
466 | sv.erase_unordered (i); |
|
|
467 | else |
|
|
468 | sv[i]->call (*sv[i]); |
|
|
469 | |
|
|
470 | sv.pending = false; |
|
|
471 | } |
|
|
472 | } |
|
|
473 | # endif |
|
|
474 | |
|
|
475 | # if IOM_IO |
|
|
476 | for (int i = iow.size (); i--; ) |
|
|
477 | if (!iow[i]) |
|
|
478 | iow.erase_unordered (i); |
|
|
479 | else |
|
|
480 | { |
|
|
481 | io_watcher &w = *iow[i]; |
|
|
482 | short revents = w.events; |
|
|
483 | |
|
|
484 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
|
|
485 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
|
|
486 | |
|
|
487 | if (revents) |
|
|
488 | w.call (w, revents); |
|
|
489 | } |
|
|
490 | #endif |
|
|
491 | } |
|
|
492 | else if (fds < 0 && errno != EINTR) |
|
|
493 | { |
|
|
494 | perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); |
|
|
495 | abort (); |
|
|
496 | } |
|
|
497 | #if IOM_IDLE |
|
|
498 | else |
|
|
499 | for (int i = iw.size (); i--; ) |
|
|
500 | if (!iw[i]) |
|
|
501 | iw.erase_unordered (i); |
|
|
502 | else |
|
|
503 | iw[i]->call (*iw[i]); |
|
|
504 | #endif |
|
|
505 | |
|
|
506 | #elif IOM_TIME |
|
|
507 | if (!to) |
|
|
508 | break; |
|
|
509 | |
|
|
510 | select (0, 0, 0, 0, &to); |
|
|
511 | set_now (); |
|
|
512 | #else |
|
|
513 | break; |
|
|
514 | #endif |
91 | } |
515 | } |
92 | } |
516 | } |
93 | |
517 | |
94 | void io_manager::reg (time_watcher *w) |
|
|
95 | { |
|
|
96 | tw.push_back (w); |
|
|
97 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
98 | } |
|
|
99 | |
|
|
100 | void io_manager::unreg (const time_watcher *w) |
|
|
101 | { |
|
|
102 | unsigned int sz = tw.size (); |
|
|
103 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
|
|
104 | |
|
|
105 | if (i != sz) |
|
|
106 | { |
|
|
107 | if (i != sz - 1) |
|
|
108 | tw[i] = tw[sz - 1]; |
|
|
109 | |
|
|
110 | tw.pop_back (); |
|
|
111 | make_heap (tw.begin (), tw.end (), lowest_first); |
|
|
112 | } |
|
|
113 | } |
|
|
114 | |
|
|
115 | inline void set_now (void) |
|
|
116 | { |
|
|
117 | struct timeval tv; |
|
|
118 | |
|
|
119 | gettimeofday (&tv, 0); |
|
|
120 | |
|
|
121 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
122 | } |
|
|
123 | |
|
|
124 | void io_manager::loop () |
|
|
125 | { |
|
|
126 | set_now (); |
|
|
127 | |
|
|
128 | for (;;) |
|
|
129 | { |
|
|
130 | while (tw[0]->at <= NOW) |
|
|
131 | { |
|
|
132 | pop_heap (tw.begin (), tw.end (), lowest_first); |
|
|
133 | time_watcher *w = *(tw.end () - 1); |
|
|
134 | |
|
|
135 | if (w->at >= 0) |
|
|
136 | { |
|
|
137 | w->call (w->at); |
|
|
138 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | tw.pop_back (); |
|
|
142 | } |
|
|
143 | |
|
|
144 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
145 | |
|
|
146 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
147 | |
|
|
148 | set_now (); |
|
|
149 | |
|
|
150 | for (unsigned int i = iow.size (); fds > 0 && i--; ) |
|
|
151 | if (pfs[i].revents) |
|
|
152 | { |
|
|
153 | --fds; |
|
|
154 | iow[i]->call (pfs[i].revents); |
|
|
155 | } |
|
|
156 | } |
|
|
157 | } |
|
|
158 | |
|
|
159 | void io_manager::idle_cb (tstamp &ts) |
|
|
160 | { |
|
|
161 | ts = NOW + 86400; // wake up every day, for no good reason |
|
|
162 | } |
|
|
163 | |
|
|
164 | io_manager::io_manager () |
|
|
165 | { |
|
|
166 | set_now (); |
|
|
167 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
168 | idle->start (0); |
|
|
169 | } |
|
|
170 | |
|
|
171 | io_manager::~io_manager () |
|
|
172 | { |
|
|
173 | // |
|
|
174 | } |
|
|
175 | |
|
|