… | |
… | |
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)(void); |
|
|
159 | |
|
|
160 | void ev_set_syserr_cb (void (*cb)(void)) |
|
|
161 | { |
|
|
162 | syserr_cb = cb; |
|
|
163 | } |
|
|
164 | |
|
|
165 | static void |
|
|
166 | syserr (void) |
|
|
167 | { |
|
|
168 | if (syserr_cb) |
|
|
169 | syserr_cb (); |
|
|
170 | else |
|
|
171 | { |
|
|
172 | perror ("libev"); |
|
|
173 | abort (); |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | static void *(*alloc)(void *ptr, long size); |
|
|
178 | |
|
|
179 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
180 | { |
|
|
181 | alloc = cb; |
|
|
182 | } |
|
|
183 | |
|
|
184 | static void * |
|
|
185 | ev_realloc (void *ptr, long size) |
|
|
186 | { |
|
|
187 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
188 | |
|
|
189 | if (!ptr && size) |
|
|
190 | { |
|
|
191 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
192 | abort (); |
|
|
193 | } |
|
|
194 | |
|
|
195 | return ptr; |
|
|
196 | } |
|
|
197 | |
|
|
198 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
199 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
200 | |
|
|
201 | /*****************************************************************************/ |
|
|
202 | |
144 | typedef struct |
203 | typedef struct |
145 | { |
204 | { |
146 | struct ev_watcher_list *head; |
205 | WL head; |
147 | unsigned char events; |
206 | unsigned char events; |
148 | unsigned char reify; |
207 | unsigned char reify; |
149 | } ANFD; |
208 | } ANFD; |
150 | |
209 | |
151 | typedef struct |
210 | typedef struct |
… | |
… | |
209 | return rt_now; |
268 | return rt_now; |
210 | } |
269 | } |
211 | |
270 | |
212 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
271 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
213 | |
272 | |
214 | #define array_needsize(base,cur,cnt,init) \ |
273 | #define array_needsize(base,cur,cnt,init) \ |
215 | if (expect_false ((cnt) > cur)) \ |
274 | if (expect_false ((cnt) > cur)) \ |
216 | { \ |
275 | { \ |
217 | int newcnt = cur; \ |
276 | int newcnt = cur; \ |
218 | do \ |
277 | do \ |
219 | { \ |
278 | { \ |
220 | newcnt = array_roundsize (base, newcnt << 1); \ |
279 | newcnt = array_roundsize (base, newcnt << 1); \ |
221 | } \ |
280 | } \ |
222 | while ((cnt) > newcnt); \ |
281 | while ((cnt) > newcnt); \ |
223 | \ |
282 | \ |
224 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
283 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
225 | init (base + cur, newcnt - cur); \ |
284 | init (base + cur, newcnt - cur); \ |
226 | cur = newcnt; \ |
285 | cur = newcnt; \ |
227 | } |
286 | } |
|
|
287 | |
|
|
288 | #define array_slim(stem) \ |
|
|
289 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
290 | { \ |
|
|
291 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
292 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
293 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
294 | } |
|
|
295 | |
|
|
296 | #define array_free(stem, idx) \ |
|
|
297 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
228 | |
298 | |
229 | /*****************************************************************************/ |
299 | /*****************************************************************************/ |
230 | |
300 | |
231 | static void |
301 | static void |
232 | anfds_init (ANFD *base, int count) |
302 | anfds_init (ANFD *base, int count) |
… | |
… | |
298 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
368 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
299 | events |= w->events; |
369 | events |= w->events; |
300 | |
370 | |
301 | anfd->reify = 0; |
371 | anfd->reify = 0; |
302 | |
372 | |
303 | if (anfd->events != events) |
|
|
304 | { |
|
|
305 | method_modify (EV_A_ fd, anfd->events, events); |
373 | method_modify (EV_A_ fd, anfd->events, events); |
306 | anfd->events = events; |
374 | anfd->events = events; |
307 | } |
|
|
308 | } |
375 | } |
309 | |
376 | |
310 | fdchangecnt = 0; |
377 | fdchangecnt = 0; |
311 | } |
378 | } |
312 | |
379 | |
… | |
… | |
349 | |
416 | |
350 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
417 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
351 | static void |
418 | static void |
352 | fd_enomem (EV_P) |
419 | fd_enomem (EV_P) |
353 | { |
420 | { |
354 | int fd = anfdmax; |
421 | int fd; |
355 | |
422 | |
356 | while (fd--) |
423 | for (fd = anfdmax; fd--; ) |
357 | if (anfds [fd].events) |
424 | if (anfds [fd].events) |
358 | { |
425 | { |
359 | close (fd); |
|
|
360 | fd_kill (EV_A_ fd); |
426 | fd_kill (EV_A_ fd); |
361 | return; |
427 | return; |
362 | } |
428 | } |
363 | } |
429 | } |
364 | |
430 | |
… | |
… | |
385 | WT w = heap [k]; |
451 | WT w = heap [k]; |
386 | |
452 | |
387 | while (k && heap [k >> 1]->at > w->at) |
453 | while (k && heap [k >> 1]->at > w->at) |
388 | { |
454 | { |
389 | heap [k] = heap [k >> 1]; |
455 | heap [k] = heap [k >> 1]; |
390 | heap [k]->active = k + 1; |
456 | ((W)heap [k])->active = k + 1; |
391 | k >>= 1; |
457 | k >>= 1; |
392 | } |
458 | } |
393 | |
459 | |
394 | heap [k] = w; |
460 | heap [k] = w; |
395 | heap [k]->active = k + 1; |
461 | ((W)heap [k])->active = k + 1; |
396 | |
462 | |
397 | } |
463 | } |
398 | |
464 | |
399 | static void |
465 | static void |
400 | downheap (WT *heap, int N, int k) |
466 | downheap (WT *heap, int N, int k) |
… | |
… | |
410 | |
476 | |
411 | if (w->at <= heap [j]->at) |
477 | if (w->at <= heap [j]->at) |
412 | break; |
478 | break; |
413 | |
479 | |
414 | heap [k] = heap [j]; |
480 | heap [k] = heap [j]; |
415 | heap [k]->active = k + 1; |
481 | ((W)heap [k])->active = k + 1; |
416 | k = j; |
482 | k = j; |
417 | } |
483 | } |
418 | |
484 | |
419 | heap [k] = w; |
485 | heap [k] = w; |
420 | heap [k]->active = k + 1; |
486 | ((W)heap [k])->active = k + 1; |
421 | } |
487 | } |
422 | |
488 | |
423 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
424 | |
490 | |
425 | typedef struct |
491 | typedef struct |
426 | { |
492 | { |
427 | struct ev_watcher_list *head; |
493 | WL head; |
428 | sig_atomic_t volatile gotsig; |
494 | sig_atomic_t volatile gotsig; |
429 | } ANSIG; |
495 | } ANSIG; |
430 | |
496 | |
431 | static ANSIG *signals; |
497 | static ANSIG *signals; |
432 | static int signalmax; |
498 | static int signalmax; |
… | |
… | |
448 | } |
514 | } |
449 | |
515 | |
450 | static void |
516 | static void |
451 | sighandler (int signum) |
517 | sighandler (int signum) |
452 | { |
518 | { |
|
|
519 | #if WIN32 |
|
|
520 | signal (signum, sighandler); |
|
|
521 | #endif |
|
|
522 | |
453 | signals [signum - 1].gotsig = 1; |
523 | signals [signum - 1].gotsig = 1; |
454 | |
524 | |
455 | if (!gotsig) |
525 | if (!gotsig) |
456 | { |
526 | { |
457 | int old_errno = errno; |
527 | int old_errno = errno; |
… | |
… | |
462 | } |
532 | } |
463 | |
533 | |
464 | static void |
534 | static void |
465 | sigcb (EV_P_ struct ev_io *iow, int revents) |
535 | sigcb (EV_P_ struct ev_io *iow, int revents) |
466 | { |
536 | { |
467 | struct ev_watcher_list *w; |
537 | WL w; |
468 | int signum; |
538 | int signum; |
469 | |
539 | |
470 | read (sigpipe [0], &revents, 1); |
540 | read (sigpipe [0], &revents, 1); |
471 | gotsig = 0; |
541 | gotsig = 0; |
472 | |
542 | |
… | |
… | |
514 | struct ev_child *w; |
584 | struct ev_child *w; |
515 | |
585 | |
516 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
586 | 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) |
587 | if (w->pid == pid || !w->pid) |
518 | { |
588 | { |
519 | w->priority = sw->priority; /* need to do it *now* */ |
589 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
520 | w->rpid = pid; |
590 | w->rpid = pid; |
521 | w->rstatus = status; |
591 | w->rstatus = status; |
522 | event (EV_A_ (W)w, EV_CHILD); |
592 | event (EV_A_ (W)w, EV_CHILD); |
523 | } |
593 | } |
524 | } |
594 | } |
525 | |
595 | |
526 | static void |
596 | static void |
… | |
… | |
608 | methods = atoi (getenv ("LIBEV_METHODS")); |
678 | methods = atoi (getenv ("LIBEV_METHODS")); |
609 | else |
679 | else |
610 | methods = EVMETHOD_ANY; |
680 | methods = EVMETHOD_ANY; |
611 | |
681 | |
612 | method = 0; |
682 | method = 0; |
|
|
683 | #if EV_USE_WIN32 |
|
|
684 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
685 | #endif |
613 | #if EV_USE_KQUEUE |
686 | #if EV_USE_KQUEUE |
614 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
687 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
615 | #endif |
688 | #endif |
616 | #if EV_USE_EPOLL |
689 | #if EV_USE_EPOLL |
617 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
690 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
626 | } |
699 | } |
627 | |
700 | |
628 | void |
701 | void |
629 | loop_destroy (EV_P) |
702 | loop_destroy (EV_P) |
630 | { |
703 | { |
|
|
704 | int i; |
|
|
705 | |
|
|
706 | #if EV_USE_WIN32 |
|
|
707 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
708 | #endif |
631 | #if EV_USE_KQUEUE |
709 | #if EV_USE_KQUEUE |
632 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
710 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
633 | #endif |
711 | #endif |
634 | #if EV_USE_EPOLL |
712 | #if EV_USE_EPOLL |
635 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
713 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
638 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
716 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
639 | #endif |
717 | #endif |
640 | #if EV_USE_SELECT |
718 | #if EV_USE_SELECT |
641 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
719 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
642 | #endif |
720 | #endif |
|
|
721 | |
|
|
722 | for (i = NUMPRI; i--; ) |
|
|
723 | array_free (pending, [i]); |
|
|
724 | |
|
|
725 | array_free (fdchange, ); |
|
|
726 | array_free (timer, ); |
|
|
727 | array_free (periodic, ); |
|
|
728 | array_free (idle, ); |
|
|
729 | array_free (prepare, ); |
|
|
730 | array_free (check, ); |
643 | |
731 | |
644 | method = 0; |
732 | method = 0; |
645 | /*TODO*/ |
733 | /*TODO*/ |
646 | } |
734 | } |
647 | |
735 | |
… | |
… | |
659 | |
747 | |
660 | #if EV_MULTIPLICITY |
748 | #if EV_MULTIPLICITY |
661 | struct ev_loop * |
749 | struct ev_loop * |
662 | ev_loop_new (int methods) |
750 | ev_loop_new (int methods) |
663 | { |
751 | { |
664 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
752 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
753 | |
|
|
754 | memset (loop, 0, sizeof (struct ev_loop)); |
665 | |
755 | |
666 | loop_init (EV_A_ methods); |
756 | loop_init (EV_A_ methods); |
667 | |
757 | |
668 | if (ev_method (EV_A)) |
758 | if (ev_method (EV_A)) |
669 | return loop; |
759 | return loop; |
… | |
… | |
673 | |
763 | |
674 | void |
764 | void |
675 | ev_loop_destroy (EV_P) |
765 | ev_loop_destroy (EV_P) |
676 | { |
766 | { |
677 | loop_destroy (EV_A); |
767 | loop_destroy (EV_A); |
678 | free (loop); |
768 | ev_free (loop); |
679 | } |
769 | } |
680 | |
770 | |
681 | void |
771 | void |
682 | ev_loop_fork (EV_P) |
772 | ev_loop_fork (EV_P) |
683 | { |
773 | { |
… | |
… | |
790 | } |
880 | } |
791 | |
881 | |
792 | static void |
882 | static void |
793 | timers_reify (EV_P) |
883 | timers_reify (EV_P) |
794 | { |
884 | { |
795 | while (timercnt && timers [0]->at <= mn_now) |
885 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
796 | { |
886 | { |
797 | struct ev_timer *w = timers [0]; |
887 | struct ev_timer *w = timers [0]; |
798 | |
888 | |
799 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
889 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
800 | |
890 | |
801 | /* first reschedule or stop timer */ |
891 | /* first reschedule or stop timer */ |
802 | if (w->repeat) |
892 | if (w->repeat) |
803 | { |
893 | { |
804 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
894 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
805 | w->at = mn_now + w->repeat; |
895 | ((WT)w)->at = mn_now + w->repeat; |
806 | downheap ((WT *)timers, timercnt, 0); |
896 | downheap ((WT *)timers, timercnt, 0); |
807 | } |
897 | } |
808 | else |
898 | else |
809 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
899 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
810 | |
900 | |
… | |
… | |
813 | } |
903 | } |
814 | |
904 | |
815 | static void |
905 | static void |
816 | periodics_reify (EV_P) |
906 | periodics_reify (EV_P) |
817 | { |
907 | { |
818 | while (periodiccnt && periodics [0]->at <= rt_now) |
908 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
819 | { |
909 | { |
820 | struct ev_periodic *w = periodics [0]; |
910 | struct ev_periodic *w = periodics [0]; |
821 | |
911 | |
822 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
912 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
823 | |
913 | |
824 | /* first reschedule or stop timer */ |
914 | /* first reschedule or stop timer */ |
825 | if (w->interval) |
915 | if (w->interval) |
826 | { |
916 | { |
827 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
917 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
828 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
918 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
829 | downheap ((WT *)periodics, periodiccnt, 0); |
919 | downheap ((WT *)periodics, periodiccnt, 0); |
830 | } |
920 | } |
831 | else |
921 | else |
832 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
922 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
833 | |
923 | |
… | |
… | |
845 | { |
935 | { |
846 | struct ev_periodic *w = periodics [i]; |
936 | struct ev_periodic *w = periodics [i]; |
847 | |
937 | |
848 | if (w->interval) |
938 | if (w->interval) |
849 | { |
939 | { |
850 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
940 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
851 | |
941 | |
852 | if (fabs (diff) >= 1e-4) |
942 | if (fabs (diff) >= 1e-4) |
853 | { |
943 | { |
854 | ev_periodic_stop (EV_A_ w); |
944 | ev_periodic_stop (EV_A_ w); |
855 | ev_periodic_start (EV_A_ w); |
945 | ev_periodic_start (EV_A_ w); |
… | |
… | |
916 | { |
1006 | { |
917 | periodics_reschedule (EV_A); |
1007 | periodics_reschedule (EV_A); |
918 | |
1008 | |
919 | /* adjust timers. this is easy, as the offset is the same for all */ |
1009 | /* adjust timers. this is easy, as the offset is the same for all */ |
920 | for (i = 0; i < timercnt; ++i) |
1010 | for (i = 0; i < timercnt; ++i) |
921 | timers [i]->at += rt_now - mn_now; |
1011 | ((WT)timers [i])->at += rt_now - mn_now; |
922 | } |
1012 | } |
923 | |
1013 | |
924 | mn_now = rt_now; |
1014 | mn_now = rt_now; |
925 | } |
1015 | } |
926 | } |
1016 | } |
… | |
… | |
977 | { |
1067 | { |
978 | block = MAX_BLOCKTIME; |
1068 | block = MAX_BLOCKTIME; |
979 | |
1069 | |
980 | if (timercnt) |
1070 | if (timercnt) |
981 | { |
1071 | { |
982 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1072 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
983 | if (block > to) block = to; |
1073 | if (block > to) block = to; |
984 | } |
1074 | } |
985 | |
1075 | |
986 | if (periodiccnt) |
1076 | if (periodiccnt) |
987 | { |
1077 | { |
988 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1078 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
989 | if (block > to) block = to; |
1079 | if (block > to) block = to; |
990 | } |
1080 | } |
991 | |
1081 | |
992 | if (block < 0.) block = 0.; |
1082 | if (block < 0.) block = 0.; |
993 | } |
1083 | } |
… | |
… | |
1110 | ev_timer_start (EV_P_ struct ev_timer *w) |
1200 | ev_timer_start (EV_P_ struct ev_timer *w) |
1111 | { |
1201 | { |
1112 | if (ev_is_active (w)) |
1202 | if (ev_is_active (w)) |
1113 | return; |
1203 | return; |
1114 | |
1204 | |
1115 | w->at += mn_now; |
1205 | ((WT)w)->at += mn_now; |
1116 | |
1206 | |
1117 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1207 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1118 | |
1208 | |
1119 | ev_start (EV_A_ (W)w, ++timercnt); |
1209 | ev_start (EV_A_ (W)w, ++timercnt); |
1120 | array_needsize (timers, timermax, timercnt, ); |
1210 | array_needsize (timers, timermax, timercnt, ); |
1121 | timers [timercnt - 1] = w; |
1211 | timers [timercnt - 1] = w; |
1122 | upheap ((WT *)timers, timercnt - 1); |
1212 | upheap ((WT *)timers, timercnt - 1); |
|
|
1213 | |
|
|
1214 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1123 | } |
1215 | } |
1124 | |
1216 | |
1125 | void |
1217 | void |
1126 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1218 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1127 | { |
1219 | { |
1128 | ev_clear_pending (EV_A_ (W)w); |
1220 | ev_clear_pending (EV_A_ (W)w); |
1129 | if (!ev_is_active (w)) |
1221 | if (!ev_is_active (w)) |
1130 | return; |
1222 | return; |
1131 | |
1223 | |
|
|
1224 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1225 | |
1132 | if (w->active < timercnt--) |
1226 | if (((W)w)->active < timercnt--) |
1133 | { |
1227 | { |
1134 | timers [w->active - 1] = timers [timercnt]; |
1228 | timers [((W)w)->active - 1] = timers [timercnt]; |
1135 | downheap ((WT *)timers, timercnt, w->active - 1); |
1229 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1136 | } |
1230 | } |
1137 | |
1231 | |
1138 | w->at = w->repeat; |
1232 | ((WT)w)->at = w->repeat; |
1139 | |
1233 | |
1140 | ev_stop (EV_A_ (W)w); |
1234 | ev_stop (EV_A_ (W)w); |
1141 | } |
1235 | } |
1142 | |
1236 | |
1143 | void |
1237 | void |
… | |
… | |
1145 | { |
1239 | { |
1146 | if (ev_is_active (w)) |
1240 | if (ev_is_active (w)) |
1147 | { |
1241 | { |
1148 | if (w->repeat) |
1242 | if (w->repeat) |
1149 | { |
1243 | { |
1150 | w->at = mn_now + w->repeat; |
1244 | ((WT)w)->at = mn_now + w->repeat; |
1151 | downheap ((WT *)timers, timercnt, w->active - 1); |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1152 | } |
1246 | } |
1153 | else |
1247 | else |
1154 | ev_timer_stop (EV_A_ w); |
1248 | ev_timer_stop (EV_A_ w); |
1155 | } |
1249 | } |
1156 | else if (w->repeat) |
1250 | else if (w->repeat) |
… | |
… | |
1165 | |
1259 | |
1166 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1260 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1167 | |
1261 | |
1168 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1262 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1169 | if (w->interval) |
1263 | if (w->interval) |
1170 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1264 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1171 | |
1265 | |
1172 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1266 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1173 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1267 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1174 | periodics [periodiccnt - 1] = w; |
1268 | periodics [periodiccnt - 1] = w; |
1175 | upheap ((WT *)periodics, periodiccnt - 1); |
1269 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1270 | |
|
|
1271 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1176 | } |
1272 | } |
1177 | |
1273 | |
1178 | void |
1274 | void |
1179 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1275 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1180 | { |
1276 | { |
1181 | ev_clear_pending (EV_A_ (W)w); |
1277 | ev_clear_pending (EV_A_ (W)w); |
1182 | if (!ev_is_active (w)) |
1278 | if (!ev_is_active (w)) |
1183 | return; |
1279 | return; |
1184 | |
1280 | |
|
|
1281 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1282 | |
1185 | if (w->active < periodiccnt--) |
1283 | if (((W)w)->active < periodiccnt--) |
1186 | { |
1284 | { |
1187 | periodics [w->active - 1] = periodics [periodiccnt]; |
1285 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1188 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1286 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1189 | } |
1287 | } |
1190 | |
1288 | |
1191 | ev_stop (EV_A_ (W)w); |
1289 | ev_stop (EV_A_ (W)w); |
1192 | } |
1290 | } |
1193 | |
1291 | |
… | |
… | |
1207 | { |
1305 | { |
1208 | ev_clear_pending (EV_A_ (W)w); |
1306 | ev_clear_pending (EV_A_ (W)w); |
1209 | if (ev_is_active (w)) |
1307 | if (ev_is_active (w)) |
1210 | return; |
1308 | return; |
1211 | |
1309 | |
1212 | idles [w->active - 1] = idles [--idlecnt]; |
1310 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1213 | ev_stop (EV_A_ (W)w); |
1311 | ev_stop (EV_A_ (W)w); |
1214 | } |
1312 | } |
1215 | |
1313 | |
1216 | void |
1314 | void |
1217 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1315 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1229 | { |
1327 | { |
1230 | ev_clear_pending (EV_A_ (W)w); |
1328 | ev_clear_pending (EV_A_ (W)w); |
1231 | if (ev_is_active (w)) |
1329 | if (ev_is_active (w)) |
1232 | return; |
1330 | return; |
1233 | |
1331 | |
1234 | prepares [w->active - 1] = prepares [--preparecnt]; |
1332 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1235 | ev_stop (EV_A_ (W)w); |
1333 | ev_stop (EV_A_ (W)w); |
1236 | } |
1334 | } |
1237 | |
1335 | |
1238 | void |
1336 | void |
1239 | ev_check_start (EV_P_ struct ev_check *w) |
1337 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1251 | { |
1349 | { |
1252 | ev_clear_pending (EV_A_ (W)w); |
1350 | ev_clear_pending (EV_A_ (W)w); |
1253 | if (ev_is_active (w)) |
1351 | if (ev_is_active (w)) |
1254 | return; |
1352 | return; |
1255 | |
1353 | |
1256 | checks [w->active - 1] = checks [--checkcnt]; |
1354 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1257 | ev_stop (EV_A_ (W)w); |
1355 | ev_stop (EV_A_ (W)w); |
1258 | } |
1356 | } |
1259 | |
1357 | |
1260 | #ifndef SA_RESTART |
1358 | #ifndef SA_RESTART |
1261 | # define SA_RESTART 0 |
1359 | # define SA_RESTART 0 |
… | |
… | |
1274 | |
1372 | |
1275 | ev_start (EV_A_ (W)w, 1); |
1373 | ev_start (EV_A_ (W)w, 1); |
1276 | array_needsize (signals, signalmax, w->signum, signals_init); |
1374 | array_needsize (signals, signalmax, w->signum, signals_init); |
1277 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1375 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1278 | |
1376 | |
1279 | if (!w->next) |
1377 | if (!((WL)w)->next) |
1280 | { |
1378 | { |
|
|
1379 | #if WIN32 |
|
|
1380 | signal (w->signum, sighandler); |
|
|
1381 | #else |
1281 | struct sigaction sa; |
1382 | struct sigaction sa; |
1282 | sa.sa_handler = sighandler; |
1383 | sa.sa_handler = sighandler; |
1283 | sigfillset (&sa.sa_mask); |
1384 | sigfillset (&sa.sa_mask); |
1284 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1385 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1285 | sigaction (w->signum, &sa, 0); |
1386 | sigaction (w->signum, &sa, 0); |
|
|
1387 | #endif |
1286 | } |
1388 | } |
1287 | } |
1389 | } |
1288 | |
1390 | |
1289 | void |
1391 | void |
1290 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1392 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1340 | void (*cb)(int revents, void *arg) = once->cb; |
1442 | void (*cb)(int revents, void *arg) = once->cb; |
1341 | void *arg = once->arg; |
1443 | void *arg = once->arg; |
1342 | |
1444 | |
1343 | ev_io_stop (EV_A_ &once->io); |
1445 | ev_io_stop (EV_A_ &once->io); |
1344 | ev_timer_stop (EV_A_ &once->to); |
1446 | ev_timer_stop (EV_A_ &once->to); |
1345 | free (once); |
1447 | ev_free (once); |
1346 | |
1448 | |
1347 | cb (revents, arg); |
1449 | cb (revents, arg); |
1348 | } |
1450 | } |
1349 | |
1451 | |
1350 | static void |
1452 | static void |
… | |
… | |
1360 | } |
1462 | } |
1361 | |
1463 | |
1362 | void |
1464 | void |
1363 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1465 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1364 | { |
1466 | { |
1365 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1467 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1366 | |
1468 | |
1367 | if (!once) |
1469 | if (!once) |
1368 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1470 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1369 | else |
1471 | else |
1370 | { |
1472 | { |