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

Comparing libev/ev.c (file contents):
Revision 1.178 by root, Tue Dec 11 18:36:11 2007 UTC vs.
Revision 1.183 by root, Wed Dec 12 05:11:56 2007 UTC

476 pendings [pri][w_->pending - 1].w = w_; 476 pendings [pri][w_->pending - 1].w = w_;
477 pendings [pri][w_->pending - 1].events = revents; 477 pendings [pri][w_->pending - 1].events = revents;
478 } 478 }
479} 479}
480 480
481void inline_size 481void inline_speed
482queue_events (EV_P_ W *events, int eventcnt, int type) 482queue_events (EV_P_ W *events, int eventcnt, int type)
483{ 483{
484 int i; 484 int i;
485 485
486 for (i = 0; i < eventcnt; ++i) 486 for (i = 0; i < eventcnt; ++i)
557 557
558 fdchangecnt = 0; 558 fdchangecnt = 0;
559} 559}
560 560
561void inline_size 561void inline_size
562fd_change (EV_P_ int fd) 562fd_change (EV_P_ int fd, int flags)
563{ 563{
564 if (expect_false (anfds [fd].reify)) 564 unsigned char reify = anfds [fd].reify;
565 return;
566
567 anfds [fd].reify = 1; 565 anfds [fd].reify |= flags | 1;
568 566
567 if (expect_true (!reify))
568 {
569 ++fdchangecnt; 569 ++fdchangecnt;
570 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 570 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
571 fdchanges [fdchangecnt - 1] = fd; 571 fdchanges [fdchangecnt - 1] = fd;
572 }
572} 573}
573 574
574void inline_speed 575void inline_speed
575fd_kill (EV_P_ int fd) 576fd_kill (EV_P_ int fd)
576{ 577{
627 628
628 for (fd = 0; fd < anfdmax; ++fd) 629 for (fd = 0; fd < anfdmax; ++fd)
629 if (anfds [fd].events) 630 if (anfds [fd].events)
630 { 631 {
631 anfds [fd].events = 0; 632 anfds [fd].events = 0;
632 fd_change (EV_A_ fd); 633 fd_change (EV_A_ fd, EV_IOFDSET);
633 } 634 }
634} 635}
635 636
636/*****************************************************************************/ 637/*****************************************************************************/
637 638
638void inline_speed 639void inline_speed
639upheap (WT *heap, int k) 640upheap (WT *heap, int k)
640{ 641{
641 WT w = heap [k]; 642 WT w = heap [k];
642 643
643 while (k && heap [k >> 1]->at > w->at) 644 while (k)
644 { 645 {
646 int p = (k - 1) >> 1;
647
648 if (heap [p]->at <= w->at)
649 break;
650
645 heap [k] = heap [k >> 1]; 651 heap [k] = heap [p];
646 ((W)heap [k])->active = k + 1; 652 ((W)heap [k])->active = k + 1;
647 k >>= 1; 653 k = p;
648 } 654 }
649 655
650 heap [k] = w; 656 heap [k] = w;
651 ((W)heap [k])->active = k + 1; 657 ((W)heap [k])->active = k + 1;
652
653} 658}
654 659
655void inline_speed 660void inline_speed
656downheap (WT *heap, int N, int k) 661downheap (WT *heap, int N, int k)
657{ 662{
658 WT w = heap [k]; 663 WT w = heap [k];
659 664
660 while (k < (N >> 1)) 665 for (;;)
661 { 666 {
662 int j = k << 1; 667 int c = (k << 1) + 1;
663 668
664 if (j + 1 < N && heap [j]->at > heap [j + 1]->at) 669 if (c >= N)
665 ++j;
666
667 if (w->at <= heap [j]->at)
668 break; 670 break;
669 671
672 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
673 ? 1 : 0;
674
675 if (w->at <= heap [c]->at)
676 break;
677
670 heap [k] = heap [j]; 678 heap [k] = heap [c];
671 ((W)heap [k])->active = k + 1; 679 ((W)heap [k])->active = k + 1;
680
672 k = j; 681 k = c;
673 } 682 }
674 683
675 heap [k] = w; 684 heap [k] = w;
676 ((W)heap [k])->active = k + 1; 685 ((W)heap [k])->active = k + 1;
677} 686}
784 ev_unref (EV_A); /* child watcher should not keep loop alive */ 793 ev_unref (EV_A); /* child watcher should not keep loop alive */
785} 794}
786 795
787/*****************************************************************************/ 796/*****************************************************************************/
788 797
789static ev_child *childs [EV_PID_HASHSIZE]; 798static WL childs [EV_PID_HASHSIZE];
790 799
791#ifndef _WIN32 800#ifndef _WIN32
792 801
793static ev_signal childev; 802static ev_signal childev;
794 803
1206void inline_size 1215void inline_size
1207timers_reify (EV_P) 1216timers_reify (EV_P)
1208{ 1217{
1209 while (timercnt && ((WT)timers [0])->at <= mn_now) 1218 while (timercnt && ((WT)timers [0])->at <= mn_now)
1210 { 1219 {
1211 ev_timer *w = timers [0]; 1220 ev_timer *w = (ev_timer *)timers [0];
1212 1221
1213 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1222 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1214 1223
1215 /* first reschedule or stop timer */ 1224 /* first reschedule or stop timer */
1216 if (w->repeat) 1225 if (w->repeat)
1219 1228
1220 ((WT)w)->at += w->repeat; 1229 ((WT)w)->at += w->repeat;
1221 if (((WT)w)->at < mn_now) 1230 if (((WT)w)->at < mn_now)
1222 ((WT)w)->at = mn_now; 1231 ((WT)w)->at = mn_now;
1223 1232
1224 downheap ((WT *)timers, timercnt, 0); 1233 downheap (timers, timercnt, 0);
1225 } 1234 }
1226 else 1235 else
1227 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1236 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1228 1237
1229 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1238 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1234void inline_size 1243void inline_size
1235periodics_reify (EV_P) 1244periodics_reify (EV_P)
1236{ 1245{
1237 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1246 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1238 { 1247 {
1239 ev_periodic *w = periodics [0]; 1248 ev_periodic *w = (ev_periodic *)periodics [0];
1240 1249
1241 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1250 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1242 1251
1243 /* first reschedule or stop timer */ 1252 /* first reschedule or stop timer */
1244 if (w->reschedule_cb) 1253 if (w->reschedule_cb)
1245 { 1254 {
1246 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); 1255 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1247 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1256 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1248 downheap ((WT *)periodics, periodiccnt, 0); 1257 downheap (periodics, periodiccnt, 0);
1249 } 1258 }
1250 else if (w->interval) 1259 else if (w->interval)
1251 { 1260 {
1252 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1261 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1253 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; 1262 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1254 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); 1263 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1255 downheap ((WT *)periodics, periodiccnt, 0); 1264 downheap (periodics, periodiccnt, 0);
1256 } 1265 }
1257 else 1266 else
1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1267 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1259 1268
1260 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1269 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1267 int i; 1276 int i;
1268 1277
1269 /* adjust periodics after time jump */ 1278 /* adjust periodics after time jump */
1270 for (i = 0; i < periodiccnt; ++i) 1279 for (i = 0; i < periodiccnt; ++i)
1271 { 1280 {
1272 ev_periodic *w = periodics [i]; 1281 ev_periodic *w = (ev_periodic *)periodics [i];
1273 1282
1274 if (w->reschedule_cb) 1283 if (w->reschedule_cb)
1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1284 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1276 else if (w->interval) 1285 else if (w->interval)
1277 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1286 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1278 } 1287 }
1279 1288
1280 /* now rebuild the heap */ 1289 /* now rebuild the heap */
1281 for (i = periodiccnt >> 1; i--; ) 1290 for (i = periodiccnt >> 1; i--; )
1282 downheap ((WT *)periodics, periodiccnt, i); 1291 downheap (periodics, periodiccnt, i);
1283} 1292}
1284#endif 1293#endif
1285 1294
1286#if EV_IDLE_ENABLE 1295#if EV_IDLE_ENABLE
1287void inline_size 1296void inline_size
1589 1598
1590 assert (("ev_io_start called with negative fd", fd >= 0)); 1599 assert (("ev_io_start called with negative fd", fd >= 0));
1591 1600
1592 ev_start (EV_A_ (W)w, 1); 1601 ev_start (EV_A_ (W)w, 1);
1593 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); 1602 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1594 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1603 wlist_add (&anfds[fd].head, (WL)w);
1595 1604
1596 fd_change (EV_A_ fd); 1605 fd_change (EV_A_ fd, w->events & EV_IOFDSET);
1606 w->events &= ~ EV_IOFDSET;
1597} 1607}
1598 1608
1599void noinline 1609void noinline
1600ev_io_stop (EV_P_ ev_io *w) 1610ev_io_stop (EV_P_ ev_io *w)
1601{ 1611{
1603 if (expect_false (!ev_is_active (w))) 1613 if (expect_false (!ev_is_active (w)))
1604 return; 1614 return;
1605 1615
1606 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1616 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1607 1617
1608 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1618 wlist_del (&anfds[w->fd].head, (WL)w);
1609 ev_stop (EV_A_ (W)w); 1619 ev_stop (EV_A_ (W)w);
1610 1620
1611 fd_change (EV_A_ w->fd); 1621 fd_change (EV_A_ w->fd, 0);
1612} 1622}
1613 1623
1614void noinline 1624void noinline
1615ev_timer_start (EV_P_ ev_timer *w) 1625ev_timer_start (EV_P_ ev_timer *w)
1616{ 1626{
1620 ((WT)w)->at += mn_now; 1630 ((WT)w)->at += mn_now;
1621 1631
1622 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1632 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1623 1633
1624 ev_start (EV_A_ (W)w, ++timercnt); 1634 ev_start (EV_A_ (W)w, ++timercnt);
1625 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); 1635 array_needsize (WT, timers, timermax, timercnt, EMPTY2);
1626 timers [timercnt - 1] = w; 1636 timers [timercnt - 1] = (WT)w;
1627 upheap ((WT *)timers, timercnt - 1); 1637 upheap (timers, timercnt - 1);
1628 1638
1629 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1639 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1630} 1640}
1631 1641
1632void noinline 1642void noinline
1634{ 1644{
1635 clear_pending (EV_A_ (W)w); 1645 clear_pending (EV_A_ (W)w);
1636 if (expect_false (!ev_is_active (w))) 1646 if (expect_false (!ev_is_active (w)))
1637 return; 1647 return;
1638 1648
1639 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1649 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1640 1650
1641 { 1651 {
1642 int active = ((W)w)->active; 1652 int active = ((W)w)->active;
1643 1653
1644 if (expect_true (--active < --timercnt)) 1654 if (expect_true (--active < --timercnt))
1645 { 1655 {
1646 timers [active] = timers [timercnt]; 1656 timers [active] = timers [timercnt];
1647 adjustheap ((WT *)timers, timercnt, active); 1657 adjustheap (timers, timercnt, active);
1648 } 1658 }
1649 } 1659 }
1650 1660
1651 ((WT)w)->at -= mn_now; 1661 ((WT)w)->at -= mn_now;
1652 1662
1659 if (ev_is_active (w)) 1669 if (ev_is_active (w))
1660 { 1670 {
1661 if (w->repeat) 1671 if (w->repeat)
1662 { 1672 {
1663 ((WT)w)->at = mn_now + w->repeat; 1673 ((WT)w)->at = mn_now + w->repeat;
1664 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1674 adjustheap (timers, timercnt, ((W)w)->active - 1);
1665 } 1675 }
1666 else 1676 else
1667 ev_timer_stop (EV_A_ w); 1677 ev_timer_stop (EV_A_ w);
1668 } 1678 }
1669 else if (w->repeat) 1679 else if (w->repeat)
1690 } 1700 }
1691 else 1701 else
1692 ((WT)w)->at = w->offset; 1702 ((WT)w)->at = w->offset;
1693 1703
1694 ev_start (EV_A_ (W)w, ++periodiccnt); 1704 ev_start (EV_A_ (W)w, ++periodiccnt);
1695 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1705 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2);
1696 periodics [periodiccnt - 1] = w; 1706 periodics [periodiccnt - 1] = (WT)w;
1697 upheap ((WT *)periodics, periodiccnt - 1); 1707 upheap (periodics, periodiccnt - 1);
1698 1708
1699 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1709 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1700} 1710}
1701 1711
1702void noinline 1712void noinline
1704{ 1714{
1705 clear_pending (EV_A_ (W)w); 1715 clear_pending (EV_A_ (W)w);
1706 if (expect_false (!ev_is_active (w))) 1716 if (expect_false (!ev_is_active (w)))
1707 return; 1717 return;
1708 1718
1709 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1719 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1710 1720
1711 { 1721 {
1712 int active = ((W)w)->active; 1722 int active = ((W)w)->active;
1713 1723
1714 if (expect_true (--active < --periodiccnt)) 1724 if (expect_true (--active < --periodiccnt))
1715 { 1725 {
1716 periodics [active] = periodics [periodiccnt]; 1726 periodics [active] = periodics [periodiccnt];
1717 adjustheap ((WT *)periodics, periodiccnt, active); 1727 adjustheap (periodics, periodiccnt, active);
1718 } 1728 }
1719 } 1729 }
1720 1730
1721 ev_stop (EV_A_ (W)w); 1731 ev_stop (EV_A_ (W)w);
1722} 1732}
1743 if (expect_false (ev_is_active (w))) 1753 if (expect_false (ev_is_active (w)))
1744 return; 1754 return;
1745 1755
1746 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1756 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1747 1757
1758 {
1759#ifndef _WIN32
1760 sigset_t full, prev;
1761 sigfillset (&full);
1762 sigprocmask (SIG_SETMASK, &full, &prev);
1763#endif
1764
1765 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1766
1767#ifndef _WIN32
1768 sigprocmask (SIG_SETMASK, &prev, 0);
1769#endif
1770 }
1771
1748 ev_start (EV_A_ (W)w, 1); 1772 ev_start (EV_A_ (W)w, 1);
1749 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1750 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1773 wlist_add (&signals [w->signum - 1].head, (WL)w);
1751 1774
1752 if (!((WL)w)->next) 1775 if (!((WL)w)->next)
1753 { 1776 {
1754#if _WIN32 1777#if _WIN32
1755 signal (w->signum, sighandler); 1778 signal (w->signum, sighandler);
1768{ 1791{
1769 clear_pending (EV_A_ (W)w); 1792 clear_pending (EV_A_ (W)w);
1770 if (expect_false (!ev_is_active (w))) 1793 if (expect_false (!ev_is_active (w)))
1771 return; 1794 return;
1772 1795
1773 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1796 wlist_del (&signals [w->signum - 1].head, (WL)w);
1774 ev_stop (EV_A_ (W)w); 1797 ev_stop (EV_A_ (W)w);
1775 1798
1776 if (!signals [w->signum - 1].head) 1799 if (!signals [w->signum - 1].head)
1777 signal (w->signum, SIG_DFL); 1800 signal (w->signum, SIG_DFL);
1778} 1801}
1785#endif 1808#endif
1786 if (expect_false (ev_is_active (w))) 1809 if (expect_false (ev_is_active (w)))
1787 return; 1810 return;
1788 1811
1789 ev_start (EV_A_ (W)w, 1); 1812 ev_start (EV_A_ (W)w, 1);
1790 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1813 wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1791} 1814}
1792 1815
1793void 1816void
1794ev_child_stop (EV_P_ ev_child *w) 1817ev_child_stop (EV_P_ ev_child *w)
1795{ 1818{
1796 clear_pending (EV_A_ (W)w); 1819 clear_pending (EV_A_ (W)w);
1797 if (expect_false (!ev_is_active (w))) 1820 if (expect_false (!ev_is_active (w)))
1798 return; 1821 return;
1799 1822
1800 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1823 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1801 ev_stop (EV_A_ (W)w); 1824 ev_stop (EV_A_ (W)w);
1802} 1825}
1803 1826
1804#if EV_STAT_ENABLE 1827#if EV_STAT_ENABLE
1805 1828

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines