… | |
… | |
54 | |
54 | |
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | #include <math.h> |
57 | #include <math.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
59 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
60 | #include <stddef.h> |
63 | |
61 | |
64 | #include <stdio.h> |
62 | #include <stdio.h> |
65 | |
63 | |
66 | #include <assert.h> |
64 | #include <assert.h> |
67 | #include <errno.h> |
65 | #include <errno.h> |
68 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #include <signal.h> |
|
|
70 | |
69 | #ifndef WIN32 |
71 | #ifndef WIN32 |
|
|
72 | # include <unistd.h> |
|
|
73 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
71 | #endif |
75 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
76 | /**/ |
76 | |
77 | |
77 | #ifndef EV_USE_MONOTONIC |
78 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
79 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
80 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
95 | # define EV_USE_KQUEUE 0 |
95 | #endif |
96 | #endif |
96 | |
97 | |
97 | #ifndef EV_USE_WIN32 |
98 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
99 | # ifdef WIN32 |
|
|
100 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
101 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
102 | # define EV_USE_SELECT 1 |
100 | # else |
103 | # else |
101 | # define EV_USE_WIN32 0 |
104 | # define EV_USE_WIN32 0 |
102 | # endif |
105 | # endif |
103 | #endif |
106 | #endif |
104 | |
107 | |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
147 | |
150 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
152 | |
150 | #if WIN32 |
153 | #include "ev_win32.c" |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
154 | |
156 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
157 | |
156 | |
158 | static void (*syserr_cb)(const char *msg); |
157 | static void (*syserr_cb)(const char *msg); |
159 | |
158 | |
… | |
… | |
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
293 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
294 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
295 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
297 | } |
296 | } |
298 | |
297 | |
|
|
298 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
299 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
300 | #define array_free_microshit(stem) \ |
|
|
301 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
302 | |
299 | #define array_free(stem, idx) \ |
303 | #define array_free(stem, idx) \ |
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
304 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
301 | |
305 | |
302 | /*****************************************************************************/ |
306 | /*****************************************************************************/ |
303 | |
307 | |
… | |
… | |
322 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
323 | return; |
327 | return; |
324 | } |
328 | } |
325 | |
329 | |
326 | w->pending = ++pendingcnt [ABSPRI (w)]; |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
327 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
331 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
328 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
329 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
330 | } |
334 | } |
331 | |
335 | |
332 | static void |
336 | static void |
… | |
… | |
387 | return; |
391 | return; |
388 | |
392 | |
389 | anfds [fd].reify = 1; |
393 | anfds [fd].reify = 1; |
390 | |
394 | |
391 | ++fdchangecnt; |
395 | ++fdchangecnt; |
392 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
396 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
393 | fdchanges [fdchangecnt - 1] = fd; |
397 | fdchanges [fdchangecnt - 1] = fd; |
394 | } |
398 | } |
395 | |
399 | |
396 | static void |
400 | static void |
397 | fd_kill (EV_P_ int fd) |
401 | fd_kill (EV_P_ int fd) |
… | |
… | |
403 | ev_io_stop (EV_A_ w); |
407 | ev_io_stop (EV_A_ w); |
404 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
405 | } |
409 | } |
406 | } |
410 | } |
407 | |
411 | |
|
|
412 | static int |
|
|
413 | fd_valid (int fd) |
|
|
414 | { |
|
|
415 | #ifdef WIN32 |
|
|
416 | return !!win32_get_osfhandle (fd); |
|
|
417 | #else |
|
|
418 | return fcntl (fd, F_GETFD) != -1; |
|
|
419 | #endif |
|
|
420 | } |
|
|
421 | |
408 | /* called on EBADF to verify fds */ |
422 | /* called on EBADF to verify fds */ |
409 | static void |
423 | static void |
410 | fd_ebadf (EV_P) |
424 | fd_ebadf (EV_P) |
411 | { |
425 | { |
412 | int fd; |
426 | int fd; |
413 | |
427 | |
414 | for (fd = 0; fd < anfdmax; ++fd) |
428 | for (fd = 0; fd < anfdmax; ++fd) |
415 | if (anfds [fd].events) |
429 | if (anfds [fd].events) |
416 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
430 | if (!fd_valid (fd) == -1 && errno == EBADF) |
417 | fd_kill (EV_A_ fd); |
431 | fd_kill (EV_A_ fd); |
418 | } |
432 | } |
419 | |
433 | |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
434 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
421 | static void |
435 | static void |
… | |
… | |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
584 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
571 | } |
585 | } |
572 | |
586 | |
573 | /*****************************************************************************/ |
587 | /*****************************************************************************/ |
574 | |
588 | |
|
|
589 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
590 | |
575 | #ifndef WIN32 |
591 | #ifndef WIN32 |
576 | |
592 | |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
593 | static struct ev_signal childev; |
579 | |
594 | |
580 | #ifndef WCONTINUED |
595 | #ifndef WCONTINUED |
581 | # define WCONTINUED 0 |
596 | # define WCONTINUED 0 |
582 | #endif |
597 | #endif |
… | |
… | |
726 | #endif |
741 | #endif |
727 | |
742 | |
728 | for (i = NUMPRI; i--; ) |
743 | for (i = NUMPRI; i--; ) |
729 | array_free (pending, [i]); |
744 | array_free (pending, [i]); |
730 | |
745 | |
|
|
746 | /* have to use the microsoft-never-gets-it-right macro */ |
731 | array_free (fdchange, ); |
747 | array_free_microshit (fdchange); |
732 | array_free (timer, ); |
748 | array_free_microshit (timer); |
733 | array_free (periodic, ); |
749 | array_free_microshit (periodic); |
734 | array_free (idle, ); |
750 | array_free_microshit (idle); |
735 | array_free (prepare, ); |
751 | array_free_microshit (prepare); |
736 | array_free (check, ); |
752 | array_free_microshit (check); |
737 | |
753 | |
738 | method = 0; |
754 | method = 0; |
739 | } |
755 | } |
740 | |
756 | |
741 | static void |
757 | static void |
… | |
… | |
846 | { |
862 | { |
847 | #if EV_MULTIPLICITY |
863 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
864 | struct ev_loop *loop = default_loop; |
849 | #endif |
865 | #endif |
850 | |
866 | |
|
|
867 | #ifndef WIN32 |
851 | ev_ref (EV_A); /* child watcher */ |
868 | ev_ref (EV_A); /* child watcher */ |
852 | ev_signal_stop (EV_A_ &childev); |
869 | ev_signal_stop (EV_A_ &childev); |
|
|
870 | #endif |
853 | |
871 | |
854 | ev_ref (EV_A); /* signal watcher */ |
872 | ev_ref (EV_A); /* signal watcher */ |
855 | ev_io_stop (EV_A_ &sigev); |
873 | ev_io_stop (EV_A_ &sigev); |
856 | |
874 | |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
875 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
1221 | ((WT)w)->at += mn_now; |
1239 | ((WT)w)->at += mn_now; |
1222 | |
1240 | |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1241 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1224 | |
1242 | |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1243 | ev_start (EV_A_ (W)w, ++timercnt); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1244 | array_needsize (timers, timermax, timercnt, (void)); |
1227 | timers [timercnt - 1] = w; |
1245 | timers [timercnt - 1] = w; |
1228 | upheap ((WT *)timers, timercnt - 1); |
1246 | upheap ((WT *)timers, timercnt - 1); |
1229 | |
1247 | |
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1248 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1231 | } |
1249 | } |
… | |
… | |
1278 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1296 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1279 | if (w->interval) |
1297 | if (w->interval) |
1280 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1298 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1281 | |
1299 | |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1300 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1301 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1284 | periodics [periodiccnt - 1] = w; |
1302 | periodics [periodiccnt - 1] = w; |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
1303 | upheap ((WT *)periodics, periodiccnt - 1); |
1286 | |
1304 | |
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1305 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1288 | } |
1306 | } |
… | |
… | |
1310 | { |
1328 | { |
1311 | if (ev_is_active (w)) |
1329 | if (ev_is_active (w)) |
1312 | return; |
1330 | return; |
1313 | |
1331 | |
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
1332 | ev_start (EV_A_ (W)w, ++idlecnt); |
1315 | array_needsize (idles, idlemax, idlecnt, ); |
1333 | array_needsize (idles, idlemax, idlecnt, (void)); |
1316 | idles [idlecnt - 1] = w; |
1334 | idles [idlecnt - 1] = w; |
1317 | } |
1335 | } |
1318 | |
1336 | |
1319 | void |
1337 | void |
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1338 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1332 | { |
1350 | { |
1333 | if (ev_is_active (w)) |
1351 | if (ev_is_active (w)) |
1334 | return; |
1352 | return; |
1335 | |
1353 | |
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
1354 | ev_start (EV_A_ (W)w, ++preparecnt); |
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
1355 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1338 | prepares [preparecnt - 1] = w; |
1356 | prepares [preparecnt - 1] = w; |
1339 | } |
1357 | } |
1340 | |
1358 | |
1341 | void |
1359 | void |
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1360 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1354 | { |
1372 | { |
1355 | if (ev_is_active (w)) |
1373 | if (ev_is_active (w)) |
1356 | return; |
1374 | return; |
1357 | |
1375 | |
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
1376 | ev_start (EV_A_ (W)w, ++checkcnt); |
1359 | array_needsize (checks, checkmax, checkcnt, ); |
1377 | array_needsize (checks, checkmax, checkcnt, (void)); |
1360 | checks [checkcnt - 1] = w; |
1378 | checks [checkcnt - 1] = w; |
1361 | } |
1379 | } |
1362 | |
1380 | |
1363 | void |
1381 | void |
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
1382 | ev_check_stop (EV_P_ struct ev_check *w) |