… | |
… | |
202 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
202 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
203 | |
203 | |
204 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
204 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
205 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
205 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
206 | |
206 | |
207 | typedef struct ev_watcher *W; |
207 | typedef ev_watcher *W; |
208 | typedef struct ev_watcher_list *WL; |
208 | typedef ev_watcher_list *WL; |
209 | typedef struct ev_watcher_time *WT; |
209 | typedef ev_watcher_time *WT; |
210 | |
210 | |
211 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
211 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
212 | |
212 | |
213 | #ifdef _WIN32 |
213 | #ifdef _WIN32 |
214 | # include "ev_win32.c" |
214 | # include "ev_win32.c" |
… | |
… | |
397 | { |
397 | { |
398 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
398 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
399 | return; |
399 | return; |
400 | } |
400 | } |
401 | |
401 | |
402 | if (expect_false (!w_->cb)) |
|
|
403 | return; |
|
|
404 | |
|
|
405 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
402 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
406 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
403 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
407 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
404 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
408 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
405 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
409 | } |
406 | } |
… | |
… | |
419 | |
416 | |
420 | inline void |
417 | inline void |
421 | fd_event (EV_P_ int fd, int revents) |
418 | fd_event (EV_P_ int fd, int revents) |
422 | { |
419 | { |
423 | ANFD *anfd = anfds + fd; |
420 | ANFD *anfd = anfds + fd; |
424 | struct ev_io *w; |
421 | ev_io *w; |
425 | |
422 | |
426 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
423 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
427 | { |
424 | { |
428 | int ev = w->events & revents; |
425 | int ev = w->events & revents; |
429 | |
426 | |
430 | if (ev) |
427 | if (ev) |
431 | ev_feed_event (EV_A_ (W)w, ev); |
428 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
447 | |
444 | |
448 | for (i = 0; i < fdchangecnt; ++i) |
445 | for (i = 0; i < fdchangecnt; ++i) |
449 | { |
446 | { |
450 | int fd = fdchanges [i]; |
447 | int fd = fdchanges [i]; |
451 | ANFD *anfd = anfds + fd; |
448 | ANFD *anfd = anfds + fd; |
452 | struct ev_io *w; |
449 | ev_io *w; |
453 | |
450 | |
454 | int events = 0; |
451 | int events = 0; |
455 | |
452 | |
456 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
453 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
457 | events |= w->events; |
454 | events |= w->events; |
458 | |
455 | |
459 | #if EV_SELECT_IS_WINSOCKET |
456 | #if EV_SELECT_IS_WINSOCKET |
460 | if (events) |
457 | if (events) |
461 | { |
458 | { |
… | |
… | |
488 | } |
485 | } |
489 | |
486 | |
490 | static void |
487 | static void |
491 | fd_kill (EV_P_ int fd) |
488 | fd_kill (EV_P_ int fd) |
492 | { |
489 | { |
493 | struct ev_io *w; |
490 | ev_io *w; |
494 | |
491 | |
495 | while ((w = (struct ev_io *)anfds [fd].head)) |
492 | while ((w = (ev_io *)anfds [fd].head)) |
496 | { |
493 | { |
497 | ev_io_stop (EV_A_ w); |
494 | ev_io_stop (EV_A_ w); |
498 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
495 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
499 | } |
496 | } |
500 | } |
497 | } |
… | |
… | |
611 | static ANSIG *signals; |
608 | static ANSIG *signals; |
612 | static int signalmax; |
609 | static int signalmax; |
613 | |
610 | |
614 | static int sigpipe [2]; |
611 | static int sigpipe [2]; |
615 | static sig_atomic_t volatile gotsig; |
612 | static sig_atomic_t volatile gotsig; |
616 | static struct ev_io sigev; |
613 | static ev_io sigev; |
617 | |
614 | |
618 | static void |
615 | static void |
619 | signals_init (ANSIG *base, int count) |
616 | signals_init (ANSIG *base, int count) |
620 | { |
617 | { |
621 | while (count--) |
618 | while (count--) |
… | |
… | |
664 | for (w = signals [signum].head; w; w = w->next) |
661 | for (w = signals [signum].head; w; w = w->next) |
665 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
662 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
666 | } |
663 | } |
667 | |
664 | |
668 | static void |
665 | static void |
669 | sigcb (EV_P_ struct ev_io *iow, int revents) |
666 | sigcb (EV_P_ ev_io *iow, int revents) |
670 | { |
667 | { |
671 | int signum; |
668 | int signum; |
672 | |
669 | |
673 | read (sigpipe [0], &revents, 1); |
670 | read (sigpipe [0], &revents, 1); |
674 | gotsig = 0; |
671 | gotsig = 0; |
… | |
… | |
701 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
698 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
702 | } |
699 | } |
703 | |
700 | |
704 | /*****************************************************************************/ |
701 | /*****************************************************************************/ |
705 | |
702 | |
706 | static struct ev_child *childs [PID_HASHSIZE]; |
703 | static ev_child *childs [PID_HASHSIZE]; |
707 | |
704 | |
708 | #ifndef _WIN32 |
705 | #ifndef _WIN32 |
709 | |
706 | |
710 | static struct ev_signal childev; |
707 | static ev_signal childev; |
711 | |
708 | |
712 | #ifndef WCONTINUED |
709 | #ifndef WCONTINUED |
713 | # define WCONTINUED 0 |
710 | # define WCONTINUED 0 |
714 | #endif |
711 | #endif |
715 | |
712 | |
716 | static void |
713 | static void |
717 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
714 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
718 | { |
715 | { |
719 | struct ev_child *w; |
716 | ev_child *w; |
720 | |
717 | |
721 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
718 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
722 | if (w->pid == pid || !w->pid) |
719 | if (w->pid == pid || !w->pid) |
723 | { |
720 | { |
724 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
721 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
725 | w->rpid = pid; |
722 | w->rpid = pid; |
726 | w->rstatus = status; |
723 | w->rstatus = status; |
727 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
724 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
728 | } |
725 | } |
729 | } |
726 | } |
730 | |
727 | |
731 | static void |
728 | static void |
732 | childcb (EV_P_ struct ev_signal *sw, int revents) |
729 | childcb (EV_P_ ev_signal *sw, int revents) |
733 | { |
730 | { |
734 | int pid, status; |
731 | int pid, status; |
735 | |
732 | |
736 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
733 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
737 | { |
734 | { |
… | |
… | |
1077 | { |
1074 | { |
1078 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1075 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1079 | |
1076 | |
1080 | if (expect_true (p->w)) |
1077 | if (expect_true (p->w)) |
1081 | { |
1078 | { |
|
|
1079 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1080 | |
1082 | p->w->pending = 0; |
1081 | p->w->pending = 0; |
1083 | EV_CB_INVOKE (p->w, p->events); |
1082 | EV_CB_INVOKE (p->w, p->events); |
1084 | } |
1083 | } |
1085 | } |
1084 | } |
1086 | } |
1085 | } |
… | |
… | |
1088 | inline void |
1087 | inline void |
1089 | timers_reify (EV_P) |
1088 | timers_reify (EV_P) |
1090 | { |
1089 | { |
1091 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1090 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1092 | { |
1091 | { |
1093 | struct ev_timer *w = timers [0]; |
1092 | ev_timer *w = timers [0]; |
1094 | |
1093 | |
1095 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1094 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1096 | |
1095 | |
1097 | /* first reschedule or stop timer */ |
1096 | /* first reschedule or stop timer */ |
1098 | if (w->repeat) |
1097 | if (w->repeat) |
… | |
… | |
1116 | inline void |
1115 | inline void |
1117 | periodics_reify (EV_P) |
1116 | periodics_reify (EV_P) |
1118 | { |
1117 | { |
1119 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1118 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1120 | { |
1119 | { |
1121 | struct ev_periodic *w = periodics [0]; |
1120 | ev_periodic *w = periodics [0]; |
1122 | |
1121 | |
1123 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1122 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1124 | |
1123 | |
1125 | /* first reschedule or stop timer */ |
1124 | /* first reschedule or stop timer */ |
1126 | if (w->reschedule_cb) |
1125 | if (w->reschedule_cb) |
… | |
… | |
1148 | int i; |
1147 | int i; |
1149 | |
1148 | |
1150 | /* adjust periodics after time jump */ |
1149 | /* adjust periodics after time jump */ |
1151 | for (i = 0; i < periodiccnt; ++i) |
1150 | for (i = 0; i < periodiccnt; ++i) |
1152 | { |
1151 | { |
1153 | struct ev_periodic *w = periodics [i]; |
1152 | ev_periodic *w = periodics [i]; |
1154 | |
1153 | |
1155 | if (w->reschedule_cb) |
1154 | if (w->reschedule_cb) |
1156 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1155 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1157 | else if (w->interval) |
1156 | else if (w->interval) |
1158 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1157 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
… | |
… | |
1192 | { |
1191 | { |
1193 | if (time_update_monotonic (EV_A)) |
1192 | if (time_update_monotonic (EV_A)) |
1194 | { |
1193 | { |
1195 | ev_tstamp odiff = rtmn_diff; |
1194 | ev_tstamp odiff = rtmn_diff; |
1196 | |
1195 | |
1197 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1196 | /* loop a few times, before making important decisions. |
|
|
1197 | * on the choice of "4": one iteration isn't enough, |
|
|
1198 | * in case we get preempted during the calls to |
|
|
1199 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1200 | * to succeed in that case, though. and looping a few more times |
|
|
1201 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1202 | * in the unlikely event of getting preempted here. |
|
|
1203 | */ |
|
|
1204 | for (i = 4; --i; ) |
1198 | { |
1205 | { |
1199 | rtmn_diff = ev_rt_now - mn_now; |
1206 | rtmn_diff = ev_rt_now - mn_now; |
1200 | |
1207 | |
1201 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1208 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1202 | return; /* all is well */ |
1209 | return; /* all is well */ |
… | |
… | |
1318 | timers_reify (EV_A); /* relative timers called last */ |
1325 | timers_reify (EV_A); /* relative timers called last */ |
1319 | #if EV_PERIODICS |
1326 | #if EV_PERIODICS |
1320 | periodics_reify (EV_A); /* absolute timers called first */ |
1327 | periodics_reify (EV_A); /* absolute timers called first */ |
1321 | #endif |
1328 | #endif |
1322 | |
1329 | |
1323 | /* queue idle watchers unless io or timers are pending */ |
1330 | /* queue idle watchers unless other events are pending */ |
1324 | if (idlecnt && !any_pending (EV_A)) |
1331 | if (idlecnt && !any_pending (EV_A)) |
1325 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1332 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1326 | |
1333 | |
1327 | /* queue check watchers, to be executed first */ |
1334 | /* queue check watchers, to be executed first */ |
1328 | if (expect_false (checkcnt)) |
1335 | if (expect_false (checkcnt)) |
… | |
… | |
1396 | } |
1403 | } |
1397 | |
1404 | |
1398 | /*****************************************************************************/ |
1405 | /*****************************************************************************/ |
1399 | |
1406 | |
1400 | void |
1407 | void |
1401 | ev_io_start (EV_P_ struct ev_io *w) |
1408 | ev_io_start (EV_P_ ev_io *w) |
1402 | { |
1409 | { |
1403 | int fd = w->fd; |
1410 | int fd = w->fd; |
1404 | |
1411 | |
1405 | if (expect_false (ev_is_active (w))) |
1412 | if (expect_false (ev_is_active (w))) |
1406 | return; |
1413 | return; |
… | |
… | |
1413 | |
1420 | |
1414 | fd_change (EV_A_ fd); |
1421 | fd_change (EV_A_ fd); |
1415 | } |
1422 | } |
1416 | |
1423 | |
1417 | void |
1424 | void |
1418 | ev_io_stop (EV_P_ struct ev_io *w) |
1425 | ev_io_stop (EV_P_ ev_io *w) |
1419 | { |
1426 | { |
1420 | ev_clear_pending (EV_A_ (W)w); |
1427 | ev_clear_pending (EV_A_ (W)w); |
1421 | if (expect_false (!ev_is_active (w))) |
1428 | if (expect_false (!ev_is_active (w))) |
1422 | return; |
1429 | return; |
1423 | |
1430 | |
… | |
… | |
1428 | |
1435 | |
1429 | fd_change (EV_A_ w->fd); |
1436 | fd_change (EV_A_ w->fd); |
1430 | } |
1437 | } |
1431 | |
1438 | |
1432 | void |
1439 | void |
1433 | ev_timer_start (EV_P_ struct ev_timer *w) |
1440 | ev_timer_start (EV_P_ ev_timer *w) |
1434 | { |
1441 | { |
1435 | if (expect_false (ev_is_active (w))) |
1442 | if (expect_false (ev_is_active (w))) |
1436 | return; |
1443 | return; |
1437 | |
1444 | |
1438 | ((WT)w)->at += mn_now; |
1445 | ((WT)w)->at += mn_now; |
1439 | |
1446 | |
1440 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1447 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1441 | |
1448 | |
1442 | ev_start (EV_A_ (W)w, ++timercnt); |
1449 | ev_start (EV_A_ (W)w, ++timercnt); |
1443 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1450 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1444 | timers [timercnt - 1] = w; |
1451 | timers [timercnt - 1] = w; |
1445 | upheap ((WT *)timers, timercnt - 1); |
1452 | upheap ((WT *)timers, timercnt - 1); |
1446 | |
1453 | |
1447 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1454 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1448 | } |
1455 | } |
1449 | |
1456 | |
1450 | void |
1457 | void |
1451 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1458 | ev_timer_stop (EV_P_ ev_timer *w) |
1452 | { |
1459 | { |
1453 | ev_clear_pending (EV_A_ (W)w); |
1460 | ev_clear_pending (EV_A_ (W)w); |
1454 | if (expect_false (!ev_is_active (w))) |
1461 | if (expect_false (!ev_is_active (w))) |
1455 | return; |
1462 | return; |
1456 | |
1463 | |
… | |
… | |
1466 | |
1473 | |
1467 | ev_stop (EV_A_ (W)w); |
1474 | ev_stop (EV_A_ (W)w); |
1468 | } |
1475 | } |
1469 | |
1476 | |
1470 | void |
1477 | void |
1471 | ev_timer_again (EV_P_ struct ev_timer *w) |
1478 | ev_timer_again (EV_P_ ev_timer *w) |
1472 | { |
1479 | { |
1473 | if (ev_is_active (w)) |
1480 | if (ev_is_active (w)) |
1474 | { |
1481 | { |
1475 | if (w->repeat) |
1482 | if (w->repeat) |
1476 | { |
1483 | { |
… | |
… | |
1487 | } |
1494 | } |
1488 | } |
1495 | } |
1489 | |
1496 | |
1490 | #if EV_PERIODICS |
1497 | #if EV_PERIODICS |
1491 | void |
1498 | void |
1492 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1499 | ev_periodic_start (EV_P_ ev_periodic *w) |
1493 | { |
1500 | { |
1494 | if (expect_false (ev_is_active (w))) |
1501 | if (expect_false (ev_is_active (w))) |
1495 | return; |
1502 | return; |
1496 | |
1503 | |
1497 | if (w->reschedule_cb) |
1504 | if (w->reschedule_cb) |
… | |
… | |
1502 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1509 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1503 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1510 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1504 | } |
1511 | } |
1505 | |
1512 | |
1506 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1513 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1507 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1514 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1508 | periodics [periodiccnt - 1] = w; |
1515 | periodics [periodiccnt - 1] = w; |
1509 | upheap ((WT *)periodics, periodiccnt - 1); |
1516 | upheap ((WT *)periodics, periodiccnt - 1); |
1510 | |
1517 | |
1511 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1518 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1512 | } |
1519 | } |
1513 | |
1520 | |
1514 | void |
1521 | void |
1515 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1522 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1516 | { |
1523 | { |
1517 | ev_clear_pending (EV_A_ (W)w); |
1524 | ev_clear_pending (EV_A_ (W)w); |
1518 | if (expect_false (!ev_is_active (w))) |
1525 | if (expect_false (!ev_is_active (w))) |
1519 | return; |
1526 | return; |
1520 | |
1527 | |
… | |
… | |
1528 | |
1535 | |
1529 | ev_stop (EV_A_ (W)w); |
1536 | ev_stop (EV_A_ (W)w); |
1530 | } |
1537 | } |
1531 | |
1538 | |
1532 | void |
1539 | void |
1533 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1540 | ev_periodic_again (EV_P_ ev_periodic *w) |
1534 | { |
1541 | { |
1535 | /* TODO: use adjustheap and recalculation */ |
1542 | /* TODO: use adjustheap and recalculation */ |
1536 | ev_periodic_stop (EV_A_ w); |
1543 | ev_periodic_stop (EV_A_ w); |
1537 | ev_periodic_start (EV_A_ w); |
1544 | ev_periodic_start (EV_A_ w); |
1538 | } |
1545 | } |
1539 | #endif |
1546 | #endif |
1540 | |
1547 | |
1541 | void |
1548 | void |
1542 | ev_idle_start (EV_P_ struct ev_idle *w) |
1549 | ev_idle_start (EV_P_ ev_idle *w) |
1543 | { |
1550 | { |
1544 | if (expect_false (ev_is_active (w))) |
1551 | if (expect_false (ev_is_active (w))) |
1545 | return; |
1552 | return; |
1546 | |
1553 | |
1547 | ev_start (EV_A_ (W)w, ++idlecnt); |
1554 | ev_start (EV_A_ (W)w, ++idlecnt); |
1548 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1555 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1549 | idles [idlecnt - 1] = w; |
1556 | idles [idlecnt - 1] = w; |
1550 | } |
1557 | } |
1551 | |
1558 | |
1552 | void |
1559 | void |
1553 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1560 | ev_idle_stop (EV_P_ ev_idle *w) |
1554 | { |
1561 | { |
1555 | ev_clear_pending (EV_A_ (W)w); |
1562 | ev_clear_pending (EV_A_ (W)w); |
1556 | if (expect_false (!ev_is_active (w))) |
1563 | if (expect_false (!ev_is_active (w))) |
1557 | return; |
1564 | return; |
1558 | |
1565 | |
|
|
1566 | { |
|
|
1567 | int active = ((W)w)->active; |
1559 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1568 | idles [active - 1] = idles [--idlecnt]; |
|
|
1569 | ((W)idles [active - 1])->active = active; |
|
|
1570 | } |
|
|
1571 | |
1560 | ev_stop (EV_A_ (W)w); |
1572 | ev_stop (EV_A_ (W)w); |
1561 | } |
1573 | } |
1562 | |
1574 | |
1563 | void |
1575 | void |
1564 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1576 | ev_prepare_start (EV_P_ ev_prepare *w) |
1565 | { |
1577 | { |
1566 | if (expect_false (ev_is_active (w))) |
1578 | if (expect_false (ev_is_active (w))) |
1567 | return; |
1579 | return; |
1568 | |
1580 | |
1569 | ev_start (EV_A_ (W)w, ++preparecnt); |
1581 | ev_start (EV_A_ (W)w, ++preparecnt); |
1570 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1582 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1571 | prepares [preparecnt - 1] = w; |
1583 | prepares [preparecnt - 1] = w; |
1572 | } |
1584 | } |
1573 | |
1585 | |
1574 | void |
1586 | void |
1575 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1587 | ev_prepare_stop (EV_P_ ev_prepare *w) |
1576 | { |
1588 | { |
1577 | ev_clear_pending (EV_A_ (W)w); |
1589 | ev_clear_pending (EV_A_ (W)w); |
1578 | if (expect_false (!ev_is_active (w))) |
1590 | if (expect_false (!ev_is_active (w))) |
1579 | return; |
1591 | return; |
1580 | |
1592 | |
|
|
1593 | { |
|
|
1594 | int active = ((W)w)->active; |
1581 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1595 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1596 | ((W)prepares [active - 1])->active = active; |
|
|
1597 | } |
|
|
1598 | |
1582 | ev_stop (EV_A_ (W)w); |
1599 | ev_stop (EV_A_ (W)w); |
1583 | } |
1600 | } |
1584 | |
1601 | |
1585 | void |
1602 | void |
1586 | ev_check_start (EV_P_ struct ev_check *w) |
1603 | ev_check_start (EV_P_ ev_check *w) |
1587 | { |
1604 | { |
1588 | if (expect_false (ev_is_active (w))) |
1605 | if (expect_false (ev_is_active (w))) |
1589 | return; |
1606 | return; |
1590 | |
1607 | |
1591 | ev_start (EV_A_ (W)w, ++checkcnt); |
1608 | ev_start (EV_A_ (W)w, ++checkcnt); |
1592 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1609 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1593 | checks [checkcnt - 1] = w; |
1610 | checks [checkcnt - 1] = w; |
1594 | } |
1611 | } |
1595 | |
1612 | |
1596 | void |
1613 | void |
1597 | ev_check_stop (EV_P_ struct ev_check *w) |
1614 | ev_check_stop (EV_P_ ev_check *w) |
1598 | { |
1615 | { |
1599 | ev_clear_pending (EV_A_ (W)w); |
1616 | ev_clear_pending (EV_A_ (W)w); |
1600 | if (expect_false (!ev_is_active (w))) |
1617 | if (expect_false (!ev_is_active (w))) |
1601 | return; |
1618 | return; |
1602 | |
1619 | |
|
|
1620 | { |
|
|
1621 | int active = ((W)w)->active; |
1603 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1622 | checks [active - 1] = checks [--checkcnt]; |
|
|
1623 | ((W)checks [active - 1])->active = active; |
|
|
1624 | } |
|
|
1625 | |
1604 | ev_stop (EV_A_ (W)w); |
1626 | ev_stop (EV_A_ (W)w); |
1605 | } |
1627 | } |
1606 | |
1628 | |
1607 | #ifndef SA_RESTART |
1629 | #ifndef SA_RESTART |
1608 | # define SA_RESTART 0 |
1630 | # define SA_RESTART 0 |
1609 | #endif |
1631 | #endif |
1610 | |
1632 | |
1611 | void |
1633 | void |
1612 | ev_signal_start (EV_P_ struct ev_signal *w) |
1634 | ev_signal_start (EV_P_ ev_signal *w) |
1613 | { |
1635 | { |
1614 | #if EV_MULTIPLICITY |
1636 | #if EV_MULTIPLICITY |
1615 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1637 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1616 | #endif |
1638 | #endif |
1617 | if (expect_false (ev_is_active (w))) |
1639 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1636 | #endif |
1658 | #endif |
1637 | } |
1659 | } |
1638 | } |
1660 | } |
1639 | |
1661 | |
1640 | void |
1662 | void |
1641 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1663 | ev_signal_stop (EV_P_ ev_signal *w) |
1642 | { |
1664 | { |
1643 | ev_clear_pending (EV_A_ (W)w); |
1665 | ev_clear_pending (EV_A_ (W)w); |
1644 | if (expect_false (!ev_is_active (w))) |
1666 | if (expect_false (!ev_is_active (w))) |
1645 | return; |
1667 | return; |
1646 | |
1668 | |
… | |
… | |
1650 | if (!signals [w->signum - 1].head) |
1672 | if (!signals [w->signum - 1].head) |
1651 | signal (w->signum, SIG_DFL); |
1673 | signal (w->signum, SIG_DFL); |
1652 | } |
1674 | } |
1653 | |
1675 | |
1654 | void |
1676 | void |
1655 | ev_child_start (EV_P_ struct ev_child *w) |
1677 | ev_child_start (EV_P_ ev_child *w) |
1656 | { |
1678 | { |
1657 | #if EV_MULTIPLICITY |
1679 | #if EV_MULTIPLICITY |
1658 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1680 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1659 | #endif |
1681 | #endif |
1660 | if (expect_false (ev_is_active (w))) |
1682 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1663 | ev_start (EV_A_ (W)w, 1); |
1685 | ev_start (EV_A_ (W)w, 1); |
1664 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1686 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1665 | } |
1687 | } |
1666 | |
1688 | |
1667 | void |
1689 | void |
1668 | ev_child_stop (EV_P_ struct ev_child *w) |
1690 | ev_child_stop (EV_P_ ev_child *w) |
1669 | { |
1691 | { |
1670 | ev_clear_pending (EV_A_ (W)w); |
1692 | ev_clear_pending (EV_A_ (W)w); |
1671 | if (expect_false (!ev_is_active (w))) |
1693 | if (expect_false (!ev_is_active (w))) |
1672 | return; |
1694 | return; |
1673 | |
1695 | |
1674 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1696 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1675 | ev_stop (EV_A_ (W)w); |
1697 | ev_stop (EV_A_ (W)w); |
1676 | } |
1698 | } |
1677 | |
1699 | |
1678 | #if EV_MULTIPLICITY |
1700 | #if EV_MULTIPLICITY |
1679 | static void |
1701 | void |
1680 | embed_cb (EV_P_ struct ev_io *io, int revents) |
1702 | ev_embed_sweep (EV_P_ ev_embed *w) |
1681 | { |
1703 | { |
1682 | struct ev_embed *w = (struct ev_embed *)(((char *)io) - offsetof (struct ev_embed, io)); |
|
|
1683 | |
|
|
1684 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1685 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
1704 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
1686 | } |
1705 | } |
1687 | |
1706 | |
|
|
1707 | static void |
|
|
1708 | embed_cb (EV_P_ ev_io *io, int revents) |
|
|
1709 | { |
|
|
1710 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
|
|
1711 | |
|
|
1712 | if (ev_cb (w)) |
|
|
1713 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1714 | else |
|
|
1715 | ev_embed_sweep (loop, w); |
|
|
1716 | } |
|
|
1717 | |
1688 | void |
1718 | void |
1689 | ev_embed_start (EV_P_ struct ev_embed *w) |
1719 | ev_embed_start (EV_P_ ev_embed *w) |
1690 | { |
1720 | { |
1691 | if (expect_false (ev_is_active (w))) |
1721 | if (expect_false (ev_is_active (w))) |
1692 | return; |
1722 | return; |
1693 | |
1723 | |
1694 | { |
1724 | { |
1695 | struct ev_loop *loop = w->loop; |
1725 | struct ev_loop *loop = w->loop; |
1696 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
1726 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
1697 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
1727 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
1698 | } |
1728 | } |
1699 | |
1729 | |
|
|
1730 | ev_set_priority (&w->io, ev_priority (w)); |
1700 | ev_io_start (EV_A_ &w->io); |
1731 | ev_io_start (EV_A_ &w->io); |
1701 | ev_start (EV_A_ (W)w, 1); |
1732 | ev_start (EV_A_ (W)w, 1); |
1702 | } |
1733 | } |
1703 | |
1734 | |
1704 | void |
1735 | void |
1705 | ev_embed_stop (EV_P_ struct ev_embed *w) |
1736 | ev_embed_stop (EV_P_ ev_embed *w) |
1706 | { |
1737 | { |
1707 | ev_clear_pending (EV_A_ (W)w); |
1738 | ev_clear_pending (EV_A_ (W)w); |
1708 | if (expect_false (!ev_is_active (w))) |
1739 | if (expect_false (!ev_is_active (w))) |
1709 | return; |
1740 | return; |
1710 | |
1741 | |
… | |
… | |
1715 | |
1746 | |
1716 | /*****************************************************************************/ |
1747 | /*****************************************************************************/ |
1717 | |
1748 | |
1718 | struct ev_once |
1749 | struct ev_once |
1719 | { |
1750 | { |
1720 | struct ev_io io; |
1751 | ev_io io; |
1721 | struct ev_timer to; |
1752 | ev_timer to; |
1722 | void (*cb)(int revents, void *arg); |
1753 | void (*cb)(int revents, void *arg); |
1723 | void *arg; |
1754 | void *arg; |
1724 | }; |
1755 | }; |
1725 | |
1756 | |
1726 | static void |
1757 | static void |
… | |
… | |
1735 | |
1766 | |
1736 | cb (revents, arg); |
1767 | cb (revents, arg); |
1737 | } |
1768 | } |
1738 | |
1769 | |
1739 | static void |
1770 | static void |
1740 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1771 | once_cb_io (EV_P_ ev_io *w, int revents) |
1741 | { |
1772 | { |
1742 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1773 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1743 | } |
1774 | } |
1744 | |
1775 | |
1745 | static void |
1776 | static void |
1746 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1777 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1747 | { |
1778 | { |
1748 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1779 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1749 | } |
1780 | } |
1750 | |
1781 | |
1751 | void |
1782 | void |