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

Comparing libev/ev.c (file contents):
Revision 1.288 by root, Sat Apr 25 14:12:48 2009 UTC vs.
Revision 1.304 by root, Sun Jul 19 03:12:28 2009 UTC

57# endif 57# endif
58# ifndef EV_USE_MONOTONIC 58# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 59# define EV_USE_MONOTONIC 1
60# endif 60# endif
61# endif 61# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL)
63# define EV_USE_CLOCK_SYSCALL 0
62# endif 64# endif
63 65
64# if HAVE_CLOCK_GETTIME 66# if HAVE_CLOCK_GETTIME
65# ifndef EV_USE_MONOTONIC 67# ifndef EV_USE_MONOTONIC
66# define EV_USE_MONOTONIC 1 68# define EV_USE_MONOTONIC 1
131# else 133# else
132# define EV_USE_INOTIFY 0 134# define EV_USE_INOTIFY 0
133# endif 135# endif
134# endif 136# endif
135 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
136# ifndef EV_USE_EVENTFD 146# ifndef EV_USE_EVENTFD
137# if HAVE_EVENTFD 147# if HAVE_EVENTFD
138# define EV_USE_EVENTFD 1 148# define EV_USE_EVENTFD 1
139# else 149# else
140# define EV_USE_EVENTFD 0 150# define EV_USE_EVENTFD 0
264# else 274# else
265# define EV_USE_EVENTFD 0 275# define EV_USE_EVENTFD 0
266# endif 276# endif
267#endif 277#endif
268 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
284# endif
285#endif
286
269#if 0 /* debugging */ 287#if 0 /* debugging */
270# define EV_VERIFY 3 288# define EV_VERIFY 3
271# define EV_USE_4HEAP 1 289# define EV_USE_4HEAP 1
272# define EV_HEAP_CACHE_AT 1 290# define EV_HEAP_CACHE_AT 1
273#endif 291#endif
280# define EV_USE_4HEAP !EV_MINIMAL 298# define EV_USE_4HEAP !EV_MINIMAL
281#endif 299#endif
282 300
283#ifndef EV_HEAP_CACHE_AT 301#ifndef EV_HEAP_CACHE_AT
284# define EV_HEAP_CACHE_AT !EV_MINIMAL 302# define EV_HEAP_CACHE_AT !EV_MINIMAL
303#endif
304
305/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
306/* which makes programs even slower. might work on other unices, too. */
307#if EV_USE_CLOCK_SYSCALL
308# include <syscall.h>
309# ifdef SYS_clock_gettime
310# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
311# undef EV_USE_MONOTONIC
312# define EV_USE_MONOTONIC 1
313# else
314# undef EV_USE_CLOCK_SYSCALL
315# define EV_USE_CLOCK_SYSCALL 0
316# endif
285#endif 317#endif
286 318
287/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 319/* this block fixes any misconfiguration where we know we run into trouble otherwise */
288 320
289#ifndef CLOCK_MONOTONIC 321#ifndef CLOCK_MONOTONIC
320 352
321#if EV_SELECT_IS_WINSOCKET 353#if EV_SELECT_IS_WINSOCKET
322# include <winsock.h> 354# include <winsock.h>
323#endif 355#endif
324 356
325/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
326/* which makes programs even slower. might work on other unices, too. */
327#if EV_USE_CLOCK_SYSCALL
328# include <syscall.h>
329# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
330# undef EV_USE_MONOTONIC
331# define EV_USE_MONOTONIC 1
332#endif
333
334#if EV_USE_EVENTFD 357#if EV_USE_EVENTFD
335/* 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 */
336# 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
337# ifdef __cplusplus 366# ifdef __cplusplus
338extern "C" { 367extern "C" {
339# endif 368# endif
340int eventfd (unsigned int initval, int flags); 369int eventfd (unsigned int initval, int flags);
341# ifdef __cplusplus 370# ifdef __cplusplus
342} 371}
343# endif 372# endif
373#endif
374
375#if EV_USE_SIGNALFD
376# include <sys/signalfd.h>
344#endif 377#endif
345 378
346/**/ 379/**/
347 380
348#if EV_VERIFY >= 3 381#if EV_VERIFY >= 3
384# define inline_speed static noinline 417# define inline_speed static noinline
385#else 418#else
386# define inline_speed static inline 419# define inline_speed static inline
387#endif 420#endif
388 421
389#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
390#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 427# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
428#endif
391 429
392#define EMPTY /* required for microsofts broken pseudo-c compiler */ 430#define EMPTY /* required for microsofts broken pseudo-c compiler */
393#define EMPTY2(a,b) /* used to suppress some warnings */ 431#define EMPTY2(a,b) /* used to suppress some warnings */
394 432
395typedef ev_watcher *W; 433typedef ev_watcher *W;
478#define ev_malloc(size) ev_realloc (0, (size)) 516#define ev_malloc(size) ev_realloc (0, (size))
479#define ev_free(ptr) ev_realloc ((ptr), 0) 517#define ev_free(ptr) ev_realloc ((ptr), 0)
480 518
481/*****************************************************************************/ 519/*****************************************************************************/
482 520
521/* set in reify when reification needed */
522#define EV_ANFD_REIFY 1
523
483/* file descriptor info structure */ 524/* file descriptor info structure */
484typedef struct 525typedef struct
485{ 526{
486 WL head; 527 WL head;
487 unsigned char events; /* the events watched for */ 528 unsigned char events; /* the events watched for */
488 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) */
489 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 */
490 unsigned char unused; 531 unsigned char unused;
491#if EV_USE_EPOLL 532#if EV_USE_EPOLL
492 unsigned int egen; /* generation counter to counter epoll bugs */ 533 unsigned int egen; /* generation counter to counter epoll bugs */
493#endif 534#endif
555 596
556 static int ev_default_loop_ptr; 597 static int ev_default_loop_ptr;
557 598
558#endif 599#endif
559 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
560/*****************************************************************************/ 613/*****************************************************************************/
561 614
615#ifndef EV_HAVE_EV_TIME
562ev_tstamp 616ev_tstamp
563ev_time (void) 617ev_time (void)
564{ 618{
565#if EV_USE_REALTIME 619#if EV_USE_REALTIME
566 if (expect_true (have_realtime)) 620 if (expect_true (have_realtime))
573 627
574 struct timeval tv; 628 struct timeval tv;
575 gettimeofday (&tv, 0); 629 gettimeofday (&tv, 0);
576 return tv.tv_sec + tv.tv_usec * 1e-6; 630 return tv.tv_sec + tv.tv_usec * 1e-6;
577} 631}
632#endif
578 633
579inline_size ev_tstamp 634inline_size ev_tstamp
580get_clock (void) 635get_clock (void)
581{ 636{
582#if EV_USE_MONOTONIC 637#if EV_USE_MONOTONIC
618 673
619 tv.tv_sec = (time_t)delay; 674 tv.tv_sec = (time_t)delay;
620 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 675 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
621 676
622 /* 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 */
623 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 678 /* something not guaranteed by newer posix versions, but guaranteed */
624 /* by older ones */ 679 /* by older ones */
625 select (0, 0, 0, 0, &tv); 680 select (0, 0, 0, 0, &tv);
626#endif 681#endif
627 } 682 }
628} 683}
736} 791}
737 792
738/*****************************************************************************/ 793/*****************************************************************************/
739 794
740inline_speed void 795inline_speed void
741fd_event (EV_P_ int fd, int revents) 796fd_event_nc (EV_P_ int fd, int revents)
742{ 797{
743 ANFD *anfd = anfds + fd; 798 ANFD *anfd = anfds + fd;
744 ev_io *w; 799 ev_io *w;
745 800
746 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)
750 if (ev) 805 if (ev)
751 ev_feed_event (EV_A_ (W)w, ev); 806 ev_feed_event (EV_A_ (W)w, ev);
752 } 807 }
753} 808}
754 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
755void 821void
756ev_feed_fd_event (EV_P_ int fd, int revents) 822ev_feed_fd_event (EV_P_ int fd, int revents)
757{ 823{
758 if (fd >= 0 && fd < anfdmax) 824 if (fd >= 0 && fd < anfdmax)
759 fd_event (EV_A_ fd, revents); 825 fd_event_nc (EV_A_ fd, revents);
760} 826}
761 827
762/* make sure the external fd watch events are in-sync */ 828/* make sure the external fd watch events are in-sync */
763/* with the kernel/libev internal state */ 829/* with the kernel/libev internal state */
764inline_size void 830inline_size void
879 for (fd = 0; fd < anfdmax; ++fd) 945 for (fd = 0; fd < anfdmax; ++fd)
880 if (anfds [fd].events) 946 if (anfds [fd].events)
881 { 947 {
882 anfds [fd].events = 0; 948 anfds [fd].events = 0;
883 anfds [fd].emask = 0; 949 anfds [fd].emask = 0;
884 fd_change (EV_A_ fd, EV__IOFDSET | 1); 950 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
885 } 951 }
886} 952}
887 953
888/*****************************************************************************/ 954/*****************************************************************************/
889 955
1064evpipe_init (EV_P) 1130evpipe_init (EV_P)
1065{ 1131{
1066 if (!ev_is_active (&pipe_w)) 1132 if (!ev_is_active (&pipe_w))
1067 { 1133 {
1068#if EV_USE_EVENTFD 1134#if EV_USE_EVENTFD
1135 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1136 if (evfd < 0 && errno == EINVAL)
1069 if ((evfd = eventfd (0, 0)) >= 0) 1137 evfd = eventfd (0, 0);
1138
1139 if (evfd >= 0)
1070 { 1140 {
1071 evpipe [0] = -1; 1141 evpipe [0] = -1;
1072 fd_intern (evfd); 1142 fd_intern (evfd); /* doing it twice doesn't hurt */
1073 ev_io_set (&pipe_w, evfd, EV_READ); 1143 ev_io_set (&pipe_w, evfd, EV_READ);
1074 } 1144 }
1075 else 1145 else
1076#endif 1146#endif
1077 { 1147 {
1189 signals [signum].gotsig = 0; 1259 signals [signum].gotsig = 0;
1190 1260
1191 for (w = signals [signum].head; w; w = w->next) 1261 for (w = signals [signum].head; w; w = w->next)
1192 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1262 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1193} 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
1194 1284
1195/*****************************************************************************/ 1285/*****************************************************************************/
1196 1286
1197static WL childs [EV_PID_HASHSIZE]; 1287static WL childs [EV_PID_HASHSIZE];
1198 1288
1343ev_backend (EV_P) 1433ev_backend (EV_P)
1344{ 1434{
1345 return backend; 1435 return backend;
1346} 1436}
1347 1437
1438#if EV_MINIMAL < 2
1348unsigned int 1439unsigned int
1349ev_loop_count (EV_P) 1440ev_loop_count (EV_P)
1350{ 1441{
1351 return loop_count; 1442 return loop_count;
1352} 1443}
1353 1444
1445unsigned int
1446ev_loop_depth (EV_P)
1447{
1448 return loop_depth;
1449}
1450
1354void 1451void
1355ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1452ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1356{ 1453{
1357 io_blocktime = interval; 1454 io_blocktime = interval;
1358} 1455}
1360void 1457void
1361ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1458ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1362{ 1459{
1363 timeout_blocktime = interval; 1460 timeout_blocktime = interval;
1364} 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
1365 1486
1366/* initialise a loop structure, must be zero-initialised */ 1487/* initialise a loop structure, must be zero-initialised */
1367static void noinline 1488static void noinline
1368loop_init (EV_P_ unsigned int flags) 1489loop_init (EV_P_ unsigned int flags)
1369{ 1490{
1391 1512
1392 ev_rt_now = ev_time (); 1513 ev_rt_now = ev_time ();
1393 mn_now = get_clock (); 1514 mn_now = get_clock ();
1394 now_floor = mn_now; 1515 now_floor = mn_now;
1395 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
1396 1520
1397 io_blocktime = 0.; 1521 io_blocktime = 0.;
1398 timeout_blocktime = 0.; 1522 timeout_blocktime = 0.;
1399 backend = 0; 1523 backend = 0;
1400 backend_fd = -1; 1524 backend_fd = -1;
1401 gotasync = 0; 1525 gotasync = 0;
1402#if EV_USE_INOTIFY 1526#if EV_USE_INOTIFY
1403 fs_fd = -2; 1527 fs_fd = -2;
1404#endif 1528#endif
1529#if EV_USE_SIGNALFD
1530 sigfd = -2;
1531#endif
1405 1532
1406 /* pid check not overridable via env */ 1533 /* pid check not overridable via env */
1407#ifndef _WIN32 1534#ifndef _WIN32
1408 if (flags & EVFLAG_FORKCHECK) 1535 if (flags & EVFLAG_FORKCHECK)
1409 curpid = getpid (); 1536 curpid = getpid ();
1446{ 1573{
1447 int i; 1574 int i;
1448 1575
1449 if (ev_is_active (&pipe_w)) 1576 if (ev_is_active (&pipe_w))
1450 { 1577 {
1451 ev_ref (EV_A); /* signal watcher */ 1578 /*ev_ref (EV_A);*/
1452 ev_io_stop (EV_A_ &pipe_w); 1579 /*ev_io_stop (EV_A_ &pipe_w);*/
1453 1580
1454#if EV_USE_EVENTFD 1581#if EV_USE_EVENTFD
1455 if (evfd >= 0) 1582 if (evfd >= 0)
1456 close (evfd); 1583 close (evfd);
1457#endif 1584#endif
1460 { 1587 {
1461 close (evpipe [0]); 1588 close (evpipe [0]);
1462 close (evpipe [1]); 1589 close (evpipe [1]);
1463 } 1590 }
1464 } 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
1465 1602
1466#if EV_USE_INOTIFY 1603#if EV_USE_INOTIFY
1467 if (fs_fd >= 0) 1604 if (fs_fd >= 0)
1468 close (fs_fd); 1605 close (fs_fd);
1469#endif 1606#endif
1573ev_loop_new (unsigned int flags) 1710ev_loop_new (unsigned int flags)
1574{ 1711{
1575 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));
1576 1713
1577 memset (loop, 0, sizeof (struct ev_loop)); 1714 memset (loop, 0, sizeof (struct ev_loop));
1578
1579 loop_init (EV_A_ flags); 1715 loop_init (EV_A_ flags);
1580 1716
1581 if (ev_backend (EV_A)) 1717 if (ev_backend (EV_A))
1582 return loop; 1718 return loop;
1583 1719
1594void 1730void
1595ev_loop_fork (EV_P) 1731ev_loop_fork (EV_P)
1596{ 1732{
1597 postfork = 1; /* must be in line with ev_default_fork */ 1733 postfork = 1; /* must be in line with ev_default_fork */
1598} 1734}
1735#endif /* multiplicity */
1599 1736
1600#if EV_VERIFY 1737#if EV_VERIFY
1601static void noinline 1738static void noinline
1602verify_watcher (EV_P_ W w) 1739verify_watcher (EV_P_ W w)
1603{ 1740{
1631 verify_watcher (EV_A_ ws [cnt]); 1768 verify_watcher (EV_A_ ws [cnt]);
1632 } 1769 }
1633} 1770}
1634#endif 1771#endif
1635 1772
1773#if EV_MINIMAL < 2
1636void 1774void
1637ev_loop_verify (EV_P) 1775ev_loop_verify (EV_P)
1638{ 1776{
1639#if EV_VERIFY 1777#if EV_VERIFY
1640 int i; 1778 int i;
1693 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)
1694 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1832 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1695# endif 1833# endif
1696#endif 1834#endif
1697} 1835}
1698 1836#endif
1699#endif /* multiplicity */
1700 1837
1701#if EV_MULTIPLICITY 1838#if EV_MULTIPLICITY
1702struct ev_loop * 1839struct ev_loop *
1703ev_default_loop_init (unsigned int flags) 1840ev_default_loop_init (unsigned int flags)
1704#else 1841#else
1765ev_invoke (EV_P_ void *w, int revents) 1902ev_invoke (EV_P_ void *w, int revents)
1766{ 1903{
1767 EV_CB_INVOKE ((W)w, revents); 1904 EV_CB_INVOKE ((W)w, revents);
1768} 1905}
1769 1906
1770inline_speed void 1907unsigned int
1771call_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)
1772{ 1921{
1773 int pri; 1922 int pri;
1774 1923
1775 for (pri = NUMPRI; pri--; ) 1924 for (pri = NUMPRI; pri--; )
1776 while (pendingcnt [pri]) 1925 while (pendingcnt [pri])
1948/* fetch new monotonic and realtime times from the kernel */ 2097/* fetch new monotonic and realtime times from the kernel */
1949/* also detetc if there was a timejump, and act accordingly */ 2098/* also detetc if there was a timejump, and act accordingly */
1950inline_speed void 2099inline_speed void
1951time_update (EV_P_ ev_tstamp max_block) 2100time_update (EV_P_ ev_tstamp max_block)
1952{ 2101{
1953 int i;
1954
1955#if EV_USE_MONOTONIC 2102#if EV_USE_MONOTONIC
1956 if (expect_true (have_monotonic)) 2103 if (expect_true (have_monotonic))
1957 { 2104 {
2105 int i;
1958 ev_tstamp odiff = rtmn_diff; 2106 ev_tstamp odiff = rtmn_diff;
1959 2107
1960 mn_now = get_clock (); 2108 mn_now = get_clock ();
1961 2109
1962 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 2110 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
2012 2160
2013 mn_now = ev_rt_now; 2161 mn_now = ev_rt_now;
2014 } 2162 }
2015} 2163}
2016 2164
2017static int loop_done;
2018
2019void 2165void
2020ev_loop (EV_P_ int flags) 2166ev_loop (EV_P_ int flags)
2021{ 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
2022 loop_done = EVUNLOOP_CANCEL; 2174 loop_done = EVUNLOOP_CANCEL;
2023 2175
2024 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 */
2025 2177
2026 do 2178 do
2027 { 2179 {
2028#if EV_VERIFY >= 2 2180#if EV_VERIFY >= 2
2029 ev_loop_verify (EV_A); 2181 ev_loop_verify (EV_A);
2042 /* we might have forked, so queue fork handlers */ 2194 /* we might have forked, so queue fork handlers */
2043 if (expect_false (postfork)) 2195 if (expect_false (postfork))
2044 if (forkcnt) 2196 if (forkcnt)
2045 { 2197 {
2046 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2198 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2047 call_pending (EV_A); 2199 EV_INVOKE_PENDING;
2048 } 2200 }
2049#endif 2201#endif
2050 2202
2051 /* queue prepare watchers (and execute them) */ 2203 /* queue prepare watchers (and execute them) */
2052 if (expect_false (preparecnt)) 2204 if (expect_false (preparecnt))
2053 { 2205 {
2054 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2206 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2055 call_pending (EV_A); 2207 EV_INVOKE_PENDING;
2056 } 2208 }
2209
2210 if (expect_false (loop_done))
2211 break;
2057 2212
2058 /* we might have forked, so reify kernel state if necessary */ 2213 /* we might have forked, so reify kernel state if necessary */
2059 if (expect_false (postfork)) 2214 if (expect_false (postfork))
2060 loop_fork (EV_A); 2215 loop_fork (EV_A);
2061 2216
2067 ev_tstamp waittime = 0.; 2222 ev_tstamp waittime = 0.;
2068 ev_tstamp sleeptime = 0.; 2223 ev_tstamp sleeptime = 0.;
2069 2224
2070 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2225 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2071 { 2226 {
2227 /* remember old timestamp for io_blocktime calculation */
2228 ev_tstamp prev_mn_now = mn_now;
2229
2072 /* update time to cancel out callback processing overhead */ 2230 /* update time to cancel out callback processing overhead */
2073 time_update (EV_A_ 1e100); 2231 time_update (EV_A_ 1e100);
2074 2232
2075 waittime = MAX_BLOCKTIME; 2233 waittime = MAX_BLOCKTIME;
2076 2234
2086 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;
2087 if (waittime > to) waittime = to; 2245 if (waittime > to) waittime = to;
2088 } 2246 }
2089#endif 2247#endif
2090 2248
2249 /* don't let timeouts decrease the waittime below timeout_blocktime */
2091 if (expect_false (waittime < timeout_blocktime)) 2250 if (expect_false (waittime < timeout_blocktime))
2092 waittime = timeout_blocktime; 2251 waittime = timeout_blocktime;
2093 2252
2094 sleeptime = waittime - backend_fudge; 2253 /* extra check because io_blocktime is commonly 0 */
2095
2096 if (expect_true (sleeptime > io_blocktime)) 2254 if (expect_false (io_blocktime))
2097 sleeptime = io_blocktime;
2098
2099 if (sleeptime)
2100 { 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 {
2101 ev_sleep (sleeptime); 2263 ev_sleep (sleeptime);
2102 waittime -= sleeptime; 2264 waittime -= sleeptime;
2265 }
2103 } 2266 }
2104 } 2267 }
2105 2268
2269#if EV_MINIMAL < 2
2106 ++loop_count; 2270 ++loop_count;
2271#endif
2272 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2107 backend_poll (EV_A_ waittime); 2273 backend_poll (EV_A_ waittime);
2274 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2108 2275
2109 /* update ev_rt_now, do magic */ 2276 /* update ev_rt_now, do magic */
2110 time_update (EV_A_ waittime + sleeptime); 2277 time_update (EV_A_ waittime + sleeptime);
2111 } 2278 }
2112 2279
2123 2290
2124 /* queue check watchers, to be executed first */ 2291 /* queue check watchers, to be executed first */
2125 if (expect_false (checkcnt)) 2292 if (expect_false (checkcnt))
2126 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2293 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2127 2294
2128 call_pending (EV_A); 2295 EV_INVOKE_PENDING;
2129 } 2296 }
2130 while (expect_true ( 2297 while (expect_true (
2131 activecnt 2298 activecnt
2132 && !loop_done 2299 && !loop_done
2133 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2300 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2134 )); 2301 ));
2135 2302
2136 if (loop_done == EVUNLOOP_ONE) 2303 if (loop_done == EVUNLOOP_ONE)
2137 loop_done = EVUNLOOP_CANCEL; 2304 loop_done = EVUNLOOP_CANCEL;
2305
2306#if EV_MINIMAL < 2
2307 --loop_depth;
2308#endif
2138} 2309}
2139 2310
2140void 2311void
2141ev_unloop (EV_P_ int how) 2312ev_unloop (EV_P_ int how)
2142{ 2313{
2234} 2405}
2235 2406
2236inline_size void 2407inline_size void
2237pri_adjust (EV_P_ W w) 2408pri_adjust (EV_P_ W w)
2238{ 2409{
2239 int pri = w->priority; 2410 int pri = ev_priority (w);
2240 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2411 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2241 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2412 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2242 w->priority = pri; 2413 ev_set_priority (w, pri);
2243} 2414}
2244 2415
2245inline_speed void 2416inline_speed void
2246ev_start (EV_P_ W w, int active) 2417ev_start (EV_P_ W w, int active)
2247{ 2418{
2274 2445
2275 ev_start (EV_A_ (W)w, 1); 2446 ev_start (EV_A_ (W)w, 1);
2276 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2447 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2277 wlist_add (&anfds[fd].head, (WL)w); 2448 wlist_add (&anfds[fd].head, (WL)w);
2278 2449
2279 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2450 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2280 w->events &= ~EV__IOFDSET; 2451 w->events &= ~EV__IOFDSET;
2281 2452
2282 EV_FREQUENT_CHECK; 2453 EV_FREQUENT_CHECK;
2283} 2454}
2284 2455
2378 } 2549 }
2379 2550
2380 EV_FREQUENT_CHECK; 2551 EV_FREQUENT_CHECK;
2381} 2552}
2382 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
2383#if EV_PERIODIC_ENABLE 2560#if EV_PERIODIC_ENABLE
2384void noinline 2561void noinline
2385ev_periodic_start (EV_P_ ev_periodic *w) 2562ev_periodic_start (EV_P_ ev_periodic *w)
2386{ 2563{
2387 if (expect_false (ev_is_active (w))) 2564 if (expect_false (ev_is_active (w)))
2462 if (expect_false (ev_is_active (w))) 2639 if (expect_false (ev_is_active (w)))
2463 return; 2640 return;
2464 2641
2465 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));
2466 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
2467 evpipe_init (EV_A); 2676 evpipe_init (EV_A);
2468
2469 EV_FREQUENT_CHECK;
2470 2677
2471 { 2678 {
2472#ifndef _WIN32 2679#ifndef _WIN32
2473 sigset_t full, prev; 2680 sigset_t full, prev;
2474 sigfillset (&full); 2681 sigfillset (&full);
2476#endif 2683#endif
2477 2684
2478 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2685 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2479 2686
2480#ifndef _WIN32 2687#ifndef _WIN32
2688# if EV_USE_SIGNALFD
2689 if (sigfd < 0)/*TODO*/
2690# endif
2691 sigdelset (&prev, w->signum);
2481 sigprocmask (SIG_SETMASK, &prev, 0); 2692 sigprocmask (SIG_SETMASK, &prev, 0);
2482#endif 2693#endif
2483 } 2694 }
2484 2695
2485 ev_start (EV_A_ (W)w, 1); 2696 ev_start (EV_A_ (W)w, 1);
2488 if (!((WL)w)->next) 2699 if (!((WL)w)->next)
2489 { 2700 {
2490#if _WIN32 2701#if _WIN32
2491 signal (w->signum, ev_sighandler); 2702 signal (w->signum, ev_sighandler);
2492#else 2703#else
2704# if EV_USE_SIGNALFD
2705 if (sigfd < 0) /*TODO*/
2706# endif
2707 {
2493 struct sigaction sa; 2708 struct sigaction sa = { };
2494 sa.sa_handler = ev_sighandler; 2709 sa.sa_handler = ev_sighandler;
2495 sigfillset (&sa.sa_mask); 2710 sigfillset (&sa.sa_mask);
2496 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 */
2497 sigaction (w->signum, &sa, 0); 2712 sigaction (w->signum, &sa, 0);
2713 }
2498#endif 2714#endif
2499 } 2715 }
2500 2716
2501 EV_FREQUENT_CHECK; 2717 EV_FREQUENT_CHECK;
2502} 2718}
2512 2728
2513 wlist_del (&signals [w->signum - 1].head, (WL)w); 2729 wlist_del (&signals [w->signum - 1].head, (WL)w);
2514 ev_stop (EV_A_ (W)w); 2730 ev_stop (EV_A_ (W)w);
2515 2731
2516 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
2517 signal (w->signum, SIG_DFL); 2744 signal (w->signum, SIG_DFL);
2518 2745
2519 EV_FREQUENT_CHECK; 2746 EV_FREQUENT_CHECK;
2520} 2747}
2521 2748
2522void 2749void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines