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

Comparing libev/ev.c (file contents):
Revision 1.302 by root, Thu Jul 16 15:08:08 2009 UTC vs.
Revision 1.303 by root, Sun Jul 19 01:36:34 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
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
2577 if (expect_false (ev_is_active (w))) 2639 if (expect_false (ev_is_active (w)))
2578 return; 2640 return;
2579 2641
2580 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));
2581 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
2582 evpipe_init (EV_A); 2676 evpipe_init (EV_A);
2583
2584 EV_FREQUENT_CHECK;
2585 2677
2586 { 2678 {
2587#ifndef _WIN32 2679#ifndef _WIN32
2588 sigset_t full, prev; 2680 sigset_t full, prev;
2589 sigfillset (&full); 2681 sigfillset (&full);
2591#endif 2683#endif
2592 2684
2593 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2685 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2594 2686
2595#ifndef _WIN32 2687#ifndef _WIN32
2688 if (sigfd < 0)/*TODO*/
2689 sigdelset (&prev, w->signum);
2596 sigprocmask (SIG_SETMASK, &prev, 0); 2690 sigprocmask (SIG_SETMASK, &prev, 0);
2597#endif 2691#endif
2598 } 2692 }
2599 2693
2600 ev_start (EV_A_ (W)w, 1); 2694 ev_start (EV_A_ (W)w, 1);
2603 if (!((WL)w)->next) 2697 if (!((WL)w)->next)
2604 { 2698 {
2605#if _WIN32 2699#if _WIN32
2606 signal (w->signum, ev_sighandler); 2700 signal (w->signum, ev_sighandler);
2607#else 2701#else
2702 if (sigfd < 0) /*TODO*/
2703 {
2608 struct sigaction sa = { }; 2704 struct sigaction sa = { };
2609 sa.sa_handler = ev_sighandler; 2705 sa.sa_handler = ev_sighandler;
2610 sigfillset (&sa.sa_mask); 2706 sigfillset (&sa.sa_mask);
2611 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2707 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2612 sigaction (w->signum, &sa, 0); 2708 sigaction (w->signum, &sa, 0);
2709 }
2613#endif 2710#endif
2614 } 2711 }
2615 2712
2616 EV_FREQUENT_CHECK; 2713 EV_FREQUENT_CHECK;
2617} 2714}
2627 2724
2628 wlist_del (&signals [w->signum - 1].head, (WL)w); 2725 wlist_del (&signals [w->signum - 1].head, (WL)w);
2629 ev_stop (EV_A_ (W)w); 2726 ev_stop (EV_A_ (W)w);
2630 2727
2631 if (!signals [w->signum - 1].head) 2728 if (!signals [w->signum - 1].head)
2729#if EV_USE_SIGNALFD
2730 if (sigfd >= 0)
2731 {
2732 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2733 sigdelset (&sigfd_set, w->signum);
2734 signalfd (sigfd, &sigfd_set, 0);
2735 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2736 /*TODO: maybe unblock signal? */
2737 }
2738 else
2739#endif
2632 signal (w->signum, SIG_DFL); 2740 signal (w->signum, SIG_DFL);
2633 2741
2634 EV_FREQUENT_CHECK; 2742 EV_FREQUENT_CHECK;
2635} 2743}
2636 2744
2637void 2745void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines