X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/ae1704c5ed25ba151cb448264ada4b96c9b88225..168dbe26f6016988116006b49fbb4362be416ab7:/ev.c diff --git a/ev.c b/ev.c index f7aab9c..d024017 100644 --- a/ev.c +++ b/ev.c @@ -4,29 +4,37 @@ * Copyright (c) 2007 Marc Alexander Lehmann * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. */ #ifdef __cplusplus @@ -56,6 +64,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 +164,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 +224,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 + /**/ /* @@ -223,8 +250,7 @@ extern "C" { * It is added to ev_rt_now when scheduling periodics * to ensure progress, time-wise, even when rounding * errors are against us. - * This value is good at least till the year 4000 - * and intervals up to 20 years. + * This value is good at least till the year 4000. * Better solutions welcome. */ #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ @@ -233,7 +259,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 @@ -264,7 +290,11 @@ typedef ev_watcher *W; typedef ev_watcher_list *WL; typedef ev_watcher_time *WT; -static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ +#if EV_USE_MONOTONIC +/* 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? */ +#endif #ifdef _WIN32 # include "ev_win32.c" @@ -410,6 +440,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) { @@ -479,7 +536,7 @@ ev_feed_event (EV_P_ void *w, int revents) } } -void inline_size +void inline_speed queue_events (EV_P_ W *events, int eventcnt, int type) { int i; @@ -536,40 +593,51 @@ 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) { unsigned long argp; - anfd->handle = _get_osfhandle (fd); + #ifdef EV_FD_TO_WIN32_HANDLE + anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); + #else + anfd->handle = _get_osfhandle (fd); + #endif assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); } #endif - anfd->reify = 0; + { + unsigned char o_events = anfd->events; + unsigned char o_reify = anfd->reify; + + anfd->reify = 0; + anfd->events = events; - backend_modify (EV_A_ fd, anfd->events, events); - 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; + unsigned char reify = anfds [fd].reify; + anfds [fd].reify |= flags; - anfds [fd].reify = 1; - - ++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 @@ -630,7 +698,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); } } @@ -641,16 +709,20 @@ upheap (WT *heap, int k) { WT w = heap [k]; - while (k && heap [k >> 1]->at > w->at) + while (k) { - heap [k] = heap [k >> 1]; + int p = (k - 1) >> 1; + + if (heap [p]->at <= w->at) + break; + + heap [k] = heap [p]; ((W)heap [k])->active = k + 1; - k >>= 1; + k = p; } heap [k] = w; ((W)heap [k])->active = k + 1; - } void inline_speed @@ -658,19 +730,23 @@ downheap (WT *heap, int N, int k) { WT w = heap [k]; - while (k < (N >> 1)) + for (;;) { - int j = k << 1; + int c = (k << 1) + 1; - if (j + 1 < N && heap [j]->at > heap [j + 1]->at) - ++j; + if (c >= N) + break; + + c += c + 1 < N && heap [c]->at > heap [c + 1]->at + ? 1 : 0; - if (w->at <= heap [j]->at) + if (w->at <= heap [c]->at) break; - heap [k] = heap [j]; + heap [k] = heap [c]; ((W)heap [k])->active = k + 1; - k = j; + + k = c; } heap [k] = w; @@ -787,7 +863,7 @@ siginit (EV_P) /*****************************************************************************/ -static ev_child *childs [EV_PID_HASHSIZE]; +static WL childs [EV_PID_HASHSIZE]; #ifndef _WIN32 @@ -912,9 +988,13 @@ ev_recommended_backends (void) unsigned int ev_embeddable_backends (void) { - return EVBACKEND_EPOLL - | EVBACKEND_KQUEUE - | EVBACKEND_PORT; + int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; + + /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ + /* please fix it and tell me how to detect the fix */ + flags &= ~EVBACKEND_EPOLL; + + return flags; } unsigned int @@ -929,6 +1009,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) { @@ -947,6 +1039,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) @@ -1025,11 +1120,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); @@ -1068,6 +1168,7 @@ loop_fork (EV_P) syserr ("(libev) error creating pipe"); siginit (EV_A); + sigcb (EV_A_ &sigev, EV_READ); } postfork = 0; @@ -1099,7 +1200,7 @@ ev_loop_destroy (EV_P) void ev_loop_fork (EV_P) { - postfork = 1; + postfork = 1; // must be in line with ev_default_fork } #endif @@ -1173,7 +1274,7 @@ ev_default_fork (void) #endif if (backend) - postfork = 1; + postfork = 1; // must be in line with ev_loop_fork } /*****************************************************************************/ @@ -1209,7 +1310,7 @@ timers_reify (EV_P) { while (timercnt && ((WT)timers [0])->at <= mn_now) { - ev_timer *w = timers [0]; + ev_timer *w = (ev_timer *)timers [0]; /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ @@ -1222,7 +1323,7 @@ timers_reify (EV_P) if (((WT)w)->at < mn_now) ((WT)w)->at = mn_now; - downheap ((WT *)timers, timercnt, 0); + downheap (timers, timercnt, 0); } else ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ @@ -1237,7 +1338,7 @@ periodics_reify (EV_P) { while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) { - ev_periodic *w = periodics [0]; + ev_periodic *w = (ev_periodic *)periodics [0]; /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ @@ -1246,13 +1347,14 @@ periodics_reify (EV_P) { ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); - downheap ((WT *)periodics, periodiccnt, 0); + downheap (periodics, periodiccnt, 0); } else if (w->interval) { - ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval; + ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; + if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); - downheap ((WT *)periodics, periodiccnt, 0); + downheap (periodics, periodiccnt, 0); } else ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ @@ -1269,7 +1371,7 @@ periodics_reschedule (EV_P) /* adjust periodics after time jump */ for (i = 0; i < periodiccnt; ++i) { - ev_periodic *w = periodics [i]; + ev_periodic *w = (ev_periodic *)periodics [i]; if (w->reschedule_cb) ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); @@ -1279,7 +1381,7 @@ periodics_reschedule (EV_P) /* now rebuild the heap */ for (i = periodiccnt >> 1; i--; ) - downheap ((WT *)periodics, periodiccnt, i); + downheap (periodics, periodiccnt, i); } #endif @@ -1306,74 +1408,65 @@ idle_reify (EV_P) } #endif -int inline_size -time_update_monotonic (EV_P) -{ - mn_now = get_clock (); - - if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) - { - ev_rt_now = rtmn_diff + mn_now; - return 0; - } - else - { - now_floor = mn_now; - ev_rt_now = ev_time (); - return 1; - } -} - -void inline_size -time_update (EV_P) +void inline_speed +time_update (EV_P_ ev_tstamp max_block) { int i; #if EV_USE_MONOTONIC if (expect_true (have_monotonic)) { - if (time_update_monotonic (EV_A)) + ev_tstamp odiff = rtmn_diff; + + mn_now = get_clock (); + + /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ + /* interpolate in the meantime */ + if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) { - ev_tstamp odiff = rtmn_diff; - - /* loop a few times, before making important decisions. - * on the choice of "4": one iteration isn't enough, - * in case we get preempted during the calls to - * ev_time and get_clock. a second call is almost guaranteed - * to succeed in that case, though. and looping a few more times - * doesn't hurt either as we only do this on time-jumps or - * in the unlikely event of having been preempted here. - */ - for (i = 4; --i; ) - { - rtmn_diff = ev_rt_now - mn_now; + ev_rt_now = rtmn_diff + mn_now; + return; + } - if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) - return; /* all is well */ + now_floor = mn_now; + ev_rt_now = ev_time (); - ev_rt_now = ev_time (); - mn_now = get_clock (); - now_floor = mn_now; - } + /* loop a few times, before making important decisions. + * on the choice of "4": one iteration isn't enough, + * in case we get preempted during the calls to + * ev_time and get_clock. a second call is almost guaranteed + * to succeed in that case, though. and looping a few more times + * doesn't hurt either as we only do this on time-jumps or + * in the unlikely event of having been preempted here. + */ + for (i = 4; --i; ) + { + rtmn_diff = ev_rt_now - mn_now; + + if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) + return; /* all is well */ + + ev_rt_now = ev_time (); + mn_now = get_clock (); + now_floor = mn_now; + } # if EV_PERIODIC_ENABLE - periodics_reschedule (EV_A); + periodics_reschedule (EV_A); # endif - /* no timer adjustment, as the monotonic clock doesn't jump */ - /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ - } + /* no timer adjustment, as the monotonic clock doesn't jump */ + /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ } else #endif { ev_rt_now = ev_time (); - if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) + if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) { #if EV_PERIODIC_ENABLE periodics_reschedule (EV_A); #endif - /* adjust timers. this is easy, as the offset is the same for all of them */ for (i = 0; i < timercnt; ++i) ((WT)timers [i])->at += ev_rt_now - mn_now; @@ -1446,48 +1539,51 @@ 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 */ -#if EV_USE_MONOTONIC - if (expect_true (have_monotonic)) - time_update_monotonic (EV_A); - else -#endif - { - ev_rt_now = ev_time (); - mn_now = ev_rt_now; - } + 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); + /* update ev_rt_now, do magic */ + time_update (EV_A_ waittime + sleeptime); + } /* queue pending timers and reschedule them */ timers_reify (EV_A); /* relative timers called last */ @@ -1608,9 +1704,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 @@ -1622,10 +1719,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 @@ -1639,9 +1736,9 @@ ev_timer_start (EV_P_ 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 (ev_timer *, timers, timermax, timercnt, EMPTY2); - timers [timercnt - 1] = w; - upheap ((WT *)timers, timercnt - 1); + array_needsize (WT, timers, timermax, timercnt, EMPTY2); + timers [timercnt - 1] = (WT)w; + upheap (timers, timercnt - 1); /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ } @@ -1653,7 +1750,7 @@ ev_timer_stop (EV_P_ ev_timer *w) if (expect_false (!ev_is_active (w))) return; - assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); + assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); { int active = ((W)w)->active; @@ -1661,7 +1758,7 @@ ev_timer_stop (EV_P_ ev_timer *w) if (expect_true (--active < --timercnt)) { timers [active] = timers [timercnt]; - adjustheap ((WT *)timers, timercnt, active); + adjustheap (timers, timercnt, active); } } @@ -1678,7 +1775,7 @@ ev_timer_again (EV_P_ ev_timer *w) if (w->repeat) { ((WT)w)->at = mn_now + w->repeat; - adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); + adjustheap (timers, timercnt, ((W)w)->active - 1); } else ev_timer_stop (EV_A_ w); @@ -1709,9 +1806,9 @@ ev_periodic_start (EV_P_ ev_periodic *w) ((WT)w)->at = w->offset; ev_start (EV_A_ (W)w, ++periodiccnt); - array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); - periodics [periodiccnt - 1] = w; - upheap ((WT *)periodics, periodiccnt - 1); + array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); + periodics [periodiccnt - 1] = (WT)w; + upheap (periodics, periodiccnt - 1); /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ } @@ -1723,7 +1820,7 @@ ev_periodic_stop (EV_P_ ev_periodic *w) if (expect_false (!ev_is_active (w))) return; - assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); + assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); { int active = ((W)w)->active; @@ -1731,7 +1828,7 @@ ev_periodic_stop (EV_P_ ev_periodic *w) if (expect_true (--active < --periodiccnt)) { periodics [active] = periodics [periodiccnt]; - adjustheap ((WT *)periodics, periodiccnt, active); + adjustheap (periodics, periodiccnt, active); } } @@ -1762,9 +1859,22 @@ ev_signal_start (EV_P_ ev_signal *w) assert (("ev_signal_start called with illegal signal number", w->signum > 0)); + { +#ifndef _WIN32 + sigset_t full, prev; + sigfillset (&full); + sigprocmask (SIG_SETMASK, &full, &prev); +#endif + + array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); + +#ifndef _WIN32 + sigprocmask (SIG_SETMASK, &prev, 0); +#endif + } + ev_start (EV_A_ (W)w, 1); - array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); - wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); + wlist_add (&signals [w->signum - 1].head, (WL)w); if (!((WL)w)->next) { @@ -1787,7 +1897,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) @@ -1804,7 +1914,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 @@ -1814,7 +1924,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); } @@ -2166,20 +2276,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) { @@ -2187,14 +2321,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); } @@ -2206,6 +2346,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); } @@ -2304,6 +2445,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