… | |
… | |
92 | |
92 | |
93 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
94 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
95 | #endif |
95 | #endif |
96 | |
96 | |
|
|
97 | #ifndef EV_USE_WIN32 |
|
|
98 | # ifdef WIN32 |
|
|
99 | # define EV_USE_WIN32 1 |
|
|
100 | # else |
|
|
101 | # define EV_USE_WIN32 0 |
|
|
102 | # endif |
|
|
103 | #endif |
|
|
104 | |
97 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
98 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
99 | #endif |
107 | #endif |
100 | |
108 | |
101 | /**/ |
109 | /**/ |
… | |
… | |
137 | typedef struct ev_watcher_list *WL; |
145 | typedef struct ev_watcher_list *WL; |
138 | typedef struct ev_watcher_time *WT; |
146 | typedef struct ev_watcher_time *WT; |
139 | |
147 | |
140 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
141 | |
149 | |
|
|
150 | #if WIN32 |
|
|
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 | |
142 | /*****************************************************************************/ |
156 | /*****************************************************************************/ |
143 | |
157 | |
|
|
158 | static void (*syserr_cb)(const char *msg); |
|
|
159 | |
|
|
160 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
161 | { |
|
|
162 | syserr_cb = cb; |
|
|
163 | } |
|
|
164 | |
|
|
165 | static void |
|
|
166 | syserr (const char *msg) |
|
|
167 | { |
|
|
168 | if (!msg) |
|
|
169 | msg = "(libev) system error"; |
|
|
170 | |
|
|
171 | if (syserr_cb) |
|
|
172 | syserr_cb (msg); |
|
|
173 | else |
|
|
174 | { |
|
|
175 | perror (msg); |
|
|
176 | abort (); |
|
|
177 | } |
|
|
178 | } |
|
|
179 | |
|
|
180 | static void *(*alloc)(void *ptr, long size); |
|
|
181 | |
|
|
182 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
183 | { |
|
|
184 | alloc = cb; |
|
|
185 | } |
|
|
186 | |
|
|
187 | static void * |
|
|
188 | ev_realloc (void *ptr, long size) |
|
|
189 | { |
|
|
190 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
191 | |
|
|
192 | if (!ptr && size) |
|
|
193 | { |
|
|
194 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
195 | abort (); |
|
|
196 | } |
|
|
197 | |
|
|
198 | return ptr; |
|
|
199 | } |
|
|
200 | |
|
|
201 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
202 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
203 | |
|
|
204 | /*****************************************************************************/ |
|
|
205 | |
144 | typedef struct |
206 | typedef struct |
145 | { |
207 | { |
146 | struct ev_watcher_list *head; |
208 | WL head; |
147 | unsigned char events; |
209 | unsigned char events; |
148 | unsigned char reify; |
210 | unsigned char reify; |
149 | } ANFD; |
211 | } ANFD; |
150 | |
212 | |
151 | typedef struct |
213 | typedef struct |
… | |
… | |
209 | return rt_now; |
271 | return rt_now; |
210 | } |
272 | } |
211 | |
273 | |
212 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
274 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
213 | |
275 | |
214 | #define array_needsize(base,cur,cnt,init) \ |
276 | #define array_needsize(base,cur,cnt,init) \ |
215 | if (expect_false ((cnt) > cur)) \ |
277 | if (expect_false ((cnt) > cur)) \ |
216 | { \ |
278 | { \ |
217 | int newcnt = cur; \ |
279 | int newcnt = cur; \ |
218 | do \ |
280 | do \ |
219 | { \ |
281 | { \ |
220 | newcnt = array_roundsize (base, newcnt << 1); \ |
282 | newcnt = array_roundsize (base, newcnt << 1); \ |
221 | } \ |
283 | } \ |
222 | while ((cnt) > newcnt); \ |
284 | while ((cnt) > newcnt); \ |
223 | \ |
285 | \ |
224 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
286 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
225 | init (base + cur, newcnt - cur); \ |
287 | init (base + cur, newcnt - cur); \ |
226 | cur = newcnt; \ |
288 | cur = newcnt; \ |
227 | } |
289 | } |
|
|
290 | |
|
|
291 | #define array_slim(stem) \ |
|
|
292 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
293 | { \ |
|
|
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
297 | } |
|
|
298 | |
|
|
299 | #define array_free(stem, idx) \ |
|
|
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
228 | |
301 | |
229 | /*****************************************************************************/ |
302 | /*****************************************************************************/ |
230 | |
303 | |
231 | static void |
304 | static void |
232 | anfds_init (ANFD *base, int count) |
305 | anfds_init (ANFD *base, int count) |
… | |
… | |
298 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
371 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
299 | events |= w->events; |
372 | events |= w->events; |
300 | |
373 | |
301 | anfd->reify = 0; |
374 | anfd->reify = 0; |
302 | |
375 | |
303 | if (anfd->events != events) |
|
|
304 | { |
|
|
305 | method_modify (EV_A_ fd, anfd->events, events); |
376 | method_modify (EV_A_ fd, anfd->events, events); |
306 | anfd->events = events; |
377 | anfd->events = events; |
307 | } |
|
|
308 | } |
378 | } |
309 | |
379 | |
310 | fdchangecnt = 0; |
380 | fdchangecnt = 0; |
311 | } |
381 | } |
312 | |
382 | |
313 | static void |
383 | static void |
314 | fd_change (EV_P_ int fd) |
384 | fd_change (EV_P_ int fd) |
315 | { |
385 | { |
316 | if (anfds [fd].reify || fdchangecnt < 0) |
386 | if (anfds [fd].reify) |
317 | return; |
387 | return; |
318 | |
388 | |
319 | anfds [fd].reify = 1; |
389 | anfds [fd].reify = 1; |
320 | |
390 | |
321 | ++fdchangecnt; |
391 | ++fdchangecnt; |
… | |
… | |
349 | |
419 | |
350 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
351 | static void |
421 | static void |
352 | fd_enomem (EV_P) |
422 | fd_enomem (EV_P) |
353 | { |
423 | { |
354 | int fd = anfdmax; |
424 | int fd; |
355 | |
425 | |
356 | while (fd--) |
426 | for (fd = anfdmax; fd--; ) |
357 | if (anfds [fd].events) |
427 | if (anfds [fd].events) |
358 | { |
428 | { |
359 | close (fd); |
|
|
360 | fd_kill (EV_A_ fd); |
429 | fd_kill (EV_A_ fd); |
361 | return; |
430 | return; |
362 | } |
431 | } |
363 | } |
432 | } |
364 | |
433 | |
365 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
434 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
366 | static void |
435 | static void |
367 | fd_rearm_all (EV_P) |
436 | fd_rearm_all (EV_P) |
368 | { |
437 | { |
369 | int fd; |
438 | int fd; |
370 | |
439 | |
… | |
… | |
385 | WT w = heap [k]; |
454 | WT w = heap [k]; |
386 | |
455 | |
387 | while (k && heap [k >> 1]->at > w->at) |
456 | while (k && heap [k >> 1]->at > w->at) |
388 | { |
457 | { |
389 | heap [k] = heap [k >> 1]; |
458 | heap [k] = heap [k >> 1]; |
390 | heap [k]->active = k + 1; |
459 | ((W)heap [k])->active = k + 1; |
391 | k >>= 1; |
460 | k >>= 1; |
392 | } |
461 | } |
393 | |
462 | |
394 | heap [k] = w; |
463 | heap [k] = w; |
395 | heap [k]->active = k + 1; |
464 | ((W)heap [k])->active = k + 1; |
396 | |
465 | |
397 | } |
466 | } |
398 | |
467 | |
399 | static void |
468 | static void |
400 | downheap (WT *heap, int N, int k) |
469 | downheap (WT *heap, int N, int k) |
… | |
… | |
410 | |
479 | |
411 | if (w->at <= heap [j]->at) |
480 | if (w->at <= heap [j]->at) |
412 | break; |
481 | break; |
413 | |
482 | |
414 | heap [k] = heap [j]; |
483 | heap [k] = heap [j]; |
415 | heap [k]->active = k + 1; |
484 | ((W)heap [k])->active = k + 1; |
416 | k = j; |
485 | k = j; |
417 | } |
486 | } |
418 | |
487 | |
419 | heap [k] = w; |
488 | heap [k] = w; |
420 | heap [k]->active = k + 1; |
489 | ((W)heap [k])->active = k + 1; |
421 | } |
490 | } |
422 | |
491 | |
423 | /*****************************************************************************/ |
492 | /*****************************************************************************/ |
424 | |
493 | |
425 | typedef struct |
494 | typedef struct |
426 | { |
495 | { |
427 | struct ev_watcher_list *head; |
496 | WL head; |
428 | sig_atomic_t volatile gotsig; |
497 | sig_atomic_t volatile gotsig; |
429 | } ANSIG; |
498 | } ANSIG; |
430 | |
499 | |
431 | static ANSIG *signals; |
500 | static ANSIG *signals; |
432 | static int signalmax; |
501 | static int signalmax; |
… | |
… | |
448 | } |
517 | } |
449 | |
518 | |
450 | static void |
519 | static void |
451 | sighandler (int signum) |
520 | sighandler (int signum) |
452 | { |
521 | { |
|
|
522 | #if WIN32 |
|
|
523 | signal (signum, sighandler); |
|
|
524 | #endif |
|
|
525 | |
453 | signals [signum - 1].gotsig = 1; |
526 | signals [signum - 1].gotsig = 1; |
454 | |
527 | |
455 | if (!gotsig) |
528 | if (!gotsig) |
456 | { |
529 | { |
457 | int old_errno = errno; |
530 | int old_errno = errno; |
… | |
… | |
462 | } |
535 | } |
463 | |
536 | |
464 | static void |
537 | static void |
465 | sigcb (EV_P_ struct ev_io *iow, int revents) |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
466 | { |
539 | { |
467 | struct ev_watcher_list *w; |
540 | WL w; |
468 | int signum; |
541 | int signum; |
469 | |
542 | |
470 | read (sigpipe [0], &revents, 1); |
543 | read (sigpipe [0], &revents, 1); |
471 | gotsig = 0; |
544 | gotsig = 0; |
472 | |
545 | |
… | |
… | |
514 | struct ev_child *w; |
587 | struct ev_child *w; |
515 | |
588 | |
516 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
589 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
517 | if (w->pid == pid || !w->pid) |
590 | if (w->pid == pid || !w->pid) |
518 | { |
591 | { |
519 | w->priority = sw->priority; /* need to do it *now* */ |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
520 | w->rpid = pid; |
593 | w->rpid = pid; |
521 | w->rstatus = status; |
594 | w->rstatus = status; |
522 | event (EV_A_ (W)w, EV_CHILD); |
595 | event (EV_A_ (W)w, EV_CHILD); |
523 | } |
596 | } |
524 | } |
597 | } |
525 | |
598 | |
526 | static void |
599 | static void |
… | |
… | |
608 | methods = atoi (getenv ("LIBEV_METHODS")); |
681 | methods = atoi (getenv ("LIBEV_METHODS")); |
609 | else |
682 | else |
610 | methods = EVMETHOD_ANY; |
683 | methods = EVMETHOD_ANY; |
611 | |
684 | |
612 | method = 0; |
685 | method = 0; |
|
|
686 | #if EV_USE_WIN32 |
|
|
687 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
688 | #endif |
613 | #if EV_USE_KQUEUE |
689 | #if EV_USE_KQUEUE |
614 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
690 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
615 | #endif |
691 | #endif |
616 | #if EV_USE_EPOLL |
692 | #if EV_USE_EPOLL |
617 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
693 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
620 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
696 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
621 | #endif |
697 | #endif |
622 | #if EV_USE_SELECT |
698 | #if EV_USE_SELECT |
623 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
699 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
624 | #endif |
700 | #endif |
|
|
701 | |
|
|
702 | ev_watcher_init (&sigev, sigcb); |
|
|
703 | ev_set_priority (&sigev, EV_MAXPRI); |
625 | } |
704 | } |
626 | } |
705 | } |
627 | |
706 | |
628 | void |
707 | void |
629 | loop_destroy (EV_P) |
708 | loop_destroy (EV_P) |
630 | { |
709 | { |
|
|
710 | int i; |
|
|
711 | |
|
|
712 | #if EV_USE_WIN32 |
|
|
713 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
714 | #endif |
631 | #if EV_USE_KQUEUE |
715 | #if EV_USE_KQUEUE |
632 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
716 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
633 | #endif |
717 | #endif |
634 | #if EV_USE_EPOLL |
718 | #if EV_USE_EPOLL |
635 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
719 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
639 | #endif |
723 | #endif |
640 | #if EV_USE_SELECT |
724 | #if EV_USE_SELECT |
641 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
725 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
642 | #endif |
726 | #endif |
643 | |
727 | |
|
|
728 | for (i = NUMPRI; i--; ) |
|
|
729 | array_free (pending, [i]); |
|
|
730 | |
|
|
731 | array_free (fdchange, ); |
|
|
732 | array_free (timer, ); |
|
|
733 | array_free (periodic, ); |
|
|
734 | array_free (idle, ); |
|
|
735 | array_free (prepare, ); |
|
|
736 | array_free (check, ); |
|
|
737 | |
644 | method = 0; |
738 | method = 0; |
645 | /*TODO*/ |
|
|
646 | } |
739 | } |
647 | |
740 | |
648 | void |
741 | static void |
649 | loop_fork (EV_P) |
742 | loop_fork (EV_P) |
650 | { |
743 | { |
651 | /*TODO*/ |
|
|
652 | #if EV_USE_EPOLL |
744 | #if EV_USE_EPOLL |
653 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
745 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
654 | #endif |
746 | #endif |
655 | #if EV_USE_KQUEUE |
747 | #if EV_USE_KQUEUE |
656 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
748 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
657 | #endif |
749 | #endif |
|
|
750 | |
|
|
751 | if (ev_is_active (&sigev)) |
|
|
752 | { |
|
|
753 | /* default loop */ |
|
|
754 | |
|
|
755 | ev_ref (EV_A); |
|
|
756 | ev_io_stop (EV_A_ &sigev); |
|
|
757 | close (sigpipe [0]); |
|
|
758 | close (sigpipe [1]); |
|
|
759 | |
|
|
760 | while (pipe (sigpipe)) |
|
|
761 | syserr ("(libev) error creating pipe"); |
|
|
762 | |
|
|
763 | siginit (EV_A); |
|
|
764 | } |
|
|
765 | |
|
|
766 | postfork = 0; |
658 | } |
767 | } |
659 | |
768 | |
660 | #if EV_MULTIPLICITY |
769 | #if EV_MULTIPLICITY |
661 | struct ev_loop * |
770 | struct ev_loop * |
662 | ev_loop_new (int methods) |
771 | ev_loop_new (int methods) |
663 | { |
772 | { |
664 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
773 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
774 | |
|
|
775 | memset (loop, 0, sizeof (struct ev_loop)); |
665 | |
776 | |
666 | loop_init (EV_A_ methods); |
777 | loop_init (EV_A_ methods); |
667 | |
778 | |
668 | if (ev_method (EV_A)) |
779 | if (ev_method (EV_A)) |
669 | return loop; |
780 | return loop; |
… | |
… | |
673 | |
784 | |
674 | void |
785 | void |
675 | ev_loop_destroy (EV_P) |
786 | ev_loop_destroy (EV_P) |
676 | { |
787 | { |
677 | loop_destroy (EV_A); |
788 | loop_destroy (EV_A); |
678 | free (loop); |
789 | ev_free (loop); |
679 | } |
790 | } |
680 | |
791 | |
681 | void |
792 | void |
682 | ev_loop_fork (EV_P) |
793 | ev_loop_fork (EV_P) |
683 | { |
794 | { |
684 | loop_fork (EV_A); |
795 | postfork = 1; |
685 | } |
796 | } |
686 | |
797 | |
687 | #endif |
798 | #endif |
688 | |
799 | |
689 | #if EV_MULTIPLICITY |
800 | #if EV_MULTIPLICITY |
… | |
… | |
712 | |
823 | |
713 | loop_init (EV_A_ methods); |
824 | loop_init (EV_A_ methods); |
714 | |
825 | |
715 | if (ev_method (EV_A)) |
826 | if (ev_method (EV_A)) |
716 | { |
827 | { |
717 | ev_watcher_init (&sigev, sigcb); |
|
|
718 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
719 | siginit (EV_A); |
828 | siginit (EV_A); |
720 | |
829 | |
721 | #ifndef WIN32 |
830 | #ifndef WIN32 |
722 | ev_signal_init (&childev, childcb, SIGCHLD); |
831 | ev_signal_init (&childev, childcb, SIGCHLD); |
723 | ev_set_priority (&childev, EV_MAXPRI); |
832 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
756 | { |
865 | { |
757 | #if EV_MULTIPLICITY |
866 | #if EV_MULTIPLICITY |
758 | struct ev_loop *loop = default_loop; |
867 | struct ev_loop *loop = default_loop; |
759 | #endif |
868 | #endif |
760 | |
869 | |
761 | loop_fork (EV_A); |
870 | if (method) |
762 | |
871 | postfork = 1; |
763 | ev_io_stop (EV_A_ &sigev); |
|
|
764 | close (sigpipe [0]); |
|
|
765 | close (sigpipe [1]); |
|
|
766 | pipe (sigpipe); |
|
|
767 | |
|
|
768 | ev_ref (EV_A); /* signal watcher */ |
|
|
769 | siginit (EV_A); |
|
|
770 | } |
872 | } |
771 | |
873 | |
772 | /*****************************************************************************/ |
874 | /*****************************************************************************/ |
773 | |
875 | |
774 | static void |
876 | static void |
… | |
… | |
790 | } |
892 | } |
791 | |
893 | |
792 | static void |
894 | static void |
793 | timers_reify (EV_P) |
895 | timers_reify (EV_P) |
794 | { |
896 | { |
795 | while (timercnt && timers [0]->at <= mn_now) |
897 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
796 | { |
898 | { |
797 | struct ev_timer *w = timers [0]; |
899 | struct ev_timer *w = timers [0]; |
|
|
900 | |
|
|
901 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
798 | |
902 | |
799 | /* first reschedule or stop timer */ |
903 | /* first reschedule or stop timer */ |
800 | if (w->repeat) |
904 | if (w->repeat) |
801 | { |
905 | { |
802 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
906 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
803 | w->at = mn_now + w->repeat; |
907 | ((WT)w)->at = mn_now + w->repeat; |
804 | downheap ((WT *)timers, timercnt, 0); |
908 | downheap ((WT *)timers, timercnt, 0); |
805 | } |
909 | } |
806 | else |
910 | else |
807 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
808 | |
912 | |
… | |
… | |
811 | } |
915 | } |
812 | |
916 | |
813 | static void |
917 | static void |
814 | periodics_reify (EV_P) |
918 | periodics_reify (EV_P) |
815 | { |
919 | { |
816 | while (periodiccnt && periodics [0]->at <= rt_now) |
920 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
817 | { |
921 | { |
818 | struct ev_periodic *w = periodics [0]; |
922 | struct ev_periodic *w = periodics [0]; |
|
|
923 | |
|
|
924 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
819 | |
925 | |
820 | /* first reschedule or stop timer */ |
926 | /* first reschedule or stop timer */ |
821 | if (w->interval) |
927 | if (w->interval) |
822 | { |
928 | { |
823 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
929 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
824 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
930 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
825 | downheap ((WT *)periodics, periodiccnt, 0); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
826 | } |
932 | } |
827 | else |
933 | else |
828 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
829 | |
935 | |
… | |
… | |
841 | { |
947 | { |
842 | struct ev_periodic *w = periodics [i]; |
948 | struct ev_periodic *w = periodics [i]; |
843 | |
949 | |
844 | if (w->interval) |
950 | if (w->interval) |
845 | { |
951 | { |
846 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
952 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
847 | |
953 | |
848 | if (fabs (diff) >= 1e-4) |
954 | if (fabs (diff) >= 1e-4) |
849 | { |
955 | { |
850 | ev_periodic_stop (EV_A_ w); |
956 | ev_periodic_stop (EV_A_ w); |
851 | ev_periodic_start (EV_A_ w); |
957 | ev_periodic_start (EV_A_ w); |
… | |
… | |
912 | { |
1018 | { |
913 | periodics_reschedule (EV_A); |
1019 | periodics_reschedule (EV_A); |
914 | |
1020 | |
915 | /* adjust timers. this is easy, as the offset is the same for all */ |
1021 | /* adjust timers. this is easy, as the offset is the same for all */ |
916 | for (i = 0; i < timercnt; ++i) |
1022 | for (i = 0; i < timercnt; ++i) |
917 | timers [i]->at += rt_now - mn_now; |
1023 | ((WT)timers [i])->at += rt_now - mn_now; |
918 | } |
1024 | } |
919 | |
1025 | |
920 | mn_now = rt_now; |
1026 | mn_now = rt_now; |
921 | } |
1027 | } |
922 | } |
1028 | } |
… | |
… | |
948 | { |
1054 | { |
949 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1055 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
950 | call_pending (EV_A); |
1056 | call_pending (EV_A); |
951 | } |
1057 | } |
952 | |
1058 | |
|
|
1059 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1060 | if (expect_false (postfork)) |
|
|
1061 | loop_fork (EV_A); |
|
|
1062 | |
953 | /* update fd-related kernel structures */ |
1063 | /* update fd-related kernel structures */ |
954 | fd_reify (EV_A); |
1064 | fd_reify (EV_A); |
955 | |
1065 | |
956 | /* calculate blocking time */ |
1066 | /* calculate blocking time */ |
957 | |
1067 | |
… | |
… | |
973 | { |
1083 | { |
974 | block = MAX_BLOCKTIME; |
1084 | block = MAX_BLOCKTIME; |
975 | |
1085 | |
976 | if (timercnt) |
1086 | if (timercnt) |
977 | { |
1087 | { |
978 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1088 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
979 | if (block > to) block = to; |
1089 | if (block > to) block = to; |
980 | } |
1090 | } |
981 | |
1091 | |
982 | if (periodiccnt) |
1092 | if (periodiccnt) |
983 | { |
1093 | { |
984 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1094 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
985 | if (block > to) block = to; |
1095 | if (block > to) block = to; |
986 | } |
1096 | } |
987 | |
1097 | |
988 | if (block < 0.) block = 0.; |
1098 | if (block < 0.) block = 0.; |
989 | } |
1099 | } |
… | |
… | |
1106 | ev_timer_start (EV_P_ struct ev_timer *w) |
1216 | ev_timer_start (EV_P_ struct ev_timer *w) |
1107 | { |
1217 | { |
1108 | if (ev_is_active (w)) |
1218 | if (ev_is_active (w)) |
1109 | return; |
1219 | return; |
1110 | |
1220 | |
1111 | w->at += mn_now; |
1221 | ((WT)w)->at += mn_now; |
1112 | |
1222 | |
1113 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1114 | |
1224 | |
1115 | ev_start (EV_A_ (W)w, ++timercnt); |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1116 | array_needsize (timers, timermax, timercnt, ); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1117 | timers [timercnt - 1] = w; |
1227 | timers [timercnt - 1] = w; |
1118 | upheap ((WT *)timers, timercnt - 1); |
1228 | upheap ((WT *)timers, timercnt - 1); |
|
|
1229 | |
|
|
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1119 | } |
1231 | } |
1120 | |
1232 | |
1121 | void |
1233 | void |
1122 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1234 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1123 | { |
1235 | { |
1124 | ev_clear_pending (EV_A_ (W)w); |
1236 | ev_clear_pending (EV_A_ (W)w); |
1125 | if (!ev_is_active (w)) |
1237 | if (!ev_is_active (w)) |
1126 | return; |
1238 | return; |
1127 | |
1239 | |
|
|
1240 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1241 | |
1128 | if (w->active < timercnt--) |
1242 | if (((W)w)->active < timercnt--) |
1129 | { |
1243 | { |
1130 | timers [w->active - 1] = timers [timercnt]; |
1244 | timers [((W)w)->active - 1] = timers [timercnt]; |
1131 | downheap ((WT *)timers, timercnt, w->active - 1); |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1132 | } |
1246 | } |
1133 | |
1247 | |
1134 | w->at = w->repeat; |
1248 | ((WT)w)->at = w->repeat; |
1135 | |
1249 | |
1136 | ev_stop (EV_A_ (W)w); |
1250 | ev_stop (EV_A_ (W)w); |
1137 | } |
1251 | } |
1138 | |
1252 | |
1139 | void |
1253 | void |
… | |
… | |
1141 | { |
1255 | { |
1142 | if (ev_is_active (w)) |
1256 | if (ev_is_active (w)) |
1143 | { |
1257 | { |
1144 | if (w->repeat) |
1258 | if (w->repeat) |
1145 | { |
1259 | { |
1146 | w->at = mn_now + w->repeat; |
1260 | ((WT)w)->at = mn_now + w->repeat; |
1147 | downheap ((WT *)timers, timercnt, w->active - 1); |
1261 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1148 | } |
1262 | } |
1149 | else |
1263 | else |
1150 | ev_timer_stop (EV_A_ w); |
1264 | ev_timer_stop (EV_A_ w); |
1151 | } |
1265 | } |
1152 | else if (w->repeat) |
1266 | else if (w->repeat) |
… | |
… | |
1161 | |
1275 | |
1162 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1276 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1163 | |
1277 | |
1164 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1278 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1165 | if (w->interval) |
1279 | if (w->interval) |
1166 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1280 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1167 | |
1281 | |
1168 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1169 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1170 | periodics [periodiccnt - 1] = w; |
1284 | periodics [periodiccnt - 1] = w; |
1171 | upheap ((WT *)periodics, periodiccnt - 1); |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1286 | |
|
|
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1172 | } |
1288 | } |
1173 | |
1289 | |
1174 | void |
1290 | void |
1175 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1291 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1176 | { |
1292 | { |
1177 | ev_clear_pending (EV_A_ (W)w); |
1293 | ev_clear_pending (EV_A_ (W)w); |
1178 | if (!ev_is_active (w)) |
1294 | if (!ev_is_active (w)) |
1179 | return; |
1295 | return; |
1180 | |
1296 | |
|
|
1297 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1298 | |
1181 | if (w->active < periodiccnt--) |
1299 | if (((W)w)->active < periodiccnt--) |
1182 | { |
1300 | { |
1183 | periodics [w->active - 1] = periodics [periodiccnt]; |
1301 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1184 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1302 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1185 | } |
1303 | } |
1186 | |
1304 | |
1187 | ev_stop (EV_A_ (W)w); |
1305 | ev_stop (EV_A_ (W)w); |
1188 | } |
1306 | } |
1189 | |
1307 | |
… | |
… | |
1203 | { |
1321 | { |
1204 | ev_clear_pending (EV_A_ (W)w); |
1322 | ev_clear_pending (EV_A_ (W)w); |
1205 | if (ev_is_active (w)) |
1323 | if (ev_is_active (w)) |
1206 | return; |
1324 | return; |
1207 | |
1325 | |
1208 | idles [w->active - 1] = idles [--idlecnt]; |
1326 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1209 | ev_stop (EV_A_ (W)w); |
1327 | ev_stop (EV_A_ (W)w); |
1210 | } |
1328 | } |
1211 | |
1329 | |
1212 | void |
1330 | void |
1213 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1331 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1225 | { |
1343 | { |
1226 | ev_clear_pending (EV_A_ (W)w); |
1344 | ev_clear_pending (EV_A_ (W)w); |
1227 | if (ev_is_active (w)) |
1345 | if (ev_is_active (w)) |
1228 | return; |
1346 | return; |
1229 | |
1347 | |
1230 | prepares [w->active - 1] = prepares [--preparecnt]; |
1348 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1231 | ev_stop (EV_A_ (W)w); |
1349 | ev_stop (EV_A_ (W)w); |
1232 | } |
1350 | } |
1233 | |
1351 | |
1234 | void |
1352 | void |
1235 | ev_check_start (EV_P_ struct ev_check *w) |
1353 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1247 | { |
1365 | { |
1248 | ev_clear_pending (EV_A_ (W)w); |
1366 | ev_clear_pending (EV_A_ (W)w); |
1249 | if (ev_is_active (w)) |
1367 | if (ev_is_active (w)) |
1250 | return; |
1368 | return; |
1251 | |
1369 | |
1252 | checks [w->active - 1] = checks [--checkcnt]; |
1370 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1253 | ev_stop (EV_A_ (W)w); |
1371 | ev_stop (EV_A_ (W)w); |
1254 | } |
1372 | } |
1255 | |
1373 | |
1256 | #ifndef SA_RESTART |
1374 | #ifndef SA_RESTART |
1257 | # define SA_RESTART 0 |
1375 | # define SA_RESTART 0 |
… | |
… | |
1270 | |
1388 | |
1271 | ev_start (EV_A_ (W)w, 1); |
1389 | ev_start (EV_A_ (W)w, 1); |
1272 | array_needsize (signals, signalmax, w->signum, signals_init); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1273 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1274 | |
1392 | |
1275 | if (!w->next) |
1393 | if (!((WL)w)->next) |
1276 | { |
1394 | { |
|
|
1395 | #if WIN32 |
|
|
1396 | signal (w->signum, sighandler); |
|
|
1397 | #else |
1277 | struct sigaction sa; |
1398 | struct sigaction sa; |
1278 | sa.sa_handler = sighandler; |
1399 | sa.sa_handler = sighandler; |
1279 | sigfillset (&sa.sa_mask); |
1400 | sigfillset (&sa.sa_mask); |
1280 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1401 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1281 | sigaction (w->signum, &sa, 0); |
1402 | sigaction (w->signum, &sa, 0); |
|
|
1403 | #endif |
1282 | } |
1404 | } |
1283 | } |
1405 | } |
1284 | |
1406 | |
1285 | void |
1407 | void |
1286 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1408 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1336 | void (*cb)(int revents, void *arg) = once->cb; |
1458 | void (*cb)(int revents, void *arg) = once->cb; |
1337 | void *arg = once->arg; |
1459 | void *arg = once->arg; |
1338 | |
1460 | |
1339 | ev_io_stop (EV_A_ &once->io); |
1461 | ev_io_stop (EV_A_ &once->io); |
1340 | ev_timer_stop (EV_A_ &once->to); |
1462 | ev_timer_stop (EV_A_ &once->to); |
1341 | free (once); |
1463 | ev_free (once); |
1342 | |
1464 | |
1343 | cb (revents, arg); |
1465 | cb (revents, arg); |
1344 | } |
1466 | } |
1345 | |
1467 | |
1346 | static void |
1468 | static void |
… | |
… | |
1356 | } |
1478 | } |
1357 | |
1479 | |
1358 | void |
1480 | void |
1359 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1481 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1360 | { |
1482 | { |
1361 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1362 | |
1484 | |
1363 | if (!once) |
1485 | if (!once) |
1364 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1365 | else |
1487 | else |
1366 | { |
1488 | { |