… | |
… | |
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 | |
251 | static void *(*alloc)(void *ptr, long size); |
258 | static void *(*alloc)(void *ptr, size_t size) = realloc; |
252 | |
259 | |
253 | void |
260 | void |
254 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
261 | ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
255 | { |
262 | { |
256 | alloc = cb; |
263 | alloc = cb; |
257 | } |
264 | } |
258 | |
265 | |
259 | static void * |
266 | inline_speed void * |
260 | ev_realloc (void *ptr, long size) |
267 | ev_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 | |
714 | static ev_child *childs [PID_HASHSIZE]; |
721 | static ev_child *childs [EV_PID_HASHSIZE]; |
715 | |
722 | |
716 | #ifndef _WIN32 |
723 | #ifndef _WIN32 |
717 | |
724 | |
718 | static ev_signal childev; |
725 | static ev_signal childev; |
719 | |
726 | |
720 | void inline_speed |
727 | void inline_speed |
721 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
728 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
722 | { |
729 | { |
723 | ev_child *w; |
730 | ev_child *w; |
724 | |
731 | |
725 | 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) |
726 | if (w->pid == pid || !w->pid) |
733 | if (w->pid == pid || !w->pid) |
727 | { |
734 | { |
728 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
735 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
729 | w->rpid = pid; |
736 | w->rpid = pid; |
730 | w->rstatus = status; |
737 | w->rstatus = status; |
… | |
… | |
751 | /* 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 */ |
752 | /* 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 */ |
753 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
760 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
754 | |
761 | |
755 | child_reap (EV_A_ sw, pid, pid, status); |
762 | child_reap (EV_A_ sw, pid, pid, status); |
|
|
763 | if (EV_PID_HASHSIZE > 1) |
756 | 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 */ |
757 | } |
765 | } |
758 | |
766 | |
759 | #endif |
767 | #endif |
760 | |
768 | |
761 | /*****************************************************************************/ |
769 | /*****************************************************************************/ |
… | |
… | |
844 | ev_backend (EV_P) |
852 | ev_backend (EV_P) |
845 | { |
853 | { |
846 | return backend; |
854 | return backend; |
847 | } |
855 | } |
848 | |
856 | |
849 | static void |
857 | static void noinline |
850 | loop_init (EV_P_ unsigned int flags) |
858 | loop_init (EV_P_ unsigned int flags) |
851 | { |
859 | { |
852 | if (!backend) |
860 | if (!backend) |
853 | { |
861 | { |
854 | #if EV_USE_MONOTONIC |
862 | #if EV_USE_MONOTONIC |
… | |
… | |
892 | ev_init (&sigev, sigcb); |
900 | ev_init (&sigev, sigcb); |
893 | ev_set_priority (&sigev, EV_MAXPRI); |
901 | ev_set_priority (&sigev, EV_MAXPRI); |
894 | } |
902 | } |
895 | } |
903 | } |
896 | |
904 | |
897 | static void |
905 | static void noinline |
898 | loop_destroy (EV_P) |
906 | loop_destroy (EV_P) |
899 | { |
907 | { |
900 | int i; |
908 | int i; |
901 | |
909 | |
902 | #if EV_USE_PORT |
910 | #if EV_USE_PORT |
… | |
… | |
929 | array_free (check, EMPTY0); |
937 | array_free (check, EMPTY0); |
930 | |
938 | |
931 | backend = 0; |
939 | backend = 0; |
932 | } |
940 | } |
933 | |
941 | |
934 | static void |
942 | void inline_size |
935 | loop_fork (EV_P) |
943 | loop_fork (EV_P) |
936 | { |
944 | { |
937 | #if EV_USE_PORT |
945 | #if EV_USE_PORT |
938 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
946 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
939 | #endif |
947 | #endif |
… | |
… | |
1089 | { |
1097 | { |
1090 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1098 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1091 | |
1099 | |
1092 | if (expect_true (p->w)) |
1100 | if (expect_true (p->w)) |
1093 | { |
1101 | { |
1094 | assert (("non-pending watcher on pending list", p->w->pending)); |
1102 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1095 | |
1103 | |
1096 | p->w->pending = 0; |
1104 | p->w->pending = 0; |
1097 | EV_CB_INVOKE (p->w, p->events); |
1105 | EV_CB_INVOKE (p->w, p->events); |
1098 | } |
1106 | } |
1099 | } |
1107 | } |
… | |
… | |
1104 | { |
1112 | { |
1105 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1113 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1106 | { |
1114 | { |
1107 | ev_timer *w = timers [0]; |
1115 | ev_timer *w = timers [0]; |
1108 | |
1116 | |
1109 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1117 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
1110 | |
1118 | |
1111 | /* first reschedule or stop timer */ |
1119 | /* first reschedule or stop timer */ |
1112 | if (w->repeat) |
1120 | if (w->repeat) |
1113 | { |
1121 | { |
1114 | 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.)); |
… | |
… | |
1132 | { |
1140 | { |
1133 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1141 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1134 | { |
1142 | { |
1135 | ev_periodic *w = periodics [0]; |
1143 | ev_periodic *w = periodics [0]; |
1136 | |
1144 | |
1137 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1145 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1138 | |
1146 | |
1139 | /* first reschedule or stop timer */ |
1147 | /* first reschedule or stop timer */ |
1140 | if (w->reschedule_cb) |
1148 | if (w->reschedule_cb) |
1141 | { |
1149 | { |
1142 | ((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); |
… | |
… | |
1276 | ? EVUNLOOP_ONE |
1284 | ? EVUNLOOP_ONE |
1277 | : EVUNLOOP_CANCEL; |
1285 | : EVUNLOOP_CANCEL; |
1278 | |
1286 | |
1279 | while (activecnt) |
1287 | while (activecnt) |
1280 | { |
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 | |
1281 | /* queue check watchers (and execute them) */ |
1299 | /* queue check watchers (and execute them) */ |
1282 | if (expect_false (preparecnt)) |
1300 | if (expect_false (preparecnt)) |
1283 | { |
1301 | { |
1284 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1302 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1285 | call_pending (EV_A); |
1303 | call_pending (EV_A); |
… | |
… | |
1464 | ev_start (EV_A_ (W)w, ++timercnt); |
1482 | ev_start (EV_A_ (W)w, ++timercnt); |
1465 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1483 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1466 | timers [timercnt - 1] = w; |
1484 | timers [timercnt - 1] = w; |
1467 | upheap ((WT *)timers, timercnt - 1); |
1485 | upheap ((WT *)timers, timercnt - 1); |
1468 | |
1486 | |
1469 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1487 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1470 | } |
1488 | } |
1471 | |
1489 | |
1472 | void |
1490 | void |
1473 | ev_timer_stop (EV_P_ ev_timer *w) |
1491 | ev_timer_stop (EV_P_ ev_timer *w) |
1474 | { |
1492 | { |
… | |
… | |
1476 | if (expect_false (!ev_is_active (w))) |
1494 | if (expect_false (!ev_is_active (w))) |
1477 | return; |
1495 | return; |
1478 | |
1496 | |
1479 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1497 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1480 | |
1498 | |
|
|
1499 | { |
|
|
1500 | int active = ((W)w)->active; |
|
|
1501 | |
1481 | if (expect_true (((W)w)->active < timercnt--)) |
1502 | if (expect_true (--active < --timercnt)) |
1482 | { |
1503 | { |
1483 | timers [((W)w)->active - 1] = timers [timercnt]; |
1504 | timers [active] = timers [timercnt]; |
1484 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1505 | adjustheap ((WT *)timers, timercnt, active); |
1485 | } |
1506 | } |
|
|
1507 | } |
1486 | |
1508 | |
1487 | ((WT)w)->at -= mn_now; |
1509 | ((WT)w)->at -= mn_now; |
1488 | |
1510 | |
1489 | ev_stop (EV_A_ (W)w); |
1511 | ev_stop (EV_A_ (W)w); |
1490 | } |
1512 | } |
… | |
… | |
1528 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1550 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1529 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1551 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1530 | periodics [periodiccnt - 1] = w; |
1552 | periodics [periodiccnt - 1] = w; |
1531 | upheap ((WT *)periodics, periodiccnt - 1); |
1553 | upheap ((WT *)periodics, periodiccnt - 1); |
1532 | |
1554 | |
1533 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1555 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1534 | } |
1556 | } |
1535 | |
1557 | |
1536 | void |
1558 | void |
1537 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1559 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1538 | { |
1560 | { |
… | |
… | |
1540 | if (expect_false (!ev_is_active (w))) |
1562 | if (expect_false (!ev_is_active (w))) |
1541 | return; |
1563 | return; |
1542 | |
1564 | |
1543 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1565 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1544 | |
1566 | |
|
|
1567 | { |
|
|
1568 | int active = ((W)w)->active; |
|
|
1569 | |
1545 | if (expect_true (((W)w)->active < periodiccnt--)) |
1570 | if (expect_true (--active < --periodiccnt)) |
1546 | { |
1571 | { |
1547 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1572 | periodics [active] = periodics [periodiccnt]; |
1548 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1573 | adjustheap ((WT *)periodics, periodiccnt, active); |
1549 | } |
1574 | } |
|
|
1575 | } |
1550 | |
1576 | |
1551 | ev_stop (EV_A_ (W)w); |
1577 | ev_stop (EV_A_ (W)w); |
1552 | } |
1578 | } |
1553 | |
1579 | |
1554 | void |
1580 | void |
… | |
… | |
1557 | /* TODO: use adjustheap and recalculation */ |
1583 | /* TODO: use adjustheap and recalculation */ |
1558 | ev_periodic_stop (EV_A_ w); |
1584 | ev_periodic_stop (EV_A_ w); |
1559 | ev_periodic_start (EV_A_ w); |
1585 | ev_periodic_start (EV_A_ w); |
1560 | } |
1586 | } |
1561 | #endif |
1587 | #endif |
1562 | |
|
|
1563 | void |
|
|
1564 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1565 | { |
|
|
1566 | if (expect_false (ev_is_active (w))) |
|
|
1567 | return; |
|
|
1568 | |
|
|
1569 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1570 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1571 | idles [idlecnt - 1] = w; |
|
|
1572 | } |
|
|
1573 | |
|
|
1574 | void |
|
|
1575 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1576 | { |
|
|
1577 | ev_clear_pending (EV_A_ (W)w); |
|
|
1578 | if (expect_false (!ev_is_active (w))) |
|
|
1579 | return; |
|
|
1580 | |
|
|
1581 | { |
|
|
1582 | int active = ((W)w)->active; |
|
|
1583 | idles [active - 1] = idles [--idlecnt]; |
|
|
1584 | ((W)idles [active - 1])->active = active; |
|
|
1585 | } |
|
|
1586 | |
|
|
1587 | ev_stop (EV_A_ (W)w); |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | void |
|
|
1591 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1592 | { |
|
|
1593 | if (expect_false (ev_is_active (w))) |
|
|
1594 | return; |
|
|
1595 | |
|
|
1596 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1597 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1598 | prepares [preparecnt - 1] = w; |
|
|
1599 | } |
|
|
1600 | |
|
|
1601 | void |
|
|
1602 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1603 | { |
|
|
1604 | ev_clear_pending (EV_A_ (W)w); |
|
|
1605 | if (expect_false (!ev_is_active (w))) |
|
|
1606 | return; |
|
|
1607 | |
|
|
1608 | { |
|
|
1609 | int active = ((W)w)->active; |
|
|
1610 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1611 | ((W)prepares [active - 1])->active = active; |
|
|
1612 | } |
|
|
1613 | |
|
|
1614 | ev_stop (EV_A_ (W)w); |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | void |
|
|
1618 | ev_check_start (EV_P_ ev_check *w) |
|
|
1619 | { |
|
|
1620 | if (expect_false (ev_is_active (w))) |
|
|
1621 | return; |
|
|
1622 | |
|
|
1623 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1624 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1625 | checks [checkcnt - 1] = w; |
|
|
1626 | } |
|
|
1627 | |
|
|
1628 | void |
|
|
1629 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1630 | { |
|
|
1631 | ev_clear_pending (EV_A_ (W)w); |
|
|
1632 | if (expect_false (!ev_is_active (w))) |
|
|
1633 | return; |
|
|
1634 | |
|
|
1635 | { |
|
|
1636 | int active = ((W)w)->active; |
|
|
1637 | checks [active - 1] = checks [--checkcnt]; |
|
|
1638 | ((W)checks [active - 1])->active = active; |
|
|
1639 | } |
|
|
1640 | |
|
|
1641 | ev_stop (EV_A_ (W)w); |
|
|
1642 | } |
|
|
1643 | |
1588 | |
1644 | #ifndef SA_RESTART |
1589 | #ifndef SA_RESTART |
1645 | # define SA_RESTART 0 |
1590 | # define SA_RESTART 0 |
1646 | #endif |
1591 | #endif |
1647 | |
1592 | |
… | |
… | |
1696 | #endif |
1641 | #endif |
1697 | if (expect_false (ev_is_active (w))) |
1642 | if (expect_false (ev_is_active (w))) |
1698 | return; |
1643 | return; |
1699 | |
1644 | |
1700 | ev_start (EV_A_ (W)w, 1); |
1645 | ev_start (EV_A_ (W)w, 1); |
1701 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1646 | wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1702 | } |
1647 | } |
1703 | |
1648 | |
1704 | void |
1649 | void |
1705 | ev_child_stop (EV_P_ ev_child *w) |
1650 | ev_child_stop (EV_P_ ev_child *w) |
1706 | { |
1651 | { |
1707 | ev_clear_pending (EV_A_ (W)w); |
1652 | ev_clear_pending (EV_A_ (W)w); |
1708 | if (expect_false (!ev_is_active (w))) |
1653 | if (expect_false (!ev_is_active (w))) |
1709 | return; |
1654 | return; |
1710 | |
1655 | |
1711 | 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 | |
|
|
1670 | void |
|
|
1671 | ev_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 | |
|
|
1679 | static void |
|
|
1680 | stat_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 | |
|
|
1693 | void |
|
|
1694 | ev_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 | |
|
|
1715 | void |
|
|
1716 | ev_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 | |
|
|
1728 | void |
|
|
1729 | ev_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 | |
|
|
1739 | void |
|
|
1740 | ev_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 | |
|
|
1755 | void |
|
|
1756 | ev_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 | |
|
|
1766 | void |
|
|
1767 | ev_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 | |
|
|
1782 | void |
|
|
1783 | ev_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 | |
|
|
1793 | void |
|
|
1794 | ev_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 | |
1712 | ev_stop (EV_A_ (W)w); |
1806 | ev_stop (EV_A_ (W)w); |
1713 | } |
1807 | } |
1714 | |
1808 | |
1715 | #if EV_EMBED_ENABLE |
1809 | #if EV_EMBED_ENABLE |
1716 | void noinline |
1810 | void noinline |
… | |
… | |
1759 | |
1853 | |
1760 | ev_stop (EV_A_ (W)w); |
1854 | ev_stop (EV_A_ (W)w); |
1761 | } |
1855 | } |
1762 | #endif |
1856 | #endif |
1763 | |
1857 | |
1764 | #if EV_STAT_ENABLE |
1858 | #if EV_FORK_ENABLE |
1765 | |
|
|
1766 | # ifdef _WIN32 |
|
|
1767 | # define lstat(a,b) stat(a,b) |
|
|
1768 | # endif |
|
|
1769 | |
|
|
1770 | void |
1859 | void |
1771 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1772 | { |
|
|
1773 | if (lstat (w->path, &w->attr) < 0) |
|
|
1774 | w->attr.st_nlink = 0; |
|
|
1775 | else if (!w->attr.st_nlink) |
|
|
1776 | w->attr.st_nlink = 1; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | static void |
|
|
1780 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1781 | { |
|
|
1782 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1783 | |
|
|
1784 | /* we copy this here each the time so that */ |
|
|
1785 | /* prev has the old value when the callback gets invoked */ |
|
|
1786 | w->prev = w->attr; |
|
|
1787 | ev_stat_stat (EV_A_ w); |
|
|
1788 | |
|
|
1789 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1790 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | void |
|
|
1794 | ev_stat_start (EV_P_ ev_stat *w) |
1860 | ev_fork_start (EV_P_ ev_fork *w) |
1795 | { |
1861 | { |
1796 | if (expect_false (ev_is_active (w))) |
1862 | if (expect_false (ev_is_active (w))) |
1797 | return; |
1863 | return; |
1798 | |
1864 | |
1799 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1800 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1801 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1802 | |
|
|
1803 | ev_stat_stat (EV_A_ w); |
|
|
1804 | |
|
|
1805 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1806 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1807 | ev_timer_start (EV_A_ &w->timer); |
|
|
1808 | |
|
|
1809 | 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; |
1810 | } |
1868 | } |
1811 | |
1869 | |
1812 | void |
1870 | void |
1813 | ev_stat_stop (EV_P_ ev_stat *w) |
1871 | ev_fork_stop (EV_P_ ev_fork *w) |
1814 | { |
1872 | { |
1815 | ev_clear_pending (EV_A_ (W)w); |
1873 | ev_clear_pending (EV_A_ (W)w); |
1816 | if (expect_false (!ev_is_active (w))) |
1874 | if (expect_false (!ev_is_active (w))) |
1817 | return; |
1875 | return; |
1818 | |
1876 | |
1819 | 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 | } |
1820 | |
1882 | |
1821 | ev_stop (EV_A_ (W)w); |
1883 | ev_stop (EV_A_ (W)w); |
1822 | } |
1884 | } |
1823 | #endif |
1885 | #endif |
1824 | |
1886 | |