… | |
… | |
49 | |
49 | |
50 | #define TIMEVAL timeval |
50 | #define TIMEVAL timeval |
51 | #define TV_FRAC tv_usec |
51 | #define TV_FRAC tv_usec |
52 | #define TV_MULT 1000000L |
52 | #define TV_MULT 1000000L |
53 | |
53 | |
|
|
54 | #ifndef IOM_LIBEVENT |
54 | #if IOM_IO |
55 | #if IOM_IO |
55 | static io_manager_vec<io_watcher> iow; |
56 | static io_manager_vec<io_watcher> iow; |
56 | #endif |
57 | #endif |
|
|
58 | #if IOM_TIME |
|
|
59 | static io_manager_vec<time_watcher> tw; |
|
|
60 | #endif |
|
|
61 | #endif |
|
|
62 | |
57 | #if IOM_CHECK |
63 | #if IOM_CHECK |
58 | static io_manager_vec<check_watcher> cw; |
64 | static io_manager_vec<check_watcher> cw; |
59 | #endif |
65 | #endif |
60 | #if IOM_TIME |
|
|
61 | static io_manager_vec<time_watcher> tw; |
|
|
62 | #endif |
|
|
63 | #if IOM_IDLE |
66 | #if IOM_IDLE |
64 | static io_manager_vec<idle_watcher> iw; |
67 | static io_manager_vec<idle_watcher> iw; |
65 | #endif |
68 | #endif |
|
|
69 | |
66 | #if IOM_SIG |
70 | #if IOM_SIG |
67 | static int sigpipe[2]; // signal signalling pipe |
71 | static int sigpipe[2]; // signal signalling pipe |
68 | static sigset_t sigs; |
72 | static sigset_t sigs; |
69 | struct sig_vec : io_manager_vec<sig_watcher> { |
73 | struct sig_vec : io_manager_vec<sig_watcher> { |
70 | int pending; |
74 | int pending; |
… | |
… | |
72 | : pending (false) |
76 | : pending (false) |
73 | { } |
77 | { } |
74 | }; |
78 | }; |
75 | static vector<sig_vec *> sw; |
79 | static vector<sig_vec *> sw; |
76 | #endif |
80 | #endif |
|
|
81 | |
77 | #if IOM_CHILD |
82 | #if IOM_CHILD |
78 | static io_manager_vec<child_watcher> pw; |
83 | static io_manager_vec<child_watcher> pw; |
79 | #endif |
84 | #endif |
80 | |
85 | |
81 | // this is a dummy time watcher to ensure that the first |
86 | // this is a dummy time watcher to ensure that the first |
… | |
… | |
120 | { |
125 | { |
121 | io_manager::unreg (*w); |
126 | io_manager::unreg (*w); |
122 | w->call (*w, status); |
127 | w->call (*w, status); |
123 | } |
128 | } |
124 | } |
129 | } |
125 | |
|
|
126 | } |
130 | } |
127 | |
131 | |
128 | sw0 () |
132 | sw0 () |
129 | : sig_watcher (this, &sw0::cb) |
133 | : sig_watcher (this, &sw0::cb) |
130 | { } |
134 | { } |
… | |
… | |
150 | |
154 | |
151 | // used for initialisation only |
155 | // used for initialisation only |
152 | static struct init { |
156 | static struct init { |
153 | init () |
157 | init () |
154 | { |
158 | { |
|
|
159 | #ifdef IOM_PREINIT |
|
|
160 | { IOM_PREINIT } |
|
|
161 | #endif |
|
|
162 | #ifdef IOM_LIBEVENT |
|
|
163 | event_init (); |
|
|
164 | #endif |
155 | iom_valid = true; |
165 | iom_valid = true; |
156 | |
166 | |
157 | #if IOM_SIG |
167 | #if IOM_SIG |
158 | sigemptyset (&sigs); |
168 | sigemptyset (&sigs); |
159 | |
169 | |
… | |
… | |
173 | |
183 | |
174 | #if IOM_TIME |
184 | #if IOM_TIME |
175 | io_manager::set_now (); |
185 | io_manager::set_now (); |
176 | |
186 | |
177 | tw0.start (TSTAMP_MAX); |
187 | tw0.start (TSTAMP_MAX); |
|
|
188 | #endif |
|
|
189 | |
|
|
190 | #ifdef IOM_POSTINIT |
|
|
191 | { IOM_POSTINIT } |
178 | #endif |
192 | #endif |
179 | } |
193 | } |
180 | |
194 | |
181 | ~init () |
195 | ~init () |
182 | { |
196 | { |
… | |
… | |
221 | w.active = 0; |
235 | w.active = 0; |
222 | } |
236 | } |
223 | } |
237 | } |
224 | |
238 | |
225 | #if IOM_TIME |
239 | #if IOM_TIME |
|
|
240 | # ifdef IOM_LIBEVENT |
|
|
241 | void iom_time_c_callback (int fd, short events, void *data) |
|
|
242 | { |
|
|
243 | time_watcher *w = static_cast<time_watcher *>(data); |
|
|
244 | w->call (*w); |
|
|
245 | } |
|
|
246 | # else |
|
|
247 | void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } |
|
|
248 | void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } |
|
|
249 | # endif |
|
|
250 | |
226 | void time_watcher::trigger () |
251 | void time_watcher::trigger () |
227 | { |
252 | { |
228 | call (*this); |
253 | call (*this); |
229 | io_manager::reg (*this); |
254 | start (); |
230 | } |
255 | } |
231 | |
|
|
232 | void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } |
|
|
233 | void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } |
|
|
234 | #endif |
256 | #endif |
235 | |
257 | |
236 | #if IOM_IO |
258 | #if IOM_IO |
|
|
259 | # ifdef IOM_LIBEVENT |
|
|
260 | void iom_io_c_callback (int fd, short events, void *data) |
|
|
261 | { |
|
|
262 | io_watcher *w = static_cast<io_watcher *>(data); |
|
|
263 | w->call (*w, events); |
|
|
264 | } |
|
|
265 | # else |
237 | void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } |
266 | 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); } |
267 | void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } |
|
|
268 | # endif |
239 | #endif |
269 | #endif |
240 | |
270 | |
241 | #if IOM_CHECK |
271 | #if IOM_CHECK |
242 | void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } |
272 | void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } |
243 | void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } |
273 | void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } |
… | |
… | |
321 | |
351 | |
322 | void io_manager::loop () |
352 | void io_manager::loop () |
323 | { |
353 | { |
324 | init::required (); |
354 | init::required (); |
325 | |
355 | |
326 | #if IOM_TIME |
356 | #if IOM_TIME |
327 | set_now (); |
357 | set_now (); |
328 | #endif |
358 | #endif |
329 | |
359 | |
330 | for (;;) |
360 | for (;;) |
331 | { |
361 | { |
332 | |
362 | #ifndef IOM_LIBEVENT |
333 | #if IOM_TIME |
363 | #if IOM_TIME |
334 | // call pending time watchers |
364 | // call pending time watchers |
335 | { |
365 | { |
336 | bool activity; |
366 | bool activity; |
337 | |
367 | |
338 | do |
368 | do |
339 | { |
369 | { |
340 | activity = false; |
370 | activity = false; |
341 | |
371 | |
342 | for (int i = tw.size (); i--; ) |
372 | for (int i = tw.size (); i--; ) |
343 | if (!tw[i]) |
373 | if (!tw[i]) |
344 | tw.erase_unordered (i); |
374 | tw.erase_unordered (i); |
345 | else if (tw[i]->at <= NOW) |
375 | else if (tw[i]->at <= NOW) |
346 | { |
376 | { |
347 | time_watcher &w = *tw[i]; |
377 | time_watcher &w = *tw[i]; |
|
|
378 | |
|
|
379 | unreg (w); |
|
|
380 | w.call (w); |
|
|
381 | |
|
|
382 | activity = true; |
348 | |
383 | } |
349 | unreg (w); |
|
|
350 | w.call (w); |
|
|
351 | |
|
|
352 | activity = true; |
|
|
353 | } |
|
|
354 | } |
384 | } |
355 | while (activity); |
385 | while (activity); |
356 | } |
386 | } |
357 | #endif |
387 | #endif |
|
|
388 | #endif |
358 | |
389 | |
359 | #if IOM_CHECK |
390 | #if IOM_CHECK |
360 | // call all check watchers |
391 | // call all check watchers |
361 | for (int i = cw.size (); i--; ) |
392 | for (int i = cw.size (); i--; ) |
362 | if (!cw[i]) |
393 | if (!cw[i]) |
363 | cw.erase_unordered (i); |
394 | cw.erase_unordered (i); |
364 | else |
395 | else |
365 | cw[i]->call (*cw[i]); |
396 | cw[i]->call (*cw[i]); |
366 | #endif |
397 | #endif |
367 | |
398 | |
368 | struct TIMEVAL *to = 0; |
399 | struct TIMEVAL *to = 0; |
369 | struct TIMEVAL tval; |
400 | struct TIMEVAL tval; |
370 | |
401 | |
371 | #if IOM_IDLE |
402 | #if IOM_IDLE |
… | |
… | |
375 | tval.TV_FRAC = 0; |
406 | tval.TV_FRAC = 0; |
376 | to = &tval; |
407 | to = &tval; |
377 | } |
408 | } |
378 | else |
409 | else |
379 | #endif |
410 | #endif |
|
|
411 | #ifndef IOM_LIBEVENT |
380 | { |
412 | { |
381 | #if IOM_TIME |
413 | #if IOM_TIME |
382 | // find earliest active watcher |
414 | // find earliest active watcher |
383 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
415 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
384 | |
416 | |
385 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
417 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
386 | if (*i && (*i)->at < next->at) |
418 | if (*i && (*i)->at < next->at) |
387 | next = *i; |
419 | next = *i; |
388 | |
420 | |
389 | if (next->at > NOW && next != tw[0]) |
421 | if (next->at > NOW && next != tw[0]) |
390 | { |
422 | { |
391 | double diff = next->at - NOW; |
423 | double diff = next->at - NOW; |
392 | tval.tv_sec = (int)diff; |
424 | tval.tv_sec = (int)diff; |
393 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
425 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
394 | to = &tval; |
426 | to = &tval; |
395 | } |
427 | } |
|
|
428 | #endif |
396 | } |
429 | } |
397 | #endif |
|
|
398 | |
430 | |
399 | #if IOM_IO || IOM_SIG |
431 | #if IOM_IO || IOM_SIG |
400 | fd_set rfd, wfd; |
432 | fd_set rfd, wfd; |
401 | |
433 | |
402 | FD_ZERO (&rfd); |
434 | FD_ZERO (&rfd); |
403 | FD_ZERO (&wfd); |
435 | FD_ZERO (&wfd); |
404 | |
436 | |
405 | int fds = 0; |
437 | int fds = 0; |
406 | |
438 | |
407 | # if IOM_IO |
439 | #if IOM_IO |
408 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
440 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
409 | if (*i) |
441 | if (*i) |
410 | { |
442 | { |
411 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
443 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
412 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
444 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
413 | |
445 | |
414 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
446 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
415 | } |
447 | } |
416 | # endif |
448 | #endif |
417 | |
449 | |
418 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
450 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
419 | break; // no events |
451 | break; // no events |
420 | |
452 | |
421 | # if IOM_SIG |
453 | #if IOM_SIG |
422 | FD_SET (sigpipe[0], &rfd); |
454 | FD_SET (sigpipe[0], &rfd); |
423 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
455 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
424 | # endif |
456 | #endif |
425 | |
457 | |
426 | # if IOM_SIG |
458 | #if IOM_SIG |
427 | // there is no race, as we use a pipe for signals, so select |
459 | // there is no race, as we use a pipe for signals, so select |
428 | // will return if a signal is caught. |
460 | // will return if a signal is caught. |
429 | sigprocmask (SIG_UNBLOCK, &sigs, NULL); |
461 | sigprocmask (SIG_UNBLOCK, &sigs, NULL); |
430 | # endif |
462 | #endif |
431 | fds = select (fds, &rfd, &wfd, NULL, to); |
463 | fds = select (fds, &rfd, &wfd, NULL, to); |
432 | # if IOM_SIG |
464 | #if IOM_SIG |
433 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
465 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
466 | #endif |
|
|
467 | |
|
|
468 | #else |
|
|
469 | if (to) |
|
|
470 | event_loop (EVLOOP_NONBLOCK); |
|
|
471 | else |
|
|
472 | event_loop (EVLOOP_ONCE); |
434 | # endif |
473 | #endif |
435 | |
474 | |
436 | # if IOM_TIME |
475 | #if IOM_TIME |
437 | { |
476 | { |
438 | // update time, try to compensate for gross non-monotonic time changes |
477 | // update time, try to compensate for gross non-monotonic time changes |
439 | tstamp diff = NOW; |
478 | tstamp diff = NOW; |
440 | set_now (); |
479 | set_now (); |
441 | diff = NOW - diff; |
480 | diff = NOW - diff; |
442 | |
481 | |
443 | if (diff < 0) |
482 | if (diff < 0) |
444 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
483 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
445 | if (*i) |
484 | if (*i) |
446 | (*i)->at += diff; |
485 | (*i)->at += diff; |
447 | } |
486 | } |
448 | # endif |
487 | #endif |
449 | |
488 | |
|
|
489 | #ifndef IOM_LIBEVENT |
450 | if (fds > 0) |
490 | if (fds > 0) |
451 | { |
491 | { |
452 | # if IOM_SIG |
492 | #if IOM_SIG |
453 | if (FD_ISSET (sigpipe[0], &rfd)) |
493 | 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 | { |
494 | { |
481 | io_watcher &w = *iow[i]; |
495 | char ch; |
482 | short revents = w.events; |
|
|
483 | |
496 | |
484 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
497 | while (read (sigpipe[0], &ch, 1) > 0) |
485 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
498 | ; |
486 | |
499 | |
487 | if (revents) |
500 | for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
488 | w.call (w, revents); |
501 | if (*svp && (*svp)->pending) |
|
|
502 | { |
|
|
503 | sig_vec &sv = **svp; |
|
|
504 | for (int i = sv.size (); i--; ) |
|
|
505 | if (!sv[i]) |
|
|
506 | sv.erase_unordered (i); |
|
|
507 | else |
|
|
508 | sv[i]->call (*sv[i]); |
|
|
509 | |
|
|
510 | sv.pending = false; |
|
|
511 | } |
489 | } |
512 | } |
490 | #endif |
513 | #endif |
|
|
514 | |
|
|
515 | #if IOM_IO |
|
|
516 | for (int i = iow.size (); i--; ) |
|
|
517 | if (!iow[i]) |
|
|
518 | iow.erase_unordered (i); |
|
|
519 | else |
|
|
520 | { |
|
|
521 | io_watcher &w = *iow[i]; |
|
|
522 | short revents = w.events; |
|
|
523 | |
|
|
524 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
|
|
525 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
|
|
526 | |
|
|
527 | if (revents) |
|
|
528 | w.call (w, revents); |
|
|
529 | } |
|
|
530 | #endif |
491 | } |
531 | } |
492 | else if (fds < 0 && errno != EINTR) |
532 | else if (fds < 0 && errno != EINTR) |
493 | { |
533 | { |
494 | perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); |
534 | perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); |
495 | abort (); |
535 | abort (); |
496 | } |
536 | } |
|
|
537 | #else |
|
|
538 | if (0) |
|
|
539 | ; |
|
|
540 | #endif |
497 | #if IOM_IDLE |
541 | #if IOM_IDLE |
498 | else |
542 | else |
499 | for (int i = iw.size (); i--; ) |
543 | for (int i = iw.size (); i--; ) |
500 | if (!iw[i]) |
544 | if (!iw[i]) |
501 | iw.erase_unordered (i); |
545 | iw.erase_unordered (i); |
… | |
… | |
505 | |
549 | |
506 | #elif IOM_TIME |
550 | #elif IOM_TIME |
507 | if (!to) |
551 | if (!to) |
508 | break; |
552 | break; |
509 | |
553 | |
510 | select (0, 0, 0, 0, &to); |
554 | select (0, 0, 0, 0, to); |
511 | set_now (); |
555 | set_now (); |
512 | #else |
|
|
513 | break; |
556 | break; |
514 | #endif |
557 | #endif |
515 | } |
558 | } |
516 | } |
559 | } |
517 | |
560 | |