… | |
… | |
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 | |
… | |
… | |
149 | |
152 | |
150 | #if WIN32 |
153 | #if WIN32 |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
154 | /* note: the comment below could not be substantiated, but what would I care */ |
152 | /* MSDN says this is required to handle SIGFPE */ |
155 | /* MSDN says this is required to handle SIGFPE */ |
153 | volatile double SIGFPE_REQ = 0.0f; |
156 | volatile double SIGFPE_REQ = 0.0f; |
|
|
157 | |
|
|
158 | static int |
|
|
159 | ev_socketpair_tcp (int filedes [2]) |
|
|
160 | { |
|
|
161 | struct sockaddr_in addr = { 0 }; |
|
|
162 | int addr_size = sizeof (addr); |
|
|
163 | SOCKET listener; |
|
|
164 | SOCKET sock [2] = { -1, -1 }; |
|
|
165 | |
|
|
166 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
167 | return -1; |
|
|
168 | |
|
|
169 | addr.sin_family = AF_INET; |
|
|
170 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
|
|
171 | addr.sin_port = 0; |
|
|
172 | |
|
|
173 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
|
|
174 | goto fail; |
|
|
175 | |
|
|
176 | if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
|
|
177 | goto fail; |
|
|
178 | |
|
|
179 | if (listen (listener, 1)) |
|
|
180 | goto fail; |
|
|
181 | |
|
|
182 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
183 | goto fail; |
|
|
184 | |
|
|
185 | if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
|
|
186 | goto fail; |
|
|
187 | |
|
|
188 | if ((sock[1] = accept (listener, 0, 0)) < 0) |
|
|
189 | goto fail; |
|
|
190 | |
|
|
191 | closesocket (listener); |
|
|
192 | |
|
|
193 | filedes [0] = sock [0]; |
|
|
194 | filedes [1] = sock [1]; |
|
|
195 | |
|
|
196 | return 0; |
|
|
197 | |
|
|
198 | fail: |
|
|
199 | closesocket (listener); |
|
|
200 | |
|
|
201 | if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); |
|
|
202 | if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); |
|
|
203 | |
|
|
204 | return -1; |
|
|
205 | } |
|
|
206 | |
|
|
207 | # define ev_pipe(filedes) ev_socketpair_tcp (filedes) |
|
|
208 | #else |
|
|
209 | # define ev_pipe(filedes) pipe (filedes) |
154 | #endif |
210 | #endif |
155 | |
211 | |
156 | /*****************************************************************************/ |
212 | /*****************************************************************************/ |
157 | |
213 | |
158 | static void (*syserr_cb)(const char *msg); |
214 | static void (*syserr_cb)(const char *msg); |
… | |
… | |
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
350 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
351 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
352 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
297 | } |
353 | } |
298 | |
354 | |
|
|
355 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
356 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
357 | #define array_free_microshit(stem) \ |
|
|
358 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
359 | |
299 | #define array_free(stem, idx) \ |
360 | #define array_free(stem, idx) \ |
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
361 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
301 | |
362 | |
302 | /*****************************************************************************/ |
363 | /*****************************************************************************/ |
303 | |
364 | |
… | |
… | |
322 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
383 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
323 | return; |
384 | return; |
324 | } |
385 | } |
325 | |
386 | |
326 | w->pending = ++pendingcnt [ABSPRI (w)]; |
387 | w->pending = ++pendingcnt [ABSPRI (w)]; |
327 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
388 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
328 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
389 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
329 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
390 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
330 | } |
391 | } |
331 | |
392 | |
332 | static void |
393 | static void |
… | |
… | |
387 | return; |
448 | return; |
388 | |
449 | |
389 | anfds [fd].reify = 1; |
450 | anfds [fd].reify = 1; |
390 | |
451 | |
391 | ++fdchangecnt; |
452 | ++fdchangecnt; |
392 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
453 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
393 | fdchanges [fdchangecnt - 1] = fd; |
454 | fdchanges [fdchangecnt - 1] = fd; |
394 | } |
455 | } |
395 | |
456 | |
396 | static void |
457 | static void |
397 | fd_kill (EV_P_ int fd) |
458 | fd_kill (EV_P_ int fd) |
… | |
… | |
403 | ev_io_stop (EV_A_ w); |
464 | ev_io_stop (EV_A_ w); |
404 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
465 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
405 | } |
466 | } |
406 | } |
467 | } |
407 | |
468 | |
|
|
469 | static int |
|
|
470 | fd_valid (int fd) |
|
|
471 | { |
|
|
472 | #ifdef WIN32 |
|
|
473 | return !!win32_get_osfhandle (fd); |
|
|
474 | #else |
|
|
475 | return fcntl (fd, F_GETFD) != -1; |
|
|
476 | #endif |
|
|
477 | } |
|
|
478 | |
408 | /* called on EBADF to verify fds */ |
479 | /* called on EBADF to verify fds */ |
409 | static void |
480 | static void |
410 | fd_ebadf (EV_P) |
481 | fd_ebadf (EV_P) |
411 | { |
482 | { |
412 | int fd; |
483 | int fd; |
413 | |
484 | |
414 | for (fd = 0; fd < anfdmax; ++fd) |
485 | for (fd = 0; fd < anfdmax; ++fd) |
415 | if (anfds [fd].events) |
486 | if (anfds [fd].events) |
416 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
487 | if (!fd_valid (fd) == -1 && errno == EBADF) |
417 | fd_kill (EV_A_ fd); |
488 | fd_kill (EV_A_ fd); |
418 | } |
489 | } |
419 | |
490 | |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
491 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
421 | static void |
492 | static void |
… | |
… | |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
641 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
571 | } |
642 | } |
572 | |
643 | |
573 | /*****************************************************************************/ |
644 | /*****************************************************************************/ |
574 | |
645 | |
|
|
646 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
647 | |
575 | #ifndef WIN32 |
648 | #ifndef WIN32 |
576 | |
649 | |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
650 | static struct ev_signal childev; |
579 | |
651 | |
580 | #ifndef WCONTINUED |
652 | #ifndef WCONTINUED |
581 | # define WCONTINUED 0 |
653 | # define WCONTINUED 0 |
582 | #endif |
654 | #endif |
… | |
… | |
726 | #endif |
798 | #endif |
727 | |
799 | |
728 | for (i = NUMPRI; i--; ) |
800 | for (i = NUMPRI; i--; ) |
729 | array_free (pending, [i]); |
801 | array_free (pending, [i]); |
730 | |
802 | |
|
|
803 | /* have to use the microsoft-never-gets-it-right macro */ |
731 | array_free (fdchange, ); |
804 | array_free_microshit (fdchange); |
732 | array_free (timer, ); |
805 | array_free_microshit (timer); |
733 | array_free (periodic, ); |
806 | array_free_microshit (periodic); |
734 | array_free (idle, ); |
807 | array_free_microshit (idle); |
735 | array_free (prepare, ); |
808 | array_free_microshit (prepare); |
736 | array_free (check, ); |
809 | array_free_microshit (check); |
737 | |
810 | |
738 | method = 0; |
811 | method = 0; |
739 | } |
812 | } |
740 | |
813 | |
741 | static void |
814 | static void |
… | |
… | |
755 | ev_ref (EV_A); |
828 | ev_ref (EV_A); |
756 | ev_io_stop (EV_A_ &sigev); |
829 | ev_io_stop (EV_A_ &sigev); |
757 | close (sigpipe [0]); |
830 | close (sigpipe [0]); |
758 | close (sigpipe [1]); |
831 | close (sigpipe [1]); |
759 | |
832 | |
760 | while (pipe (sigpipe)) |
833 | while (ev_pipe (sigpipe)) |
761 | syserr ("(libev) error creating pipe"); |
834 | syserr ("(libev) error creating pipe"); |
762 | |
835 | |
763 | siginit (EV_A); |
836 | siginit (EV_A); |
764 | } |
837 | } |
765 | |
838 | |
… | |
… | |
808 | int |
881 | int |
809 | #endif |
882 | #endif |
810 | ev_default_loop (int methods) |
883 | ev_default_loop (int methods) |
811 | { |
884 | { |
812 | if (sigpipe [0] == sigpipe [1]) |
885 | if (sigpipe [0] == sigpipe [1]) |
813 | if (pipe (sigpipe)) |
886 | if (ev_pipe (sigpipe)) |
814 | return 0; |
887 | return 0; |
815 | |
888 | |
816 | if (!default_loop) |
889 | if (!default_loop) |
817 | { |
890 | { |
818 | #if EV_MULTIPLICITY |
891 | #if EV_MULTIPLICITY |
… | |
… | |
846 | { |
919 | { |
847 | #if EV_MULTIPLICITY |
920 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
921 | struct ev_loop *loop = default_loop; |
849 | #endif |
922 | #endif |
850 | |
923 | |
|
|
924 | #ifndef WIN32 |
851 | ev_ref (EV_A); /* child watcher */ |
925 | ev_ref (EV_A); /* child watcher */ |
852 | ev_signal_stop (EV_A_ &childev); |
926 | ev_signal_stop (EV_A_ &childev); |
|
|
927 | #endif |
853 | |
928 | |
854 | ev_ref (EV_A); /* signal watcher */ |
929 | ev_ref (EV_A); /* signal watcher */ |
855 | ev_io_stop (EV_A_ &sigev); |
930 | ev_io_stop (EV_A_ &sigev); |
856 | |
931 | |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
932 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
1221 | ((WT)w)->at += mn_now; |
1296 | ((WT)w)->at += mn_now; |
1222 | |
1297 | |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1298 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1224 | |
1299 | |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1227 | timers [timercnt - 1] = w; |
1302 | timers [timercnt - 1] = w; |
1228 | upheap ((WT *)timers, timercnt - 1); |
1303 | upheap ((WT *)timers, timercnt - 1); |
1229 | |
1304 | |
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1231 | } |
1306 | } |
… | |
… | |
1278 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1353 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1279 | if (w->interval) |
1354 | if (w->interval) |
1280 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1355 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1281 | |
1356 | |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1284 | periodics [periodiccnt - 1] = w; |
1359 | periodics [periodiccnt - 1] = w; |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1286 | |
1361 | |
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1288 | } |
1363 | } |
… | |
… | |
1310 | { |
1385 | { |
1311 | if (ev_is_active (w)) |
1386 | if (ev_is_active (w)) |
1312 | return; |
1387 | return; |
1313 | |
1388 | |
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1315 | array_needsize (idles, idlemax, idlecnt, ); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1316 | idles [idlecnt - 1] = w; |
1391 | idles [idlecnt - 1] = w; |
1317 | } |
1392 | } |
1318 | |
1393 | |
1319 | void |
1394 | void |
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1332 | { |
1407 | { |
1333 | if (ev_is_active (w)) |
1408 | if (ev_is_active (w)) |
1334 | return; |
1409 | return; |
1335 | |
1410 | |
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1338 | prepares [preparecnt - 1] = w; |
1413 | prepares [preparecnt - 1] = w; |
1339 | } |
1414 | } |
1340 | |
1415 | |
1341 | void |
1416 | void |
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1354 | { |
1429 | { |
1355 | if (ev_is_active (w)) |
1430 | if (ev_is_active (w)) |
1356 | return; |
1431 | return; |
1357 | |
1432 | |
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1359 | array_needsize (checks, checkmax, checkcnt, ); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1360 | checks [checkcnt - 1] = w; |
1435 | checks [checkcnt - 1] = w; |
1361 | } |
1436 | } |
1362 | |
1437 | |
1363 | void |
1438 | void |
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |