… | |
… | |
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" |
… | |
… | |
416 | |
416 | |
417 | inline void |
417 | inline void |
418 | fd_event (EV_P_ int fd, int revents) |
418 | fd_event (EV_P_ int fd, int revents) |
419 | { |
419 | { |
420 | ANFD *anfd = anfds + fd; |
420 | ANFD *anfd = anfds + fd; |
421 | struct ev_io *w; |
421 | ev_io *w; |
422 | |
422 | |
423 | 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) |
424 | { |
424 | { |
425 | int ev = w->events & revents; |
425 | int ev = w->events & revents; |
426 | |
426 | |
427 | if (ev) |
427 | if (ev) |
428 | ev_feed_event (EV_A_ (W)w, ev); |
428 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
444 | |
444 | |
445 | for (i = 0; i < fdchangecnt; ++i) |
445 | for (i = 0; i < fdchangecnt; ++i) |
446 | { |
446 | { |
447 | int fd = fdchanges [i]; |
447 | int fd = fdchanges [i]; |
448 | ANFD *anfd = anfds + fd; |
448 | ANFD *anfd = anfds + fd; |
449 | struct ev_io *w; |
449 | ev_io *w; |
450 | |
450 | |
451 | int events = 0; |
451 | int events = 0; |
452 | |
452 | |
453 | 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) |
454 | events |= w->events; |
454 | events |= w->events; |
455 | |
455 | |
456 | #if EV_SELECT_IS_WINSOCKET |
456 | #if EV_SELECT_IS_WINSOCKET |
457 | if (events) |
457 | if (events) |
458 | { |
458 | { |
… | |
… | |
485 | } |
485 | } |
486 | |
486 | |
487 | static void |
487 | static void |
488 | fd_kill (EV_P_ int fd) |
488 | fd_kill (EV_P_ int fd) |
489 | { |
489 | { |
490 | struct ev_io *w; |
490 | ev_io *w; |
491 | |
491 | |
492 | while ((w = (struct ev_io *)anfds [fd].head)) |
492 | while ((w = (ev_io *)anfds [fd].head)) |
493 | { |
493 | { |
494 | ev_io_stop (EV_A_ w); |
494 | ev_io_stop (EV_A_ w); |
495 | 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); |
496 | } |
496 | } |
497 | } |
497 | } |
… | |
… | |
608 | static ANSIG *signals; |
608 | static ANSIG *signals; |
609 | static int signalmax; |
609 | static int signalmax; |
610 | |
610 | |
611 | static int sigpipe [2]; |
611 | static int sigpipe [2]; |
612 | static sig_atomic_t volatile gotsig; |
612 | static sig_atomic_t volatile gotsig; |
613 | static struct ev_io sigev; |
613 | static ev_io sigev; |
614 | |
614 | |
615 | static void |
615 | static void |
616 | signals_init (ANSIG *base, int count) |
616 | signals_init (ANSIG *base, int count) |
617 | { |
617 | { |
618 | while (count--) |
618 | while (count--) |
… | |
… | |
661 | for (w = signals [signum].head; w; w = w->next) |
661 | for (w = signals [signum].head; w; w = w->next) |
662 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
662 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
663 | } |
663 | } |
664 | |
664 | |
665 | static void |
665 | static void |
666 | sigcb (EV_P_ struct ev_io *iow, int revents) |
666 | sigcb (EV_P_ ev_io *iow, int revents) |
667 | { |
667 | { |
668 | int signum; |
668 | int signum; |
669 | |
669 | |
670 | read (sigpipe [0], &revents, 1); |
670 | read (sigpipe [0], &revents, 1); |
671 | gotsig = 0; |
671 | gotsig = 0; |
… | |
… | |
698 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
698 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
699 | } |
699 | } |
700 | |
700 | |
701 | /*****************************************************************************/ |
701 | /*****************************************************************************/ |
702 | |
702 | |
703 | static struct ev_child *childs [PID_HASHSIZE]; |
703 | static ev_child *childs [PID_HASHSIZE]; |
704 | |
704 | |
705 | #ifndef _WIN32 |
705 | #ifndef _WIN32 |
706 | |
706 | |
707 | static struct ev_signal childev; |
707 | static ev_signal childev; |
708 | |
708 | |
709 | #ifndef WCONTINUED |
709 | #ifndef WCONTINUED |
710 | # define WCONTINUED 0 |
710 | # define WCONTINUED 0 |
711 | #endif |
711 | #endif |
712 | |
712 | |
713 | static void |
713 | static void |
714 | 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) |
715 | { |
715 | { |
716 | struct ev_child *w; |
716 | ev_child *w; |
717 | |
717 | |
718 | 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) |
719 | if (w->pid == pid || !w->pid) |
719 | if (w->pid == pid || !w->pid) |
720 | { |
720 | { |
721 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
721 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
722 | w->rpid = pid; |
722 | w->rpid = pid; |
723 | w->rstatus = status; |
723 | w->rstatus = status; |
724 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
724 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
725 | } |
725 | } |
726 | } |
726 | } |
727 | |
727 | |
728 | static void |
728 | static void |
729 | childcb (EV_P_ struct ev_signal *sw, int revents) |
729 | childcb (EV_P_ ev_signal *sw, int revents) |
730 | { |
730 | { |
731 | int pid, status; |
731 | int pid, status; |
732 | |
732 | |
733 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
733 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
734 | { |
734 | { |
… | |
… | |
816 | |
816 | |
817 | return flags; |
817 | return flags; |
818 | } |
818 | } |
819 | |
819 | |
820 | unsigned int |
820 | unsigned int |
|
|
821 | ev_embeddable_backends (void) |
|
|
822 | { |
|
|
823 | return EVBACKEND_EPOLL |
|
|
824 | | EVBACKEND_KQUEUE |
|
|
825 | | EVBACKEND_PORT; |
|
|
826 | } |
|
|
827 | |
|
|
828 | unsigned int |
821 | ev_backend (EV_P) |
829 | ev_backend (EV_P) |
822 | { |
830 | { |
823 | return backend; |
831 | return backend; |
824 | } |
832 | } |
825 | |
833 | |
… | |
… | |
1066 | { |
1074 | { |
1067 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1075 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1068 | |
1076 | |
1069 | if (expect_true (p->w)) |
1077 | if (expect_true (p->w)) |
1070 | { |
1078 | { |
|
|
1079 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1080 | |
1071 | p->w->pending = 0; |
1081 | p->w->pending = 0; |
1072 | EV_CB_INVOKE (p->w, p->events); |
1082 | EV_CB_INVOKE (p->w, p->events); |
1073 | } |
1083 | } |
1074 | } |
1084 | } |
1075 | } |
1085 | } |
… | |
… | |
1077 | inline void |
1087 | inline void |
1078 | timers_reify (EV_P) |
1088 | timers_reify (EV_P) |
1079 | { |
1089 | { |
1080 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1090 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1081 | { |
1091 | { |
1082 | struct ev_timer *w = timers [0]; |
1092 | ev_timer *w = timers [0]; |
1083 | |
1093 | |
1084 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1094 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1085 | |
1095 | |
1086 | /* first reschedule or stop timer */ |
1096 | /* first reschedule or stop timer */ |
1087 | if (w->repeat) |
1097 | if (w->repeat) |
… | |
… | |
1105 | inline void |
1115 | inline void |
1106 | periodics_reify (EV_P) |
1116 | periodics_reify (EV_P) |
1107 | { |
1117 | { |
1108 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1118 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1109 | { |
1119 | { |
1110 | struct ev_periodic *w = periodics [0]; |
1120 | ev_periodic *w = periodics [0]; |
1111 | |
1121 | |
1112 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1122 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1113 | |
1123 | |
1114 | /* first reschedule or stop timer */ |
1124 | /* first reschedule or stop timer */ |
1115 | if (w->reschedule_cb) |
1125 | if (w->reschedule_cb) |
… | |
… | |
1137 | int i; |
1147 | int i; |
1138 | |
1148 | |
1139 | /* adjust periodics after time jump */ |
1149 | /* adjust periodics after time jump */ |
1140 | for (i = 0; i < periodiccnt; ++i) |
1150 | for (i = 0; i < periodiccnt; ++i) |
1141 | { |
1151 | { |
1142 | struct ev_periodic *w = periodics [i]; |
1152 | ev_periodic *w = periodics [i]; |
1143 | |
1153 | |
1144 | if (w->reschedule_cb) |
1154 | if (w->reschedule_cb) |
1145 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1155 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1146 | else if (w->interval) |
1156 | else if (w->interval) |
1147 | ((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; |
… | |
… | |
1181 | { |
1191 | { |
1182 | if (time_update_monotonic (EV_A)) |
1192 | if (time_update_monotonic (EV_A)) |
1183 | { |
1193 | { |
1184 | ev_tstamp odiff = rtmn_diff; |
1194 | ev_tstamp odiff = rtmn_diff; |
1185 | |
1195 | |
1186 | 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; ) |
1187 | { |
1205 | { |
1188 | rtmn_diff = ev_rt_now - mn_now; |
1206 | rtmn_diff = ev_rt_now - mn_now; |
1189 | |
1207 | |
1190 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1208 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1191 | return; /* all is well */ |
1209 | return; /* all is well */ |
… | |
… | |
1237 | static int loop_done; |
1255 | static int loop_done; |
1238 | |
1256 | |
1239 | void |
1257 | void |
1240 | ev_loop (EV_P_ int flags) |
1258 | ev_loop (EV_P_ int flags) |
1241 | { |
1259 | { |
1242 | double block; |
|
|
1243 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1260 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1261 | ? EVUNLOOP_ONE |
|
|
1262 | : EVUNLOOP_CANCEL; |
1244 | |
1263 | |
1245 | while (activecnt) |
1264 | while (activecnt) |
1246 | { |
1265 | { |
1247 | /* queue check watchers (and execute them) */ |
1266 | /* queue check watchers (and execute them) */ |
1248 | if (expect_false (preparecnt)) |
1267 | if (expect_false (preparecnt)) |
… | |
… | |
1257 | |
1276 | |
1258 | /* update fd-related kernel structures */ |
1277 | /* update fd-related kernel structures */ |
1259 | fd_reify (EV_A); |
1278 | fd_reify (EV_A); |
1260 | |
1279 | |
1261 | /* calculate blocking time */ |
1280 | /* calculate blocking time */ |
|
|
1281 | { |
|
|
1282 | double block; |
1262 | |
1283 | |
1263 | /* we only need this for !monotonic clock or timers, but as we basically |
1284 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1264 | always have timers, we just calculate it always */ |
1285 | block = 0.; /* do not block at all */ |
|
|
1286 | else |
|
|
1287 | { |
|
|
1288 | /* update time to cancel out callback processing overhead */ |
1265 | #if EV_USE_MONOTONIC |
1289 | #if EV_USE_MONOTONIC |
1266 | if (expect_true (have_monotonic)) |
1290 | if (expect_true (have_monotonic)) |
1267 | time_update_monotonic (EV_A); |
1291 | time_update_monotonic (EV_A); |
1268 | else |
1292 | else |
1269 | #endif |
1293 | #endif |
1270 | { |
1294 | { |
1271 | ev_rt_now = ev_time (); |
1295 | ev_rt_now = ev_time (); |
1272 | mn_now = ev_rt_now; |
1296 | mn_now = ev_rt_now; |
1273 | } |
1297 | } |
1274 | |
1298 | |
1275 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
1276 | block = 0.; |
|
|
1277 | else |
|
|
1278 | { |
|
|
1279 | block = MAX_BLOCKTIME; |
1299 | block = MAX_BLOCKTIME; |
1280 | |
1300 | |
1281 | if (timercnt) |
1301 | if (timercnt) |
1282 | { |
1302 | { |
1283 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1303 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1284 | if (block > to) block = to; |
1304 | if (block > to) block = to; |
1285 | } |
1305 | } |
1286 | |
1306 | |
1287 | #if EV_PERIODICS |
1307 | #if EV_PERIODICS |
1288 | if (periodiccnt) |
1308 | if (periodiccnt) |
1289 | { |
1309 | { |
1290 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1310 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1291 | if (block > to) block = to; |
1311 | if (block > to) block = to; |
1292 | } |
1312 | } |
1293 | #endif |
1313 | #endif |
1294 | |
1314 | |
1295 | if (expect_false (block < 0.)) block = 0.; |
1315 | if (expect_false (block < 0.)) block = 0.; |
1296 | } |
1316 | } |
1297 | |
1317 | |
1298 | backend_poll (EV_A_ block); |
1318 | backend_poll (EV_A_ block); |
|
|
1319 | } |
1299 | |
1320 | |
1300 | /* update ev_rt_now, do magic */ |
1321 | /* update ev_rt_now, do magic */ |
1301 | time_update (EV_A); |
1322 | time_update (EV_A); |
1302 | |
1323 | |
1303 | /* queue pending timers and reschedule them */ |
1324 | /* queue pending timers and reschedule them */ |
1304 | timers_reify (EV_A); /* relative timers called last */ |
1325 | timers_reify (EV_A); /* relative timers called last */ |
1305 | #if EV_PERIODICS |
1326 | #if EV_PERIODICS |
1306 | periodics_reify (EV_A); /* absolute timers called first */ |
1327 | periodics_reify (EV_A); /* absolute timers called first */ |
1307 | #endif |
1328 | #endif |
1308 | |
1329 | |
1309 | /* queue idle watchers unless io or timers are pending */ |
1330 | /* queue idle watchers unless other events are pending */ |
1310 | if (idlecnt && !any_pending (EV_A)) |
1331 | if (idlecnt && !any_pending (EV_A)) |
1311 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1332 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1312 | |
1333 | |
1313 | /* queue check watchers, to be executed first */ |
1334 | /* queue check watchers, to be executed first */ |
1314 | if (expect_false (checkcnt)) |
1335 | if (expect_false (checkcnt)) |
… | |
… | |
1318 | |
1339 | |
1319 | if (expect_false (loop_done)) |
1340 | if (expect_false (loop_done)) |
1320 | break; |
1341 | break; |
1321 | } |
1342 | } |
1322 | |
1343 | |
1323 | if (loop_done != 2) |
1344 | if (loop_done == EVUNLOOP_ONE) |
1324 | loop_done = 0; |
1345 | loop_done = EVUNLOOP_CANCEL; |
1325 | } |
1346 | } |
1326 | |
1347 | |
1327 | void |
1348 | void |
1328 | ev_unloop (EV_P_ int how) |
1349 | ev_unloop (EV_P_ int how) |
1329 | { |
1350 | { |
… | |
… | |
1382 | } |
1403 | } |
1383 | |
1404 | |
1384 | /*****************************************************************************/ |
1405 | /*****************************************************************************/ |
1385 | |
1406 | |
1386 | void |
1407 | void |
1387 | ev_io_start (EV_P_ struct ev_io *w) |
1408 | ev_io_start (EV_P_ ev_io *w) |
1388 | { |
1409 | { |
1389 | int fd = w->fd; |
1410 | int fd = w->fd; |
1390 | |
1411 | |
1391 | if (expect_false (ev_is_active (w))) |
1412 | if (expect_false (ev_is_active (w))) |
1392 | return; |
1413 | return; |
… | |
… | |
1399 | |
1420 | |
1400 | fd_change (EV_A_ fd); |
1421 | fd_change (EV_A_ fd); |
1401 | } |
1422 | } |
1402 | |
1423 | |
1403 | void |
1424 | void |
1404 | ev_io_stop (EV_P_ struct ev_io *w) |
1425 | ev_io_stop (EV_P_ ev_io *w) |
1405 | { |
1426 | { |
1406 | ev_clear_pending (EV_A_ (W)w); |
1427 | ev_clear_pending (EV_A_ (W)w); |
1407 | if (expect_false (!ev_is_active (w))) |
1428 | if (expect_false (!ev_is_active (w))) |
1408 | return; |
1429 | return; |
1409 | |
1430 | |
… | |
… | |
1414 | |
1435 | |
1415 | fd_change (EV_A_ w->fd); |
1436 | fd_change (EV_A_ w->fd); |
1416 | } |
1437 | } |
1417 | |
1438 | |
1418 | void |
1439 | void |
1419 | ev_timer_start (EV_P_ struct ev_timer *w) |
1440 | ev_timer_start (EV_P_ ev_timer *w) |
1420 | { |
1441 | { |
1421 | if (expect_false (ev_is_active (w))) |
1442 | if (expect_false (ev_is_active (w))) |
1422 | return; |
1443 | return; |
1423 | |
1444 | |
1424 | ((WT)w)->at += mn_now; |
1445 | ((WT)w)->at += mn_now; |
1425 | |
1446 | |
1426 | 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.)); |
1427 | |
1448 | |
1428 | ev_start (EV_A_ (W)w, ++timercnt); |
1449 | ev_start (EV_A_ (W)w, ++timercnt); |
1429 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1450 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1430 | timers [timercnt - 1] = w; |
1451 | timers [timercnt - 1] = w; |
1431 | upheap ((WT *)timers, timercnt - 1); |
1452 | upheap ((WT *)timers, timercnt - 1); |
1432 | |
1453 | |
1433 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1454 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1434 | } |
1455 | } |
1435 | |
1456 | |
1436 | void |
1457 | void |
1437 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1458 | ev_timer_stop (EV_P_ ev_timer *w) |
1438 | { |
1459 | { |
1439 | ev_clear_pending (EV_A_ (W)w); |
1460 | ev_clear_pending (EV_A_ (W)w); |
1440 | if (expect_false (!ev_is_active (w))) |
1461 | if (expect_false (!ev_is_active (w))) |
1441 | return; |
1462 | return; |
1442 | |
1463 | |
… | |
… | |
1452 | |
1473 | |
1453 | ev_stop (EV_A_ (W)w); |
1474 | ev_stop (EV_A_ (W)w); |
1454 | } |
1475 | } |
1455 | |
1476 | |
1456 | void |
1477 | void |
1457 | ev_timer_again (EV_P_ struct ev_timer *w) |
1478 | ev_timer_again (EV_P_ ev_timer *w) |
1458 | { |
1479 | { |
1459 | if (ev_is_active (w)) |
1480 | if (ev_is_active (w)) |
1460 | { |
1481 | { |
1461 | if (w->repeat) |
1482 | if (w->repeat) |
1462 | { |
1483 | { |
… | |
… | |
1473 | } |
1494 | } |
1474 | } |
1495 | } |
1475 | |
1496 | |
1476 | #if EV_PERIODICS |
1497 | #if EV_PERIODICS |
1477 | void |
1498 | void |
1478 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1499 | ev_periodic_start (EV_P_ ev_periodic *w) |
1479 | { |
1500 | { |
1480 | if (expect_false (ev_is_active (w))) |
1501 | if (expect_false (ev_is_active (w))) |
1481 | return; |
1502 | return; |
1482 | |
1503 | |
1483 | if (w->reschedule_cb) |
1504 | if (w->reschedule_cb) |
… | |
… | |
1488 | /* 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 */ |
1489 | ((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; |
1490 | } |
1511 | } |
1491 | |
1512 | |
1492 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1513 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1493 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1514 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1494 | periodics [periodiccnt - 1] = w; |
1515 | periodics [periodiccnt - 1] = w; |
1495 | upheap ((WT *)periodics, periodiccnt - 1); |
1516 | upheap ((WT *)periodics, periodiccnt - 1); |
1496 | |
1517 | |
1497 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1518 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1498 | } |
1519 | } |
1499 | |
1520 | |
1500 | void |
1521 | void |
1501 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1522 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1502 | { |
1523 | { |
1503 | ev_clear_pending (EV_A_ (W)w); |
1524 | ev_clear_pending (EV_A_ (W)w); |
1504 | if (expect_false (!ev_is_active (w))) |
1525 | if (expect_false (!ev_is_active (w))) |
1505 | return; |
1526 | return; |
1506 | |
1527 | |
… | |
… | |
1514 | |
1535 | |
1515 | ev_stop (EV_A_ (W)w); |
1536 | ev_stop (EV_A_ (W)w); |
1516 | } |
1537 | } |
1517 | |
1538 | |
1518 | void |
1539 | void |
1519 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1540 | ev_periodic_again (EV_P_ ev_periodic *w) |
1520 | { |
1541 | { |
1521 | /* TODO: use adjustheap and recalculation */ |
1542 | /* TODO: use adjustheap and recalculation */ |
1522 | ev_periodic_stop (EV_A_ w); |
1543 | ev_periodic_stop (EV_A_ w); |
1523 | ev_periodic_start (EV_A_ w); |
1544 | ev_periodic_start (EV_A_ w); |
1524 | } |
1545 | } |
1525 | #endif |
1546 | #endif |
1526 | |
1547 | |
1527 | void |
1548 | void |
1528 | ev_idle_start (EV_P_ struct ev_idle *w) |
1549 | ev_idle_start (EV_P_ ev_idle *w) |
1529 | { |
1550 | { |
1530 | if (expect_false (ev_is_active (w))) |
1551 | if (expect_false (ev_is_active (w))) |
1531 | return; |
1552 | return; |
1532 | |
1553 | |
1533 | ev_start (EV_A_ (W)w, ++idlecnt); |
1554 | ev_start (EV_A_ (W)w, ++idlecnt); |
1534 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1555 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1535 | idles [idlecnt - 1] = w; |
1556 | idles [idlecnt - 1] = w; |
1536 | } |
1557 | } |
1537 | |
1558 | |
1538 | void |
1559 | void |
1539 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1560 | ev_idle_stop (EV_P_ ev_idle *w) |
1540 | { |
1561 | { |
1541 | ev_clear_pending (EV_A_ (W)w); |
1562 | ev_clear_pending (EV_A_ (W)w); |
1542 | if (expect_false (!ev_is_active (w))) |
1563 | if (expect_false (!ev_is_active (w))) |
1543 | return; |
1564 | return; |
1544 | |
1565 | |
|
|
1566 | { |
|
|
1567 | int active = ((W)w)->active; |
1545 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1568 | idles [active - 1] = idles [--idlecnt]; |
|
|
1569 | ((W)idles [active - 1])->active = active; |
|
|
1570 | } |
|
|
1571 | |
1546 | ev_stop (EV_A_ (W)w); |
1572 | ev_stop (EV_A_ (W)w); |
1547 | } |
1573 | } |
1548 | |
1574 | |
1549 | void |
1575 | void |
1550 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1576 | ev_prepare_start (EV_P_ ev_prepare *w) |
1551 | { |
1577 | { |
1552 | if (expect_false (ev_is_active (w))) |
1578 | if (expect_false (ev_is_active (w))) |
1553 | return; |
1579 | return; |
1554 | |
1580 | |
1555 | ev_start (EV_A_ (W)w, ++preparecnt); |
1581 | ev_start (EV_A_ (W)w, ++preparecnt); |
1556 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1582 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1557 | prepares [preparecnt - 1] = w; |
1583 | prepares [preparecnt - 1] = w; |
1558 | } |
1584 | } |
1559 | |
1585 | |
1560 | void |
1586 | void |
1561 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1587 | ev_prepare_stop (EV_P_ ev_prepare *w) |
1562 | { |
1588 | { |
1563 | ev_clear_pending (EV_A_ (W)w); |
1589 | ev_clear_pending (EV_A_ (W)w); |
1564 | if (expect_false (!ev_is_active (w))) |
1590 | if (expect_false (!ev_is_active (w))) |
1565 | return; |
1591 | return; |
1566 | |
1592 | |
|
|
1593 | { |
|
|
1594 | int active = ((W)w)->active; |
1567 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1595 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1596 | ((W)prepares [active - 1])->active = active; |
|
|
1597 | } |
|
|
1598 | |
1568 | ev_stop (EV_A_ (W)w); |
1599 | ev_stop (EV_A_ (W)w); |
1569 | } |
1600 | } |
1570 | |
1601 | |
1571 | void |
1602 | void |
1572 | ev_check_start (EV_P_ struct ev_check *w) |
1603 | ev_check_start (EV_P_ ev_check *w) |
1573 | { |
1604 | { |
1574 | if (expect_false (ev_is_active (w))) |
1605 | if (expect_false (ev_is_active (w))) |
1575 | return; |
1606 | return; |
1576 | |
1607 | |
1577 | ev_start (EV_A_ (W)w, ++checkcnt); |
1608 | ev_start (EV_A_ (W)w, ++checkcnt); |
1578 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1609 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1579 | checks [checkcnt - 1] = w; |
1610 | checks [checkcnt - 1] = w; |
1580 | } |
1611 | } |
1581 | |
1612 | |
1582 | void |
1613 | void |
1583 | ev_check_stop (EV_P_ struct ev_check *w) |
1614 | ev_check_stop (EV_P_ ev_check *w) |
1584 | { |
1615 | { |
1585 | ev_clear_pending (EV_A_ (W)w); |
1616 | ev_clear_pending (EV_A_ (W)w); |
1586 | if (expect_false (!ev_is_active (w))) |
1617 | if (expect_false (!ev_is_active (w))) |
1587 | return; |
1618 | return; |
1588 | |
1619 | |
|
|
1620 | { |
|
|
1621 | int active = ((W)w)->active; |
1589 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1622 | checks [active - 1] = checks [--checkcnt]; |
|
|
1623 | ((W)checks [active - 1])->active = active; |
|
|
1624 | } |
|
|
1625 | |
1590 | ev_stop (EV_A_ (W)w); |
1626 | ev_stop (EV_A_ (W)w); |
1591 | } |
1627 | } |
1592 | |
1628 | |
1593 | #ifndef SA_RESTART |
1629 | #ifndef SA_RESTART |
1594 | # define SA_RESTART 0 |
1630 | # define SA_RESTART 0 |
1595 | #endif |
1631 | #endif |
1596 | |
1632 | |
1597 | void |
1633 | void |
1598 | ev_signal_start (EV_P_ struct ev_signal *w) |
1634 | ev_signal_start (EV_P_ ev_signal *w) |
1599 | { |
1635 | { |
1600 | #if EV_MULTIPLICITY |
1636 | #if EV_MULTIPLICITY |
1601 | 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)); |
1602 | #endif |
1638 | #endif |
1603 | if (expect_false (ev_is_active (w))) |
1639 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1622 | #endif |
1658 | #endif |
1623 | } |
1659 | } |
1624 | } |
1660 | } |
1625 | |
1661 | |
1626 | void |
1662 | void |
1627 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1663 | ev_signal_stop (EV_P_ ev_signal *w) |
1628 | { |
1664 | { |
1629 | ev_clear_pending (EV_A_ (W)w); |
1665 | ev_clear_pending (EV_A_ (W)w); |
1630 | if (expect_false (!ev_is_active (w))) |
1666 | if (expect_false (!ev_is_active (w))) |
1631 | return; |
1667 | return; |
1632 | |
1668 | |
… | |
… | |
1636 | if (!signals [w->signum - 1].head) |
1672 | if (!signals [w->signum - 1].head) |
1637 | signal (w->signum, SIG_DFL); |
1673 | signal (w->signum, SIG_DFL); |
1638 | } |
1674 | } |
1639 | |
1675 | |
1640 | void |
1676 | void |
1641 | ev_child_start (EV_P_ struct ev_child *w) |
1677 | ev_child_start (EV_P_ ev_child *w) |
1642 | { |
1678 | { |
1643 | #if EV_MULTIPLICITY |
1679 | #if EV_MULTIPLICITY |
1644 | 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)); |
1645 | #endif |
1681 | #endif |
1646 | if (expect_false (ev_is_active (w))) |
1682 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1649 | ev_start (EV_A_ (W)w, 1); |
1685 | ev_start (EV_A_ (W)w, 1); |
1650 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1686 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1651 | } |
1687 | } |
1652 | |
1688 | |
1653 | void |
1689 | void |
1654 | ev_child_stop (EV_P_ struct ev_child *w) |
1690 | ev_child_stop (EV_P_ ev_child *w) |
1655 | { |
1691 | { |
1656 | ev_clear_pending (EV_A_ (W)w); |
1692 | ev_clear_pending (EV_A_ (W)w); |
1657 | if (expect_false (!ev_is_active (w))) |
1693 | if (expect_false (!ev_is_active (w))) |
1658 | return; |
1694 | return; |
1659 | |
1695 | |
1660 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1696 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1661 | ev_stop (EV_A_ (W)w); |
1697 | ev_stop (EV_A_ (W)w); |
1662 | } |
1698 | } |
1663 | |
1699 | |
|
|
1700 | #if EV_MULTIPLICITY |
|
|
1701 | void |
|
|
1702 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1703 | { |
|
|
1704 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1705 | } |
|
|
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 | |
|
|
1718 | void |
|
|
1719 | ev_embed_start (EV_P_ ev_embed *w) |
|
|
1720 | { |
|
|
1721 | if (expect_false (ev_is_active (w))) |
|
|
1722 | return; |
|
|
1723 | |
|
|
1724 | { |
|
|
1725 | struct ev_loop *loop = w->loop; |
|
|
1726 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
|
|
1727 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
|
|
1728 | } |
|
|
1729 | |
|
|
1730 | ev_set_priority (&w->io, ev_priority (w)); |
|
|
1731 | ev_io_start (EV_A_ &w->io); |
|
|
1732 | ev_start (EV_A_ (W)w, 1); |
|
|
1733 | } |
|
|
1734 | |
|
|
1735 | void |
|
|
1736 | ev_embed_stop (EV_P_ ev_embed *w) |
|
|
1737 | { |
|
|
1738 | ev_clear_pending (EV_A_ (W)w); |
|
|
1739 | if (expect_false (!ev_is_active (w))) |
|
|
1740 | return; |
|
|
1741 | |
|
|
1742 | ev_io_stop (EV_A_ &w->io); |
|
|
1743 | ev_stop (EV_A_ (W)w); |
|
|
1744 | } |
|
|
1745 | #endif |
|
|
1746 | |
1664 | /*****************************************************************************/ |
1747 | /*****************************************************************************/ |
1665 | |
1748 | |
1666 | struct ev_once |
1749 | struct ev_once |
1667 | { |
1750 | { |
1668 | struct ev_io io; |
1751 | ev_io io; |
1669 | struct ev_timer to; |
1752 | ev_timer to; |
1670 | void (*cb)(int revents, void *arg); |
1753 | void (*cb)(int revents, void *arg); |
1671 | void *arg; |
1754 | void *arg; |
1672 | }; |
1755 | }; |
1673 | |
1756 | |
1674 | static void |
1757 | static void |
… | |
… | |
1683 | |
1766 | |
1684 | cb (revents, arg); |
1767 | cb (revents, arg); |
1685 | } |
1768 | } |
1686 | |
1769 | |
1687 | static void |
1770 | static void |
1688 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1771 | once_cb_io (EV_P_ ev_io *w, int revents) |
1689 | { |
1772 | { |
1690 | 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); |
1691 | } |
1774 | } |
1692 | |
1775 | |
1693 | static void |
1776 | static void |
1694 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1777 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1695 | { |
1778 | { |
1696 | 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); |
1697 | } |
1780 | } |
1698 | |
1781 | |
1699 | void |
1782 | void |