ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.124 by root, Sat Nov 17 02:26:24 2007 UTC vs.
Revision 1.136 by root, Sat Nov 24 07:14:26 2007 UTC

32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
34#endif 34#endif
35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
37# include "config.h" 40# include "config.h"
41# endif
38 42
39# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC 44# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
42# endif 46# endif
43# ifndef EV_USE_REALTIME 47# ifndef EV_USE_REALTIME
44# define EV_USE_REALTIME 1 48# define EV_USE_REALTIME 1
45# endif 49# endif
50# else
51# ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 0
53# endif
54# ifndef EV_USE_REALTIME
55# define EV_USE_REALTIME 0
56# endif
46# endif 57# endif
47 58
48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 59# ifndef EV_USE_SELECT
60# if HAVE_SELECT && HAVE_SYS_SELECT_H
49# define EV_USE_SELECT 1 61# define EV_USE_SELECT 1
62# else
63# define EV_USE_SELECT 0
64# endif
50# endif 65# endif
51 66
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 67# ifndef EV_USE_POLL
68# if HAVE_POLL && HAVE_POLL_H
53# define EV_USE_POLL 1 69# define EV_USE_POLL 1
70# else
71# define EV_USE_POLL 0
72# endif
54# endif 73# endif
55 74
56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 75# ifndef EV_USE_EPOLL
76# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
57# define EV_USE_EPOLL 1 77# define EV_USE_EPOLL 1
78# else
79# define EV_USE_EPOLL 0
80# endif
58# endif 81# endif
59 82
83# ifndef EV_USE_KQUEUE
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 84# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
61# define EV_USE_KQUEUE 1 85# define EV_USE_KQUEUE 1
86# else
87# define EV_USE_KQUEUE 0
88# endif
62# endif 89# endif
63 90
64# if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT) 91# ifndef EV_USE_PORT
92# if HAVE_PORT_H && HAVE_PORT_CREATE
65# define EV_USE_PORT 1 93# define EV_USE_PORT 1
94# else
95# define EV_USE_PORT 0
96# endif
66# endif 97# endif
67 98
68#endif 99#endif
69 100
70#include <math.h> 101#include <math.h>
127# define EV_USE_PORT 0 158# define EV_USE_PORT 0
128#endif 159#endif
129 160
130/**/ 161/**/
131 162
132/* darwin simply cannot be helped */
133#ifdef __APPLE__
134# undef EV_USE_POLL
135# undef EV_USE_KQUEUE
136#endif
137
138#ifndef CLOCK_MONOTONIC 163#ifndef CLOCK_MONOTONIC
139# undef EV_USE_MONOTONIC 164# undef EV_USE_MONOTONIC
140# define EV_USE_MONOTONIC 0 165# define EV_USE_MONOTONIC 0
141#endif 166#endif
142 167
177#define ABSPRI(w) ((w)->priority - EV_MINPRI) 202#define ABSPRI(w) ((w)->priority - EV_MINPRI)
178 203
179#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
180#define EMPTY2(a,b) /* used to suppress some warnings */ 205#define EMPTY2(a,b) /* used to suppress some warnings */
181 206
182typedef struct ev_watcher *W; 207typedef ev_watcher *W;
183typedef struct ev_watcher_list *WL; 208typedef ev_watcher_list *WL;
184typedef struct ev_watcher_time *WT; 209typedef ev_watcher_time *WT;
185 210
186static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
187 212
188#ifdef _WIN32 213#ifdef _WIN32
189# include "ev_win32.c" 214# include "ev_win32.c"
391 416
392inline void 417inline void
393fd_event (EV_P_ int fd, int revents) 418fd_event (EV_P_ int fd, int revents)
394{ 419{
395 ANFD *anfd = anfds + fd; 420 ANFD *anfd = anfds + fd;
396 struct ev_io *w; 421 ev_io *w;
397 422
398 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 423 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
399 { 424 {
400 int ev = w->events & revents; 425 int ev = w->events & revents;
401 426
402 if (ev) 427 if (ev)
403 ev_feed_event (EV_A_ (W)w, ev); 428 ev_feed_event (EV_A_ (W)w, ev);
419 444
420 for (i = 0; i < fdchangecnt; ++i) 445 for (i = 0; i < fdchangecnt; ++i)
421 { 446 {
422 int fd = fdchanges [i]; 447 int fd = fdchanges [i];
423 ANFD *anfd = anfds + fd; 448 ANFD *anfd = anfds + fd;
424 struct ev_io *w; 449 ev_io *w;
425 450
426 int events = 0; 451 int events = 0;
427 452
428 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 453 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
429 events |= w->events; 454 events |= w->events;
430 455
431#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
432 if (events) 457 if (events)
433 { 458 {
437 } 462 }
438#endif 463#endif
439 464
440 anfd->reify = 0; 465 anfd->reify = 0;
441 466
442 method_modify (EV_A_ fd, anfd->events, events); 467 backend_modify (EV_A_ fd, anfd->events, events);
443 anfd->events = events; 468 anfd->events = events;
444 } 469 }
445 470
446 fdchangecnt = 0; 471 fdchangecnt = 0;
447} 472}
460} 485}
461 486
462static void 487static void
463fd_kill (EV_P_ int fd) 488fd_kill (EV_P_ int fd)
464{ 489{
465 struct ev_io *w; 490 ev_io *w;
466 491
467 while ((w = (struct ev_io *)anfds [fd].head)) 492 while ((w = (ev_io *)anfds [fd].head))
468 { 493 {
469 ev_io_stop (EV_A_ w); 494 ev_io_stop (EV_A_ w);
470 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 495 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
471 } 496 }
472} 497}
505 fd_kill (EV_A_ fd); 530 fd_kill (EV_A_ fd);
506 return; 531 return;
507 } 532 }
508} 533}
509 534
510/* usually called after fork if method needs to re-arm all fds from scratch */ 535/* usually called after fork if backend needs to re-arm all fds from scratch */
511static void 536static void
512fd_rearm_all (EV_P) 537fd_rearm_all (EV_P)
513{ 538{
514 int fd; 539 int fd;
515 540
583static ANSIG *signals; 608static ANSIG *signals;
584static int signalmax; 609static int signalmax;
585 610
586static int sigpipe [2]; 611static int sigpipe [2];
587static sig_atomic_t volatile gotsig; 612static sig_atomic_t volatile gotsig;
588static struct ev_io sigev; 613static ev_io sigev;
589 614
590static void 615static void
591signals_init (ANSIG *base, int count) 616signals_init (ANSIG *base, int count)
592{ 617{
593 while (count--) 618 while (count--)
636 for (w = signals [signum].head; w; w = w->next) 661 for (w = signals [signum].head; w; w = w->next)
637 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
638} 663}
639 664
640static void 665static void
641sigcb (EV_P_ struct ev_io *iow, int revents) 666sigcb (EV_P_ ev_io *iow, int revents)
642{ 667{
643 int signum; 668 int signum;
644 669
645 read (sigpipe [0], &revents, 1); 670 read (sigpipe [0], &revents, 1);
646 gotsig = 0; 671 gotsig = 0;
673 ev_unref (EV_A); /* child watcher should not keep loop alive */ 698 ev_unref (EV_A); /* child watcher should not keep loop alive */
674} 699}
675 700
676/*****************************************************************************/ 701/*****************************************************************************/
677 702
678static struct ev_child *childs [PID_HASHSIZE]; 703static ev_child *childs [PID_HASHSIZE];
679 704
680#ifndef _WIN32 705#ifndef _WIN32
681 706
682static struct ev_signal childev; 707static ev_signal childev;
683 708
684#ifndef WCONTINUED 709#ifndef WCONTINUED
685# define WCONTINUED 0 710# define WCONTINUED 0
686#endif 711#endif
687 712
688static void 713static void
689child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 714child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
690{ 715{
691 struct ev_child *w; 716 ev_child *w;
692 717
693 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 718 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
694 if (w->pid == pid || !w->pid) 719 if (w->pid == pid || !w->pid)
695 { 720 {
696 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
697 w->rpid = pid; 722 w->rpid = pid;
698 w->rstatus = status; 723 w->rstatus = status;
699 ev_feed_event (EV_A_ (W)w, EV_CHILD); 724 ev_feed_event (EV_A_ (W)w, EV_CHILD);
700 } 725 }
701} 726}
702 727
703static void 728static void
704childcb (EV_P_ struct ev_signal *sw, int revents) 729childcb (EV_P_ ev_signal *sw, int revents)
705{ 730{
706 int pid, status; 731 int pid, status;
707 732
708 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
709 { 734 {
710 /* make sure we are called again until all childs have been reaped */ 735 /* make sure we are called again until all childs have been reaped */
736 /* we need to do it this way so that the callback gets called before we continue */
711 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 737 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
712 738
713 child_reap (EV_A_ sw, pid, pid, status); 739 child_reap (EV_A_ sw, pid, pid, status);
714 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 740 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
715 } 741 }
716} 742}
717 743
718#endif 744#endif
719 745
758 || getgid () != getegid (); 784 || getgid () != getegid ();
759#endif 785#endif
760} 786}
761 787
762unsigned int 788unsigned int
763ev_method (EV_P) 789ev_supported_backends (void)
764{ 790{
765 return method; 791 unsigned int flags = 0;
792
793 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
794 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
795 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
796 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
797 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
798
799 return flags;
800}
801
802unsigned int
803ev_recommended_backends (void)
804{
805 unsigned int flags = ev_supported_backends ();
806
807#ifndef __NetBSD__
808 /* kqueue is borked on everything but netbsd apparently */
809 /* it usually doesn't work correctly on anything but sockets and pipes */
810 flags &= ~EVBACKEND_KQUEUE;
811#endif
812#ifdef __APPLE__
813 // flags &= ~EVBACKEND_KQUEUE; for documentation
814 flags &= ~EVBACKEND_POLL;
815#endif
816
817 return flags;
818}
819
820unsigned int
821ev_embeddable_backends (void)
822{
823 return EVBACKEND_EPOLL
824 | EVBACKEND_KQUEUE
825 | EVBACKEND_PORT;
826}
827
828unsigned int
829ev_backend (EV_P)
830{
831 return backend;
766} 832}
767 833
768static void 834static void
769loop_init (EV_P_ unsigned int flags) 835loop_init (EV_P_ unsigned int flags)
770{ 836{
771 if (!method) 837 if (!backend)
772 { 838 {
773#if EV_USE_MONOTONIC 839#if EV_USE_MONOTONIC
774 { 840 {
775 struct timespec ts; 841 struct timespec ts;
776 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 842 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
781 ev_rt_now = ev_time (); 847 ev_rt_now = ev_time ();
782 mn_now = get_clock (); 848 mn_now = get_clock ();
783 now_floor = mn_now; 849 now_floor = mn_now;
784 rtmn_diff = ev_rt_now - mn_now; 850 rtmn_diff = ev_rt_now - mn_now;
785 851
786 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) 852 if (!(flags & EVFLAG_NOENV)
853 && !enable_secure ()
854 && getenv ("LIBEV_FLAGS"))
787 flags = atoi (getenv ("LIBEV_FLAGS")); 855 flags = atoi (getenv ("LIBEV_FLAGS"));
788 856
789 if (!(flags & 0x0000ffff)) 857 if (!(flags & 0x0000ffffUL))
790 flags |= 0x0000ffff; 858 flags |= ev_recommended_backends ();
791 859
792 method = 0; 860 backend = 0;
793#if EV_USE_PORT 861#if EV_USE_PORT
794 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); 862 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
795#endif 863#endif
796#if EV_USE_KQUEUE 864#if EV_USE_KQUEUE
797 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 865 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
798#endif 866#endif
799#if EV_USE_EPOLL 867#if EV_USE_EPOLL
800 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 868 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
801#endif 869#endif
802#if EV_USE_POLL 870#if EV_USE_POLL
803 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); 871 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
804#endif 872#endif
805#if EV_USE_SELECT 873#if EV_USE_SELECT
806 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); 874 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
807#endif 875#endif
808 876
809 ev_init (&sigev, sigcb); 877 ev_init (&sigev, sigcb);
810 ev_set_priority (&sigev, EV_MAXPRI); 878 ev_set_priority (&sigev, EV_MAXPRI);
811 } 879 }
815loop_destroy (EV_P) 883loop_destroy (EV_P)
816{ 884{
817 int i; 885 int i;
818 886
819#if EV_USE_PORT 887#if EV_USE_PORT
820 if (method == EVMETHOD_PORT ) port_destroy (EV_A); 888 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
821#endif 889#endif
822#if EV_USE_KQUEUE 890#if EV_USE_KQUEUE
823 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 891 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
824#endif 892#endif
825#if EV_USE_EPOLL 893#if EV_USE_EPOLL
826 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 894 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
827#endif 895#endif
828#if EV_USE_POLL 896#if EV_USE_POLL
829 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 897 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
830#endif 898#endif
831#if EV_USE_SELECT 899#if EV_USE_SELECT
832 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 900 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
833#endif 901#endif
834 902
835 for (i = NUMPRI; i--; ) 903 for (i = NUMPRI; i--; )
836 array_free (pending, [i]); 904 array_free (pending, [i]);
837 905
843#endif 911#endif
844 array_free (idle, EMPTY0); 912 array_free (idle, EMPTY0);
845 array_free (prepare, EMPTY0); 913 array_free (prepare, EMPTY0);
846 array_free (check, EMPTY0); 914 array_free (check, EMPTY0);
847 915
848 method = 0; 916 backend = 0;
849} 917}
850 918
851static void 919static void
852loop_fork (EV_P) 920loop_fork (EV_P)
853{ 921{
854#if EV_USE_PORT 922#if EV_USE_PORT
855 if (method == EVMETHOD_PORT ) port_fork (EV_A); 923 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
856#endif 924#endif
857#if EV_USE_KQUEUE 925#if EV_USE_KQUEUE
858 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 926 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
859#endif 927#endif
860#if EV_USE_EPOLL 928#if EV_USE_EPOLL
861 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 929 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
862#endif 930#endif
863 931
864 if (ev_is_active (&sigev)) 932 if (ev_is_active (&sigev))
865 { 933 {
866 /* default loop */ 934 /* default loop */
887 955
888 memset (loop, 0, sizeof (struct ev_loop)); 956 memset (loop, 0, sizeof (struct ev_loop));
889 957
890 loop_init (EV_A_ flags); 958 loop_init (EV_A_ flags);
891 959
892 if (ev_method (EV_A)) 960 if (ev_backend (EV_A))
893 return loop; 961 return loop;
894 962
895 return 0; 963 return 0;
896} 964}
897 965
910 978
911#endif 979#endif
912 980
913#if EV_MULTIPLICITY 981#if EV_MULTIPLICITY
914struct ev_loop * 982struct ev_loop *
915ev_default_loop_ (unsigned int flags) 983ev_default_loop_init (unsigned int flags)
916#else 984#else
917int 985int
918ev_default_loop (unsigned int flags) 986ev_default_loop (unsigned int flags)
919#endif 987#endif
920{ 988{
930 ev_default_loop_ptr = 1; 998 ev_default_loop_ptr = 1;
931#endif 999#endif
932 1000
933 loop_init (EV_A_ flags); 1001 loop_init (EV_A_ flags);
934 1002
935 if (ev_method (EV_A)) 1003 if (ev_backend (EV_A))
936 { 1004 {
937 siginit (EV_A); 1005 siginit (EV_A);
938 1006
939#ifndef _WIN32 1007#ifndef _WIN32
940 ev_signal_init (&childev, childcb, SIGCHLD); 1008 ev_signal_init (&childev, childcb, SIGCHLD);
976{ 1044{
977#if EV_MULTIPLICITY 1045#if EV_MULTIPLICITY
978 struct ev_loop *loop = ev_default_loop_ptr; 1046 struct ev_loop *loop = ev_default_loop_ptr;
979#endif 1047#endif
980 1048
981 if (method) 1049 if (backend)
982 postfork = 1; 1050 postfork = 1;
983} 1051}
984 1052
985/*****************************************************************************/ 1053/*****************************************************************************/
986 1054
1017inline void 1085inline void
1018timers_reify (EV_P) 1086timers_reify (EV_P)
1019{ 1087{
1020 while (timercnt && ((WT)timers [0])->at <= mn_now) 1088 while (timercnt && ((WT)timers [0])->at <= mn_now)
1021 { 1089 {
1022 struct ev_timer *w = timers [0]; 1090 ev_timer *w = timers [0];
1023 1091
1024 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1092 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1025 1093
1026 /* first reschedule or stop timer */ 1094 /* first reschedule or stop timer */
1027 if (w->repeat) 1095 if (w->repeat)
1045inline void 1113inline void
1046periodics_reify (EV_P) 1114periodics_reify (EV_P)
1047{ 1115{
1048 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1116 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1049 { 1117 {
1050 struct ev_periodic *w = periodics [0]; 1118 ev_periodic *w = periodics [0];
1051 1119
1052 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1120 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1053 1121
1054 /* first reschedule or stop timer */ 1122 /* first reschedule or stop timer */
1055 if (w->reschedule_cb) 1123 if (w->reschedule_cb)
1077 int i; 1145 int i;
1078 1146
1079 /* adjust periodics after time jump */ 1147 /* adjust periodics after time jump */
1080 for (i = 0; i < periodiccnt; ++i) 1148 for (i = 0; i < periodiccnt; ++i)
1081 { 1149 {
1082 struct ev_periodic *w = periodics [i]; 1150 ev_periodic *w = periodics [i];
1083 1151
1084 if (w->reschedule_cb) 1152 if (w->reschedule_cb)
1085 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1153 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1086 else if (w->interval) 1154 else if (w->interval)
1087 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1155 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1177static int loop_done; 1245static int loop_done;
1178 1246
1179void 1247void
1180ev_loop (EV_P_ int flags) 1248ev_loop (EV_P_ int flags)
1181{ 1249{
1182 double block;
1183 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1250 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1251 ? EVUNLOOP_ONE
1252 : EVUNLOOP_CANCEL;
1184 1253
1185 while (activecnt) 1254 while (activecnt)
1186 { 1255 {
1187 /* queue check watchers (and execute them) */ 1256 /* queue check watchers (and execute them) */
1188 if (expect_false (preparecnt)) 1257 if (expect_false (preparecnt))
1197 1266
1198 /* update fd-related kernel structures */ 1267 /* update fd-related kernel structures */
1199 fd_reify (EV_A); 1268 fd_reify (EV_A);
1200 1269
1201 /* calculate blocking time */ 1270 /* calculate blocking time */
1271 {
1272 double block;
1202 1273
1203 /* we only need this for !monotonic clock or timers, but as we basically 1274 if (flags & EVLOOP_NONBLOCK || idlecnt)
1204 always have timers, we just calculate it always */ 1275 block = 0.; /* do not block at all */
1276 else
1277 {
1278 /* update time to cancel out callback processing overhead */
1205#if EV_USE_MONOTONIC 1279#if EV_USE_MONOTONIC
1206 if (expect_true (have_monotonic)) 1280 if (expect_true (have_monotonic))
1207 time_update_monotonic (EV_A); 1281 time_update_monotonic (EV_A);
1208 else 1282 else
1209#endif 1283#endif
1210 { 1284 {
1211 ev_rt_now = ev_time (); 1285 ev_rt_now = ev_time ();
1212 mn_now = ev_rt_now; 1286 mn_now = ev_rt_now;
1213 } 1287 }
1214 1288
1215 if (flags & EVLOOP_NONBLOCK || idlecnt)
1216 block = 0.;
1217 else
1218 {
1219 block = MAX_BLOCKTIME; 1289 block = MAX_BLOCKTIME;
1220 1290
1221 if (timercnt) 1291 if (timercnt)
1222 { 1292 {
1223 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1293 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1224 if (block > to) block = to; 1294 if (block > to) block = to;
1225 } 1295 }
1226 1296
1227#if EV_PERIODICS 1297#if EV_PERIODICS
1228 if (periodiccnt) 1298 if (periodiccnt)
1229 { 1299 {
1230 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1300 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1231 if (block > to) block = to; 1301 if (block > to) block = to;
1232 } 1302 }
1233#endif 1303#endif
1234 1304
1235 if (expect_false (block < 0.)) block = 0.; 1305 if (expect_false (block < 0.)) block = 0.;
1236 } 1306 }
1237 1307
1238 method_poll (EV_A_ block); 1308 backend_poll (EV_A_ block);
1309 }
1239 1310
1240 /* update ev_rt_now, do magic */ 1311 /* update ev_rt_now, do magic */
1241 time_update (EV_A); 1312 time_update (EV_A);
1242 1313
1243 /* queue pending timers and reschedule them */ 1314 /* queue pending timers and reschedule them */
1258 1329
1259 if (expect_false (loop_done)) 1330 if (expect_false (loop_done))
1260 break; 1331 break;
1261 } 1332 }
1262 1333
1263 if (loop_done != 2) 1334 if (loop_done == EVUNLOOP_ONE)
1264 loop_done = 0; 1335 loop_done = EVUNLOOP_CANCEL;
1265} 1336}
1266 1337
1267void 1338void
1268ev_unloop (EV_P_ int how) 1339ev_unloop (EV_P_ int how)
1269{ 1340{
1322} 1393}
1323 1394
1324/*****************************************************************************/ 1395/*****************************************************************************/
1325 1396
1326void 1397void
1327ev_io_start (EV_P_ struct ev_io *w) 1398ev_io_start (EV_P_ ev_io *w)
1328{ 1399{
1329 int fd = w->fd; 1400 int fd = w->fd;
1330 1401
1331 if (expect_false (ev_is_active (w))) 1402 if (expect_false (ev_is_active (w)))
1332 return; 1403 return;
1339 1410
1340 fd_change (EV_A_ fd); 1411 fd_change (EV_A_ fd);
1341} 1412}
1342 1413
1343void 1414void
1344ev_io_stop (EV_P_ struct ev_io *w) 1415ev_io_stop (EV_P_ ev_io *w)
1345{ 1416{
1346 ev_clear_pending (EV_A_ (W)w); 1417 ev_clear_pending (EV_A_ (W)w);
1347 if (expect_false (!ev_is_active (w))) 1418 if (expect_false (!ev_is_active (w)))
1348 return; 1419 return;
1349 1420
1354 1425
1355 fd_change (EV_A_ w->fd); 1426 fd_change (EV_A_ w->fd);
1356} 1427}
1357 1428
1358void 1429void
1359ev_timer_start (EV_P_ struct ev_timer *w) 1430ev_timer_start (EV_P_ ev_timer *w)
1360{ 1431{
1361 if (expect_false (ev_is_active (w))) 1432 if (expect_false (ev_is_active (w)))
1362 return; 1433 return;
1363 1434
1364 ((WT)w)->at += mn_now; 1435 ((WT)w)->at += mn_now;
1365 1436
1366 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1437 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1367 1438
1368 ev_start (EV_A_ (W)w, ++timercnt); 1439 ev_start (EV_A_ (W)w, ++timercnt);
1369 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1440 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1370 timers [timercnt - 1] = w; 1441 timers [timercnt - 1] = w;
1371 upheap ((WT *)timers, timercnt - 1); 1442 upheap ((WT *)timers, timercnt - 1);
1372 1443
1373 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1374} 1445}
1375 1446
1376void 1447void
1377ev_timer_stop (EV_P_ struct ev_timer *w) 1448ev_timer_stop (EV_P_ ev_timer *w)
1378{ 1449{
1379 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1380 if (expect_false (!ev_is_active (w))) 1451 if (expect_false (!ev_is_active (w)))
1381 return; 1452 return;
1382 1453
1392 1463
1393 ev_stop (EV_A_ (W)w); 1464 ev_stop (EV_A_ (W)w);
1394} 1465}
1395 1466
1396void 1467void
1397ev_timer_again (EV_P_ struct ev_timer *w) 1468ev_timer_again (EV_P_ ev_timer *w)
1398{ 1469{
1399 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1400 { 1471 {
1401 if (w->repeat) 1472 if (w->repeat)
1402 { 1473 {
1413 } 1484 }
1414} 1485}
1415 1486
1416#if EV_PERIODICS 1487#if EV_PERIODICS
1417void 1488void
1418ev_periodic_start (EV_P_ struct ev_periodic *w) 1489ev_periodic_start (EV_P_ ev_periodic *w)
1419{ 1490{
1420 if (expect_false (ev_is_active (w))) 1491 if (expect_false (ev_is_active (w)))
1421 return; 1492 return;
1422 1493
1423 if (w->reschedule_cb) 1494 if (w->reschedule_cb)
1428 /* this formula differs from the one in periodic_reify because we do not always round up */ 1499 /* this formula differs from the one in periodic_reify because we do not always round up */
1429 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1500 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1430 } 1501 }
1431 1502
1432 ev_start (EV_A_ (W)w, ++periodiccnt); 1503 ev_start (EV_A_ (W)w, ++periodiccnt);
1433 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1504 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1434 periodics [periodiccnt - 1] = w; 1505 periodics [periodiccnt - 1] = w;
1435 upheap ((WT *)periodics, periodiccnt - 1); 1506 upheap ((WT *)periodics, periodiccnt - 1);
1436 1507
1437 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1438} 1509}
1439 1510
1440void 1511void
1441ev_periodic_stop (EV_P_ struct ev_periodic *w) 1512ev_periodic_stop (EV_P_ ev_periodic *w)
1442{ 1513{
1443 ev_clear_pending (EV_A_ (W)w); 1514 ev_clear_pending (EV_A_ (W)w);
1444 if (expect_false (!ev_is_active (w))) 1515 if (expect_false (!ev_is_active (w)))
1445 return; 1516 return;
1446 1517
1454 1525
1455 ev_stop (EV_A_ (W)w); 1526 ev_stop (EV_A_ (W)w);
1456} 1527}
1457 1528
1458void 1529void
1459ev_periodic_again (EV_P_ struct ev_periodic *w) 1530ev_periodic_again (EV_P_ ev_periodic *w)
1460{ 1531{
1461 /* TODO: use adjustheap and recalculation */ 1532 /* TODO: use adjustheap and recalculation */
1462 ev_periodic_stop (EV_A_ w); 1533 ev_periodic_stop (EV_A_ w);
1463 ev_periodic_start (EV_A_ w); 1534 ev_periodic_start (EV_A_ w);
1464} 1535}
1465#endif 1536#endif
1466 1537
1467void 1538void
1468ev_idle_start (EV_P_ struct ev_idle *w) 1539ev_idle_start (EV_P_ ev_idle *w)
1469{ 1540{
1470 if (expect_false (ev_is_active (w))) 1541 if (expect_false (ev_is_active (w)))
1471 return; 1542 return;
1472 1543
1473 ev_start (EV_A_ (W)w, ++idlecnt); 1544 ev_start (EV_A_ (W)w, ++idlecnt);
1474 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1545 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1475 idles [idlecnt - 1] = w; 1546 idles [idlecnt - 1] = w;
1476} 1547}
1477 1548
1478void 1549void
1479ev_idle_stop (EV_P_ struct ev_idle *w) 1550ev_idle_stop (EV_P_ ev_idle *w)
1480{ 1551{
1481 ev_clear_pending (EV_A_ (W)w); 1552 ev_clear_pending (EV_A_ (W)w);
1482 if (expect_false (!ev_is_active (w))) 1553 if (expect_false (!ev_is_active (w)))
1483 return; 1554 return;
1484 1555
1485 idles [((W)w)->active - 1] = idles [--idlecnt]; 1556 idles [((W)w)->active - 1] = idles [--idlecnt];
1486 ev_stop (EV_A_ (W)w); 1557 ev_stop (EV_A_ (W)w);
1487} 1558}
1488 1559
1489void 1560void
1490ev_prepare_start (EV_P_ struct ev_prepare *w) 1561ev_prepare_start (EV_P_ ev_prepare *w)
1491{ 1562{
1492 if (expect_false (ev_is_active (w))) 1563 if (expect_false (ev_is_active (w)))
1493 return; 1564 return;
1494 1565
1495 ev_start (EV_A_ (W)w, ++preparecnt); 1566 ev_start (EV_A_ (W)w, ++preparecnt);
1496 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1567 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1497 prepares [preparecnt - 1] = w; 1568 prepares [preparecnt - 1] = w;
1498} 1569}
1499 1570
1500void 1571void
1501ev_prepare_stop (EV_P_ struct ev_prepare *w) 1572ev_prepare_stop (EV_P_ ev_prepare *w)
1502{ 1573{
1503 ev_clear_pending (EV_A_ (W)w); 1574 ev_clear_pending (EV_A_ (W)w);
1504 if (expect_false (!ev_is_active (w))) 1575 if (expect_false (!ev_is_active (w)))
1505 return; 1576 return;
1506 1577
1507 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1578 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1508 ev_stop (EV_A_ (W)w); 1579 ev_stop (EV_A_ (W)w);
1509} 1580}
1510 1581
1511void 1582void
1512ev_check_start (EV_P_ struct ev_check *w) 1583ev_check_start (EV_P_ ev_check *w)
1513{ 1584{
1514 if (expect_false (ev_is_active (w))) 1585 if (expect_false (ev_is_active (w)))
1515 return; 1586 return;
1516 1587
1517 ev_start (EV_A_ (W)w, ++checkcnt); 1588 ev_start (EV_A_ (W)w, ++checkcnt);
1518 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1589 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1519 checks [checkcnt - 1] = w; 1590 checks [checkcnt - 1] = w;
1520} 1591}
1521 1592
1522void 1593void
1523ev_check_stop (EV_P_ struct ev_check *w) 1594ev_check_stop (EV_P_ ev_check *w)
1524{ 1595{
1525 ev_clear_pending (EV_A_ (W)w); 1596 ev_clear_pending (EV_A_ (W)w);
1526 if (expect_false (!ev_is_active (w))) 1597 if (expect_false (!ev_is_active (w)))
1527 return; 1598 return;
1528 1599
1533#ifndef SA_RESTART 1604#ifndef SA_RESTART
1534# define SA_RESTART 0 1605# define SA_RESTART 0
1535#endif 1606#endif
1536 1607
1537void 1608void
1538ev_signal_start (EV_P_ struct ev_signal *w) 1609ev_signal_start (EV_P_ ev_signal *w)
1539{ 1610{
1540#if EV_MULTIPLICITY 1611#if EV_MULTIPLICITY
1541 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1612 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1542#endif 1613#endif
1543 if (expect_false (ev_is_active (w))) 1614 if (expect_false (ev_is_active (w)))
1562#endif 1633#endif
1563 } 1634 }
1564} 1635}
1565 1636
1566void 1637void
1567ev_signal_stop (EV_P_ struct ev_signal *w) 1638ev_signal_stop (EV_P_ ev_signal *w)
1568{ 1639{
1569 ev_clear_pending (EV_A_ (W)w); 1640 ev_clear_pending (EV_A_ (W)w);
1570 if (expect_false (!ev_is_active (w))) 1641 if (expect_false (!ev_is_active (w)))
1571 return; 1642 return;
1572 1643
1576 if (!signals [w->signum - 1].head) 1647 if (!signals [w->signum - 1].head)
1577 signal (w->signum, SIG_DFL); 1648 signal (w->signum, SIG_DFL);
1578} 1649}
1579 1650
1580void 1651void
1581ev_child_start (EV_P_ struct ev_child *w) 1652ev_child_start (EV_P_ ev_child *w)
1582{ 1653{
1583#if EV_MULTIPLICITY 1654#if EV_MULTIPLICITY
1584 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1655 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1585#endif 1656#endif
1586 if (expect_false (ev_is_active (w))) 1657 if (expect_false (ev_is_active (w)))
1589 ev_start (EV_A_ (W)w, 1); 1660 ev_start (EV_A_ (W)w, 1);
1590 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1591} 1662}
1592 1663
1593void 1664void
1594ev_child_stop (EV_P_ struct ev_child *w) 1665ev_child_stop (EV_P_ ev_child *w)
1595{ 1666{
1596 ev_clear_pending (EV_A_ (W)w); 1667 ev_clear_pending (EV_A_ (W)w);
1597 if (expect_false (!ev_is_active (w))) 1668 if (expect_false (!ev_is_active (w)))
1598 return; 1669 return;
1599 1670
1600 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1601 ev_stop (EV_A_ (W)w); 1672 ev_stop (EV_A_ (W)w);
1602} 1673}
1603 1674
1675#if EV_MULTIPLICITY
1676void
1677ev_embed_loop (EV_P_ ev_embed *w)
1678{
1679 ev_loop (w->loop, EVLOOP_NONBLOCK);
1680}
1681
1682static void
1683embed_cb (EV_P_ ev_io *io, int revents)
1684{
1685 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1686
1687 if (ev_cb (w))
1688 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1689 else
1690 ev_embed_loop (loop, w);
1691}
1692
1693void
1694ev_embed_start (EV_P_ ev_embed *w)
1695{
1696 if (expect_false (ev_is_active (w)))
1697 return;
1698
1699 {
1700 struct ev_loop *loop = w->loop;
1701 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1702 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1703 }
1704
1705 ev_set_priority (&w->io, ev_priority (w));
1706 ev_io_start (EV_A_ &w->io);
1707 ev_start (EV_A_ (W)w, 1);
1708}
1709
1710void
1711ev_embed_stop (EV_P_ ev_embed *w)
1712{
1713 ev_clear_pending (EV_A_ (W)w);
1714 if (expect_false (!ev_is_active (w)))
1715 return;
1716
1717 ev_io_stop (EV_A_ &w->io);
1718 ev_stop (EV_A_ (W)w);
1719}
1720#endif
1721
1604/*****************************************************************************/ 1722/*****************************************************************************/
1605 1723
1606struct ev_once 1724struct ev_once
1607{ 1725{
1608 struct ev_io io; 1726 ev_io io;
1609 struct ev_timer to; 1727 ev_timer to;
1610 void (*cb)(int revents, void *arg); 1728 void (*cb)(int revents, void *arg);
1611 void *arg; 1729 void *arg;
1612}; 1730};
1613 1731
1614static void 1732static void
1623 1741
1624 cb (revents, arg); 1742 cb (revents, arg);
1625} 1743}
1626 1744
1627static void 1745static void
1628once_cb_io (EV_P_ struct ev_io *w, int revents) 1746once_cb_io (EV_P_ ev_io *w, int revents)
1629{ 1747{
1630 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1748 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1631} 1749}
1632 1750
1633static void 1751static void
1634once_cb_to (EV_P_ struct ev_timer *w, int revents) 1752once_cb_to (EV_P_ ev_timer *w, int revents)
1635{ 1753{
1636 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1754 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1637} 1755}
1638 1756
1639void 1757void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines