… | |
… | |
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 | #ifndef PERL |
|
|
70 | # include <signal.h> |
|
|
71 | #endif |
|
|
72 | |
69 | #ifndef WIN32 |
73 | #ifndef WIN32 |
|
|
74 | # include <unistd.h> |
|
|
75 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
76 | # include <sys/wait.h> |
71 | #endif |
77 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
78 | /**/ |
76 | |
79 | |
77 | #ifndef EV_USE_MONOTONIC |
80 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
81 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
82 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
97 | # define EV_USE_KQUEUE 0 |
95 | #endif |
98 | #endif |
96 | |
99 | |
97 | #ifndef EV_USE_WIN32 |
100 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
101 | # ifdef WIN32 |
|
|
102 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
103 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
104 | # define EV_USE_SELECT 1 |
100 | # else |
105 | # else |
101 | # define EV_USE_WIN32 0 |
106 | # define EV_USE_WIN32 0 |
102 | # endif |
107 | # endif |
103 | #endif |
108 | #endif |
104 | |
109 | |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
150 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
151 | typedef struct ev_watcher_time *WT; |
147 | |
152 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
153 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
154 | |
|
|
155 | #if WIN32 |
|
|
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 | |
150 | /*****************************************************************************/ |
161 | /*****************************************************************************/ |
151 | |
162 | |
|
|
163 | static void (*syserr_cb)(const char *msg); |
|
|
164 | |
|
|
165 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
166 | { |
|
|
167 | syserr_cb = cb; |
|
|
168 | } |
|
|
169 | |
|
|
170 | static void |
|
|
171 | syserr (const char *msg) |
|
|
172 | { |
|
|
173 | if (!msg) |
|
|
174 | msg = "(libev) system error"; |
|
|
175 | |
|
|
176 | if (syserr_cb) |
|
|
177 | syserr_cb (msg); |
|
|
178 | else |
|
|
179 | { |
|
|
180 | perror (msg); |
|
|
181 | abort (); |
|
|
182 | } |
|
|
183 | } |
|
|
184 | |
|
|
185 | static void *(*alloc)(void *ptr, long size); |
|
|
186 | |
|
|
187 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
188 | { |
|
|
189 | alloc = cb; |
|
|
190 | } |
|
|
191 | |
|
|
192 | static void * |
|
|
193 | ev_realloc (void *ptr, long size) |
|
|
194 | { |
|
|
195 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
196 | |
|
|
197 | if (!ptr && size) |
|
|
198 | { |
|
|
199 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
200 | abort (); |
|
|
201 | } |
|
|
202 | |
|
|
203 | return ptr; |
|
|
204 | } |
|
|
205 | |
|
|
206 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
207 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
208 | |
|
|
209 | /*****************************************************************************/ |
|
|
210 | |
152 | typedef struct |
211 | typedef struct |
153 | { |
212 | { |
154 | struct ev_watcher_list *head; |
213 | WL head; |
155 | unsigned char events; |
214 | unsigned char events; |
156 | unsigned char reify; |
215 | unsigned char reify; |
157 | } ANFD; |
216 | } ANFD; |
158 | |
217 | |
159 | typedef struct |
218 | typedef struct |
… | |
… | |
217 | return rt_now; |
276 | return rt_now; |
218 | } |
277 | } |
219 | |
278 | |
220 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
279 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
221 | |
280 | |
222 | #define array_needsize(base,cur,cnt,init) \ |
281 | #define array_needsize(base,cur,cnt,init) \ |
223 | if (expect_false ((cnt) > cur)) \ |
282 | if (expect_false ((cnt) > cur)) \ |
224 | { \ |
283 | { \ |
225 | int newcnt = cur; \ |
284 | int newcnt = cur; \ |
226 | do \ |
285 | do \ |
227 | { \ |
286 | { \ |
228 | newcnt = array_roundsize (base, newcnt << 1); \ |
287 | newcnt = array_roundsize (base, newcnt << 1); \ |
229 | } \ |
288 | } \ |
230 | while ((cnt) > newcnt); \ |
289 | while ((cnt) > newcnt); \ |
231 | \ |
290 | \ |
232 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
291 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
233 | init (base + cur, newcnt - cur); \ |
292 | init (base + cur, newcnt - cur); \ |
234 | cur = newcnt; \ |
293 | cur = newcnt; \ |
235 | } |
294 | } |
|
|
295 | |
|
|
296 | #define array_slim(stem) \ |
|
|
297 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
298 | { \ |
|
|
299 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
300 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
301 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
302 | } |
|
|
303 | |
|
|
304 | /* 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 */ |
|
|
306 | #define array_free_microshit(stem) \ |
|
|
307 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
308 | |
|
|
309 | #define array_free(stem, idx) \ |
|
|
310 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
236 | |
311 | |
237 | /*****************************************************************************/ |
312 | /*****************************************************************************/ |
238 | |
313 | |
239 | static void |
314 | static void |
240 | anfds_init (ANFD *base, int count) |
315 | anfds_init (ANFD *base, int count) |
… | |
… | |
257 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
332 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
258 | return; |
333 | return; |
259 | } |
334 | } |
260 | |
335 | |
261 | w->pending = ++pendingcnt [ABSPRI (w)]; |
336 | w->pending = ++pendingcnt [ABSPRI (w)]; |
262 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
337 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
263 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
338 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
264 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
339 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
265 | } |
340 | } |
266 | |
341 | |
267 | static void |
342 | static void |
… | |
… | |
306 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
381 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
307 | events |= w->events; |
382 | events |= w->events; |
308 | |
383 | |
309 | anfd->reify = 0; |
384 | anfd->reify = 0; |
310 | |
385 | |
311 | if (anfd->events != events) |
|
|
312 | { |
|
|
313 | method_modify (EV_A_ fd, anfd->events, events); |
386 | method_modify (EV_A_ fd, anfd->events, events); |
314 | anfd->events = events; |
387 | anfd->events = events; |
315 | } |
|
|
316 | } |
388 | } |
317 | |
389 | |
318 | fdchangecnt = 0; |
390 | fdchangecnt = 0; |
319 | } |
391 | } |
320 | |
392 | |
321 | static void |
393 | static void |
322 | fd_change (EV_P_ int fd) |
394 | fd_change (EV_P_ int fd) |
323 | { |
395 | { |
324 | if (anfds [fd].reify || fdchangecnt < 0) |
396 | if (anfds [fd].reify) |
325 | return; |
397 | return; |
326 | |
398 | |
327 | anfds [fd].reify = 1; |
399 | anfds [fd].reify = 1; |
328 | |
400 | |
329 | ++fdchangecnt; |
401 | ++fdchangecnt; |
330 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
402 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
331 | fdchanges [fdchangecnt - 1] = fd; |
403 | fdchanges [fdchangecnt - 1] = fd; |
332 | } |
404 | } |
333 | |
405 | |
334 | static void |
406 | static void |
335 | fd_kill (EV_P_ int fd) |
407 | fd_kill (EV_P_ int fd) |
… | |
… | |
341 | ev_io_stop (EV_A_ w); |
413 | ev_io_stop (EV_A_ w); |
342 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
414 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
343 | } |
415 | } |
344 | } |
416 | } |
345 | |
417 | |
|
|
418 | static int |
|
|
419 | fd_valid (int fd) |
|
|
420 | { |
|
|
421 | #ifdef WIN32 |
|
|
422 | return !!win32_get_osfhandle (fd); |
|
|
423 | #else |
|
|
424 | return fcntl (fd, F_GETFD) != -1; |
|
|
425 | #endif |
|
|
426 | } |
|
|
427 | |
346 | /* called on EBADF to verify fds */ |
428 | /* called on EBADF to verify fds */ |
347 | static void |
429 | static void |
348 | fd_ebadf (EV_P) |
430 | fd_ebadf (EV_P) |
349 | { |
431 | { |
350 | int fd; |
432 | int fd; |
351 | |
433 | |
352 | for (fd = 0; fd < anfdmax; ++fd) |
434 | for (fd = 0; fd < anfdmax; ++fd) |
353 | if (anfds [fd].events) |
435 | if (anfds [fd].events) |
354 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
436 | if (!fd_valid (fd) == -1 && errno == EBADF) |
355 | fd_kill (EV_A_ fd); |
437 | fd_kill (EV_A_ fd); |
356 | } |
438 | } |
357 | |
439 | |
358 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
440 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
359 | static void |
441 | static void |
… | |
… | |
362 | int fd; |
444 | int fd; |
363 | |
445 | |
364 | for (fd = anfdmax; fd--; ) |
446 | for (fd = anfdmax; fd--; ) |
365 | if (anfds [fd].events) |
447 | if (anfds [fd].events) |
366 | { |
448 | { |
367 | close (fd); |
|
|
368 | fd_kill (EV_A_ fd); |
449 | fd_kill (EV_A_ fd); |
369 | return; |
450 | return; |
370 | } |
451 | } |
371 | } |
452 | } |
372 | |
453 | |
373 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
454 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
374 | static void |
455 | static void |
375 | fd_rearm_all (EV_P) |
456 | fd_rearm_all (EV_P) |
376 | { |
457 | { |
377 | int fd; |
458 | int fd; |
378 | |
459 | |
… | |
… | |
430 | |
511 | |
431 | /*****************************************************************************/ |
512 | /*****************************************************************************/ |
432 | |
513 | |
433 | typedef struct |
514 | typedef struct |
434 | { |
515 | { |
435 | struct ev_watcher_list *head; |
516 | WL head; |
436 | sig_atomic_t volatile gotsig; |
517 | sig_atomic_t volatile gotsig; |
437 | } ANSIG; |
518 | } ANSIG; |
438 | |
519 | |
439 | static ANSIG *signals; |
520 | static ANSIG *signals; |
440 | static int signalmax; |
521 | static int signalmax; |
… | |
… | |
456 | } |
537 | } |
457 | |
538 | |
458 | static void |
539 | static void |
459 | sighandler (int signum) |
540 | sighandler (int signum) |
460 | { |
541 | { |
|
|
542 | #if WIN32 |
|
|
543 | signal (signum, sighandler); |
|
|
544 | #endif |
|
|
545 | |
461 | signals [signum - 1].gotsig = 1; |
546 | signals [signum - 1].gotsig = 1; |
462 | |
547 | |
463 | if (!gotsig) |
548 | if (!gotsig) |
464 | { |
549 | { |
465 | int old_errno = errno; |
550 | int old_errno = errno; |
… | |
… | |
470 | } |
555 | } |
471 | |
556 | |
472 | static void |
557 | static void |
473 | sigcb (EV_P_ struct ev_io *iow, int revents) |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
474 | { |
559 | { |
475 | struct ev_watcher_list *w; |
560 | WL w; |
476 | int signum; |
561 | int signum; |
477 | |
562 | |
478 | read (sigpipe [0], &revents, 1); |
563 | read (sigpipe [0], &revents, 1); |
479 | gotsig = 0; |
564 | gotsig = 0; |
480 | |
565 | |
… | |
… | |
505 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
590 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
506 | } |
591 | } |
507 | |
592 | |
508 | /*****************************************************************************/ |
593 | /*****************************************************************************/ |
509 | |
594 | |
|
|
595 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
596 | |
510 | #ifndef WIN32 |
597 | #ifndef WIN32 |
511 | |
598 | |
512 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
513 | static struct ev_signal childev; |
599 | static struct ev_signal childev; |
514 | |
600 | |
515 | #ifndef WCONTINUED |
601 | #ifndef WCONTINUED |
516 | # define WCONTINUED 0 |
602 | # define WCONTINUED 0 |
517 | #endif |
603 | #endif |
… | |
… | |
631 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
717 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
632 | #endif |
718 | #endif |
633 | #if EV_USE_SELECT |
719 | #if EV_USE_SELECT |
634 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
720 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
635 | #endif |
721 | #endif |
|
|
722 | |
|
|
723 | ev_watcher_init (&sigev, sigcb); |
|
|
724 | ev_set_priority (&sigev, EV_MAXPRI); |
636 | } |
725 | } |
637 | } |
726 | } |
638 | |
727 | |
639 | void |
728 | void |
640 | loop_destroy (EV_P) |
729 | loop_destroy (EV_P) |
641 | { |
730 | { |
|
|
731 | int i; |
|
|
732 | |
642 | #if EV_USE_WIN32 |
733 | #if EV_USE_WIN32 |
643 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
734 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
644 | #endif |
735 | #endif |
645 | #if EV_USE_KQUEUE |
736 | #if EV_USE_KQUEUE |
646 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
737 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
… | |
… | |
653 | #endif |
744 | #endif |
654 | #if EV_USE_SELECT |
745 | #if EV_USE_SELECT |
655 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
746 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
656 | #endif |
747 | #endif |
657 | |
748 | |
|
|
749 | for (i = NUMPRI; i--; ) |
|
|
750 | array_free (pending, [i]); |
|
|
751 | |
|
|
752 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
753 | array_free_microshit (fdchange); |
|
|
754 | array_free_microshit (timer); |
|
|
755 | array_free_microshit (periodic); |
|
|
756 | array_free_microshit (idle); |
|
|
757 | array_free_microshit (prepare); |
|
|
758 | array_free_microshit (check); |
|
|
759 | |
658 | method = 0; |
760 | method = 0; |
659 | /*TODO*/ |
|
|
660 | } |
761 | } |
661 | |
762 | |
662 | void |
763 | static void |
663 | loop_fork (EV_P) |
764 | loop_fork (EV_P) |
664 | { |
765 | { |
665 | /*TODO*/ |
|
|
666 | #if EV_USE_EPOLL |
766 | #if EV_USE_EPOLL |
667 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
767 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
668 | #endif |
768 | #endif |
669 | #if EV_USE_KQUEUE |
769 | #if EV_USE_KQUEUE |
670 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
770 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
671 | #endif |
771 | #endif |
|
|
772 | |
|
|
773 | if (ev_is_active (&sigev)) |
|
|
774 | { |
|
|
775 | /* default loop */ |
|
|
776 | |
|
|
777 | ev_ref (EV_A); |
|
|
778 | ev_io_stop (EV_A_ &sigev); |
|
|
779 | close (sigpipe [0]); |
|
|
780 | close (sigpipe [1]); |
|
|
781 | |
|
|
782 | while (pipe (sigpipe)) |
|
|
783 | syserr ("(libev) error creating pipe"); |
|
|
784 | |
|
|
785 | siginit (EV_A); |
|
|
786 | } |
|
|
787 | |
|
|
788 | postfork = 0; |
672 | } |
789 | } |
673 | |
790 | |
674 | #if EV_MULTIPLICITY |
791 | #if EV_MULTIPLICITY |
675 | struct ev_loop * |
792 | struct ev_loop * |
676 | ev_loop_new (int methods) |
793 | ev_loop_new (int methods) |
677 | { |
794 | { |
678 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
795 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
796 | |
|
|
797 | memset (loop, 0, sizeof (struct ev_loop)); |
679 | |
798 | |
680 | loop_init (EV_A_ methods); |
799 | loop_init (EV_A_ methods); |
681 | |
800 | |
682 | if (ev_method (EV_A)) |
801 | if (ev_method (EV_A)) |
683 | return loop; |
802 | return loop; |
… | |
… | |
687 | |
806 | |
688 | void |
807 | void |
689 | ev_loop_destroy (EV_P) |
808 | ev_loop_destroy (EV_P) |
690 | { |
809 | { |
691 | loop_destroy (EV_A); |
810 | loop_destroy (EV_A); |
692 | free (loop); |
811 | ev_free (loop); |
693 | } |
812 | } |
694 | |
813 | |
695 | void |
814 | void |
696 | ev_loop_fork (EV_P) |
815 | ev_loop_fork (EV_P) |
697 | { |
816 | { |
698 | loop_fork (EV_A); |
817 | postfork = 1; |
699 | } |
818 | } |
700 | |
819 | |
701 | #endif |
820 | #endif |
702 | |
821 | |
703 | #if EV_MULTIPLICITY |
822 | #if EV_MULTIPLICITY |
… | |
… | |
726 | |
845 | |
727 | loop_init (EV_A_ methods); |
846 | loop_init (EV_A_ methods); |
728 | |
847 | |
729 | if (ev_method (EV_A)) |
848 | if (ev_method (EV_A)) |
730 | { |
849 | { |
731 | ev_watcher_init (&sigev, sigcb); |
|
|
732 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
733 | siginit (EV_A); |
850 | siginit (EV_A); |
734 | |
851 | |
735 | #ifndef WIN32 |
852 | #ifndef WIN32 |
736 | ev_signal_init (&childev, childcb, SIGCHLD); |
853 | ev_signal_init (&childev, childcb, SIGCHLD); |
737 | ev_set_priority (&childev, EV_MAXPRI); |
854 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
751 | { |
868 | { |
752 | #if EV_MULTIPLICITY |
869 | #if EV_MULTIPLICITY |
753 | struct ev_loop *loop = default_loop; |
870 | struct ev_loop *loop = default_loop; |
754 | #endif |
871 | #endif |
755 | |
872 | |
|
|
873 | #ifndef WIN32 |
756 | ev_ref (EV_A); /* child watcher */ |
874 | ev_ref (EV_A); /* child watcher */ |
757 | ev_signal_stop (EV_A_ &childev); |
875 | ev_signal_stop (EV_A_ &childev); |
|
|
876 | #endif |
758 | |
877 | |
759 | ev_ref (EV_A); /* signal watcher */ |
878 | ev_ref (EV_A); /* signal watcher */ |
760 | ev_io_stop (EV_A_ &sigev); |
879 | ev_io_stop (EV_A_ &sigev); |
761 | |
880 | |
762 | close (sigpipe [0]); sigpipe [0] = 0; |
881 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
770 | { |
889 | { |
771 | #if EV_MULTIPLICITY |
890 | #if EV_MULTIPLICITY |
772 | struct ev_loop *loop = default_loop; |
891 | struct ev_loop *loop = default_loop; |
773 | #endif |
892 | #endif |
774 | |
893 | |
775 | loop_fork (EV_A); |
894 | if (method) |
776 | |
895 | postfork = 1; |
777 | ev_io_stop (EV_A_ &sigev); |
|
|
778 | close (sigpipe [0]); |
|
|
779 | close (sigpipe [1]); |
|
|
780 | pipe (sigpipe); |
|
|
781 | |
|
|
782 | ev_ref (EV_A); /* signal watcher */ |
|
|
783 | siginit (EV_A); |
|
|
784 | } |
896 | } |
785 | |
897 | |
786 | /*****************************************************************************/ |
898 | /*****************************************************************************/ |
787 | |
899 | |
788 | static void |
900 | static void |
… | |
… | |
796 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
908 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
797 | |
909 | |
798 | if (p->w) |
910 | if (p->w) |
799 | { |
911 | { |
800 | p->w->pending = 0; |
912 | p->w->pending = 0; |
801 | |
|
|
802 | (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); |
913 | p->w->cb (EV_A_ p->w, p->events); |
803 | } |
914 | } |
804 | } |
915 | } |
805 | } |
916 | } |
806 | |
917 | |
807 | static void |
918 | static void |
… | |
… | |
967 | { |
1078 | { |
968 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1079 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
969 | call_pending (EV_A); |
1080 | call_pending (EV_A); |
970 | } |
1081 | } |
971 | |
1082 | |
|
|
1083 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1084 | if (expect_false (postfork)) |
|
|
1085 | loop_fork (EV_A); |
|
|
1086 | |
972 | /* update fd-related kernel structures */ |
1087 | /* update fd-related kernel structures */ |
973 | fd_reify (EV_A); |
1088 | fd_reify (EV_A); |
974 | |
1089 | |
975 | /* calculate blocking time */ |
1090 | /* calculate blocking time */ |
976 | |
1091 | |
… | |
… | |
1130 | ((WT)w)->at += mn_now; |
1245 | ((WT)w)->at += mn_now; |
1131 | |
1246 | |
1132 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1247 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1133 | |
1248 | |
1134 | ev_start (EV_A_ (W)w, ++timercnt); |
1249 | ev_start (EV_A_ (W)w, ++timercnt); |
1135 | array_needsize (timers, timermax, timercnt, ); |
1250 | array_needsize (timers, timermax, timercnt, (void)); |
1136 | timers [timercnt - 1] = w; |
1251 | timers [timercnt - 1] = w; |
1137 | upheap ((WT *)timers, timercnt - 1); |
1252 | upheap ((WT *)timers, timercnt - 1); |
1138 | |
1253 | |
1139 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1254 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1140 | } |
1255 | } |
… | |
… | |
1187 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1302 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1188 | if (w->interval) |
1303 | if (w->interval) |
1189 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1304 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1190 | |
1305 | |
1191 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1306 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1192 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1307 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1193 | periodics [periodiccnt - 1] = w; |
1308 | periodics [periodiccnt - 1] = w; |
1194 | upheap ((WT *)periodics, periodiccnt - 1); |
1309 | upheap ((WT *)periodics, periodiccnt - 1); |
1195 | |
1310 | |
1196 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1311 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1197 | } |
1312 | } |
… | |
… | |
1219 | { |
1334 | { |
1220 | if (ev_is_active (w)) |
1335 | if (ev_is_active (w)) |
1221 | return; |
1336 | return; |
1222 | |
1337 | |
1223 | ev_start (EV_A_ (W)w, ++idlecnt); |
1338 | ev_start (EV_A_ (W)w, ++idlecnt); |
1224 | array_needsize (idles, idlemax, idlecnt, ); |
1339 | array_needsize (idles, idlemax, idlecnt, (void)); |
1225 | idles [idlecnt - 1] = w; |
1340 | idles [idlecnt - 1] = w; |
1226 | } |
1341 | } |
1227 | |
1342 | |
1228 | void |
1343 | void |
1229 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1344 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1241 | { |
1356 | { |
1242 | if (ev_is_active (w)) |
1357 | if (ev_is_active (w)) |
1243 | return; |
1358 | return; |
1244 | |
1359 | |
1245 | ev_start (EV_A_ (W)w, ++preparecnt); |
1360 | ev_start (EV_A_ (W)w, ++preparecnt); |
1246 | array_needsize (prepares, preparemax, preparecnt, ); |
1361 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1247 | prepares [preparecnt - 1] = w; |
1362 | prepares [preparecnt - 1] = w; |
1248 | } |
1363 | } |
1249 | |
1364 | |
1250 | void |
1365 | void |
1251 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1366 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1263 | { |
1378 | { |
1264 | if (ev_is_active (w)) |
1379 | if (ev_is_active (w)) |
1265 | return; |
1380 | return; |
1266 | |
1381 | |
1267 | ev_start (EV_A_ (W)w, ++checkcnt); |
1382 | ev_start (EV_A_ (W)w, ++checkcnt); |
1268 | array_needsize (checks, checkmax, checkcnt, ); |
1383 | array_needsize (checks, checkmax, checkcnt, (void)); |
1269 | checks [checkcnt - 1] = w; |
1384 | checks [checkcnt - 1] = w; |
1270 | } |
1385 | } |
1271 | |
1386 | |
1272 | void |
1387 | void |
1273 | ev_check_stop (EV_P_ struct ev_check *w) |
1388 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1299 | array_needsize (signals, signalmax, w->signum, signals_init); |
1414 | array_needsize (signals, signalmax, w->signum, signals_init); |
1300 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1415 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1301 | |
1416 | |
1302 | if (!((WL)w)->next) |
1417 | if (!((WL)w)->next) |
1303 | { |
1418 | { |
|
|
1419 | #if WIN32 |
|
|
1420 | signal (w->signum, sighandler); |
|
|
1421 | #else |
1304 | struct sigaction sa; |
1422 | struct sigaction sa; |
1305 | sa.sa_handler = sighandler; |
1423 | sa.sa_handler = sighandler; |
1306 | sigfillset (&sa.sa_mask); |
1424 | sigfillset (&sa.sa_mask); |
1307 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1425 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1308 | sigaction (w->signum, &sa, 0); |
1426 | sigaction (w->signum, &sa, 0); |
|
|
1427 | #endif |
1309 | } |
1428 | } |
1310 | } |
1429 | } |
1311 | |
1430 | |
1312 | void |
1431 | void |
1313 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1432 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1363 | void (*cb)(int revents, void *arg) = once->cb; |
1482 | void (*cb)(int revents, void *arg) = once->cb; |
1364 | void *arg = once->arg; |
1483 | void *arg = once->arg; |
1365 | |
1484 | |
1366 | ev_io_stop (EV_A_ &once->io); |
1485 | ev_io_stop (EV_A_ &once->io); |
1367 | ev_timer_stop (EV_A_ &once->to); |
1486 | ev_timer_stop (EV_A_ &once->to); |
1368 | free (once); |
1487 | ev_free (once); |
1369 | |
1488 | |
1370 | cb (revents, arg); |
1489 | cb (revents, arg); |
1371 | } |
1490 | } |
1372 | |
1491 | |
1373 | static void |
1492 | static void |
… | |
… | |
1383 | } |
1502 | } |
1384 | |
1503 | |
1385 | void |
1504 | void |
1386 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1505 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1387 | { |
1506 | { |
1388 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1507 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1389 | |
1508 | |
1390 | if (!once) |
1509 | if (!once) |
1391 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1510 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1392 | else |
1511 | else |
1393 | { |
1512 | { |