… | |
… | |
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 |
… | |
… | |
202 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
232 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
203 | init (base + cur, newcnt - cur); \ |
233 | init (base + cur, newcnt - cur); \ |
204 | cur = newcnt; \ |
234 | cur = newcnt; \ |
205 | } |
235 | } |
206 | |
236 | |
|
|
237 | #define array_free(stem, idx) \ |
|
|
238 | free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
|
|
239 | |
207 | /*****************************************************************************/ |
240 | /*****************************************************************************/ |
208 | |
241 | |
209 | static void |
242 | static void |
210 | anfds_init (ANFD *base, int count) |
243 | anfds_init (ANFD *base, int count) |
211 | { |
244 | { |
… | |
… | |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
309 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
277 | events |= w->events; |
310 | events |= w->events; |
278 | |
311 | |
279 | anfd->reify = 0; |
312 | anfd->reify = 0; |
280 | |
313 | |
281 | if (anfd->events != events) |
|
|
282 | { |
|
|
283 | method_modify (EV_A_ fd, anfd->events, events); |
314 | method_modify (EV_A_ fd, anfd->events, events); |
284 | anfd->events = events; |
315 | anfd->events = events; |
285 | } |
|
|
286 | } |
316 | } |
287 | |
317 | |
288 | fdchangecnt = 0; |
318 | fdchangecnt = 0; |
289 | } |
319 | } |
290 | |
320 | |
… | |
… | |
327 | |
357 | |
328 | /* 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 */ |
329 | static void |
359 | static void |
330 | fd_enomem (EV_P) |
360 | fd_enomem (EV_P) |
331 | { |
361 | { |
332 | int fd = anfdmax; |
362 | int fd; |
333 | |
363 | |
334 | while (fd--) |
364 | for (fd = anfdmax; fd--; ) |
335 | if (anfds [fd].events) |
365 | if (anfds [fd].events) |
336 | { |
366 | { |
337 | close (fd); |
367 | close (fd); |
338 | fd_kill (EV_A_ fd); |
368 | fd_kill (EV_A_ fd); |
339 | return; |
369 | return; |
… | |
… | |
349 | /* this should be highly optimised to not do anything but set a flag */ |
379 | /* this should be highly optimised to not do anything but set a flag */ |
350 | for (fd = 0; fd < anfdmax; ++fd) |
380 | for (fd = 0; fd < anfdmax; ++fd) |
351 | if (anfds [fd].events) |
381 | if (anfds [fd].events) |
352 | { |
382 | { |
353 | anfds [fd].events = 0; |
383 | anfds [fd].events = 0; |
354 | fd_change (fd); |
384 | fd_change (EV_A_ fd); |
355 | } |
385 | } |
356 | } |
386 | } |
357 | |
387 | |
358 | /*****************************************************************************/ |
388 | /*****************************************************************************/ |
359 | |
389 | |
… | |
… | |
363 | WT w = heap [k]; |
393 | WT w = heap [k]; |
364 | |
394 | |
365 | while (k && heap [k >> 1]->at > w->at) |
395 | while (k && heap [k >> 1]->at > w->at) |
366 | { |
396 | { |
367 | heap [k] = heap [k >> 1]; |
397 | heap [k] = heap [k >> 1]; |
368 | heap [k]->active = k + 1; |
398 | ((W)heap [k])->active = k + 1; |
369 | k >>= 1; |
399 | k >>= 1; |
370 | } |
400 | } |
371 | |
401 | |
372 | heap [k] = w; |
402 | heap [k] = w; |
373 | heap [k]->active = k + 1; |
403 | ((W)heap [k])->active = k + 1; |
374 | |
404 | |
375 | } |
405 | } |
376 | |
406 | |
377 | static void |
407 | static void |
378 | downheap (WT *heap, int N, int k) |
408 | downheap (WT *heap, int N, int k) |
… | |
… | |
388 | |
418 | |
389 | if (w->at <= heap [j]->at) |
419 | if (w->at <= heap [j]->at) |
390 | break; |
420 | break; |
391 | |
421 | |
392 | heap [k] = heap [j]; |
422 | heap [k] = heap [j]; |
393 | heap [k]->active = k + 1; |
423 | ((W)heap [k])->active = k + 1; |
394 | k = j; |
424 | k = j; |
395 | } |
425 | } |
396 | |
426 | |
397 | heap [k] = w; |
427 | heap [k] = w; |
398 | heap [k]->active = k + 1; |
428 | ((W)heap [k])->active = k + 1; |
399 | } |
429 | } |
400 | |
430 | |
401 | /*****************************************************************************/ |
431 | /*****************************************************************************/ |
402 | |
432 | |
403 | typedef struct |
433 | typedef struct |
… | |
… | |
409 | static ANSIG *signals; |
439 | static ANSIG *signals; |
410 | static int signalmax; |
440 | static int signalmax; |
411 | |
441 | |
412 | static int sigpipe [2]; |
442 | static int sigpipe [2]; |
413 | static sig_atomic_t volatile gotsig; |
443 | static sig_atomic_t volatile gotsig; |
|
|
444 | static struct ev_io sigev; |
414 | |
445 | |
415 | static void |
446 | static void |
416 | signals_init (ANSIG *base, int count) |
447 | signals_init (ANSIG *base, int count) |
417 | { |
448 | { |
418 | while (count--) |
449 | while (count--) |
… | |
… | |
476 | |
507 | |
477 | /*****************************************************************************/ |
508 | /*****************************************************************************/ |
478 | |
509 | |
479 | #ifndef WIN32 |
510 | #ifndef WIN32 |
480 | |
511 | |
|
|
512 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
513 | static struct ev_signal childev; |
|
|
514 | |
481 | #ifndef WCONTINUED |
515 | #ifndef WCONTINUED |
482 | # define WCONTINUED 0 |
516 | # define WCONTINUED 0 |
483 | #endif |
517 | #endif |
484 | |
518 | |
485 | static void |
519 | static void |
… | |
… | |
488 | struct ev_child *w; |
522 | struct ev_child *w; |
489 | |
523 | |
490 | 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) |
491 | if (w->pid == pid || !w->pid) |
525 | if (w->pid == pid || !w->pid) |
492 | { |
526 | { |
493 | w->priority = sw->priority; /* need to do it *now* */ |
527 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
494 | w->rpid = pid; |
528 | w->rpid = pid; |
495 | w->rstatus = status; |
529 | w->rstatus = status; |
496 | event (EV_A_ (W)w, EV_CHILD); |
530 | event (EV_A_ (W)w, EV_CHILD); |
497 | } |
531 | } |
498 | } |
532 | } |
499 | |
533 | |
500 | static void |
534 | static void |
… | |
… | |
520 | # include "ev_kqueue.c" |
554 | # include "ev_kqueue.c" |
521 | #endif |
555 | #endif |
522 | #if EV_USE_EPOLL |
556 | #if EV_USE_EPOLL |
523 | # include "ev_epoll.c" |
557 | # include "ev_epoll.c" |
524 | #endif |
558 | #endif |
525 | #if EV_USEV_POLL |
559 | #if EV_USE_POLL |
526 | # include "ev_poll.c" |
560 | # include "ev_poll.c" |
527 | #endif |
561 | #endif |
528 | #if EV_USE_SELECT |
562 | #if EV_USE_SELECT |
529 | # include "ev_select.c" |
563 | # include "ev_select.c" |
530 | #endif |
564 | #endif |
… | |
… | |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
616 | methods = atoi (getenv ("LIBEV_METHODS")); |
583 | else |
617 | else |
584 | methods = EVMETHOD_ANY; |
618 | methods = EVMETHOD_ANY; |
585 | |
619 | |
586 | method = 0; |
620 | method = 0; |
|
|
621 | #if EV_USE_WIN32 |
|
|
622 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
623 | #endif |
587 | #if EV_USE_KQUEUE |
624 | #if EV_USE_KQUEUE |
588 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
625 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
589 | #endif |
626 | #endif |
590 | #if EV_USE_EPOLL |
627 | #if EV_USE_EPOLL |
591 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
628 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
592 | #endif |
629 | #endif |
593 | #if EV_USEV_POLL |
630 | #if EV_USE_POLL |
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
631 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
595 | #endif |
632 | #endif |
596 | #if EV_USE_SELECT |
633 | #if EV_USE_SELECT |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
634 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
598 | #endif |
635 | #endif |
… | |
… | |
600 | } |
637 | } |
601 | |
638 | |
602 | void |
639 | void |
603 | loop_destroy (EV_P) |
640 | loop_destroy (EV_P) |
604 | { |
641 | { |
|
|
642 | int i; |
|
|
643 | |
|
|
644 | #if EV_USE_WIN32 |
|
|
645 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
646 | #endif |
605 | #if EV_USE_KQUEUE |
647 | #if EV_USE_KQUEUE |
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
648 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
607 | #endif |
649 | #endif |
608 | #if EV_USE_EPOLL |
650 | #if EV_USE_EPOLL |
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
651 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
610 | #endif |
652 | #endif |
611 | #if EV_USEV_POLL |
653 | #if EV_USE_POLL |
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
654 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
613 | #endif |
655 | #endif |
614 | #if EV_USE_SELECT |
656 | #if EV_USE_SELECT |
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
657 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
616 | #endif |
658 | #endif |
|
|
659 | |
|
|
660 | for (i = NUMPRI; i--; ) |
|
|
661 | array_free (pending, [i]); |
|
|
662 | |
|
|
663 | array_free (fdchange, ); |
|
|
664 | array_free (timer, ); |
|
|
665 | array_free (periodic, ); |
|
|
666 | array_free (idle, ); |
|
|
667 | array_free (prepare, ); |
|
|
668 | array_free (check, ); |
617 | |
669 | |
618 | method = 0; |
670 | method = 0; |
619 | /*TODO*/ |
671 | /*TODO*/ |
620 | } |
672 | } |
621 | |
673 | |
… | |
… | |
637 | { |
689 | { |
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
690 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
639 | |
691 | |
640 | loop_init (EV_A_ methods); |
692 | loop_init (EV_A_ methods); |
641 | |
693 | |
642 | if (ev_methods (EV_A)) |
694 | if (ev_method (EV_A)) |
643 | return loop; |
695 | return loop; |
644 | |
696 | |
645 | return 0; |
697 | return 0; |
646 | } |
698 | } |
647 | |
699 | |
… | |
… | |
707 | } |
759 | } |
708 | |
760 | |
709 | void |
761 | void |
710 | ev_default_destroy (void) |
762 | ev_default_destroy (void) |
711 | { |
763 | { |
|
|
764 | #if EV_MULTIPLICITY |
712 | struct ev_loop *loop = default_loop; |
765 | struct ev_loop *loop = default_loop; |
|
|
766 | #endif |
713 | |
767 | |
714 | ev_ref (EV_A); /* child watcher */ |
768 | ev_ref (EV_A); /* child watcher */ |
715 | ev_signal_stop (EV_A_ &childev); |
769 | ev_signal_stop (EV_A_ &childev); |
716 | |
770 | |
717 | ev_ref (EV_A); /* signal watcher */ |
771 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
722 | |
776 | |
723 | loop_destroy (EV_A); |
777 | loop_destroy (EV_A); |
724 | } |
778 | } |
725 | |
779 | |
726 | void |
780 | void |
727 | ev_default_fork (EV_P) |
781 | ev_default_fork (void) |
728 | { |
782 | { |
|
|
783 | #if EV_MULTIPLICITY |
|
|
784 | struct ev_loop *loop = default_loop; |
|
|
785 | #endif |
|
|
786 | |
729 | loop_fork (EV_A); |
787 | loop_fork (EV_A); |
730 | |
788 | |
731 | ev_io_stop (EV_A_ &sigev); |
789 | ev_io_stop (EV_A_ &sigev); |
732 | close (sigpipe [0]); |
790 | close (sigpipe [0]); |
733 | close (sigpipe [1]); |
791 | close (sigpipe [1]); |
… | |
… | |
750 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
808 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
751 | |
809 | |
752 | if (p->w) |
810 | if (p->w) |
753 | { |
811 | { |
754 | p->w->pending = 0; |
812 | p->w->pending = 0; |
|
|
813 | |
755 | p->w->cb (EV_A_ p->w, p->events); |
814 | ((void (*)(EV_P_ W, int))p->w->cb) (EV_A_ p->w, p->events); |
756 | } |
815 | } |
757 | } |
816 | } |
758 | } |
817 | } |
759 | |
818 | |
760 | static void |
819 | static void |
761 | timers_reify (EV_P) |
820 | timers_reify (EV_P) |
762 | { |
821 | { |
763 | while (timercnt && timers [0]->at <= mn_now) |
822 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
764 | { |
823 | { |
765 | struct ev_timer *w = timers [0]; |
824 | struct ev_timer *w = timers [0]; |
|
|
825 | |
|
|
826 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
766 | |
827 | |
767 | /* first reschedule or stop timer */ |
828 | /* first reschedule or stop timer */ |
768 | if (w->repeat) |
829 | if (w->repeat) |
769 | { |
830 | { |
770 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
831 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
771 | w->at = mn_now + w->repeat; |
832 | ((WT)w)->at = mn_now + w->repeat; |
772 | downheap ((WT *)timers, timercnt, 0); |
833 | downheap ((WT *)timers, timercnt, 0); |
773 | } |
834 | } |
774 | else |
835 | else |
775 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
836 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
776 | |
837 | |
… | |
… | |
779 | } |
840 | } |
780 | |
841 | |
781 | static void |
842 | static void |
782 | periodics_reify (EV_P) |
843 | periodics_reify (EV_P) |
783 | { |
844 | { |
784 | while (periodiccnt && periodics [0]->at <= rt_now) |
845 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
785 | { |
846 | { |
786 | struct ev_periodic *w = periodics [0]; |
847 | struct ev_periodic *w = periodics [0]; |
|
|
848 | |
|
|
849 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
787 | |
850 | |
788 | /* first reschedule or stop timer */ |
851 | /* first reschedule or stop timer */ |
789 | if (w->interval) |
852 | if (w->interval) |
790 | { |
853 | { |
791 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
854 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
792 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
855 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
793 | downheap ((WT *)periodics, periodiccnt, 0); |
856 | downheap ((WT *)periodics, periodiccnt, 0); |
794 | } |
857 | } |
795 | else |
858 | else |
796 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
859 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
797 | |
860 | |
… | |
… | |
809 | { |
872 | { |
810 | struct ev_periodic *w = periodics [i]; |
873 | struct ev_periodic *w = periodics [i]; |
811 | |
874 | |
812 | if (w->interval) |
875 | if (w->interval) |
813 | { |
876 | { |
814 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
877 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
815 | |
878 | |
816 | if (fabs (diff) >= 1e-4) |
879 | if (fabs (diff) >= 1e-4) |
817 | { |
880 | { |
818 | ev_periodic_stop (EV_A_ w); |
881 | ev_periodic_stop (EV_A_ w); |
819 | ev_periodic_start (EV_A_ w); |
882 | ev_periodic_start (EV_A_ w); |
… | |
… | |
880 | { |
943 | { |
881 | periodics_reschedule (EV_A); |
944 | periodics_reschedule (EV_A); |
882 | |
945 | |
883 | /* adjust timers. this is easy, as the offset is the same for all */ |
946 | /* adjust timers. this is easy, as the offset is the same for all */ |
884 | for (i = 0; i < timercnt; ++i) |
947 | for (i = 0; i < timercnt; ++i) |
885 | timers [i]->at += rt_now - mn_now; |
948 | ((WT)timers [i])->at += rt_now - mn_now; |
886 | } |
949 | } |
887 | |
950 | |
888 | mn_now = rt_now; |
951 | mn_now = rt_now; |
889 | } |
952 | } |
890 | } |
953 | } |
… | |
… | |
941 | { |
1004 | { |
942 | block = MAX_BLOCKTIME; |
1005 | block = MAX_BLOCKTIME; |
943 | |
1006 | |
944 | if (timercnt) |
1007 | if (timercnt) |
945 | { |
1008 | { |
946 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1009 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
947 | if (block > to) block = to; |
1010 | if (block > to) block = to; |
948 | } |
1011 | } |
949 | |
1012 | |
950 | if (periodiccnt) |
1013 | if (periodiccnt) |
951 | { |
1014 | { |
952 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1015 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
953 | if (block > to) block = to; |
1016 | if (block > to) block = to; |
954 | } |
1017 | } |
955 | |
1018 | |
956 | if (block < 0.) block = 0.; |
1019 | if (block < 0.) block = 0.; |
957 | } |
1020 | } |
… | |
… | |
1074 | ev_timer_start (EV_P_ struct ev_timer *w) |
1137 | ev_timer_start (EV_P_ struct ev_timer *w) |
1075 | { |
1138 | { |
1076 | if (ev_is_active (w)) |
1139 | if (ev_is_active (w)) |
1077 | return; |
1140 | return; |
1078 | |
1141 | |
1079 | w->at += mn_now; |
1142 | ((WT)w)->at += mn_now; |
1080 | |
1143 | |
1081 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1144 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1082 | |
1145 | |
1083 | ev_start (EV_A_ (W)w, ++timercnt); |
1146 | ev_start (EV_A_ (W)w, ++timercnt); |
1084 | array_needsize (timers, timermax, timercnt, ); |
1147 | array_needsize (timers, timermax, timercnt, ); |
1085 | timers [timercnt - 1] = w; |
1148 | timers [timercnt - 1] = w; |
1086 | upheap ((WT *)timers, timercnt - 1); |
1149 | upheap ((WT *)timers, timercnt - 1); |
|
|
1150 | |
|
|
1151 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1087 | } |
1152 | } |
1088 | |
1153 | |
1089 | void |
1154 | void |
1090 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1155 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1091 | { |
1156 | { |
1092 | ev_clear_pending (EV_A_ (W)w); |
1157 | ev_clear_pending (EV_A_ (W)w); |
1093 | if (!ev_is_active (w)) |
1158 | if (!ev_is_active (w)) |
1094 | return; |
1159 | return; |
1095 | |
1160 | |
|
|
1161 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1162 | |
1096 | if (w->active < timercnt--) |
1163 | if (((W)w)->active < timercnt--) |
1097 | { |
1164 | { |
1098 | timers [w->active - 1] = timers [timercnt]; |
1165 | timers [((W)w)->active - 1] = timers [timercnt]; |
1099 | downheap ((WT *)timers, timercnt, w->active - 1); |
1166 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1100 | } |
1167 | } |
1101 | |
1168 | |
1102 | w->at = w->repeat; |
1169 | ((WT)w)->at = w->repeat; |
1103 | |
1170 | |
1104 | ev_stop (EV_A_ (W)w); |
1171 | ev_stop (EV_A_ (W)w); |
1105 | } |
1172 | } |
1106 | |
1173 | |
1107 | void |
1174 | void |
… | |
… | |
1109 | { |
1176 | { |
1110 | if (ev_is_active (w)) |
1177 | if (ev_is_active (w)) |
1111 | { |
1178 | { |
1112 | if (w->repeat) |
1179 | if (w->repeat) |
1113 | { |
1180 | { |
1114 | w->at = mn_now + w->repeat; |
1181 | ((WT)w)->at = mn_now + w->repeat; |
1115 | downheap ((WT *)timers, timercnt, w->active - 1); |
1182 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1116 | } |
1183 | } |
1117 | else |
1184 | else |
1118 | ev_timer_stop (EV_A_ w); |
1185 | ev_timer_stop (EV_A_ w); |
1119 | } |
1186 | } |
1120 | else if (w->repeat) |
1187 | else if (w->repeat) |
… | |
… | |
1129 | |
1196 | |
1130 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1197 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1131 | |
1198 | |
1132 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1199 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1133 | if (w->interval) |
1200 | if (w->interval) |
1134 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1201 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1135 | |
1202 | |
1136 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1203 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1137 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1204 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1138 | periodics [periodiccnt - 1] = w; |
1205 | periodics [periodiccnt - 1] = w; |
1139 | upheap ((WT *)periodics, periodiccnt - 1); |
1206 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1207 | |
|
|
1208 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1140 | } |
1209 | } |
1141 | |
1210 | |
1142 | void |
1211 | void |
1143 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1212 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1144 | { |
1213 | { |
1145 | ev_clear_pending (EV_A_ (W)w); |
1214 | ev_clear_pending (EV_A_ (W)w); |
1146 | if (!ev_is_active (w)) |
1215 | if (!ev_is_active (w)) |
1147 | return; |
1216 | return; |
1148 | |
1217 | |
|
|
1218 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1219 | |
1149 | if (w->active < periodiccnt--) |
1220 | if (((W)w)->active < periodiccnt--) |
1150 | { |
1221 | { |
1151 | periodics [w->active - 1] = periodics [periodiccnt]; |
1222 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1152 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1223 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1153 | } |
1224 | } |
1154 | |
1225 | |
1155 | ev_stop (EV_A_ (W)w); |
1226 | ev_stop (EV_A_ (W)w); |
1156 | } |
1227 | } |
1157 | |
1228 | |
… | |
… | |
1171 | { |
1242 | { |
1172 | ev_clear_pending (EV_A_ (W)w); |
1243 | ev_clear_pending (EV_A_ (W)w); |
1173 | if (ev_is_active (w)) |
1244 | if (ev_is_active (w)) |
1174 | return; |
1245 | return; |
1175 | |
1246 | |
1176 | idles [w->active - 1] = idles [--idlecnt]; |
1247 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1177 | ev_stop (EV_A_ (W)w); |
1248 | ev_stop (EV_A_ (W)w); |
1178 | } |
1249 | } |
1179 | |
1250 | |
1180 | void |
1251 | void |
1181 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1252 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1193 | { |
1264 | { |
1194 | ev_clear_pending (EV_A_ (W)w); |
1265 | ev_clear_pending (EV_A_ (W)w); |
1195 | if (ev_is_active (w)) |
1266 | if (ev_is_active (w)) |
1196 | return; |
1267 | return; |
1197 | |
1268 | |
1198 | prepares [w->active - 1] = prepares [--preparecnt]; |
1269 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1199 | ev_stop (EV_A_ (W)w); |
1270 | ev_stop (EV_A_ (W)w); |
1200 | } |
1271 | } |
1201 | |
1272 | |
1202 | void |
1273 | void |
1203 | ev_check_start (EV_P_ struct ev_check *w) |
1274 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1215 | { |
1286 | { |
1216 | ev_clear_pending (EV_A_ (W)w); |
1287 | ev_clear_pending (EV_A_ (W)w); |
1217 | if (ev_is_active (w)) |
1288 | if (ev_is_active (w)) |
1218 | return; |
1289 | return; |
1219 | |
1290 | |
1220 | checks [w->active - 1] = checks [--checkcnt]; |
1291 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1221 | ev_stop (EV_A_ (W)w); |
1292 | ev_stop (EV_A_ (W)w); |
1222 | } |
1293 | } |
1223 | |
1294 | |
1224 | #ifndef SA_RESTART |
1295 | #ifndef SA_RESTART |
1225 | # define SA_RESTART 0 |
1296 | # define SA_RESTART 0 |
… | |
… | |
1238 | |
1309 | |
1239 | ev_start (EV_A_ (W)w, 1); |
1310 | ev_start (EV_A_ (W)w, 1); |
1240 | array_needsize (signals, signalmax, w->signum, signals_init); |
1311 | array_needsize (signals, signalmax, w->signum, signals_init); |
1241 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1312 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1242 | |
1313 | |
1243 | if (!w->next) |
1314 | if (!((WL)w)->next) |
1244 | { |
1315 | { |
1245 | struct sigaction sa; |
1316 | struct sigaction sa; |
1246 | sa.sa_handler = sighandler; |
1317 | sa.sa_handler = sighandler; |
1247 | sigfillset (&sa.sa_mask); |
1318 | sigfillset (&sa.sa_mask); |
1248 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1319 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |