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

Comparing libev/ev.c (file contents):
Revision 1.299 by root, Tue Jul 14 00:09:59 2009 UTC vs.
Revision 1.304 by root, Sun Jul 19 03:12:28 2009 UTC

133# else 133# else
134# define EV_USE_INOTIFY 0 134# define EV_USE_INOTIFY 0
135# endif 135# endif
136# endif 136# endif
137 137
138# ifndef EV_USE_SIGNALFD
139# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
140# define EV_USE_SIGNALFD 1
141# else
142# define EV_USE_SIGNALFD 0
143# endif
144# endif
145
138# ifndef EV_USE_EVENTFD 146# ifndef EV_USE_EVENTFD
139# if HAVE_EVENTFD 147# if HAVE_EVENTFD
140# define EV_USE_EVENTFD 1 148# define EV_USE_EVENTFD 1
141# else 149# else
142# define EV_USE_EVENTFD 0 150# define EV_USE_EVENTFD 0
263#ifndef EV_USE_EVENTFD 271#ifndef EV_USE_EVENTFD
264# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 272# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
265# define EV_USE_EVENTFD 1 273# define EV_USE_EVENTFD 1
266# else 274# else
267# define EV_USE_EVENTFD 0 275# define EV_USE_EVENTFD 0
276# endif
277#endif
278
279#ifndef EV_USE_SIGNALFD
280# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9))
281# define EV_USE_SIGNALFD 1
282# else
283# define EV_USE_SIGNALFD 0
268# endif 284# endif
269#endif 285#endif
270 286
271#if 0 /* debugging */ 287#if 0 /* debugging */
272# define EV_VERIFY 3 288# define EV_VERIFY 3
339#endif 355#endif
340 356
341#if EV_USE_EVENTFD 357#if EV_USE_EVENTFD
342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 358/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
343# include <stdint.h> 359# include <stdint.h>
360# ifndef EFD_NONBLOCK
361# define EFD_NONBLOCK O_NONBLOCK
362# endif
363# ifndef EFD_CLOEXEC
364# define EFD_CLOEXEC O_CLOEXEC
365# endif
344# ifdef __cplusplus 366# ifdef __cplusplus
345extern "C" { 367extern "C" {
346# endif 368# endif
347int eventfd (unsigned int initval, int flags); 369int eventfd (unsigned int initval, int flags);
348# ifdef __cplusplus 370# ifdef __cplusplus
349} 371}
350# endif 372# endif
373#endif
374
375#if EV_USE_SIGNALFD
376# include <sys/signalfd.h>
351#endif 377#endif
352 378
353/**/ 379/**/
354 380
355#if EV_VERIFY >= 3 381#if EV_VERIFY >= 3
647 673
648 tv.tv_sec = (time_t)delay; 674 tv.tv_sec = (time_t)delay;
649 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 675 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
650 676
651 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 677 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
652 /* somehting not guaranteed by newer posix versions, but guaranteed */ 678 /* something not guaranteed by newer posix versions, but guaranteed */
653 /* by older ones */ 679 /* by older ones */
654 select (0, 0, 0, 0, &tv); 680 select (0, 0, 0, 0, &tv);
655#endif 681#endif
656 } 682 }
657} 683}
1104evpipe_init (EV_P) 1130evpipe_init (EV_P)
1105{ 1131{
1106 if (!ev_is_active (&pipe_w)) 1132 if (!ev_is_active (&pipe_w))
1107 { 1133 {
1108#if EV_USE_EVENTFD 1134#if EV_USE_EVENTFD
1135 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1136 if (evfd < 0 && errno == EINVAL)
1109 if ((evfd = eventfd (0, 0)) >= 0) 1137 evfd = eventfd (0, 0);
1138
1139 if (evfd >= 0)
1110 { 1140 {
1111 evpipe [0] = -1; 1141 evpipe [0] = -1;
1112 fd_intern (evfd); 1142 fd_intern (evfd); /* doing it twice doesn't hurt */
1113 ev_io_set (&pipe_w, evfd, EV_READ); 1143 ev_io_set (&pipe_w, evfd, EV_READ);
1114 } 1144 }
1115 else 1145 else
1116#endif 1146#endif
1117 { 1147 {
1229 signals [signum].gotsig = 0; 1259 signals [signum].gotsig = 0;
1230 1260
1231 for (w = signals [signum].head; w; w = w->next) 1261 for (w = signals [signum].head; w; w = w->next)
1232 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1262 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1233} 1263}
1264
1265#if EV_USE_SIGNALFD
1266static void
1267sigfdcb (EV_P_ ev_io *iow, int revents)
1268{
1269 struct signalfd_siginfo si[4], *sip;
1270
1271 for (;;)
1272 {
1273 ssize_t res = read (sigfd, si, sizeof (si));
1274
1275 /* not ISO-C, as res might be -1, but works with SuS */
1276 for (sip = si; (char *)sip < (char *)si + res; ++sip)
1277 ev_feed_signal_event (EV_A_ sip->ssi_signo);
1278
1279 if (res < (ssize_t)sizeof (si))
1280 break;
1281 }
1282}
1283#endif
1234 1284
1235/*****************************************************************************/ 1285/*****************************************************************************/
1236 1286
1237static WL childs [EV_PID_HASHSIZE]; 1287static WL childs [EV_PID_HASHSIZE];
1238 1288
1474 backend_fd = -1; 1524 backend_fd = -1;
1475 gotasync = 0; 1525 gotasync = 0;
1476#if EV_USE_INOTIFY 1526#if EV_USE_INOTIFY
1477 fs_fd = -2; 1527 fs_fd = -2;
1478#endif 1528#endif
1529#if EV_USE_SIGNALFD
1530 sigfd = -2;
1531#endif
1479 1532
1480 /* pid check not overridable via env */ 1533 /* pid check not overridable via env */
1481#ifndef _WIN32 1534#ifndef _WIN32
1482 if (flags & EVFLAG_FORKCHECK) 1535 if (flags & EVFLAG_FORKCHECK)
1483 curpid = getpid (); 1536 curpid = getpid ();
1520{ 1573{
1521 int i; 1574 int i;
1522 1575
1523 if (ev_is_active (&pipe_w)) 1576 if (ev_is_active (&pipe_w))
1524 { 1577 {
1525 ev_ref (EV_A); /* signal watcher */ 1578 /*ev_ref (EV_A);*/
1526 ev_io_stop (EV_A_ &pipe_w); 1579 /*ev_io_stop (EV_A_ &pipe_w);*/
1527 1580
1528#if EV_USE_EVENTFD 1581#if EV_USE_EVENTFD
1529 if (evfd >= 0) 1582 if (evfd >= 0)
1530 close (evfd); 1583 close (evfd);
1531#endif 1584#endif
1534 { 1587 {
1535 close (evpipe [0]); 1588 close (evpipe [0]);
1536 close (evpipe [1]); 1589 close (evpipe [1]);
1537 } 1590 }
1538 } 1591 }
1592
1593#if EV_USE_SIGNALFD
1594 if (ev_is_active (&sigfd_w))
1595 {
1596 /*ev_ref (EV_A);*/
1597 /*ev_io_stop (EV_A_ &sigfd_w);*/
1598
1599 close (sigfd);
1600 }
1601#endif
1539 1602
1540#if EV_USE_INOTIFY 1603#if EV_USE_INOTIFY
1541 if (fs_fd >= 0) 1604 if (fs_fd >= 0)
1542 close (fs_fd); 1605 close (fs_fd);
1543#endif 1606#endif
1647ev_loop_new (unsigned int flags) 1710ev_loop_new (unsigned int flags)
1648{ 1711{
1649 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1712 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1650 1713
1651 memset (loop, 0, sizeof (struct ev_loop)); 1714 memset (loop, 0, sizeof (struct ev_loop));
1652
1653 loop_init (EV_A_ flags); 1715 loop_init (EV_A_ flags);
1654 1716
1655 if (ev_backend (EV_A)) 1717 if (ev_backend (EV_A))
1656 return loop; 1718 return loop;
1657 1719
1838 1900
1839void 1901void
1840ev_invoke (EV_P_ void *w, int revents) 1902ev_invoke (EV_P_ void *w, int revents)
1841{ 1903{
1842 EV_CB_INVOKE ((W)w, revents); 1904 EV_CB_INVOKE ((W)w, revents);
1905}
1906
1907unsigned int
1908ev_pending_count (EV_P)
1909{
1910 int pri;
1911 unsigned int count = 0;
1912
1913 for (pri = NUMPRI; pri--; )
1914 count += pendingcnt [pri];
1915
1916 return count;
1843} 1917}
1844 1918
1845void noinline 1919void noinline
1846ev_invoke_pending (EV_P) 1920ev_invoke_pending (EV_P)
1847{ 1921{
2475 } 2549 }
2476 2550
2477 EV_FREQUENT_CHECK; 2551 EV_FREQUENT_CHECK;
2478} 2552}
2479 2553
2554ev_tstamp
2555ev_timer_remaining (EV_P_ ev_timer *w)
2556{
2557 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2558}
2559
2480#if EV_PERIODIC_ENABLE 2560#if EV_PERIODIC_ENABLE
2481void noinline 2561void noinline
2482ev_periodic_start (EV_P_ ev_periodic *w) 2562ev_periodic_start (EV_P_ ev_periodic *w)
2483{ 2563{
2484 if (expect_false (ev_is_active (w))) 2564 if (expect_false (ev_is_active (w)))
2559 if (expect_false (ev_is_active (w))) 2639 if (expect_false (ev_is_active (w)))
2560 return; 2640 return;
2561 2641
2562 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); 2642 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0));
2563 2643
2644 EV_FREQUENT_CHECK;
2645
2646#if EV_USE_SIGNALFD
2647 if (sigfd == -2)
2648 {
2649 sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
2650 if (sigfd < 0 && errno == EINVAL)
2651 sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
2652
2653 if (sigfd >= 0)
2654 {
2655 fd_intern (sigfd); /* doing it twice will not hurt */
2656
2657 sigemptyset (&sigfd_set);
2658
2659 ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
2660 ev_set_priority (&sigfd_w, EV_MAXPRI);
2661 ev_io_start (EV_A_ &sigfd_w);
2662 ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
2663 }
2664 }
2665
2666 if (sigfd >= 0)
2667 {
2668 /* TODO: check .head */
2669 sigaddset (&sigfd_set, w->signum);
2670 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2671
2672 signalfd (sigfd, &sigfd_set, 0);
2673 }
2674 else
2675#endif
2564 evpipe_init (EV_A); 2676 evpipe_init (EV_A);
2565
2566 EV_FREQUENT_CHECK;
2567 2677
2568 { 2678 {
2569#ifndef _WIN32 2679#ifndef _WIN32
2570 sigset_t full, prev; 2680 sigset_t full, prev;
2571 sigfillset (&full); 2681 sigfillset (&full);
2573#endif 2683#endif
2574 2684
2575 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2685 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2576 2686
2577#ifndef _WIN32 2687#ifndef _WIN32
2688# if EV_USE_SIGNALFD
2689 if (sigfd < 0)/*TODO*/
2690# endif
2691 sigdelset (&prev, w->signum);
2578 sigprocmask (SIG_SETMASK, &prev, 0); 2692 sigprocmask (SIG_SETMASK, &prev, 0);
2579#endif 2693#endif
2580 } 2694 }
2581 2695
2582 ev_start (EV_A_ (W)w, 1); 2696 ev_start (EV_A_ (W)w, 1);
2585 if (!((WL)w)->next) 2699 if (!((WL)w)->next)
2586 { 2700 {
2587#if _WIN32 2701#if _WIN32
2588 signal (w->signum, ev_sighandler); 2702 signal (w->signum, ev_sighandler);
2589#else 2703#else
2704# if EV_USE_SIGNALFD
2705 if (sigfd < 0) /*TODO*/
2706# endif
2707 {
2590 struct sigaction sa = { }; 2708 struct sigaction sa = { };
2591 sa.sa_handler = ev_sighandler; 2709 sa.sa_handler = ev_sighandler;
2592 sigfillset (&sa.sa_mask); 2710 sigfillset (&sa.sa_mask);
2593 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2711 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2594 sigaction (w->signum, &sa, 0); 2712 sigaction (w->signum, &sa, 0);
2713 }
2595#endif 2714#endif
2596 } 2715 }
2597 2716
2598 EV_FREQUENT_CHECK; 2717 EV_FREQUENT_CHECK;
2599} 2718}
2609 2728
2610 wlist_del (&signals [w->signum - 1].head, (WL)w); 2729 wlist_del (&signals [w->signum - 1].head, (WL)w);
2611 ev_stop (EV_A_ (W)w); 2730 ev_stop (EV_A_ (W)w);
2612 2731
2613 if (!signals [w->signum - 1].head) 2732 if (!signals [w->signum - 1].head)
2733#if EV_USE_SIGNALFD
2734 if (sigfd >= 0)
2735 {
2736 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2737 sigdelset (&sigfd_set, w->signum);
2738 signalfd (sigfd, &sigfd_set, 0);
2739 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2740 /*TODO: maybe unblock signal? */
2741 }
2742 else
2743#endif
2614 signal (w->signum, SIG_DFL); 2744 signal (w->signum, SIG_DFL);
2615 2745
2616 EV_FREQUENT_CHECK; 2746 EV_FREQUENT_CHECK;
2617} 2747}
2618 2748
2619void 2749void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines