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

Comparing libev/ev.c (file contents):
Revision 1.291 by root, Mon Jun 29 04:44:18 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
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
615#ifndef EV_HAVE_EV_TIME
569ev_tstamp 616ev_tstamp
570ev_time (void) 617ev_time (void)
571{ 618{
572#if EV_USE_REALTIME 619#if EV_USE_REALTIME
573 if (expect_true (have_realtime)) 620 if (expect_true (have_realtime))
580 627
581 struct timeval tv; 628 struct timeval tv;
582 gettimeofday (&tv, 0); 629 gettimeofday (&tv, 0);
583 return tv.tv_sec + tv.tv_usec * 1e-6; 630 return tv.tv_sec + tv.tv_usec * 1e-6;
584} 631}
632#endif
585 633
586inline_size ev_tstamp 634inline_size ev_tstamp
587get_clock (void) 635get_clock (void)
588{ 636{
589#if EV_USE_MONOTONIC 637#if EV_USE_MONOTONIC
625 673
626 tv.tv_sec = (time_t)delay; 674 tv.tv_sec = (time_t)delay;
627 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 675 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
628 676
629 /* 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 */
630 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 678 /* something not guaranteed by newer posix versions, but guaranteed */
631 /* by older ones */ 679 /* by older ones */
632 select (0, 0, 0, 0, &tv); 680 select (0, 0, 0, 0, &tv);
633#endif 681#endif
634 } 682 }
635} 683}
743} 791}
744 792
745/*****************************************************************************/ 793/*****************************************************************************/
746 794
747inline_speed void 795inline_speed void
748fd_event (EV_P_ int fd, int revents) 796fd_event_nc (EV_P_ int fd, int revents)
749{ 797{
750 ANFD *anfd = anfds + fd; 798 ANFD *anfd = anfds + fd;
751 ev_io *w; 799 ev_io *w;
752 800
753 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)
757 if (ev) 805 if (ev)
758 ev_feed_event (EV_A_ (W)w, ev); 806 ev_feed_event (EV_A_ (W)w, ev);
759 } 807 }
760} 808}
761 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
762void 821void
763ev_feed_fd_event (EV_P_ int fd, int revents) 822ev_feed_fd_event (EV_P_ int fd, int revents)
764{ 823{
765 if (fd >= 0 && fd < anfdmax) 824 if (fd >= 0 && fd < anfdmax)
766 fd_event (EV_A_ fd, revents); 825 fd_event_nc (EV_A_ fd, revents);
767} 826}
768 827
769/* make sure the external fd watch events are in-sync */ 828/* make sure the external fd watch events are in-sync */
770/* with the kernel/libev internal state */ 829/* with the kernel/libev internal state */
771inline_size void 830inline_size void
886 for (fd = 0; fd < anfdmax; ++fd) 945 for (fd = 0; fd < anfdmax; ++fd)
887 if (anfds [fd].events) 946 if (anfds [fd].events)
888 { 947 {
889 anfds [fd].events = 0; 948 anfds [fd].events = 0;
890 anfds [fd].emask = 0; 949 anfds [fd].emask = 0;
891 fd_change (EV_A_ fd, EV__IOFDSET | 1); 950 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
892 } 951 }
893} 952}
894 953
895/*****************************************************************************/ 954/*****************************************************************************/
896 955
1071evpipe_init (EV_P) 1130evpipe_init (EV_P)
1072{ 1131{
1073 if (!ev_is_active (&pipe_w)) 1132 if (!ev_is_active (&pipe_w))
1074 { 1133 {
1075#if EV_USE_EVENTFD 1134#if EV_USE_EVENTFD
1135 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1136 if (evfd < 0 && errno == EINVAL)
1076 if ((evfd = eventfd (0, 0)) >= 0) 1137 evfd = eventfd (0, 0);
1138
1139 if (evfd >= 0)
1077 { 1140 {
1078 evpipe [0] = -1; 1141 evpipe [0] = -1;
1079 fd_intern (evfd); 1142 fd_intern (evfd); /* doing it twice doesn't hurt */
1080 ev_io_set (&pipe_w, evfd, EV_READ); 1143 ev_io_set (&pipe_w, evfd, EV_READ);
1081 } 1144 }
1082 else 1145 else
1083#endif 1146#endif
1084 { 1147 {
1196 signals [signum].gotsig = 0; 1259 signals [signum].gotsig = 0;
1197 1260
1198 for (w = signals [signum].head; w; w = w->next) 1261 for (w = signals [signum].head; w; w = w->next)
1199 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1262 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1200} 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
1201 1284
1202/*****************************************************************************/ 1285/*****************************************************************************/
1203 1286
1204static WL childs [EV_PID_HASHSIZE]; 1287static WL childs [EV_PID_HASHSIZE];
1205 1288
1350ev_backend (EV_P) 1433ev_backend (EV_P)
1351{ 1434{
1352 return backend; 1435 return backend;
1353} 1436}
1354 1437
1438#if EV_MINIMAL < 2
1355unsigned int 1439unsigned int
1356ev_loop_count (EV_P) 1440ev_loop_count (EV_P)
1357{ 1441{
1358 return loop_count; 1442 return loop_count;
1359} 1443}
1360 1444
1445unsigned int
1446ev_loop_depth (EV_P)
1447{
1448 return loop_depth;
1449}
1450
1361void 1451void
1362ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1452ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1363{ 1453{
1364 io_blocktime = interval; 1454 io_blocktime = interval;
1365} 1455}
1367void 1457void
1368ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1458ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1369{ 1459{
1370 timeout_blocktime = interval; 1460 timeout_blocktime = interval;
1371} 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
1372 1486
1373/* initialise a loop structure, must be zero-initialised */ 1487/* initialise a loop structure, must be zero-initialised */
1374static void noinline 1488static void noinline
1375loop_init (EV_P_ unsigned int flags) 1489loop_init (EV_P_ unsigned int flags)
1376{ 1490{
1398 1512
1399 ev_rt_now = ev_time (); 1513 ev_rt_now = ev_time ();
1400 mn_now = get_clock (); 1514 mn_now = get_clock ();
1401 now_floor = mn_now; 1515 now_floor = mn_now;
1402 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
1403 1520
1404 io_blocktime = 0.; 1521 io_blocktime = 0.;
1405 timeout_blocktime = 0.; 1522 timeout_blocktime = 0.;
1406 backend = 0; 1523 backend = 0;
1407 backend_fd = -1; 1524 backend_fd = -1;
1408 gotasync = 0; 1525 gotasync = 0;
1409#if EV_USE_INOTIFY 1526#if EV_USE_INOTIFY
1410 fs_fd = -2; 1527 fs_fd = -2;
1411#endif 1528#endif
1529#if EV_USE_SIGNALFD
1530 sigfd = -2;
1531#endif
1412 1532
1413 /* pid check not overridable via env */ 1533 /* pid check not overridable via env */
1414#ifndef _WIN32 1534#ifndef _WIN32
1415 if (flags & EVFLAG_FORKCHECK) 1535 if (flags & EVFLAG_FORKCHECK)
1416 curpid = getpid (); 1536 curpid = getpid ();
1453{ 1573{
1454 int i; 1574 int i;
1455 1575
1456 if (ev_is_active (&pipe_w)) 1576 if (ev_is_active (&pipe_w))
1457 { 1577 {
1458 ev_ref (EV_A); /* signal watcher */ 1578 /*ev_ref (EV_A);*/
1459 ev_io_stop (EV_A_ &pipe_w); 1579 /*ev_io_stop (EV_A_ &pipe_w);*/
1460 1580
1461#if EV_USE_EVENTFD 1581#if EV_USE_EVENTFD
1462 if (evfd >= 0) 1582 if (evfd >= 0)
1463 close (evfd); 1583 close (evfd);
1464#endif 1584#endif
1467 { 1587 {
1468 close (evpipe [0]); 1588 close (evpipe [0]);
1469 close (evpipe [1]); 1589 close (evpipe [1]);
1470 } 1590 }
1471 } 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
1472 1602
1473#if EV_USE_INOTIFY 1603#if EV_USE_INOTIFY
1474 if (fs_fd >= 0) 1604 if (fs_fd >= 0)
1475 close (fs_fd); 1605 close (fs_fd);
1476#endif 1606#endif
1580ev_loop_new (unsigned int flags) 1710ev_loop_new (unsigned int flags)
1581{ 1711{
1582 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));
1583 1713
1584 memset (loop, 0, sizeof (struct ev_loop)); 1714 memset (loop, 0, sizeof (struct ev_loop));
1585
1586 loop_init (EV_A_ flags); 1715 loop_init (EV_A_ flags);
1587 1716
1588 if (ev_backend (EV_A)) 1717 if (ev_backend (EV_A))
1589 return loop; 1718 return loop;
1590 1719
1601void 1730void
1602ev_loop_fork (EV_P) 1731ev_loop_fork (EV_P)
1603{ 1732{
1604 postfork = 1; /* must be in line with ev_default_fork */ 1733 postfork = 1; /* must be in line with ev_default_fork */
1605} 1734}
1735#endif /* multiplicity */
1606 1736
1607#if EV_VERIFY 1737#if EV_VERIFY
1608static void noinline 1738static void noinline
1609verify_watcher (EV_P_ W w) 1739verify_watcher (EV_P_ W w)
1610{ 1740{
1638 verify_watcher (EV_A_ ws [cnt]); 1768 verify_watcher (EV_A_ ws [cnt]);
1639 } 1769 }
1640} 1770}
1641#endif 1771#endif
1642 1772
1773#if EV_MINIMAL < 2
1643void 1774void
1644ev_loop_verify (EV_P) 1775ev_loop_verify (EV_P)
1645{ 1776{
1646#if EV_VERIFY 1777#if EV_VERIFY
1647 int i; 1778 int i;
1700 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)
1701 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1832 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1702# endif 1833# endif
1703#endif 1834#endif
1704} 1835}
1705 1836#endif
1706#endif /* multiplicity */
1707 1837
1708#if EV_MULTIPLICITY 1838#if EV_MULTIPLICITY
1709struct ev_loop * 1839struct ev_loop *
1710ev_default_loop_init (unsigned int flags) 1840ev_default_loop_init (unsigned int flags)
1711#else 1841#else
1772ev_invoke (EV_P_ void *w, int revents) 1902ev_invoke (EV_P_ void *w, int revents)
1773{ 1903{
1774 EV_CB_INVOKE ((W)w, revents); 1904 EV_CB_INVOKE ((W)w, revents);
1775} 1905}
1776 1906
1777inline_speed void 1907unsigned int
1778call_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)
1779{ 1921{
1780 int pri; 1922 int pri;
1781 1923
1782 for (pri = NUMPRI; pri--; ) 1924 for (pri = NUMPRI; pri--; )
1783 while (pendingcnt [pri]) 1925 while (pendingcnt [pri])
2018 2160
2019 mn_now = ev_rt_now; 2161 mn_now = ev_rt_now;
2020 } 2162 }
2021} 2163}
2022 2164
2023static int loop_done;
2024
2025void 2165void
2026ev_loop (EV_P_ int flags) 2166ev_loop (EV_P_ int flags)
2027{ 2167{
2168#if EV_MINIMAL < 2
2169 ++loop_depth;
2170#endif
2171
2172 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2173
2028 loop_done = EVUNLOOP_CANCEL; 2174 loop_done = EVUNLOOP_CANCEL;
2029 2175
2030 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 */
2031 2177
2032 do 2178 do
2033 { 2179 {
2034#if EV_VERIFY >= 2 2180#if EV_VERIFY >= 2
2035 ev_loop_verify (EV_A); 2181 ev_loop_verify (EV_A);
2048 /* we might have forked, so queue fork handlers */ 2194 /* we might have forked, so queue fork handlers */
2049 if (expect_false (postfork)) 2195 if (expect_false (postfork))
2050 if (forkcnt) 2196 if (forkcnt)
2051 { 2197 {
2052 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2198 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2053 call_pending (EV_A); 2199 EV_INVOKE_PENDING;
2054 } 2200 }
2055#endif 2201#endif
2056 2202
2057 /* queue prepare watchers (and execute them) */ 2203 /* queue prepare watchers (and execute them) */
2058 if (expect_false (preparecnt)) 2204 if (expect_false (preparecnt))
2059 { 2205 {
2060 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2206 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2061 call_pending (EV_A); 2207 EV_INVOKE_PENDING;
2062 } 2208 }
2209
2210 if (expect_false (loop_done))
2211 break;
2063 2212
2064 /* we might have forked, so reify kernel state if necessary */ 2213 /* we might have forked, so reify kernel state if necessary */
2065 if (expect_false (postfork)) 2214 if (expect_false (postfork))
2066 loop_fork (EV_A); 2215 loop_fork (EV_A);
2067 2216
2073 ev_tstamp waittime = 0.; 2222 ev_tstamp waittime = 0.;
2074 ev_tstamp sleeptime = 0.; 2223 ev_tstamp sleeptime = 0.;
2075 2224
2076 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2225 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2077 { 2226 {
2227 /* remember old timestamp for io_blocktime calculation */
2228 ev_tstamp prev_mn_now = mn_now;
2229
2078 /* update time to cancel out callback processing overhead */ 2230 /* update time to cancel out callback processing overhead */
2079 time_update (EV_A_ 1e100); 2231 time_update (EV_A_ 1e100);
2080 2232
2081 waittime = MAX_BLOCKTIME; 2233 waittime = MAX_BLOCKTIME;
2082 2234
2092 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2244 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2093 if (waittime > to) waittime = to; 2245 if (waittime > to) waittime = to;
2094 } 2246 }
2095#endif 2247#endif
2096 2248
2249 /* don't let timeouts decrease the waittime below timeout_blocktime */
2097 if (expect_false (waittime < timeout_blocktime)) 2250 if (expect_false (waittime < timeout_blocktime))
2098 waittime = timeout_blocktime; 2251 waittime = timeout_blocktime;
2099 2252
2100 sleeptime = waittime - backend_fudge; 2253 /* extra check because io_blocktime is commonly 0 */
2101
2102 if (expect_true (sleeptime > io_blocktime)) 2254 if (expect_false (io_blocktime))
2103 sleeptime = io_blocktime;
2104
2105 if (sleeptime)
2106 { 2255 {
2256 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2257
2258 if (sleeptime > waittime - backend_fudge)
2259 sleeptime = waittime - backend_fudge;
2260
2261 if (expect_true (sleeptime > 0.))
2262 {
2107 ev_sleep (sleeptime); 2263 ev_sleep (sleeptime);
2108 waittime -= sleeptime; 2264 waittime -= sleeptime;
2265 }
2109 } 2266 }
2110 } 2267 }
2111 2268
2269#if EV_MINIMAL < 2
2112 ++loop_count; 2270 ++loop_count;
2271#endif
2272 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2113 backend_poll (EV_A_ waittime); 2273 backend_poll (EV_A_ waittime);
2274 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2114 2275
2115 /* update ev_rt_now, do magic */ 2276 /* update ev_rt_now, do magic */
2116 time_update (EV_A_ waittime + sleeptime); 2277 time_update (EV_A_ waittime + sleeptime);
2117 } 2278 }
2118 2279
2129 2290
2130 /* queue check watchers, to be executed first */ 2291 /* queue check watchers, to be executed first */
2131 if (expect_false (checkcnt)) 2292 if (expect_false (checkcnt))
2132 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2293 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2133 2294
2134 call_pending (EV_A); 2295 EV_INVOKE_PENDING;
2135 } 2296 }
2136 while (expect_true ( 2297 while (expect_true (
2137 activecnt 2298 activecnt
2138 && !loop_done 2299 && !loop_done
2139 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2300 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2140 )); 2301 ));
2141 2302
2142 if (loop_done == EVUNLOOP_ONE) 2303 if (loop_done == EVUNLOOP_ONE)
2143 loop_done = EVUNLOOP_CANCEL; 2304 loop_done = EVUNLOOP_CANCEL;
2305
2306#if EV_MINIMAL < 2
2307 --loop_depth;
2308#endif
2144} 2309}
2145 2310
2146void 2311void
2147ev_unloop (EV_P_ int how) 2312ev_unloop (EV_P_ int how)
2148{ 2313{
2240} 2405}
2241 2406
2242inline_size void 2407inline_size void
2243pri_adjust (EV_P_ W w) 2408pri_adjust (EV_P_ W w)
2244{ 2409{
2245 int pri = w->priority; 2410 int pri = ev_priority (w);
2246 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2411 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2247 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2412 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2248 w->priority = pri; 2413 ev_set_priority (w, pri);
2249} 2414}
2250 2415
2251inline_speed void 2416inline_speed void
2252ev_start (EV_P_ W w, int active) 2417ev_start (EV_P_ W w, int active)
2253{ 2418{
2280 2445
2281 ev_start (EV_A_ (W)w, 1); 2446 ev_start (EV_A_ (W)w, 1);
2282 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2447 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2283 wlist_add (&anfds[fd].head, (WL)w); 2448 wlist_add (&anfds[fd].head, (WL)w);
2284 2449
2285 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2450 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2286 w->events &= ~EV__IOFDSET; 2451 w->events &= ~EV__IOFDSET;
2287 2452
2288 EV_FREQUENT_CHECK; 2453 EV_FREQUENT_CHECK;
2289} 2454}
2290 2455
2384 } 2549 }
2385 2550
2386 EV_FREQUENT_CHECK; 2551 EV_FREQUENT_CHECK;
2387} 2552}
2388 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
2389#if EV_PERIODIC_ENABLE 2560#if EV_PERIODIC_ENABLE
2390void noinline 2561void noinline
2391ev_periodic_start (EV_P_ ev_periodic *w) 2562ev_periodic_start (EV_P_ ev_periodic *w)
2392{ 2563{
2393 if (expect_false (ev_is_active (w))) 2564 if (expect_false (ev_is_active (w)))
2468 if (expect_false (ev_is_active (w))) 2639 if (expect_false (ev_is_active (w)))
2469 return; 2640 return;
2470 2641
2471 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));
2472 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
2473 evpipe_init (EV_A); 2676 evpipe_init (EV_A);
2474
2475 EV_FREQUENT_CHECK;
2476 2677
2477 { 2678 {
2478#ifndef _WIN32 2679#ifndef _WIN32
2479 sigset_t full, prev; 2680 sigset_t full, prev;
2480 sigfillset (&full); 2681 sigfillset (&full);
2482#endif 2683#endif
2483 2684
2484 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2685 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2485 2686
2486#ifndef _WIN32 2687#ifndef _WIN32
2688 if (sigfd < 0)/*TODO*/
2689 sigdelset (&prev, w->signum);
2487 sigprocmask (SIG_SETMASK, &prev, 0); 2690 sigprocmask (SIG_SETMASK, &prev, 0);
2488#endif 2691#endif
2489 } 2692 }
2490 2693
2491 ev_start (EV_A_ (W)w, 1); 2694 ev_start (EV_A_ (W)w, 1);
2494 if (!((WL)w)->next) 2697 if (!((WL)w)->next)
2495 { 2698 {
2496#if _WIN32 2699#if _WIN32
2497 signal (w->signum, ev_sighandler); 2700 signal (w->signum, ev_sighandler);
2498#else 2701#else
2702 if (sigfd < 0) /*TODO*/
2703 {
2499 struct sigaction sa; 2704 struct sigaction sa = { };
2500 sa.sa_handler = ev_sighandler; 2705 sa.sa_handler = ev_sighandler;
2501 sigfillset (&sa.sa_mask); 2706 sigfillset (&sa.sa_mask);
2502 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 */
2503 sigaction (w->signum, &sa, 0); 2708 sigaction (w->signum, &sa, 0);
2709 }
2504#endif 2710#endif
2505 } 2711 }
2506 2712
2507 EV_FREQUENT_CHECK; 2713 EV_FREQUENT_CHECK;
2508} 2714}
2518 2724
2519 wlist_del (&signals [w->signum - 1].head, (WL)w); 2725 wlist_del (&signals [w->signum - 1].head, (WL)w);
2520 ev_stop (EV_A_ (W)w); 2726 ev_stop (EV_A_ (W)w);
2521 2727
2522 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
2523 signal (w->signum, SIG_DFL); 2740 signal (w->signum, SIG_DFL);
2524 2741
2525 EV_FREQUENT_CHECK; 2742 EV_FREQUENT_CHECK;
2526} 2743}
2527 2744
2528void 2745void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines