… | |
… | |
9 | #include <assert.h> |
9 | #include <assert.h> |
10 | #include <errno.h> |
10 | #include <errno.h> |
11 | #include <sys/time.h> |
11 | #include <sys/time.h> |
12 | #include <time.h> |
12 | #include <time.h> |
13 | |
13 | |
|
|
14 | #ifndef HAVE_MONOTONIC |
14 | #ifdef CLOCK_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
15 | # define HAVE_MONOTONIC 1 |
16 | # define HAVE_MONOTONIC 1 |
16 | #endif |
17 | # endif |
|
|
18 | #endif |
17 | |
19 | |
18 | #define HAVE_REALTIME 1 |
20 | #ifndef HAVE_SELECT |
19 | #define HAVE_EPOLL 1 |
|
|
20 | #define HAVE_SELECT 1 |
21 | # define HAVE_SELECT 1 |
|
|
22 | #endif |
|
|
23 | |
|
|
24 | #ifndef HAVE_EPOLL |
|
|
25 | # define HAVE_EPOLL 0 |
|
|
26 | #endif |
|
|
27 | |
|
|
28 | #ifndef HAVE_REALTIME |
|
|
29 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
30 | #endif |
21 | |
31 | |
22 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
23 | #define MAX_BLOCKTIME 60. |
33 | #define MAX_BLOCKTIME 60. |
24 | |
34 | |
25 | #include "ev.h" |
35 | #include "ev.h" |
… | |
… | |
29 | }; |
39 | }; |
30 | |
40 | |
31 | struct ev_watcher_list { |
41 | struct ev_watcher_list { |
32 | EV_WATCHER_LIST (ev_watcher_list); |
42 | EV_WATCHER_LIST (ev_watcher_list); |
33 | }; |
43 | }; |
|
|
44 | |
|
|
45 | typedef struct ev_watcher *W; |
|
|
46 | typedef struct ev_watcher_list *WL; |
34 | |
47 | |
35 | static ev_tstamp now, diff; /* monotonic clock */ |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
36 | ev_tstamp ev_now; |
49 | ev_tstamp ev_now; |
37 | int ev_method; |
50 | int ev_method; |
38 | |
51 | |
… | |
… | |
108 | } |
121 | } |
109 | } |
122 | } |
110 | |
123 | |
111 | typedef struct |
124 | typedef struct |
112 | { |
125 | { |
113 | struct ev_watcher *w; |
126 | W w; |
114 | int events; |
127 | int events; |
115 | } ANPENDING; |
128 | } ANPENDING; |
116 | |
129 | |
117 | static ANPENDING *pendings; |
130 | static ANPENDING *pendings; |
118 | static int pendingmax, pendingcnt; |
131 | static int pendingmax, pendingcnt; |
119 | |
132 | |
120 | static void |
133 | static void |
121 | event (struct ev_watcher *w, int events) |
134 | event (W w, int events) |
122 | { |
135 | { |
123 | w->pending = ++pendingcnt; |
136 | w->pending = ++pendingcnt; |
124 | array_needsize (pendings, pendingmax, pendingcnt, ); |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
125 | pendings [pendingcnt - 1].w = w; |
138 | pendings [pendingcnt - 1].w = w; |
126 | pendings [pendingcnt - 1].events = events; |
139 | pendings [pendingcnt - 1].events = events; |
… | |
… | |
135 | for (w = anfd->head; w; w = w->next) |
148 | for (w = anfd->head; w; w = w->next) |
136 | { |
149 | { |
137 | int ev = w->events & events; |
150 | int ev = w->events & events; |
138 | |
151 | |
139 | if (ev) |
152 | if (ev) |
140 | event ((struct ev_watcher *)w, ev); |
153 | event ((W)w, ev); |
141 | } |
154 | } |
|
|
155 | } |
|
|
156 | |
|
|
157 | static void |
|
|
158 | queue_events (W *events, int eventcnt, int type) |
|
|
159 | { |
|
|
160 | int i; |
|
|
161 | |
|
|
162 | for (i = 0; i < eventcnt; ++i) |
|
|
163 | event (events [i], type); |
142 | } |
164 | } |
143 | |
165 | |
144 | /*****************************************************************************/ |
166 | /*****************************************************************************/ |
145 | |
167 | |
146 | static struct ev_timer **atimers; |
168 | static struct ev_timer **atimers; |
… | |
… | |
241 | if (signals [sig].gotsig) |
263 | if (signals [sig].gotsig) |
242 | { |
264 | { |
243 | signals [sig].gotsig = 0; |
265 | signals [sig].gotsig = 0; |
244 | |
266 | |
245 | for (w = signals [sig].head; w; w = w->next) |
267 | for (w = signals [sig].head; w; w = w->next) |
246 | event ((struct ev_watcher *)w, EV_SIGNAL); |
268 | event ((W)w, EV_SIGNAL); |
247 | } |
269 | } |
248 | } |
270 | } |
249 | |
271 | |
250 | static void |
272 | static void |
251 | siginit (void) |
273 | siginit (void) |
… | |
… | |
258 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
280 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
259 | |
281 | |
260 | evio_set (&sigev, sigpipe [0], EV_READ); |
282 | evio_set (&sigev, sigpipe [0], EV_READ); |
261 | evio_start (&sigev); |
283 | evio_start (&sigev); |
262 | } |
284 | } |
|
|
285 | |
|
|
286 | /*****************************************************************************/ |
|
|
287 | |
|
|
288 | static struct ev_idle **idles; |
|
|
289 | static int idlemax, idlecnt; |
|
|
290 | |
|
|
291 | static struct ev_check **checks; |
|
|
292 | static int checkmax, checkcnt; |
263 | |
293 | |
264 | /*****************************************************************************/ |
294 | /*****************************************************************************/ |
265 | |
295 | |
266 | #if HAVE_EPOLL |
296 | #if HAVE_EPOLL |
267 | # include "ev_epoll.c" |
297 | # include "ev_epoll.c" |
… | |
… | |
306 | |
336 | |
307 | /*****************************************************************************/ |
337 | /*****************************************************************************/ |
308 | |
338 | |
309 | void ev_prefork (void) |
339 | void ev_prefork (void) |
310 | { |
340 | { |
|
|
341 | /* nop */ |
311 | } |
342 | } |
312 | |
343 | |
313 | void ev_postfork_parent (void) |
344 | void ev_postfork_parent (void) |
314 | { |
345 | { |
|
|
346 | /* nop */ |
315 | } |
347 | } |
316 | |
348 | |
317 | void ev_postfork_child (void) |
349 | void ev_postfork_child (void) |
318 | { |
350 | { |
319 | #if HAVE_EPOLL |
351 | #if HAVE_EPOLL |
… | |
… | |
328 | siginit (); |
360 | siginit (); |
329 | } |
361 | } |
330 | |
362 | |
331 | /*****************************************************************************/ |
363 | /*****************************************************************************/ |
332 | |
364 | |
333 | static ev_hook hooks [EVHOOK_NUM]; |
|
|
334 | |
|
|
335 | void |
|
|
336 | ev_hook_register (int type, ev_hook hook) |
|
|
337 | { |
|
|
338 | hooks [type] = hook; |
|
|
339 | } |
|
|
340 | |
|
|
341 | void |
|
|
342 | ev_hook_unregister (int type, ev_hook hook) |
|
|
343 | { |
|
|
344 | hooks [type] = 0; |
|
|
345 | } |
|
|
346 | |
|
|
347 | static void |
|
|
348 | hook_call (int type) |
|
|
349 | { |
|
|
350 | if (hooks [type]) |
|
|
351 | hooks [type] (); |
|
|
352 | } |
|
|
353 | |
|
|
354 | static void |
365 | static void |
355 | fd_reify (void) |
366 | fd_reify (void) |
356 | { |
367 | { |
357 | int i; |
368 | int i; |
358 | |
369 | |
… | |
… | |
419 | { |
430 | { |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | --timercnt; /* maybe pass by reference instead? */ |
432 | --timercnt; /* maybe pass by reference instead? */ |
422 | } |
433 | } |
423 | |
434 | |
424 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
435 | event ((W)w, EV_TIMEOUT); |
425 | } |
436 | } |
426 | } |
437 | } |
427 | |
438 | |
428 | static void |
439 | static void |
429 | time_update () |
440 | time_update () |
… | |
… | |
470 | void ev_loop (int flags) |
481 | void ev_loop (int flags) |
471 | { |
482 | { |
472 | double block; |
483 | double block; |
473 | ev_loop_done = flags & EVLOOP_ONESHOT; |
484 | ev_loop_done = flags & EVLOOP_ONESHOT; |
474 | |
485 | |
|
|
486 | if (checkcnt) |
|
|
487 | { |
|
|
488 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
489 | call_pending (); |
|
|
490 | } |
|
|
491 | |
475 | do |
492 | do |
476 | { |
493 | { |
477 | hook_call (EVHOOK_PREPOLL); |
|
|
478 | |
|
|
479 | /* update fd-related kernel structures */ |
494 | /* update fd-related kernel structures */ |
480 | fd_reify (); |
495 | fd_reify (); |
481 | |
496 | |
482 | /* calculate blocking time */ |
497 | /* calculate blocking time */ |
483 | if (flags & EVLOOP_NONBLOCK) |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
484 | block = 0.; |
499 | block = 0.; |
485 | else |
500 | else |
486 | { |
501 | { |
487 | block = MAX_BLOCKTIME; |
502 | block = MAX_BLOCKTIME; |
488 | |
503 | |
… | |
… | |
504 | method_poll (block); |
519 | method_poll (block); |
505 | |
520 | |
506 | /* update ev_now, do magic */ |
521 | /* update ev_now, do magic */ |
507 | time_update (); |
522 | time_update (); |
508 | |
523 | |
509 | hook_call (EVHOOK_POSTPOLL); |
|
|
510 | |
|
|
511 | /* put pending timers into pendign queue and reschedule them */ |
524 | /* queue pending timers and reschedule them */ |
512 | /* absolute timers first */ |
525 | /* absolute timers first */ |
513 | timers_reify (atimers, atimercnt, ev_now); |
526 | timers_reify (atimers, atimercnt, ev_now); |
514 | /* relative timers second */ |
527 | /* relative timers second */ |
515 | timers_reify (rtimers, rtimercnt, now); |
528 | timers_reify (rtimers, rtimercnt, now); |
516 | |
529 | |
|
|
530 | /* queue idle watchers unless io or timers are pending */ |
|
|
531 | if (!pendingcnt) |
|
|
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
|
|
533 | |
|
|
534 | /* queue check and possibly idle watchers */ |
|
|
535 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
536 | |
517 | call_pending (); |
537 | call_pending (); |
518 | } |
538 | } |
519 | while (!ev_loop_done); |
539 | while (!ev_loop_done); |
520 | } |
540 | } |
521 | |
541 | |
522 | /*****************************************************************************/ |
542 | /*****************************************************************************/ |
523 | |
543 | |
524 | static void |
544 | static void |
525 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
545 | wlist_add (WL *head, WL elem) |
526 | { |
546 | { |
527 | elem->next = *head; |
547 | elem->next = *head; |
528 | *head = elem; |
548 | *head = elem; |
529 | } |
549 | } |
530 | |
550 | |
531 | static void |
551 | static void |
532 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
552 | wlist_del (WL *head, WL elem) |
533 | { |
553 | { |
534 | while (*head) |
554 | while (*head) |
535 | { |
555 | { |
536 | if (*head == elem) |
556 | if (*head == elem) |
537 | { |
557 | { |
… | |
… | |
542 | head = &(*head)->next; |
562 | head = &(*head)->next; |
543 | } |
563 | } |
544 | } |
564 | } |
545 | |
565 | |
546 | static void |
566 | static void |
547 | ev_start (struct ev_watcher *w, int active) |
567 | ev_start (W w, int active) |
548 | { |
568 | { |
549 | w->pending = 0; |
569 | w->pending = 0; |
550 | w->active = active; |
570 | w->active = active; |
551 | } |
571 | } |
552 | |
572 | |
553 | static void |
573 | static void |
554 | ev_stop (struct ev_watcher *w) |
574 | ev_stop (W w) |
555 | { |
575 | { |
556 | if (w->pending) |
576 | if (w->pending) |
557 | pendings [w->pending - 1].w = 0; |
577 | pendings [w->pending - 1].w = 0; |
558 | |
578 | |
559 | w->active = 0; |
579 | w->active = 0; |
560 | /* nop */ |
|
|
561 | } |
580 | } |
562 | |
581 | |
563 | /*****************************************************************************/ |
582 | /*****************************************************************************/ |
564 | |
583 | |
565 | void |
584 | void |
… | |
… | |
568 | if (ev_is_active (w)) |
587 | if (ev_is_active (w)) |
569 | return; |
588 | return; |
570 | |
589 | |
571 | int fd = w->fd; |
590 | int fd = w->fd; |
572 | |
591 | |
573 | ev_start ((struct ev_watcher *)w, 1); |
592 | ev_start ((W)w, 1); |
574 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
593 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
575 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
594 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
576 | |
595 | |
577 | ++fdchangecnt; |
596 | ++fdchangecnt; |
578 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
597 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
579 | fdchanges [fdchangecnt - 1] = fd; |
598 | fdchanges [fdchangecnt - 1] = fd; |
580 | } |
599 | } |
… | |
… | |
583 | evio_stop (struct ev_io *w) |
602 | evio_stop (struct ev_io *w) |
584 | { |
603 | { |
585 | if (!ev_is_active (w)) |
604 | if (!ev_is_active (w)) |
586 | return; |
605 | return; |
587 | |
606 | |
588 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
607 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
589 | ev_stop ((struct ev_watcher *)w); |
608 | ev_stop ((W)w); |
590 | |
609 | |
591 | ++fdchangecnt; |
610 | ++fdchangecnt; |
592 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
611 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
593 | fdchanges [fdchangecnt - 1] = w->fd; |
612 | fdchanges [fdchangecnt - 1] = w->fd; |
594 | } |
613 | } |
… | |
… | |
603 | { |
622 | { |
604 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
623 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
605 | if (w->repeat) |
624 | if (w->repeat) |
606 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
625 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
607 | |
626 | |
608 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
627 | ev_start ((W)w, ++atimercnt); |
609 | array_needsize (atimers, atimermax, atimercnt, ); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
610 | atimers [atimercnt - 1] = w; |
629 | atimers [atimercnt - 1] = w; |
611 | upheap (atimers, atimercnt - 1); |
630 | upheap (atimers, atimercnt - 1); |
612 | } |
631 | } |
613 | else |
632 | else |
614 | { |
633 | { |
615 | w->at += now; |
634 | w->at += now; |
616 | |
635 | |
617 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
636 | ev_start ((W)w, ++rtimercnt); |
618 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
637 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
619 | rtimers [rtimercnt - 1] = w; |
638 | rtimers [rtimercnt - 1] = w; |
620 | upheap (rtimers, rtimercnt - 1); |
639 | upheap (rtimers, rtimercnt - 1); |
621 | } |
640 | } |
622 | |
641 | |
… | |
… | |
643 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
644 | downheap (rtimers, rtimercnt, w->active - 1); |
663 | downheap (rtimers, rtimercnt, w->active - 1); |
645 | } |
664 | } |
646 | } |
665 | } |
647 | |
666 | |
648 | ev_stop ((struct ev_watcher *)w); |
667 | ev_stop ((W)w); |
649 | } |
668 | } |
650 | |
669 | |
651 | void |
670 | void |
652 | evsignal_start (struct ev_signal *w) |
671 | evsignal_start (struct ev_signal *w) |
653 | { |
672 | { |
654 | if (ev_is_active (w)) |
673 | if (ev_is_active (w)) |
655 | return; |
674 | return; |
656 | |
675 | |
657 | ev_start ((struct ev_watcher *)w, 1); |
676 | ev_start ((W)w, 1); |
658 | array_needsize (signals, signalmax, w->signum, signals_init); |
677 | array_needsize (signals, signalmax, w->signum, signals_init); |
659 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
678 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
660 | |
679 | |
661 | if (!w->next) |
680 | if (!w->next) |
662 | { |
681 | { |
663 | struct sigaction sa; |
682 | struct sigaction sa; |
664 | sa.sa_handler = sighandler; |
683 | sa.sa_handler = sighandler; |
… | |
… | |
672 | evsignal_stop (struct ev_signal *w) |
691 | evsignal_stop (struct ev_signal *w) |
673 | { |
692 | { |
674 | if (!ev_is_active (w)) |
693 | if (!ev_is_active (w)) |
675 | return; |
694 | return; |
676 | |
695 | |
677 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
696 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
678 | ev_stop ((struct ev_watcher *)w); |
697 | ev_stop ((W)w); |
679 | |
698 | |
680 | if (!signals [w->signum - 1].head) |
699 | if (!signals [w->signum - 1].head) |
681 | signal (w->signum, SIG_DFL); |
700 | signal (w->signum, SIG_DFL); |
682 | } |
701 | } |
683 | |
702 | |
|
|
703 | void evidle_start (struct ev_idle *w) |
|
|
704 | { |
|
|
705 | if (ev_is_active (w)) |
|
|
706 | return; |
|
|
707 | |
|
|
708 | ev_start ((W)w, ++idlecnt); |
|
|
709 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
710 | idles [idlecnt - 1] = w; |
|
|
711 | } |
|
|
712 | |
|
|
713 | void evidle_stop (struct ev_idle *w) |
|
|
714 | { |
|
|
715 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
716 | ev_stop ((W)w); |
|
|
717 | } |
|
|
718 | |
|
|
719 | void evcheck_start (struct ev_check *w) |
|
|
720 | { |
|
|
721 | if (ev_is_active (w)) |
|
|
722 | return; |
|
|
723 | |
|
|
724 | ev_start ((W)w, ++checkcnt); |
|
|
725 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
726 | checks [checkcnt - 1] = w; |
|
|
727 | } |
|
|
728 | |
|
|
729 | void evcheck_stop (struct ev_check *w) |
|
|
730 | { |
|
|
731 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
732 | ev_stop ((W)w); |
|
|
733 | } |
|
|
734 | |
684 | /*****************************************************************************/ |
735 | /*****************************************************************************/ |
|
|
736 | |
685 | #if 1 |
737 | #if 0 |
686 | |
738 | |
687 | static void |
739 | static void |
688 | sin_cb (struct ev_io *w, int revents) |
740 | sin_cb (struct ev_io *w, int revents) |
689 | { |
741 | { |
690 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
702 | scb (struct ev_signal *w, int revents) |
754 | scb (struct ev_signal *w, int revents) |
703 | { |
755 | { |
704 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
705 | } |
757 | } |
706 | |
758 | |
|
|
759 | static void |
|
|
760 | gcb (struct ev_signal *w, int revents) |
|
|
761 | { |
|
|
762 | fprintf (stderr, "generic %x\n", revents); |
|
|
763 | } |
|
|
764 | |
707 | int main (void) |
765 | int main (void) |
708 | { |
766 | { |
709 | struct ev_io sin; |
767 | struct ev_io sin; |
710 | |
768 | |
711 | ev_init (0); |
769 | ev_init (0); |
… | |
… | |
714 | evio_set (&sin, 0, EV_READ); |
772 | evio_set (&sin, 0, EV_READ); |
715 | evio_start (&sin); |
773 | evio_start (&sin); |
716 | |
774 | |
717 | struct ev_timer t[10000]; |
775 | struct ev_timer t[10000]; |
718 | |
776 | |
719 | #if 1 |
777 | #if 0 |
720 | int i; |
778 | int i; |
721 | for (i = 0; i < 10000; ++i) |
779 | for (i = 0; i < 10000; ++i) |
722 | { |
780 | { |
723 | struct ev_timer *w = t + i; |
781 | struct ev_timer *w = t + i; |
724 | evw_init (w, ocb, i); |
782 | evw_init (w, ocb, i); |
… | |
… | |
737 | struct ev_signal sig; |
795 | struct ev_signal sig; |
738 | evw_init (&sig, scb, 65535); |
796 | evw_init (&sig, scb, 65535); |
739 | evsignal_set (&sig, SIGQUIT); |
797 | evsignal_set (&sig, SIGQUIT); |
740 | evsignal_start (&sig); |
798 | evsignal_start (&sig); |
741 | |
799 | |
|
|
800 | struct ev_check cw; |
|
|
801 | evw_init (&cw, gcb, 0); |
|
|
802 | evcheck_start (&cw); |
|
|
803 | |
|
|
804 | struct ev_idle iw; |
|
|
805 | evw_init (&iw, gcb, 0); |
|
|
806 | evidle_start (&iw); |
|
|
807 | |
742 | ev_loop (0); |
808 | ev_loop (0); |
743 | |
809 | |
744 | return 0; |
810 | return 0; |
745 | } |
811 | } |
746 | |
812 | |