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.305 by root, Sun Jul 19 03:49:04 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
178# endif 186# endif
179#endif 187#endif
180 188
181/* this block tries to deduce configuration from header-defined symbols and defaults */ 189/* this block tries to deduce configuration from header-defined symbols and defaults */
182 190
191/* try to deduce the maximum number of signals on this platform */
192#if defined (EV_NSIG)
193/* use what's provided */
194#elif defined (NSIG)
195# define EV_NSIG (NSIG)
196#elif defined(_NSIG)
197# define EV_NSIG (_NSIG)
198#elif defined (SIGMAX)
199# define EV_NSIG (SIGMAX+1)
200#elif defined (SIG_MAX)
201# define EV_NSIG (SIG_MAX+1)
202#elif defined (_SIG_MAX)
203# define EV_NSIG (_SIG_MAX+1)
204#elif defined (MAXSIG)
205# define EV_NSIG (MAXSIG+1)
206#elif defined (MAX_SIG)
207# define EV_NSIG (MAX_SIG+1)
208#elif defined (SIGARRAYSIZE)
209# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */
210#elif defined (_sys_nsig)
211# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
212#else
213# error "unable to find value for NSIG, please report"
214/* to make it compile regardless, just remove the above line */
215# define EV_NSIG 64
216#endif
217
218/* Default to some arbitrary number that's big enough to get most
219 of the common signals.
220*/
221#ifndef NSIG
222# define NSIG 50
223#endif
224/* <-- NSIG logic from Configure */
183#ifndef EV_USE_CLOCK_SYSCALL 225#ifndef EV_USE_CLOCK_SYSCALL
184# if __linux && __GLIBC__ >= 2 226# if __linux && __GLIBC__ >= 2
185# define EV_USE_CLOCK_SYSCALL 1 227# define EV_USE_CLOCK_SYSCALL 1
186# else 228# else
187# define EV_USE_CLOCK_SYSCALL 0 229# define EV_USE_CLOCK_SYSCALL 0
263#ifndef EV_USE_EVENTFD 305#ifndef EV_USE_EVENTFD
264# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 306# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
265# define EV_USE_EVENTFD 1 307# define EV_USE_EVENTFD 1
266# else 308# else
267# define EV_USE_EVENTFD 0 309# define EV_USE_EVENTFD 0
310# endif
311#endif
312
313#ifndef EV_USE_SIGNALFD
314# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9))
315# define EV_USE_SIGNALFD 1
316# else
317# define EV_USE_SIGNALFD 0
268# endif 318# endif
269#endif 319#endif
270 320
271#if 0 /* debugging */ 321#if 0 /* debugging */
272# define EV_VERIFY 3 322# define EV_VERIFY 3
339#endif 389#endif
340 390
341#if EV_USE_EVENTFD 391#if EV_USE_EVENTFD
342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 392/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
343# include <stdint.h> 393# include <stdint.h>
394# ifndef EFD_NONBLOCK
395# define EFD_NONBLOCK O_NONBLOCK
396# endif
397# ifndef EFD_CLOEXEC
398# define EFD_CLOEXEC O_CLOEXEC
399# endif
344# ifdef __cplusplus 400# ifdef __cplusplus
345extern "C" { 401extern "C" {
346# endif 402# endif
347int eventfd (unsigned int initval, int flags); 403int eventfd (unsigned int initval, int flags);
348# ifdef __cplusplus 404# ifdef __cplusplus
349} 405}
350# endif 406# endif
407#endif
408
409#if EV_USE_SIGNALFD
410# include <sys/signalfd.h>
351#endif 411#endif
352 412
353/**/ 413/**/
354 414
355#if EV_VERIFY >= 3 415#if EV_VERIFY >= 3
647 707
648 tv.tv_sec = (time_t)delay; 708 tv.tv_sec = (time_t)delay;
649 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 709 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
650 710
651 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 711 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
652 /* somehting not guaranteed by newer posix versions, but guaranteed */ 712 /* something not guaranteed by newer posix versions, but guaranteed */
653 /* by older ones */ 713 /* by older ones */
654 select (0, 0, 0, 0, &tv); 714 select (0, 0, 0, 0, &tv);
655#endif 715#endif
656 } 716 }
657} 717}
1104evpipe_init (EV_P) 1164evpipe_init (EV_P)
1105{ 1165{
1106 if (!ev_is_active (&pipe_w)) 1166 if (!ev_is_active (&pipe_w))
1107 { 1167 {
1108#if EV_USE_EVENTFD 1168#if EV_USE_EVENTFD
1169 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1170 if (evfd < 0 && errno == EINVAL)
1109 if ((evfd = eventfd (0, 0)) >= 0) 1171 evfd = eventfd (0, 0);
1172
1173 if (evfd >= 0)
1110 { 1174 {
1111 evpipe [0] = -1; 1175 evpipe [0] = -1;
1112 fd_intern (evfd); 1176 fd_intern (evfd); /* doing it twice doesn't hurt */
1113 ev_io_set (&pipe_w, evfd, EV_READ); 1177 ev_io_set (&pipe_w, evfd, EV_READ);
1114 } 1178 }
1115 else 1179 else
1116#endif 1180#endif
1117 { 1181 {
1229 signals [signum].gotsig = 0; 1293 signals [signum].gotsig = 0;
1230 1294
1231 for (w = signals [signum].head; w; w = w->next) 1295 for (w = signals [signum].head; w; w = w->next)
1232 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1296 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1233} 1297}
1298
1299#if EV_USE_SIGNALFD
1300static void
1301sigfdcb (EV_P_ ev_io *iow, int revents)
1302{
1303 struct signalfd_siginfo si[4], *sip;
1304
1305 for (;;)
1306 {
1307 ssize_t res = read (sigfd, si, sizeof (si));
1308
1309 /* not ISO-C, as res might be -1, but works with SuS */
1310 for (sip = si; (char *)sip < (char *)si + res; ++sip)
1311 ev_feed_signal_event (EV_A_ sip->ssi_signo);
1312
1313 if (res < (ssize_t)sizeof (si))
1314 break;
1315 }
1316}
1317#endif
1234 1318
1235/*****************************************************************************/ 1319/*****************************************************************************/
1236 1320
1237static WL childs [EV_PID_HASHSIZE]; 1321static WL childs [EV_PID_HASHSIZE];
1238 1322
1474 backend_fd = -1; 1558 backend_fd = -1;
1475 gotasync = 0; 1559 gotasync = 0;
1476#if EV_USE_INOTIFY 1560#if EV_USE_INOTIFY
1477 fs_fd = -2; 1561 fs_fd = -2;
1478#endif 1562#endif
1563#if EV_USE_SIGNALFD
1564 sigfd = -2;
1565#endif
1479 1566
1480 /* pid check not overridable via env */ 1567 /* pid check not overridable via env */
1481#ifndef _WIN32 1568#ifndef _WIN32
1482 if (flags & EVFLAG_FORKCHECK) 1569 if (flags & EVFLAG_FORKCHECK)
1483 curpid = getpid (); 1570 curpid = getpid ();
1520{ 1607{
1521 int i; 1608 int i;
1522 1609
1523 if (ev_is_active (&pipe_w)) 1610 if (ev_is_active (&pipe_w))
1524 { 1611 {
1525 ev_ref (EV_A); /* signal watcher */ 1612 /*ev_ref (EV_A);*/
1526 ev_io_stop (EV_A_ &pipe_w); 1613 /*ev_io_stop (EV_A_ &pipe_w);*/
1527 1614
1528#if EV_USE_EVENTFD 1615#if EV_USE_EVENTFD
1529 if (evfd >= 0) 1616 if (evfd >= 0)
1530 close (evfd); 1617 close (evfd);
1531#endif 1618#endif
1535 close (evpipe [0]); 1622 close (evpipe [0]);
1536 close (evpipe [1]); 1623 close (evpipe [1]);
1537 } 1624 }
1538 } 1625 }
1539 1626
1627#if EV_USE_SIGNALFD
1628 if (ev_is_active (&sigfd_w))
1629 {
1630 /*ev_ref (EV_A);*/
1631 /*ev_io_stop (EV_A_ &sigfd_w);*/
1632
1633 close (sigfd);
1634 }
1635#endif
1636
1540#if EV_USE_INOTIFY 1637#if EV_USE_INOTIFY
1541 if (fs_fd >= 0) 1638 if (fs_fd >= 0)
1542 close (fs_fd); 1639 close (fs_fd);
1543#endif 1640#endif
1544 1641
1567#if EV_IDLE_ENABLE 1664#if EV_IDLE_ENABLE
1568 array_free (idle, [i]); 1665 array_free (idle, [i]);
1569#endif 1666#endif
1570 } 1667 }
1571 1668
1572 ev_free (anfds); anfdmax = 0; 1669 ev_free (anfds); anfds = 0; anfdmax = 0;
1573 1670
1574 /* have to use the microsoft-never-gets-it-right macro */ 1671 /* have to use the microsoft-never-gets-it-right macro */
1575 array_free (rfeed, EMPTY); 1672 array_free (rfeed, EMPTY);
1576 array_free (fdchange, EMPTY); 1673 array_free (fdchange, EMPTY);
1577 array_free (timer, EMPTY); 1674 array_free (timer, EMPTY);
1647ev_loop_new (unsigned int flags) 1744ev_loop_new (unsigned int flags)
1648{ 1745{
1649 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1746 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1650 1747
1651 memset (loop, 0, sizeof (struct ev_loop)); 1748 memset (loop, 0, sizeof (struct ev_loop));
1652
1653 loop_init (EV_A_ flags); 1749 loop_init (EV_A_ flags);
1654 1750
1655 if (ev_backend (EV_A)) 1751 if (ev_backend (EV_A))
1656 return loop; 1752 return loop;
1657 1753
1838 1934
1839void 1935void
1840ev_invoke (EV_P_ void *w, int revents) 1936ev_invoke (EV_P_ void *w, int revents)
1841{ 1937{
1842 EV_CB_INVOKE ((W)w, revents); 1938 EV_CB_INVOKE ((W)w, revents);
1939}
1940
1941unsigned int
1942ev_pending_count (EV_P)
1943{
1944 int pri;
1945 unsigned int count = 0;
1946
1947 for (pri = NUMPRI; pri--; )
1948 count += pendingcnt [pri];
1949
1950 return count;
1843} 1951}
1844 1952
1845void noinline 1953void noinline
1846ev_invoke_pending (EV_P) 1954ev_invoke_pending (EV_P)
1847{ 1955{
2475 } 2583 }
2476 2584
2477 EV_FREQUENT_CHECK; 2585 EV_FREQUENT_CHECK;
2478} 2586}
2479 2587
2588ev_tstamp
2589ev_timer_remaining (EV_P_ ev_timer *w)
2590{
2591 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2592}
2593
2480#if EV_PERIODIC_ENABLE 2594#if EV_PERIODIC_ENABLE
2481void noinline 2595void noinline
2482ev_periodic_start (EV_P_ ev_periodic *w) 2596ev_periodic_start (EV_P_ ev_periodic *w)
2483{ 2597{
2484 if (expect_false (ev_is_active (w))) 2598 if (expect_false (ev_is_active (w)))
2559 if (expect_false (ev_is_active (w))) 2673 if (expect_false (ev_is_active (w)))
2560 return; 2674 return;
2561 2675
2562 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); 2676 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0));
2563 2677
2678 EV_FREQUENT_CHECK;
2679
2680#if EV_USE_SIGNALFD
2681 if (sigfd == -2)
2682 {
2683 sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
2684 if (sigfd < 0 && errno == EINVAL)
2685 sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
2686
2687 if (sigfd >= 0)
2688 {
2689 fd_intern (sigfd); /* doing it twice will not hurt */
2690
2691 sigemptyset (&sigfd_set);
2692
2693 ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
2694 ev_set_priority (&sigfd_w, EV_MAXPRI);
2695 ev_io_start (EV_A_ &sigfd_w);
2696 ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
2697 }
2698 }
2699
2700 if (sigfd >= 0)
2701 {
2702 /* TODO: check .head */
2703 sigaddset (&sigfd_set, w->signum);
2704 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2705
2706 signalfd (sigfd, &sigfd_set, 0);
2707 }
2708 else
2709#endif
2564 evpipe_init (EV_A); 2710 evpipe_init (EV_A);
2565
2566 EV_FREQUENT_CHECK;
2567 2711
2568 { 2712 {
2569#ifndef _WIN32 2713#ifndef _WIN32
2570 sigset_t full, prev; 2714 sigset_t full, prev;
2571 sigfillset (&full); 2715 sigfillset (&full);
2573#endif 2717#endif
2574 2718
2575 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2719 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2576 2720
2577#ifndef _WIN32 2721#ifndef _WIN32
2722# if EV_USE_SIGNALFD
2723 if (sigfd < 0)/*TODO*/
2724# endif
2725 sigdelset (&prev, w->signum);
2578 sigprocmask (SIG_SETMASK, &prev, 0); 2726 sigprocmask (SIG_SETMASK, &prev, 0);
2579#endif 2727#endif
2580 } 2728 }
2581 2729
2582 ev_start (EV_A_ (W)w, 1); 2730 ev_start (EV_A_ (W)w, 1);
2585 if (!((WL)w)->next) 2733 if (!((WL)w)->next)
2586 { 2734 {
2587#if _WIN32 2735#if _WIN32
2588 signal (w->signum, ev_sighandler); 2736 signal (w->signum, ev_sighandler);
2589#else 2737#else
2738# if EV_USE_SIGNALFD
2739 if (sigfd < 0) /*TODO*/
2740# endif
2741 {
2590 struct sigaction sa = { }; 2742 struct sigaction sa = { };
2591 sa.sa_handler = ev_sighandler; 2743 sa.sa_handler = ev_sighandler;
2592 sigfillset (&sa.sa_mask); 2744 sigfillset (&sa.sa_mask);
2593 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2745 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2594 sigaction (w->signum, &sa, 0); 2746 sigaction (w->signum, &sa, 0);
2747 }
2595#endif 2748#endif
2596 } 2749 }
2597 2750
2598 EV_FREQUENT_CHECK; 2751 EV_FREQUENT_CHECK;
2599} 2752}
2609 2762
2610 wlist_del (&signals [w->signum - 1].head, (WL)w); 2763 wlist_del (&signals [w->signum - 1].head, (WL)w);
2611 ev_stop (EV_A_ (W)w); 2764 ev_stop (EV_A_ (W)w);
2612 2765
2613 if (!signals [w->signum - 1].head) 2766 if (!signals [w->signum - 1].head)
2767#if EV_USE_SIGNALFD
2768 if (sigfd >= 0)
2769 {
2770 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2771 sigdelset (&sigfd_set, w->signum);
2772 signalfd (sigfd, &sigfd_set, 0);
2773 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2774 /*TODO: maybe unblock signal? */
2775 }
2776 else
2777#endif
2614 signal (w->signum, SIG_DFL); 2778 signal (w->signum, SIG_DFL);
2615 2779
2616 EV_FREQUENT_CHECK; 2780 EV_FREQUENT_CHECK;
2617} 2781}
2618 2782
2619void 2783void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines