2 * libev event processing core, watcher management
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 # if HAVE_CLOCK_GETTIME
40 # ifndef EV_USE_MONOTONIC
41 # define EV_USE_MONOTONIC 1
43 # ifndef EV_USE_REALTIME
44 # define EV_USE_REALTIME 1
48 # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
49 # define EV_USE_SELECT 1
52 # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
53 # define EV_USE_POLL 1
56 # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
57 # define EV_USE_EPOLL 1
60 # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
61 # define EV_USE_KQUEUE 1
75 #include <sys/types.h>
82 # include <sys/time.h>
83 # include <sys/wait.h>
87 #ifndef EV_USE_MONOTONIC
88 # define EV_USE_MONOTONIC 1
92 # define EV_USE_SELECT 1
96 # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
100 # define EV_USE_EPOLL 0
103 #ifndef EV_USE_KQUEUE
104 # define EV_USE_KQUEUE 0
109 # define EV_USE_WIN32 0 /* it does not exist, use select */
110 # undef EV_USE_SELECT
111 # define EV_USE_SELECT 1
113 # define EV_USE_WIN32 0
117 #ifndef EV_USE_REALTIME
118 # define EV_USE_REALTIME 1
123 #ifndef CLOCK_MONOTONIC
124 # undef EV_USE_MONOTONIC
125 # define EV_USE_MONOTONIC 0
128 #ifndef CLOCK_REALTIME
129 # undef EV_USE_REALTIME
130 # define EV_USE_REALTIME 0
135 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
136 #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
137 #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
138 /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
147 # define expect(expr,value) __builtin_expect ((expr),(value))
148 # define inline inline
150 # define expect(expr,value) (expr)
151 # define inline static
154 #define expect_false(expr) expect ((expr) != 0, 0)
155 #define expect_true(expr) expect ((expr) != 0, 1)
157 #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
158 #define ABSPRI(w) ((w)->priority - EV_MINPRI)
160 typedef struct ev_watcher *W;
161 typedef struct ev_watcher_list *WL;
162 typedef struct ev_watcher_time *WT;
164 static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
167 # include "ev_win32.c"
170 /*****************************************************************************/
172 static void (*syserr_cb)(const char *msg);
174 void ev_set_syserr_cb (void (*cb)(const char *msg))
180 syserr (const char *msg)
183 msg = "(libev) system error";
194 static void *(*alloc)(void *ptr, long size);
196 void ev_set_allocator (void *(*cb)(void *ptr, long size))
202 ev_realloc (void *ptr, long size)
204 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
208 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
215 #define ev_malloc(size) ev_realloc (0, (size))
216 #define ev_free(ptr) ev_realloc ((ptr), 0)
218 /*****************************************************************************/
223 unsigned char events;
238 #define ev_rt_now ((loop)->ev_rt_now)
239 #define VAR(name,decl) decl;
245 struct ev_loop default_loop_struct;
246 static struct ev_loop *default_loop;
251 #define VAR(name,decl) static decl;
255 static int default_loop;
259 /*****************************************************************************/
266 clock_gettime (CLOCK_REALTIME, &ts);
267 return ts.tv_sec + ts.tv_nsec * 1e-9;
270 gettimeofday (&tv, 0);
271 return tv.tv_sec + tv.tv_usec * 1e-6;
279 if (expect_true (have_monotonic))
282 clock_gettime (CLOCK_MONOTONIC, &ts);
283 return ts.tv_sec + ts.tv_nsec * 1e-9;
298 #define array_roundsize(type,n) ((n) | 4 & ~3)
300 #define array_needsize(type,base,cur,cnt,init) \
301 if (expect_false ((cnt) > cur)) \
306 newcnt = array_roundsize (type, newcnt << 1); \
308 while ((cnt) > newcnt); \
310 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
311 init (base + cur, newcnt - cur); \
315 #define array_slim(type,stem) \
316 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
318 stem ## max = array_roundsize (stem ## cnt >> 1); \
319 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
320 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
323 /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
324 /* bringing us everlasting joy in form of stupid extra macros that are not required in C */
325 #define array_free_microshit(stem) \
326 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
328 #define array_free(stem, idx) \
329 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
331 /*****************************************************************************/
334 anfds_init (ANFD *base, int count)
339 base->events = EV_NONE;
347 ev_feed_event (EV_P_ void *w, int revents)
353 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
357 w_->pending = ++pendingcnt [ABSPRI (w_)];
358 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
359 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
360 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
364 queue_events (EV_P_ W *events, int eventcnt, int type)
368 for (i = 0; i < eventcnt; ++i)
369 ev_feed_event (EV_A_ events [i], type);
373 fd_event (EV_P_ int fd, int revents)
375 ANFD *anfd = anfds + fd;
378 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
380 int ev = w->events & revents;
383 ev_feed_event (EV_A_ (W)w, ev);
388 ev_feed_fd_event (EV_P_ int fd, int revents)
390 fd_event (EV_A_ fd, revents);
393 /*****************************************************************************/
400 for (i = 0; i < fdchangecnt; ++i)
402 int fd = fdchanges [i];
403 ANFD *anfd = anfds + fd;
408 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
413 method_modify (EV_A_ fd, anfd->events, events);
414 anfd->events = events;
421 fd_change (EV_P_ int fd)
423 if (anfds [fd].reify)
426 anfds [fd].reify = 1;
429 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
430 fdchanges [fdchangecnt - 1] = fd;
434 fd_kill (EV_P_ int fd)
438 while ((w = (struct ev_io *)anfds [fd].head))
440 ev_io_stop (EV_A_ w);
441 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
449 return !!win32_get_osfhandle (fd);
451 return fcntl (fd, F_GETFD) != -1;
455 /* called on EBADF to verify fds */
461 for (fd = 0; fd < anfdmax; ++fd)
462 if (anfds [fd].events)
463 if (!fd_valid (fd) == -1 && errno == EBADF)
467 /* called on ENOMEM in select/poll to kill some fds and retry */
473 for (fd = anfdmax; fd--; )
474 if (anfds [fd].events)
481 /* usually called after fork if method needs to re-arm all fds from scratch */
487 /* this should be highly optimised to not do anything but set a flag */
488 for (fd = 0; fd < anfdmax; ++fd)
489 if (anfds [fd].events)
491 anfds [fd].events = 0;
492 fd_change (EV_A_ fd);
496 /*****************************************************************************/
499 upheap (WT *heap, int k)
503 while (k && heap [k >> 1]->at > w->at)
505 heap [k] = heap [k >> 1];
506 ((W)heap [k])->active = k + 1;
511 ((W)heap [k])->active = k + 1;
516 downheap (WT *heap, int N, int k)
524 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
527 if (w->at <= heap [j]->at)
531 ((W)heap [k])->active = k + 1;
536 ((W)heap [k])->active = k + 1;
540 adjustheap (WT *heap, int N, int k)
543 downheap (heap, N, k);
546 /*****************************************************************************/
551 sig_atomic_t volatile gotsig;
554 static ANSIG *signals;
555 static int signalmax;
557 static int sigpipe [2];
558 static sig_atomic_t volatile gotsig;
559 static struct ev_io sigev;
562 signals_init (ANSIG *base, int count)
574 sighandler (int signum)
577 signal (signum, sighandler);
580 signals [signum - 1].gotsig = 1;
584 int old_errno = errno;
587 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
589 write (sigpipe [1], &signum, 1);
596 ev_feed_signal_event (EV_P_ int signum)
601 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
606 if (signum < 0 || signum >= signalmax)
609 signals [signum].gotsig = 0;
611 for (w = signals [signum].head; w; w = w->next)
612 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
616 sigcb (EV_P_ struct ev_io *iow, int revents)
621 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
623 read (sigpipe [0], &revents, 1);
627 for (signum = signalmax; signum--; )
628 if (signals [signum].gotsig)
629 ev_feed_signal_event (EV_A_ signum + 1);
636 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
637 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
639 /* rather than sort out wether we really need nb, set it */
640 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
641 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
644 ev_io_set (&sigev, sigpipe [0], EV_READ);
645 ev_io_start (EV_A_ &sigev);
646 ev_unref (EV_A); /* child watcher should not keep loop alive */
649 /*****************************************************************************/
651 static struct ev_child *childs [PID_HASHSIZE];
655 static struct ev_signal childev;
658 # define WCONTINUED 0
662 child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
666 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
667 if (w->pid == pid || !w->pid)
669 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
672 ev_feed_event (EV_A_ (W)w, EV_CHILD);
677 childcb (EV_P_ struct ev_signal *sw, int revents)
681 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
683 /* make sure we are called again until all childs have been reaped */
684 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
686 child_reap (EV_A_ sw, pid, pid, status);
687 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
693 /*****************************************************************************/
696 # include "ev_kqueue.c"
699 # include "ev_epoll.c"
702 # include "ev_poll.c"
705 # include "ev_select.c"
709 ev_version_major (void)
711 return EV_VERSION_MAJOR;
715 ev_version_minor (void)
717 return EV_VERSION_MINOR;
720 /* return true if we are running with elevated privileges and should ignore env variables */
727 return getuid () != geteuid ()
728 || getgid () != getegid ();
739 loop_init (EV_P_ int methods)
746 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
751 ev_rt_now = ev_time ();
752 mn_now = get_clock ();
754 rtmn_diff = ev_rt_now - mn_now;
756 if (methods == EVMETHOD_AUTO)
757 if (!enable_secure () && getenv ("LIBEV_METHODS"))
758 methods = atoi (getenv ("LIBEV_METHODS"));
760 methods = EVMETHOD_ANY;
764 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
767 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
770 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
773 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
776 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
779 ev_init (&sigev, sigcb);
780 ev_set_priority (&sigev, EV_MAXPRI);
790 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
793 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
796 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
799 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
802 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
805 for (i = NUMPRI; i--; )
806 array_free (pending, [i]);
808 /* have to use the microsoft-never-gets-it-right macro */
809 array_free_microshit (fdchange);
810 array_free_microshit (timer);
812 array_free_microshit (periodic);
814 array_free_microshit (idle);
815 array_free_microshit (prepare);
816 array_free_microshit (check);
825 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
828 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
831 if (ev_is_active (&sigev))
836 ev_io_stop (EV_A_ &sigev);
840 while (pipe (sigpipe))
841 syserr ("(libev) error creating pipe");
851 ev_loop_new (int methods)
853 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
855 memset (loop, 0, sizeof (struct ev_loop));
857 loop_init (EV_A_ methods);
859 if (ev_method (EV_A))
866 ev_loop_destroy (EV_P)
885 ev_default_loop (int methods)
887 if (sigpipe [0] == sigpipe [1])
894 struct ev_loop *loop = default_loop = &default_loop_struct;
899 loop_init (EV_A_ methods);
901 if (ev_method (EV_A))
906 ev_signal_init (&childev, childcb, SIGCHLD);
907 ev_set_priority (&childev, EV_MAXPRI);
908 ev_signal_start (EV_A_ &childev);
909 ev_unref (EV_A); /* child watcher should not keep loop alive */
920 ev_default_destroy (void)
923 struct ev_loop *loop = default_loop;
927 ev_ref (EV_A); /* child watcher */
928 ev_signal_stop (EV_A_ &childev);
931 ev_ref (EV_A); /* signal watcher */
932 ev_io_stop (EV_A_ &sigev);
934 close (sigpipe [0]); sigpipe [0] = 0;
935 close (sigpipe [1]); sigpipe [1] = 0;
941 ev_default_fork (void)
944 struct ev_loop *loop = default_loop;
951 /*****************************************************************************/
958 for (pri = NUMPRI; pri--; )
959 if (pendingcnt [pri])
970 for (pri = NUMPRI; pri--; )
971 while (pendingcnt [pri])
973 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
978 EV_CB_INVOKE (p->w, p->events);
986 while (timercnt && ((WT)timers [0])->at <= mn_now)
988 struct ev_timer *w = timers [0];
990 assert (("inactive timer on timer heap detected", ev_is_active (w)));
992 /* first reschedule or stop timer */
995 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
997 ((WT)w)->at += w->repeat;
998 if (((WT)w)->at < mn_now)
999 ((WT)w)->at = mn_now;
1001 downheap ((WT *)timers, timercnt, 0);
1004 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1006 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1012 periodics_reify (EV_P)
1014 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1016 struct ev_periodic *w = periodics [0];
1018 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1020 /* first reschedule or stop timer */
1021 if (w->reschedule_cb)
1023 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1025 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1026 downheap ((WT *)periodics, periodiccnt, 0);
1028 else if (w->interval)
1030 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
1031 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1032 downheap ((WT *)periodics, periodiccnt, 0);
1035 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1037 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1042 periodics_reschedule (EV_P)
1046 /* adjust periodics after time jump */
1047 for (i = 0; i < periodiccnt; ++i)
1049 struct ev_periodic *w = periodics [i];
1051 if (w->reschedule_cb)
1052 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1053 else if (w->interval)
1054 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1057 /* now rebuild the heap */
1058 for (i = periodiccnt >> 1; i--; )
1059 downheap ((WT *)periodics, periodiccnt, i);
1064 time_update_monotonic (EV_P)
1066 mn_now = get_clock ();
1068 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1070 ev_rt_now = rtmn_diff + mn_now;
1076 ev_rt_now = ev_time ();
1086 #if EV_USE_MONOTONIC
1087 if (expect_true (have_monotonic))
1089 if (time_update_monotonic (EV_A))
1091 ev_tstamp odiff = rtmn_diff;
1093 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1095 rtmn_diff = ev_rt_now - mn_now;
1097 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1098 return; /* all is well */
1100 ev_rt_now = ev_time ();
1101 mn_now = get_clock ();
1106 periodics_reschedule (EV_A);
1108 /* no timer adjustment, as the monotonic clock doesn't jump */
1109 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1115 ev_rt_now = ev_time ();
1117 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1120 periodics_reschedule (EV_A);
1123 /* adjust timers. this is easy, as the offset is the same for all */
1124 for (i = 0; i < timercnt; ++i)
1125 ((WT)timers [i])->at += ev_rt_now - mn_now;
1144 static int loop_done;
1147 ev_loop (EV_P_ int flags)
1150 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1154 /* queue check watchers (and execute them) */
1155 if (expect_false (preparecnt))
1157 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1158 call_pending (EV_A);
1161 /* we might have forked, so reify kernel state if necessary */
1162 if (expect_false (postfork))
1165 /* update fd-related kernel structures */
1168 /* calculate blocking time */
1170 /* we only need this for !monotonic clock or timers, but as we basically
1171 always have timers, we just calculate it always */
1172 #if EV_USE_MONOTONIC
1173 if (expect_true (have_monotonic))
1174 time_update_monotonic (EV_A);
1178 ev_rt_now = ev_time ();
1182 if (flags & EVLOOP_NONBLOCK || idlecnt)
1186 block = MAX_BLOCKTIME;
1190 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1191 if (block > to) block = to;
1197 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1198 if (block > to) block = to;
1202 if (block < 0.) block = 0.;
1205 method_poll (EV_A_ block);
1207 /* update ev_rt_now, do magic */
1210 /* queue pending timers and reschedule them */
1211 timers_reify (EV_A); /* relative timers called last */
1213 periodics_reify (EV_A); /* absolute timers called first */
1216 /* queue idle watchers unless io or timers are pending */
1217 if (idlecnt && !any_pending (EV_A))
1218 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1220 /* queue check watchers, to be executed first */
1222 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1224 call_pending (EV_A);
1226 while (activecnt && !loop_done);
1233 ev_unloop (EV_P_ int how)
1238 /*****************************************************************************/
1241 wlist_add (WL *head, WL elem)
1248 wlist_del (WL *head, WL elem)
1258 head = &(*head)->next;
1263 ev_clear_pending (EV_P_ W w)
1267 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1273 ev_start (EV_P_ W w, int active)
1275 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1276 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1289 /*****************************************************************************/
1292 ev_io_start (EV_P_ struct ev_io *w)
1296 if (ev_is_active (w))
1299 assert (("ev_io_start called with negative fd", fd >= 0));
1301 ev_start (EV_A_ (W)w, 1);
1302 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1303 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1305 fd_change (EV_A_ fd);
1309 ev_io_stop (EV_P_ struct ev_io *w)
1311 ev_clear_pending (EV_A_ (W)w);
1312 if (!ev_is_active (w))
1315 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1317 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1318 ev_stop (EV_A_ (W)w);
1320 fd_change (EV_A_ w->fd);
1324 ev_timer_start (EV_P_ struct ev_timer *w)
1326 if (ev_is_active (w))
1329 ((WT)w)->at += mn_now;
1331 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1333 ev_start (EV_A_ (W)w, ++timercnt);
1334 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1335 timers [timercnt - 1] = w;
1336 upheap ((WT *)timers, timercnt - 1);
1338 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1342 ev_timer_stop (EV_P_ struct ev_timer *w)
1344 ev_clear_pending (EV_A_ (W)w);
1345 if (!ev_is_active (w))
1348 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1350 if (((W)w)->active < timercnt--)
1352 timers [((W)w)->active - 1] = timers [timercnt];
1353 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1356 ((WT)w)->at -= mn_now;
1358 ev_stop (EV_A_ (W)w);
1362 ev_timer_again (EV_P_ struct ev_timer *w)
1364 if (ev_is_active (w))
1368 ((WT)w)->at = mn_now + w->repeat;
1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1372 ev_timer_stop (EV_A_ w);
1375 ev_timer_start (EV_A_ w);
1380 ev_periodic_start (EV_P_ struct ev_periodic *w)
1382 if (ev_is_active (w))
1385 if (w->reschedule_cb)
1386 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1387 else if (w->interval)
1389 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1390 /* this formula differs from the one in periodic_reify because we do not always round up */
1391 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1394 ev_start (EV_A_ (W)w, ++periodiccnt);
1395 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1396 periodics [periodiccnt - 1] = w;
1397 upheap ((WT *)periodics, periodiccnt - 1);
1399 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1403 ev_periodic_stop (EV_P_ struct ev_periodic *w)
1405 ev_clear_pending (EV_A_ (W)w);
1406 if (!ev_is_active (w))
1409 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1411 if (((W)w)->active < periodiccnt--)
1413 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1414 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1417 ev_stop (EV_A_ (W)w);
1421 ev_periodic_again (EV_P_ struct ev_periodic *w)
1423 /* TODO: use adjustheap and recalculation */
1424 ev_periodic_stop (EV_A_ w);
1425 ev_periodic_start (EV_A_ w);
1430 ev_idle_start (EV_P_ struct ev_idle *w)
1432 if (ev_is_active (w))
1435 ev_start (EV_A_ (W)w, ++idlecnt);
1436 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1437 idles [idlecnt - 1] = w;
1441 ev_idle_stop (EV_P_ struct ev_idle *w)
1443 ev_clear_pending (EV_A_ (W)w);
1444 if (!ev_is_active (w))
1447 idles [((W)w)->active - 1] = idles [--idlecnt];
1448 ev_stop (EV_A_ (W)w);
1452 ev_prepare_start (EV_P_ struct ev_prepare *w)
1454 if (ev_is_active (w))
1457 ev_start (EV_A_ (W)w, ++preparecnt);
1458 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1459 prepares [preparecnt - 1] = w;
1463 ev_prepare_stop (EV_P_ struct ev_prepare *w)
1465 ev_clear_pending (EV_A_ (W)w);
1466 if (!ev_is_active (w))
1469 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1470 ev_stop (EV_A_ (W)w);
1474 ev_check_start (EV_P_ struct ev_check *w)
1476 if (ev_is_active (w))
1479 ev_start (EV_A_ (W)w, ++checkcnt);
1480 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1481 checks [checkcnt - 1] = w;
1485 ev_check_stop (EV_P_ struct ev_check *w)
1487 ev_clear_pending (EV_A_ (W)w);
1488 if (!ev_is_active (w))
1491 checks [((W)w)->active - 1] = checks [--checkcnt];
1492 ev_stop (EV_A_ (W)w);
1496 # define SA_RESTART 0
1500 ev_signal_start (EV_P_ struct ev_signal *w)
1503 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1505 if (ev_is_active (w))
1508 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1510 ev_start (EV_A_ (W)w, 1);
1511 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1512 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1517 signal (w->signum, sighandler);
1519 struct sigaction sa;
1520 sa.sa_handler = sighandler;
1521 sigfillset (&sa.sa_mask);
1522 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1523 sigaction (w->signum, &sa, 0);
1529 ev_signal_stop (EV_P_ struct ev_signal *w)
1531 ev_clear_pending (EV_A_ (W)w);
1532 if (!ev_is_active (w))
1535 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1536 ev_stop (EV_A_ (W)w);
1538 if (!signals [w->signum - 1].head)
1539 signal (w->signum, SIG_DFL);
1543 ev_child_start (EV_P_ struct ev_child *w)
1546 assert (("child watchers are only supported in the default loop", loop == default_loop));
1548 if (ev_is_active (w))
1551 ev_start (EV_A_ (W)w, 1);
1552 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1556 ev_child_stop (EV_P_ struct ev_child *w)
1558 ev_clear_pending (EV_A_ (W)w);
1559 if (!ev_is_active (w))
1562 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1563 ev_stop (EV_A_ (W)w);
1566 /*****************************************************************************/
1572 void (*cb)(int revents, void *arg);
1577 once_cb (EV_P_ struct ev_once *once, int revents)
1579 void (*cb)(int revents, void *arg) = once->cb;
1580 void *arg = once->arg;
1582 ev_io_stop (EV_A_ &once->io);
1583 ev_timer_stop (EV_A_ &once->to);
1590 once_cb_io (EV_P_ struct ev_io *w, int revents)
1592 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1596 once_cb_to (EV_P_ struct ev_timer *w, int revents)
1598 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1602 ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1604 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1607 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1613 ev_init (&once->io, once_cb_io);
1616 ev_io_set (&once->io, fd, events);
1617 ev_io_start (EV_A_ &once->io);
1620 ev_init (&once->to, once_cb_to);
1623 ev_timer_set (&once->to, timeout, 0.);
1624 ev_timer_start (EV_A_ &once->to);