X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/d41579a0646e8afb2d9f3923b93efae744e77cdd..34a787b55768ddae6645862dc60434188e1fad8d:/ev.c diff --git a/ev.c b/ev.c index ce730b5..3e1e8b4 100644 --- a/ev.c +++ b/ev.c @@ -44,46 +44,78 @@ #include #include #include -#include +#ifndef WIN32 +# include +#endif #include #include +/**/ + #ifndef EV_USE_MONOTONIC # define EV_USE_MONOTONIC 1 #endif -#ifndef CLOCK_MONOTONIC -# undef EV_USE_MONOTONIC -# define EV_USE_MONOTONIC 0 -#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 CLOCK_REALTIME -# define EV_USE_REALTIME 0 +#ifndef EV_USE_KQUEUE +# define EV_USE_KQUEUE 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 /* never wait longer than this time (to detetc time jumps) */ +#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 (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ +/*#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; @@ -113,7 +145,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); @@ -127,7 +159,7 @@ get_clock (void) #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 \ @@ -172,22 +204,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->pending) { - pendings [w->pending - 1].events |= events; + pendings [ABSPRI (w)][w->pending - 1].events |= events; return; } - w->pending = ++pendingcnt; - array_needsize (pendings, pendingmax, pendingcnt, ); - pendings [pendingcnt - 1].w = w; - pendings [pendingcnt - 1].events = events; + 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 @@ -260,20 +292,44 @@ fd_change (int fd) 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) - { - ev_io_stop (anfds [fd].head); - event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); - } + 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; + } } /*****************************************************************************/ @@ -359,8 +415,10 @@ sighandler (int signum) if (!gotsig) { + int old_errno = errno; gotsig = 1; write (sigpipe [1], &signum, 1); + errno = old_errno; } } @@ -386,12 +444,14 @@ sigcb (struct ev_io *iow, int revents) static void siginit (void) { +#ifndef WIN32 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); /* rather than sort out wether we really need nb, set it */ fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); +#endif ev_io_set (&sigev, sigpipe [0], EV_READ); ev_io_start (&sigev); @@ -413,30 +473,57 @@ static int checkmax, checkcnt; static struct ev_child *childs [PID_HASHSIZE]; static struct ev_signal childev; +#ifndef WIN32 + #ifndef WCONTINUED # define WCONTINUED 0 #endif static void -childcb (struct ev_signal *sw, int revents) +child_reap (struct ev_signal *sw, int chain, int pid, int status) { struct ev_child *w; + + for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) + if (w->pid == pid || !w->pid) + { + w->priority = sw->priority; /* need to do it *now* */ + w->rpid = pid; + w->rstatus = status; + printf ("rpid %p %d %d\n", w, pid, w->pid);//D + event ((W)w, EV_CHILD); + } +} + +static void +childcb (struct ev_signal *sw, int revents) +{ int pid, status; - 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) - { - w->status = status; - event ((W)w, EV_CHILD); - } + printf ("chld %x\n", revents);//D + if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) + { + /* make sure we are called again until all childs have been reaped */ + event ((W)sw, EV_SIGNAL); + + child_reap (sw, pid, pid, status); + child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ + } } +#endif + /*****************************************************************************/ +#if EV_USE_KQUEUE +# include "ev_kqueue.c" +#endif #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 @@ -453,7 +540,19 @@ ev_version_minor (void) return EV_VERSION_MINOR; } -int ev_init (int flags) +/* return true if we are running with elevated privileges and should ignore env variables */ +static int +enable_secure () +{ +#ifdef WIN32 + return 0; +#else + return getuid () != geteuid () + || getgid () != getegid (); +#endif +} + +int ev_init (int methods) { if (!ev_method) { @@ -465,28 +564,45 @@ 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_KQUEUE + if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); +#endif #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) { ev_watcher_init (&sigev, sigcb); + ev_set_priority (&sigev, EV_MAXPRI); siginit (); +#ifndef WIN32 ev_signal_init (&childev, childcb, SIGCHLD); + ev_set_priority (&childev, EV_MAXPRI); ev_signal_start (&childev); +#endif } } @@ -527,16 +643,19 @@ ev_fork_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 @@ -606,34 +725,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; - if (fabs (odiff - diff) < MIN_TIMEJUMP) - return; /* all is well */ + for (i = 4; --i; ) /* loop a few times, before making important decisions */ + { + diff = ev_now - now; - ev_now = ev_time (); - } + if (fabs (odiff - diff) < MIN_TIMEJUMP) + return; /* all is well */ + + 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); @@ -656,7 +799,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 (); @@ -669,7 +812,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.; @@ -679,7 +830,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; } @@ -746,7 +897,7 @@ 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; } } @@ -754,6 +905,9 @@ ev_clear_pending (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; } @@ -880,6 +1034,10 @@ ev_periodic_stop (struct ev_periodic *w) ev_stop ((W)w); } +#ifndef SA_RESTART +# define SA_RESTART 0 +#endif + void ev_signal_start (struct ev_signal *w) { @@ -897,7 +1055,7 @@ ev_signal_start (struct ev_signal *w) struct sigaction sa; sa.sa_handler = sighandler; sigfillset (&sa.sa_mask); - sa.sa_flags = 0; + sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ sigaction (w->signum, &sa, 0); } }