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

Comparing libev/ev.c (file contents):
Revision 1.294 by root, Wed Jul 8 02:46:05 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
391# define inline_speed static noinline 417# define inline_speed static noinline
392#else 418#else
393# define inline_speed static inline 419# define inline_speed static inline
394#endif 420#endif
395 421
396#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 422#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
423
424#if EV_MINPRI == EV_MAXPRI
425# define ABSPRI(w) (((W)w), 0)
426#else
397#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 427# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
428#endif
398 429
399#define EMPTY /* required for microsofts broken pseudo-c compiler */ 430#define EMPTY /* required for microsofts broken pseudo-c compiler */
400#define EMPTY2(a,b) /* used to suppress some warnings */ 431#define EMPTY2(a,b) /* used to suppress some warnings */
401 432
402typedef ev_watcher *W; 433typedef ev_watcher *W;
485#define ev_malloc(size) ev_realloc (0, (size)) 516#define ev_malloc(size) ev_realloc (0, (size))
486#define ev_free(ptr) ev_realloc ((ptr), 0) 517#define ev_free(ptr) ev_realloc ((ptr), 0)
487 518
488/*****************************************************************************/ 519/*****************************************************************************/
489 520
521/* set in reify when reification needed */
522#define EV_ANFD_REIFY 1
523
490/* file descriptor info structure */ 524/* file descriptor info structure */
491typedef struct 525typedef struct
492{ 526{
493 WL head; 527 WL head;
494 unsigned char events; /* the events watched for */ 528 unsigned char events; /* the events watched for */
495 unsigned char reify; /* flag set when this ANFD needs reification */ 529 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
496 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 530 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
497 unsigned char unused; 531 unsigned char unused;
498#if EV_USE_EPOLL 532#if EV_USE_EPOLL
499 unsigned int egen; /* generation counter to counter epoll bugs */ 533 unsigned int egen; /* generation counter to counter epoll bugs */
500#endif 534#endif
562 596
563 static int ev_default_loop_ptr; 597 static int ev_default_loop_ptr;
564 598
565#endif 599#endif
566 600
601#if EV_MINIMAL < 2
602# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
603# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
604# define EV_INVOKE_PENDING invoke_cb (EV_A)
605#else
606# define EV_RELEASE_CB (void)0
607# define EV_ACQUIRE_CB (void)0
608# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
609#endif
610
611#define EVUNLOOP_RECURSE 0x80
612
567/*****************************************************************************/ 613/*****************************************************************************/
568 614
569#ifndef EV_HAVE_EV_TIME 615#ifndef EV_HAVE_EV_TIME
570ev_tstamp 616ev_tstamp
571ev_time (void) 617ev_time (void)
627 673
628 tv.tv_sec = (time_t)delay; 674 tv.tv_sec = (time_t)delay;
629 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 675 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
630 676
631 /* 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 */
632 /* somehting not guaranteed by newer posix versions, but guaranteed */ 678 /* something not guaranteed by newer posix versions, but guaranteed */
633 /* by older ones */ 679 /* by older ones */
634 select (0, 0, 0, 0, &tv); 680 select (0, 0, 0, 0, &tv);
635#endif 681#endif
636 } 682 }
637} 683}
745} 791}
746 792
747/*****************************************************************************/ 793/*****************************************************************************/
748 794
749inline_speed void 795inline_speed void
750fd_event (EV_P_ int fd, int revents) 796fd_event_nc (EV_P_ int fd, int revents)
751{ 797{
752 ANFD *anfd = anfds + fd; 798 ANFD *anfd = anfds + fd;
753 ev_io *w; 799 ev_io *w;
754 800
755 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 801 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
759 if (ev) 805 if (ev)
760 ev_feed_event (EV_A_ (W)w, ev); 806 ev_feed_event (EV_A_ (W)w, ev);
761 } 807 }
762} 808}
763 809
810/* do not submit kernel events for fds that have reify set */
811/* because that means they changed while we were polling for new events */
812inline_speed void
813fd_event (EV_P_ int fd, int revents)
814{
815 ANFD *anfd = anfds + fd;
816
817 if (expect_true (!anfd->reify))
818 fd_event_nc (EV_A_ fd, revents);
819}
820
764void 821void
765ev_feed_fd_event (EV_P_ int fd, int revents) 822ev_feed_fd_event (EV_P_ int fd, int revents)
766{ 823{
767 if (fd >= 0 && fd < anfdmax) 824 if (fd >= 0 && fd < anfdmax)
768 fd_event (EV_A_ fd, revents); 825 fd_event_nc (EV_A_ fd, revents);
769} 826}
770 827
771/* make sure the external fd watch events are in-sync */ 828/* make sure the external fd watch events are in-sync */
772/* with the kernel/libev internal state */ 829/* with the kernel/libev internal state */
773inline_size void 830inline_size void
888 for (fd = 0; fd < anfdmax; ++fd) 945 for (fd = 0; fd < anfdmax; ++fd)
889 if (anfds [fd].events) 946 if (anfds [fd].events)
890 { 947 {
891 anfds [fd].events = 0; 948 anfds [fd].events = 0;
892 anfds [fd].emask = 0; 949 anfds [fd].emask = 0;
893 fd_change (EV_A_ fd, EV__IOFDSET | 1); 950 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
894 } 951 }
895} 952}
896 953
897/*****************************************************************************/ 954/*****************************************************************************/
898 955
1073evpipe_init (EV_P) 1130evpipe_init (EV_P)
1074{ 1131{
1075 if (!ev_is_active (&pipe_w)) 1132 if (!ev_is_active (&pipe_w))
1076 { 1133 {
1077#if EV_USE_EVENTFD 1134#if EV_USE_EVENTFD
1135 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1136 if (evfd < 0 && errno == EINVAL)
1078 if ((evfd = eventfd (0, 0)) >= 0) 1137 evfd = eventfd (0, 0);
1138
1139 if (evfd >= 0)
1079 { 1140 {
1080 evpipe [0] = -1; 1141 evpipe [0] = -1;
1081 fd_intern (evfd); 1142 fd_intern (evfd); /* doing it twice doesn't hurt */
1082 ev_io_set (&pipe_w, evfd, EV_READ); 1143 ev_io_set (&pipe_w, evfd, EV_READ);
1083 } 1144 }
1084 else 1145 else
1085#endif 1146#endif
1086 { 1147 {
1198 signals [signum].gotsig = 0; 1259 signals [signum].gotsig = 0;
1199 1260
1200 for (w = signals [signum].head; w; w = w->next) 1261 for (w = signals [signum].head; w; w = w->next)
1201 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1262 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1202} 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
1203 1284
1204/*****************************************************************************/ 1285/*****************************************************************************/
1205 1286
1206static WL childs [EV_PID_HASHSIZE]; 1287static WL childs [EV_PID_HASHSIZE];
1207 1288
1352ev_backend (EV_P) 1433ev_backend (EV_P)
1353{ 1434{
1354 return backend; 1435 return backend;
1355} 1436}
1356 1437
1438#if EV_MINIMAL < 2
1357unsigned int 1439unsigned int
1358ev_loop_count (EV_P) 1440ev_loop_count (EV_P)
1359{ 1441{
1360 return loop_count; 1442 return loop_count;
1361} 1443}
1375void 1457void
1376ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1458ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1377{ 1459{
1378 timeout_blocktime = interval; 1460 timeout_blocktime = interval;
1379} 1461}
1462
1463void
1464ev_set_userdata (EV_P_ void *data)
1465{
1466 userdata = data;
1467}
1468
1469void *
1470ev_userdata (EV_P)
1471{
1472 return userdata;
1473}
1474
1475void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1476{
1477 invoke_cb = invoke_pending_cb;
1478}
1479
1480void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1481{
1482 release_cb = release;
1483 acquire_cb = acquire;
1484}
1485#endif
1380 1486
1381/* initialise a loop structure, must be zero-initialised */ 1487/* initialise a loop structure, must be zero-initialised */
1382static void noinline 1488static void noinline
1383loop_init (EV_P_ unsigned int flags) 1489loop_init (EV_P_ unsigned int flags)
1384{ 1490{
1406 1512
1407 ev_rt_now = ev_time (); 1513 ev_rt_now = ev_time ();
1408 mn_now = get_clock (); 1514 mn_now = get_clock ();
1409 now_floor = mn_now; 1515 now_floor = mn_now;
1410 rtmn_diff = ev_rt_now - mn_now; 1516 rtmn_diff = ev_rt_now - mn_now;
1517#if EV_MINIMAL < 2
1518 invoke_cb = ev_invoke_pending;
1519#endif
1411 1520
1412 io_blocktime = 0.; 1521 io_blocktime = 0.;
1413 timeout_blocktime = 0.; 1522 timeout_blocktime = 0.;
1414 backend = 0; 1523 backend = 0;
1415 backend_fd = -1; 1524 backend_fd = -1;
1416 gotasync = 0; 1525 gotasync = 0;
1417#if EV_USE_INOTIFY 1526#if EV_USE_INOTIFY
1418 fs_fd = -2; 1527 fs_fd = -2;
1419#endif 1528#endif
1529#if EV_USE_SIGNALFD
1530 sigfd = -2;
1531#endif
1420 1532
1421 /* pid check not overridable via env */ 1533 /* pid check not overridable via env */
1422#ifndef _WIN32 1534#ifndef _WIN32
1423 if (flags & EVFLAG_FORKCHECK) 1535 if (flags & EVFLAG_FORKCHECK)
1424 curpid = getpid (); 1536 curpid = getpid ();
1461{ 1573{
1462 int i; 1574 int i;
1463 1575
1464 if (ev_is_active (&pipe_w)) 1576 if (ev_is_active (&pipe_w))
1465 { 1577 {
1466 ev_ref (EV_A); /* signal watcher */ 1578 /*ev_ref (EV_A);*/
1467 ev_io_stop (EV_A_ &pipe_w); 1579 /*ev_io_stop (EV_A_ &pipe_w);*/
1468 1580
1469#if EV_USE_EVENTFD 1581#if EV_USE_EVENTFD
1470 if (evfd >= 0) 1582 if (evfd >= 0)
1471 close (evfd); 1583 close (evfd);
1472#endif 1584#endif
1475 { 1587 {
1476 close (evpipe [0]); 1588 close (evpipe [0]);
1477 close (evpipe [1]); 1589 close (evpipe [1]);
1478 } 1590 }
1479 } 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
1480 1602
1481#if EV_USE_INOTIFY 1603#if EV_USE_INOTIFY
1482 if (fs_fd >= 0) 1604 if (fs_fd >= 0)
1483 close (fs_fd); 1605 close (fs_fd);
1484#endif 1606#endif
1588ev_loop_new (unsigned int flags) 1710ev_loop_new (unsigned int flags)
1589{ 1711{
1590 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));
1591 1713
1592 memset (loop, 0, sizeof (struct ev_loop)); 1714 memset (loop, 0, sizeof (struct ev_loop));
1593
1594 loop_init (EV_A_ flags); 1715 loop_init (EV_A_ flags);
1595 1716
1596 if (ev_backend (EV_A)) 1717 if (ev_backend (EV_A))
1597 return loop; 1718 return loop;
1598 1719
1609void 1730void
1610ev_loop_fork (EV_P) 1731ev_loop_fork (EV_P)
1611{ 1732{
1612 postfork = 1; /* must be in line with ev_default_fork */ 1733 postfork = 1; /* must be in line with ev_default_fork */
1613} 1734}
1735#endif /* multiplicity */
1614 1736
1615#if EV_VERIFY 1737#if EV_VERIFY
1616static void noinline 1738static void noinline
1617verify_watcher (EV_P_ W w) 1739verify_watcher (EV_P_ W w)
1618{ 1740{
1646 verify_watcher (EV_A_ ws [cnt]); 1768 verify_watcher (EV_A_ ws [cnt]);
1647 } 1769 }
1648} 1770}
1649#endif 1771#endif
1650 1772
1773#if EV_MINIMAL < 2
1651void 1774void
1652ev_loop_verify (EV_P) 1775ev_loop_verify (EV_P)
1653{ 1776{
1654#if EV_VERIFY 1777#if EV_VERIFY
1655 int i; 1778 int i;
1708 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1831 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1709 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1832 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1710# endif 1833# endif
1711#endif 1834#endif
1712} 1835}
1713 1836#endif
1714#endif /* multiplicity */
1715 1837
1716#if EV_MULTIPLICITY 1838#if EV_MULTIPLICITY
1717struct ev_loop * 1839struct ev_loop *
1718ev_default_loop_init (unsigned int flags) 1840ev_default_loop_init (unsigned int flags)
1719#else 1841#else
1780ev_invoke (EV_P_ void *w, int revents) 1902ev_invoke (EV_P_ void *w, int revents)
1781{ 1903{
1782 EV_CB_INVOKE ((W)w, revents); 1904 EV_CB_INVOKE ((W)w, revents);
1783} 1905}
1784 1906
1785inline_speed void 1907unsigned int
1786call_pending (EV_P) 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;
1917}
1918
1919void noinline
1920ev_invoke_pending (EV_P)
1787{ 1921{
1788 int pri; 1922 int pri;
1789 1923
1790 for (pri = NUMPRI; pri--; ) 1924 for (pri = NUMPRI; pri--; )
1791 while (pendingcnt [pri]) 1925 while (pendingcnt [pri])
2029} 2163}
2030 2164
2031void 2165void
2032ev_loop (EV_P_ int flags) 2166ev_loop (EV_P_ int flags)
2033{ 2167{
2168#if EV_MINIMAL < 2
2034 ++loop_depth; 2169 ++loop_depth;
2170#endif
2171
2172 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2035 2173
2036 loop_done = EVUNLOOP_CANCEL; 2174 loop_done = EVUNLOOP_CANCEL;
2037 2175
2038 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2176 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2039 2177
2040 do 2178 do
2041 { 2179 {
2042#if EV_VERIFY >= 2 2180#if EV_VERIFY >= 2
2043 ev_loop_verify (EV_A); 2181 ev_loop_verify (EV_A);
2056 /* we might have forked, so queue fork handlers */ 2194 /* we might have forked, so queue fork handlers */
2057 if (expect_false (postfork)) 2195 if (expect_false (postfork))
2058 if (forkcnt) 2196 if (forkcnt)
2059 { 2197 {
2060 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2198 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2061 call_pending (EV_A); 2199 EV_INVOKE_PENDING;
2062 } 2200 }
2063#endif 2201#endif
2064 2202
2065 /* queue prepare watchers (and execute them) */ 2203 /* queue prepare watchers (and execute them) */
2066 if (expect_false (preparecnt)) 2204 if (expect_false (preparecnt))
2067 { 2205 {
2068 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2206 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2069 call_pending (EV_A); 2207 EV_INVOKE_PENDING;
2070 } 2208 }
2209
2210 if (expect_false (loop_done))
2211 break;
2071 2212
2072 /* we might have forked, so reify kernel state if necessary */ 2213 /* we might have forked, so reify kernel state if necessary */
2073 if (expect_false (postfork)) 2214 if (expect_false (postfork))
2074 loop_fork (EV_A); 2215 loop_fork (EV_A);
2075 2216
2123 waittime -= sleeptime; 2264 waittime -= sleeptime;
2124 } 2265 }
2125 } 2266 }
2126 } 2267 }
2127 2268
2269#if EV_MINIMAL < 2
2128 ++loop_count; 2270 ++loop_count;
2271#endif
2272 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2129 backend_poll (EV_A_ waittime); 2273 backend_poll (EV_A_ waittime);
2274 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2130 2275
2131 /* update ev_rt_now, do magic */ 2276 /* update ev_rt_now, do magic */
2132 time_update (EV_A_ waittime + sleeptime); 2277 time_update (EV_A_ waittime + sleeptime);
2133 } 2278 }
2134 2279
2145 2290
2146 /* queue check watchers, to be executed first */ 2291 /* queue check watchers, to be executed first */
2147 if (expect_false (checkcnt)) 2292 if (expect_false (checkcnt))
2148 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2293 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2149 2294
2150 call_pending (EV_A); 2295 EV_INVOKE_PENDING;
2151 } 2296 }
2152 while (expect_true ( 2297 while (expect_true (
2153 activecnt 2298 activecnt
2154 && !loop_done 2299 && !loop_done
2155 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2300 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2156 )); 2301 ));
2157 2302
2158 if (loop_done == EVUNLOOP_ONE) 2303 if (loop_done == EVUNLOOP_ONE)
2159 loop_done = EVUNLOOP_CANCEL; 2304 loop_done = EVUNLOOP_CANCEL;
2160 2305
2306#if EV_MINIMAL < 2
2161 --loop_depth; 2307 --loop_depth;
2308#endif
2162} 2309}
2163 2310
2164void 2311void
2165ev_unloop (EV_P_ int how) 2312ev_unloop (EV_P_ int how)
2166{ 2313{
2258} 2405}
2259 2406
2260inline_size void 2407inline_size void
2261pri_adjust (EV_P_ W w) 2408pri_adjust (EV_P_ W w)
2262{ 2409{
2263 int pri = w->priority; 2410 int pri = ev_priority (w);
2264 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2411 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2265 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2412 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2266 w->priority = pri; 2413 ev_set_priority (w, pri);
2267} 2414}
2268 2415
2269inline_speed void 2416inline_speed void
2270ev_start (EV_P_ W w, int active) 2417ev_start (EV_P_ W w, int active)
2271{ 2418{
2298 2445
2299 ev_start (EV_A_ (W)w, 1); 2446 ev_start (EV_A_ (W)w, 1);
2300 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2447 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2301 wlist_add (&anfds[fd].head, (WL)w); 2448 wlist_add (&anfds[fd].head, (WL)w);
2302 2449
2303 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2450 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2304 w->events &= ~EV__IOFDSET; 2451 w->events &= ~EV__IOFDSET;
2305 2452
2306 EV_FREQUENT_CHECK; 2453 EV_FREQUENT_CHECK;
2307} 2454}
2308 2455
2402 } 2549 }
2403 2550
2404 EV_FREQUENT_CHECK; 2551 EV_FREQUENT_CHECK;
2405} 2552}
2406 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
2407#if EV_PERIODIC_ENABLE 2560#if EV_PERIODIC_ENABLE
2408void noinline 2561void noinline
2409ev_periodic_start (EV_P_ ev_periodic *w) 2562ev_periodic_start (EV_P_ ev_periodic *w)
2410{ 2563{
2411 if (expect_false (ev_is_active (w))) 2564 if (expect_false (ev_is_active (w)))
2486 if (expect_false (ev_is_active (w))) 2639 if (expect_false (ev_is_active (w)))
2487 return; 2640 return;
2488 2641
2489 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));
2490 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
2491 evpipe_init (EV_A); 2676 evpipe_init (EV_A);
2492
2493 EV_FREQUENT_CHECK;
2494 2677
2495 { 2678 {
2496#ifndef _WIN32 2679#ifndef _WIN32
2497 sigset_t full, prev; 2680 sigset_t full, prev;
2498 sigfillset (&full); 2681 sigfillset (&full);
2500#endif 2683#endif
2501 2684
2502 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2685 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2503 2686
2504#ifndef _WIN32 2687#ifndef _WIN32
2688# if EV_USE_SIGNALFD
2689 if (sigfd < 0)/*TODO*/
2690# endif
2691 sigdelset (&prev, w->signum);
2505 sigprocmask (SIG_SETMASK, &prev, 0); 2692 sigprocmask (SIG_SETMASK, &prev, 0);
2506#endif 2693#endif
2507 } 2694 }
2508 2695
2509 ev_start (EV_A_ (W)w, 1); 2696 ev_start (EV_A_ (W)w, 1);
2512 if (!((WL)w)->next) 2699 if (!((WL)w)->next)
2513 { 2700 {
2514#if _WIN32 2701#if _WIN32
2515 signal (w->signum, ev_sighandler); 2702 signal (w->signum, ev_sighandler);
2516#else 2703#else
2704# if EV_USE_SIGNALFD
2705 if (sigfd < 0) /*TODO*/
2706# endif
2707 {
2517 struct sigaction sa; 2708 struct sigaction sa = { };
2518 sa.sa_handler = ev_sighandler; 2709 sa.sa_handler = ev_sighandler;
2519 sigfillset (&sa.sa_mask); 2710 sigfillset (&sa.sa_mask);
2520 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 */
2521 sigaction (w->signum, &sa, 0); 2712 sigaction (w->signum, &sa, 0);
2713 }
2522#endif 2714#endif
2523 } 2715 }
2524 2716
2525 EV_FREQUENT_CHECK; 2717 EV_FREQUENT_CHECK;
2526} 2718}
2536 2728
2537 wlist_del (&signals [w->signum - 1].head, (WL)w); 2729 wlist_del (&signals [w->signum - 1].head, (WL)w);
2538 ev_stop (EV_A_ (W)w); 2730 ev_stop (EV_A_ (W)w);
2539 2731
2540 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
2541 signal (w->signum, SIG_DFL); 2744 signal (w->signum, SIG_DFL);
2542 2745
2543 EV_FREQUENT_CHECK; 2746 EV_FREQUENT_CHECK;
2544} 2747}
2545 2748
2546void 2749void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines