… | |
… | |
167 | #define expect_true(expr) expect ((expr) != 0, 1) |
167 | #define expect_true(expr) expect ((expr) != 0, 1) |
168 | |
168 | |
169 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
169 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
170 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
170 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
171 | |
171 | |
172 | #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 */ |
173 | |
174 | |
174 | typedef struct ev_watcher *W; |
175 | typedef struct ev_watcher *W; |
175 | typedef struct ev_watcher_list *WL; |
176 | typedef struct ev_watcher_list *WL; |
176 | typedef struct ev_watcher_time *WT; |
177 | typedef struct ev_watcher_time *WT; |
177 | |
178 | |
… | |
… | |
257 | #include "ev_vars.h" |
258 | #include "ev_vars.h" |
258 | #undef VAR |
259 | #undef VAR |
259 | }; |
260 | }; |
260 | #include "ev_wrap.h" |
261 | #include "ev_wrap.h" |
261 | |
262 | |
262 | struct ev_loop default_loop_struct; |
263 | static struct ev_loop default_loop_struct; |
263 | static struct ev_loop *default_loop; |
264 | struct ev_loop *ev_default_loop_ptr; |
264 | |
265 | |
265 | #else |
266 | #else |
266 | |
267 | |
267 | ev_tstamp ev_rt_now; |
268 | ev_tstamp ev_rt_now; |
268 | #define VAR(name,decl) static decl; |
269 | #define VAR(name,decl) static decl; |
269 | #include "ev_vars.h" |
270 | #include "ev_vars.h" |
270 | #undef VAR |
271 | #undef VAR |
271 | |
272 | |
272 | static int default_loop; |
273 | static int ev_default_loop_ptr; |
273 | |
274 | |
274 | #endif |
275 | #endif |
275 | |
276 | |
276 | /*****************************************************************************/ |
277 | /*****************************************************************************/ |
277 | |
278 | |
… | |
… | |
365 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
366 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
366 | return; |
367 | return; |
367 | } |
368 | } |
368 | |
369 | |
369 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
370 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
370 | 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); |
371 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
372 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
372 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
373 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
373 | } |
374 | } |
374 | |
375 | |
375 | static void |
376 | static void |
… | |
… | |
445 | return; |
446 | return; |
446 | |
447 | |
447 | anfds [fd].reify = 1; |
448 | anfds [fd].reify = 1; |
448 | |
449 | |
449 | ++fdchangecnt; |
450 | ++fdchangecnt; |
450 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
451 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
451 | fdchanges [fdchangecnt - 1] = fd; |
452 | fdchanges [fdchangecnt - 1] = fd; |
452 | } |
453 | } |
453 | |
454 | |
454 | static void |
455 | static void |
455 | fd_kill (EV_P_ int fd) |
456 | fd_kill (EV_P_ int fd) |
… | |
… | |
613 | ev_feed_signal_event (EV_P_ int signum) |
614 | ev_feed_signal_event (EV_P_ int signum) |
614 | { |
615 | { |
615 | WL w; |
616 | WL w; |
616 | |
617 | |
617 | #if EV_MULTIPLICITY |
618 | #if EV_MULTIPLICITY |
618 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
619 | assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); |
619 | #endif |
620 | #endif |
620 | |
621 | |
621 | --signum; |
622 | --signum; |
622 | |
623 | |
623 | if (signum < 0 || signum >= signalmax) |
624 | if (signum < 0 || signum >= signalmax) |
… | |
… | |
746 | return getuid () != geteuid () |
747 | return getuid () != geteuid () |
747 | || getgid () != getegid (); |
748 | || getgid () != getegid (); |
748 | #endif |
749 | #endif |
749 | } |
750 | } |
750 | |
751 | |
751 | int |
752 | unsigned int |
752 | ev_method (EV_P) |
753 | ev_method (EV_P) |
753 | { |
754 | { |
754 | return method; |
755 | return method; |
755 | } |
756 | } |
756 | |
757 | |
… | |
… | |
770 | ev_rt_now = ev_time (); |
771 | ev_rt_now = ev_time (); |
771 | mn_now = get_clock (); |
772 | mn_now = get_clock (); |
772 | now_floor = mn_now; |
773 | now_floor = mn_now; |
773 | rtmn_diff = ev_rt_now - mn_now; |
774 | rtmn_diff = ev_rt_now - mn_now; |
774 | |
775 | |
775 | if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
776 | if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
776 | flags = atoi (getenv ("LIBEV_FLAGS")); |
777 | flags = atoi (getenv ("LIBEV_FLAGS")); |
777 | |
778 | |
778 | if (!(flags & 0x0000ffff)) |
779 | if (!(flags & 0x0000ffff)) |
779 | flags |= 0x0000ffff; |
780 | flags |= 0x0000ffff; |
780 | |
781 | |
… | |
… | |
817 | |
818 | |
818 | for (i = NUMPRI; i--; ) |
819 | for (i = NUMPRI; i--; ) |
819 | array_free (pending, [i]); |
820 | array_free (pending, [i]); |
820 | |
821 | |
821 | /* have to use the microsoft-never-gets-it-right macro */ |
822 | /* have to use the microsoft-never-gets-it-right macro */ |
822 | array_free (fdchange, EMPTY); |
823 | array_free (fdchange, EMPTY0); |
823 | array_free (timer, EMPTY); |
824 | array_free (timer, EMPTY0); |
824 | #if EV_PERIODICS |
825 | #if EV_PERIODICS |
825 | array_free (periodic, EMPTY); |
826 | array_free (periodic, EMPTY0); |
826 | #endif |
827 | #endif |
827 | array_free (idle, EMPTY); |
828 | array_free (idle, EMPTY0); |
828 | array_free (prepare, EMPTY); |
829 | array_free (prepare, EMPTY0); |
829 | array_free (check, EMPTY); |
830 | array_free (check, EMPTY0); |
830 | |
831 | |
831 | method = 0; |
832 | method = 0; |
832 | } |
833 | } |
833 | |
834 | |
834 | static void |
835 | static void |
… | |
… | |
890 | |
891 | |
891 | #endif |
892 | #endif |
892 | |
893 | |
893 | #if EV_MULTIPLICITY |
894 | #if EV_MULTIPLICITY |
894 | struct ev_loop * |
895 | struct ev_loop * |
|
|
896 | ev_default_loop_ (unsigned int flags) |
895 | #else |
897 | #else |
896 | int |
898 | int |
897 | #endif |
|
|
898 | ev_default_loop (unsigned int methods) |
899 | ev_default_loop (unsigned int flags) |
|
|
900 | #endif |
899 | { |
901 | { |
900 | if (sigpipe [0] == sigpipe [1]) |
902 | if (sigpipe [0] == sigpipe [1]) |
901 | if (pipe (sigpipe)) |
903 | if (pipe (sigpipe)) |
902 | return 0; |
904 | return 0; |
903 | |
905 | |
904 | if (!default_loop) |
906 | if (!ev_default_loop_ptr) |
905 | { |
907 | { |
906 | #if EV_MULTIPLICITY |
908 | #if EV_MULTIPLICITY |
907 | struct ev_loop *loop = default_loop = &default_loop_struct; |
909 | struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; |
908 | #else |
910 | #else |
909 | default_loop = 1; |
911 | ev_default_default_loop_ptr = 1; |
910 | #endif |
912 | #endif |
911 | |
913 | |
912 | loop_init (EV_A_ methods); |
914 | loop_init (EV_A_ flags); |
913 | |
915 | |
914 | if (ev_method (EV_A)) |
916 | if (ev_method (EV_A)) |
915 | { |
917 | { |
916 | siginit (EV_A); |
918 | siginit (EV_A); |
917 | |
919 | |
… | |
… | |
921 | ev_signal_start (EV_A_ &childev); |
923 | ev_signal_start (EV_A_ &childev); |
922 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
924 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
923 | #endif |
925 | #endif |
924 | } |
926 | } |
925 | else |
927 | else |
926 | default_loop = 0; |
928 | ev_default_loop_ptr = 0; |
927 | } |
929 | } |
928 | |
930 | |
929 | return default_loop; |
931 | return ev_default_loop_ptr; |
930 | } |
932 | } |
931 | |
933 | |
932 | void |
934 | void |
933 | ev_default_destroy (void) |
935 | ev_default_destroy (void) |
934 | { |
936 | { |
935 | #if EV_MULTIPLICITY |
937 | #if EV_MULTIPLICITY |
936 | struct ev_loop *loop = default_loop; |
938 | struct ev_loop *loop = ev_default_loop_ptr; |
937 | #endif |
939 | #endif |
938 | |
940 | |
939 | #ifndef _WIN32 |
941 | #ifndef _WIN32 |
940 | ev_ref (EV_A); /* child watcher */ |
942 | ev_ref (EV_A); /* child watcher */ |
941 | ev_signal_stop (EV_A_ &childev); |
943 | ev_signal_stop (EV_A_ &childev); |
… | |
… | |
952 | |
954 | |
953 | void |
955 | void |
954 | ev_default_fork (void) |
956 | ev_default_fork (void) |
955 | { |
957 | { |
956 | #if EV_MULTIPLICITY |
958 | #if EV_MULTIPLICITY |
957 | struct ev_loop *loop = default_loop; |
959 | struct ev_loop *loop = ev_default_loop_ptr; |
958 | #endif |
960 | #endif |
959 | |
961 | |
960 | if (method) |
962 | if (method) |
961 | postfork = 1; |
963 | postfork = 1; |
962 | } |
964 | } |
… | |
… | |
1159 | ev_loop (EV_P_ int flags) |
1161 | ev_loop (EV_P_ int flags) |
1160 | { |
1162 | { |
1161 | double block; |
1163 | double block; |
1162 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1164 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1163 | |
1165 | |
1164 | do |
1166 | while (activecnt) |
1165 | { |
1167 | { |
1166 | /* queue check watchers (and execute them) */ |
1168 | /* queue check watchers (and execute them) */ |
1167 | if (expect_false (preparecnt)) |
1169 | if (expect_false (preparecnt)) |
1168 | { |
1170 | { |
1169 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1171 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
… | |
… | |
1232 | /* queue check watchers, to be executed first */ |
1234 | /* queue check watchers, to be executed first */ |
1233 | if (checkcnt) |
1235 | if (checkcnt) |
1234 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1236 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1235 | |
1237 | |
1236 | call_pending (EV_A); |
1238 | call_pending (EV_A); |
|
|
1239 | |
|
|
1240 | if (loop_done) |
|
|
1241 | break; |
1237 | } |
1242 | } |
1238 | while (activecnt && !loop_done); |
|
|
1239 | |
1243 | |
1240 | if (loop_done != 2) |
1244 | if (loop_done != 2) |
1241 | loop_done = 0; |
1245 | loop_done = 0; |
1242 | } |
1246 | } |
1243 | |
1247 | |
… | |
… | |
1341 | ((WT)w)->at += mn_now; |
1345 | ((WT)w)->at += mn_now; |
1342 | |
1346 | |
1343 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1347 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1344 | |
1348 | |
1345 | ev_start (EV_A_ (W)w, ++timercnt); |
1349 | ev_start (EV_A_ (W)w, ++timercnt); |
1346 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1350 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1347 | timers [timercnt - 1] = w; |
1351 | timers [timercnt - 1] = w; |
1348 | upheap ((WT *)timers, timercnt - 1); |
1352 | upheap ((WT *)timers, timercnt - 1); |
1349 | |
1353 | |
1350 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1354 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1351 | } |
1355 | } |
… | |
… | |
1382 | } |
1386 | } |
1383 | else |
1387 | else |
1384 | ev_timer_stop (EV_A_ w); |
1388 | ev_timer_stop (EV_A_ w); |
1385 | } |
1389 | } |
1386 | else if (w->repeat) |
1390 | else if (w->repeat) |
|
|
1391 | { |
|
|
1392 | w->at = w->repeat; |
1387 | ev_timer_start (EV_A_ w); |
1393 | ev_timer_start (EV_A_ w); |
|
|
1394 | } |
1388 | } |
1395 | } |
1389 | |
1396 | |
1390 | #if EV_PERIODICS |
1397 | #if EV_PERIODICS |
1391 | void |
1398 | void |
1392 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1399 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
… | |
… | |
1402 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1409 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1403 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1410 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1404 | } |
1411 | } |
1405 | |
1412 | |
1406 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1413 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1407 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1414 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1408 | periodics [periodiccnt - 1] = w; |
1415 | periodics [periodiccnt - 1] = w; |
1409 | upheap ((WT *)periodics, periodiccnt - 1); |
1416 | upheap ((WT *)periodics, periodiccnt - 1); |
1410 | |
1417 | |
1411 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1418 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1412 | } |
1419 | } |
… | |
… | |
1443 | { |
1450 | { |
1444 | if (ev_is_active (w)) |
1451 | if (ev_is_active (w)) |
1445 | return; |
1452 | return; |
1446 | |
1453 | |
1447 | ev_start (EV_A_ (W)w, ++idlecnt); |
1454 | ev_start (EV_A_ (W)w, ++idlecnt); |
1448 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1455 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1449 | idles [idlecnt - 1] = w; |
1456 | idles [idlecnt - 1] = w; |
1450 | } |
1457 | } |
1451 | |
1458 | |
1452 | void |
1459 | void |
1453 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1460 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1465 | { |
1472 | { |
1466 | if (ev_is_active (w)) |
1473 | if (ev_is_active (w)) |
1467 | return; |
1474 | return; |
1468 | |
1475 | |
1469 | ev_start (EV_A_ (W)w, ++preparecnt); |
1476 | ev_start (EV_A_ (W)w, ++preparecnt); |
1470 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1477 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1471 | prepares [preparecnt - 1] = w; |
1478 | prepares [preparecnt - 1] = w; |
1472 | } |
1479 | } |
1473 | |
1480 | |
1474 | void |
1481 | void |
1475 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1482 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1487 | { |
1494 | { |
1488 | if (ev_is_active (w)) |
1495 | if (ev_is_active (w)) |
1489 | return; |
1496 | return; |
1490 | |
1497 | |
1491 | ev_start (EV_A_ (W)w, ++checkcnt); |
1498 | ev_start (EV_A_ (W)w, ++checkcnt); |
1492 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1499 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1493 | checks [checkcnt - 1] = w; |
1500 | checks [checkcnt - 1] = w; |
1494 | } |
1501 | } |
1495 | |
1502 | |
1496 | void |
1503 | void |
1497 | ev_check_stop (EV_P_ struct ev_check *w) |
1504 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1510 | |
1517 | |
1511 | void |
1518 | void |
1512 | ev_signal_start (EV_P_ struct ev_signal *w) |
1519 | ev_signal_start (EV_P_ struct ev_signal *w) |
1513 | { |
1520 | { |
1514 | #if EV_MULTIPLICITY |
1521 | #if EV_MULTIPLICITY |
1515 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
1522 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1516 | #endif |
1523 | #endif |
1517 | if (ev_is_active (w)) |
1524 | if (ev_is_active (w)) |
1518 | return; |
1525 | return; |
1519 | |
1526 | |
1520 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1527 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
… | |
… | |
1553 | |
1560 | |
1554 | void |
1561 | void |
1555 | ev_child_start (EV_P_ struct ev_child *w) |
1562 | ev_child_start (EV_P_ struct ev_child *w) |
1556 | { |
1563 | { |
1557 | #if EV_MULTIPLICITY |
1564 | #if EV_MULTIPLICITY |
1558 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
1565 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1559 | #endif |
1566 | #endif |
1560 | if (ev_is_active (w)) |
1567 | if (ev_is_active (w)) |
1561 | return; |
1568 | return; |
1562 | |
1569 | |
1563 | ev_start (EV_A_ (W)w, 1); |
1570 | ev_start (EV_A_ (W)w, 1); |