… | |
… | |
113 | } |
113 | } |
114 | |
114 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
117 | { \ |
117 | { \ |
118 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
|
|
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
119 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
120 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
121 | cur = newcnt; \ |
127 | cur = newcnt; \ |
122 | } |
128 | } |
123 | |
129 | |
124 | /*****************************************************************************/ |
130 | /*****************************************************************************/ |
125 | |
131 | |
126 | typedef struct |
132 | typedef struct |
127 | { |
133 | { |
128 | struct ev_io *head; |
134 | struct ev_io *head; |
129 | unsigned char wev, rev; /* want, received event set */ |
135 | int events; |
130 | } ANFD; |
136 | } ANFD; |
131 | |
137 | |
132 | static ANFD *anfds; |
138 | static ANFD *anfds; |
133 | static int anfdmax; |
139 | static int anfdmax; |
134 | |
140 | |
135 | static int *fdchanges; |
|
|
136 | static int fdchangemax, fdchangecnt; |
|
|
137 | |
|
|
138 | static void |
141 | static void |
139 | anfds_init (ANFD *base, int count) |
142 | anfds_init (ANFD *base, int count) |
140 | { |
143 | { |
141 | while (count--) |
144 | while (count--) |
142 | { |
145 | { |
143 | base->head = 0; |
146 | base->head = 0; |
144 | base->wev = base->rev = EV_NONE; |
147 | base->events = EV_NONE; |
145 | ++base; |
148 | ++base; |
146 | } |
149 | } |
147 | } |
150 | } |
148 | |
151 | |
149 | typedef struct |
152 | typedef struct |
… | |
… | |
166 | pendings [pendingcnt - 1].events = events; |
169 | pendings [pendingcnt - 1].events = events; |
167 | } |
170 | } |
168 | } |
171 | } |
169 | |
172 | |
170 | static void |
173 | static void |
|
|
174 | queue_events (W *events, int eventcnt, int type) |
|
|
175 | { |
|
|
176 | int i; |
|
|
177 | |
|
|
178 | for (i = 0; i < eventcnt; ++i) |
|
|
179 | event (events [i], type); |
|
|
180 | } |
|
|
181 | |
|
|
182 | static void |
171 | fd_event (int fd, int events) |
183 | fd_event (int fd, int events) |
172 | { |
184 | { |
173 | ANFD *anfd = anfds + fd; |
185 | ANFD *anfd = anfds + fd; |
174 | struct ev_io *w; |
186 | struct ev_io *w; |
175 | |
187 | |
… | |
… | |
180 | if (ev) |
192 | if (ev) |
181 | event ((W)w, ev); |
193 | event ((W)w, ev); |
182 | } |
194 | } |
183 | } |
195 | } |
184 | |
196 | |
|
|
197 | /*****************************************************************************/ |
|
|
198 | |
|
|
199 | static int *fdchanges; |
|
|
200 | static int fdchangemax, fdchangecnt; |
|
|
201 | |
185 | static void |
202 | static void |
186 | queue_events (W *events, int eventcnt, int type) |
203 | fd_reify (void) |
187 | { |
204 | { |
188 | int i; |
205 | int i; |
189 | |
206 | |
190 | for (i = 0; i < eventcnt; ++i) |
207 | for (i = 0; i < fdchangecnt; ++i) |
191 | event (events [i], type); |
208 | { |
|
|
209 | int fd = fdchanges [i]; |
|
|
210 | ANFD *anfd = anfds + fd; |
|
|
211 | struct ev_io *w; |
|
|
212 | |
|
|
213 | int events = 0; |
|
|
214 | |
|
|
215 | for (w = anfd->head; w; w = w->next) |
|
|
216 | events |= w->events; |
|
|
217 | |
|
|
218 | anfd->events &= ~EV_REIFY; |
|
|
219 | |
|
|
220 | if (anfd->events != events) |
|
|
221 | { |
|
|
222 | method_modify (fd, anfd->events, events); |
|
|
223 | anfd->events = events; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | fdchangecnt = 0; |
|
|
228 | } |
|
|
229 | |
|
|
230 | static void |
|
|
231 | fd_change (int fd) |
|
|
232 | { |
|
|
233 | if (anfds [fd].events & EV_REIFY) |
|
|
234 | return; |
|
|
235 | |
|
|
236 | anfds [fd].events |= EV_REIFY; |
|
|
237 | |
|
|
238 | ++fdchangecnt; |
|
|
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
240 | fdchanges [fdchangecnt - 1] = fd; |
192 | } |
241 | } |
193 | |
242 | |
194 | /* called on EBADF to verify fds */ |
243 | /* called on EBADF to verify fds */ |
195 | static void |
244 | static void |
196 | fd_recheck () |
245 | fd_recheck (void) |
197 | { |
246 | { |
198 | int fd; |
247 | int fd; |
199 | |
248 | |
200 | for (fd = 0; fd < anfdmax; ++fd) |
249 | for (fd = 0; fd < anfdmax; ++fd) |
201 | if (anfds [fd].wev) |
250 | if (anfds [fd].events) |
202 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
203 | while (anfds [fd].head) |
252 | while (anfds [fd].head) |
|
|
253 | { |
|
|
254 | event ((W)anfds [fd].head, EV_ERROR); |
204 | evio_stop (anfds [fd].head); |
255 | ev_io_stop (anfds [fd].head); |
|
|
256 | } |
205 | } |
257 | } |
206 | |
258 | |
207 | /*****************************************************************************/ |
259 | /*****************************************************************************/ |
208 | |
260 | |
209 | static struct ev_timer **timers; |
261 | static struct ev_timer **timers; |
… | |
… | |
318 | |
370 | |
319 | /* rather than sort out wether we really need nb, set it */ |
371 | /* rather than sort out wether we really need nb, set it */ |
320 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
321 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
322 | |
374 | |
323 | evio_set (&sigev, sigpipe [0], EV_READ); |
375 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
324 | evio_start (&sigev); |
376 | ev_io_start (&sigev); |
325 | } |
377 | } |
326 | |
378 | |
327 | /*****************************************************************************/ |
379 | /*****************************************************************************/ |
328 | |
380 | |
329 | static struct ev_idle **idles; |
381 | static struct ev_idle **idles; |
… | |
… | |
366 | #endif |
418 | #endif |
367 | #if HAVE_SELECT |
419 | #if HAVE_SELECT |
368 | # include "ev_select.c" |
420 | # include "ev_select.c" |
369 | #endif |
421 | #endif |
370 | |
422 | |
|
|
423 | int |
|
|
424 | ev_version_major (void) |
|
|
425 | { |
|
|
426 | return EV_VERSION_MAJOR; |
|
|
427 | } |
|
|
428 | |
|
|
429 | int |
|
|
430 | ev_version_minor (void) |
|
|
431 | { |
|
|
432 | return EV_VERSION_MINOR; |
|
|
433 | } |
|
|
434 | |
371 | int ev_init (int flags) |
435 | int ev_init (int flags) |
372 | { |
436 | { |
|
|
437 | if (!ev_method) |
|
|
438 | { |
373 | #if HAVE_MONOTONIC |
439 | #if HAVE_MONOTONIC |
374 | { |
440 | { |
375 | struct timespec ts; |
441 | struct timespec ts; |
376 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
377 | have_monotonic = 1; |
443 | have_monotonic = 1; |
378 | } |
444 | } |
379 | #endif |
445 | #endif |
380 | |
446 | |
381 | ev_now = ev_time (); |
447 | ev_now = ev_time (); |
382 | now = get_clock (); |
448 | now = get_clock (); |
383 | diff = ev_now - now; |
449 | diff = ev_now - now; |
384 | |
450 | |
385 | if (pipe (sigpipe)) |
451 | if (pipe (sigpipe)) |
386 | return 0; |
452 | return 0; |
387 | |
453 | |
388 | ev_method = EVMETHOD_NONE; |
454 | ev_method = EVMETHOD_NONE; |
389 | #if HAVE_EPOLL |
455 | #if HAVE_EPOLL |
390 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
391 | #endif |
457 | #endif |
392 | #if HAVE_SELECT |
458 | #if HAVE_SELECT |
393 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
394 | #endif |
460 | #endif |
395 | |
461 | |
396 | if (ev_method) |
462 | if (ev_method) |
397 | { |
463 | { |
398 | evw_init (&sigev, sigcb); |
464 | ev_watcher_init (&sigev, sigcb); |
399 | siginit (); |
465 | siginit (); |
400 | |
466 | |
401 | evsignal_init (&childev, childcb, SIGCHLD); |
467 | ev_signal_init (&childev, childcb, SIGCHLD); |
402 | evsignal_start (&childev); |
468 | ev_signal_start (&childev); |
|
|
469 | } |
403 | } |
470 | } |
404 | |
471 | |
405 | return ev_method; |
472 | return ev_method; |
406 | } |
473 | } |
407 | |
474 | |
408 | /*****************************************************************************/ |
475 | /*****************************************************************************/ |
409 | |
476 | |
|
|
477 | void |
410 | void ev_prefork (void) |
478 | ev_prefork (void) |
411 | { |
479 | { |
412 | /* nop */ |
480 | /* nop */ |
413 | } |
481 | } |
414 | |
482 | |
|
|
483 | void |
415 | void ev_postfork_parent (void) |
484 | ev_postfork_parent (void) |
416 | { |
485 | { |
417 | /* nop */ |
486 | /* nop */ |
418 | } |
487 | } |
419 | |
488 | |
|
|
489 | void |
420 | void ev_postfork_child (void) |
490 | ev_postfork_child (void) |
421 | { |
491 | { |
422 | #if HAVE_EPOLL |
492 | #if HAVE_EPOLL |
423 | if (ev_method == EVMETHOD_EPOLL) |
493 | if (ev_method == EVMETHOD_EPOLL) |
424 | epoll_postfork_child (); |
494 | epoll_postfork_child (); |
425 | #endif |
495 | #endif |
426 | |
496 | |
427 | evio_stop (&sigev); |
497 | ev_io_stop (&sigev); |
428 | close (sigpipe [0]); |
498 | close (sigpipe [0]); |
429 | close (sigpipe [1]); |
499 | close (sigpipe [1]); |
430 | pipe (sigpipe); |
500 | pipe (sigpipe); |
431 | siginit (); |
501 | siginit (); |
432 | } |
502 | } |
433 | |
503 | |
434 | /*****************************************************************************/ |
504 | /*****************************************************************************/ |
435 | |
505 | |
436 | static void |
506 | static void |
437 | fd_reify (void) |
|
|
438 | { |
|
|
439 | int i; |
|
|
440 | |
|
|
441 | for (i = 0; i < fdchangecnt; ++i) |
|
|
442 | { |
|
|
443 | int fd = fdchanges [i]; |
|
|
444 | ANFD *anfd = anfds + fd; |
|
|
445 | struct ev_io *w; |
|
|
446 | |
|
|
447 | int wev = 0; |
|
|
448 | |
|
|
449 | for (w = anfd->head; w; w = w->next) |
|
|
450 | wev |= w->events; |
|
|
451 | |
|
|
452 | if (anfd->wev != wev) |
|
|
453 | { |
|
|
454 | method_modify (fd, anfd->wev, wev); |
|
|
455 | anfd->wev = wev; |
|
|
456 | } |
|
|
457 | } |
|
|
458 | |
|
|
459 | fdchangecnt = 0; |
|
|
460 | } |
|
|
461 | |
|
|
462 | static void |
|
|
463 | call_pending () |
507 | call_pending (void) |
464 | { |
508 | { |
465 | while (pendingcnt) |
509 | while (pendingcnt) |
466 | { |
510 | { |
467 | ANPENDING *p = pendings + --pendingcnt; |
511 | ANPENDING *p = pendings + --pendingcnt; |
468 | |
512 | |
… | |
… | |
473 | } |
517 | } |
474 | } |
518 | } |
475 | } |
519 | } |
476 | |
520 | |
477 | static void |
521 | static void |
478 | timers_reify () |
522 | timers_reify (void) |
479 | { |
523 | { |
480 | while (timercnt && timers [0]->at <= now) |
524 | while (timercnt && timers [0]->at <= now) |
481 | { |
525 | { |
482 | struct ev_timer *w = timers [0]; |
526 | struct ev_timer *w = timers [0]; |
483 | |
527 | |
… | |
… | |
489 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
490 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
491 | downheap ((WT *)timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
492 | } |
536 | } |
493 | else |
537 | else |
494 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
495 | } |
539 | } |
496 | } |
540 | } |
497 | |
541 | |
498 | static void |
542 | static void |
499 | periodics_reify () |
543 | periodics_reify (void) |
500 | { |
544 | { |
501 | while (periodiccnt && periodics [0]->at <= ev_now) |
545 | while (periodiccnt && periodics [0]->at <= ev_now) |
502 | { |
546 | { |
503 | struct ev_periodic *w = periodics [0]; |
547 | struct ev_periodic *w = periodics [0]; |
504 | |
548 | |
… | |
… | |
508 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
509 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
553 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
510 | downheap ((WT *)periodics, periodiccnt, 0); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
511 | } |
555 | } |
512 | else |
556 | else |
513 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
514 | |
558 | |
515 | event ((W)w, EV_TIMEOUT); |
559 | event ((W)w, EV_TIMEOUT); |
516 | } |
560 | } |
517 | } |
561 | } |
518 | |
562 | |
… | |
… | |
530 | { |
574 | { |
531 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
532 | |
576 | |
533 | if (fabs (diff) >= 1e-4) |
577 | if (fabs (diff) >= 1e-4) |
534 | { |
578 | { |
535 | evperiodic_stop (w); |
579 | ev_periodic_stop (w); |
536 | evperiodic_start (w); |
580 | ev_periodic_start (w); |
537 | |
581 | |
538 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
539 | } |
583 | } |
540 | } |
584 | } |
541 | } |
585 | } |
542 | } |
586 | } |
543 | |
587 | |
544 | static void |
588 | static void |
545 | time_update () |
589 | time_update (void) |
546 | { |
590 | { |
547 | int i; |
591 | int i; |
548 | |
592 | |
549 | ev_now = ev_time (); |
593 | ev_now = ev_time (); |
550 | |
594 | |
… | |
… | |
584 | int ev_loop_done; |
628 | int ev_loop_done; |
585 | |
629 | |
586 | void ev_loop (int flags) |
630 | void ev_loop (int flags) |
587 | { |
631 | { |
588 | double block; |
632 | double block; |
589 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
590 | |
634 | |
591 | do |
635 | do |
592 | { |
636 | { |
593 | /* queue check watchers (and execute them) */ |
637 | /* queue check watchers (and execute them) */ |
594 | if (preparecnt) |
638 | if (preparecnt) |
… | |
… | |
699 | } |
743 | } |
700 | |
744 | |
701 | /*****************************************************************************/ |
745 | /*****************************************************************************/ |
702 | |
746 | |
703 | void |
747 | void |
704 | evio_start (struct ev_io *w) |
748 | ev_io_start (struct ev_io *w) |
705 | { |
749 | { |
706 | if (ev_is_active (w)) |
750 | if (ev_is_active (w)) |
707 | return; |
751 | return; |
708 | |
752 | |
709 | int fd = w->fd; |
753 | int fd = w->fd; |
710 | |
754 | |
711 | ev_start ((W)w, 1); |
755 | ev_start ((W)w, 1); |
712 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
713 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
714 | |
758 | |
715 | ++fdchangecnt; |
759 | fd_change (fd); |
716 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
717 | fdchanges [fdchangecnt - 1] = fd; |
|
|
718 | } |
760 | } |
719 | |
761 | |
720 | void |
762 | void |
721 | evio_stop (struct ev_io *w) |
763 | ev_io_stop (struct ev_io *w) |
722 | { |
764 | { |
723 | ev_clear ((W)w); |
765 | ev_clear ((W)w); |
724 | if (!ev_is_active (w)) |
766 | if (!ev_is_active (w)) |
725 | return; |
767 | return; |
726 | |
768 | |
727 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
728 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
729 | |
771 | |
730 | ++fdchangecnt; |
772 | fd_change (w->fd); |
731 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
732 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
733 | } |
773 | } |
734 | |
774 | |
735 | void |
775 | void |
736 | evtimer_start (struct ev_timer *w) |
776 | ev_timer_start (struct ev_timer *w) |
737 | { |
777 | { |
738 | if (ev_is_active (w)) |
778 | if (ev_is_active (w)) |
739 | return; |
779 | return; |
740 | |
780 | |
741 | w->at += now; |
781 | w->at += now; |
… | |
… | |
747 | timers [timercnt - 1] = w; |
787 | timers [timercnt - 1] = w; |
748 | upheap ((WT *)timers, timercnt - 1); |
788 | upheap ((WT *)timers, timercnt - 1); |
749 | } |
789 | } |
750 | |
790 | |
751 | void |
791 | void |
752 | evtimer_stop (struct ev_timer *w) |
792 | ev_timer_stop (struct ev_timer *w) |
753 | { |
793 | { |
754 | ev_clear ((W)w); |
794 | ev_clear ((W)w); |
755 | if (!ev_is_active (w)) |
795 | if (!ev_is_active (w)) |
756 | return; |
796 | return; |
757 | |
797 | |
… | |
… | |
765 | |
805 | |
766 | ev_stop ((W)w); |
806 | ev_stop ((W)w); |
767 | } |
807 | } |
768 | |
808 | |
769 | void |
809 | void |
770 | evtimer_again (struct ev_timer *w) |
810 | ev_timer_again (struct ev_timer *w) |
771 | { |
811 | { |
772 | if (ev_is_active (w)) |
812 | if (ev_is_active (w)) |
773 | { |
813 | { |
774 | if (w->repeat) |
814 | if (w->repeat) |
775 | { |
815 | { |
776 | w->at = now + w->repeat; |
816 | w->at = now + w->repeat; |
777 | downheap ((WT *)timers, timercnt, w->active - 1); |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
778 | } |
818 | } |
779 | else |
819 | else |
780 | evtimer_stop (w); |
820 | ev_timer_stop (w); |
781 | } |
821 | } |
782 | else if (w->repeat) |
822 | else if (w->repeat) |
783 | evtimer_start (w); |
823 | ev_timer_start (w); |
784 | } |
824 | } |
785 | |
825 | |
786 | void |
826 | void |
787 | evperiodic_start (struct ev_periodic *w) |
827 | ev_periodic_start (struct ev_periodic *w) |
788 | { |
828 | { |
789 | if (ev_is_active (w)) |
829 | if (ev_is_active (w)) |
790 | return; |
830 | return; |
791 | |
831 | |
792 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
800 | periodics [periodiccnt - 1] = w; |
840 | periodics [periodiccnt - 1] = w; |
801 | upheap ((WT *)periodics, periodiccnt - 1); |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
802 | } |
842 | } |
803 | |
843 | |
804 | void |
844 | void |
805 | evperiodic_stop (struct ev_periodic *w) |
845 | ev_periodic_stop (struct ev_periodic *w) |
806 | { |
846 | { |
807 | ev_clear ((W)w); |
847 | ev_clear ((W)w); |
808 | if (!ev_is_active (w)) |
848 | if (!ev_is_active (w)) |
809 | return; |
849 | return; |
810 | |
850 | |
… | |
… | |
816 | |
856 | |
817 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
818 | } |
858 | } |
819 | |
859 | |
820 | void |
860 | void |
821 | evsignal_start (struct ev_signal *w) |
861 | ev_signal_start (struct ev_signal *w) |
822 | { |
862 | { |
823 | if (ev_is_active (w)) |
863 | if (ev_is_active (w)) |
824 | return; |
864 | return; |
825 | |
865 | |
826 | ev_start ((W)w, 1); |
866 | ev_start ((W)w, 1); |
… | |
… | |
836 | sigaction (w->signum, &sa, 0); |
876 | sigaction (w->signum, &sa, 0); |
837 | } |
877 | } |
838 | } |
878 | } |
839 | |
879 | |
840 | void |
880 | void |
841 | evsignal_stop (struct ev_signal *w) |
881 | ev_signal_stop (struct ev_signal *w) |
842 | { |
882 | { |
843 | ev_clear ((W)w); |
883 | ev_clear ((W)w); |
844 | if (!ev_is_active (w)) |
884 | if (!ev_is_active (w)) |
845 | return; |
885 | return; |
846 | |
886 | |
… | |
… | |
849 | |
889 | |
850 | if (!signals [w->signum - 1].head) |
890 | if (!signals [w->signum - 1].head) |
851 | signal (w->signum, SIG_DFL); |
891 | signal (w->signum, SIG_DFL); |
852 | } |
892 | } |
853 | |
893 | |
|
|
894 | void |
854 | void evidle_start (struct ev_idle *w) |
895 | ev_idle_start (struct ev_idle *w) |
855 | { |
896 | { |
856 | if (ev_is_active (w)) |
897 | if (ev_is_active (w)) |
857 | return; |
898 | return; |
858 | |
899 | |
859 | ev_start ((W)w, ++idlecnt); |
900 | ev_start ((W)w, ++idlecnt); |
860 | array_needsize (idles, idlemax, idlecnt, ); |
901 | array_needsize (idles, idlemax, idlecnt, ); |
861 | idles [idlecnt - 1] = w; |
902 | idles [idlecnt - 1] = w; |
862 | } |
903 | } |
863 | |
904 | |
|
|
905 | void |
864 | void evidle_stop (struct ev_idle *w) |
906 | ev_idle_stop (struct ev_idle *w) |
865 | { |
907 | { |
866 | ev_clear ((W)w); |
908 | ev_clear ((W)w); |
867 | if (ev_is_active (w)) |
909 | if (ev_is_active (w)) |
868 | return; |
910 | return; |
869 | |
911 | |
870 | idles [w->active - 1] = idles [--idlecnt]; |
912 | idles [w->active - 1] = idles [--idlecnt]; |
871 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
872 | } |
914 | } |
873 | |
915 | |
|
|
916 | void |
874 | void evprepare_start (struct ev_prepare *w) |
917 | ev_prepare_start (struct ev_prepare *w) |
875 | { |
918 | { |
876 | if (ev_is_active (w)) |
919 | if (ev_is_active (w)) |
877 | return; |
920 | return; |
878 | |
921 | |
879 | ev_start ((W)w, ++preparecnt); |
922 | ev_start ((W)w, ++preparecnt); |
880 | array_needsize (prepares, preparemax, preparecnt, ); |
923 | array_needsize (prepares, preparemax, preparecnt, ); |
881 | prepares [preparecnt - 1] = w; |
924 | prepares [preparecnt - 1] = w; |
882 | } |
925 | } |
883 | |
926 | |
|
|
927 | void |
884 | void evprepare_stop (struct ev_prepare *w) |
928 | ev_prepare_stop (struct ev_prepare *w) |
885 | { |
929 | { |
886 | ev_clear ((W)w); |
930 | ev_clear ((W)w); |
887 | if (ev_is_active (w)) |
931 | if (ev_is_active (w)) |
888 | return; |
932 | return; |
889 | |
933 | |
890 | prepares [w->active - 1] = prepares [--preparecnt]; |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
891 | ev_stop ((W)w); |
935 | ev_stop ((W)w); |
892 | } |
936 | } |
893 | |
937 | |
|
|
938 | void |
894 | void evcheck_start (struct ev_check *w) |
939 | ev_check_start (struct ev_check *w) |
895 | { |
940 | { |
896 | if (ev_is_active (w)) |
941 | if (ev_is_active (w)) |
897 | return; |
942 | return; |
898 | |
943 | |
899 | ev_start ((W)w, ++checkcnt); |
944 | ev_start ((W)w, ++checkcnt); |
900 | array_needsize (checks, checkmax, checkcnt, ); |
945 | array_needsize (checks, checkmax, checkcnt, ); |
901 | checks [checkcnt - 1] = w; |
946 | checks [checkcnt - 1] = w; |
902 | } |
947 | } |
903 | |
948 | |
|
|
949 | void |
904 | void evcheck_stop (struct ev_check *w) |
950 | ev_check_stop (struct ev_check *w) |
905 | { |
951 | { |
906 | ev_clear ((W)w); |
952 | ev_clear ((W)w); |
907 | if (ev_is_active (w)) |
953 | if (ev_is_active (w)) |
908 | return; |
954 | return; |
909 | |
955 | |
910 | checks [w->active - 1] = checks [--checkcnt]; |
956 | checks [w->active - 1] = checks [--checkcnt]; |
911 | ev_stop ((W)w); |
957 | ev_stop ((W)w); |
912 | } |
958 | } |
913 | |
959 | |
|
|
960 | void |
914 | void evchild_start (struct ev_child *w) |
961 | ev_child_start (struct ev_child *w) |
915 | { |
962 | { |
916 | if (ev_is_active (w)) |
963 | if (ev_is_active (w)) |
917 | return; |
964 | return; |
918 | |
965 | |
919 | ev_start ((W)w, 1); |
966 | ev_start ((W)w, 1); |
920 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
967 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
921 | } |
968 | } |
922 | |
969 | |
|
|
970 | void |
923 | void evchild_stop (struct ev_child *w) |
971 | ev_child_stop (struct ev_child *w) |
924 | { |
972 | { |
925 | ev_clear ((W)w); |
973 | ev_clear ((W)w); |
926 | if (ev_is_active (w)) |
974 | if (ev_is_active (w)) |
927 | return; |
975 | return; |
928 | |
976 | |
… | |
… | |
944 | once_cb (struct ev_once *once, int revents) |
992 | once_cb (struct ev_once *once, int revents) |
945 | { |
993 | { |
946 | void (*cb)(int revents, void *arg) = once->cb; |
994 | void (*cb)(int revents, void *arg) = once->cb; |
947 | void *arg = once->arg; |
995 | void *arg = once->arg; |
948 | |
996 | |
949 | evio_stop (&once->io); |
997 | ev_io_stop (&once->io); |
950 | evtimer_stop (&once->to); |
998 | ev_timer_stop (&once->to); |
951 | free (once); |
999 | free (once); |
952 | |
1000 | |
953 | cb (revents, arg); |
1001 | cb (revents, arg); |
954 | } |
1002 | } |
955 | |
1003 | |
… | |
… | |
975 | else |
1023 | else |
976 | { |
1024 | { |
977 | once->cb = cb; |
1025 | once->cb = cb; |
978 | once->arg = arg; |
1026 | once->arg = arg; |
979 | |
1027 | |
980 | evw_init (&once->io, once_cb_io); |
1028 | ev_watcher_init (&once->io, once_cb_io); |
981 | |
1029 | |
982 | if (fd >= 0) |
1030 | if (fd >= 0) |
983 | { |
1031 | { |
984 | evio_set (&once->io, fd, events); |
1032 | ev_io_set (&once->io, fd, events); |
985 | evio_start (&once->io); |
1033 | ev_io_start (&once->io); |
986 | } |
1034 | } |
987 | |
1035 | |
988 | evw_init (&once->to, once_cb_to); |
1036 | ev_watcher_init (&once->to, once_cb_to); |
989 | |
1037 | |
990 | if (timeout >= 0.) |
1038 | if (timeout >= 0.) |
991 | { |
1039 | { |
992 | evtimer_set (&once->to, timeout, 0.); |
1040 | ev_timer_set (&once->to, timeout, 0.); |
993 | evtimer_start (&once->to); |
1041 | ev_timer_start (&once->to); |
994 | } |
1042 | } |
995 | } |
1043 | } |
996 | } |
1044 | } |
997 | |
1045 | |
998 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
… | |
… | |
1009 | |
1057 | |
1010 | static void |
1058 | static void |
1011 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
1012 | { |
1060 | { |
1013 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1061 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1014 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
1015 | evtimer_start (w); |
1063 | ev_timer_start (w); |
1016 | } |
1064 | } |
1017 | |
1065 | |
1018 | static void |
1066 | static void |
1019 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
1020 | { |
1068 | { |
1021 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1022 | evio_stop (&wio); |
1070 | ev_io_stop (&wio); |
1023 | evio_start (&wio); |
1071 | ev_io_start (&wio); |
1024 | } |
1072 | } |
1025 | |
1073 | |
1026 | static void |
1074 | static void |
1027 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
1028 | { |
1076 | { |
… | |
… | |
1032 | |
1080 | |
1033 | int main (void) |
1081 | int main (void) |
1034 | { |
1082 | { |
1035 | ev_init (0); |
1083 | ev_init (0); |
1036 | |
1084 | |
1037 | evio_init (&wio, sin_cb, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1038 | evio_start (&wio); |
1086 | ev_io_start (&wio); |
1039 | |
1087 | |
1040 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
1041 | |
1089 | |
1042 | #if 0 |
1090 | #if 0 |
1043 | int i; |
1091 | int i; |
1044 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
1045 | { |
1093 | { |
1046 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
1047 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
1048 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1049 | evtimer_start (w); |
1097 | ev_timer_start (w); |
1050 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
1051 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
1052 | } |
1100 | } |
1053 | #endif |
1101 | #endif |
1054 | |
1102 | |
1055 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
1056 | evtimer_init (&t1, ocb, 5, 10); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
1057 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
1058 | |
1106 | |
1059 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
1060 | evsignal_init (&sig, scb, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
1061 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
1062 | |
1110 | |
1063 | struct ev_check cw; |
1111 | struct ev_check cw; |
1064 | evcheck_init (&cw, gcb); |
1112 | ev_check_init (&cw, gcb); |
1065 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
1066 | |
1114 | |
1067 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
1068 | evidle_init (&iw, gcb); |
1116 | ev_idle_init (&iw, gcb); |
1069 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
1070 | |
1118 | |
1071 | ev_loop (0); |
1119 | ev_loop (0); |
1072 | |
1120 | |
1073 | return 0; |
1121 | return 0; |
1074 | } |
1122 | } |