… | |
… | |
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 | |
|
|
153 | #if WIN32 |
|
|
154 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
155 | /* MSDN says this is required to handle SIGFPE */ |
|
|
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) |
|
|
210 | #endif |
|
|
211 | |
150 | /*****************************************************************************/ |
212 | /*****************************************************************************/ |
151 | |
213 | |
|
|
214 | static void (*syserr_cb)(const char *msg); |
|
|
215 | |
|
|
216 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
217 | { |
|
|
218 | syserr_cb = cb; |
|
|
219 | } |
|
|
220 | |
|
|
221 | static void |
|
|
222 | syserr (const char *msg) |
|
|
223 | { |
|
|
224 | if (!msg) |
|
|
225 | msg = "(libev) system error"; |
|
|
226 | |
|
|
227 | if (syserr_cb) |
|
|
228 | syserr_cb (msg); |
|
|
229 | else |
|
|
230 | { |
|
|
231 | perror (msg); |
|
|
232 | abort (); |
|
|
233 | } |
|
|
234 | } |
|
|
235 | |
|
|
236 | static void *(*alloc)(void *ptr, long size); |
|
|
237 | |
|
|
238 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
239 | { |
|
|
240 | alloc = cb; |
|
|
241 | } |
|
|
242 | |
|
|
243 | static void * |
|
|
244 | ev_realloc (void *ptr, long size) |
|
|
245 | { |
|
|
246 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
247 | |
|
|
248 | if (!ptr && size) |
|
|
249 | { |
|
|
250 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
251 | abort (); |
|
|
252 | } |
|
|
253 | |
|
|
254 | return ptr; |
|
|
255 | } |
|
|
256 | |
|
|
257 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
258 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
259 | |
|
|
260 | /*****************************************************************************/ |
|
|
261 | |
152 | typedef struct |
262 | typedef struct |
153 | { |
263 | { |
154 | struct ev_watcher_list *head; |
264 | WL head; |
155 | unsigned char events; |
265 | unsigned char events; |
156 | unsigned char reify; |
266 | unsigned char reify; |
157 | } ANFD; |
267 | } ANFD; |
158 | |
268 | |
159 | typedef struct |
269 | typedef struct |
… | |
… | |
217 | return rt_now; |
327 | return rt_now; |
218 | } |
328 | } |
219 | |
329 | |
220 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
330 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
221 | |
331 | |
222 | #define array_needsize(base,cur,cnt,init) \ |
332 | #define array_needsize(base,cur,cnt,init) \ |
223 | if (expect_false ((cnt) > cur)) \ |
333 | if (expect_false ((cnt) > cur)) \ |
224 | { \ |
334 | { \ |
225 | int newcnt = cur; \ |
335 | int newcnt = cur; \ |
226 | do \ |
336 | do \ |
227 | { \ |
337 | { \ |
228 | newcnt = array_roundsize (base, newcnt << 1); \ |
338 | newcnt = array_roundsize (base, newcnt << 1); \ |
229 | } \ |
339 | } \ |
230 | while ((cnt) > newcnt); \ |
340 | while ((cnt) > newcnt); \ |
231 | \ |
341 | \ |
232 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
342 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
233 | init (base + cur, newcnt - cur); \ |
343 | init (base + cur, newcnt - cur); \ |
234 | cur = newcnt; \ |
344 | cur = newcnt; \ |
235 | } |
345 | } |
|
|
346 | |
|
|
347 | #define array_slim(stem) \ |
|
|
348 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
349 | { \ |
|
|
350 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
351 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
352 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
353 | } |
|
|
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; |
236 | |
359 | |
237 | #define array_free(stem, idx) \ |
360 | #define array_free(stem, idx) \ |
238 | free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
361 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
239 | |
362 | |
240 | /*****************************************************************************/ |
363 | /*****************************************************************************/ |
241 | |
364 | |
242 | static void |
365 | static void |
243 | anfds_init (ANFD *base, int count) |
366 | anfds_init (ANFD *base, int count) |
… | |
… | |
260 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
383 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
261 | return; |
384 | return; |
262 | } |
385 | } |
263 | |
386 | |
264 | w->pending = ++pendingcnt [ABSPRI (w)]; |
387 | w->pending = ++pendingcnt [ABSPRI (w)]; |
265 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
388 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
266 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
389 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
267 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
390 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
268 | } |
391 | } |
269 | |
392 | |
270 | static void |
393 | static void |
… | |
… | |
319 | } |
442 | } |
320 | |
443 | |
321 | static void |
444 | static void |
322 | fd_change (EV_P_ int fd) |
445 | fd_change (EV_P_ int fd) |
323 | { |
446 | { |
324 | if (anfds [fd].reify || fdchangecnt < 0) |
447 | if (anfds [fd].reify) |
325 | return; |
448 | return; |
326 | |
449 | |
327 | anfds [fd].reify = 1; |
450 | anfds [fd].reify = 1; |
328 | |
451 | |
329 | ++fdchangecnt; |
452 | ++fdchangecnt; |
330 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
453 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
331 | fdchanges [fdchangecnt - 1] = fd; |
454 | fdchanges [fdchangecnt - 1] = fd; |
332 | } |
455 | } |
333 | |
456 | |
334 | static void |
457 | static void |
335 | fd_kill (EV_P_ int fd) |
458 | fd_kill (EV_P_ int fd) |
… | |
… | |
341 | ev_io_stop (EV_A_ w); |
464 | ev_io_stop (EV_A_ w); |
342 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
465 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
343 | } |
466 | } |
344 | } |
467 | } |
345 | |
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 | |
346 | /* called on EBADF to verify fds */ |
479 | /* called on EBADF to verify fds */ |
347 | static void |
480 | static void |
348 | fd_ebadf (EV_P) |
481 | fd_ebadf (EV_P) |
349 | { |
482 | { |
350 | int fd; |
483 | int fd; |
351 | |
484 | |
352 | for (fd = 0; fd < anfdmax; ++fd) |
485 | for (fd = 0; fd < anfdmax; ++fd) |
353 | if (anfds [fd].events) |
486 | if (anfds [fd].events) |
354 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
487 | if (!fd_valid (fd) == -1 && errno == EBADF) |
355 | fd_kill (EV_A_ fd); |
488 | fd_kill (EV_A_ fd); |
356 | } |
489 | } |
357 | |
490 | |
358 | /* 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 */ |
359 | static void |
492 | static void |
… | |
… | |
362 | int fd; |
495 | int fd; |
363 | |
496 | |
364 | for (fd = anfdmax; fd--; ) |
497 | for (fd = anfdmax; fd--; ) |
365 | if (anfds [fd].events) |
498 | if (anfds [fd].events) |
366 | { |
499 | { |
367 | close (fd); |
|
|
368 | fd_kill (EV_A_ fd); |
500 | fd_kill (EV_A_ fd); |
369 | return; |
501 | return; |
370 | } |
502 | } |
371 | } |
503 | } |
372 | |
504 | |
373 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
505 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
374 | static void |
506 | static void |
375 | fd_rearm_all (EV_P) |
507 | fd_rearm_all (EV_P) |
376 | { |
508 | { |
377 | int fd; |
509 | int fd; |
378 | |
510 | |
… | |
… | |
430 | |
562 | |
431 | /*****************************************************************************/ |
563 | /*****************************************************************************/ |
432 | |
564 | |
433 | typedef struct |
565 | typedef struct |
434 | { |
566 | { |
435 | struct ev_watcher_list *head; |
567 | WL head; |
436 | sig_atomic_t volatile gotsig; |
568 | sig_atomic_t volatile gotsig; |
437 | } ANSIG; |
569 | } ANSIG; |
438 | |
570 | |
439 | static ANSIG *signals; |
571 | static ANSIG *signals; |
440 | static int signalmax; |
572 | static int signalmax; |
… | |
… | |
456 | } |
588 | } |
457 | |
589 | |
458 | static void |
590 | static void |
459 | sighandler (int signum) |
591 | sighandler (int signum) |
460 | { |
592 | { |
|
|
593 | #if WIN32 |
|
|
594 | signal (signum, sighandler); |
|
|
595 | #endif |
|
|
596 | |
461 | signals [signum - 1].gotsig = 1; |
597 | signals [signum - 1].gotsig = 1; |
462 | |
598 | |
463 | if (!gotsig) |
599 | if (!gotsig) |
464 | { |
600 | { |
465 | int old_errno = errno; |
601 | int old_errno = errno; |
… | |
… | |
470 | } |
606 | } |
471 | |
607 | |
472 | static void |
608 | static void |
473 | sigcb (EV_P_ struct ev_io *iow, int revents) |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
474 | { |
610 | { |
475 | struct ev_watcher_list *w; |
611 | WL w; |
476 | int signum; |
612 | int signum; |
477 | |
613 | |
478 | read (sigpipe [0], &revents, 1); |
614 | read (sigpipe [0], &revents, 1); |
479 | gotsig = 0; |
615 | gotsig = 0; |
480 | |
616 | |
… | |
… | |
505 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
641 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
506 | } |
642 | } |
507 | |
643 | |
508 | /*****************************************************************************/ |
644 | /*****************************************************************************/ |
509 | |
645 | |
|
|
646 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
647 | |
510 | #ifndef WIN32 |
648 | #ifndef WIN32 |
511 | |
649 | |
512 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
513 | static struct ev_signal childev; |
650 | static struct ev_signal childev; |
514 | |
651 | |
515 | #ifndef WCONTINUED |
652 | #ifndef WCONTINUED |
516 | # define WCONTINUED 0 |
653 | # define WCONTINUED 0 |
517 | #endif |
654 | #endif |
… | |
… | |
631 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
768 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
632 | #endif |
769 | #endif |
633 | #if EV_USE_SELECT |
770 | #if EV_USE_SELECT |
634 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
771 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
635 | #endif |
772 | #endif |
|
|
773 | |
|
|
774 | ev_watcher_init (&sigev, sigcb); |
|
|
775 | ev_set_priority (&sigev, EV_MAXPRI); |
636 | } |
776 | } |
637 | } |
777 | } |
638 | |
778 | |
639 | void |
779 | void |
640 | loop_destroy (EV_P) |
780 | loop_destroy (EV_P) |
… | |
… | |
658 | #endif |
798 | #endif |
659 | |
799 | |
660 | for (i = NUMPRI; i--; ) |
800 | for (i = NUMPRI; i--; ) |
661 | array_free (pending, [i]); |
801 | array_free (pending, [i]); |
662 | |
802 | |
|
|
803 | /* have to use the microsoft-never-gets-it-right macro */ |
663 | array_free (fdchange, ); |
804 | array_free_microshit (fdchange); |
664 | array_free (timer, ); |
805 | array_free_microshit (timer); |
665 | array_free (periodic, ); |
806 | array_free_microshit (periodic); |
666 | array_free (idle, ); |
807 | array_free_microshit (idle); |
667 | array_free (prepare, ); |
808 | array_free_microshit (prepare); |
668 | array_free (check, ); |
809 | array_free_microshit (check); |
669 | |
810 | |
670 | method = 0; |
811 | method = 0; |
671 | /*TODO*/ |
|
|
672 | } |
812 | } |
673 | |
813 | |
674 | void |
814 | static void |
675 | loop_fork (EV_P) |
815 | loop_fork (EV_P) |
676 | { |
816 | { |
677 | /*TODO*/ |
|
|
678 | #if EV_USE_EPOLL |
817 | #if EV_USE_EPOLL |
679 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
818 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
680 | #endif |
819 | #endif |
681 | #if EV_USE_KQUEUE |
820 | #if EV_USE_KQUEUE |
682 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
821 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
683 | #endif |
822 | #endif |
|
|
823 | |
|
|
824 | if (ev_is_active (&sigev)) |
|
|
825 | { |
|
|
826 | /* default loop */ |
|
|
827 | |
|
|
828 | ev_ref (EV_A); |
|
|
829 | ev_io_stop (EV_A_ &sigev); |
|
|
830 | close (sigpipe [0]); |
|
|
831 | close (sigpipe [1]); |
|
|
832 | |
|
|
833 | while (ev_pipe (sigpipe)) |
|
|
834 | syserr ("(libev) error creating pipe"); |
|
|
835 | |
|
|
836 | siginit (EV_A); |
|
|
837 | } |
|
|
838 | |
|
|
839 | postfork = 0; |
684 | } |
840 | } |
685 | |
841 | |
686 | #if EV_MULTIPLICITY |
842 | #if EV_MULTIPLICITY |
687 | struct ev_loop * |
843 | struct ev_loop * |
688 | ev_loop_new (int methods) |
844 | ev_loop_new (int methods) |
689 | { |
845 | { |
690 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
846 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
847 | |
|
|
848 | memset (loop, 0, sizeof (struct ev_loop)); |
691 | |
849 | |
692 | loop_init (EV_A_ methods); |
850 | loop_init (EV_A_ methods); |
693 | |
851 | |
694 | if (ev_method (EV_A)) |
852 | if (ev_method (EV_A)) |
695 | return loop; |
853 | return loop; |
… | |
… | |
699 | |
857 | |
700 | void |
858 | void |
701 | ev_loop_destroy (EV_P) |
859 | ev_loop_destroy (EV_P) |
702 | { |
860 | { |
703 | loop_destroy (EV_A); |
861 | loop_destroy (EV_A); |
704 | free (loop); |
862 | ev_free (loop); |
705 | } |
863 | } |
706 | |
864 | |
707 | void |
865 | void |
708 | ev_loop_fork (EV_P) |
866 | ev_loop_fork (EV_P) |
709 | { |
867 | { |
710 | loop_fork (EV_A); |
868 | postfork = 1; |
711 | } |
869 | } |
712 | |
870 | |
713 | #endif |
871 | #endif |
714 | |
872 | |
715 | #if EV_MULTIPLICITY |
873 | #if EV_MULTIPLICITY |
… | |
… | |
723 | int |
881 | int |
724 | #endif |
882 | #endif |
725 | ev_default_loop (int methods) |
883 | ev_default_loop (int methods) |
726 | { |
884 | { |
727 | if (sigpipe [0] == sigpipe [1]) |
885 | if (sigpipe [0] == sigpipe [1]) |
728 | if (pipe (sigpipe)) |
886 | if (ev_pipe (sigpipe)) |
729 | return 0; |
887 | return 0; |
730 | |
888 | |
731 | if (!default_loop) |
889 | if (!default_loop) |
732 | { |
890 | { |
733 | #if EV_MULTIPLICITY |
891 | #if EV_MULTIPLICITY |
… | |
… | |
738 | |
896 | |
739 | loop_init (EV_A_ methods); |
897 | loop_init (EV_A_ methods); |
740 | |
898 | |
741 | if (ev_method (EV_A)) |
899 | if (ev_method (EV_A)) |
742 | { |
900 | { |
743 | ev_watcher_init (&sigev, sigcb); |
|
|
744 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
745 | siginit (EV_A); |
901 | siginit (EV_A); |
746 | |
902 | |
747 | #ifndef WIN32 |
903 | #ifndef WIN32 |
748 | ev_signal_init (&childev, childcb, SIGCHLD); |
904 | ev_signal_init (&childev, childcb, SIGCHLD); |
749 | ev_set_priority (&childev, EV_MAXPRI); |
905 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
763 | { |
919 | { |
764 | #if EV_MULTIPLICITY |
920 | #if EV_MULTIPLICITY |
765 | struct ev_loop *loop = default_loop; |
921 | struct ev_loop *loop = default_loop; |
766 | #endif |
922 | #endif |
767 | |
923 | |
|
|
924 | #ifndef WIN32 |
768 | ev_ref (EV_A); /* child watcher */ |
925 | ev_ref (EV_A); /* child watcher */ |
769 | ev_signal_stop (EV_A_ &childev); |
926 | ev_signal_stop (EV_A_ &childev); |
|
|
927 | #endif |
770 | |
928 | |
771 | ev_ref (EV_A); /* signal watcher */ |
929 | ev_ref (EV_A); /* signal watcher */ |
772 | ev_io_stop (EV_A_ &sigev); |
930 | ev_io_stop (EV_A_ &sigev); |
773 | |
931 | |
774 | close (sigpipe [0]); sigpipe [0] = 0; |
932 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
782 | { |
940 | { |
783 | #if EV_MULTIPLICITY |
941 | #if EV_MULTIPLICITY |
784 | struct ev_loop *loop = default_loop; |
942 | struct ev_loop *loop = default_loop; |
785 | #endif |
943 | #endif |
786 | |
944 | |
787 | loop_fork (EV_A); |
945 | if (method) |
788 | |
946 | postfork = 1; |
789 | ev_io_stop (EV_A_ &sigev); |
|
|
790 | close (sigpipe [0]); |
|
|
791 | close (sigpipe [1]); |
|
|
792 | pipe (sigpipe); |
|
|
793 | |
|
|
794 | ev_ref (EV_A); /* signal watcher */ |
|
|
795 | siginit (EV_A); |
|
|
796 | } |
947 | } |
797 | |
948 | |
798 | /*****************************************************************************/ |
949 | /*****************************************************************************/ |
799 | |
950 | |
800 | static void |
951 | static void |
… | |
… | |
808 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
959 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
809 | |
960 | |
810 | if (p->w) |
961 | if (p->w) |
811 | { |
962 | { |
812 | p->w->pending = 0; |
963 | p->w->pending = 0; |
813 | |
|
|
814 | ((void (*)(EV_P_ W, int))p->w->cb) (EV_A_ p->w, p->events); |
964 | p->w->cb (EV_A_ p->w, p->events); |
815 | } |
965 | } |
816 | } |
966 | } |
817 | } |
967 | } |
818 | |
968 | |
819 | static void |
969 | static void |
… | |
… | |
979 | { |
1129 | { |
980 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1130 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
981 | call_pending (EV_A); |
1131 | call_pending (EV_A); |
982 | } |
1132 | } |
983 | |
1133 | |
|
|
1134 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1135 | if (expect_false (postfork)) |
|
|
1136 | loop_fork (EV_A); |
|
|
1137 | |
984 | /* update fd-related kernel structures */ |
1138 | /* update fd-related kernel structures */ |
985 | fd_reify (EV_A); |
1139 | fd_reify (EV_A); |
986 | |
1140 | |
987 | /* calculate blocking time */ |
1141 | /* calculate blocking time */ |
988 | |
1142 | |
… | |
… | |
1142 | ((WT)w)->at += mn_now; |
1296 | ((WT)w)->at += mn_now; |
1143 | |
1297 | |
1144 | 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.)); |
1145 | |
1299 | |
1146 | ev_start (EV_A_ (W)w, ++timercnt); |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1147 | array_needsize (timers, timermax, timercnt, ); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1148 | timers [timercnt - 1] = w; |
1302 | timers [timercnt - 1] = w; |
1149 | upheap ((WT *)timers, timercnt - 1); |
1303 | upheap ((WT *)timers, timercnt - 1); |
1150 | |
1304 | |
1151 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1152 | } |
1306 | } |
… | |
… | |
1199 | /* 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 */ |
1200 | if (w->interval) |
1354 | if (w->interval) |
1201 | ((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; |
1202 | |
1356 | |
1203 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1204 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1205 | periodics [periodiccnt - 1] = w; |
1359 | periodics [periodiccnt - 1] = w; |
1206 | upheap ((WT *)periodics, periodiccnt - 1); |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1207 | |
1361 | |
1208 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1209 | } |
1363 | } |
… | |
… | |
1231 | { |
1385 | { |
1232 | if (ev_is_active (w)) |
1386 | if (ev_is_active (w)) |
1233 | return; |
1387 | return; |
1234 | |
1388 | |
1235 | ev_start (EV_A_ (W)w, ++idlecnt); |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1236 | array_needsize (idles, idlemax, idlecnt, ); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1237 | idles [idlecnt - 1] = w; |
1391 | idles [idlecnt - 1] = w; |
1238 | } |
1392 | } |
1239 | |
1393 | |
1240 | void |
1394 | void |
1241 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1253 | { |
1407 | { |
1254 | if (ev_is_active (w)) |
1408 | if (ev_is_active (w)) |
1255 | return; |
1409 | return; |
1256 | |
1410 | |
1257 | ev_start (EV_A_ (W)w, ++preparecnt); |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1258 | array_needsize (prepares, preparemax, preparecnt, ); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1259 | prepares [preparecnt - 1] = w; |
1413 | prepares [preparecnt - 1] = w; |
1260 | } |
1414 | } |
1261 | |
1415 | |
1262 | void |
1416 | void |
1263 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1275 | { |
1429 | { |
1276 | if (ev_is_active (w)) |
1430 | if (ev_is_active (w)) |
1277 | return; |
1431 | return; |
1278 | |
1432 | |
1279 | ev_start (EV_A_ (W)w, ++checkcnt); |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1280 | array_needsize (checks, checkmax, checkcnt, ); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1281 | checks [checkcnt - 1] = w; |
1435 | checks [checkcnt - 1] = w; |
1282 | } |
1436 | } |
1283 | |
1437 | |
1284 | void |
1438 | void |
1285 | ev_check_stop (EV_P_ struct ev_check *w) |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1311 | array_needsize (signals, signalmax, w->signum, signals_init); |
1465 | array_needsize (signals, signalmax, w->signum, signals_init); |
1312 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1466 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1313 | |
1467 | |
1314 | if (!((WL)w)->next) |
1468 | if (!((WL)w)->next) |
1315 | { |
1469 | { |
|
|
1470 | #if WIN32 |
|
|
1471 | signal (w->signum, sighandler); |
|
|
1472 | #else |
1316 | struct sigaction sa; |
1473 | struct sigaction sa; |
1317 | sa.sa_handler = sighandler; |
1474 | sa.sa_handler = sighandler; |
1318 | sigfillset (&sa.sa_mask); |
1475 | sigfillset (&sa.sa_mask); |
1319 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1476 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1320 | sigaction (w->signum, &sa, 0); |
1477 | sigaction (w->signum, &sa, 0); |
|
|
1478 | #endif |
1321 | } |
1479 | } |
1322 | } |
1480 | } |
1323 | |
1481 | |
1324 | void |
1482 | void |
1325 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1483 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1375 | void (*cb)(int revents, void *arg) = once->cb; |
1533 | void (*cb)(int revents, void *arg) = once->cb; |
1376 | void *arg = once->arg; |
1534 | void *arg = once->arg; |
1377 | |
1535 | |
1378 | ev_io_stop (EV_A_ &once->io); |
1536 | ev_io_stop (EV_A_ &once->io); |
1379 | ev_timer_stop (EV_A_ &once->to); |
1537 | ev_timer_stop (EV_A_ &once->to); |
1380 | free (once); |
1538 | ev_free (once); |
1381 | |
1539 | |
1382 | cb (revents, arg); |
1540 | cb (revents, arg); |
1383 | } |
1541 | } |
1384 | |
1542 | |
1385 | static void |
1543 | static void |
… | |
… | |
1395 | } |
1553 | } |
1396 | |
1554 | |
1397 | void |
1555 | void |
1398 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1556 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1399 | { |
1557 | { |
1400 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1558 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1401 | |
1559 | |
1402 | if (!once) |
1560 | if (!once) |
1403 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1561 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1404 | else |
1562 | else |
1405 | { |
1563 | { |