… | |
… | |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #ifndef EV_STANDALONE |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
|
|
33 | |
|
|
34 | # if HAVE_CLOCK_GETTIME |
|
|
35 | # define EV_USE_MONOTONIC 1 |
|
|
36 | # define EV_USE_REALTIME 1 |
|
|
37 | # endif |
|
|
38 | |
|
|
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
40 | # define EV_USE_SELECT 1 |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_POLL && HAVE_POLL_H |
|
|
44 | # define EV_USE_POLL 1 |
|
|
45 | # endif |
|
|
46 | |
|
|
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
48 | # define EV_USE_EPOLL 1 |
|
|
49 | # endif |
|
|
50 | |
|
|
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
52 | # define EV_USE_KQUEUE 1 |
|
|
53 | # endif |
|
|
54 | |
33 | #endif |
55 | #endif |
34 | |
56 | |
35 | #include <math.h> |
57 | #include <math.h> |
36 | #include <stdlib.h> |
58 | #include <stdlib.h> |
37 | #include <unistd.h> |
59 | #include <unistd.h> |
… | |
… | |
58 | |
80 | |
59 | #ifndef EV_USE_SELECT |
81 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
82 | # define EV_USE_SELECT 1 |
61 | #endif |
83 | #endif |
62 | |
84 | |
63 | #ifndef EV_USEV_POLL |
85 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
86 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
87 | #endif |
66 | |
88 | |
67 | #ifndef EV_USE_EPOLL |
89 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
90 | # define EV_USE_EPOLL 0 |
69 | #endif |
91 | #endif |
70 | |
92 | |
71 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | #ifndef EV_USE_WIN32 |
|
|
98 | # ifdef WIN32 |
|
|
99 | # define EV_USE_WIN32 1 |
|
|
100 | # else |
|
|
101 | # define EV_USE_WIN32 0 |
|
|
102 | # endif |
73 | #endif |
103 | #endif |
74 | |
104 | |
75 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
77 | #endif |
107 | #endif |
… | |
… | |
113 | |
143 | |
114 | typedef struct ev_watcher *W; |
144 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
145 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
146 | typedef struct ev_watcher_time *WT; |
117 | |
147 | |
|
|
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
|
|
149 | |
118 | /*****************************************************************************/ |
150 | /*****************************************************************************/ |
119 | |
151 | |
120 | typedef struct |
152 | typedef struct |
121 | { |
153 | { |
122 | struct ev_watcher_list *head; |
154 | struct ev_watcher_list *head; |
… | |
… | |
128 | { |
160 | { |
129 | W w; |
161 | W w; |
130 | int events; |
162 | int events; |
131 | } ANPENDING; |
163 | } ANPENDING; |
132 | |
164 | |
133 | #ifdef EV_MULTIPLICITY |
165 | #if EV_MULTIPLICITY |
|
|
166 | |
134 | struct ev_loop |
167 | struct ev_loop |
135 | { |
168 | { |
136 | # define VAR(name,decl) decl |
169 | # define VAR(name,decl) decl; |
137 | # include "ev_vars.h" |
170 | # include "ev_vars.h" |
138 | }; |
171 | }; |
|
|
172 | # undef VAR |
|
|
173 | # include "ev_wrap.h" |
|
|
174 | |
139 | #else |
175 | #else |
|
|
176 | |
140 | # define VAR(name,decl) static decl |
177 | # define VAR(name,decl) static decl; |
141 | # include "ev_vars.h" |
178 | # include "ev_vars.h" |
142 | #endif |
|
|
143 | #undef VAR |
179 | # undef VAR |
|
|
180 | |
|
|
181 | #endif |
144 | |
182 | |
145 | /*****************************************************************************/ |
183 | /*****************************************************************************/ |
146 | |
184 | |
147 | inline ev_tstamp |
185 | inline ev_tstamp |
148 | ev_time (void) |
186 | ev_time (void) |
… | |
… | |
319 | |
357 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
358 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
359 | static void |
322 | fd_enomem (EV_P) |
360 | fd_enomem (EV_P) |
323 | { |
361 | { |
324 | int fd = anfdmax; |
362 | int fd; |
325 | |
363 | |
326 | while (fd--) |
364 | for (fd = anfdmax; fd--; ) |
327 | if (anfds [fd].events) |
365 | if (anfds [fd].events) |
328 | { |
366 | { |
329 | close (fd); |
367 | close (fd); |
330 | fd_kill (EV_A_ fd); |
368 | fd_kill (EV_A_ fd); |
331 | return; |
369 | return; |
332 | } |
370 | } |
333 | } |
371 | } |
334 | |
372 | |
|
|
373 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
374 | static void |
|
|
375 | fd_rearm_all (EV_P) |
|
|
376 | { |
|
|
377 | int fd; |
|
|
378 | |
|
|
379 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
380 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
381 | if (anfds [fd].events) |
|
|
382 | { |
|
|
383 | anfds [fd].events = 0; |
|
|
384 | fd_change (EV_A_ fd); |
|
|
385 | } |
|
|
386 | } |
|
|
387 | |
335 | /*****************************************************************************/ |
388 | /*****************************************************************************/ |
336 | |
389 | |
337 | static void |
390 | static void |
338 | upheap (WT *timers, int k) |
391 | upheap (WT *heap, int k) |
339 | { |
392 | { |
340 | WT w = timers [k]; |
393 | WT w = heap [k]; |
341 | |
394 | |
342 | while (k && timers [k >> 1]->at > w->at) |
395 | while (k && heap [k >> 1]->at > w->at) |
343 | { |
396 | { |
344 | timers [k] = timers [k >> 1]; |
397 | heap [k] = heap [k >> 1]; |
345 | timers [k]->active = k + 1; |
398 | ((W)heap [k])->active = k + 1; |
346 | k >>= 1; |
399 | k >>= 1; |
347 | } |
400 | } |
348 | |
401 | |
349 | timers [k] = w; |
402 | heap [k] = w; |
350 | timers [k]->active = k + 1; |
403 | ((W)heap [k])->active = k + 1; |
351 | |
404 | |
352 | } |
405 | } |
353 | |
406 | |
354 | static void |
407 | static void |
355 | downheap (WT *timers, int N, int k) |
408 | downheap (WT *heap, int N, int k) |
356 | { |
409 | { |
357 | WT w = timers [k]; |
410 | WT w = heap [k]; |
358 | |
411 | |
359 | while (k < (N >> 1)) |
412 | while (k < (N >> 1)) |
360 | { |
413 | { |
361 | int j = k << 1; |
414 | int j = k << 1; |
362 | |
415 | |
363 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
416 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
364 | ++j; |
417 | ++j; |
365 | |
418 | |
366 | if (w->at <= timers [j]->at) |
419 | if (w->at <= heap [j]->at) |
367 | break; |
420 | break; |
368 | |
421 | |
369 | timers [k] = timers [j]; |
422 | heap [k] = heap [j]; |
370 | timers [k]->active = k + 1; |
423 | ((W)heap [k])->active = k + 1; |
371 | k = j; |
424 | k = j; |
372 | } |
425 | } |
373 | |
426 | |
374 | timers [k] = w; |
427 | heap [k] = w; |
375 | timers [k]->active = k + 1; |
428 | ((W)heap [k])->active = k + 1; |
376 | } |
429 | } |
377 | |
430 | |
378 | /*****************************************************************************/ |
431 | /*****************************************************************************/ |
379 | |
432 | |
380 | typedef struct |
433 | typedef struct |
… | |
… | |
386 | static ANSIG *signals; |
439 | static ANSIG *signals; |
387 | static int signalmax; |
440 | static int signalmax; |
388 | |
441 | |
389 | static int sigpipe [2]; |
442 | static int sigpipe [2]; |
390 | static sig_atomic_t volatile gotsig; |
443 | static sig_atomic_t volatile gotsig; |
|
|
444 | static struct ev_io sigev; |
391 | |
445 | |
392 | static void |
446 | static void |
393 | signals_init (ANSIG *base, int count) |
447 | signals_init (ANSIG *base, int count) |
394 | { |
448 | { |
395 | while (count--) |
449 | while (count--) |
… | |
… | |
445 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
499 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
446 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
500 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
447 | #endif |
501 | #endif |
448 | |
502 | |
449 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
503 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
450 | ev_io_start (&sigev); |
504 | ev_io_start (EV_A_ &sigev); |
451 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
505 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
452 | } |
506 | } |
453 | |
507 | |
454 | /*****************************************************************************/ |
508 | /*****************************************************************************/ |
455 | |
509 | |
456 | #ifndef WIN32 |
510 | #ifndef WIN32 |
|
|
511 | |
|
|
512 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
513 | static struct ev_signal childev; |
457 | |
514 | |
458 | #ifndef WCONTINUED |
515 | #ifndef WCONTINUED |
459 | # define WCONTINUED 0 |
516 | # define WCONTINUED 0 |
460 | #endif |
517 | #endif |
461 | |
518 | |
… | |
… | |
465 | struct ev_child *w; |
522 | struct ev_child *w; |
466 | |
523 | |
467 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
524 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
468 | if (w->pid == pid || !w->pid) |
525 | if (w->pid == pid || !w->pid) |
469 | { |
526 | { |
470 | w->priority = sw->priority; /* need to do it *now* */ |
527 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
471 | w->rpid = pid; |
528 | w->rpid = pid; |
472 | w->rstatus = status; |
529 | w->rstatus = status; |
473 | event (EV_A_ (W)w, EV_CHILD); |
530 | event (EV_A_ (W)w, EV_CHILD); |
474 | } |
531 | } |
475 | } |
532 | } |
476 | |
533 | |
477 | static void |
534 | static void |
… | |
… | |
497 | # include "ev_kqueue.c" |
554 | # include "ev_kqueue.c" |
498 | #endif |
555 | #endif |
499 | #if EV_USE_EPOLL |
556 | #if EV_USE_EPOLL |
500 | # include "ev_epoll.c" |
557 | # include "ev_epoll.c" |
501 | #endif |
558 | #endif |
502 | #if EV_USEV_POLL |
559 | #if EV_USE_POLL |
503 | # include "ev_poll.c" |
560 | # include "ev_poll.c" |
504 | #endif |
561 | #endif |
505 | #if EV_USE_SELECT |
562 | #if EV_USE_SELECT |
506 | # include "ev_select.c" |
563 | # include "ev_select.c" |
507 | #endif |
564 | #endif |
… | |
… | |
534 | ev_method (EV_P) |
591 | ev_method (EV_P) |
535 | { |
592 | { |
536 | return method; |
593 | return method; |
537 | } |
594 | } |
538 | |
595 | |
539 | int |
596 | static void |
540 | ev_init (EV_P_ int methods) |
597 | loop_init (EV_P_ int methods) |
541 | { |
598 | { |
542 | #ifdef EV_MULTIPLICITY |
|
|
543 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
544 | #endif |
|
|
545 | |
|
|
546 | if (!method) |
599 | if (!method) |
547 | { |
600 | { |
548 | #if EV_USE_MONOTONIC |
601 | #if EV_USE_MONOTONIC |
549 | { |
602 | { |
550 | struct timespec ts; |
603 | struct timespec ts; |
… | |
… | |
554 | #endif |
607 | #endif |
555 | |
608 | |
556 | rt_now = ev_time (); |
609 | rt_now = ev_time (); |
557 | mn_now = get_clock (); |
610 | mn_now = get_clock (); |
558 | now_floor = mn_now; |
611 | now_floor = mn_now; |
559 | diff = rt_now - mn_now; |
612 | rtmn_diff = rt_now - mn_now; |
560 | |
|
|
561 | if (pipe (sigpipe)) |
|
|
562 | return 0; |
|
|
563 | |
613 | |
564 | if (methods == EVMETHOD_AUTO) |
614 | if (methods == EVMETHOD_AUTO) |
565 | if (!enable_secure () && getenv ("LIBmethodS")) |
615 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
566 | methods = atoi (getenv ("LIBmethodS")); |
616 | methods = atoi (getenv ("LIBEV_METHODS")); |
567 | else |
617 | else |
568 | methods = EVMETHOD_ANY; |
618 | methods = EVMETHOD_ANY; |
569 | |
619 | |
570 | method = 0; |
620 | method = 0; |
|
|
621 | #if EV_USE_WIN32 |
|
|
622 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
623 | #endif |
571 | #if EV_USE_KQUEUE |
624 | #if EV_USE_KQUEUE |
572 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
625 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
573 | #endif |
626 | #endif |
574 | #if EV_USE_EPOLL |
627 | #if EV_USE_EPOLL |
575 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
628 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
576 | #endif |
629 | #endif |
577 | #if EV_USEV_POLL |
630 | #if EV_USE_POLL |
578 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
631 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
579 | #endif |
632 | #endif |
580 | #if EV_USE_SELECT |
633 | #if EV_USE_SELECT |
581 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
634 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
582 | #endif |
635 | #endif |
|
|
636 | } |
|
|
637 | } |
583 | |
638 | |
|
|
639 | void |
|
|
640 | loop_destroy (EV_P) |
|
|
641 | { |
|
|
642 | #if EV_USE_WIN32 |
|
|
643 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
644 | #endif |
|
|
645 | #if EV_USE_KQUEUE |
|
|
646 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
647 | #endif |
|
|
648 | #if EV_USE_EPOLL |
|
|
649 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
650 | #endif |
|
|
651 | #if EV_USE_POLL |
|
|
652 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
653 | #endif |
|
|
654 | #if EV_USE_SELECT |
|
|
655 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
656 | #endif |
|
|
657 | |
|
|
658 | method = 0; |
|
|
659 | /*TODO*/ |
|
|
660 | } |
|
|
661 | |
|
|
662 | void |
|
|
663 | loop_fork (EV_P) |
|
|
664 | { |
|
|
665 | /*TODO*/ |
|
|
666 | #if EV_USE_EPOLL |
|
|
667 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
668 | #endif |
|
|
669 | #if EV_USE_KQUEUE |
|
|
670 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
671 | #endif |
|
|
672 | } |
|
|
673 | |
|
|
674 | #if EV_MULTIPLICITY |
|
|
675 | struct ev_loop * |
|
|
676 | ev_loop_new (int methods) |
|
|
677 | { |
|
|
678 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
679 | |
|
|
680 | loop_init (EV_A_ methods); |
|
|
681 | |
|
|
682 | if (ev_method (EV_A)) |
|
|
683 | return loop; |
|
|
684 | |
|
|
685 | return 0; |
|
|
686 | } |
|
|
687 | |
|
|
688 | void |
|
|
689 | ev_loop_destroy (EV_P) |
|
|
690 | { |
|
|
691 | loop_destroy (EV_A); |
|
|
692 | free (loop); |
|
|
693 | } |
|
|
694 | |
|
|
695 | void |
|
|
696 | ev_loop_fork (EV_P) |
|
|
697 | { |
|
|
698 | loop_fork (EV_A); |
|
|
699 | } |
|
|
700 | |
|
|
701 | #endif |
|
|
702 | |
|
|
703 | #if EV_MULTIPLICITY |
|
|
704 | struct ev_loop default_loop_struct; |
|
|
705 | static struct ev_loop *default_loop; |
|
|
706 | |
|
|
707 | struct ev_loop * |
|
|
708 | #else |
|
|
709 | static int default_loop; |
|
|
710 | |
|
|
711 | int |
|
|
712 | #endif |
|
|
713 | ev_default_loop (int methods) |
|
|
714 | { |
|
|
715 | if (sigpipe [0] == sigpipe [1]) |
|
|
716 | if (pipe (sigpipe)) |
|
|
717 | return 0; |
|
|
718 | |
|
|
719 | if (!default_loop) |
|
|
720 | { |
|
|
721 | #if EV_MULTIPLICITY |
|
|
722 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
723 | #else |
|
|
724 | default_loop = 1; |
|
|
725 | #endif |
|
|
726 | |
|
|
727 | loop_init (EV_A_ methods); |
|
|
728 | |
584 | if (method) |
729 | if (ev_method (EV_A)) |
585 | { |
730 | { |
586 | ev_watcher_init (&sigev, sigcb); |
731 | ev_watcher_init (&sigev, sigcb); |
587 | ev_set_priority (&sigev, EV_MAXPRI); |
732 | ev_set_priority (&sigev, EV_MAXPRI); |
588 | siginit (EV_A); |
733 | siginit (EV_A); |
589 | |
734 | |
… | |
… | |
592 | ev_set_priority (&childev, EV_MAXPRI); |
737 | ev_set_priority (&childev, EV_MAXPRI); |
593 | ev_signal_start (EV_A_ &childev); |
738 | ev_signal_start (EV_A_ &childev); |
594 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
739 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
595 | #endif |
740 | #endif |
596 | } |
741 | } |
|
|
742 | else |
|
|
743 | default_loop = 0; |
597 | } |
744 | } |
598 | |
745 | |
599 | return method; |
746 | return default_loop; |
600 | } |
747 | } |
601 | |
748 | |
602 | /*****************************************************************************/ |
|
|
603 | |
|
|
604 | void |
749 | void |
605 | ev_fork_prepare (void) |
750 | ev_default_destroy (void) |
606 | { |
751 | { |
607 | /* nop */ |
752 | #if EV_MULTIPLICITY |
608 | } |
753 | struct ev_loop *loop = default_loop; |
609 | |
|
|
610 | void |
|
|
611 | ev_fork_parent (void) |
|
|
612 | { |
|
|
613 | /* nop */ |
|
|
614 | } |
|
|
615 | |
|
|
616 | void |
|
|
617 | ev_fork_child (void) |
|
|
618 | { |
|
|
619 | #if EV_USE_EPOLL |
|
|
620 | if (method == EVMETHOD_EPOLL) |
|
|
621 | epoll_postfork_child (); |
|
|
622 | #endif |
754 | #endif |
623 | |
755 | |
|
|
756 | ev_ref (EV_A); /* child watcher */ |
|
|
757 | ev_signal_stop (EV_A_ &childev); |
|
|
758 | |
|
|
759 | ev_ref (EV_A); /* signal watcher */ |
624 | ev_io_stop (&sigev); |
760 | ev_io_stop (EV_A_ &sigev); |
|
|
761 | |
|
|
762 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
763 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
764 | |
|
|
765 | loop_destroy (EV_A); |
|
|
766 | } |
|
|
767 | |
|
|
768 | void |
|
|
769 | ev_default_fork (void) |
|
|
770 | { |
|
|
771 | #if EV_MULTIPLICITY |
|
|
772 | struct ev_loop *loop = default_loop; |
|
|
773 | #endif |
|
|
774 | |
|
|
775 | loop_fork (EV_A); |
|
|
776 | |
|
|
777 | ev_io_stop (EV_A_ &sigev); |
625 | close (sigpipe [0]); |
778 | close (sigpipe [0]); |
626 | close (sigpipe [1]); |
779 | close (sigpipe [1]); |
627 | pipe (sigpipe); |
780 | pipe (sigpipe); |
|
|
781 | |
|
|
782 | ev_ref (EV_A); /* signal watcher */ |
628 | siginit (); |
783 | siginit (EV_A); |
629 | } |
784 | } |
630 | |
785 | |
631 | /*****************************************************************************/ |
786 | /*****************************************************************************/ |
632 | |
787 | |
633 | static void |
788 | static void |
… | |
… | |
641 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
796 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
642 | |
797 | |
643 | if (p->w) |
798 | if (p->w) |
644 | { |
799 | { |
645 | p->w->pending = 0; |
800 | p->w->pending = 0; |
|
|
801 | |
646 | p->w->cb (EV_A_ p->w, p->events); |
802 | (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); |
647 | } |
803 | } |
648 | } |
804 | } |
649 | } |
805 | } |
650 | |
806 | |
651 | static void |
807 | static void |
652 | timers_reify (EV_P) |
808 | timers_reify (EV_P) |
653 | { |
809 | { |
654 | while (timercnt && timers [0]->at <= mn_now) |
810 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
655 | { |
811 | { |
656 | struct ev_timer *w = timers [0]; |
812 | struct ev_timer *w = timers [0]; |
|
|
813 | |
|
|
814 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
657 | |
815 | |
658 | /* first reschedule or stop timer */ |
816 | /* first reschedule or stop timer */ |
659 | if (w->repeat) |
817 | if (w->repeat) |
660 | { |
818 | { |
661 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
819 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
662 | w->at = mn_now + w->repeat; |
820 | ((WT)w)->at = mn_now + w->repeat; |
663 | downheap ((WT *)timers, timercnt, 0); |
821 | downheap ((WT *)timers, timercnt, 0); |
664 | } |
822 | } |
665 | else |
823 | else |
666 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
824 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
667 | |
825 | |
668 | event ((W)w, EV_TIMEOUT); |
826 | event (EV_A_ (W)w, EV_TIMEOUT); |
669 | } |
827 | } |
670 | } |
828 | } |
671 | |
829 | |
672 | static void |
830 | static void |
673 | periodics_reify (EV_P) |
831 | periodics_reify (EV_P) |
674 | { |
832 | { |
675 | while (periodiccnt && periodics [0]->at <= rt_now) |
833 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
676 | { |
834 | { |
677 | struct ev_periodic *w = periodics [0]; |
835 | struct ev_periodic *w = periodics [0]; |
|
|
836 | |
|
|
837 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
678 | |
838 | |
679 | /* first reschedule or stop timer */ |
839 | /* first reschedule or stop timer */ |
680 | if (w->interval) |
840 | if (w->interval) |
681 | { |
841 | { |
682 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
842 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
683 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
843 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
684 | downheap ((WT *)periodics, periodiccnt, 0); |
844 | downheap ((WT *)periodics, periodiccnt, 0); |
685 | } |
845 | } |
686 | else |
846 | else |
687 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
847 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
688 | |
848 | |
689 | event (EV_A_ (W)w, EV_PERIODIC); |
849 | event (EV_A_ (W)w, EV_PERIODIC); |
690 | } |
850 | } |
691 | } |
851 | } |
692 | |
852 | |
693 | static void |
853 | static void |
694 | periodics_reschedule (EV_P_ ev_tstamp diff) |
854 | periodics_reschedule (EV_P) |
695 | { |
855 | { |
696 | int i; |
856 | int i; |
697 | |
857 | |
698 | /* adjust periodics after time jump */ |
858 | /* adjust periodics after time jump */ |
699 | for (i = 0; i < periodiccnt; ++i) |
859 | for (i = 0; i < periodiccnt; ++i) |
700 | { |
860 | { |
701 | struct ev_periodic *w = periodics [i]; |
861 | struct ev_periodic *w = periodics [i]; |
702 | |
862 | |
703 | if (w->interval) |
863 | if (w->interval) |
704 | { |
864 | { |
705 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
865 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
706 | |
866 | |
707 | if (fabs (diff) >= 1e-4) |
867 | if (fabs (diff) >= 1e-4) |
708 | { |
868 | { |
709 | ev_periodic_stop (EV_A_ w); |
869 | ev_periodic_stop (EV_A_ w); |
710 | ev_periodic_start (EV_A_ w); |
870 | ev_periodic_start (EV_A_ w); |
… | |
… | |
720 | { |
880 | { |
721 | mn_now = get_clock (); |
881 | mn_now = get_clock (); |
722 | |
882 | |
723 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
883 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
724 | { |
884 | { |
725 | rt_now = mn_now + diff; |
885 | rt_now = rtmn_diff + mn_now; |
726 | return 0; |
886 | return 0; |
727 | } |
887 | } |
728 | else |
888 | else |
729 | { |
889 | { |
730 | now_floor = mn_now; |
890 | now_floor = mn_now; |
… | |
… | |
741 | #if EV_USE_MONOTONIC |
901 | #if EV_USE_MONOTONIC |
742 | if (expect_true (have_monotonic)) |
902 | if (expect_true (have_monotonic)) |
743 | { |
903 | { |
744 | if (time_update_monotonic (EV_A)) |
904 | if (time_update_monotonic (EV_A)) |
745 | { |
905 | { |
746 | ev_tstamp odiff = diff; |
906 | ev_tstamp odiff = rtmn_diff; |
747 | |
907 | |
748 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
908 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
749 | { |
909 | { |
750 | diff = rt_now - mn_now; |
910 | rtmn_diff = rt_now - mn_now; |
751 | |
911 | |
752 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
912 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
753 | return; /* all is well */ |
913 | return; /* all is well */ |
754 | |
914 | |
755 | rt_now = ev_time (); |
915 | rt_now = ev_time (); |
756 | mn_now = get_clock (); |
916 | mn_now = get_clock (); |
757 | now_floor = mn_now; |
917 | now_floor = mn_now; |
758 | } |
918 | } |
759 | |
919 | |
760 | periodics_reschedule (EV_A_ diff - odiff); |
920 | periodics_reschedule (EV_A); |
761 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
921 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
922 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
762 | } |
923 | } |
763 | } |
924 | } |
764 | else |
925 | else |
765 | #endif |
926 | #endif |
766 | { |
927 | { |
767 | rt_now = ev_time (); |
928 | rt_now = ev_time (); |
768 | |
929 | |
769 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
930 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
770 | { |
931 | { |
771 | periodics_reschedule (EV_A_ rt_now - mn_now); |
932 | periodics_reschedule (EV_A); |
772 | |
933 | |
773 | /* adjust timers. this is easy, as the offset is the same for all */ |
934 | /* adjust timers. this is easy, as the offset is the same for all */ |
774 | for (i = 0; i < timercnt; ++i) |
935 | for (i = 0; i < timercnt; ++i) |
775 | timers [i]->at += diff; |
936 | ((WT)timers [i])->at += rt_now - mn_now; |
776 | } |
937 | } |
777 | |
938 | |
778 | mn_now = rt_now; |
939 | mn_now = rt_now; |
779 | } |
940 | } |
780 | } |
941 | } |
… | |
… | |
831 | { |
992 | { |
832 | block = MAX_BLOCKTIME; |
993 | block = MAX_BLOCKTIME; |
833 | |
994 | |
834 | if (timercnt) |
995 | if (timercnt) |
835 | { |
996 | { |
836 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
997 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
837 | if (block > to) block = to; |
998 | if (block > to) block = to; |
838 | } |
999 | } |
839 | |
1000 | |
840 | if (periodiccnt) |
1001 | if (periodiccnt) |
841 | { |
1002 | { |
842 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1003 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
843 | if (block > to) block = to; |
1004 | if (block > to) block = to; |
844 | } |
1005 | } |
845 | |
1006 | |
846 | if (block < 0.) block = 0.; |
1007 | if (block < 0.) block = 0.; |
847 | } |
1008 | } |
… | |
… | |
964 | ev_timer_start (EV_P_ struct ev_timer *w) |
1125 | ev_timer_start (EV_P_ struct ev_timer *w) |
965 | { |
1126 | { |
966 | if (ev_is_active (w)) |
1127 | if (ev_is_active (w)) |
967 | return; |
1128 | return; |
968 | |
1129 | |
969 | w->at += mn_now; |
1130 | ((WT)w)->at += mn_now; |
970 | |
1131 | |
971 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1132 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
972 | |
1133 | |
973 | ev_start (EV_A_ (W)w, ++timercnt); |
1134 | ev_start (EV_A_ (W)w, ++timercnt); |
974 | array_needsize (timers, timermax, timercnt, ); |
1135 | array_needsize (timers, timermax, timercnt, ); |
975 | timers [timercnt - 1] = w; |
1136 | timers [timercnt - 1] = w; |
976 | upheap ((WT *)timers, timercnt - 1); |
1137 | upheap ((WT *)timers, timercnt - 1); |
|
|
1138 | |
|
|
1139 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
977 | } |
1140 | } |
978 | |
1141 | |
979 | void |
1142 | void |
980 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1143 | ev_timer_stop (EV_P_ struct ev_timer *w) |
981 | { |
1144 | { |
982 | ev_clear_pending (EV_A_ (W)w); |
1145 | ev_clear_pending (EV_A_ (W)w); |
983 | if (!ev_is_active (w)) |
1146 | if (!ev_is_active (w)) |
984 | return; |
1147 | return; |
985 | |
1148 | |
|
|
1149 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1150 | |
986 | if (w->active < timercnt--) |
1151 | if (((W)w)->active < timercnt--) |
987 | { |
1152 | { |
988 | timers [w->active - 1] = timers [timercnt]; |
1153 | timers [((W)w)->active - 1] = timers [timercnt]; |
989 | downheap ((WT *)timers, timercnt, w->active - 1); |
1154 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
990 | } |
1155 | } |
991 | |
1156 | |
992 | w->at = w->repeat; |
1157 | ((WT)w)->at = w->repeat; |
993 | |
1158 | |
994 | ev_stop (EV_A_ (W)w); |
1159 | ev_stop (EV_A_ (W)w); |
995 | } |
1160 | } |
996 | |
1161 | |
997 | void |
1162 | void |
… | |
… | |
999 | { |
1164 | { |
1000 | if (ev_is_active (w)) |
1165 | if (ev_is_active (w)) |
1001 | { |
1166 | { |
1002 | if (w->repeat) |
1167 | if (w->repeat) |
1003 | { |
1168 | { |
1004 | w->at = mn_now + w->repeat; |
1169 | ((WT)w)->at = mn_now + w->repeat; |
1005 | downheap ((WT *)timers, timercnt, w->active - 1); |
1170 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1006 | } |
1171 | } |
1007 | else |
1172 | else |
1008 | ev_timer_stop (EV_A_ w); |
1173 | ev_timer_stop (EV_A_ w); |
1009 | } |
1174 | } |
1010 | else if (w->repeat) |
1175 | else if (w->repeat) |
… | |
… | |
1019 | |
1184 | |
1020 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1185 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1021 | |
1186 | |
1022 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1187 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1023 | if (w->interval) |
1188 | if (w->interval) |
1024 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1189 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1025 | |
1190 | |
1026 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1191 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1027 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1192 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1028 | periodics [periodiccnt - 1] = w; |
1193 | periodics [periodiccnt - 1] = w; |
1029 | upheap ((WT *)periodics, periodiccnt - 1); |
1194 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1195 | |
|
|
1196 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1030 | } |
1197 | } |
1031 | |
1198 | |
1032 | void |
1199 | void |
1033 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1200 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1034 | { |
1201 | { |
1035 | ev_clear_pending (EV_A_ (W)w); |
1202 | ev_clear_pending (EV_A_ (W)w); |
1036 | if (!ev_is_active (w)) |
1203 | if (!ev_is_active (w)) |
1037 | return; |
1204 | return; |
1038 | |
1205 | |
|
|
1206 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1207 | |
1039 | if (w->active < periodiccnt--) |
1208 | if (((W)w)->active < periodiccnt--) |
1040 | { |
1209 | { |
1041 | periodics [w->active - 1] = periodics [periodiccnt]; |
1210 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1042 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1211 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1043 | } |
1212 | } |
1044 | |
1213 | |
|
|
1214 | ev_stop (EV_A_ (W)w); |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | void |
|
|
1218 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1219 | { |
|
|
1220 | if (ev_is_active (w)) |
|
|
1221 | return; |
|
|
1222 | |
|
|
1223 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1224 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1225 | idles [idlecnt - 1] = w; |
|
|
1226 | } |
|
|
1227 | |
|
|
1228 | void |
|
|
1229 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1230 | { |
|
|
1231 | ev_clear_pending (EV_A_ (W)w); |
|
|
1232 | if (ev_is_active (w)) |
|
|
1233 | return; |
|
|
1234 | |
|
|
1235 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1236 | ev_stop (EV_A_ (W)w); |
|
|
1237 | } |
|
|
1238 | |
|
|
1239 | void |
|
|
1240 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1241 | { |
|
|
1242 | if (ev_is_active (w)) |
|
|
1243 | return; |
|
|
1244 | |
|
|
1245 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1246 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1247 | prepares [preparecnt - 1] = w; |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | void |
|
|
1251 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1252 | { |
|
|
1253 | ev_clear_pending (EV_A_ (W)w); |
|
|
1254 | if (ev_is_active (w)) |
|
|
1255 | return; |
|
|
1256 | |
|
|
1257 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1258 | ev_stop (EV_A_ (W)w); |
|
|
1259 | } |
|
|
1260 | |
|
|
1261 | void |
|
|
1262 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1263 | { |
|
|
1264 | if (ev_is_active (w)) |
|
|
1265 | return; |
|
|
1266 | |
|
|
1267 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1268 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1269 | checks [checkcnt - 1] = w; |
|
|
1270 | } |
|
|
1271 | |
|
|
1272 | void |
|
|
1273 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1274 | { |
|
|
1275 | ev_clear_pending (EV_A_ (W)w); |
|
|
1276 | if (ev_is_active (w)) |
|
|
1277 | return; |
|
|
1278 | |
|
|
1279 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1045 | ev_stop (EV_A_ (W)w); |
1280 | ev_stop (EV_A_ (W)w); |
1046 | } |
1281 | } |
1047 | |
1282 | |
1048 | #ifndef SA_RESTART |
1283 | #ifndef SA_RESTART |
1049 | # define SA_RESTART 0 |
1284 | # define SA_RESTART 0 |
1050 | #endif |
1285 | #endif |
1051 | |
1286 | |
1052 | void |
1287 | void |
1053 | ev_signal_start (EV_P_ struct ev_signal *w) |
1288 | ev_signal_start (EV_P_ struct ev_signal *w) |
1054 | { |
1289 | { |
|
|
1290 | #if EV_MULTIPLICITY |
|
|
1291 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1292 | #endif |
1055 | if (ev_is_active (w)) |
1293 | if (ev_is_active (w)) |
1056 | return; |
1294 | return; |
1057 | |
1295 | |
1058 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1296 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1059 | |
1297 | |
1060 | ev_start (EV_A_ (W)w, 1); |
1298 | ev_start (EV_A_ (W)w, 1); |
1061 | array_needsize (signals, signalmax, w->signum, signals_init); |
1299 | array_needsize (signals, signalmax, w->signum, signals_init); |
1062 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1300 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1063 | |
1301 | |
1064 | if (!w->next) |
1302 | if (!((WL)w)->next) |
1065 | { |
1303 | { |
1066 | struct sigaction sa; |
1304 | struct sigaction sa; |
1067 | sa.sa_handler = sighandler; |
1305 | sa.sa_handler = sighandler; |
1068 | sigfillset (&sa.sa_mask); |
1306 | sigfillset (&sa.sa_mask); |
1069 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1307 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
… | |
… | |
1084 | if (!signals [w->signum - 1].head) |
1322 | if (!signals [w->signum - 1].head) |
1085 | signal (w->signum, SIG_DFL); |
1323 | signal (w->signum, SIG_DFL); |
1086 | } |
1324 | } |
1087 | |
1325 | |
1088 | void |
1326 | void |
1089 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1090 | { |
|
|
1091 | if (ev_is_active (w)) |
|
|
1092 | return; |
|
|
1093 | |
|
|
1094 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1095 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1096 | idles [idlecnt - 1] = w; |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | void |
|
|
1100 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1101 | { |
|
|
1102 | ev_clear_pending (EV_A_ (W)w); |
|
|
1103 | if (ev_is_active (w)) |
|
|
1104 | return; |
|
|
1105 | |
|
|
1106 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1107 | ev_stop (EV_A_ (W)w); |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | void |
|
|
1111 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1112 | { |
|
|
1113 | if (ev_is_active (w)) |
|
|
1114 | return; |
|
|
1115 | |
|
|
1116 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1117 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1118 | prepares [preparecnt - 1] = w; |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | void |
|
|
1122 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1123 | { |
|
|
1124 | ev_clear_pending (EV_A_ (W)w); |
|
|
1125 | if (ev_is_active (w)) |
|
|
1126 | return; |
|
|
1127 | |
|
|
1128 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1129 | ev_stop (EV_A_ (W)w); |
|
|
1130 | } |
|
|
1131 | |
|
|
1132 | void |
|
|
1133 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1134 | { |
|
|
1135 | if (ev_is_active (w)) |
|
|
1136 | return; |
|
|
1137 | |
|
|
1138 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1139 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1140 | checks [checkcnt - 1] = w; |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | void |
|
|
1144 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1145 | { |
|
|
1146 | ev_clear_pending (EV_A_ (W)w); |
|
|
1147 | if (ev_is_active (w)) |
|
|
1148 | return; |
|
|
1149 | |
|
|
1150 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1151 | ev_stop (EV_A_ (W)w); |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | void |
|
|
1155 | ev_child_start (EV_P_ struct ev_child *w) |
1327 | ev_child_start (EV_P_ struct ev_child *w) |
1156 | { |
1328 | { |
|
|
1329 | #if EV_MULTIPLICITY |
|
|
1330 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1331 | #endif |
1157 | if (ev_is_active (w)) |
1332 | if (ev_is_active (w)) |
1158 | return; |
1333 | return; |
1159 | |
1334 | |
1160 | ev_start (EV_A_ (W)w, 1); |
1335 | ev_start (EV_A_ (W)w, 1); |
1161 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1336 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1233 | ev_timer_start (EV_A_ &once->to); |
1408 | ev_timer_start (EV_A_ &once->to); |
1234 | } |
1409 | } |
1235 | } |
1410 | } |
1236 | } |
1411 | } |
1237 | |
1412 | |
1238 | /*****************************************************************************/ |
|
|
1239 | |
|
|
1240 | #if 0 |
|
|
1241 | |
|
|
1242 | struct ev_io wio; |
|
|
1243 | |
|
|
1244 | static void |
|
|
1245 | sin_cb (struct ev_io *w, int revents) |
|
|
1246 | { |
|
|
1247 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | static void |
|
|
1251 | ocb (struct ev_timer *w, int revents) |
|
|
1252 | { |
|
|
1253 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1254 | ev_timer_stop (w); |
|
|
1255 | ev_timer_start (w); |
|
|
1256 | } |
|
|
1257 | |
|
|
1258 | static void |
|
|
1259 | scb (struct ev_signal *w, int revents) |
|
|
1260 | { |
|
|
1261 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1262 | ev_io_stop (&wio); |
|
|
1263 | ev_io_start (&wio); |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | static void |
|
|
1267 | gcb (struct ev_signal *w, int revents) |
|
|
1268 | { |
|
|
1269 | fprintf (stderr, "generic %x\n", revents); |
|
|
1270 | |
|
|
1271 | } |
|
|
1272 | |
|
|
1273 | int main (void) |
|
|
1274 | { |
|
|
1275 | ev_init (0); |
|
|
1276 | |
|
|
1277 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1278 | ev_io_start (&wio); |
|
|
1279 | |
|
|
1280 | struct ev_timer t[10000]; |
|
|
1281 | |
|
|
1282 | #if 0 |
|
|
1283 | int i; |
|
|
1284 | for (i = 0; i < 10000; ++i) |
|
|
1285 | { |
|
|
1286 | struct ev_timer *w = t + i; |
|
|
1287 | ev_watcher_init (w, ocb, i); |
|
|
1288 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1289 | ev_timer_start (w); |
|
|
1290 | if (drand48 () < 0.5) |
|
|
1291 | ev_timer_stop (w); |
|
|
1292 | } |
|
|
1293 | #endif |
|
|
1294 | |
|
|
1295 | struct ev_timer t1; |
|
|
1296 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1297 | ev_timer_start (&t1); |
|
|
1298 | |
|
|
1299 | struct ev_signal sig; |
|
|
1300 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1301 | ev_signal_start (&sig); |
|
|
1302 | |
|
|
1303 | struct ev_check cw; |
|
|
1304 | ev_check_init (&cw, gcb); |
|
|
1305 | ev_check_start (&cw); |
|
|
1306 | |
|
|
1307 | struct ev_idle iw; |
|
|
1308 | ev_idle_init (&iw, gcb); |
|
|
1309 | ev_idle_start (&iw); |
|
|
1310 | |
|
|
1311 | ev_loop (0); |
|
|
1312 | |
|
|
1313 | return 0; |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | #endif |
|
|
1317 | |
|
|
1318 | |
|
|
1319 | |
|
|
1320 | |
|
|