… | |
… | |
338 | fd_kill (EV_A_ fd); |
338 | fd_kill (EV_A_ fd); |
339 | return; |
339 | return; |
340 | } |
340 | } |
341 | } |
341 | } |
342 | |
342 | |
|
|
343 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
344 | static void |
|
|
345 | fd_rearm_all (EV_P) |
|
|
346 | { |
|
|
347 | int fd; |
|
|
348 | |
|
|
349 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
350 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
351 | if (anfds [fd].events) |
|
|
352 | { |
|
|
353 | anfds [fd].events = 0; |
|
|
354 | fd_change (fd); |
|
|
355 | } |
|
|
356 | } |
|
|
357 | |
343 | /*****************************************************************************/ |
358 | /*****************************************************************************/ |
344 | |
359 | |
345 | static void |
360 | static void |
346 | upheap (WT *heap, int k) |
361 | upheap (WT *heap, int k) |
347 | { |
362 | { |
… | |
… | |
542 | ev_method (EV_P) |
557 | ev_method (EV_P) |
543 | { |
558 | { |
544 | return method; |
559 | return method; |
545 | } |
560 | } |
546 | |
561 | |
547 | inline int |
562 | static void |
548 | loop_init (EV_P_ int methods) |
563 | loop_init (EV_P_ int methods) |
549 | { |
564 | { |
550 | if (!method) |
565 | if (!method) |
551 | { |
566 | { |
552 | #if EV_USE_MONOTONIC |
567 | #if EV_USE_MONOTONIC |
… | |
… | |
560 | rt_now = ev_time (); |
575 | rt_now = ev_time (); |
561 | mn_now = get_clock (); |
576 | mn_now = get_clock (); |
562 | now_floor = mn_now; |
577 | now_floor = mn_now; |
563 | rtmn_diff = rt_now - mn_now; |
578 | rtmn_diff = rt_now - mn_now; |
564 | |
579 | |
565 | if (pipe (sigpipe)) |
|
|
566 | return 0; |
|
|
567 | |
|
|
568 | if (methods == EVMETHOD_AUTO) |
580 | if (methods == EVMETHOD_AUTO) |
569 | if (!enable_secure () && getenv ("LIBmethodS")) |
581 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
570 | methods = atoi (getenv ("LIBmethodS")); |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
571 | else |
583 | else |
572 | methods = EVMETHOD_ANY; |
584 | methods = EVMETHOD_ANY; |
573 | |
585 | |
574 | method = 0; |
586 | method = 0; |
575 | #if EV_USE_KQUEUE |
587 | #if EV_USE_KQUEUE |
… | |
… | |
582 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
583 | #endif |
595 | #endif |
584 | #if EV_USE_SELECT |
596 | #if EV_USE_SELECT |
585 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
586 | #endif |
598 | #endif |
|
|
599 | } |
|
|
600 | } |
587 | |
601 | |
|
|
602 | void |
|
|
603 | loop_destroy (EV_P) |
|
|
604 | { |
|
|
605 | #if EV_USE_KQUEUE |
|
|
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
607 | #endif |
|
|
608 | #if EV_USE_EPOLL |
|
|
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
610 | #endif |
|
|
611 | #if EV_USEV_POLL |
|
|
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
613 | #endif |
|
|
614 | #if EV_USE_SELECT |
|
|
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
616 | #endif |
|
|
617 | |
|
|
618 | method = 0; |
|
|
619 | /*TODO*/ |
|
|
620 | } |
|
|
621 | |
|
|
622 | void |
|
|
623 | loop_fork (EV_P) |
|
|
624 | { |
|
|
625 | /*TODO*/ |
|
|
626 | #if EV_USE_EPOLL |
|
|
627 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
628 | #endif |
|
|
629 | #if EV_USE_KQUEUE |
|
|
630 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
631 | #endif |
|
|
632 | } |
|
|
633 | |
|
|
634 | #if EV_MULTIPLICITY |
|
|
635 | struct ev_loop * |
|
|
636 | ev_loop_new (int methods) |
|
|
637 | { |
|
|
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
639 | |
|
|
640 | loop_init (EV_A_ methods); |
|
|
641 | |
|
|
642 | if (ev_methods (EV_A)) |
|
|
643 | return loop; |
|
|
644 | |
|
|
645 | return 0; |
|
|
646 | } |
|
|
647 | |
|
|
648 | void |
|
|
649 | ev_loop_destroy (EV_P) |
|
|
650 | { |
|
|
651 | loop_destroy (EV_A); |
|
|
652 | free (loop); |
|
|
653 | } |
|
|
654 | |
|
|
655 | void |
|
|
656 | ev_loop_fork (EV_P) |
|
|
657 | { |
|
|
658 | loop_fork (EV_A); |
|
|
659 | } |
|
|
660 | |
|
|
661 | #endif |
|
|
662 | |
|
|
663 | #if EV_MULTIPLICITY |
|
|
664 | struct ev_loop default_loop_struct; |
|
|
665 | static struct ev_loop *default_loop; |
|
|
666 | |
|
|
667 | struct ev_loop * |
|
|
668 | #else |
|
|
669 | static int default_loop; |
|
|
670 | |
|
|
671 | int |
|
|
672 | #endif |
|
|
673 | ev_default_loop (int methods) |
|
|
674 | { |
|
|
675 | if (sigpipe [0] == sigpipe [1]) |
|
|
676 | if (pipe (sigpipe)) |
|
|
677 | return 0; |
|
|
678 | |
|
|
679 | if (!default_loop) |
|
|
680 | { |
|
|
681 | #if EV_MULTIPLICITY |
|
|
682 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
683 | #else |
|
|
684 | default_loop = 1; |
|
|
685 | #endif |
|
|
686 | |
|
|
687 | loop_init (EV_A_ methods); |
|
|
688 | |
588 | if (method) |
689 | if (ev_method (EV_A)) |
589 | { |
690 | { |
590 | ev_watcher_init (&sigev, sigcb); |
691 | ev_watcher_init (&sigev, sigcb); |
591 | ev_set_priority (&sigev, EV_MAXPRI); |
692 | ev_set_priority (&sigev, EV_MAXPRI); |
592 | siginit (EV_A); |
693 | siginit (EV_A); |
593 | |
694 | |
… | |
… | |
596 | ev_set_priority (&childev, EV_MAXPRI); |
697 | ev_set_priority (&childev, EV_MAXPRI); |
597 | ev_signal_start (EV_A_ &childev); |
698 | ev_signal_start (EV_A_ &childev); |
598 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
699 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
599 | #endif |
700 | #endif |
600 | } |
701 | } |
|
|
702 | else |
|
|
703 | default_loop = 0; |
601 | } |
704 | } |
602 | |
705 | |
603 | return method; |
706 | return default_loop; |
604 | } |
707 | } |
605 | |
708 | |
|
|
709 | void |
|
|
710 | ev_default_destroy (void) |
|
|
711 | { |
606 | #if EV_MULTIPLICITY |
712 | #if EV_MULTIPLICITY |
607 | |
713 | struct ev_loop *loop = default_loop; |
608 | struct ev_loop * |
|
|
609 | ev_loop_new (int methods) |
|
|
610 | { |
|
|
611 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
612 | |
|
|
613 | if (loop_init (EV_A_ methods)) |
|
|
614 | return loop; |
|
|
615 | |
|
|
616 | ev_loop_delete (loop); |
|
|
617 | |
|
|
618 | return 0; |
|
|
619 | } |
|
|
620 | |
|
|
621 | void |
|
|
622 | ev_loop_delete (EV_P) |
|
|
623 | { |
|
|
624 | /*TODO*/ |
|
|
625 | free (loop); |
|
|
626 | } |
|
|
627 | |
|
|
628 | #else |
|
|
629 | |
|
|
630 | int |
|
|
631 | ev_init (int methods) |
|
|
632 | { |
|
|
633 | return loop_init (methods); |
|
|
634 | } |
|
|
635 | |
|
|
636 | #endif |
714 | #endif |
637 | |
715 | |
638 | /*****************************************************************************/ |
716 | ev_ref (EV_A); /* child watcher */ |
|
|
717 | ev_signal_stop (EV_A_ &childev); |
639 | |
718 | |
640 | void |
719 | ev_ref (EV_A); /* signal watcher */ |
641 | ev_fork_prepare (void) |
720 | ev_io_stop (EV_A_ &sigev); |
642 | { |
|
|
643 | /* nop */ |
|
|
644 | } |
|
|
645 | |
721 | |
646 | void |
722 | close (sigpipe [0]); sigpipe [0] = 0; |
647 | ev_fork_parent (void) |
723 | close (sigpipe [1]); sigpipe [1] = 0; |
648 | { |
|
|
649 | /* nop */ |
|
|
650 | } |
|
|
651 | |
724 | |
|
|
725 | loop_destroy (EV_A); |
|
|
726 | } |
|
|
727 | |
652 | void |
728 | void |
653 | ev_fork_child (void) |
729 | ev_default_fork (EV_P) |
654 | { |
730 | { |
655 | /*TODO*/ |
731 | loop_fork (EV_A); |
656 | #if !EV_MULTIPLICITY |
|
|
657 | #if EV_USE_EPOLL |
|
|
658 | if (method == EVMETHOD_EPOLL) |
|
|
659 | epoll_postfork_child (EV_A); |
|
|
660 | #endif |
|
|
661 | |
732 | |
662 | ev_io_stop (EV_A_ &sigev); |
733 | ev_io_stop (EV_A_ &sigev); |
663 | close (sigpipe [0]); |
734 | close (sigpipe [0]); |
664 | close (sigpipe [1]); |
735 | close (sigpipe [1]); |
665 | pipe (sigpipe); |
736 | pipe (sigpipe); |
|
|
737 | |
|
|
738 | ev_ref (EV_A); /* signal watcher */ |
666 | siginit (EV_A); |
739 | siginit (EV_A); |
667 | #endif |
|
|
668 | } |
740 | } |
669 | |
741 | |
670 | /*****************************************************************************/ |
742 | /*****************************************************************************/ |
671 | |
743 | |
672 | static void |
744 | static void |
… | |
… | |
1083 | } |
1155 | } |
1084 | |
1156 | |
1085 | ev_stop (EV_A_ (W)w); |
1157 | ev_stop (EV_A_ (W)w); |
1086 | } |
1158 | } |
1087 | |
1159 | |
|
|
1160 | void |
|
|
1161 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1162 | { |
|
|
1163 | if (ev_is_active (w)) |
|
|
1164 | return; |
|
|
1165 | |
|
|
1166 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1167 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1168 | idles [idlecnt - 1] = w; |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | void |
|
|
1172 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1173 | { |
|
|
1174 | ev_clear_pending (EV_A_ (W)w); |
|
|
1175 | if (ev_is_active (w)) |
|
|
1176 | return; |
|
|
1177 | |
|
|
1178 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1179 | ev_stop (EV_A_ (W)w); |
|
|
1180 | } |
|
|
1181 | |
|
|
1182 | void |
|
|
1183 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1184 | { |
|
|
1185 | if (ev_is_active (w)) |
|
|
1186 | return; |
|
|
1187 | |
|
|
1188 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1189 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1190 | prepares [preparecnt - 1] = w; |
|
|
1191 | } |
|
|
1192 | |
|
|
1193 | void |
|
|
1194 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1195 | { |
|
|
1196 | ev_clear_pending (EV_A_ (W)w); |
|
|
1197 | if (ev_is_active (w)) |
|
|
1198 | return; |
|
|
1199 | |
|
|
1200 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1201 | ev_stop (EV_A_ (W)w); |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | void |
|
|
1205 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1206 | { |
|
|
1207 | if (ev_is_active (w)) |
|
|
1208 | return; |
|
|
1209 | |
|
|
1210 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1211 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1212 | checks [checkcnt - 1] = w; |
|
|
1213 | } |
|
|
1214 | |
|
|
1215 | void |
|
|
1216 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1217 | { |
|
|
1218 | ev_clear_pending (EV_A_ (W)w); |
|
|
1219 | if (ev_is_active (w)) |
|
|
1220 | return; |
|
|
1221 | |
|
|
1222 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1223 | ev_stop (EV_A_ (W)w); |
|
|
1224 | } |
|
|
1225 | |
1088 | #ifndef SA_RESTART |
1226 | #ifndef SA_RESTART |
1089 | # define SA_RESTART 0 |
1227 | # define SA_RESTART 0 |
1090 | #endif |
1228 | #endif |
1091 | |
1229 | |
1092 | void |
1230 | void |
1093 | ev_signal_start (EV_P_ struct ev_signal *w) |
1231 | ev_signal_start (EV_P_ struct ev_signal *w) |
1094 | { |
1232 | { |
|
|
1233 | #if EV_MULTIPLICITY |
|
|
1234 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1235 | #endif |
1095 | if (ev_is_active (w)) |
1236 | if (ev_is_active (w)) |
1096 | return; |
1237 | return; |
1097 | |
1238 | |
1098 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1239 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1099 | |
1240 | |
… | |
… | |
1124 | if (!signals [w->signum - 1].head) |
1265 | if (!signals [w->signum - 1].head) |
1125 | signal (w->signum, SIG_DFL); |
1266 | signal (w->signum, SIG_DFL); |
1126 | } |
1267 | } |
1127 | |
1268 | |
1128 | void |
1269 | void |
1129 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1130 | { |
|
|
1131 | if (ev_is_active (w)) |
|
|
1132 | return; |
|
|
1133 | |
|
|
1134 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1135 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1136 | idles [idlecnt - 1] = w; |
|
|
1137 | } |
|
|
1138 | |
|
|
1139 | void |
|
|
1140 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1141 | { |
|
|
1142 | ev_clear_pending (EV_A_ (W)w); |
|
|
1143 | if (ev_is_active (w)) |
|
|
1144 | return; |
|
|
1145 | |
|
|
1146 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1147 | ev_stop (EV_A_ (W)w); |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | void |
|
|
1151 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1152 | { |
|
|
1153 | if (ev_is_active (w)) |
|
|
1154 | return; |
|
|
1155 | |
|
|
1156 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1157 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1158 | prepares [preparecnt - 1] = w; |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | void |
|
|
1162 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1163 | { |
|
|
1164 | ev_clear_pending (EV_A_ (W)w); |
|
|
1165 | if (ev_is_active (w)) |
|
|
1166 | return; |
|
|
1167 | |
|
|
1168 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1169 | ev_stop (EV_A_ (W)w); |
|
|
1170 | } |
|
|
1171 | |
|
|
1172 | void |
|
|
1173 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1174 | { |
|
|
1175 | if (ev_is_active (w)) |
|
|
1176 | return; |
|
|
1177 | |
|
|
1178 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1179 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1180 | checks [checkcnt - 1] = w; |
|
|
1181 | } |
|
|
1182 | |
|
|
1183 | void |
|
|
1184 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1185 | { |
|
|
1186 | ev_clear_pending (EV_A_ (W)w); |
|
|
1187 | if (ev_is_active (w)) |
|
|
1188 | return; |
|
|
1189 | |
|
|
1190 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1191 | ev_stop (EV_A_ (W)w); |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | void |
|
|
1195 | ev_child_start (EV_P_ struct ev_child *w) |
1270 | ev_child_start (EV_P_ struct ev_child *w) |
1196 | { |
1271 | { |
|
|
1272 | #if EV_MULTIPLICITY |
|
|
1273 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1274 | #endif |
1197 | if (ev_is_active (w)) |
1275 | if (ev_is_active (w)) |
1198 | return; |
1276 | return; |
1199 | |
1277 | |
1200 | ev_start (EV_A_ (W)w, 1); |
1278 | ev_start (EV_A_ (W)w, 1); |
1201 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1279 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1273 | ev_timer_start (EV_A_ &once->to); |
1351 | ev_timer_start (EV_A_ &once->to); |
1274 | } |
1352 | } |
1275 | } |
1353 | } |
1276 | } |
1354 | } |
1277 | |
1355 | |
1278 | /*****************************************************************************/ |
|
|
1279 | |
|
|
1280 | #if 0 |
|
|
1281 | |
|
|
1282 | struct ev_io wio; |
|
|
1283 | |
|
|
1284 | static void |
|
|
1285 | sin_cb (struct ev_io *w, int revents) |
|
|
1286 | { |
|
|
1287 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1288 | } |
|
|
1289 | |
|
|
1290 | static void |
|
|
1291 | ocb (struct ev_timer *w, int revents) |
|
|
1292 | { |
|
|
1293 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1294 | ev_timer_stop (w); |
|
|
1295 | ev_timer_start (w); |
|
|
1296 | } |
|
|
1297 | |
|
|
1298 | static void |
|
|
1299 | scb (struct ev_signal *w, int revents) |
|
|
1300 | { |
|
|
1301 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1302 | ev_io_stop (&wio); |
|
|
1303 | ev_io_start (&wio); |
|
|
1304 | } |
|
|
1305 | |
|
|
1306 | static void |
|
|
1307 | gcb (struct ev_signal *w, int revents) |
|
|
1308 | { |
|
|
1309 | fprintf (stderr, "generic %x\n", revents); |
|
|
1310 | |
|
|
1311 | } |
|
|
1312 | |
|
|
1313 | int main (void) |
|
|
1314 | { |
|
|
1315 | ev_init (0); |
|
|
1316 | |
|
|
1317 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1318 | ev_io_start (&wio); |
|
|
1319 | |
|
|
1320 | struct ev_timer t[10000]; |
|
|
1321 | |
|
|
1322 | #if 0 |
|
|
1323 | int i; |
|
|
1324 | for (i = 0; i < 10000; ++i) |
|
|
1325 | { |
|
|
1326 | struct ev_timer *w = t + i; |
|
|
1327 | ev_watcher_init (w, ocb, i); |
|
|
1328 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1329 | ev_timer_start (w); |
|
|
1330 | if (drand48 () < 0.5) |
|
|
1331 | ev_timer_stop (w); |
|
|
1332 | } |
|
|
1333 | #endif |
|
|
1334 | |
|
|
1335 | struct ev_timer t1; |
|
|
1336 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1337 | ev_timer_start (&t1); |
|
|
1338 | |
|
|
1339 | struct ev_signal sig; |
|
|
1340 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1341 | ev_signal_start (&sig); |
|
|
1342 | |
|
|
1343 | struct ev_check cw; |
|
|
1344 | ev_check_init (&cw, gcb); |
|
|
1345 | ev_check_start (&cw); |
|
|
1346 | |
|
|
1347 | struct ev_idle iw; |
|
|
1348 | ev_idle_init (&iw, gcb); |
|
|
1349 | ev_idle_start (&iw); |
|
|
1350 | |
|
|
1351 | ev_loop (0); |
|
|
1352 | |
|
|
1353 | return 0; |
|
|
1354 | } |
|
|
1355 | |
|
|
1356 | #endif |
|
|
1357 | |
|
|
1358 | |
|
|
1359 | |
|
|
1360 | |
|
|