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

Comparing libev/ev.c (file contents):
Revision 1.141 by root, Mon Nov 26 20:33:58 2007 UTC vs.
Revision 1.151 by root, Tue Nov 27 19:59:08 2007 UTC

156 156
157#ifndef EV_USE_PORT 157#ifndef EV_USE_PORT
158# define EV_USE_PORT 0 158# define EV_USE_PORT 0
159#endif 159#endif
160 160
161#ifndef EV_PID_HASHSIZE
162# if EV_MINIMAL
163# define EV_PID_HASHSIZE 1
164# else
165# define EV_PID_HASHSIZE 16
166# endif
167#endif
168
161/**/ 169/**/
162 170
163#ifndef CLOCK_MONOTONIC 171#ifndef CLOCK_MONOTONIC
164# undef EV_USE_MONOTONIC 172# undef EV_USE_MONOTONIC
165# define EV_USE_MONOTONIC 0 173# define EV_USE_MONOTONIC 0
176 184
177/**/ 185/**/
178 186
179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 187#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
180#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 188#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
181#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
182/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 189/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
183 190
184#ifdef EV_H 191#ifdef EV_H
185# include EV_H 192# include EV_H
186#else 193#else
198# define inline_speed static inline 205# define inline_speed static inline
199# endif 206# endif
200#else 207#else
201# define expect(expr,value) (expr) 208# define expect(expr,value) (expr)
202# define inline_speed static 209# define inline_speed static
203# define inline_minimal static 210# define inline_size static
204# define noinline 211# define noinline
205#endif 212#endif
206 213
207#define expect_false(expr) expect ((expr) != 0, 0) 214#define expect_false(expr) expect ((expr) != 0, 0)
208#define expect_true(expr) expect ((expr) != 0, 1) 215#define expect_true(expr) expect ((expr) != 0, 1)
246 perror (msg); 253 perror (msg);
247 abort (); 254 abort ();
248 } 255 }
249} 256}
250 257
251static void *(*alloc)(void *ptr, long size); 258static void *(*alloc)(void *ptr, size_t size) = realloc;
252 259
253void 260void
254ev_set_allocator (void *(*cb)(void *ptr, long size)) 261ev_set_allocator (void *(*cb)(void *ptr, size_t size))
255{ 262{
256 alloc = cb; 263 alloc = cb;
257} 264}
258 265
259static void * 266inline_speed void *
260ev_realloc (void *ptr, long size) 267ev_realloc (void *ptr, size_t size)
261{ 268{
262 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 269 ptr = alloc (ptr, size);
263 270
264 if (!ptr && size) 271 if (!ptr && size)
265 { 272 {
266 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 273 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size);
267 abort (); 274 abort ();
268 } 275 }
269 276
270 return ptr; 277 return ptr;
271} 278}
709 ev_unref (EV_A); /* child watcher should not keep loop alive */ 716 ev_unref (EV_A); /* child watcher should not keep loop alive */
710} 717}
711 718
712/*****************************************************************************/ 719/*****************************************************************************/
713 720
714static ev_child *childs [PID_HASHSIZE]; 721static ev_child *childs [EV_PID_HASHSIZE];
715 722
716#ifndef _WIN32 723#ifndef _WIN32
717 724
718static ev_signal childev; 725static ev_signal childev;
719
720#ifndef WCONTINUED
721# define WCONTINUED 0
722#endif
723 726
724void inline_speed 727void inline_speed
725child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 728child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
726{ 729{
727 ev_child *w; 730 ev_child *w;
728 731
729 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 732 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
730 if (w->pid == pid || !w->pid) 733 if (w->pid == pid || !w->pid)
731 { 734 {
732 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 735 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
733 w->rpid = pid; 736 w->rpid = pid;
734 w->rstatus = status; 737 w->rstatus = status;
735 ev_feed_event (EV_A_ (W)w, EV_CHILD); 738 ev_feed_event (EV_A_ (W)w, EV_CHILD);
736 } 739 }
737} 740}
738 741
742#ifndef WCONTINUED
743# define WCONTINUED 0
744#endif
745
739static void 746static void
740childcb (EV_P_ ev_signal *sw, int revents) 747childcb (EV_P_ ev_signal *sw, int revents)
741{ 748{
742 int pid, status; 749 int pid, status;
743 750
751 /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
744 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 752 if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
745 { 753 if (!WCONTINUED
754 || errno != EINVAL
755 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
756 return;
757
746 /* make sure we are called again until all childs have been reaped */ 758 /* make sure we are called again until all childs have been reaped */
747 /* we need to do it this way so that the callback gets called before we continue */ 759 /* we need to do it this way so that the callback gets called before we continue */
748 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 760 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
749 761
750 child_reap (EV_A_ sw, pid, pid, status); 762 child_reap (EV_A_ sw, pid, pid, status);
763 if (EV_PID_HASHSIZE > 1)
751 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 764 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
752 }
753} 765}
754 766
755#endif 767#endif
756 768
757/*****************************************************************************/ 769/*****************************************************************************/
840ev_backend (EV_P) 852ev_backend (EV_P)
841{ 853{
842 return backend; 854 return backend;
843} 855}
844 856
845static void 857static void noinline
846loop_init (EV_P_ unsigned int flags) 858loop_init (EV_P_ unsigned int flags)
847{ 859{
848 if (!backend) 860 if (!backend)
849 { 861 {
850#if EV_USE_MONOTONIC 862#if EV_USE_MONOTONIC
888 ev_init (&sigev, sigcb); 900 ev_init (&sigev, sigcb);
889 ev_set_priority (&sigev, EV_MAXPRI); 901 ev_set_priority (&sigev, EV_MAXPRI);
890 } 902 }
891} 903}
892 904
893static void 905static void noinline
894loop_destroy (EV_P) 906loop_destroy (EV_P)
895{ 907{
896 int i; 908 int i;
897 909
898#if EV_USE_PORT 910#if EV_USE_PORT
925 array_free (check, EMPTY0); 937 array_free (check, EMPTY0);
926 938
927 backend = 0; 939 backend = 0;
928} 940}
929 941
930static void 942void inline_size
931loop_fork (EV_P) 943loop_fork (EV_P)
932{ 944{
933#if EV_USE_PORT 945#if EV_USE_PORT
934 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 946 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
935#endif 947#endif
1085 { 1097 {
1086 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1098 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1087 1099
1088 if (expect_true (p->w)) 1100 if (expect_true (p->w))
1089 { 1101 {
1090 assert (("non-pending watcher on pending list", p->w->pending)); 1102 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1091 1103
1092 p->w->pending = 0; 1104 p->w->pending = 0;
1093 EV_CB_INVOKE (p->w, p->events); 1105 EV_CB_INVOKE (p->w, p->events);
1094 } 1106 }
1095 } 1107 }
1100{ 1112{
1101 while (timercnt && ((WT)timers [0])->at <= mn_now) 1113 while (timercnt && ((WT)timers [0])->at <= mn_now)
1102 { 1114 {
1103 ev_timer *w = timers [0]; 1115 ev_timer *w = timers [0];
1104 1116
1105 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1117 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1106 1118
1107 /* first reschedule or stop timer */ 1119 /* first reschedule or stop timer */
1108 if (w->repeat) 1120 if (w->repeat)
1109 { 1121 {
1110 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1122 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1128{ 1140{
1129 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1141 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1130 { 1142 {
1131 ev_periodic *w = periodics [0]; 1143 ev_periodic *w = periodics [0];
1132 1144
1133 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1145 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1134 1146
1135 /* first reschedule or stop timer */ 1147 /* first reschedule or stop timer */
1136 if (w->reschedule_cb) 1148 if (w->reschedule_cb)
1137 { 1149 {
1138 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1150 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1272 ? EVUNLOOP_ONE 1284 ? EVUNLOOP_ONE
1273 : EVUNLOOP_CANCEL; 1285 : EVUNLOOP_CANCEL;
1274 1286
1275 while (activecnt) 1287 while (activecnt)
1276 { 1288 {
1289 /* we might have forked, so reify kernel state if necessary */
1290 #if EV_FORK_ENABLE
1291 if (expect_false (postfork))
1292 if (forkcnt)
1293 {
1294 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1295 call_pending (EV_A);
1296 }
1297 #endif
1298
1277 /* queue check watchers (and execute them) */ 1299 /* queue check watchers (and execute them) */
1278 if (expect_false (preparecnt)) 1300 if (expect_false (preparecnt))
1279 { 1301 {
1280 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1302 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1281 call_pending (EV_A); 1303 call_pending (EV_A);
1460 ev_start (EV_A_ (W)w, ++timercnt); 1482 ev_start (EV_A_ (W)w, ++timercnt);
1461 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); 1483 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1462 timers [timercnt - 1] = w; 1484 timers [timercnt - 1] = w;
1463 upheap ((WT *)timers, timercnt - 1); 1485 upheap ((WT *)timers, timercnt - 1);
1464 1486
1465 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1487 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1466} 1488}
1467 1489
1468void 1490void
1469ev_timer_stop (EV_P_ ev_timer *w) 1491ev_timer_stop (EV_P_ ev_timer *w)
1470{ 1492{
1472 if (expect_false (!ev_is_active (w))) 1494 if (expect_false (!ev_is_active (w)))
1473 return; 1495 return;
1474 1496
1475 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1497 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1476 1498
1499 {
1500 int active = ((W)w)->active;
1501
1477 if (expect_true (((W)w)->active < timercnt--)) 1502 if (expect_true (--active < --timercnt))
1478 { 1503 {
1479 timers [((W)w)->active - 1] = timers [timercnt]; 1504 timers [active] = timers [timercnt];
1480 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1505 adjustheap ((WT *)timers, timercnt, active);
1481 } 1506 }
1507 }
1482 1508
1483 ((WT)w)->at -= mn_now; 1509 ((WT)w)->at -= mn_now;
1484 1510
1485 ev_stop (EV_A_ (W)w); 1511 ev_stop (EV_A_ (W)w);
1486} 1512}
1524 ev_start (EV_A_ (W)w, ++periodiccnt); 1550 ev_start (EV_A_ (W)w, ++periodiccnt);
1525 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1551 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1526 periodics [periodiccnt - 1] = w; 1552 periodics [periodiccnt - 1] = w;
1527 upheap ((WT *)periodics, periodiccnt - 1); 1553 upheap ((WT *)periodics, periodiccnt - 1);
1528 1554
1529 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1555 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1530} 1556}
1531 1557
1532void 1558void
1533ev_periodic_stop (EV_P_ ev_periodic *w) 1559ev_periodic_stop (EV_P_ ev_periodic *w)
1534{ 1560{
1536 if (expect_false (!ev_is_active (w))) 1562 if (expect_false (!ev_is_active (w)))
1537 return; 1563 return;
1538 1564
1539 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1565 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1540 1566
1567 {
1568 int active = ((W)w)->active;
1569
1541 if (expect_true (((W)w)->active < periodiccnt--)) 1570 if (expect_true (--active < --periodiccnt))
1542 { 1571 {
1543 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1572 periodics [active] = periodics [periodiccnt];
1544 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1573 adjustheap ((WT *)periodics, periodiccnt, active);
1545 } 1574 }
1575 }
1546 1576
1547 ev_stop (EV_A_ (W)w); 1577 ev_stop (EV_A_ (W)w);
1548} 1578}
1549 1579
1550void 1580void
1553 /* TODO: use adjustheap and recalculation */ 1583 /* TODO: use adjustheap and recalculation */
1554 ev_periodic_stop (EV_A_ w); 1584 ev_periodic_stop (EV_A_ w);
1555 ev_periodic_start (EV_A_ w); 1585 ev_periodic_start (EV_A_ w);
1556} 1586}
1557#endif 1587#endif
1558
1559void
1560ev_idle_start (EV_P_ ev_idle *w)
1561{
1562 if (expect_false (ev_is_active (w)))
1563 return;
1564
1565 ev_start (EV_A_ (W)w, ++idlecnt);
1566 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1567 idles [idlecnt - 1] = w;
1568}
1569
1570void
1571ev_idle_stop (EV_P_ ev_idle *w)
1572{
1573 ev_clear_pending (EV_A_ (W)w);
1574 if (expect_false (!ev_is_active (w)))
1575 return;
1576
1577 {
1578 int active = ((W)w)->active;
1579 idles [active - 1] = idles [--idlecnt];
1580 ((W)idles [active - 1])->active = active;
1581 }
1582
1583 ev_stop (EV_A_ (W)w);
1584}
1585
1586void
1587ev_prepare_start (EV_P_ ev_prepare *w)
1588{
1589 if (expect_false (ev_is_active (w)))
1590 return;
1591
1592 ev_start (EV_A_ (W)w, ++preparecnt);
1593 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1594 prepares [preparecnt - 1] = w;
1595}
1596
1597void
1598ev_prepare_stop (EV_P_ ev_prepare *w)
1599{
1600 ev_clear_pending (EV_A_ (W)w);
1601 if (expect_false (!ev_is_active (w)))
1602 return;
1603
1604 {
1605 int active = ((W)w)->active;
1606 prepares [active - 1] = prepares [--preparecnt];
1607 ((W)prepares [active - 1])->active = active;
1608 }
1609
1610 ev_stop (EV_A_ (W)w);
1611}
1612
1613void
1614ev_check_start (EV_P_ ev_check *w)
1615{
1616 if (expect_false (ev_is_active (w)))
1617 return;
1618
1619 ev_start (EV_A_ (W)w, ++checkcnt);
1620 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1621 checks [checkcnt - 1] = w;
1622}
1623
1624void
1625ev_check_stop (EV_P_ ev_check *w)
1626{
1627 ev_clear_pending (EV_A_ (W)w);
1628 if (expect_false (!ev_is_active (w)))
1629 return;
1630
1631 {
1632 int active = ((W)w)->active;
1633 checks [active - 1] = checks [--checkcnt];
1634 ((W)checks [active - 1])->active = active;
1635 }
1636
1637 ev_stop (EV_A_ (W)w);
1638}
1639 1588
1640#ifndef SA_RESTART 1589#ifndef SA_RESTART
1641# define SA_RESTART 0 1590# define SA_RESTART 0
1642#endif 1591#endif
1643 1592
1692#endif 1641#endif
1693 if (expect_false (ev_is_active (w))) 1642 if (expect_false (ev_is_active (w)))
1694 return; 1643 return;
1695 1644
1696 ev_start (EV_A_ (W)w, 1); 1645 ev_start (EV_A_ (W)w, 1);
1697 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1646 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1698} 1647}
1699 1648
1700void 1649void
1701ev_child_stop (EV_P_ ev_child *w) 1650ev_child_stop (EV_P_ ev_child *w)
1702{ 1651{
1703 ev_clear_pending (EV_A_ (W)w); 1652 ev_clear_pending (EV_A_ (W)w);
1704 if (expect_false (!ev_is_active (w))) 1653 if (expect_false (!ev_is_active (w)))
1705 return; 1654 return;
1706 1655
1707 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1656 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1657 ev_stop (EV_A_ (W)w);
1658}
1659
1660#if EV_STAT_ENABLE
1661
1662# ifdef _WIN32
1663# undef lstat
1664# define lstat(a,b) _stati64 (a,b)
1665# endif
1666
1667#define DEF_STAT_INTERVAL 5.0074891
1668#define MIN_STAT_INTERVAL 0.1074891
1669
1670void
1671ev_stat_stat (EV_P_ ev_stat *w)
1672{
1673 if (lstat (w->path, &w->attr) < 0)
1674 w->attr.st_nlink = 0;
1675 else if (!w->attr.st_nlink)
1676 w->attr.st_nlink = 1;
1677}
1678
1679static void
1680stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1681{
1682 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1683
1684 /* we copy this here each the time so that */
1685 /* prev has the old value when the callback gets invoked */
1686 w->prev = w->attr;
1687 ev_stat_stat (EV_A_ w);
1688
1689 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1690 ev_feed_event (EV_A_ w, EV_STAT);
1691}
1692
1693void
1694ev_stat_start (EV_P_ ev_stat *w)
1695{
1696 if (expect_false (ev_is_active (w)))
1697 return;
1698
1699 /* since we use memcmp, we need to clear any padding data etc. */
1700 memset (&w->prev, 0, sizeof (ev_statdata));
1701 memset (&w->attr, 0, sizeof (ev_statdata));
1702
1703 ev_stat_stat (EV_A_ w);
1704
1705 if (w->interval < MIN_STAT_INTERVAL)
1706 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1707
1708 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1709 ev_set_priority (&w->timer, ev_priority (w));
1710 ev_timer_start (EV_A_ &w->timer);
1711
1712 ev_start (EV_A_ (W)w, 1);
1713}
1714
1715void
1716ev_stat_stop (EV_P_ ev_stat *w)
1717{
1718 ev_clear_pending (EV_A_ (W)w);
1719 if (expect_false (!ev_is_active (w)))
1720 return;
1721
1722 ev_timer_stop (EV_A_ &w->timer);
1723
1724 ev_stop (EV_A_ (W)w);
1725}
1726#endif
1727
1728void
1729ev_idle_start (EV_P_ ev_idle *w)
1730{
1731 if (expect_false (ev_is_active (w)))
1732 return;
1733
1734 ev_start (EV_A_ (W)w, ++idlecnt);
1735 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1736 idles [idlecnt - 1] = w;
1737}
1738
1739void
1740ev_idle_stop (EV_P_ ev_idle *w)
1741{
1742 ev_clear_pending (EV_A_ (W)w);
1743 if (expect_false (!ev_is_active (w)))
1744 return;
1745
1746 {
1747 int active = ((W)w)->active;
1748 idles [active - 1] = idles [--idlecnt];
1749 ((W)idles [active - 1])->active = active;
1750 }
1751
1752 ev_stop (EV_A_ (W)w);
1753}
1754
1755void
1756ev_prepare_start (EV_P_ ev_prepare *w)
1757{
1758 if (expect_false (ev_is_active (w)))
1759 return;
1760
1761 ev_start (EV_A_ (W)w, ++preparecnt);
1762 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1763 prepares [preparecnt - 1] = w;
1764}
1765
1766void
1767ev_prepare_stop (EV_P_ ev_prepare *w)
1768{
1769 ev_clear_pending (EV_A_ (W)w);
1770 if (expect_false (!ev_is_active (w)))
1771 return;
1772
1773 {
1774 int active = ((W)w)->active;
1775 prepares [active - 1] = prepares [--preparecnt];
1776 ((W)prepares [active - 1])->active = active;
1777 }
1778
1779 ev_stop (EV_A_ (W)w);
1780}
1781
1782void
1783ev_check_start (EV_P_ ev_check *w)
1784{
1785 if (expect_false (ev_is_active (w)))
1786 return;
1787
1788 ev_start (EV_A_ (W)w, ++checkcnt);
1789 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1790 checks [checkcnt - 1] = w;
1791}
1792
1793void
1794ev_check_stop (EV_P_ ev_check *w)
1795{
1796 ev_clear_pending (EV_A_ (W)w);
1797 if (expect_false (!ev_is_active (w)))
1798 return;
1799
1800 {
1801 int active = ((W)w)->active;
1802 checks [active - 1] = checks [--checkcnt];
1803 ((W)checks [active - 1])->active = active;
1804 }
1805
1708 ev_stop (EV_A_ (W)w); 1806 ev_stop (EV_A_ (W)w);
1709} 1807}
1710 1808
1711#if EV_EMBED_ENABLE 1809#if EV_EMBED_ENABLE
1712void noinline 1810void noinline
1755 1853
1756 ev_stop (EV_A_ (W)w); 1854 ev_stop (EV_A_ (W)w);
1757} 1855}
1758#endif 1856#endif
1759 1857
1760#if EV_STAT_ENABLE 1858#if EV_FORK_ENABLE
1761
1762# ifdef _WIN32
1763# define lstat(a,b) stat(a,b)
1764# endif
1765
1766void 1859void
1767ev_stat_stat (EV_P_ ev_stat *w)
1768{
1769 if (lstat (w->path, &w->attr) < 0)
1770 w->attr.st_nlink = 0;
1771 else if (!w->attr.st_nlink)
1772 w->attr.st_nlink = 1;
1773}
1774
1775static void
1776stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1777{
1778 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1779
1780 /* we copy this here each the time so that */
1781 /* prev has the old value when the callback gets invoked */
1782 w->prev = w->attr;
1783 ev_stat_stat (EV_A_ w);
1784
1785 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1786 ev_feed_event (EV_A_ w, EV_STAT);
1787}
1788
1789void
1790ev_stat_start (EV_P_ ev_stat *w) 1860ev_fork_start (EV_P_ ev_fork *w)
1791{ 1861{
1792 if (expect_false (ev_is_active (w))) 1862 if (expect_false (ev_is_active (w)))
1793 return; 1863 return;
1794 1864
1795 /* since we use memcmp, we need to clear any padding data etc. */
1796 memset (&w->prev, 0, sizeof (ev_statdata));
1797 memset (&w->attr, 0, sizeof (ev_statdata));
1798
1799 ev_stat_stat (EV_A_ w);
1800
1801 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1802 ev_set_priority (&w->timer, ev_priority (w));
1803 ev_timer_start (EV_A_ &w->timer);
1804
1805 ev_start (EV_A_ (W)w, 1); 1865 ev_start (EV_A_ (W)w, ++forkcnt);
1866 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
1867 forks [forkcnt - 1] = w;
1806} 1868}
1807 1869
1808void 1870void
1809ev_stat_stop (EV_P_ ev_stat *w) 1871ev_fork_stop (EV_P_ ev_fork *w)
1810{ 1872{
1811 ev_clear_pending (EV_A_ (W)w); 1873 ev_clear_pending (EV_A_ (W)w);
1812 if (expect_false (!ev_is_active (w))) 1874 if (expect_false (!ev_is_active (w)))
1813 return; 1875 return;
1814 1876
1815 ev_timer_stop (EV_A_ &w->timer); 1877 {
1878 int active = ((W)w)->active;
1879 forks [active - 1] = forks [--forkcnt];
1880 ((W)forks [active - 1])->active = active;
1881 }
1816 1882
1817 ev_stop (EV_A_ (W)w); 1883 ev_stop (EV_A_ (W)w);
1818} 1884}
1819#endif 1885#endif
1820 1886

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines