… | |
… | |
95 | # define EV_USE_MONOTONIC 1 |
95 | # define EV_USE_MONOTONIC 1 |
96 | #endif |
96 | #endif |
97 | |
97 | |
98 | #ifndef EV_USE_SELECT |
98 | #ifndef EV_USE_SELECT |
99 | # define EV_USE_SELECT 1 |
99 | # define EV_USE_SELECT 1 |
|
|
100 | # define EV_SELECT_USE_FD_SET 1 |
100 | #endif |
101 | #endif |
101 | |
102 | |
102 | #ifndef EV_USE_POLL |
103 | #ifndef EV_USE_POLL |
103 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
104 | # ifdef _WIN32 |
|
|
105 | # define EV_USE_POLL 0 |
|
|
106 | # else |
|
|
107 | # define EV_USE_POLL 1 |
|
|
108 | # endif |
104 | #endif |
109 | #endif |
105 | |
110 | |
106 | #ifndef EV_USE_EPOLL |
111 | #ifndef EV_USE_EPOLL |
107 | # define EV_USE_EPOLL 0 |
112 | # define EV_USE_EPOLL 0 |
108 | #endif |
113 | #endif |
… | |
… | |
114 | #ifndef EV_USE_REALTIME |
119 | #ifndef EV_USE_REALTIME |
115 | # define EV_USE_REALTIME 1 |
120 | # define EV_USE_REALTIME 1 |
116 | #endif |
121 | #endif |
117 | |
122 | |
118 | /**/ |
123 | /**/ |
|
|
124 | |
|
|
125 | /* darwin simply cannot be helped */ |
|
|
126 | #ifdef __APPLE__ |
|
|
127 | # undef EV_USE_POLL |
|
|
128 | # undef EV_USE_KQUEUE |
|
|
129 | #endif |
119 | |
130 | |
120 | #ifndef CLOCK_MONOTONIC |
131 | #ifndef CLOCK_MONOTONIC |
121 | # undef EV_USE_MONOTONIC |
132 | # undef EV_USE_MONOTONIC |
122 | # define EV_USE_MONOTONIC 0 |
133 | # define EV_USE_MONOTONIC 0 |
123 | #endif |
134 | #endif |
… | |
… | |
156 | #define expect_true(expr) expect ((expr) != 0, 1) |
167 | #define expect_true(expr) expect ((expr) != 0, 1) |
157 | |
168 | |
158 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
169 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
159 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
170 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
160 | |
171 | |
161 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
172 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
|
|
173 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
162 | |
174 | |
163 | typedef struct ev_watcher *W; |
175 | typedef struct ev_watcher *W; |
164 | typedef struct ev_watcher_list *WL; |
176 | typedef struct ev_watcher_list *WL; |
165 | typedef struct ev_watcher_time *WT; |
177 | typedef struct ev_watcher_time *WT; |
166 | |
178 | |
… | |
… | |
299 | { |
311 | { |
300 | return ev_rt_now; |
312 | return ev_rt_now; |
301 | } |
313 | } |
302 | #endif |
314 | #endif |
303 | |
315 | |
304 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
316 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
305 | |
317 | |
306 | #define array_needsize(type,base,cur,cnt,init) \ |
318 | #define array_needsize(type,base,cur,cnt,init) \ |
307 | if (expect_false ((cnt) > cur)) \ |
319 | if (expect_false ((cnt) > cur)) \ |
308 | { \ |
320 | { \ |
309 | int newcnt = cur; \ |
321 | int newcnt = cur; \ |
… | |
… | |
354 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
366 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
355 | return; |
367 | return; |
356 | } |
368 | } |
357 | |
369 | |
358 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
370 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
359 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
371 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
360 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
372 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
361 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
373 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
362 | } |
374 | } |
363 | |
375 | |
364 | static void |
376 | static void |
… | |
… | |
434 | return; |
446 | return; |
435 | |
447 | |
436 | anfds [fd].reify = 1; |
448 | anfds [fd].reify = 1; |
437 | |
449 | |
438 | ++fdchangecnt; |
450 | ++fdchangecnt; |
439 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
451 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
440 | fdchanges [fdchangecnt - 1] = fd; |
452 | fdchanges [fdchangecnt - 1] = fd; |
441 | } |
453 | } |
442 | |
454 | |
443 | static void |
455 | static void |
444 | fd_kill (EV_P_ int fd) |
456 | fd_kill (EV_P_ int fd) |
… | |
… | |
735 | return getuid () != geteuid () |
747 | return getuid () != geteuid () |
736 | || getgid () != getegid (); |
748 | || getgid () != getegid (); |
737 | #endif |
749 | #endif |
738 | } |
750 | } |
739 | |
751 | |
740 | int |
752 | unsigned int |
741 | ev_method (EV_P) |
753 | ev_method (EV_P) |
742 | { |
754 | { |
743 | return method; |
755 | return method; |
744 | } |
756 | } |
745 | |
757 | |
746 | static void |
758 | static void |
747 | loop_init (EV_P_ int methods) |
759 | loop_init (EV_P_ unsigned int flags) |
748 | { |
760 | { |
749 | if (!method) |
761 | if (!method) |
750 | { |
762 | { |
751 | #if EV_USE_MONOTONIC |
763 | #if EV_USE_MONOTONIC |
752 | { |
764 | { |
… | |
… | |
759 | ev_rt_now = ev_time (); |
771 | ev_rt_now = ev_time (); |
760 | mn_now = get_clock (); |
772 | mn_now = get_clock (); |
761 | now_floor = mn_now; |
773 | now_floor = mn_now; |
762 | rtmn_diff = ev_rt_now - mn_now; |
774 | rtmn_diff = ev_rt_now - mn_now; |
763 | |
775 | |
764 | if (methods == EVMETHOD_AUTO) |
776 | if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
765 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
766 | methods = atoi (getenv ("LIBEV_METHODS")); |
777 | flags = atoi (getenv ("LIBEV_FLAGS")); |
767 | else |
778 | |
768 | methods = EVMETHOD_ANY; |
779 | if (!(flags & 0x0000ffff)) |
|
|
780 | flags |= 0x0000ffff; |
769 | |
781 | |
770 | method = 0; |
782 | method = 0; |
771 | #if EV_USE_KQUEUE |
783 | #if EV_USE_KQUEUE |
772 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
784 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
773 | #endif |
785 | #endif |
774 | #if EV_USE_EPOLL |
786 | #if EV_USE_EPOLL |
775 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
787 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
776 | #endif |
788 | #endif |
777 | #if EV_USE_POLL |
789 | #if EV_USE_POLL |
778 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
790 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
779 | #endif |
791 | #endif |
780 | #if EV_USE_SELECT |
792 | #if EV_USE_SELECT |
781 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
793 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
782 | #endif |
794 | #endif |
783 | |
795 | |
784 | ev_init (&sigev, sigcb); |
796 | ev_init (&sigev, sigcb); |
785 | ev_set_priority (&sigev, EV_MAXPRI); |
797 | ev_set_priority (&sigev, EV_MAXPRI); |
786 | } |
798 | } |
… | |
… | |
806 | |
818 | |
807 | for (i = NUMPRI; i--; ) |
819 | for (i = NUMPRI; i--; ) |
808 | array_free (pending, [i]); |
820 | array_free (pending, [i]); |
809 | |
821 | |
810 | /* have to use the microsoft-never-gets-it-right macro */ |
822 | /* have to use the microsoft-never-gets-it-right macro */ |
811 | array_free (fdchange, EMPTY); |
823 | array_free (fdchange, EMPTY0); |
812 | array_free (timer, EMPTY); |
824 | array_free (timer, EMPTY0); |
813 | #if EV_PERIODICS |
825 | #if EV_PERIODICS |
814 | array_free (periodic, EMPTY); |
826 | array_free (periodic, EMPTY0); |
815 | #endif |
827 | #endif |
816 | array_free (idle, EMPTY); |
828 | array_free (idle, EMPTY0); |
817 | array_free (prepare, EMPTY); |
829 | array_free (prepare, EMPTY0); |
818 | array_free (check, EMPTY); |
830 | array_free (check, EMPTY0); |
819 | |
831 | |
820 | method = 0; |
832 | method = 0; |
821 | } |
833 | } |
822 | |
834 | |
823 | static void |
835 | static void |
… | |
… | |
848 | postfork = 0; |
860 | postfork = 0; |
849 | } |
861 | } |
850 | |
862 | |
851 | #if EV_MULTIPLICITY |
863 | #if EV_MULTIPLICITY |
852 | struct ev_loop * |
864 | struct ev_loop * |
853 | ev_loop_new (int methods) |
865 | ev_loop_new (unsigned int flags) |
854 | { |
866 | { |
855 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
867 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
856 | |
868 | |
857 | memset (loop, 0, sizeof (struct ev_loop)); |
869 | memset (loop, 0, sizeof (struct ev_loop)); |
858 | |
870 | |
859 | loop_init (EV_A_ methods); |
871 | loop_init (EV_A_ flags); |
860 | |
872 | |
861 | if (ev_method (EV_A)) |
873 | if (ev_method (EV_A)) |
862 | return loop; |
874 | return loop; |
863 | |
875 | |
864 | return 0; |
876 | return 0; |
… | |
… | |
882 | #if EV_MULTIPLICITY |
894 | #if EV_MULTIPLICITY |
883 | struct ev_loop * |
895 | struct ev_loop * |
884 | #else |
896 | #else |
885 | int |
897 | int |
886 | #endif |
898 | #endif |
887 | ev_default_loop (int methods) |
899 | ev_default_loop (unsigned int flags) |
888 | { |
900 | { |
889 | if (sigpipe [0] == sigpipe [1]) |
901 | if (sigpipe [0] == sigpipe [1]) |
890 | if (pipe (sigpipe)) |
902 | if (pipe (sigpipe)) |
891 | return 0; |
903 | return 0; |
892 | |
904 | |
… | |
… | |
896 | struct ev_loop *loop = default_loop = &default_loop_struct; |
908 | struct ev_loop *loop = default_loop = &default_loop_struct; |
897 | #else |
909 | #else |
898 | default_loop = 1; |
910 | default_loop = 1; |
899 | #endif |
911 | #endif |
900 | |
912 | |
901 | loop_init (EV_A_ methods); |
913 | loop_init (EV_A_ flags); |
902 | |
914 | |
903 | if (ev_method (EV_A)) |
915 | if (ev_method (EV_A)) |
904 | { |
916 | { |
905 | siginit (EV_A); |
917 | siginit (EV_A); |
906 | |
918 | |
… | |
… | |
1020 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1032 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1021 | |
1033 | |
1022 | /* first reschedule or stop timer */ |
1034 | /* first reschedule or stop timer */ |
1023 | if (w->reschedule_cb) |
1035 | if (w->reschedule_cb) |
1024 | { |
1036 | { |
1025 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1037 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1026 | |
|
|
1027 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1038 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1028 | downheap ((WT *)periodics, periodiccnt, 0); |
1039 | downheap ((WT *)periodics, periodiccnt, 0); |
1029 | } |
1040 | } |
1030 | else if (w->interval) |
1041 | else if (w->interval) |
1031 | { |
1042 | { |
… | |
… | |
1149 | ev_loop (EV_P_ int flags) |
1160 | ev_loop (EV_P_ int flags) |
1150 | { |
1161 | { |
1151 | double block; |
1162 | double block; |
1152 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1163 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1153 | |
1164 | |
1154 | do |
1165 | while (activecnt) |
1155 | { |
1166 | { |
1156 | /* queue check watchers (and execute them) */ |
1167 | /* queue check watchers (and execute them) */ |
1157 | if (expect_false (preparecnt)) |
1168 | if (expect_false (preparecnt)) |
1158 | { |
1169 | { |
1159 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1170 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
… | |
… | |
1222 | /* queue check watchers, to be executed first */ |
1233 | /* queue check watchers, to be executed first */ |
1223 | if (checkcnt) |
1234 | if (checkcnt) |
1224 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1235 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1225 | |
1236 | |
1226 | call_pending (EV_A); |
1237 | call_pending (EV_A); |
|
|
1238 | |
|
|
1239 | if (loop_done) |
|
|
1240 | break; |
1227 | } |
1241 | } |
1228 | while (activecnt && !loop_done); |
|
|
1229 | |
1242 | |
1230 | if (loop_done != 2) |
1243 | if (loop_done != 2) |
1231 | loop_done = 0; |
1244 | loop_done = 0; |
1232 | } |
1245 | } |
1233 | |
1246 | |
… | |
… | |
1331 | ((WT)w)->at += mn_now; |
1344 | ((WT)w)->at += mn_now; |
1332 | |
1345 | |
1333 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1346 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1334 | |
1347 | |
1335 | ev_start (EV_A_ (W)w, ++timercnt); |
1348 | ev_start (EV_A_ (W)w, ++timercnt); |
1336 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1349 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1337 | timers [timercnt - 1] = w; |
1350 | timers [timercnt - 1] = w; |
1338 | upheap ((WT *)timers, timercnt - 1); |
1351 | upheap ((WT *)timers, timercnt - 1); |
1339 | |
1352 | |
1340 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1353 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1341 | } |
1354 | } |
… | |
… | |
1372 | } |
1385 | } |
1373 | else |
1386 | else |
1374 | ev_timer_stop (EV_A_ w); |
1387 | ev_timer_stop (EV_A_ w); |
1375 | } |
1388 | } |
1376 | else if (w->repeat) |
1389 | else if (w->repeat) |
|
|
1390 | { |
|
|
1391 | w->at = w->repeat; |
1377 | ev_timer_start (EV_A_ w); |
1392 | ev_timer_start (EV_A_ w); |
|
|
1393 | } |
1378 | } |
1394 | } |
1379 | |
1395 | |
1380 | #if EV_PERIODICS |
1396 | #if EV_PERIODICS |
1381 | void |
1397 | void |
1382 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1398 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
… | |
… | |
1392 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1408 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1393 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1409 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1394 | } |
1410 | } |
1395 | |
1411 | |
1396 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1412 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1397 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1413 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1398 | periodics [periodiccnt - 1] = w; |
1414 | periodics [periodiccnt - 1] = w; |
1399 | upheap ((WT *)periodics, periodiccnt - 1); |
1415 | upheap ((WT *)periodics, periodiccnt - 1); |
1400 | |
1416 | |
1401 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1417 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1402 | } |
1418 | } |
… | |
… | |
1433 | { |
1449 | { |
1434 | if (ev_is_active (w)) |
1450 | if (ev_is_active (w)) |
1435 | return; |
1451 | return; |
1436 | |
1452 | |
1437 | ev_start (EV_A_ (W)w, ++idlecnt); |
1453 | ev_start (EV_A_ (W)w, ++idlecnt); |
1438 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1454 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1439 | idles [idlecnt - 1] = w; |
1455 | idles [idlecnt - 1] = w; |
1440 | } |
1456 | } |
1441 | |
1457 | |
1442 | void |
1458 | void |
1443 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1459 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1455 | { |
1471 | { |
1456 | if (ev_is_active (w)) |
1472 | if (ev_is_active (w)) |
1457 | return; |
1473 | return; |
1458 | |
1474 | |
1459 | ev_start (EV_A_ (W)w, ++preparecnt); |
1475 | ev_start (EV_A_ (W)w, ++preparecnt); |
1460 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1476 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1461 | prepares [preparecnt - 1] = w; |
1477 | prepares [preparecnt - 1] = w; |
1462 | } |
1478 | } |
1463 | |
1479 | |
1464 | void |
1480 | void |
1465 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1481 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1477 | { |
1493 | { |
1478 | if (ev_is_active (w)) |
1494 | if (ev_is_active (w)) |
1479 | return; |
1495 | return; |
1480 | |
1496 | |
1481 | ev_start (EV_A_ (W)w, ++checkcnt); |
1497 | ev_start (EV_A_ (W)w, ++checkcnt); |
1482 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1498 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1483 | checks [checkcnt - 1] = w; |
1499 | checks [checkcnt - 1] = w; |
1484 | } |
1500 | } |
1485 | |
1501 | |
1486 | void |
1502 | void |
1487 | ev_check_stop (EV_P_ struct ev_check *w) |
1503 | ev_check_stop (EV_P_ struct ev_check *w) |