X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/fa5f32e568e5e1f7bcb548b108379c434d253731..338036c31a5f2290ed99ef7e27f1bc450f8487bd:/ev.c?ds=inline diff --git a/ev.c b/ev.c index 8c3bf01..a9864a7 100644 --- a/ev.c +++ b/ev.c @@ -1,4 +1,6 @@ /* + * libev event processing core, watcher management + * * Copyright (c) 2007 Marc Alexander Lehmann * All rights reserved. * @@ -46,35 +48,68 @@ #include #include +/**/ + #ifndef EV_USE_MONOTONIC -# ifdef CLOCK_MONOTONIC -# define EV_USE_MONOTONIC 1 -# endif +# define EV_USE_MONOTONIC 1 #endif #ifndef EV_USE_SELECT # define EV_USE_SELECT 1 #endif +#ifndef EV_USE_POLL +# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ +#endif + #ifndef EV_USE_EPOLL # define EV_USE_EPOLL 0 #endif #ifndef EV_USE_REALTIME -# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ +# define EV_USE_REALTIME 1 +#endif + +/**/ + +#ifndef CLOCK_MONOTONIC +# undef EV_USE_MONOTONIC +# define EV_USE_MONOTONIC 0 +#endif + +#ifndef CLOCK_REALTIME +# undef EV_USE_REALTIME +# define EV_USE_REALTIME 0 #endif +/**/ + #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ -#define MAX_BLOCKTIME 59.731 -#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ +#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ +#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ +/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ #include "ev.h" +#if __GNUC__ >= 3 +# define expect(expr,value) __builtin_expect ((expr),(value)) +# define inline inline +#else +# define expect(expr,value) (expr) +# define inline static +#endif + +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + +#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) +#define ABSPRI(w) ((w)->priority - EV_MINPRI) + typedef struct ev_watcher *W; typedef struct ev_watcher_list *WL; typedef struct ev_watcher_time *WT; -static ev_tstamp now, diff; /* monotonic clock */ +static ev_tstamp now_floor, now, diff; /* monotonic clock */ ev_tstamp ev_now; int ev_method; @@ -104,7 +139,7 @@ static ev_tstamp get_clock (void) { #if EV_USE_MONOTONIC - if (have_monotonic) + if (expect_true (have_monotonic)) { struct timespec ts; clock_gettime (CLOCK_MONOTONIC, &ts); @@ -115,16 +150,15 @@ get_clock (void) return ev_time (); } -#define array_nextsize(n) (((n) << 1) | 4 & ~3) -#define array_prevsize(n) (((n) >> 1) | 4 & ~3) +#define array_roundsize(base,n) ((n) | 4 & ~3) #define array_needsize(base,cur,cnt,init) \ - if ((cnt) > cur) \ + if (expect_false ((cnt) > cur)) \ { \ int newcnt = cur; \ do \ { \ - newcnt = array_nextsize (newcnt); \ + newcnt = array_roundsize (base, newcnt << 1); \ } \ while ((cnt) > newcnt); \ \ @@ -138,7 +172,8 @@ get_clock (void) typedef struct { struct ev_io *head; - int events; + unsigned char events; + unsigned char reify; } ANFD; static ANFD *anfds; @@ -151,6 +186,8 @@ anfds_init (ANFD *base, int count) { base->head = 0; base->events = EV_NONE; + base->reify = 0; + ++base; } } @@ -161,19 +198,22 @@ typedef struct int events; } ANPENDING; -static ANPENDING *pendings; -static int pendingmax, pendingcnt; +static ANPENDING *pendings [NUMPRI]; +static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; static void event (W w, int events) { - if (w->active) + if (w->pending) { - w->pending = ++pendingcnt; - array_needsize (pendings, pendingmax, pendingcnt, ); - pendings [pendingcnt - 1].w = w; - pendings [pendingcnt - 1].events = events; + pendings [ABSPRI (w)][w->pending - 1].events |= events; + return; } + + w->pending = ++pendingcnt [ABSPRI (w)]; + array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); + pendings [ABSPRI (w)][w->pending - 1].w = w; + pendings [ABSPRI (w)][w->pending - 1].events = events; } static void @@ -221,7 +261,7 @@ fd_reify (void) for (w = anfd->head; w; w = w->next) events |= w->events; - anfd->events &= ~EV_REIFY; + anfd->reify = 0; if (anfd->events != events) { @@ -236,30 +276,54 @@ fd_reify (void) static void fd_change (int fd) { - if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) + if (anfds [fd].reify || fdchangecnt < 0) return; - anfds [fd].events |= EV_REIFY; + anfds [fd].reify = 1; ++fdchangecnt; array_needsize (fdchanges, fdchangemax, fdchangecnt, ); fdchanges [fdchangecnt - 1] = fd; } +static void +fd_kill (int fd) +{ + struct ev_io *w; + + printf ("killing fd %d\n", fd);//D + while ((w = anfds [fd].head)) + { + ev_io_stop (w); + event ((W)w, EV_ERROR | EV_READ | EV_WRITE); + } +} + /* called on EBADF to verify fds */ static void -fd_recheck (void) +fd_ebadf (void) { int fd; for (fd = 0; fd < anfdmax; ++fd) if (anfds [fd].events) if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) - while (anfds [fd].head) - { - event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); - ev_io_stop (anfds [fd].head); - } + fd_kill (fd); +} + +/* called on ENOMEM in select/poll to kill some fds and retry */ +static void +fd_enomem (void) +{ + int fd = anfdmax; + + while (fd--) + if (anfds [fd].events) + { + close (fd); + fd_kill (fd); + return; + } } /*****************************************************************************/ @@ -316,14 +380,14 @@ downheap (WT *timers, int N, int k) typedef struct { struct ev_signal *head; - sig_atomic_t gotsig; + sig_atomic_t volatile gotsig; } ANSIG; static ANSIG *signals; static int signalmax; static int sigpipe [2]; -static sig_atomic_t gotsig; +static sig_atomic_t volatile gotsig; static struct ev_io sigev; static void @@ -333,6 +397,7 @@ signals_init (ANSIG *base, int count) { base->head = 0; base->gotsig = 0; + ++base; } } @@ -345,7 +410,7 @@ sighandler (int signum) if (!gotsig) { gotsig = 1; - write (sigpipe [1], &gotsig, 1); + write (sigpipe [1], &signum, 1); } } @@ -353,17 +418,17 @@ static void sigcb (struct ev_io *iow, int revents) { struct ev_signal *w; - int sig; + int signum; - gotsig = 0; read (sigpipe [0], &revents, 1); + gotsig = 0; - for (sig = signalmax; sig--; ) - if (signals [sig].gotsig) + for (signum = signalmax; signum--; ) + if (signals [signum].gotsig) { - signals [sig].gotsig = 0; + signals [signum].gotsig = 0; - for (w = signals [sig].head; w; w = w->next) + for (w = signals [signum].head; w; w = w->next) event ((W)w, EV_SIGNAL); } } @@ -410,7 +475,7 @@ childcb (struct ev_signal *sw, int revents) while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) - if (w->pid == pid || w->pid == -1) + if (w->pid == pid || !w->pid) { w->status = status; event ((W)w, EV_CHILD); @@ -422,6 +487,9 @@ childcb (struct ev_signal *sw, int revents) #if EV_USE_EPOLL # include "ev_epoll.c" #endif +#if EV_USE_POLL +# include "ev_poll.c" +#endif #if EV_USE_SELECT # include "ev_select.c" #endif @@ -438,7 +506,15 @@ ev_version_minor (void) return EV_VERSION_MINOR; } -int ev_init (int flags) +/* return true if we are running with elevated privileges and ignore env variables */ +static int +enable_secure () +{ + return getuid () != geteuid () + || getgid () != getegid (); +} + +int ev_init (int methods) { if (!ev_method) { @@ -450,19 +526,29 @@ int ev_init (int flags) } #endif - ev_now = ev_time (); - now = get_clock (); - diff = ev_now - now; + ev_now = ev_time (); + now = get_clock (); + now_floor = now; + diff = ev_now - now; if (pipe (sigpipe)) return 0; - ev_method = EVMETHOD_NONE; + if (methods == EVMETHOD_AUTO) + if (!enable_secure () && getenv ("LIBEV_METHODS")) + methods = atoi (getenv ("LIBEV_METHODS")); + else + methods = EVMETHOD_ANY; + + ev_method = 0; #if EV_USE_EPOLL - if (ev_method == EVMETHOD_NONE) epoll_init (flags); + if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); +#endif +#if EV_USE_POLL + if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); #endif #if EV_USE_SELECT - if (ev_method == EVMETHOD_NONE) select_init (flags); + if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); #endif if (ev_method) @@ -481,19 +567,19 @@ int ev_init (int flags) /*****************************************************************************/ void -ev_prefork (void) +ev_fork_prepare (void) { /* nop */ } void -ev_postfork_parent (void) +ev_fork_parent (void) { /* nop */ } void -ev_postfork_child (void) +ev_fork_child (void) { #if EV_USE_EPOLL if (ev_method == EVMETHOD_EPOLL) @@ -512,16 +598,19 @@ ev_postfork_child (void) static void call_pending (void) { - while (pendingcnt) - { - ANPENDING *p = pendings + --pendingcnt; + int pri; - if (p->w) - { - p->w->pending = 0; - p->w->cb (p->w, p->events); - } - } + for (pri = NUMPRI; pri--; ) + while (pendingcnt [pri]) + { + ANPENDING *p = pendings [pri] + --pendingcnt [pri]; + + if (p->w) + { + p->w->pending = 0; + p->w->cb (p->w, p->events); + } + } } static void @@ -531,17 +620,17 @@ timers_reify (void) { struct ev_timer *w = timers [0]; - event ((W)w, EV_TIMEOUT); - /* first reschedule or stop timer */ if (w->repeat) { + assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); w->at = now + w->repeat; - assert (("timer timeout in the past, negative repeat?", w->at > now)); downheap ((WT *)timers, timercnt, 0); } else ev_timer_stop (w); /* nonrepeating: stop timer */ + + event ((W)w, EV_TIMEOUT); } } @@ -556,13 +645,13 @@ periodics_reify (void) if (w->interval) { w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; - assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); + assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); downheap ((WT *)periodics, periodiccnt, 0); } else ev_periodic_stop (w); /* nonrepeating: stop timer */ - event ((W)w, EV_TIMEOUT); + event ((W)w, EV_PERIODIC); } } @@ -591,34 +680,58 @@ periodics_reschedule (ev_tstamp diff) } } +static int +time_update_monotonic (void) +{ + now = get_clock (); + + if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) + { + ev_now = now + diff; + return 0; + } + else + { + now_floor = now; + ev_now = ev_time (); + return 1; + } +} + static void time_update (void) { int i; - ev_now = ev_time (); - - if (have_monotonic) +#if EV_USE_MONOTONIC + if (expect_true (have_monotonic)) { - ev_tstamp odiff = diff; - - for (i = 4; --i; ) /* loop a few times, before making important decisions */ + if (time_update_monotonic ()) { - now = get_clock (); - diff = ev_now - now; + ev_tstamp odiff = diff; + + for (i = 4; --i; ) /* loop a few times, before making important decisions */ + { + diff = ev_now - now; - if (fabs (odiff - diff) < MIN_TIMEJUMP) - return; /* all is well */ + if (fabs (odiff - diff) < MIN_TIMEJUMP) + return; /* all is well */ - ev_now = ev_time (); - } + ev_now = ev_time (); + now = get_clock (); + now_floor = now; + } - periodics_reschedule (diff - odiff); - /* no timer adjustment, as the monotonic clock doesn't jump */ + periodics_reschedule (diff - odiff); + /* no timer adjustment, as the monotonic clock doesn't jump */ + } } else +#endif { - if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) + ev_now = ev_time (); + + if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) { periodics_reschedule (ev_now - now); @@ -641,7 +754,7 @@ void ev_loop (int flags) do { /* queue check watchers (and execute them) */ - if (preparecnt) + if (expect_false (preparecnt)) { queue_events ((W *)prepares, preparecnt, EV_PREPARE); call_pending (); @@ -654,7 +767,15 @@ void ev_loop (int flags) /* we only need this for !monotonic clockor timers, but as we basically always have timers, we just calculate it always */ - ev_now = ev_time (); +#if EV_USE_MONOTONIC + if (expect_true (have_monotonic)) + time_update_monotonic (); + else +#endif + { + ev_now = ev_time (); + now = ev_now; + } if (flags & EVLOOP_NONBLOCK || idlecnt) block = 0.; @@ -664,7 +785,7 @@ void ev_loop (int flags) if (timercnt) { - ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; + ev_tstamp to = timers [0]->at - now + method_fudge; if (block > to) block = to; } @@ -727,11 +848,11 @@ wlist_del (WL *head, WL elem) } static void -ev_clear (W w) +ev_clear_pending (W w) { if (w->pending) { - pendings [w->pending - 1].w = 0; + pendings [ABSPRI (w)][w->pending - 1].w = 0; w->pending = 0; } } @@ -739,6 +860,9 @@ ev_clear (W w) static void ev_start (W w, int active) { + if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; + if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; + w->active = active; } @@ -753,10 +877,12 @@ ev_stop (W w) void ev_io_start (struct ev_io *w) { + int fd = w->fd; + if (ev_is_active (w)) return; - int fd = w->fd; + assert (("ev_io_start called with negative fd", fd >= 0)); ev_start ((W)w, 1); array_needsize (anfds, anfdmax, fd + 1, anfds_init); @@ -768,7 +894,7 @@ ev_io_start (struct ev_io *w) void ev_io_stop (struct ev_io *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (!ev_is_active (w)) return; @@ -786,7 +912,7 @@ ev_timer_start (struct ev_timer *w) w->at += now; - assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); + assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); ev_start ((W)w, ++timercnt); array_needsize (timers, timermax, timercnt, ); @@ -797,7 +923,7 @@ ev_timer_start (struct ev_timer *w) void ev_timer_stop (struct ev_timer *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (!ev_is_active (w)) return; @@ -835,7 +961,7 @@ ev_periodic_start (struct ev_periodic *w) if (ev_is_active (w)) return; - assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); + assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); /* this formula differs from the one in periodic_reify because we do not always round up */ if (w->interval) @@ -850,7 +976,7 @@ ev_periodic_start (struct ev_periodic *w) void ev_periodic_stop (struct ev_periodic *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (!ev_is_active (w)) return; @@ -869,6 +995,8 @@ ev_signal_start (struct ev_signal *w) if (ev_is_active (w)) return; + assert (("ev_signal_start called with illegal signal number", w->signum > 0)); + ev_start ((W)w, 1); array_needsize (signals, signalmax, w->signum, signals_init); wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); @@ -886,7 +1014,7 @@ ev_signal_start (struct ev_signal *w) void ev_signal_stop (struct ev_signal *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (!ev_is_active (w)) return; @@ -911,7 +1039,7 @@ ev_idle_start (struct ev_idle *w) void ev_idle_stop (struct ev_idle *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (ev_is_active (w)) return; @@ -933,7 +1061,7 @@ ev_prepare_start (struct ev_prepare *w) void ev_prepare_stop (struct ev_prepare *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (ev_is_active (w)) return; @@ -955,7 +1083,7 @@ ev_check_start (struct ev_check *w) void ev_check_stop (struct ev_check *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (ev_is_active (w)) return; @@ -976,7 +1104,7 @@ ev_child_start (struct ev_child *w) void ev_child_stop (struct ev_child *w) { - ev_clear ((W)w); + ev_clear_pending ((W)w); if (ev_is_active (w)) return;