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

Comparing libev/ev.c (file contents):
Revision 1.129 by root, Fri Nov 23 05:00:44 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
198#define ABSPRI(w) ((w)->priority - EV_MINPRI) 202#define ABSPRI(w) ((w)->priority - EV_MINPRI)
199 203
200#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
201#define EMPTY2(a,b) /* used to suppress some warnings */ 205#define EMPTY2(a,b) /* used to suppress some warnings */
202 206
203typedef struct ev_watcher *W; 207typedef ev_watcher *W;
204typedef struct ev_watcher_list *WL; 208typedef ev_watcher_list *WL;
205typedef struct ev_watcher_time *WT; 209typedef ev_watcher_time *WT;
206 210
207static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
208 212
209#ifdef _WIN32 213#ifdef _WIN32
210# include "ev_win32.c" 214# include "ev_win32.c"
412 416
413inline void 417inline void
414fd_event (EV_P_ int fd, int revents) 418fd_event (EV_P_ int fd, int revents)
415{ 419{
416 ANFD *anfd = anfds + fd; 420 ANFD *anfd = anfds + fd;
417 struct ev_io *w; 421 ev_io *w;
418 422
419 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)
420 { 424 {
421 int ev = w->events & revents; 425 int ev = w->events & revents;
422 426
423 if (ev) 427 if (ev)
424 ev_feed_event (EV_A_ (W)w, ev); 428 ev_feed_event (EV_A_ (W)w, ev);
440 444
441 for (i = 0; i < fdchangecnt; ++i) 445 for (i = 0; i < fdchangecnt; ++i)
442 { 446 {
443 int fd = fdchanges [i]; 447 int fd = fdchanges [i];
444 ANFD *anfd = anfds + fd; 448 ANFD *anfd = anfds + fd;
445 struct ev_io *w; 449 ev_io *w;
446 450
447 int events = 0; 451 int events = 0;
448 452
449 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)
450 events |= w->events; 454 events |= w->events;
451 455
452#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
453 if (events) 457 if (events)
454 { 458 {
458 } 462 }
459#endif 463#endif
460 464
461 anfd->reify = 0; 465 anfd->reify = 0;
462 466
463 method_modify (EV_A_ fd, anfd->events, events); 467 backend_modify (EV_A_ fd, anfd->events, events);
464 anfd->events = events; 468 anfd->events = events;
465 } 469 }
466 470
467 fdchangecnt = 0; 471 fdchangecnt = 0;
468} 472}
481} 485}
482 486
483static void 487static void
484fd_kill (EV_P_ int fd) 488fd_kill (EV_P_ int fd)
485{ 489{
486 struct ev_io *w; 490 ev_io *w;
487 491
488 while ((w = (struct ev_io *)anfds [fd].head)) 492 while ((w = (ev_io *)anfds [fd].head))
489 { 493 {
490 ev_io_stop (EV_A_ w); 494 ev_io_stop (EV_A_ w);
491 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);
492 } 496 }
493} 497}
526 fd_kill (EV_A_ fd); 530 fd_kill (EV_A_ fd);
527 return; 531 return;
528 } 532 }
529} 533}
530 534
531/* 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 */
532static void 536static void
533fd_rearm_all (EV_P) 537fd_rearm_all (EV_P)
534{ 538{
535 int fd; 539 int fd;
536 540
604static ANSIG *signals; 608static ANSIG *signals;
605static int signalmax; 609static int signalmax;
606 610
607static int sigpipe [2]; 611static int sigpipe [2];
608static sig_atomic_t volatile gotsig; 612static sig_atomic_t volatile gotsig;
609static struct ev_io sigev; 613static ev_io sigev;
610 614
611static void 615static void
612signals_init (ANSIG *base, int count) 616signals_init (ANSIG *base, int count)
613{ 617{
614 while (count--) 618 while (count--)
657 for (w = signals [signum].head; w; w = w->next) 661 for (w = signals [signum].head; w; w = w->next)
658 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
659} 663}
660 664
661static void 665static void
662sigcb (EV_P_ struct ev_io *iow, int revents) 666sigcb (EV_P_ ev_io *iow, int revents)
663{ 667{
664 int signum; 668 int signum;
665 669
666 read (sigpipe [0], &revents, 1); 670 read (sigpipe [0], &revents, 1);
667 gotsig = 0; 671 gotsig = 0;
694 ev_unref (EV_A); /* child watcher should not keep loop alive */ 698 ev_unref (EV_A); /* child watcher should not keep loop alive */
695} 699}
696 700
697/*****************************************************************************/ 701/*****************************************************************************/
698 702
699static struct ev_child *childs [PID_HASHSIZE]; 703static ev_child *childs [PID_HASHSIZE];
700 704
701#ifndef _WIN32 705#ifndef _WIN32
702 706
703static struct ev_signal childev; 707static ev_signal childev;
704 708
705#ifndef WCONTINUED 709#ifndef WCONTINUED
706# define WCONTINUED 0 710# define WCONTINUED 0
707#endif 711#endif
708 712
709static void 713static void
710child_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)
711{ 715{
712 struct ev_child *w; 716 ev_child *w;
713 717
714 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)
715 if (w->pid == pid || !w->pid) 719 if (w->pid == pid || !w->pid)
716 { 720 {
717 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
718 w->rpid = pid; 722 w->rpid = pid;
719 w->rstatus = status; 723 w->rstatus = status;
720 ev_feed_event (EV_A_ (W)w, EV_CHILD); 724 ev_feed_event (EV_A_ (W)w, EV_CHILD);
721 } 725 }
722} 726}
723 727
724static void 728static void
725childcb (EV_P_ struct ev_signal *sw, int revents) 729childcb (EV_P_ ev_signal *sw, int revents)
726{ 730{
727 int pid, status; 731 int pid, status;
728 732
729 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
730 { 734 {
731 /* 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 */
732 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 737 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
733 738
734 child_reap (EV_A_ sw, pid, pid, status); 739 child_reap (EV_A_ sw, pid, pid, status);
735 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 */
736 } 741 }
737} 742}
738 743
739#endif 744#endif
740 745
781} 786}
782 787
783unsigned int 788unsigned int
784ev_supported_backends (void) 789ev_supported_backends (void)
785{ 790{
786}
787
788unsigned int
789ev_recommended_backends (void)
790{
791 unsigned int flags; 791 unsigned int flags = 0;
792 792
793 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 793 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
794 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 794 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
795 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 795 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
796 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 796 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
798 798
799 return flags; 799 return flags;
800} 800}
801 801
802unsigned int 802unsigned int
803ev_backend (EV_P) 803ev_recommended_backends (void)
804{ 804{
805 unsigned int flags = ev_recommended_backends (); 805 unsigned int flags = ev_supported_backends ();
806 806
807#ifndef __NetBSD__ 807#ifndef __NetBSD__
808 /* kqueue is borked on everything but netbsd apparently */ 808 /* kqueue is borked on everything but netbsd apparently */
809 /* it usually doesn't work correctly on anything but sockets and pipes */ 809 /* it usually doesn't work correctly on anything but sockets and pipes */
810 flags &= ~EVBACKEND_KQUEUE; 810 flags &= ~EVBACKEND_KQUEUE;
815#endif 815#endif
816 816
817 return flags; 817 return flags;
818} 818}
819 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;
832}
833
820static void 834static void
821loop_init (EV_P_ unsigned int flags) 835loop_init (EV_P_ unsigned int flags)
822{ 836{
823 if (!method) 837 if (!backend)
824 { 838 {
825#if EV_USE_MONOTONIC 839#if EV_USE_MONOTONIC
826 { 840 {
827 struct timespec ts; 841 struct timespec ts;
828 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 842 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
841 flags = atoi (getenv ("LIBEV_FLAGS")); 855 flags = atoi (getenv ("LIBEV_FLAGS"));
842 856
843 if (!(flags & 0x0000ffffUL)) 857 if (!(flags & 0x0000ffffUL))
844 flags |= ev_recommended_backends (); 858 flags |= ev_recommended_backends ();
845 859
846 method = 0; 860 backend = 0;
847#if EV_USE_PORT 861#if EV_USE_PORT
848 if (!method && (flags & EVBACKEND_PORT )) method = port_init (EV_A_ flags); 862 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
849#endif 863#endif
850#if EV_USE_KQUEUE 864#if EV_USE_KQUEUE
851 if (!method && (flags & EVBACKEND_KQUEUE)) method = kqueue_init (EV_A_ flags); 865 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
852#endif 866#endif
853#if EV_USE_EPOLL 867#if EV_USE_EPOLL
854 if (!method && (flags & EVBACKEND_EPOLL )) method = epoll_init (EV_A_ flags); 868 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
855#endif 869#endif
856#if EV_USE_POLL 870#if EV_USE_POLL
857 if (!method && (flags & EVBACKEND_POLL )) method = poll_init (EV_A_ flags); 871 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
858#endif 872#endif
859#if EV_USE_SELECT 873#if EV_USE_SELECT
860 if (!method && (flags & EVBACKEND_SELECT)) method = select_init (EV_A_ flags); 874 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
861#endif 875#endif
862 876
863 ev_init (&sigev, sigcb); 877 ev_init (&sigev, sigcb);
864 ev_set_priority (&sigev, EV_MAXPRI); 878 ev_set_priority (&sigev, EV_MAXPRI);
865 } 879 }
869loop_destroy (EV_P) 883loop_destroy (EV_P)
870{ 884{
871 int i; 885 int i;
872 886
873#if EV_USE_PORT 887#if EV_USE_PORT
874 if (method == EVBACKEND_PORT ) port_destroy (EV_A); 888 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
875#endif 889#endif
876#if EV_USE_KQUEUE 890#if EV_USE_KQUEUE
877 if (method == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 891 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
878#endif 892#endif
879#if EV_USE_EPOLL 893#if EV_USE_EPOLL
880 if (method == EVBACKEND_EPOLL ) epoll_destroy (EV_A); 894 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
881#endif 895#endif
882#if EV_USE_POLL 896#if EV_USE_POLL
883 if (method == EVBACKEND_POLL ) poll_destroy (EV_A); 897 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
884#endif 898#endif
885#if EV_USE_SELECT 899#if EV_USE_SELECT
886 if (method == EVBACKEND_SELECT) select_destroy (EV_A); 900 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
887#endif 901#endif
888 902
889 for (i = NUMPRI; i--; ) 903 for (i = NUMPRI; i--; )
890 array_free (pending, [i]); 904 array_free (pending, [i]);
891 905
897#endif 911#endif
898 array_free (idle, EMPTY0); 912 array_free (idle, EMPTY0);
899 array_free (prepare, EMPTY0); 913 array_free (prepare, EMPTY0);
900 array_free (check, EMPTY0); 914 array_free (check, EMPTY0);
901 915
902 method = 0; 916 backend = 0;
903} 917}
904 918
905static void 919static void
906loop_fork (EV_P) 920loop_fork (EV_P)
907{ 921{
908#if EV_USE_PORT 922#if EV_USE_PORT
909 if (method == EVBACKEND_PORT ) port_fork (EV_A); 923 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
910#endif 924#endif
911#if EV_USE_KQUEUE 925#if EV_USE_KQUEUE
912 if (method == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 926 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
913#endif 927#endif
914#if EV_USE_EPOLL 928#if EV_USE_EPOLL
915 if (method == EVBACKEND_EPOLL ) epoll_fork (EV_A); 929 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
916#endif 930#endif
917 931
918 if (ev_is_active (&sigev)) 932 if (ev_is_active (&sigev))
919 { 933 {
920 /* default loop */ 934 /* default loop */
941 955
942 memset (loop, 0, sizeof (struct ev_loop)); 956 memset (loop, 0, sizeof (struct ev_loop));
943 957
944 loop_init (EV_A_ flags); 958 loop_init (EV_A_ flags);
945 959
946 if (ev_method (EV_A)) 960 if (ev_backend (EV_A))
947 return loop; 961 return loop;
948 962
949 return 0; 963 return 0;
950} 964}
951 965
984 ev_default_loop_ptr = 1; 998 ev_default_loop_ptr = 1;
985#endif 999#endif
986 1000
987 loop_init (EV_A_ flags); 1001 loop_init (EV_A_ flags);
988 1002
989 if (ev_method (EV_A)) 1003 if (ev_backend (EV_A))
990 { 1004 {
991 siginit (EV_A); 1005 siginit (EV_A);
992 1006
993#ifndef _WIN32 1007#ifndef _WIN32
994 ev_signal_init (&childev, childcb, SIGCHLD); 1008 ev_signal_init (&childev, childcb, SIGCHLD);
1030{ 1044{
1031#if EV_MULTIPLICITY 1045#if EV_MULTIPLICITY
1032 struct ev_loop *loop = ev_default_loop_ptr; 1046 struct ev_loop *loop = ev_default_loop_ptr;
1033#endif 1047#endif
1034 1048
1035 if (method) 1049 if (backend)
1036 postfork = 1; 1050 postfork = 1;
1037} 1051}
1038 1052
1039/*****************************************************************************/ 1053/*****************************************************************************/
1040 1054
1071inline void 1085inline void
1072timers_reify (EV_P) 1086timers_reify (EV_P)
1073{ 1087{
1074 while (timercnt && ((WT)timers [0])->at <= mn_now) 1088 while (timercnt && ((WT)timers [0])->at <= mn_now)
1075 { 1089 {
1076 struct ev_timer *w = timers [0]; 1090 ev_timer *w = timers [0];
1077 1091
1078 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1092 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1079 1093
1080 /* first reschedule or stop timer */ 1094 /* first reschedule or stop timer */
1081 if (w->repeat) 1095 if (w->repeat)
1099inline void 1113inline void
1100periodics_reify (EV_P) 1114periodics_reify (EV_P)
1101{ 1115{
1102 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1116 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1103 { 1117 {
1104 struct ev_periodic *w = periodics [0]; 1118 ev_periodic *w = periodics [0];
1105 1119
1106 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1120 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1107 1121
1108 /* first reschedule or stop timer */ 1122 /* first reschedule or stop timer */
1109 if (w->reschedule_cb) 1123 if (w->reschedule_cb)
1131 int i; 1145 int i;
1132 1146
1133 /* adjust periodics after time jump */ 1147 /* adjust periodics after time jump */
1134 for (i = 0; i < periodiccnt; ++i) 1148 for (i = 0; i < periodiccnt; ++i)
1135 { 1149 {
1136 struct ev_periodic *w = periodics [i]; 1150 ev_periodic *w = periodics [i];
1137 1151
1138 if (w->reschedule_cb) 1152 if (w->reschedule_cb)
1139 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1153 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1140 else if (w->interval) 1154 else if (w->interval)
1141 ((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;
1231static int loop_done; 1245static int loop_done;
1232 1246
1233void 1247void
1234ev_loop (EV_P_ int flags) 1248ev_loop (EV_P_ int flags)
1235{ 1249{
1236 double block;
1237 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1250 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1251 ? EVUNLOOP_ONE
1252 : EVUNLOOP_CANCEL;
1238 1253
1239 while (activecnt) 1254 while (activecnt)
1240 { 1255 {
1241 /* queue check watchers (and execute them) */ 1256 /* queue check watchers (and execute them) */
1242 if (expect_false (preparecnt)) 1257 if (expect_false (preparecnt))
1251 1266
1252 /* update fd-related kernel structures */ 1267 /* update fd-related kernel structures */
1253 fd_reify (EV_A); 1268 fd_reify (EV_A);
1254 1269
1255 /* calculate blocking time */ 1270 /* calculate blocking time */
1271 {
1272 double block;
1256 1273
1257 /* we only need this for !monotonic clock or timers, but as we basically 1274 if (flags & EVLOOP_NONBLOCK || idlecnt)
1258 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 */
1259#if EV_USE_MONOTONIC 1279#if EV_USE_MONOTONIC
1260 if (expect_true (have_monotonic)) 1280 if (expect_true (have_monotonic))
1261 time_update_monotonic (EV_A); 1281 time_update_monotonic (EV_A);
1262 else 1282 else
1263#endif 1283#endif
1264 { 1284 {
1265 ev_rt_now = ev_time (); 1285 ev_rt_now = ev_time ();
1266 mn_now = ev_rt_now; 1286 mn_now = ev_rt_now;
1267 } 1287 }
1268 1288
1269 if (flags & EVLOOP_NONBLOCK || idlecnt)
1270 block = 0.;
1271 else
1272 {
1273 block = MAX_BLOCKTIME; 1289 block = MAX_BLOCKTIME;
1274 1290
1275 if (timercnt) 1291 if (timercnt)
1276 { 1292 {
1277 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1293 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1278 if (block > to) block = to; 1294 if (block > to) block = to;
1279 } 1295 }
1280 1296
1281#if EV_PERIODICS 1297#if EV_PERIODICS
1282 if (periodiccnt) 1298 if (periodiccnt)
1283 { 1299 {
1284 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;
1285 if (block > to) block = to; 1301 if (block > to) block = to;
1286 } 1302 }
1287#endif 1303#endif
1288 1304
1289 if (expect_false (block < 0.)) block = 0.; 1305 if (expect_false (block < 0.)) block = 0.;
1290 } 1306 }
1291 1307
1292 method_poll (EV_A_ block); 1308 backend_poll (EV_A_ block);
1309 }
1293 1310
1294 /* update ev_rt_now, do magic */ 1311 /* update ev_rt_now, do magic */
1295 time_update (EV_A); 1312 time_update (EV_A);
1296 1313
1297 /* queue pending timers and reschedule them */ 1314 /* queue pending timers and reschedule them */
1312 1329
1313 if (expect_false (loop_done)) 1330 if (expect_false (loop_done))
1314 break; 1331 break;
1315 } 1332 }
1316 1333
1317 if (loop_done != 2) 1334 if (loop_done == EVUNLOOP_ONE)
1318 loop_done = 0; 1335 loop_done = EVUNLOOP_CANCEL;
1319} 1336}
1320 1337
1321void 1338void
1322ev_unloop (EV_P_ int how) 1339ev_unloop (EV_P_ int how)
1323{ 1340{
1376} 1393}
1377 1394
1378/*****************************************************************************/ 1395/*****************************************************************************/
1379 1396
1380void 1397void
1381ev_io_start (EV_P_ struct ev_io *w) 1398ev_io_start (EV_P_ ev_io *w)
1382{ 1399{
1383 int fd = w->fd; 1400 int fd = w->fd;
1384 1401
1385 if (expect_false (ev_is_active (w))) 1402 if (expect_false (ev_is_active (w)))
1386 return; 1403 return;
1393 1410
1394 fd_change (EV_A_ fd); 1411 fd_change (EV_A_ fd);
1395} 1412}
1396 1413
1397void 1414void
1398ev_io_stop (EV_P_ struct ev_io *w) 1415ev_io_stop (EV_P_ ev_io *w)
1399{ 1416{
1400 ev_clear_pending (EV_A_ (W)w); 1417 ev_clear_pending (EV_A_ (W)w);
1401 if (expect_false (!ev_is_active (w))) 1418 if (expect_false (!ev_is_active (w)))
1402 return; 1419 return;
1403 1420
1408 1425
1409 fd_change (EV_A_ w->fd); 1426 fd_change (EV_A_ w->fd);
1410} 1427}
1411 1428
1412void 1429void
1413ev_timer_start (EV_P_ struct ev_timer *w) 1430ev_timer_start (EV_P_ ev_timer *w)
1414{ 1431{
1415 if (expect_false (ev_is_active (w))) 1432 if (expect_false (ev_is_active (w)))
1416 return; 1433 return;
1417 1434
1418 ((WT)w)->at += mn_now; 1435 ((WT)w)->at += mn_now;
1419 1436
1420 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.));
1421 1438
1422 ev_start (EV_A_ (W)w, ++timercnt); 1439 ev_start (EV_A_ (W)w, ++timercnt);
1423 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1440 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1424 timers [timercnt - 1] = w; 1441 timers [timercnt - 1] = w;
1425 upheap ((WT *)timers, timercnt - 1); 1442 upheap ((WT *)timers, timercnt - 1);
1426 1443
1427 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1428} 1445}
1429 1446
1430void 1447void
1431ev_timer_stop (EV_P_ struct ev_timer *w) 1448ev_timer_stop (EV_P_ ev_timer *w)
1432{ 1449{
1433 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1434 if (expect_false (!ev_is_active (w))) 1451 if (expect_false (!ev_is_active (w)))
1435 return; 1452 return;
1436 1453
1446 1463
1447 ev_stop (EV_A_ (W)w); 1464 ev_stop (EV_A_ (W)w);
1448} 1465}
1449 1466
1450void 1467void
1451ev_timer_again (EV_P_ struct ev_timer *w) 1468ev_timer_again (EV_P_ ev_timer *w)
1452{ 1469{
1453 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1454 { 1471 {
1455 if (w->repeat) 1472 if (w->repeat)
1456 { 1473 {
1467 } 1484 }
1468} 1485}
1469 1486
1470#if EV_PERIODICS 1487#if EV_PERIODICS
1471void 1488void
1472ev_periodic_start (EV_P_ struct ev_periodic *w) 1489ev_periodic_start (EV_P_ ev_periodic *w)
1473{ 1490{
1474 if (expect_false (ev_is_active (w))) 1491 if (expect_false (ev_is_active (w)))
1475 return; 1492 return;
1476 1493
1477 if (w->reschedule_cb) 1494 if (w->reschedule_cb)
1482 /* 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 */
1483 ((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;
1484 } 1501 }
1485 1502
1486 ev_start (EV_A_ (W)w, ++periodiccnt); 1503 ev_start (EV_A_ (W)w, ++periodiccnt);
1487 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1504 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1488 periodics [periodiccnt - 1] = w; 1505 periodics [periodiccnt - 1] = w;
1489 upheap ((WT *)periodics, periodiccnt - 1); 1506 upheap ((WT *)periodics, periodiccnt - 1);
1490 1507
1491 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1492} 1509}
1493 1510
1494void 1511void
1495ev_periodic_stop (EV_P_ struct ev_periodic *w) 1512ev_periodic_stop (EV_P_ ev_periodic *w)
1496{ 1513{
1497 ev_clear_pending (EV_A_ (W)w); 1514 ev_clear_pending (EV_A_ (W)w);
1498 if (expect_false (!ev_is_active (w))) 1515 if (expect_false (!ev_is_active (w)))
1499 return; 1516 return;
1500 1517
1508 1525
1509 ev_stop (EV_A_ (W)w); 1526 ev_stop (EV_A_ (W)w);
1510} 1527}
1511 1528
1512void 1529void
1513ev_periodic_again (EV_P_ struct ev_periodic *w) 1530ev_periodic_again (EV_P_ ev_periodic *w)
1514{ 1531{
1515 /* TODO: use adjustheap and recalculation */ 1532 /* TODO: use adjustheap and recalculation */
1516 ev_periodic_stop (EV_A_ w); 1533 ev_periodic_stop (EV_A_ w);
1517 ev_periodic_start (EV_A_ w); 1534 ev_periodic_start (EV_A_ w);
1518} 1535}
1519#endif 1536#endif
1520 1537
1521void 1538void
1522ev_idle_start (EV_P_ struct ev_idle *w) 1539ev_idle_start (EV_P_ ev_idle *w)
1523{ 1540{
1524 if (expect_false (ev_is_active (w))) 1541 if (expect_false (ev_is_active (w)))
1525 return; 1542 return;
1526 1543
1527 ev_start (EV_A_ (W)w, ++idlecnt); 1544 ev_start (EV_A_ (W)w, ++idlecnt);
1528 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1545 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1529 idles [idlecnt - 1] = w; 1546 idles [idlecnt - 1] = w;
1530} 1547}
1531 1548
1532void 1549void
1533ev_idle_stop (EV_P_ struct ev_idle *w) 1550ev_idle_stop (EV_P_ ev_idle *w)
1534{ 1551{
1535 ev_clear_pending (EV_A_ (W)w); 1552 ev_clear_pending (EV_A_ (W)w);
1536 if (expect_false (!ev_is_active (w))) 1553 if (expect_false (!ev_is_active (w)))
1537 return; 1554 return;
1538 1555
1539 idles [((W)w)->active - 1] = idles [--idlecnt]; 1556 idles [((W)w)->active - 1] = idles [--idlecnt];
1540 ev_stop (EV_A_ (W)w); 1557 ev_stop (EV_A_ (W)w);
1541} 1558}
1542 1559
1543void 1560void
1544ev_prepare_start (EV_P_ struct ev_prepare *w) 1561ev_prepare_start (EV_P_ ev_prepare *w)
1545{ 1562{
1546 if (expect_false (ev_is_active (w))) 1563 if (expect_false (ev_is_active (w)))
1547 return; 1564 return;
1548 1565
1549 ev_start (EV_A_ (W)w, ++preparecnt); 1566 ev_start (EV_A_ (W)w, ++preparecnt);
1550 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1567 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1551 prepares [preparecnt - 1] = w; 1568 prepares [preparecnt - 1] = w;
1552} 1569}
1553 1570
1554void 1571void
1555ev_prepare_stop (EV_P_ struct ev_prepare *w) 1572ev_prepare_stop (EV_P_ ev_prepare *w)
1556{ 1573{
1557 ev_clear_pending (EV_A_ (W)w); 1574 ev_clear_pending (EV_A_ (W)w);
1558 if (expect_false (!ev_is_active (w))) 1575 if (expect_false (!ev_is_active (w)))
1559 return; 1576 return;
1560 1577
1561 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1578 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1562 ev_stop (EV_A_ (W)w); 1579 ev_stop (EV_A_ (W)w);
1563} 1580}
1564 1581
1565void 1582void
1566ev_check_start (EV_P_ struct ev_check *w) 1583ev_check_start (EV_P_ ev_check *w)
1567{ 1584{
1568 if (expect_false (ev_is_active (w))) 1585 if (expect_false (ev_is_active (w)))
1569 return; 1586 return;
1570 1587
1571 ev_start (EV_A_ (W)w, ++checkcnt); 1588 ev_start (EV_A_ (W)w, ++checkcnt);
1572 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1589 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1573 checks [checkcnt - 1] = w; 1590 checks [checkcnt - 1] = w;
1574} 1591}
1575 1592
1576void 1593void
1577ev_check_stop (EV_P_ struct ev_check *w) 1594ev_check_stop (EV_P_ ev_check *w)
1578{ 1595{
1579 ev_clear_pending (EV_A_ (W)w); 1596 ev_clear_pending (EV_A_ (W)w);
1580 if (expect_false (!ev_is_active (w))) 1597 if (expect_false (!ev_is_active (w)))
1581 return; 1598 return;
1582 1599
1587#ifndef SA_RESTART 1604#ifndef SA_RESTART
1588# define SA_RESTART 0 1605# define SA_RESTART 0
1589#endif 1606#endif
1590 1607
1591void 1608void
1592ev_signal_start (EV_P_ struct ev_signal *w) 1609ev_signal_start (EV_P_ ev_signal *w)
1593{ 1610{
1594#if EV_MULTIPLICITY 1611#if EV_MULTIPLICITY
1595 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));
1596#endif 1613#endif
1597 if (expect_false (ev_is_active (w))) 1614 if (expect_false (ev_is_active (w)))
1616#endif 1633#endif
1617 } 1634 }
1618} 1635}
1619 1636
1620void 1637void
1621ev_signal_stop (EV_P_ struct ev_signal *w) 1638ev_signal_stop (EV_P_ ev_signal *w)
1622{ 1639{
1623 ev_clear_pending (EV_A_ (W)w); 1640 ev_clear_pending (EV_A_ (W)w);
1624 if (expect_false (!ev_is_active (w))) 1641 if (expect_false (!ev_is_active (w)))
1625 return; 1642 return;
1626 1643
1630 if (!signals [w->signum - 1].head) 1647 if (!signals [w->signum - 1].head)
1631 signal (w->signum, SIG_DFL); 1648 signal (w->signum, SIG_DFL);
1632} 1649}
1633 1650
1634void 1651void
1635ev_child_start (EV_P_ struct ev_child *w) 1652ev_child_start (EV_P_ ev_child *w)
1636{ 1653{
1637#if EV_MULTIPLICITY 1654#if EV_MULTIPLICITY
1638 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));
1639#endif 1656#endif
1640 if (expect_false (ev_is_active (w))) 1657 if (expect_false (ev_is_active (w)))
1643 ev_start (EV_A_ (W)w, 1); 1660 ev_start (EV_A_ (W)w, 1);
1644 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1645} 1662}
1646 1663
1647void 1664void
1648ev_child_stop (EV_P_ struct ev_child *w) 1665ev_child_stop (EV_P_ ev_child *w)
1649{ 1666{
1650 ev_clear_pending (EV_A_ (W)w); 1667 ev_clear_pending (EV_A_ (W)w);
1651 if (expect_false (!ev_is_active (w))) 1668 if (expect_false (!ev_is_active (w)))
1652 return; 1669 return;
1653 1670
1654 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1655 ev_stop (EV_A_ (W)w); 1672 ev_stop (EV_A_ (W)w);
1656} 1673}
1657 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
1658/*****************************************************************************/ 1722/*****************************************************************************/
1659 1723
1660struct ev_once 1724struct ev_once
1661{ 1725{
1662 struct ev_io io; 1726 ev_io io;
1663 struct ev_timer to; 1727 ev_timer to;
1664 void (*cb)(int revents, void *arg); 1728 void (*cb)(int revents, void *arg);
1665 void *arg; 1729 void *arg;
1666}; 1730};
1667 1731
1668static void 1732static void
1677 1741
1678 cb (revents, arg); 1742 cb (revents, arg);
1679} 1743}
1680 1744
1681static void 1745static void
1682once_cb_io (EV_P_ struct ev_io *w, int revents) 1746once_cb_io (EV_P_ ev_io *w, int revents)
1683{ 1747{
1684 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);
1685} 1749}
1686 1750
1687static void 1751static void
1688once_cb_to (EV_P_ struct ev_timer *w, int revents) 1752once_cb_to (EV_P_ ev_timer *w, int revents)
1689{ 1753{
1690 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);
1691} 1755}
1692 1756
1693void 1757void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines