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

Comparing libev/ev.c (file contents):
Revision 1.209 by root, Tue Feb 5 23:56:33 2008 UTC vs.
Revision 1.219 by root, Wed Apr 2 10:55:39 2008 UTC

451 ts.tv_sec = (time_t)delay; 451 ts.tv_sec = (time_t)delay;
452 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); 452 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
453 453
454 nanosleep (&ts, 0); 454 nanosleep (&ts, 0);
455#elif defined(_WIN32) 455#elif defined(_WIN32)
456 Sleep (delay * 1e3); 456 Sleep ((unsigned long)(delay * 1e3));
457#else 457#else
458 struct timeval tv; 458 struct timeval tv;
459 459
460 tv.tv_sec = (time_t)delay; 460 tv.tv_sec = (time_t)delay;
461 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 461 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
810 fd_intern (evpipe [0]); 810 fd_intern (evpipe [0]);
811 fd_intern (evpipe [1]); 811 fd_intern (evpipe [1]);
812 812
813 ev_io_set (&pipeev, evpipe [0], EV_READ); 813 ev_io_set (&pipeev, evpipe [0], EV_READ);
814 ev_io_start (EV_A_ &pipeev); 814 ev_io_start (EV_A_ &pipeev);
815 ev_unref (EV_A); /* child watcher should not keep loop alive */ 815 ev_unref (EV_A); /* watcher should not keep loop alive */
816 } 816 }
817} 817}
818 818
819void inline_size 819void inline_size
820evpipe_write (EV_P_ int sig, int async) 820evpipe_write (EV_P_ EV_ATOMIC_T *flag)
821{ 821{
822 if (!(gotasync || gotsig)) 822 if (!*flag)
823 { 823 {
824 int old_errno = errno; 824 int old_errno = errno; /* save errno because write might clobber it */
825 825
826 if (sig) gotsig = 1; 826 *flag = 1;
827 if (async) gotasync = 1;
828
829 write (evpipe [1], &old_errno, 1); 827 write (evpipe [1], &old_errno, 1);
828
830 errno = old_errno; 829 errno = old_errno;
831 } 830 }
832} 831}
833 832
834static void 833static void
837 { 836 {
838 int dummy; 837 int dummy;
839 read (evpipe [0], &dummy, 1); 838 read (evpipe [0], &dummy, 1);
840 } 839 }
841 840
842 if (gotsig) 841 if (gotsig && ev_is_default_loop (EV_A))
843 { 842 {
844 int signum; 843 int signum;
845 gotsig = 0; 844 gotsig = 0;
846 845
847 for (signum = signalmax; signum--; ) 846 for (signum = signalmax; signum--; )
866} 865}
867 866
868/*****************************************************************************/ 867/*****************************************************************************/
869 868
870static void 869static void
871sighandler (int signum) 870ev_sighandler (int signum)
872{ 871{
873#if EV_MULTIPLICITY 872#if EV_MULTIPLICITY
874 struct ev_loop *loop = &default_loop_struct; 873 struct ev_loop *loop = &default_loop_struct;
875#endif 874#endif
876 875
877#if _WIN32 876#if _WIN32
878 signal (signum, sighandler); 877 signal (signum, ev_sighandler);
879#endif 878#endif
880 879
881 signals [signum - 1].gotsig = 1; 880 signals [signum - 1].gotsig = 1;
882 evpipe_write (EV_A_ 1, 0); 881 evpipe_write (EV_A_ &gotsig);
883} 882}
884 883
885void noinline 884void noinline
886ev_feed_signal_event (EV_P_ int signum) 885ev_feed_signal_event (EV_P_ int signum)
887{ 886{
913#ifndef WIFCONTINUED 912#ifndef WIFCONTINUED
914# define WIFCONTINUED(status) 0 913# define WIFCONTINUED(status) 0
915#endif 914#endif
916 915
917void inline_speed 916void inline_speed
918child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 917child_reap (EV_P_ int chain, int pid, int status)
919{ 918{
920 ev_child *w; 919 ev_child *w;
921 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 920 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
922 921
923 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 922 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
924 { 923 {
925 if ((w->pid == pid || !w->pid) 924 if ((w->pid == pid || !w->pid)
926 && (!traced || (w->flags & 1))) 925 && (!traced || (w->flags & 1)))
927 { 926 {
928 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ 927 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
929 w->rpid = pid; 928 w->rpid = pid;
930 w->rstatus = status; 929 w->rstatus = status;
931 ev_feed_event (EV_A_ (W)w, EV_CHILD); 930 ev_feed_event (EV_A_ (W)w, EV_CHILD);
932 } 931 }
933 } 932 }
947 if (!WCONTINUED 946 if (!WCONTINUED
948 || errno != EINVAL 947 || errno != EINVAL
949 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) 948 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
950 return; 949 return;
951 950
952 /* make sure we are called again until all childs have been reaped */ 951 /* make sure we are called again until all children have been reaped */
953 /* we need to do it this way so that the callback gets called before we continue */ 952 /* we need to do it this way so that the callback gets called before we continue */
954 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 953 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
955 954
956 child_reap (EV_A_ sw, pid, pid, status); 955 child_reap (EV_A_ pid, pid, status);
957 if (EV_PID_HASHSIZE > 1) 956 if (EV_PID_HASHSIZE > 1)
958 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 957 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
959} 958}
960 959
961#endif 960#endif
962 961
963/*****************************************************************************/ 962/*****************************************************************************/
1217#endif 1216#endif
1218 1217
1219 if (ev_is_active (&pipeev)) 1218 if (ev_is_active (&pipeev))
1220 { 1219 {
1221 /* this "locks" the handlers against writing to the pipe */ 1220 /* this "locks" the handlers against writing to the pipe */
1221 /* while we modify the fd vars */
1222 gotsig = 1;
1223#if EV_ASYNC_ENABLE
1222 gotsig = gotasync = 1; 1224 gotasync = 1;
1225#endif
1223 1226
1224 ev_ref (EV_A); 1227 ev_ref (EV_A);
1225 ev_io_stop (EV_A_ &pipeev); 1228 ev_io_stop (EV_A_ &pipeev);
1226 close (evpipe [0]); 1229 close (evpipe [0]);
1227 close (evpipe [1]); 1230 close (evpipe [1]);
1539static int loop_done; 1542static int loop_done;
1540 1543
1541void 1544void
1542ev_loop (EV_P_ int flags) 1545ev_loop (EV_P_ int flags)
1543{ 1546{
1544 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1547 loop_done = EVUNLOOP_CANCEL;
1545 ? EVUNLOOP_ONE
1546 : EVUNLOOP_CANCEL;
1547 1548
1548 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1549 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1549 1550
1550 do 1551 do
1551 { 1552 {
1647 /* queue check watchers, to be executed first */ 1648 /* queue check watchers, to be executed first */
1648 if (expect_false (checkcnt)) 1649 if (expect_false (checkcnt))
1649 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1650 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1650 1651
1651 call_pending (EV_A); 1652 call_pending (EV_A);
1652
1653 } 1653 }
1654 while (expect_true (activecnt && !loop_done)); 1654 while (expect_true (
1655 activecnt
1656 && !loop_done
1657 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
1658 ));
1655 1659
1656 if (loop_done == EVUNLOOP_ONE) 1660 if (loop_done == EVUNLOOP_ONE)
1657 loop_done = EVUNLOOP_CANCEL; 1661 loop_done = EVUNLOOP_CANCEL;
1658} 1662}
1659 1663
1927 wlist_add (&signals [w->signum - 1].head, (WL)w); 1931 wlist_add (&signals [w->signum - 1].head, (WL)w);
1928 1932
1929 if (!((WL)w)->next) 1933 if (!((WL)w)->next)
1930 { 1934 {
1931#if _WIN32 1935#if _WIN32
1932 signal (w->signum, sighandler); 1936 signal (w->signum, ev_sighandler);
1933#else 1937#else
1934 struct sigaction sa; 1938 struct sigaction sa;
1935 sa.sa_handler = sighandler; 1939 sa.sa_handler = ev_sighandler;
1936 sigfillset (&sa.sa_mask); 1940 sigfillset (&sa.sa_mask);
1937 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1941 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1938 sigaction (w->signum, &sa, 0); 1942 sigaction (w->signum, &sa, 0);
1939#endif 1943#endif
1940 } 1944 }
2463 2467
2464void 2468void
2465ev_async_send (EV_P_ ev_async *w) 2469ev_async_send (EV_P_ ev_async *w)
2466{ 2470{
2467 w->sent = 1; 2471 w->sent = 1;
2468 evpipe_write (EV_A_ 0, 1); 2472 evpipe_write (EV_A_ &gotasync);
2469} 2473}
2470#endif 2474#endif
2471 2475
2472/*****************************************************************************/ 2476/*****************************************************************************/
2473 2477

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines