… | |
… | |
56 | |
56 | |
57 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
58 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
59 | #endif |
59 | #endif |
60 | |
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
61 | #ifndef EV_USE_EPOLL |
65 | #ifndef EV_USE_EPOLL |
62 | # define EV_USE_EPOLL 0 |
66 | # define EV_USE_EPOLL 0 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | #ifndef EV_USE_KQUEUE |
|
|
70 | # define EV_USE_KQUEUE 0 |
63 | #endif |
71 | #endif |
64 | |
72 | |
65 | #ifndef EV_USE_REALTIME |
73 | #ifndef EV_USE_REALTIME |
66 | # define EV_USE_REALTIME 1 |
74 | # define EV_USE_REALTIME 1 |
67 | #endif |
75 | #endif |
… | |
… | |
95 | # define inline static |
103 | # define inline static |
96 | #endif |
104 | #endif |
97 | |
105 | |
98 | #define expect_false(expr) expect ((expr) != 0, 0) |
106 | #define expect_false(expr) expect ((expr) != 0, 0) |
99 | #define expect_true(expr) expect ((expr) != 0, 1) |
107 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
108 | |
|
|
109 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
110 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
100 | |
111 | |
101 | typedef struct ev_watcher *W; |
112 | typedef struct ev_watcher *W; |
102 | typedef struct ev_watcher_list *WL; |
113 | typedef struct ev_watcher_list *WL; |
103 | typedef struct ev_watcher_time *WT; |
114 | typedef struct ev_watcher_time *WT; |
104 | |
115 | |
… | |
… | |
189 | { |
200 | { |
190 | W w; |
201 | W w; |
191 | int events; |
202 | int events; |
192 | } ANPENDING; |
203 | } ANPENDING; |
193 | |
204 | |
194 | static ANPENDING *pendings; |
205 | static ANPENDING *pendings [NUMPRI]; |
195 | static int pendingmax, pendingcnt; |
206 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
196 | |
207 | |
197 | static void |
208 | static void |
198 | event (W w, int events) |
209 | event (W w, int events) |
199 | { |
210 | { |
200 | if (w->pending) |
211 | if (w->pending) |
201 | { |
212 | { |
202 | pendings [w->pending - 1].events |= events; |
213 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
203 | return; |
214 | return; |
204 | } |
215 | } |
205 | |
216 | |
206 | w->pending = ++pendingcnt; |
217 | w->pending = ++pendingcnt [ABSPRI (w)]; |
207 | array_needsize (pendings, pendingmax, pendingcnt, ); |
218 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
208 | pendings [pendingcnt - 1].w = w; |
219 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
209 | pendings [pendingcnt - 1].events = events; |
220 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
210 | } |
221 | } |
211 | |
222 | |
212 | static void |
223 | static void |
213 | queue_events (W *events, int eventcnt, int type) |
224 | queue_events (W *events, int eventcnt, int type) |
214 | { |
225 | { |
… | |
… | |
277 | ++fdchangecnt; |
288 | ++fdchangecnt; |
278 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
289 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
279 | fdchanges [fdchangecnt - 1] = fd; |
290 | fdchanges [fdchangecnt - 1] = fd; |
280 | } |
291 | } |
281 | |
292 | |
|
|
293 | static void |
|
|
294 | fd_kill (int fd) |
|
|
295 | { |
|
|
296 | struct ev_io *w; |
|
|
297 | |
|
|
298 | printf ("killing fd %d\n", fd);//D |
|
|
299 | while ((w = anfds [fd].head)) |
|
|
300 | { |
|
|
301 | ev_io_stop (w); |
|
|
302 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
282 | /* called on EBADF to verify fds */ |
306 | /* called on EBADF to verify fds */ |
283 | static void |
307 | static void |
284 | fd_recheck (void) |
308 | fd_ebadf (void) |
285 | { |
309 | { |
286 | int fd; |
310 | int fd; |
287 | |
311 | |
288 | for (fd = 0; fd < anfdmax; ++fd) |
312 | for (fd = 0; fd < anfdmax; ++fd) |
289 | if (anfds [fd].events) |
313 | if (anfds [fd].events) |
290 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
314 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
291 | while (anfds [fd].head) |
315 | fd_kill (fd); |
|
|
316 | } |
|
|
317 | |
|
|
318 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
319 | static void |
|
|
320 | fd_enomem (void) |
|
|
321 | { |
|
|
322 | int fd = anfdmax; |
|
|
323 | |
|
|
324 | while (fd--) |
|
|
325 | if (anfds [fd].events) |
292 | { |
326 | { |
293 | ev_io_stop (anfds [fd].head); |
327 | close (fd); |
294 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
328 | fd_kill (fd); |
|
|
329 | return; |
295 | } |
330 | } |
296 | } |
331 | } |
297 | |
332 | |
298 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
299 | |
334 | |
300 | static struct ev_timer **timers; |
335 | static struct ev_timer **timers; |
… | |
… | |
451 | } |
486 | } |
452 | } |
487 | } |
453 | |
488 | |
454 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
455 | |
490 | |
|
|
491 | #if EV_USE_KQUEUE |
|
|
492 | # include "ev_kqueue.c" |
|
|
493 | #endif |
456 | #if EV_USE_EPOLL |
494 | #if EV_USE_EPOLL |
457 | # include "ev_epoll.c" |
495 | # include "ev_epoll.c" |
458 | #endif |
496 | #endif |
|
|
497 | #if EV_USE_POLL |
|
|
498 | # include "ev_poll.c" |
|
|
499 | #endif |
459 | #if EV_USE_SELECT |
500 | #if EV_USE_SELECT |
460 | # include "ev_select.c" |
501 | # include "ev_select.c" |
461 | #endif |
502 | #endif |
462 | |
503 | |
463 | int |
504 | int |
… | |
… | |
470 | ev_version_minor (void) |
511 | ev_version_minor (void) |
471 | { |
512 | { |
472 | return EV_VERSION_MINOR; |
513 | return EV_VERSION_MINOR; |
473 | } |
514 | } |
474 | |
515 | |
|
|
516 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
517 | static int |
|
|
518 | enable_secure () |
|
|
519 | { |
|
|
520 | return getuid () != geteuid () |
|
|
521 | || getgid () != getegid (); |
|
|
522 | } |
|
|
523 | |
475 | int ev_init (int flags) |
524 | int ev_init (int methods) |
476 | { |
525 | { |
477 | if (!ev_method) |
526 | if (!ev_method) |
478 | { |
527 | { |
479 | #if EV_USE_MONOTONIC |
528 | #if EV_USE_MONOTONIC |
480 | { |
529 | { |
… | |
… | |
490 | diff = ev_now - now; |
539 | diff = ev_now - now; |
491 | |
540 | |
492 | if (pipe (sigpipe)) |
541 | if (pipe (sigpipe)) |
493 | return 0; |
542 | return 0; |
494 | |
543 | |
|
|
544 | if (methods == EVMETHOD_AUTO) |
|
|
545 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
546 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
547 | else |
495 | ev_method = EVMETHOD_NONE; |
548 | methods = EVMETHOD_ANY; |
|
|
549 | |
|
|
550 | ev_method = 0; |
|
|
551 | #if EV_USE_KQUEUE |
|
|
552 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
553 | #endif |
496 | #if EV_USE_EPOLL |
554 | #if EV_USE_EPOLL |
497 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
555 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
556 | #endif |
|
|
557 | #if EV_USE_POLL |
|
|
558 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
498 | #endif |
559 | #endif |
499 | #if EV_USE_SELECT |
560 | #if EV_USE_SELECT |
500 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
561 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
501 | #endif |
562 | #endif |
502 | |
563 | |
503 | if (ev_method) |
564 | if (ev_method) |
504 | { |
565 | { |
505 | ev_watcher_init (&sigev, sigcb); |
566 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
545 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
546 | |
607 | |
547 | static void |
608 | static void |
548 | call_pending (void) |
609 | call_pending (void) |
549 | { |
610 | { |
|
|
611 | int pri; |
|
|
612 | |
|
|
613 | for (pri = NUMPRI; pri--; ) |
550 | while (pendingcnt) |
614 | while (pendingcnt [pri]) |
551 | { |
615 | { |
552 | ANPENDING *p = pendings + --pendingcnt; |
616 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
553 | |
617 | |
554 | if (p->w) |
618 | if (p->w) |
555 | { |
619 | { |
556 | p->w->pending = 0; |
620 | p->w->pending = 0; |
557 | p->w->cb (p->w, p->events); |
621 | p->w->cb (p->w, p->events); |
558 | } |
622 | } |
559 | } |
623 | } |
560 | } |
624 | } |
561 | |
625 | |
562 | static void |
626 | static void |
563 | timers_reify (void) |
627 | timers_reify (void) |
564 | { |
628 | { |
… | |
… | |
796 | static void |
860 | static void |
797 | ev_clear_pending (W w) |
861 | ev_clear_pending (W w) |
798 | { |
862 | { |
799 | if (w->pending) |
863 | if (w->pending) |
800 | { |
864 | { |
801 | pendings [w->pending - 1].w = 0; |
865 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
802 | w->pending = 0; |
866 | w->pending = 0; |
803 | } |
867 | } |
804 | } |
868 | } |
805 | |
869 | |
806 | static void |
870 | static void |
807 | ev_start (W w, int active) |
871 | ev_start (W w, int active) |
808 | { |
872 | { |
|
|
873 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
874 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
875 | |
809 | w->active = active; |
876 | w->active = active; |
810 | } |
877 | } |
811 | |
878 | |
812 | static void |
879 | static void |
813 | ev_stop (W w) |
880 | ev_stop (W w) |