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.
34 # if HAVE_CLOCK_GETTIME
35 # define EV_USE_MONOTONIC 1
36 # define EV_USE_REALTIME 1
39 # if HAVE_SELECT && HAVE_SYS_SELECT_H
40 # define EV_USE_SELECT 1
43 # if HAVE_POLL && HAVE_POLL_H
44 # define EV_USE_POLL 1
47 # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48 # define EV_USE_EPOLL 1
51 # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52 # define EV_USE_KQUEUE 1
68 #include <sys/types.h>
70 # include <sys/wait.h>
77 #ifndef EV_USE_MONOTONIC
78 # define EV_USE_MONOTONIC 1
82 # define EV_USE_SELECT 1
86 # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
90 # define EV_USE_EPOLL 0
94 # define EV_USE_KQUEUE 0
99 # define EV_USE_WIN32 1
101 # define EV_USE_WIN32 0
105 #ifndef EV_USE_REALTIME
106 # define EV_USE_REALTIME 1
111 #ifndef CLOCK_MONOTONIC
112 # undef EV_USE_MONOTONIC
113 # define EV_USE_MONOTONIC 0
116 #ifndef CLOCK_REALTIME
117 # undef EV_USE_REALTIME
118 # define EV_USE_REALTIME 0
123 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
124 #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
125 #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
126 /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
131 # define expect(expr,value) __builtin_expect ((expr),(value))
132 # define inline inline
134 # define expect(expr,value) (expr)
135 # define inline static
138 #define expect_false(expr) expect ((expr) != 0, 0)
139 #define expect_true(expr) expect ((expr) != 0, 1)
141 #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
142 #define ABSPRI(w) ((w)->priority - EV_MINPRI)
144 typedef struct ev_watcher *W;
145 typedef struct ev_watcher_list *WL;
146 typedef struct ev_watcher_time *WT;
148 static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
151 /* note: the comment below could not be substantiated, but what would I care */
152 /* MSDN says this is required to handle SIGFPE */
153 volatile double SIGFPE_REQ = 0.0f;
156 /*****************************************************************************/
158 static void (*syserr_cb)(const char *msg);
160 void ev_set_syserr_cb (void (*cb)(const char *msg))
166 syserr (const char *msg)
169 msg = "(libev) system error";
180 static void *(*alloc)(void *ptr, long size);
182 void ev_set_allocator (void *(*cb)(void *ptr, long size))
188 ev_realloc (void *ptr, long size)
190 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
194 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
201 #define ev_malloc(size) ev_realloc (0, (size))
202 #define ev_free(ptr) ev_realloc ((ptr), 0)
204 /*****************************************************************************/
209 unsigned char events;
223 # define VAR(name,decl) decl;
224 # include "ev_vars.h"
227 # include "ev_wrap.h"
231 # define VAR(name,decl) static decl;
232 # include "ev_vars.h"
237 /*****************************************************************************/
244 clock_gettime (CLOCK_REALTIME, &ts);
245 return ts.tv_sec + ts.tv_nsec * 1e-9;
248 gettimeofday (&tv, 0);
249 return tv.tv_sec + tv.tv_usec * 1e-6;
257 if (expect_true (have_monotonic))
260 clock_gettime (CLOCK_MONOTONIC, &ts);
261 return ts.tv_sec + ts.tv_nsec * 1e-9;
274 #define array_roundsize(base,n) ((n) | 4 & ~3)
276 #define array_needsize(base,cur,cnt,init) \
277 if (expect_false ((cnt) > cur)) \
282 newcnt = array_roundsize (base, newcnt << 1); \
284 while ((cnt) > newcnt); \
286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
287 init (base + cur, newcnt - cur); \
291 #define array_slim(stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
299 #define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
302 /*****************************************************************************/
305 anfds_init (ANFD *base, int count)
310 base->events = EV_NONE;
318 event (EV_P_ W w, int events)
322 pendings [ABSPRI (w)][w->pending - 1].events |= events;
326 w->pending = ++pendingcnt [ABSPRI (w)];
327 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
328 pendings [ABSPRI (w)][w->pending - 1].w = w;
329 pendings [ABSPRI (w)][w->pending - 1].events = events;
333 queue_events (EV_P_ W *events, int eventcnt, int type)
337 for (i = 0; i < eventcnt; ++i)
338 event (EV_A_ events [i], type);
342 fd_event (EV_P_ int fd, int events)
344 ANFD *anfd = anfds + fd;
347 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
349 int ev = w->events & events;
352 event (EV_A_ (W)w, ev);
356 /*****************************************************************************/
363 for (i = 0; i < fdchangecnt; ++i)
365 int fd = fdchanges [i];
366 ANFD *anfd = anfds + fd;
371 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
376 method_modify (EV_A_ fd, anfd->events, events);
377 anfd->events = events;
384 fd_change (EV_P_ int fd)
386 if (anfds [fd].reify)
389 anfds [fd].reify = 1;
392 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
393 fdchanges [fdchangecnt - 1] = fd;
397 fd_kill (EV_P_ int fd)
401 while ((w = (struct ev_io *)anfds [fd].head))
403 ev_io_stop (EV_A_ w);
404 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
408 /* called on EBADF to verify fds */
414 for (fd = 0; fd < anfdmax; ++fd)
415 if (anfds [fd].events)
416 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
420 /* called on ENOMEM in select/poll to kill some fds and retry */
426 for (fd = anfdmax; fd--; )
427 if (anfds [fd].events)
434 /* usually called after fork if method needs to re-arm all fds from scratch */
440 /* this should be highly optimised to not do anything but set a flag */
441 for (fd = 0; fd < anfdmax; ++fd)
442 if (anfds [fd].events)
444 anfds [fd].events = 0;
445 fd_change (EV_A_ fd);
449 /*****************************************************************************/
452 upheap (WT *heap, int k)
456 while (k && heap [k >> 1]->at > w->at)
458 heap [k] = heap [k >> 1];
459 ((W)heap [k])->active = k + 1;
464 ((W)heap [k])->active = k + 1;
469 downheap (WT *heap, int N, int k)
477 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
480 if (w->at <= heap [j]->at)
484 ((W)heap [k])->active = k + 1;
489 ((W)heap [k])->active = k + 1;
492 /*****************************************************************************/
497 sig_atomic_t volatile gotsig;
500 static ANSIG *signals;
501 static int signalmax;
503 static int sigpipe [2];
504 static sig_atomic_t volatile gotsig;
505 static struct ev_io sigev;
508 signals_init (ANSIG *base, int count)
520 sighandler (int signum)
523 signal (signum, sighandler);
526 signals [signum - 1].gotsig = 1;
530 int old_errno = errno;
532 write (sigpipe [1], &signum, 1);
538 sigcb (EV_P_ struct ev_io *iow, int revents)
543 read (sigpipe [0], &revents, 1);
546 for (signum = signalmax; signum--; )
547 if (signals [signum].gotsig)
549 signals [signum].gotsig = 0;
551 for (w = signals [signum].head; w; w = w->next)
552 event (EV_A_ (W)w, EV_SIGNAL);
560 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
561 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
563 /* rather than sort out wether we really need nb, set it */
564 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
565 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
568 ev_io_set (&sigev, sigpipe [0], EV_READ);
569 ev_io_start (EV_A_ &sigev);
570 ev_unref (EV_A); /* child watcher should not keep loop alive */
573 /*****************************************************************************/
577 static struct ev_child *childs [PID_HASHSIZE];
578 static struct ev_signal childev;
581 # define WCONTINUED 0
585 child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
589 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
590 if (w->pid == pid || !w->pid)
592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
595 event (EV_A_ (W)w, EV_CHILD);
600 childcb (EV_P_ struct ev_signal *sw, int revents)
604 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
606 /* make sure we are called again until all childs have been reaped */
607 event (EV_A_ (W)sw, EV_SIGNAL);
609 child_reap (EV_A_ sw, pid, pid, status);
610 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
616 /*****************************************************************************/
619 # include "ev_kqueue.c"
622 # include "ev_epoll.c"
625 # include "ev_poll.c"
628 # include "ev_select.c"
632 ev_version_major (void)
634 return EV_VERSION_MAJOR;
638 ev_version_minor (void)
640 return EV_VERSION_MINOR;
643 /* return true if we are running with elevated privileges and should ignore env variables */
650 return getuid () != geteuid ()
651 || getgid () != getegid ();
662 loop_init (EV_P_ int methods)
669 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
675 mn_now = get_clock ();
677 rtmn_diff = rt_now - mn_now;
679 if (methods == EVMETHOD_AUTO)
680 if (!enable_secure () && getenv ("LIBEV_METHODS"))
681 methods = atoi (getenv ("LIBEV_METHODS"));
683 methods = EVMETHOD_ANY;
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
722 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
728 for (i = NUMPRI; i--; )
729 array_free (pending, [i]);
731 array_free (fdchange, );
732 array_free (timer, );
733 array_free (periodic, );
735 array_free (prepare, );
736 array_free (check, );
745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
751 if (ev_is_active (&sigev))
756 ev_io_stop (EV_A_ &sigev);
760 while (pipe (sigpipe))
761 syserr ("(libev) error creating pipe");
771 ev_loop_new (int methods)
773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
775 memset (loop, 0, sizeof (struct ev_loop));
777 loop_init (EV_A_ methods);
779 if (ev_method (EV_A))
786 ev_loop_destroy (EV_P)
801 struct ev_loop default_loop_struct;
802 static struct ev_loop *default_loop;
806 static int default_loop;
810 ev_default_loop (int methods)
812 if (sigpipe [0] == sigpipe [1])
819 struct ev_loop *loop = default_loop = &default_loop_struct;
824 loop_init (EV_A_ methods);
826 if (ev_method (EV_A))
831 ev_signal_init (&childev, childcb, SIGCHLD);
832 ev_set_priority (&childev, EV_MAXPRI);
833 ev_signal_start (EV_A_ &childev);
834 ev_unref (EV_A); /* child watcher should not keep loop alive */
845 ev_default_destroy (void)
848 struct ev_loop *loop = default_loop;
851 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev);
854 ev_ref (EV_A); /* signal watcher */
855 ev_io_stop (EV_A_ &sigev);
857 close (sigpipe [0]); sigpipe [0] = 0;
858 close (sigpipe [1]); sigpipe [1] = 0;
864 ev_default_fork (void)
867 struct ev_loop *loop = default_loop;
874 /*****************************************************************************/
881 for (pri = NUMPRI; pri--; )
882 while (pendingcnt [pri])
884 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
889 p->w->cb (EV_A_ p->w, p->events);
897 while (timercnt && ((WT)timers [0])->at <= mn_now)
899 struct ev_timer *w = timers [0];
901 assert (("inactive timer on timer heap detected", ev_is_active (w)));
903 /* first reschedule or stop timer */
906 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
907 ((WT)w)->at = mn_now + w->repeat;
908 downheap ((WT *)timers, timercnt, 0);
911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
913 event (EV_A_ (W)w, EV_TIMEOUT);
918 periodics_reify (EV_P)
920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
922 struct ev_periodic *w = periodics [0];
924 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
926 /* first reschedule or stop timer */
929 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
930 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
931 downheap ((WT *)periodics, periodiccnt, 0);
934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
936 event (EV_A_ (W)w, EV_PERIODIC);
941 periodics_reschedule (EV_P)
945 /* adjust periodics after time jump */
946 for (i = 0; i < periodiccnt; ++i)
948 struct ev_periodic *w = periodics [i];
952 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
954 if (fabs (diff) >= 1e-4)
956 ev_periodic_stop (EV_A_ w);
957 ev_periodic_start (EV_A_ w);
959 i = 0; /* restart loop, inefficient, but time jumps should be rare */
966 time_update_monotonic (EV_P)
968 mn_now = get_clock ();
970 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
972 rt_now = rtmn_diff + mn_now;
989 if (expect_true (have_monotonic))
991 if (time_update_monotonic (EV_A))
993 ev_tstamp odiff = rtmn_diff;
995 for (i = 4; --i; ) /* loop a few times, before making important decisions */
997 rtmn_diff = rt_now - mn_now;
999 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1000 return; /* all is well */
1002 rt_now = ev_time ();
1003 mn_now = get_clock ();
1007 periodics_reschedule (EV_A);
1008 /* no timer adjustment, as the monotonic clock doesn't jump */
1009 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1015 rt_now = ev_time ();
1017 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1019 periodics_reschedule (EV_A);
1021 /* adjust timers. this is easy, as the offset is the same for all */
1022 for (i = 0; i < timercnt; ++i)
1023 ((WT)timers [i])->at += rt_now - mn_now;
1042 static int loop_done;
1045 ev_loop (EV_P_ int flags)
1048 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1052 /* queue check watchers (and execute them) */
1053 if (expect_false (preparecnt))
1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1056 call_pending (EV_A);
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1063 /* update fd-related kernel structures */
1066 /* calculate blocking time */
1068 /* we only need this for !monotonic clockor timers, but as we basically
1069 always have timers, we just calculate it always */
1070 #if EV_USE_MONOTONIC
1071 if (expect_true (have_monotonic))
1072 time_update_monotonic (EV_A);
1076 rt_now = ev_time ();
1080 if (flags & EVLOOP_NONBLOCK || idlecnt)
1084 block = MAX_BLOCKTIME;
1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1089 if (block > to) block = to;
1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
1095 if (block > to) block = to;
1098 if (block < 0.) block = 0.;
1101 method_poll (EV_A_ block);
1103 /* update rt_now, do magic */
1106 /* queue pending timers and reschedule them */
1107 timers_reify (EV_A); /* relative timers called last */
1108 periodics_reify (EV_A); /* absolute timers called first */
1110 /* queue idle watchers unless io or timers are pending */
1112 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1114 /* queue check watchers, to be executed first */
1116 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1118 call_pending (EV_A);
1120 while (activecnt && !loop_done);
1127 ev_unloop (EV_P_ int how)
1132 /*****************************************************************************/
1135 wlist_add (WL *head, WL elem)
1142 wlist_del (WL *head, WL elem)
1152 head = &(*head)->next;
1157 ev_clear_pending (EV_P_ W w)
1161 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1167 ev_start (EV_P_ W w, int active)
1169 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1170 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1183 /*****************************************************************************/
1186 ev_io_start (EV_P_ struct ev_io *w)
1190 if (ev_is_active (w))
1193 assert (("ev_io_start called with negative fd", fd >= 0));
1195 ev_start (EV_A_ (W)w, 1);
1196 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
1197 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1199 fd_change (EV_A_ fd);
1203 ev_io_stop (EV_P_ struct ev_io *w)
1205 ev_clear_pending (EV_A_ (W)w);
1206 if (!ev_is_active (w))
1209 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1210 ev_stop (EV_A_ (W)w);
1212 fd_change (EV_A_ w->fd);
1216 ev_timer_start (EV_P_ struct ev_timer *w)
1218 if (ev_is_active (w))
1221 ((WT)w)->at += mn_now;
1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1225 ev_start (EV_A_ (W)w, ++timercnt);
1226 array_needsize (timers, timermax, timercnt, );
1227 timers [timercnt - 1] = w;
1228 upheap ((WT *)timers, timercnt - 1);
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1234 ev_timer_stop (EV_P_ struct ev_timer *w)
1236 ev_clear_pending (EV_A_ (W)w);
1237 if (!ev_is_active (w))
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1242 if (((W)w)->active < timercnt--)
1244 timers [((W)w)->active - 1] = timers [timercnt];
1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1248 ((WT)w)->at = w->repeat;
1250 ev_stop (EV_A_ (W)w);
1254 ev_timer_again (EV_P_ struct ev_timer *w)
1256 if (ev_is_active (w))
1260 ((WT)w)->at = mn_now + w->repeat;
1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1264 ev_timer_stop (EV_A_ w);
1267 ev_timer_start (EV_A_ w);
1271 ev_periodic_start (EV_P_ struct ev_periodic *w)
1273 if (ev_is_active (w))
1276 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1278 /* this formula differs from the one in periodic_reify because we do not always round up */
1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1283 array_needsize (periodics, periodicmax, periodiccnt, );
1284 periodics [periodiccnt - 1] = w;
1285 upheap ((WT *)periodics, periodiccnt - 1);
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1291 ev_periodic_stop (EV_P_ struct ev_periodic *w)
1293 ev_clear_pending (EV_A_ (W)w);
1294 if (!ev_is_active (w))
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1299 if (((W)w)->active < periodiccnt--)
1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1305 ev_stop (EV_A_ (W)w);
1309 ev_idle_start (EV_P_ struct ev_idle *w)
1311 if (ev_is_active (w))
1314 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, );
1316 idles [idlecnt - 1] = w;
1320 ev_idle_stop (EV_P_ struct ev_idle *w)
1322 ev_clear_pending (EV_A_ (W)w);
1323 if (ev_is_active (w))
1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1327 ev_stop (EV_A_ (W)w);
1331 ev_prepare_start (EV_P_ struct ev_prepare *w)
1333 if (ev_is_active (w))
1336 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, );
1338 prepares [preparecnt - 1] = w;
1342 ev_prepare_stop (EV_P_ struct ev_prepare *w)
1344 ev_clear_pending (EV_A_ (W)w);
1345 if (ev_is_active (w))
1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1349 ev_stop (EV_A_ (W)w);
1353 ev_check_start (EV_P_ struct ev_check *w)
1355 if (ev_is_active (w))
1358 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, );
1360 checks [checkcnt - 1] = w;
1364 ev_check_stop (EV_P_ struct ev_check *w)
1366 ev_clear_pending (EV_A_ (W)w);
1367 if (ev_is_active (w))
1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1371 ev_stop (EV_A_ (W)w);
1375 # define SA_RESTART 0
1379 ev_signal_start (EV_P_ struct ev_signal *w)
1382 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1384 if (ev_is_active (w))
1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1389 ev_start (EV_A_ (W)w, 1);
1390 array_needsize (signals, signalmax, w->signum, signals_init);
1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1396 signal (w->signum, sighandler);
1398 struct sigaction sa;
1399 sa.sa_handler = sighandler;
1400 sigfillset (&sa.sa_mask);
1401 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1402 sigaction (w->signum, &sa, 0);
1408 ev_signal_stop (EV_P_ struct ev_signal *w)
1410 ev_clear_pending (EV_A_ (W)w);
1411 if (!ev_is_active (w))
1414 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1415 ev_stop (EV_A_ (W)w);
1417 if (!signals [w->signum - 1].head)
1418 signal (w->signum, SIG_DFL);
1422 ev_child_start (EV_P_ struct ev_child *w)
1425 assert (("child watchers are only supported in the default loop", loop == default_loop));
1427 if (ev_is_active (w))
1430 ev_start (EV_A_ (W)w, 1);
1431 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1435 ev_child_stop (EV_P_ struct ev_child *w)
1437 ev_clear_pending (EV_A_ (W)w);
1438 if (ev_is_active (w))
1441 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1442 ev_stop (EV_A_ (W)w);
1445 /*****************************************************************************/
1451 void (*cb)(int revents, void *arg);
1456 once_cb (EV_P_ struct ev_once *once, int revents)
1458 void (*cb)(int revents, void *arg) = once->cb;
1459 void *arg = once->arg;
1461 ev_io_stop (EV_A_ &once->io);
1462 ev_timer_stop (EV_A_ &once->to);
1469 once_cb_io (EV_P_ struct ev_io *w, int revents)
1471 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1475 once_cb_to (EV_P_ struct ev_timer *w, int revents)
1477 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1481 ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1492 ev_watcher_init (&once->io, once_cb_io);
1495 ev_io_set (&once->io, fd, events);
1496 ev_io_start (EV_A_ &once->io);
1499 ev_watcher_init (&once->to, once_cb_to);
1502 ev_timer_set (&once->to, timeout, 0.);
1503 ev_timer_start (EV_A_ &once->to);