… | |
… | |
216 | # include <sys/inotify.h> |
216 | # include <sys/inotify.h> |
217 | #endif |
217 | #endif |
218 | |
218 | |
219 | /**/ |
219 | /**/ |
220 | |
220 | |
|
|
221 | /* |
|
|
222 | * This is used to avoid floating point rounding problems. |
|
|
223 | * It is added to ev_rt_now when scheduling periodics |
|
|
224 | * to ensure progress, time-wise, even when rounding |
|
|
225 | * errors are against us. |
|
|
226 | * This value is good at least till the year 4000 |
|
|
227 | * and intervals up to 20 years. |
|
|
228 | * Better solutions welcome. |
|
|
229 | */ |
|
|
230 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
|
|
231 | |
221 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
232 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
222 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
233 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
223 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
234 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ |
224 | |
235 | |
225 | #if __GNUC__ >= 3 |
236 | #if __GNUC__ >= 3 |
226 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
237 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
227 | # define noinline __attribute__ ((noinline)) |
238 | # define noinline __attribute__ ((noinline)) |
228 | #else |
239 | #else |
… | |
… | |
418 | } |
429 | } |
419 | |
430 | |
420 | return ncur; |
431 | return ncur; |
421 | } |
432 | } |
422 | |
433 | |
423 | inline_speed void * |
434 | static noinline void * |
424 | array_realloc (int elem, void *base, int *cur, int cnt) |
435 | array_realloc (int elem, void *base, int *cur, int cnt) |
425 | { |
436 | { |
426 | *cur = array_nextsize (elem, *cur, cnt); |
437 | *cur = array_nextsize (elem, *cur, cnt); |
427 | return ev_realloc (base, elem * *cur); |
438 | return ev_realloc (base, elem * *cur); |
428 | } |
439 | } |
… | |
… | |
453 | |
464 | |
454 | void noinline |
465 | void noinline |
455 | ev_feed_event (EV_P_ void *w, int revents) |
466 | ev_feed_event (EV_P_ void *w, int revents) |
456 | { |
467 | { |
457 | W w_ = (W)w; |
468 | W w_ = (W)w; |
|
|
469 | int pri = ABSPRI (w_); |
458 | |
470 | |
459 | if (expect_false (w_->pending)) |
471 | if (expect_false (w_->pending)) |
|
|
472 | pendings [pri][w_->pending - 1].events |= revents; |
|
|
473 | else |
460 | { |
474 | { |
|
|
475 | w_->pending = ++pendingcnt [pri]; |
|
|
476 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
|
|
477 | pendings [pri][w_->pending - 1].w = w_; |
461 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
478 | pendings [pri][w_->pending - 1].events = revents; |
462 | return; |
|
|
463 | } |
479 | } |
464 | |
|
|
465 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
|
|
466 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
|
|
467 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
|
|
468 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
|
|
469 | } |
480 | } |
470 | |
481 | |
471 | void inline_size |
482 | void inline_size |
472 | queue_events (EV_P_ W *events, int eventcnt, int type) |
483 | queue_events (EV_P_ W *events, int eventcnt, int type) |
473 | { |
484 | { |
… | |
… | |
749 | for (signum = signalmax; signum--; ) |
760 | for (signum = signalmax; signum--; ) |
750 | if (signals [signum].gotsig) |
761 | if (signals [signum].gotsig) |
751 | ev_feed_signal_event (EV_A_ signum + 1); |
762 | ev_feed_signal_event (EV_A_ signum + 1); |
752 | } |
763 | } |
753 | |
764 | |
754 | void inline_size |
765 | void inline_speed |
755 | fd_intern (int fd) |
766 | fd_intern (int fd) |
756 | { |
767 | { |
757 | #ifdef _WIN32 |
768 | #ifdef _WIN32 |
758 | int arg = 1; |
769 | int arg = 1; |
759 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
770 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
1231 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1242 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1232 | |
1243 | |
1233 | /* first reschedule or stop timer */ |
1244 | /* first reschedule or stop timer */ |
1234 | if (w->reschedule_cb) |
1245 | if (w->reschedule_cb) |
1235 | { |
1246 | { |
1236 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1247 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
1237 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1248 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1238 | downheap ((WT *)periodics, periodiccnt, 0); |
1249 | downheap ((WT *)periodics, periodiccnt, 0); |
1239 | } |
1250 | } |
1240 | else if (w->interval) |
1251 | else if (w->interval) |
1241 | { |
1252 | { |
1242 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1253 | ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval; |
1243 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1254 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1244 | downheap ((WT *)periodics, periodiccnt, 0); |
1255 | downheap ((WT *)periodics, periodiccnt, 0); |
1245 | } |
1256 | } |
1246 | else |
1257 | else |
1247 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1258 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
1261 | ev_periodic *w = periodics [i]; |
1272 | ev_periodic *w = periodics [i]; |
1262 | |
1273 | |
1263 | if (w->reschedule_cb) |
1274 | if (w->reschedule_cb) |
1264 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1275 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1265 | else if (w->interval) |
1276 | else if (w->interval) |
1266 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1277 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1267 | } |
1278 | } |
1268 | |
1279 | |
1269 | /* now rebuild the heap */ |
1280 | /* now rebuild the heap */ |
1270 | for (i = periodiccnt >> 1; i--; ) |
1281 | for (i = periodiccnt >> 1; i--; ) |
1271 | downheap ((WT *)periodics, periodiccnt, i); |
1282 | downheap ((WT *)periodics, periodiccnt, i); |
… | |
… | |
1546 | ev_clear_pending (EV_P_ void *w) |
1557 | ev_clear_pending (EV_P_ void *w) |
1547 | { |
1558 | { |
1548 | W w_ = (W)w; |
1559 | W w_ = (W)w; |
1549 | int pending = w_->pending; |
1560 | int pending = w_->pending; |
1550 | |
1561 | |
1551 | if (!pending) |
1562 | if (expect_true (pending)) |
|
|
1563 | { |
|
|
1564 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
1565 | w_->pending = 0; |
|
|
1566 | p->w = 0; |
|
|
1567 | return p->events; |
|
|
1568 | } |
|
|
1569 | else |
1552 | return 0; |
1570 | return 0; |
1553 | |
|
|
1554 | w_->pending = 0; |
|
|
1555 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
1556 | p->w = 0; |
|
|
1557 | |
|
|
1558 | return p->events; |
|
|
1559 | } |
1571 | } |
1560 | |
1572 | |
1561 | void inline_size |
1573 | void inline_size |
1562 | pri_adjust (EV_P_ W w) |
1574 | pri_adjust (EV_P_ W w) |
1563 | { |
1575 | { |
… | |
… | |
1582 | w->active = 0; |
1594 | w->active = 0; |
1583 | } |
1595 | } |
1584 | |
1596 | |
1585 | /*****************************************************************************/ |
1597 | /*****************************************************************************/ |
1586 | |
1598 | |
1587 | void |
1599 | void noinline |
1588 | ev_io_start (EV_P_ ev_io *w) |
1600 | ev_io_start (EV_P_ ev_io *w) |
1589 | { |
1601 | { |
1590 | int fd = w->fd; |
1602 | int fd = w->fd; |
1591 | |
1603 | |
1592 | if (expect_false (ev_is_active (w))) |
1604 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1599 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1611 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1600 | |
1612 | |
1601 | fd_change (EV_A_ fd); |
1613 | fd_change (EV_A_ fd); |
1602 | } |
1614 | } |
1603 | |
1615 | |
1604 | void |
1616 | void noinline |
1605 | ev_io_stop (EV_P_ ev_io *w) |
1617 | ev_io_stop (EV_P_ ev_io *w) |
1606 | { |
1618 | { |
1607 | clear_pending (EV_A_ (W)w); |
1619 | clear_pending (EV_A_ (W)w); |
1608 | if (expect_false (!ev_is_active (w))) |
1620 | if (expect_false (!ev_is_active (w))) |
1609 | return; |
1621 | return; |
… | |
… | |
1614 | ev_stop (EV_A_ (W)w); |
1626 | ev_stop (EV_A_ (W)w); |
1615 | |
1627 | |
1616 | fd_change (EV_A_ w->fd); |
1628 | fd_change (EV_A_ w->fd); |
1617 | } |
1629 | } |
1618 | |
1630 | |
1619 | void |
1631 | void noinline |
1620 | ev_timer_start (EV_P_ ev_timer *w) |
1632 | ev_timer_start (EV_P_ ev_timer *w) |
1621 | { |
1633 | { |
1622 | if (expect_false (ev_is_active (w))) |
1634 | if (expect_false (ev_is_active (w))) |
1623 | return; |
1635 | return; |
1624 | |
1636 | |
… | |
… | |
1632 | upheap ((WT *)timers, timercnt - 1); |
1644 | upheap ((WT *)timers, timercnt - 1); |
1633 | |
1645 | |
1634 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1646 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1635 | } |
1647 | } |
1636 | |
1648 | |
1637 | void |
1649 | void noinline |
1638 | ev_timer_stop (EV_P_ ev_timer *w) |
1650 | ev_timer_stop (EV_P_ ev_timer *w) |
1639 | { |
1651 | { |
1640 | clear_pending (EV_A_ (W)w); |
1652 | clear_pending (EV_A_ (W)w); |
1641 | if (expect_false (!ev_is_active (w))) |
1653 | if (expect_false (!ev_is_active (w))) |
1642 | return; |
1654 | return; |
… | |
… | |
1656 | ((WT)w)->at -= mn_now; |
1668 | ((WT)w)->at -= mn_now; |
1657 | |
1669 | |
1658 | ev_stop (EV_A_ (W)w); |
1670 | ev_stop (EV_A_ (W)w); |
1659 | } |
1671 | } |
1660 | |
1672 | |
1661 | void |
1673 | void noinline |
1662 | ev_timer_again (EV_P_ ev_timer *w) |
1674 | ev_timer_again (EV_P_ ev_timer *w) |
1663 | { |
1675 | { |
1664 | if (ev_is_active (w)) |
1676 | if (ev_is_active (w)) |
1665 | { |
1677 | { |
1666 | if (w->repeat) |
1678 | if (w->repeat) |
… | |
… | |
1677 | ev_timer_start (EV_A_ w); |
1689 | ev_timer_start (EV_A_ w); |
1678 | } |
1690 | } |
1679 | } |
1691 | } |
1680 | |
1692 | |
1681 | #if EV_PERIODIC_ENABLE |
1693 | #if EV_PERIODIC_ENABLE |
1682 | void |
1694 | void noinline |
1683 | ev_periodic_start (EV_P_ ev_periodic *w) |
1695 | ev_periodic_start (EV_P_ ev_periodic *w) |
1684 | { |
1696 | { |
1685 | if (expect_false (ev_is_active (w))) |
1697 | if (expect_false (ev_is_active (w))) |
1686 | return; |
1698 | return; |
1687 | |
1699 | |
… | |
… | |
1689 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1701 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1690 | else if (w->interval) |
1702 | else if (w->interval) |
1691 | { |
1703 | { |
1692 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1704 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1693 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1705 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1694 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1706 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1695 | } |
1707 | } |
|
|
1708 | else |
|
|
1709 | ((WT)w)->at = w->offset; |
1696 | |
1710 | |
1697 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1711 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1698 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1712 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1699 | periodics [periodiccnt - 1] = w; |
1713 | periodics [periodiccnt - 1] = w; |
1700 | upheap ((WT *)periodics, periodiccnt - 1); |
1714 | upheap ((WT *)periodics, periodiccnt - 1); |
1701 | |
1715 | |
1702 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1716 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1703 | } |
1717 | } |
1704 | |
1718 | |
1705 | void |
1719 | void noinline |
1706 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1720 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1707 | { |
1721 | { |
1708 | clear_pending (EV_A_ (W)w); |
1722 | clear_pending (EV_A_ (W)w); |
1709 | if (expect_false (!ev_is_active (w))) |
1723 | if (expect_false (!ev_is_active (w))) |
1710 | return; |
1724 | return; |
… | |
… | |
1722 | } |
1736 | } |
1723 | |
1737 | |
1724 | ev_stop (EV_A_ (W)w); |
1738 | ev_stop (EV_A_ (W)w); |
1725 | } |
1739 | } |
1726 | |
1740 | |
1727 | void |
1741 | void noinline |
1728 | ev_periodic_again (EV_P_ ev_periodic *w) |
1742 | ev_periodic_again (EV_P_ ev_periodic *w) |
1729 | { |
1743 | { |
1730 | /* TODO: use adjustheap and recalculation */ |
1744 | /* TODO: use adjustheap and recalculation */ |
1731 | ev_periodic_stop (EV_A_ w); |
1745 | ev_periodic_stop (EV_A_ w); |
1732 | ev_periodic_start (EV_A_ w); |
1746 | ev_periodic_start (EV_A_ w); |
… | |
… | |
1735 | |
1749 | |
1736 | #ifndef SA_RESTART |
1750 | #ifndef SA_RESTART |
1737 | # define SA_RESTART 0 |
1751 | # define SA_RESTART 0 |
1738 | #endif |
1752 | #endif |
1739 | |
1753 | |
1740 | void |
1754 | void noinline |
1741 | ev_signal_start (EV_P_ ev_signal *w) |
1755 | ev_signal_start (EV_P_ ev_signal *w) |
1742 | { |
1756 | { |
1743 | #if EV_MULTIPLICITY |
1757 | #if EV_MULTIPLICITY |
1744 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1758 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1745 | #endif |
1759 | #endif |
… | |
… | |
1764 | sigaction (w->signum, &sa, 0); |
1778 | sigaction (w->signum, &sa, 0); |
1765 | #endif |
1779 | #endif |
1766 | } |
1780 | } |
1767 | } |
1781 | } |
1768 | |
1782 | |
1769 | void |
1783 | void noinline |
1770 | ev_signal_stop (EV_P_ ev_signal *w) |
1784 | ev_signal_stop (EV_P_ ev_signal *w) |
1771 | { |
1785 | { |
1772 | clear_pending (EV_A_ (W)w); |
1786 | clear_pending (EV_A_ (W)w); |
1773 | if (expect_false (!ev_is_active (w))) |
1787 | if (expect_false (!ev_is_active (w))) |
1774 | return; |
1788 | return; |