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

Comparing libev/ev.c (file contents):
Revision 1.127 by root, Sun Nov 18 02:17:57 2007 UTC vs.
Revision 1.139 by root, Sun Nov 25 09:24:37 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
154# define EV_USE_PORT 0 158# define EV_USE_PORT 0
155#endif 159#endif
156 160
157/**/ 161/**/
158 162
159/* darwin simply cannot be helped */
160#ifdef __APPLE__
161# undef EV_USE_POLL
162# undef EV_USE_KQUEUE
163#endif
164
165#ifndef CLOCK_MONOTONIC 163#ifndef CLOCK_MONOTONIC
166# undef EV_USE_MONOTONIC 164# undef EV_USE_MONOTONIC
167# define EV_USE_MONOTONIC 0 165# define EV_USE_MONOTONIC 0
168#endif 166#endif
169 167
204#define ABSPRI(w) ((w)->priority - EV_MINPRI) 202#define ABSPRI(w) ((w)->priority - EV_MINPRI)
205 203
206#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
207#define EMPTY2(a,b) /* used to suppress some warnings */ 205#define EMPTY2(a,b) /* used to suppress some warnings */
208 206
209typedef struct ev_watcher *W; 207typedef ev_watcher *W;
210typedef struct ev_watcher_list *WL; 208typedef ev_watcher_list *WL;
211typedef struct ev_watcher_time *WT; 209typedef ev_watcher_time *WT;
212 210
213static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
214 212
215#ifdef _WIN32 213#ifdef _WIN32
216# include "ev_win32.c" 214# include "ev_win32.c"
418 416
419inline void 417inline void
420fd_event (EV_P_ int fd, int revents) 418fd_event (EV_P_ int fd, int revents)
421{ 419{
422 ANFD *anfd = anfds + fd; 420 ANFD *anfd = anfds + fd;
423 struct ev_io *w; 421 ev_io *w;
424 422
425 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)
426 { 424 {
427 int ev = w->events & revents; 425 int ev = w->events & revents;
428 426
429 if (ev) 427 if (ev)
430 ev_feed_event (EV_A_ (W)w, ev); 428 ev_feed_event (EV_A_ (W)w, ev);
446 444
447 for (i = 0; i < fdchangecnt; ++i) 445 for (i = 0; i < fdchangecnt; ++i)
448 { 446 {
449 int fd = fdchanges [i]; 447 int fd = fdchanges [i];
450 ANFD *anfd = anfds + fd; 448 ANFD *anfd = anfds + fd;
451 struct ev_io *w; 449 ev_io *w;
452 450
453 int events = 0; 451 int events = 0;
454 452
455 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)
456 events |= w->events; 454 events |= w->events;
457 455
458#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
459 if (events) 457 if (events)
460 { 458 {
464 } 462 }
465#endif 463#endif
466 464
467 anfd->reify = 0; 465 anfd->reify = 0;
468 466
469 method_modify (EV_A_ fd, anfd->events, events); 467 backend_modify (EV_A_ fd, anfd->events, events);
470 anfd->events = events; 468 anfd->events = events;
471 } 469 }
472 470
473 fdchangecnt = 0; 471 fdchangecnt = 0;
474} 472}
487} 485}
488 486
489static void 487static void
490fd_kill (EV_P_ int fd) 488fd_kill (EV_P_ int fd)
491{ 489{
492 struct ev_io *w; 490 ev_io *w;
493 491
494 while ((w = (struct ev_io *)anfds [fd].head)) 492 while ((w = (ev_io *)anfds [fd].head))
495 { 493 {
496 ev_io_stop (EV_A_ w); 494 ev_io_stop (EV_A_ w);
497 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);
498 } 496 }
499} 497}
532 fd_kill (EV_A_ fd); 530 fd_kill (EV_A_ fd);
533 return; 531 return;
534 } 532 }
535} 533}
536 534
537/* 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 */
538static void 536static void
539fd_rearm_all (EV_P) 537fd_rearm_all (EV_P)
540{ 538{
541 int fd; 539 int fd;
542 540
610static ANSIG *signals; 608static ANSIG *signals;
611static int signalmax; 609static int signalmax;
612 610
613static int sigpipe [2]; 611static int sigpipe [2];
614static sig_atomic_t volatile gotsig; 612static sig_atomic_t volatile gotsig;
615static struct ev_io sigev; 613static ev_io sigev;
616 614
617static void 615static void
618signals_init (ANSIG *base, int count) 616signals_init (ANSIG *base, int count)
619{ 617{
620 while (count--) 618 while (count--)
663 for (w = signals [signum].head; w; w = w->next) 661 for (w = signals [signum].head; w; w = w->next)
664 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
665} 663}
666 664
667static void 665static void
668sigcb (EV_P_ struct ev_io *iow, int revents) 666sigcb (EV_P_ ev_io *iow, int revents)
669{ 667{
670 int signum; 668 int signum;
671 669
672 read (sigpipe [0], &revents, 1); 670 read (sigpipe [0], &revents, 1);
673 gotsig = 0; 671 gotsig = 0;
700 ev_unref (EV_A); /* child watcher should not keep loop alive */ 698 ev_unref (EV_A); /* child watcher should not keep loop alive */
701} 699}
702 700
703/*****************************************************************************/ 701/*****************************************************************************/
704 702
705static struct ev_child *childs [PID_HASHSIZE]; 703static ev_child *childs [PID_HASHSIZE];
706 704
707#ifndef _WIN32 705#ifndef _WIN32
708 706
709static struct ev_signal childev; 707static ev_signal childev;
710 708
711#ifndef WCONTINUED 709#ifndef WCONTINUED
712# define WCONTINUED 0 710# define WCONTINUED 0
713#endif 711#endif
714 712
715static void 713static void
716child_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)
717{ 715{
718 struct ev_child *w; 716 ev_child *w;
719 717
720 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)
721 if (w->pid == pid || !w->pid) 719 if (w->pid == pid || !w->pid)
722 { 720 {
723 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
724 w->rpid = pid; 722 w->rpid = pid;
725 w->rstatus = status; 723 w->rstatus = status;
726 ev_feed_event (EV_A_ (W)w, EV_CHILD); 724 ev_feed_event (EV_A_ (W)w, EV_CHILD);
727 } 725 }
728} 726}
729 727
730static void 728static void
731childcb (EV_P_ struct ev_signal *sw, int revents) 729childcb (EV_P_ ev_signal *sw, int revents)
732{ 730{
733 int pid, status; 731 int pid, status;
734 732
735 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
736 { 734 {
737 /* 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 */
738 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 737 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
739 738
740 child_reap (EV_A_ sw, pid, pid, status); 739 child_reap (EV_A_ sw, pid, pid, status);
741 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 */
742 } 741 }
743} 742}
744 743
745#endif 744#endif
746 745
785 || getgid () != getegid (); 784 || getgid () != getegid ();
786#endif 785#endif
787} 786}
788 787
789unsigned int 788unsigned int
790ev_method (EV_P) 789ev_supported_backends (void)
791{ 790{
792 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;
793} 832}
794 833
795static void 834static void
796loop_init (EV_P_ unsigned int flags) 835loop_init (EV_P_ unsigned int flags)
797{ 836{
798 if (!method) 837 if (!backend)
799 { 838 {
800#if EV_USE_MONOTONIC 839#if EV_USE_MONOTONIC
801 { 840 {
802 struct timespec ts; 841 struct timespec ts;
803 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 842 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
808 ev_rt_now = ev_time (); 847 ev_rt_now = ev_time ();
809 mn_now = get_clock (); 848 mn_now = get_clock ();
810 now_floor = mn_now; 849 now_floor = mn_now;
811 rtmn_diff = ev_rt_now - mn_now; 850 rtmn_diff = ev_rt_now - mn_now;
812 851
813 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) 852 if (!(flags & EVFLAG_NOENV)
853 && !enable_secure ()
854 && getenv ("LIBEV_FLAGS"))
814 flags = atoi (getenv ("LIBEV_FLAGS")); 855 flags = atoi (getenv ("LIBEV_FLAGS"));
815 856
816 if (!(flags & 0x0000ffff)) 857 if (!(flags & 0x0000ffffUL))
817 flags |= 0x0000ffff; 858 flags |= ev_recommended_backends ();
818 859
819 method = 0; 860 backend = 0;
820#if EV_USE_PORT 861#if EV_USE_PORT
821 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); 862 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
822#endif 863#endif
823#if EV_USE_KQUEUE 864#if EV_USE_KQUEUE
824 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 865 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
825#endif 866#endif
826#if EV_USE_EPOLL 867#if EV_USE_EPOLL
827 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 868 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
828#endif 869#endif
829#if EV_USE_POLL 870#if EV_USE_POLL
830 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); 871 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
831#endif 872#endif
832#if EV_USE_SELECT 873#if EV_USE_SELECT
833 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); 874 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
834#endif 875#endif
835 876
836 ev_init (&sigev, sigcb); 877 ev_init (&sigev, sigcb);
837 ev_set_priority (&sigev, EV_MAXPRI); 878 ev_set_priority (&sigev, EV_MAXPRI);
838 } 879 }
842loop_destroy (EV_P) 883loop_destroy (EV_P)
843{ 884{
844 int i; 885 int i;
845 886
846#if EV_USE_PORT 887#if EV_USE_PORT
847 if (method == EVMETHOD_PORT ) port_destroy (EV_A); 888 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
848#endif 889#endif
849#if EV_USE_KQUEUE 890#if EV_USE_KQUEUE
850 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 891 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
851#endif 892#endif
852#if EV_USE_EPOLL 893#if EV_USE_EPOLL
853 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 894 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
854#endif 895#endif
855#if EV_USE_POLL 896#if EV_USE_POLL
856 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 897 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
857#endif 898#endif
858#if EV_USE_SELECT 899#if EV_USE_SELECT
859 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 900 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
860#endif 901#endif
861 902
862 for (i = NUMPRI; i--; ) 903 for (i = NUMPRI; i--; )
863 array_free (pending, [i]); 904 array_free (pending, [i]);
864 905
870#endif 911#endif
871 array_free (idle, EMPTY0); 912 array_free (idle, EMPTY0);
872 array_free (prepare, EMPTY0); 913 array_free (prepare, EMPTY0);
873 array_free (check, EMPTY0); 914 array_free (check, EMPTY0);
874 915
875 method = 0; 916 backend = 0;
876} 917}
877 918
878static void 919static void
879loop_fork (EV_P) 920loop_fork (EV_P)
880{ 921{
881#if EV_USE_PORT 922#if EV_USE_PORT
882 if (method == EVMETHOD_PORT ) port_fork (EV_A); 923 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
883#endif 924#endif
884#if EV_USE_KQUEUE 925#if EV_USE_KQUEUE
885 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 926 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
886#endif 927#endif
887#if EV_USE_EPOLL 928#if EV_USE_EPOLL
888 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 929 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
889#endif 930#endif
890 931
891 if (ev_is_active (&sigev)) 932 if (ev_is_active (&sigev))
892 { 933 {
893 /* default loop */ 934 /* default loop */
914 955
915 memset (loop, 0, sizeof (struct ev_loop)); 956 memset (loop, 0, sizeof (struct ev_loop));
916 957
917 loop_init (EV_A_ flags); 958 loop_init (EV_A_ flags);
918 959
919 if (ev_method (EV_A)) 960 if (ev_backend (EV_A))
920 return loop; 961 return loop;
921 962
922 return 0; 963 return 0;
923} 964}
924 965
957 ev_default_loop_ptr = 1; 998 ev_default_loop_ptr = 1;
958#endif 999#endif
959 1000
960 loop_init (EV_A_ flags); 1001 loop_init (EV_A_ flags);
961 1002
962 if (ev_method (EV_A)) 1003 if (ev_backend (EV_A))
963 { 1004 {
964 siginit (EV_A); 1005 siginit (EV_A);
965 1006
966#ifndef _WIN32 1007#ifndef _WIN32
967 ev_signal_init (&childev, childcb, SIGCHLD); 1008 ev_signal_init (&childev, childcb, SIGCHLD);
1003{ 1044{
1004#if EV_MULTIPLICITY 1045#if EV_MULTIPLICITY
1005 struct ev_loop *loop = ev_default_loop_ptr; 1046 struct ev_loop *loop = ev_default_loop_ptr;
1006#endif 1047#endif
1007 1048
1008 if (method) 1049 if (backend)
1009 postfork = 1; 1050 postfork = 1;
1010} 1051}
1011 1052
1012/*****************************************************************************/ 1053/*****************************************************************************/
1013 1054
1033 { 1074 {
1034 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1075 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1035 1076
1036 if (expect_true (p->w)) 1077 if (expect_true (p->w))
1037 { 1078 {
1079 assert (("non-pending watcher on pending list", p->w->pending));
1080
1038 p->w->pending = 0; 1081 p->w->pending = 0;
1039 EV_CB_INVOKE (p->w, p->events); 1082 EV_CB_INVOKE (p->w, p->events);
1040 } 1083 }
1041 } 1084 }
1042} 1085}
1044inline void 1087inline void
1045timers_reify (EV_P) 1088timers_reify (EV_P)
1046{ 1089{
1047 while (timercnt && ((WT)timers [0])->at <= mn_now) 1090 while (timercnt && ((WT)timers [0])->at <= mn_now)
1048 { 1091 {
1049 struct ev_timer *w = timers [0]; 1092 ev_timer *w = timers [0];
1050 1093
1051 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1094 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1052 1095
1053 /* first reschedule or stop timer */ 1096 /* first reschedule or stop timer */
1054 if (w->repeat) 1097 if (w->repeat)
1072inline void 1115inline void
1073periodics_reify (EV_P) 1116periodics_reify (EV_P)
1074{ 1117{
1075 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1118 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1076 { 1119 {
1077 struct ev_periodic *w = periodics [0]; 1120 ev_periodic *w = periodics [0];
1078 1121
1079 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1122 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1080 1123
1081 /* first reschedule or stop timer */ 1124 /* first reschedule or stop timer */
1082 if (w->reschedule_cb) 1125 if (w->reschedule_cb)
1104 int i; 1147 int i;
1105 1148
1106 /* adjust periodics after time jump */ 1149 /* adjust periodics after time jump */
1107 for (i = 0; i < periodiccnt; ++i) 1150 for (i = 0; i < periodiccnt; ++i)
1108 { 1151 {
1109 struct ev_periodic *w = periodics [i]; 1152 ev_periodic *w = periodics [i];
1110 1153
1111 if (w->reschedule_cb) 1154 if (w->reschedule_cb)
1112 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1155 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1113 else if (w->interval) 1156 else if (w->interval)
1114 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1157 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1148 { 1191 {
1149 if (time_update_monotonic (EV_A)) 1192 if (time_update_monotonic (EV_A))
1150 { 1193 {
1151 ev_tstamp odiff = rtmn_diff; 1194 ev_tstamp odiff = rtmn_diff;
1152 1195
1153 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1196 /* loop a few times, before making important decisions.
1197 * on the choice of "4": one iteration isn't enough,
1198 * in case we get preempted during the calls to
1199 * ev_time and get_clock. a second call is almost guarenteed
1200 * to succeed in that case, though. and looping a few more times
1201 * doesn't hurt either as we only do this on time-jumps or
1202 * in the unlikely event of getting preempted here.
1203 */
1204 for (i = 4; --i; )
1154 { 1205 {
1155 rtmn_diff = ev_rt_now - mn_now; 1206 rtmn_diff = ev_rt_now - mn_now;
1156 1207
1157 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1208 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1158 return; /* all is well */ 1209 return; /* all is well */
1204static int loop_done; 1255static int loop_done;
1205 1256
1206void 1257void
1207ev_loop (EV_P_ int flags) 1258ev_loop (EV_P_ int flags)
1208{ 1259{
1209 double block;
1210 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1260 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1261 ? EVUNLOOP_ONE
1262 : EVUNLOOP_CANCEL;
1211 1263
1212 while (activecnt) 1264 while (activecnt)
1213 { 1265 {
1214 /* queue check watchers (and execute them) */ 1266 /* queue check watchers (and execute them) */
1215 if (expect_false (preparecnt)) 1267 if (expect_false (preparecnt))
1224 1276
1225 /* update fd-related kernel structures */ 1277 /* update fd-related kernel structures */
1226 fd_reify (EV_A); 1278 fd_reify (EV_A);
1227 1279
1228 /* calculate blocking time */ 1280 /* calculate blocking time */
1281 {
1282 double block;
1229 1283
1230 /* we only need this for !monotonic clock or timers, but as we basically 1284 if (flags & EVLOOP_NONBLOCK || idlecnt)
1231 always have timers, we just calculate it always */ 1285 block = 0.; /* do not block at all */
1286 else
1287 {
1288 /* update time to cancel out callback processing overhead */
1232#if EV_USE_MONOTONIC 1289#if EV_USE_MONOTONIC
1233 if (expect_true (have_monotonic)) 1290 if (expect_true (have_monotonic))
1234 time_update_monotonic (EV_A); 1291 time_update_monotonic (EV_A);
1235 else 1292 else
1236#endif 1293#endif
1237 { 1294 {
1238 ev_rt_now = ev_time (); 1295 ev_rt_now = ev_time ();
1239 mn_now = ev_rt_now; 1296 mn_now = ev_rt_now;
1240 } 1297 }
1241 1298
1242 if (flags & EVLOOP_NONBLOCK || idlecnt)
1243 block = 0.;
1244 else
1245 {
1246 block = MAX_BLOCKTIME; 1299 block = MAX_BLOCKTIME;
1247 1300
1248 if (timercnt) 1301 if (timercnt)
1249 { 1302 {
1250 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1303 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1251 if (block > to) block = to; 1304 if (block > to) block = to;
1252 } 1305 }
1253 1306
1254#if EV_PERIODICS 1307#if EV_PERIODICS
1255 if (periodiccnt) 1308 if (periodiccnt)
1256 { 1309 {
1257 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1310 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1258 if (block > to) block = to; 1311 if (block > to) block = to;
1259 } 1312 }
1260#endif 1313#endif
1261 1314
1262 if (expect_false (block < 0.)) block = 0.; 1315 if (expect_false (block < 0.)) block = 0.;
1263 } 1316 }
1264 1317
1265 method_poll (EV_A_ block); 1318 backend_poll (EV_A_ block);
1319 }
1266 1320
1267 /* update ev_rt_now, do magic */ 1321 /* update ev_rt_now, do magic */
1268 time_update (EV_A); 1322 time_update (EV_A);
1269 1323
1270 /* queue pending timers and reschedule them */ 1324 /* queue pending timers and reschedule them */
1271 timers_reify (EV_A); /* relative timers called last */ 1325 timers_reify (EV_A); /* relative timers called last */
1272#if EV_PERIODICS 1326#if EV_PERIODICS
1273 periodics_reify (EV_A); /* absolute timers called first */ 1327 periodics_reify (EV_A); /* absolute timers called first */
1274#endif 1328#endif
1275 1329
1276 /* queue idle watchers unless io or timers are pending */ 1330 /* queue idle watchers unless other events are pending */
1277 if (idlecnt && !any_pending (EV_A)) 1331 if (idlecnt && !any_pending (EV_A))
1278 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1332 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1279 1333
1280 /* queue check watchers, to be executed first */ 1334 /* queue check watchers, to be executed first */
1281 if (expect_false (checkcnt)) 1335 if (expect_false (checkcnt))
1285 1339
1286 if (expect_false (loop_done)) 1340 if (expect_false (loop_done))
1287 break; 1341 break;
1288 } 1342 }
1289 1343
1290 if (loop_done != 2) 1344 if (loop_done == EVUNLOOP_ONE)
1291 loop_done = 0; 1345 loop_done = EVUNLOOP_CANCEL;
1292} 1346}
1293 1347
1294void 1348void
1295ev_unloop (EV_P_ int how) 1349ev_unloop (EV_P_ int how)
1296{ 1350{
1349} 1403}
1350 1404
1351/*****************************************************************************/ 1405/*****************************************************************************/
1352 1406
1353void 1407void
1354ev_io_start (EV_P_ struct ev_io *w) 1408ev_io_start (EV_P_ ev_io *w)
1355{ 1409{
1356 int fd = w->fd; 1410 int fd = w->fd;
1357 1411
1358 if (expect_false (ev_is_active (w))) 1412 if (expect_false (ev_is_active (w)))
1359 return; 1413 return;
1366 1420
1367 fd_change (EV_A_ fd); 1421 fd_change (EV_A_ fd);
1368} 1422}
1369 1423
1370void 1424void
1371ev_io_stop (EV_P_ struct ev_io *w) 1425ev_io_stop (EV_P_ ev_io *w)
1372{ 1426{
1373 ev_clear_pending (EV_A_ (W)w); 1427 ev_clear_pending (EV_A_ (W)w);
1374 if (expect_false (!ev_is_active (w))) 1428 if (expect_false (!ev_is_active (w)))
1375 return; 1429 return;
1376 1430
1381 1435
1382 fd_change (EV_A_ w->fd); 1436 fd_change (EV_A_ w->fd);
1383} 1437}
1384 1438
1385void 1439void
1386ev_timer_start (EV_P_ struct ev_timer *w) 1440ev_timer_start (EV_P_ ev_timer *w)
1387{ 1441{
1388 if (expect_false (ev_is_active (w))) 1442 if (expect_false (ev_is_active (w)))
1389 return; 1443 return;
1390 1444
1391 ((WT)w)->at += mn_now; 1445 ((WT)w)->at += mn_now;
1392 1446
1393 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1447 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1394 1448
1395 ev_start (EV_A_ (W)w, ++timercnt); 1449 ev_start (EV_A_ (W)w, ++timercnt);
1396 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1450 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1397 timers [timercnt - 1] = w; 1451 timers [timercnt - 1] = w;
1398 upheap ((WT *)timers, timercnt - 1); 1452 upheap ((WT *)timers, timercnt - 1);
1399 1453
1400 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1454 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1401} 1455}
1402 1456
1403void 1457void
1404ev_timer_stop (EV_P_ struct ev_timer *w) 1458ev_timer_stop (EV_P_ ev_timer *w)
1405{ 1459{
1406 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1407 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1408 return; 1462 return;
1409 1463
1419 1473
1420 ev_stop (EV_A_ (W)w); 1474 ev_stop (EV_A_ (W)w);
1421} 1475}
1422 1476
1423void 1477void
1424ev_timer_again (EV_P_ struct ev_timer *w) 1478ev_timer_again (EV_P_ ev_timer *w)
1425{ 1479{
1426 if (ev_is_active (w)) 1480 if (ev_is_active (w))
1427 { 1481 {
1428 if (w->repeat) 1482 if (w->repeat)
1429 { 1483 {
1440 } 1494 }
1441} 1495}
1442 1496
1443#if EV_PERIODICS 1497#if EV_PERIODICS
1444void 1498void
1445ev_periodic_start (EV_P_ struct ev_periodic *w) 1499ev_periodic_start (EV_P_ ev_periodic *w)
1446{ 1500{
1447 if (expect_false (ev_is_active (w))) 1501 if (expect_false (ev_is_active (w)))
1448 return; 1502 return;
1449 1503
1450 if (w->reschedule_cb) 1504 if (w->reschedule_cb)
1455 /* this formula differs from the one in periodic_reify because we do not always round up */ 1509 /* this formula differs from the one in periodic_reify because we do not always round up */
1456 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1510 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1457 } 1511 }
1458 1512
1459 ev_start (EV_A_ (W)w, ++periodiccnt); 1513 ev_start (EV_A_ (W)w, ++periodiccnt);
1460 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1514 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1461 periodics [periodiccnt - 1] = w; 1515 periodics [periodiccnt - 1] = w;
1462 upheap ((WT *)periodics, periodiccnt - 1); 1516 upheap ((WT *)periodics, periodiccnt - 1);
1463 1517
1464 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1518 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1465} 1519}
1466 1520
1467void 1521void
1468ev_periodic_stop (EV_P_ struct ev_periodic *w) 1522ev_periodic_stop (EV_P_ ev_periodic *w)
1469{ 1523{
1470 ev_clear_pending (EV_A_ (W)w); 1524 ev_clear_pending (EV_A_ (W)w);
1471 if (expect_false (!ev_is_active (w))) 1525 if (expect_false (!ev_is_active (w)))
1472 return; 1526 return;
1473 1527
1481 1535
1482 ev_stop (EV_A_ (W)w); 1536 ev_stop (EV_A_ (W)w);
1483} 1537}
1484 1538
1485void 1539void
1486ev_periodic_again (EV_P_ struct ev_periodic *w) 1540ev_periodic_again (EV_P_ ev_periodic *w)
1487{ 1541{
1488 /* TODO: use adjustheap and recalculation */ 1542 /* TODO: use adjustheap and recalculation */
1489 ev_periodic_stop (EV_A_ w); 1543 ev_periodic_stop (EV_A_ w);
1490 ev_periodic_start (EV_A_ w); 1544 ev_periodic_start (EV_A_ w);
1491} 1545}
1492#endif 1546#endif
1493 1547
1494void 1548void
1495ev_idle_start (EV_P_ struct ev_idle *w) 1549ev_idle_start (EV_P_ ev_idle *w)
1496{ 1550{
1497 if (expect_false (ev_is_active (w))) 1551 if (expect_false (ev_is_active (w)))
1498 return; 1552 return;
1499 1553
1500 ev_start (EV_A_ (W)w, ++idlecnt); 1554 ev_start (EV_A_ (W)w, ++idlecnt);
1501 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1555 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1502 idles [idlecnt - 1] = w; 1556 idles [idlecnt - 1] = w;
1503} 1557}
1504 1558
1505void 1559void
1506ev_idle_stop (EV_P_ struct ev_idle *w) 1560ev_idle_stop (EV_P_ ev_idle *w)
1507{ 1561{
1508 ev_clear_pending (EV_A_ (W)w); 1562 ev_clear_pending (EV_A_ (W)w);
1509 if (expect_false (!ev_is_active (w))) 1563 if (expect_false (!ev_is_active (w)))
1510 return; 1564 return;
1511 1565
1566 {
1567 int active = ((W)w)->active;
1512 idles [((W)w)->active - 1] = idles [--idlecnt]; 1568 idles [active - 1] = idles [--idlecnt];
1569 ((W)idles [active - 1])->active = active;
1570 }
1571
1513 ev_stop (EV_A_ (W)w); 1572 ev_stop (EV_A_ (W)w);
1514} 1573}
1515 1574
1516void 1575void
1517ev_prepare_start (EV_P_ struct ev_prepare *w) 1576ev_prepare_start (EV_P_ ev_prepare *w)
1518{ 1577{
1519 if (expect_false (ev_is_active (w))) 1578 if (expect_false (ev_is_active (w)))
1520 return; 1579 return;
1521 1580
1522 ev_start (EV_A_ (W)w, ++preparecnt); 1581 ev_start (EV_A_ (W)w, ++preparecnt);
1523 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1582 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1524 prepares [preparecnt - 1] = w; 1583 prepares [preparecnt - 1] = w;
1525} 1584}
1526 1585
1527void 1586void
1528ev_prepare_stop (EV_P_ struct ev_prepare *w) 1587ev_prepare_stop (EV_P_ ev_prepare *w)
1529{ 1588{
1530 ev_clear_pending (EV_A_ (W)w); 1589 ev_clear_pending (EV_A_ (W)w);
1531 if (expect_false (!ev_is_active (w))) 1590 if (expect_false (!ev_is_active (w)))
1532 return; 1591 return;
1533 1592
1593 {
1594 int active = ((W)w)->active;
1534 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1595 prepares [active - 1] = prepares [--preparecnt];
1596 ((W)prepares [active - 1])->active = active;
1597 }
1598
1535 ev_stop (EV_A_ (W)w); 1599 ev_stop (EV_A_ (W)w);
1536} 1600}
1537 1601
1538void 1602void
1539ev_check_start (EV_P_ struct ev_check *w) 1603ev_check_start (EV_P_ ev_check *w)
1540{ 1604{
1541 if (expect_false (ev_is_active (w))) 1605 if (expect_false (ev_is_active (w)))
1542 return; 1606 return;
1543 1607
1544 ev_start (EV_A_ (W)w, ++checkcnt); 1608 ev_start (EV_A_ (W)w, ++checkcnt);
1545 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1609 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1546 checks [checkcnt - 1] = w; 1610 checks [checkcnt - 1] = w;
1547} 1611}
1548 1612
1549void 1613void
1550ev_check_stop (EV_P_ struct ev_check *w) 1614ev_check_stop (EV_P_ ev_check *w)
1551{ 1615{
1552 ev_clear_pending (EV_A_ (W)w); 1616 ev_clear_pending (EV_A_ (W)w);
1553 if (expect_false (!ev_is_active (w))) 1617 if (expect_false (!ev_is_active (w)))
1554 return; 1618 return;
1555 1619
1620 {
1621 int active = ((W)w)->active;
1556 checks [((W)w)->active - 1] = checks [--checkcnt]; 1622 checks [active - 1] = checks [--checkcnt];
1623 ((W)checks [active - 1])->active = active;
1624 }
1625
1557 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1558} 1627}
1559 1628
1560#ifndef SA_RESTART 1629#ifndef SA_RESTART
1561# define SA_RESTART 0 1630# define SA_RESTART 0
1562#endif 1631#endif
1563 1632
1564void 1633void
1565ev_signal_start (EV_P_ struct ev_signal *w) 1634ev_signal_start (EV_P_ ev_signal *w)
1566{ 1635{
1567#if EV_MULTIPLICITY 1636#if EV_MULTIPLICITY
1568 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1637 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1569#endif 1638#endif
1570 if (expect_false (ev_is_active (w))) 1639 if (expect_false (ev_is_active (w)))
1589#endif 1658#endif
1590 } 1659 }
1591} 1660}
1592 1661
1593void 1662void
1594ev_signal_stop (EV_P_ struct ev_signal *w) 1663ev_signal_stop (EV_P_ ev_signal *w)
1595{ 1664{
1596 ev_clear_pending (EV_A_ (W)w); 1665 ev_clear_pending (EV_A_ (W)w);
1597 if (expect_false (!ev_is_active (w))) 1666 if (expect_false (!ev_is_active (w)))
1598 return; 1667 return;
1599 1668
1603 if (!signals [w->signum - 1].head) 1672 if (!signals [w->signum - 1].head)
1604 signal (w->signum, SIG_DFL); 1673 signal (w->signum, SIG_DFL);
1605} 1674}
1606 1675
1607void 1676void
1608ev_child_start (EV_P_ struct ev_child *w) 1677ev_child_start (EV_P_ ev_child *w)
1609{ 1678{
1610#if EV_MULTIPLICITY 1679#if EV_MULTIPLICITY
1611 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1680 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1612#endif 1681#endif
1613 if (expect_false (ev_is_active (w))) 1682 if (expect_false (ev_is_active (w)))
1616 ev_start (EV_A_ (W)w, 1); 1685 ev_start (EV_A_ (W)w, 1);
1617 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1686 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1618} 1687}
1619 1688
1620void 1689void
1621ev_child_stop (EV_P_ struct ev_child *w) 1690ev_child_stop (EV_P_ ev_child *w)
1622{ 1691{
1623 ev_clear_pending (EV_A_ (W)w); 1692 ev_clear_pending (EV_A_ (W)w);
1624 if (expect_false (!ev_is_active (w))) 1693 if (expect_false (!ev_is_active (w)))
1625 return; 1694 return;
1626 1695
1627 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1696 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1628 ev_stop (EV_A_ (W)w); 1697 ev_stop (EV_A_ (W)w);
1629} 1698}
1630 1699
1700#if EV_MULTIPLICITY
1701void
1702ev_embed_sweep (EV_P_ ev_embed *w)
1703{
1704 ev_loop (w->loop, EVLOOP_NONBLOCK);
1705}
1706
1707static void
1708embed_cb (EV_P_ ev_io *io, int revents)
1709{
1710 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1711
1712 if (ev_cb (w))
1713 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1714 else
1715 ev_embed_sweep (loop, w);
1716}
1717
1718void
1719ev_embed_start (EV_P_ ev_embed *w)
1720{
1721 if (expect_false (ev_is_active (w)))
1722 return;
1723
1724 {
1725 struct ev_loop *loop = w->loop;
1726 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1727 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1728 }
1729
1730 ev_set_priority (&w->io, ev_priority (w));
1731 ev_io_start (EV_A_ &w->io);
1732 ev_start (EV_A_ (W)w, 1);
1733}
1734
1735void
1736ev_embed_stop (EV_P_ ev_embed *w)
1737{
1738 ev_clear_pending (EV_A_ (W)w);
1739 if (expect_false (!ev_is_active (w)))
1740 return;
1741
1742 ev_io_stop (EV_A_ &w->io);
1743 ev_stop (EV_A_ (W)w);
1744}
1745#endif
1746
1631/*****************************************************************************/ 1747/*****************************************************************************/
1632 1748
1633struct ev_once 1749struct ev_once
1634{ 1750{
1635 struct ev_io io; 1751 ev_io io;
1636 struct ev_timer to; 1752 ev_timer to;
1637 void (*cb)(int revents, void *arg); 1753 void (*cb)(int revents, void *arg);
1638 void *arg; 1754 void *arg;
1639}; 1755};
1640 1756
1641static void 1757static void
1650 1766
1651 cb (revents, arg); 1767 cb (revents, arg);
1652} 1768}
1653 1769
1654static void 1770static void
1655once_cb_io (EV_P_ struct ev_io *w, int revents) 1771once_cb_io (EV_P_ ev_io *w, int revents)
1656{ 1772{
1657 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1773 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1658} 1774}
1659 1775
1660static void 1776static void
1661once_cb_to (EV_P_ struct ev_timer *w, int revents) 1777once_cb_to (EV_P_ ev_timer *w, int revents)
1662{ 1778{
1663 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1779 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1664} 1780}
1665 1781
1666void 1782void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines