X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/4179355c2b48ffa197ae4cf21b521ccf0201436c..08ed0fbd9e065605f8ffbbae086efc2105539ce4:/ev.c?ds=sidebyside diff --git a/ev.c b/ev.c index e87da22..7efc7e0 100644 --- a/ev.c +++ b/ev.c @@ -56,6 +56,14 @@ extern "C" { # endif # endif +# ifndef EV_USE_NANOSLEEP +# if HAVE_NANOSLEEP +# define EV_USE_NANOSLEEP 1 +# else +# define EV_USE_NANOSLEEP 0 +# endif +# endif + # ifndef EV_USE_SELECT # if HAVE_SELECT && HAVE_SYS_SELECT_H # define EV_USE_SELECT 1 @@ -148,6 +156,10 @@ extern "C" { # define EV_USE_REALTIME 0 #endif +#ifndef EV_USE_NANOSLEEP +# define EV_USE_NANOSLEEP 0 +#endif + #ifndef EV_USE_SELECT # define EV_USE_SELECT 1 #endif @@ -204,18 +216,25 @@ extern "C" { # define EV_USE_REALTIME 0 #endif -#if EV_SELECT_IS_WINSOCKET -# include -#endif - #if !EV_STAT_ENABLE +# undef EV_USE_INOTIFY # define EV_USE_INOTIFY 0 #endif +#if !EV_USE_NANOSLEEP +# ifndef _WIN32 +# include +# endif +#endif + #if EV_USE_INOTIFY # include #endif +#if EV_SELECT_IS_WINSOCKET +# include +#endif + /**/ /* @@ -232,7 +251,7 @@ extern "C" { #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ -#if __GNUC__ >= 3 +#if __GNUC__ >= 4 # define expect(expr,value) __builtin_expect ((expr),(value)) # define noinline __attribute__ ((noinline)) #else @@ -263,7 +282,9 @@ typedef ev_watcher *W; typedef ev_watcher_list *WL; typedef ev_watcher_time *WT; -static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ +/* sig_atomic_t is used to avoid per-thread variables or locking but still */ +/* giving it a reasonably high chance of working on typical architetcures */ +static sig_atomic_t have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ #ifdef _WIN32 # include "ev_win32.c" @@ -409,6 +430,33 @@ ev_now (EV_P) } #endif +void +ev_sleep (ev_tstamp delay) +{ + if (delay > 0.) + { +#if EV_USE_NANOSLEEP + struct timespec ts; + + ts.tv_sec = (time_t)delay; + ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); + + nanosleep (&ts, 0); +#elif defined(_WIN32) + Sleep (delay * 1e3); +#else + struct timeval tv; + + tv.tv_sec = (time_t)delay; + tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); + + select (0, 0, 0, 0, &tv); +#endif + } +} + +/*****************************************************************************/ + int inline_size array_nextsize (int elem, int cur, int cnt) { @@ -535,10 +583,10 @@ fd_reify (EV_P) ANFD *anfd = anfds + fd; ev_io *w; - int events = 0; + unsigned char events = 0; for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) - events |= w->events; + events |= (unsigned char)w->events; #if EV_SELECT_IS_WINSOCKET if (events) @@ -549,26 +597,33 @@ fd_reify (EV_P) } #endif - anfd->reify = 0; + { + unsigned char o_events = anfd->events; + unsigned char o_reify = anfd->reify; - backend_modify (EV_A_ fd, anfd->events, events); - anfd->events = events; + anfd->reify = 0; + anfd->events = events; + + if (o_events != events || o_reify & EV_IOFDSET) + backend_modify (EV_A_ fd, o_events, events); + } } fdchangecnt = 0; } void inline_size -fd_change (EV_P_ int fd) +fd_change (EV_P_ int fd, int flags) { - if (expect_false (anfds [fd].reify)) - return; - - anfds [fd].reify = 1; + unsigned char reify = anfds [fd].reify; + anfds [fd].reify |= flags; - ++fdchangecnt; - array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); - fdchanges [fdchangecnt - 1] = fd; + if (expect_true (!reify)) + { + ++fdchangecnt; + array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); + fdchanges [fdchangecnt - 1] = fd; + } } void inline_speed @@ -629,7 +684,7 @@ fd_rearm_all (EV_P) if (anfds [fd].events) { anfds [fd].events = 0; - fd_change (EV_A_ fd); + fd_change (EV_A_ fd, EV_IOFDSET | 1); } } @@ -794,7 +849,7 @@ siginit (EV_P) /*****************************************************************************/ -static ev_child *childs [EV_PID_HASHSIZE]; +static WL childs [EV_PID_HASHSIZE]; #ifndef _WIN32 @@ -919,8 +974,8 @@ ev_recommended_backends (void) unsigned int ev_embeddable_backends (void) { - return EVBACKEND_EPOLL - | EVBACKEND_KQUEUE + /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ + return EVBACKEND_KQUEUE | EVBACKEND_PORT; } @@ -936,6 +991,18 @@ ev_loop_count (EV_P) return loop_count; } +void +ev_set_io_collect_interval (EV_P_ ev_tstamp interval) +{ + io_blocktime = interval; +} + +void +ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) +{ + timeout_blocktime = interval; +} + static void noinline loop_init (EV_P_ unsigned int flags) { @@ -954,6 +1021,9 @@ loop_init (EV_P_ unsigned int flags) now_floor = mn_now; rtmn_diff = ev_rt_now - mn_now; + io_blocktime = 0.; + timeout_blocktime = 0.; + /* pid check not overridable via env */ #ifndef _WIN32 if (flags & EVFLAG_FORKCHECK) @@ -1032,11 +1102,16 @@ loop_destroy (EV_P) #endif } + ev_free (anfds); anfdmax = 0; + /* have to use the microsoft-never-gets-it-right macro */ array_free (fdchange, EMPTY); array_free (timer, EMPTY); #if EV_PERIODIC_ENABLE array_free (periodic, EMPTY); +#endif +#if EV_FORK_ENABLE + array_free (fork, EMPTY); #endif array_free (prepare, EMPTY); array_free (check, EMPTY); @@ -1445,39 +1520,50 @@ ev_loop (EV_P_ int flags) /* calculate blocking time */ { - ev_tstamp block; + ev_tstamp waittime = 0.; + ev_tstamp sleeptime = 0.; - if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) - block = 0.; /* do not block at all */ - else + if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) { /* update time to cancel out callback processing overhead */ time_update (EV_A_ 1e100); - block = MAX_BLOCKTIME; + waittime = MAX_BLOCKTIME; if (timercnt) { ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; - if (block > to) block = to; + if (waittime > to) waittime = to; } #if EV_PERIODIC_ENABLE if (periodiccnt) { ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; - if (block > to) block = to; + if (waittime > to) waittime = to; } #endif - if (expect_false (block < 0.)) block = 0.; + if (expect_false (waittime < timeout_blocktime)) + waittime = timeout_blocktime; + + sleeptime = waittime - backend_fudge; + + if (expect_true (sleeptime > io_blocktime)) + sleeptime = io_blocktime; + + if (sleeptime) + { + ev_sleep (sleeptime); + waittime -= sleeptime; + } } ++loop_count; - backend_poll (EV_A_ block); + backend_poll (EV_A_ waittime); /* update ev_rt_now, do magic */ - time_update (EV_A_ block); + time_update (EV_A_ waittime + sleeptime); } /* queue pending timers and reschedule them */ @@ -1599,9 +1685,10 @@ ev_io_start (EV_P_ ev_io *w) ev_start (EV_A_ (W)w, 1); array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); - wlist_add ((WL *)&anfds[fd].head, (WL)w); + wlist_add (&anfds[fd].head, (WL)w); - fd_change (EV_A_ fd); + fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1); + w->events &= ~EV_IOFDSET; } void noinline @@ -1613,10 +1700,10 @@ ev_io_stop (EV_P_ ev_io *w) assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); - wlist_del ((WL *)&anfds[w->fd].head, (WL)w); + wlist_del (&anfds[w->fd].head, (WL)w); ev_stop (EV_A_ (W)w); - fd_change (EV_A_ w->fd); + fd_change (EV_A_ w->fd, 1); } void noinline @@ -1768,7 +1855,7 @@ ev_signal_start (EV_P_ ev_signal *w) } ev_start (EV_A_ (W)w, 1); - wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); + wlist_add (&signals [w->signum - 1].head, (WL)w); if (!((WL)w)->next) { @@ -1791,7 +1878,7 @@ ev_signal_stop (EV_P_ ev_signal *w) if (expect_false (!ev_is_active (w))) return; - wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); + wlist_del (&signals [w->signum - 1].head, (WL)w); ev_stop (EV_A_ (W)w); if (!signals [w->signum - 1].head) @@ -1808,7 +1895,7 @@ ev_child_start (EV_P_ ev_child *w) return; ev_start (EV_A_ (W)w, 1); - wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); + wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); } void @@ -1818,7 +1905,7 @@ ev_child_stop (EV_P_ ev_child *w) if (expect_false (!ev_is_active (w))) return; - wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); + wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); ev_stop (EV_A_ (W)w); } @@ -2170,20 +2257,44 @@ ev_check_stop (EV_P_ ev_check *w) void noinline ev_embed_sweep (EV_P_ ev_embed *w) { - ev_loop (w->loop, EVLOOP_NONBLOCK); + ev_loop (w->other, EVLOOP_NONBLOCK); } static void -embed_cb (EV_P_ ev_io *io, int revents) +embed_io_cb (EV_P_ ev_io *io, int revents) { ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); if (ev_cb (w)) ev_feed_event (EV_A_ (W)w, EV_EMBED); else - ev_embed_sweep (loop, w); + ev_loop (w->other, EVLOOP_NONBLOCK); } +static void +embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) +{ + ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); + + { + struct ev_loop *loop = w->other; + + while (fdchangecnt) + { + fd_reify (EV_A); + ev_loop (EV_A_ EVLOOP_NONBLOCK); + } + } +} + +#if 0 +static void +embed_idle_cb (EV_P_ ev_idle *idle, int revents) +{ + ev_idle_stop (EV_A_ idle); +} +#endif + void ev_embed_start (EV_P_ ev_embed *w) { @@ -2191,14 +2302,20 @@ ev_embed_start (EV_P_ ev_embed *w) return; { - struct ev_loop *loop = w->loop; + struct ev_loop *loop = w->other; assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); - ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); + ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); } ev_set_priority (&w->io, ev_priority (w)); ev_io_start (EV_A_ &w->io); + ev_prepare_init (&w->prepare, embed_prepare_cb); + ev_set_priority (&w->prepare, EV_MINPRI); + ev_prepare_start (EV_A_ &w->prepare); + + /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ + ev_start (EV_A_ (W)w, 1); } @@ -2210,6 +2327,7 @@ ev_embed_stop (EV_P_ ev_embed *w) return; ev_io_stop (EV_A_ &w->io); + ev_prepare_stop (EV_A_ &w->prepare); ev_stop (EV_A_ (W)w); } @@ -2308,6 +2426,10 @@ ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, vo } } +#if EV_MULTIPLICITY + #include "ev_wrap.h" +#endif + #ifdef __cplusplus } #endif