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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines