… | |
… | |
64 | #include <assert.h> |
64 | #include <assert.h> |
65 | #include <errno.h> |
65 | #include <errno.h> |
66 | #include <sys/types.h> |
66 | #include <sys/types.h> |
67 | #include <time.h> |
67 | #include <time.h> |
68 | |
68 | |
69 | #ifndef PERL |
|
|
70 | # include <signal.h> |
69 | #include <signal.h> |
71 | #endif |
|
|
72 | |
70 | |
73 | #ifndef WIN32 |
71 | #ifndef WIN32 |
74 | # include <unistd.h> |
72 | # include <unistd.h> |
75 | # include <sys/time.h> |
73 | # include <sys/time.h> |
76 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
… | |
… | |
150 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
151 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
152 | |
150 | |
153 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
154 | |
152 | |
155 | #if WIN32 |
153 | #include "ev_win32.c" |
156 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
157 | /* MSDN says this is required to handle SIGFPE */ |
|
|
158 | volatile double SIGFPE_REQ = 0.0f; |
|
|
159 | #endif |
|
|
160 | |
154 | |
161 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
162 | |
156 | |
163 | static void (*syserr_cb)(const char *msg); |
157 | static void (*syserr_cb)(const char *msg); |
164 | |
158 | |
… | |
… | |
274 | ev_now (EV_P) |
268 | ev_now (EV_P) |
275 | { |
269 | { |
276 | return rt_now; |
270 | return rt_now; |
277 | } |
271 | } |
278 | |
272 | |
279 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
280 | |
274 | |
281 | #define array_needsize(base,cur,cnt,init) \ |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
282 | if (expect_false ((cnt) > cur)) \ |
276 | if (expect_false ((cnt) > cur)) \ |
283 | { \ |
277 | { \ |
284 | int newcnt = cur; \ |
278 | int newcnt = cur; \ |
285 | do \ |
279 | do \ |
286 | { \ |
280 | { \ |
287 | newcnt = array_roundsize (base, newcnt << 1); \ |
281 | newcnt = array_roundsize (type, newcnt << 1); \ |
288 | } \ |
282 | } \ |
289 | while ((cnt) > newcnt); \ |
283 | while ((cnt) > newcnt); \ |
290 | \ |
284 | \ |
291 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
285 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
292 | init (base + cur, newcnt - cur); \ |
286 | init (base + cur, newcnt - cur); \ |
293 | cur = newcnt; \ |
287 | cur = newcnt; \ |
294 | } |
288 | } |
295 | |
289 | |
296 | #define array_slim(stem) \ |
290 | #define array_slim(type,stem) \ |
297 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
291 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
298 | { \ |
292 | { \ |
299 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
293 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
300 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
301 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
295 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
302 | } |
296 | } |
303 | |
297 | |
304 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
298 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
305 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
299 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
332 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
333 | return; |
327 | return; |
334 | } |
328 | } |
335 | |
329 | |
336 | w->pending = ++pendingcnt [ABSPRI (w)]; |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
337 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
338 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
339 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
340 | } |
334 | } |
341 | |
335 | |
342 | static void |
336 | static void |
… | |
… | |
397 | return; |
391 | return; |
398 | |
392 | |
399 | anfds [fd].reify = 1; |
393 | anfds [fd].reify = 1; |
400 | |
394 | |
401 | ++fdchangecnt; |
395 | ++fdchangecnt; |
402 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
396 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
403 | fdchanges [fdchangecnt - 1] = fd; |
397 | fdchanges [fdchangecnt - 1] = fd; |
404 | } |
398 | } |
405 | |
399 | |
406 | static void |
400 | static void |
407 | fd_kill (EV_P_ int fd) |
401 | fd_kill (EV_P_ int fd) |
… | |
… | |
547 | |
541 | |
548 | if (!gotsig) |
542 | if (!gotsig) |
549 | { |
543 | { |
550 | int old_errno = errno; |
544 | int old_errno = errno; |
551 | gotsig = 1; |
545 | gotsig = 1; |
|
|
546 | #ifdef WIN32 |
|
|
547 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
548 | #else |
552 | write (sigpipe [1], &signum, 1); |
549 | write (sigpipe [1], &signum, 1); |
|
|
550 | #endif |
553 | errno = old_errno; |
551 | errno = old_errno; |
554 | } |
552 | } |
555 | } |
553 | } |
556 | |
554 | |
557 | static void |
555 | static void |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
556 | sigcb (EV_P_ struct ev_io *iow, int revents) |
559 | { |
557 | { |
560 | WL w; |
558 | WL w; |
561 | int signum; |
559 | int signum; |
562 | |
560 | |
|
|
561 | #ifdef WIN32 |
|
|
562 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
563 | #else |
563 | read (sigpipe [0], &revents, 1); |
564 | read (sigpipe [0], &revents, 1); |
|
|
565 | #endif |
564 | gotsig = 0; |
566 | gotsig = 0; |
565 | |
567 | |
566 | for (signum = signalmax; signum--; ) |
568 | for (signum = signalmax; signum--; ) |
567 | if (signals [signum].gotsig) |
569 | if (signals [signum].gotsig) |
568 | { |
570 | { |
… | |
… | |
894 | if (method) |
896 | if (method) |
895 | postfork = 1; |
897 | postfork = 1; |
896 | } |
898 | } |
897 | |
899 | |
898 | /*****************************************************************************/ |
900 | /*****************************************************************************/ |
|
|
901 | |
|
|
902 | static int |
|
|
903 | any_pending (EV_P) |
|
|
904 | { |
|
|
905 | int pri; |
|
|
906 | |
|
|
907 | for (pri = NUMPRI; pri--; ) |
|
|
908 | if (pendingcnt [pri]) |
|
|
909 | return 1; |
|
|
910 | |
|
|
911 | return 0; |
|
|
912 | } |
899 | |
913 | |
900 | static void |
914 | static void |
901 | call_pending (EV_P) |
915 | call_pending (EV_P) |
902 | { |
916 | { |
903 | int pri; |
917 | int pri; |
… | |
… | |
946 | struct ev_periodic *w = periodics [0]; |
960 | struct ev_periodic *w = periodics [0]; |
947 | |
961 | |
948 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
962 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
949 | |
963 | |
950 | /* first reschedule or stop timer */ |
964 | /* first reschedule or stop timer */ |
|
|
965 | if (w->reschedule_cb) |
|
|
966 | { |
|
|
967 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
968 | |
|
|
969 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
970 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
971 | } |
951 | if (w->interval) |
972 | else if (w->interval) |
952 | { |
973 | { |
953 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
974 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
954 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
975 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
955 | downheap ((WT *)periodics, periodiccnt, 0); |
976 | downheap ((WT *)periodics, periodiccnt, 0); |
956 | } |
977 | } |
… | |
… | |
969 | /* adjust periodics after time jump */ |
990 | /* adjust periodics after time jump */ |
970 | for (i = 0; i < periodiccnt; ++i) |
991 | for (i = 0; i < periodiccnt; ++i) |
971 | { |
992 | { |
972 | struct ev_periodic *w = periodics [i]; |
993 | struct ev_periodic *w = periodics [i]; |
973 | |
994 | |
|
|
995 | if (w->reschedule_cb) |
|
|
996 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
974 | if (w->interval) |
997 | else if (w->interval) |
975 | { |
|
|
976 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
998 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
977 | |
|
|
978 | if (fabs (diff) >= 1e-4) |
|
|
979 | { |
|
|
980 | ev_periodic_stop (EV_A_ w); |
|
|
981 | ev_periodic_start (EV_A_ w); |
|
|
982 | |
|
|
983 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
984 | } |
|
|
985 | } |
|
|
986 | } |
999 | } |
|
|
1000 | |
|
|
1001 | /* now rebuild the heap */ |
|
|
1002 | for (i = periodiccnt >> 1; i--; ) |
|
|
1003 | downheap ((WT *)periodics, periodiccnt, i); |
987 | } |
1004 | } |
988 | |
1005 | |
989 | inline int |
1006 | inline int |
990 | time_update_monotonic (EV_P) |
1007 | time_update_monotonic (EV_P) |
991 | { |
1008 | { |
… | |
… | |
1087 | /* update fd-related kernel structures */ |
1104 | /* update fd-related kernel structures */ |
1088 | fd_reify (EV_A); |
1105 | fd_reify (EV_A); |
1089 | |
1106 | |
1090 | /* calculate blocking time */ |
1107 | /* calculate blocking time */ |
1091 | |
1108 | |
1092 | /* we only need this for !monotonic clockor timers, but as we basically |
1109 | /* we only need this for !monotonic clock or timers, but as we basically |
1093 | always have timers, we just calculate it always */ |
1110 | always have timers, we just calculate it always */ |
1094 | #if EV_USE_MONOTONIC |
1111 | #if EV_USE_MONOTONIC |
1095 | if (expect_true (have_monotonic)) |
1112 | if (expect_true (have_monotonic)) |
1096 | time_update_monotonic (EV_A); |
1113 | time_update_monotonic (EV_A); |
1097 | else |
1114 | else |
… | |
… | |
1130 | /* queue pending timers and reschedule them */ |
1147 | /* queue pending timers and reschedule them */ |
1131 | timers_reify (EV_A); /* relative timers called last */ |
1148 | timers_reify (EV_A); /* relative timers called last */ |
1132 | periodics_reify (EV_A); /* absolute timers called first */ |
1149 | periodics_reify (EV_A); /* absolute timers called first */ |
1133 | |
1150 | |
1134 | /* queue idle watchers unless io or timers are pending */ |
1151 | /* queue idle watchers unless io or timers are pending */ |
1135 | if (!pendingcnt) |
1152 | if (idlecnt && !any_pending (EV_A)) |
1136 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1153 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1137 | |
1154 | |
1138 | /* queue check watchers, to be executed first */ |
1155 | /* queue check watchers, to be executed first */ |
1139 | if (checkcnt) |
1156 | if (checkcnt) |
1140 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1157 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1215 | return; |
1232 | return; |
1216 | |
1233 | |
1217 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1234 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1218 | |
1235 | |
1219 | ev_start (EV_A_ (W)w, 1); |
1236 | ev_start (EV_A_ (W)w, 1); |
1220 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1237 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1221 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1238 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1222 | |
1239 | |
1223 | fd_change (EV_A_ fd); |
1240 | fd_change (EV_A_ fd); |
1224 | } |
1241 | } |
1225 | |
1242 | |
… | |
… | |
1245 | ((WT)w)->at += mn_now; |
1262 | ((WT)w)->at += mn_now; |
1246 | |
1263 | |
1247 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1264 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1248 | |
1265 | |
1249 | ev_start (EV_A_ (W)w, ++timercnt); |
1266 | ev_start (EV_A_ (W)w, ++timercnt); |
1250 | array_needsize (timers, timermax, timercnt, (void)); |
1267 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1251 | timers [timercnt - 1] = w; |
1268 | timers [timercnt - 1] = w; |
1252 | upheap ((WT *)timers, timercnt - 1); |
1269 | upheap ((WT *)timers, timercnt - 1); |
1253 | |
1270 | |
1254 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1271 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1255 | } |
1272 | } |
… | |
… | |
1295 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1312 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1296 | { |
1313 | { |
1297 | if (ev_is_active (w)) |
1314 | if (ev_is_active (w)) |
1298 | return; |
1315 | return; |
1299 | |
1316 | |
|
|
1317 | if (w->reschedule_cb) |
|
|
1318 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1319 | else if (w->interval) |
|
|
1320 | { |
1300 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1321 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1301 | |
|
|
1302 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1322 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1303 | if (w->interval) |
|
|
1304 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1323 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1324 | } |
1305 | |
1325 | |
1306 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1326 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1307 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1327 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1308 | periodics [periodiccnt - 1] = w; |
1328 | periodics [periodiccnt - 1] = w; |
1309 | upheap ((WT *)periodics, periodiccnt - 1); |
1329 | upheap ((WT *)periodics, periodiccnt - 1); |
1310 | |
1330 | |
1311 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1331 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1312 | } |
1332 | } |
… | |
… | |
1328 | |
1348 | |
1329 | ev_stop (EV_A_ (W)w); |
1349 | ev_stop (EV_A_ (W)w); |
1330 | } |
1350 | } |
1331 | |
1351 | |
1332 | void |
1352 | void |
|
|
1353 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1354 | { |
|
|
1355 | ev_periodic_stop (EV_A_ w); |
|
|
1356 | ev_periodic_start (EV_A_ w); |
|
|
1357 | } |
|
|
1358 | |
|
|
1359 | void |
1333 | ev_idle_start (EV_P_ struct ev_idle *w) |
1360 | ev_idle_start (EV_P_ struct ev_idle *w) |
1334 | { |
1361 | { |
1335 | if (ev_is_active (w)) |
1362 | if (ev_is_active (w)) |
1336 | return; |
1363 | return; |
1337 | |
1364 | |
1338 | ev_start (EV_A_ (W)w, ++idlecnt); |
1365 | ev_start (EV_A_ (W)w, ++idlecnt); |
1339 | array_needsize (idles, idlemax, idlecnt, (void)); |
1366 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1340 | idles [idlecnt - 1] = w; |
1367 | idles [idlecnt - 1] = w; |
1341 | } |
1368 | } |
1342 | |
1369 | |
1343 | void |
1370 | void |
1344 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1371 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1356 | { |
1383 | { |
1357 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1358 | return; |
1385 | return; |
1359 | |
1386 | |
1360 | ev_start (EV_A_ (W)w, ++preparecnt); |
1387 | ev_start (EV_A_ (W)w, ++preparecnt); |
1361 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1388 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1362 | prepares [preparecnt - 1] = w; |
1389 | prepares [preparecnt - 1] = w; |
1363 | } |
1390 | } |
1364 | |
1391 | |
1365 | void |
1392 | void |
1366 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1393 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1378 | { |
1405 | { |
1379 | if (ev_is_active (w)) |
1406 | if (ev_is_active (w)) |
1380 | return; |
1407 | return; |
1381 | |
1408 | |
1382 | ev_start (EV_A_ (W)w, ++checkcnt); |
1409 | ev_start (EV_A_ (W)w, ++checkcnt); |
1383 | array_needsize (checks, checkmax, checkcnt, (void)); |
1410 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1384 | checks [checkcnt - 1] = w; |
1411 | checks [checkcnt - 1] = w; |
1385 | } |
1412 | } |
1386 | |
1413 | |
1387 | void |
1414 | void |
1388 | ev_check_stop (EV_P_ struct ev_check *w) |
1415 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1409 | return; |
1436 | return; |
1410 | |
1437 | |
1411 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1438 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1412 | |
1439 | |
1413 | ev_start (EV_A_ (W)w, 1); |
1440 | ev_start (EV_A_ (W)w, 1); |
1414 | array_needsize (signals, signalmax, w->signum, signals_init); |
1441 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1415 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1442 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1416 | |
1443 | |
1417 | if (!((WL)w)->next) |
1444 | if (!((WL)w)->next) |
1418 | { |
1445 | { |
1419 | #if WIN32 |
1446 | #if WIN32 |
… | |
… | |
1502 | } |
1529 | } |
1503 | |
1530 | |
1504 | void |
1531 | void |
1505 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1532 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1506 | { |
1533 | { |
1507 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1534 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1508 | |
1535 | |
1509 | if (!once) |
1536 | if (!once) |
1510 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1537 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1511 | else |
1538 | else |
1512 | { |
1539 | { |