… | |
… | |
39 | #include <assert.h> |
39 | #include <assert.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <sys/time.h> |
41 | #include <sys/time.h> |
42 | #include <time.h> |
42 | #include <time.h> |
43 | |
43 | |
44 | #define HAVE_EPOLL 1 |
|
|
45 | |
|
|
46 | #ifndef HAVE_MONOTONIC |
44 | #ifndef HAVE_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
46 | # define HAVE_MONOTONIC 1 |
49 | # endif |
47 | # endif |
50 | #endif |
48 | #endif |
… | |
… | |
188 | |
186 | |
189 | for (i = 0; i < eventcnt; ++i) |
187 | for (i = 0; i < eventcnt; ++i) |
190 | event (events [i], type); |
188 | event (events [i], type); |
191 | } |
189 | } |
192 | |
190 | |
|
|
191 | /* called on EBADF to verify fds */ |
|
|
192 | static void |
|
|
193 | fd_recheck () |
|
|
194 | { |
|
|
195 | int fd; |
|
|
196 | |
|
|
197 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
198 | if (anfds [fd].wev) |
|
|
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
200 | while (anfds [fd].head) |
|
|
201 | evio_stop (anfds [fd].head); |
|
|
202 | } |
|
|
203 | |
193 | /*****************************************************************************/ |
204 | /*****************************************************************************/ |
194 | |
205 | |
195 | static struct ev_timer **timers; |
206 | static struct ev_timer **timers; |
196 | static int timermax, timercnt; |
207 | static int timermax, timercnt; |
197 | |
208 | |
… | |
… | |
312 | |
323 | |
313 | /*****************************************************************************/ |
324 | /*****************************************************************************/ |
314 | |
325 | |
315 | static struct ev_idle **idles; |
326 | static struct ev_idle **idles; |
316 | static int idlemax, idlecnt; |
327 | static int idlemax, idlecnt; |
|
|
328 | |
|
|
329 | static struct ev_prepare **prepares; |
|
|
330 | static int preparemax, preparecnt; |
317 | |
331 | |
318 | static struct ev_check **checks; |
332 | static struct ev_check **checks; |
319 | static int checkmax, checkcnt; |
333 | static int checkmax, checkcnt; |
320 | |
334 | |
321 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
… | |
… | |
542 | void ev_loop (int flags) |
556 | void ev_loop (int flags) |
543 | { |
557 | { |
544 | double block; |
558 | double block; |
545 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
559 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
546 | |
560 | |
547 | if (checkcnt) |
|
|
548 | { |
|
|
549 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
550 | call_pending (); |
|
|
551 | } |
|
|
552 | |
|
|
553 | do |
561 | do |
554 | { |
562 | { |
|
|
563 | /* queue check watchers (and execute them) */ |
|
|
564 | if (checkcnt) |
|
|
565 | { |
|
|
566 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
567 | call_pending (); |
|
|
568 | } |
|
|
569 | |
555 | /* update fd-related kernel structures */ |
570 | /* update fd-related kernel structures */ |
556 | fd_reify (); |
571 | fd_reify (); |
557 | |
572 | |
558 | /* calculate blocking time */ |
573 | /* calculate blocking time */ |
559 | |
574 | |
… | |
… | |
585 | |
600 | |
586 | /* update ev_now, do magic */ |
601 | /* update ev_now, do magic */ |
587 | time_update (); |
602 | time_update (); |
588 | |
603 | |
589 | /* queue pending timers and reschedule them */ |
604 | /* queue pending timers and reschedule them */ |
|
|
605 | timers_reify (); /* relative timers called last */ |
590 | periodics_reify (); /* absolute timers first */ |
606 | periodics_reify (); /* absolute timers called first */ |
591 | timers_reify (); /* relative timers second */ |
|
|
592 | |
607 | |
593 | /* queue idle watchers unless io or timers are pending */ |
608 | /* queue idle watchers unless io or timers are pending */ |
594 | if (!pendingcnt) |
609 | if (!pendingcnt) |
595 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
610 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
596 | |
611 | |
597 | /* queue check and possibly idle watchers */ |
612 | /* queue check watchers, to be executed first */ |
|
|
613 | if (checkcnt) |
598 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
614 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
599 | |
615 | |
600 | call_pending (); |
616 | call_pending (); |
601 | } |
617 | } |
602 | while (!ev_loop_done); |
618 | while (!ev_loop_done); |
603 | |
619 | |
… | |
… | |
819 | ev_clear ((W)w); |
835 | ev_clear ((W)w); |
820 | if (ev_is_active (w)) |
836 | if (ev_is_active (w)) |
821 | return; |
837 | return; |
822 | |
838 | |
823 | idles [w->active - 1] = idles [--idlecnt]; |
839 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
840 | ev_stop ((W)w); |
|
|
841 | } |
|
|
842 | |
|
|
843 | void evprepare_start (struct ev_prepare *w) |
|
|
844 | { |
|
|
845 | if (ev_is_active (w)) |
|
|
846 | return; |
|
|
847 | |
|
|
848 | ev_start ((W)w, ++preparecnt); |
|
|
849 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
850 | prepares [preparecnt - 1] = w; |
|
|
851 | } |
|
|
852 | |
|
|
853 | void evprepare_stop (struct ev_prepare *w) |
|
|
854 | { |
|
|
855 | ev_clear ((W)w); |
|
|
856 | if (ev_is_active (w)) |
|
|
857 | return; |
|
|
858 | |
|
|
859 | prepares [w->active - 1] = prepares [--preparecnt]; |
824 | ev_stop ((W)w); |
860 | ev_stop ((W)w); |
825 | } |
861 | } |
826 | |
862 | |
827 | void evcheck_start (struct ev_check *w) |
863 | void evcheck_start (struct ev_check *w) |
828 | { |
864 | { |