X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/4f1dc6c0e6bd6e6e6edab0e55fc6b96824a12f38..3bf1eb4a5e1e1dabdc1b44665d2eb49cecd04593:/ev.c?ds=sidebyside diff --git a/ev.c b/ev.c index 299ba00..d04f786 100644 --- a/ev.c +++ b/ev.c @@ -53,7 +53,7 @@ extern "C" { # define EV_USE_POLL 1 # endif -# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) +# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) # define EV_USE_EPOLL 1 # endif @@ -77,11 +77,18 @@ extern "C" { #include -#ifndef WIN32 +#ifndef _WIN32 # include # include # include +#else +# define WIN32_LEAN_AND_MEAN +# include +# ifndef EV_SELECT_IS_WINSOCKET +# define EV_SELECT_IS_WINSOCKET 1 +# endif #endif + /**/ #ifndef EV_USE_MONOTONIC @@ -90,10 +97,15 @@ extern "C" { #ifndef EV_USE_SELECT # define EV_USE_SELECT 1 +# define EV_SELECT_USE_FD_SET 1 #endif #ifndef EV_USE_POLL -# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ +# ifdef _WIN32 +# define EV_USE_POLL 0 +# else +# define EV_USE_POLL 1 +# endif #endif #ifndef EV_USE_EPOLL @@ -104,22 +116,18 @@ extern "C" { # define EV_USE_KQUEUE 0 #endif -#ifndef EV_USE_WIN32 -# ifdef WIN32 -# define EV_USE_WIN32 0 /* it does not exist, use select */ -# undef EV_USE_SELECT -# define EV_USE_SELECT 1 -# else -# define EV_USE_WIN32 0 -# endif -#endif - #ifndef EV_USE_REALTIME # define EV_USE_REALTIME 1 #endif /**/ +/* darwin simply cannot be helped */ +#ifdef __APPLE__ +# undef EV_USE_POLL +# undef EV_USE_KQUEUE +#endif + #ifndef CLOCK_MONOTONIC # undef EV_USE_MONOTONIC # define EV_USE_MONOTONIC 0 @@ -130,6 +138,10 @@ extern "C" { # define EV_USE_REALTIME 0 #endif +#if EV_SELECT_IS_WINSOCKET +# include +#endif + /**/ #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ @@ -157,13 +169,16 @@ extern "C" { #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) #define ABSPRI(w) ((w)->priority - EV_MINPRI) +#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ +#define EMPTY2(a,b) /* used to suppress some warnings */ + typedef struct ev_watcher *W; typedef struct ev_watcher_list *WL; typedef struct ev_watcher_time *WT; static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ -#ifdef WIN32 +#ifdef _WIN32 # include "ev_win32.c" #endif @@ -222,6 +237,9 @@ typedef struct WL head; unsigned char events; unsigned char reify; +#if EV_SELECT_IS_WINSOCKET + SOCKET handle; +#endif } ANFD; typedef struct @@ -295,7 +313,7 @@ ev_now (EV_P) } #endif -#define array_roundsize(type,n) ((n) | 4 & ~3) +#define array_roundsize(type,n) (((n) | 4) & ~3) #define array_needsize(type,base,cur,cnt,init) \ if (expect_false ((cnt) > cur)) \ @@ -320,11 +338,6 @@ ev_now (EV_P) fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ } -/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ -/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ -#define array_free_microshit(stem) \ - ev_free (stem ## s); stem ## cnt = stem ## max = 0; - #define array_free(stem, idx) \ ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; @@ -355,7 +368,7 @@ ev_feed_event (EV_P_ void *w, int revents) } w_->pending = ++pendingcnt [ABSPRI (w_)]; - array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); + array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); pendings [ABSPRI (w_)][w_->pending - 1].w = w_; pendings [ABSPRI (w_)][w_->pending - 1].events = revents; } @@ -408,6 +421,15 @@ fd_reify (EV_P) for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) events |= w->events; +#if EV_SELECT_IS_WINSOCKET + if (events) + { + unsigned long argp; + anfd->handle = _get_osfhandle (fd); + assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); + } +#endif + anfd->reify = 0; method_modify (EV_A_ fd, anfd->events, events); @@ -426,7 +448,7 @@ fd_change (EV_P_ int fd) anfds [fd].reify = 1; ++fdchangecnt; - array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); + array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); fdchanges [fdchangecnt - 1] = fd; } @@ -445,8 +467,8 @@ fd_kill (EV_P_ int fd) static int fd_valid (int fd) { -#ifdef WIN32 - return !!win32_get_osfhandle (fd); +#ifdef _WIN32 + return _get_osfhandle (fd) != -1; #else return fcntl (fd, F_GETFD) != -1; #endif @@ -573,7 +595,7 @@ signals_init (ANSIG *base, int count) static void sighandler (int signum) { -#if WIN32 +#if _WIN32 signal (signum, sighandler); #endif @@ -583,11 +605,7 @@ sighandler (int signum) { int old_errno = errno; gotsig = 1; -#ifdef WIN32 - send (sigpipe [1], &signum, 1, MSG_DONTWAIT); -#else write (sigpipe [1], &signum, 1); -#endif errno = old_errno; } } @@ -617,11 +635,7 @@ sigcb (EV_P_ struct ev_io *iow, int revents) { int signum; -#ifdef WIN32 - recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); -#else read (sigpipe [0], &revents, 1); -#endif gotsig = 0; for (signum = signalmax; signum--; ) @@ -629,17 +643,23 @@ sigcb (EV_P_ struct ev_io *iow, int revents) ev_feed_signal_event (EV_A_ signum + 1); } +inline void +fd_intern (int fd) +{ +#ifdef _WIN32 + int arg = 1; + ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); +#else + fcntl (fd, F_SETFD, FD_CLOEXEC); + fcntl (fd, F_SETFL, O_NONBLOCK); +#endif +} + static void siginit (EV_P) { -#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 + fd_intern (sigpipe [0]); + fd_intern (sigpipe [1]); ev_io_set (&sigev, sigpipe [0], EV_READ); ev_io_start (EV_A_ &sigev); @@ -650,7 +670,7 @@ siginit (EV_P) static struct ev_child *childs [PID_HASHSIZE]; -#ifndef WIN32 +#ifndef _WIN32 static struct ev_signal childev; @@ -721,7 +741,7 @@ ev_version_minor (void) static int enable_secure (void) { -#ifdef WIN32 +#ifdef _WIN32 return 0; #else return getuid () != geteuid () @@ -729,14 +749,14 @@ enable_secure (void) #endif } -int +unsigned int ev_method (EV_P) { return method; } static void -loop_init (EV_P_ int methods) +loop_init (EV_P_ unsigned int flags) { if (!method) { @@ -753,27 +773,24 @@ loop_init (EV_P_ int methods) now_floor = mn_now; rtmn_diff = ev_rt_now - mn_now; - if (methods == EVMETHOD_AUTO) - if (!enable_secure () && getenv ("LIBEV_METHODS")) - methods = atoi (getenv ("LIBEV_METHODS")); - else - methods = EVMETHOD_ANY; + if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) + flags = atoi (getenv ("LIBEV_FLAGS")); + + if (!(flags & 0x0000ffff)) + flags |= 0x0000ffff; method = 0; -#if EV_USE_WIN32 - if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); -#endif #if EV_USE_KQUEUE - if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); + if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); #endif #if EV_USE_EPOLL - if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); + if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); #endif #if EV_USE_POLL - if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); + if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); #endif #if EV_USE_SELECT - if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); + if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); #endif ev_init (&sigev, sigcb); @@ -786,9 +803,6 @@ loop_destroy (EV_P) { int i; -#if EV_USE_WIN32 - if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); -#endif #if EV_USE_KQUEUE if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); #endif @@ -806,14 +820,14 @@ loop_destroy (EV_P) array_free (pending, [i]); /* have to use the microsoft-never-gets-it-right macro */ - array_free_microshit (fdchange); - array_free_microshit (timer); + array_free (fdchange, EMPTY0); + array_free (timer, EMPTY0); #if EV_PERIODICS - array_free_microshit (periodic); + array_free (periodic, EMPTY0); #endif - array_free_microshit (idle); - array_free_microshit (prepare); - array_free_microshit (check); + array_free (idle, EMPTY0); + array_free (prepare, EMPTY0); + array_free (check, EMPTY0); method = 0; } @@ -848,13 +862,13 @@ loop_fork (EV_P) #if EV_MULTIPLICITY struct ev_loop * -ev_loop_new (int methods) +ev_loop_new (unsigned int flags) { struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); memset (loop, 0, sizeof (struct ev_loop)); - loop_init (EV_A_ methods); + loop_init (EV_A_ flags); if (ev_method (EV_A)) return loop; @@ -882,7 +896,7 @@ struct ev_loop * #else int #endif -ev_default_loop (int methods) +ev_default_loop (unsigned int flags) { if (sigpipe [0] == sigpipe [1]) if (pipe (sigpipe)) @@ -896,13 +910,13 @@ ev_default_loop (int methods) default_loop = 1; #endif - loop_init (EV_A_ methods); + loop_init (EV_A_ flags); if (ev_method (EV_A)) { siginit (EV_A); -#ifndef WIN32 +#ifndef _WIN32 ev_signal_init (&childev, childcb, SIGCHLD); ev_set_priority (&childev, EV_MAXPRI); ev_signal_start (EV_A_ &childev); @@ -923,7 +937,7 @@ ev_default_destroy (void) struct ev_loop *loop = default_loop; #endif -#ifndef WIN32 +#ifndef _WIN32 ev_ref (EV_A); /* child watcher */ ev_signal_stop (EV_A_ &childev); #endif @@ -1020,8 +1034,7 @@ periodics_reify (EV_P) /* first reschedule or stop timer */ if (w->reschedule_cb) { - ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); - + ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); downheap ((WT *)periodics, periodiccnt, 0); } @@ -1149,7 +1162,7 @@ ev_loop (EV_P_ int flags) double block; loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; - do + while (activecnt) { /* queue check watchers (and execute them) */ if (expect_false (preparecnt)) @@ -1222,8 +1235,10 @@ ev_loop (EV_P_ int flags) queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); call_pending (EV_A); + + if (loop_done) + break; } - while (activecnt && !loop_done); if (loop_done != 2) loop_done = 0; @@ -1331,7 +1346,7 @@ ev_timer_start (EV_P_ struct ev_timer *w) assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); ev_start (EV_A_ (W)w, ++timercnt); - array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); + array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); timers [timercnt - 1] = w; upheap ((WT *)timers, timercnt - 1); @@ -1372,7 +1387,10 @@ ev_timer_again (EV_P_ struct ev_timer *w) ev_timer_stop (EV_A_ w); } else if (w->repeat) - ev_timer_start (EV_A_ w); + { + w->at = w->repeat; + ev_timer_start (EV_A_ w); + } } #if EV_PERIODICS @@ -1392,7 +1410,7 @@ ev_periodic_start (EV_P_ struct ev_periodic *w) } ev_start (EV_A_ (W)w, ++periodiccnt); - array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); + array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); periodics [periodiccnt - 1] = w; upheap ((WT *)periodics, periodiccnt - 1); @@ -1433,7 +1451,7 @@ ev_idle_start (EV_P_ struct ev_idle *w) return; ev_start (EV_A_ (W)w, ++idlecnt); - array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); + array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); idles [idlecnt - 1] = w; } @@ -1441,7 +1459,7 @@ void ev_idle_stop (EV_P_ struct ev_idle *w) { ev_clear_pending (EV_A_ (W)w); - if (ev_is_active (w)) + if (!ev_is_active (w)) return; idles [((W)w)->active - 1] = idles [--idlecnt]; @@ -1455,7 +1473,7 @@ ev_prepare_start (EV_P_ struct ev_prepare *w) return; ev_start (EV_A_ (W)w, ++preparecnt); - array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); + array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); prepares [preparecnt - 1] = w; } @@ -1463,7 +1481,7 @@ void ev_prepare_stop (EV_P_ struct ev_prepare *w) { ev_clear_pending (EV_A_ (W)w); - if (ev_is_active (w)) + if (!ev_is_active (w)) return; prepares [((W)w)->active - 1] = prepares [--preparecnt]; @@ -1477,7 +1495,7 @@ ev_check_start (EV_P_ struct ev_check *w) return; ev_start (EV_A_ (W)w, ++checkcnt); - array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); + array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); checks [checkcnt - 1] = w; } @@ -1513,7 +1531,7 @@ ev_signal_start (EV_P_ struct ev_signal *w) if (!((WL)w)->next) { -#if WIN32 +#if _WIN32 signal (w->signum, sighandler); #else struct sigaction sa;