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
66 #include <sys/types.h>
73 # include <sys/time.h>
74 # include <sys/wait.h>
78 #ifndef EV_USE_MONOTONIC
79 # define EV_USE_MONOTONIC 1
83 # define EV_USE_SELECT 1
87 # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
91 # define EV_USE_EPOLL 0
95 # define EV_USE_KQUEUE 0
100 # define EV_USE_WIN32 0 /* it does not exist, use select */
101 # undef EV_USE_SELECT
102 # define EV_USE_SELECT 1
104 # define EV_USE_WIN32 0
108 #ifndef EV_USE_REALTIME
109 # define EV_USE_REALTIME 1
114 #ifndef CLOCK_MONOTONIC
115 # undef EV_USE_MONOTONIC
116 # define EV_USE_MONOTONIC 0
119 #ifndef CLOCK_REALTIME
120 # undef EV_USE_REALTIME
121 # define EV_USE_REALTIME 0
126 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
127 #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
128 #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
129 /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
134 # define expect(expr,value) __builtin_expect ((expr),(value))
135 # define inline inline
137 # define expect(expr,value) (expr)
138 # define inline static
141 #define expect_false(expr) expect ((expr) != 0, 0)
142 #define expect_true(expr) expect ((expr) != 0, 1)
144 #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
145 #define ABSPRI(w) ((w)->priority - EV_MINPRI)
147 typedef struct ev_watcher *W;
148 typedef struct ev_watcher_list *WL;
149 typedef struct ev_watcher_time *WT;
151 static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
153 #include "ev_win32.c"
155 /*****************************************************************************/
157 static void (*syserr_cb)(const char *msg);
159 void ev_set_syserr_cb (void (*cb)(const char *msg))
165 syserr (const char *msg)
168 msg = "(libev) system error";
179 static void *(*alloc)(void *ptr, long size);
181 void ev_set_allocator (void *(*cb)(void *ptr, long size))
187 ev_realloc (void *ptr, long size)
189 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
193 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
200 #define ev_malloc(size) ev_realloc (0, (size))
201 #define ev_free(ptr) ev_realloc ((ptr), 0)
203 /*****************************************************************************/
208 unsigned char events;
222 # define VAR(name,decl) decl;
223 # include "ev_vars.h"
226 # include "ev_wrap.h"
230 # define VAR(name,decl) static decl;
231 # include "ev_vars.h"
236 /*****************************************************************************/
243 clock_gettime (CLOCK_REALTIME, &ts);
244 return ts.tv_sec + ts.tv_nsec * 1e-9;
247 gettimeofday (&tv, 0);
248 return tv.tv_sec + tv.tv_usec * 1e-6;
256 if (expect_true (have_monotonic))
259 clock_gettime (CLOCK_MONOTONIC, &ts);
260 return ts.tv_sec + ts.tv_nsec * 1e-9;
273 #define array_roundsize(type,n) ((n) | 4 & ~3)
275 #define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \
281 newcnt = array_roundsize (type, newcnt << 1); \
283 while ((cnt) > newcnt); \
285 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
286 init (base + cur, newcnt - cur); \
290 #define array_slim(type,stem) \
291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
298 /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299 /* bringing us everlasting joy in form of stupid extra macros that are not required in C */
300 #define array_free_microshit(stem) \
301 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
303 #define array_free(stem, idx) \
304 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
306 /*****************************************************************************/
309 anfds_init (ANFD *base, int count)
314 base->events = EV_NONE;
322 event (EV_P_ W w, int events)
326 pendings [ABSPRI (w)][w->pending - 1].events |= events;
330 w->pending = ++pendingcnt [ABSPRI (w)];
331 array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w;
333 pendings [ABSPRI (w)][w->pending - 1].events = events;
337 queue_events (EV_P_ W *events, int eventcnt, int type)
341 for (i = 0; i < eventcnt; ++i)
342 event (EV_A_ events [i], type);
346 fd_event (EV_P_ int fd, int events)
348 ANFD *anfd = anfds + fd;
351 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
353 int ev = w->events & events;
356 event (EV_A_ (W)w, ev);
360 /*****************************************************************************/
367 for (i = 0; i < fdchangecnt; ++i)
369 int fd = fdchanges [i];
370 ANFD *anfd = anfds + fd;
375 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
380 method_modify (EV_A_ fd, anfd->events, events);
381 anfd->events = events;
388 fd_change (EV_P_ int fd)
390 if (anfds [fd].reify)
393 anfds [fd].reify = 1;
396 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
397 fdchanges [fdchangecnt - 1] = fd;
401 fd_kill (EV_P_ int fd)
405 while ((w = (struct ev_io *)anfds [fd].head))
407 ev_io_stop (EV_A_ w);
408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
416 return !!win32_get_osfhandle (fd);
418 return fcntl (fd, F_GETFD) != -1;
422 /* called on EBADF to verify fds */
428 for (fd = 0; fd < anfdmax; ++fd)
429 if (anfds [fd].events)
430 if (!fd_valid (fd) == -1 && errno == EBADF)
434 /* called on ENOMEM in select/poll to kill some fds and retry */
440 for (fd = anfdmax; fd--; )
441 if (anfds [fd].events)
448 /* usually called after fork if method needs to re-arm all fds from scratch */
454 /* this should be highly optimised to not do anything but set a flag */
455 for (fd = 0; fd < anfdmax; ++fd)
456 if (anfds [fd].events)
458 anfds [fd].events = 0;
459 fd_change (EV_A_ fd);
463 /*****************************************************************************/
466 upheap (WT *heap, int k)
470 while (k && heap [k >> 1]->at > w->at)
472 heap [k] = heap [k >> 1];
473 ((W)heap [k])->active = k + 1;
478 ((W)heap [k])->active = k + 1;
483 downheap (WT *heap, int N, int k)
491 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
494 if (w->at <= heap [j]->at)
498 ((W)heap [k])->active = k + 1;
503 ((W)heap [k])->active = k + 1;
506 /*****************************************************************************/
511 sig_atomic_t volatile gotsig;
514 static ANSIG *signals;
515 static int signalmax;
517 static int sigpipe [2];
518 static sig_atomic_t volatile gotsig;
519 static struct ev_io sigev;
522 signals_init (ANSIG *base, int count)
534 sighandler (int signum)
537 signal (signum, sighandler);
540 signals [signum - 1].gotsig = 1;
544 int old_errno = errno;
547 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
549 write (sigpipe [1], &signum, 1);
556 sigcb (EV_P_ struct ev_io *iow, int revents)
562 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
564 read (sigpipe [0], &revents, 1);
568 for (signum = signalmax; signum--; )
569 if (signals [signum].gotsig)
571 signals [signum].gotsig = 0;
573 for (w = signals [signum].head; w; w = w->next)
574 event (EV_A_ (W)w, EV_SIGNAL);
582 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
583 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
585 /* rather than sort out wether we really need nb, set it */
586 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
587 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
590 ev_io_set (&sigev, sigpipe [0], EV_READ);
591 ev_io_start (EV_A_ &sigev);
592 ev_unref (EV_A); /* child watcher should not keep loop alive */
595 /*****************************************************************************/
597 static struct ev_child *childs [PID_HASHSIZE];
601 static struct ev_signal childev;
604 # define WCONTINUED 0
608 child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
612 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
613 if (w->pid == pid || !w->pid)
615 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
618 event (EV_A_ (W)w, EV_CHILD);
623 childcb (EV_P_ struct ev_signal *sw, int revents)
627 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
629 /* make sure we are called again until all childs have been reaped */
630 event (EV_A_ (W)sw, EV_SIGNAL);
632 child_reap (EV_A_ sw, pid, pid, status);
633 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
639 /*****************************************************************************/
642 # include "ev_kqueue.c"
645 # include "ev_epoll.c"
648 # include "ev_poll.c"
651 # include "ev_select.c"
655 ev_version_major (void)
657 return EV_VERSION_MAJOR;
661 ev_version_minor (void)
663 return EV_VERSION_MINOR;
666 /* return true if we are running with elevated privileges and should ignore env variables */
673 return getuid () != geteuid ()
674 || getgid () != getegid ();
685 loop_init (EV_P_ int methods)
692 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
698 mn_now = get_clock ();
700 rtmn_diff = rt_now - mn_now;
702 if (methods == EVMETHOD_AUTO)
703 if (!enable_secure () && getenv ("LIBEV_METHODS"))
704 methods = atoi (getenv ("LIBEV_METHODS"));
706 methods = EVMETHOD_ANY;
710 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
713 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
716 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
719 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
722 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
725 ev_watcher_init (&sigev, sigcb);
726 ev_set_priority (&sigev, EV_MAXPRI);
736 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
739 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
742 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
745 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
748 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
751 for (i = NUMPRI; i--; )
752 array_free (pending, [i]);
754 /* have to use the microsoft-never-gets-it-right macro */
755 array_free_microshit (fdchange);
756 array_free_microshit (timer);
757 array_free_microshit (periodic);
758 array_free_microshit (idle);
759 array_free_microshit (prepare);
760 array_free_microshit (check);
769 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
772 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
775 if (ev_is_active (&sigev))
780 ev_io_stop (EV_A_ &sigev);
784 while (pipe (sigpipe))
785 syserr ("(libev) error creating pipe");
795 ev_loop_new (int methods)
797 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
799 memset (loop, 0, sizeof (struct ev_loop));
801 loop_init (EV_A_ methods);
803 if (ev_method (EV_A))
810 ev_loop_destroy (EV_P)
825 struct ev_loop default_loop_struct;
826 static struct ev_loop *default_loop;
830 static int default_loop;
834 ev_default_loop (int methods)
836 if (sigpipe [0] == sigpipe [1])
843 struct ev_loop *loop = default_loop = &default_loop_struct;
848 loop_init (EV_A_ methods);
850 if (ev_method (EV_A))
855 ev_signal_init (&childev, childcb, SIGCHLD);
856 ev_set_priority (&childev, EV_MAXPRI);
857 ev_signal_start (EV_A_ &childev);
858 ev_unref (EV_A); /* child watcher should not keep loop alive */
869 ev_default_destroy (void)
872 struct ev_loop *loop = default_loop;
876 ev_ref (EV_A); /* child watcher */
877 ev_signal_stop (EV_A_ &childev);
880 ev_ref (EV_A); /* signal watcher */
881 ev_io_stop (EV_A_ &sigev);
883 close (sigpipe [0]); sigpipe [0] = 0;
884 close (sigpipe [1]); sigpipe [1] = 0;
890 ev_default_fork (void)
893 struct ev_loop *loop = default_loop;
900 /*****************************************************************************/
907 for (pri = NUMPRI; pri--; )
908 if (pendingcnt [pri])
919 for (pri = NUMPRI; pri--; )
920 while (pendingcnt [pri])
922 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
927 p->w->cb (EV_A_ p->w, p->events);
935 while (timercnt && ((WT)timers [0])->at <= mn_now)
937 struct ev_timer *w = timers [0];
939 assert (("inactive timer on timer heap detected", ev_is_active (w)));
941 /* first reschedule or stop timer */
944 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
945 ((WT)w)->at = mn_now + w->repeat;
946 downheap ((WT *)timers, timercnt, 0);
949 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
951 event (EV_A_ (W)w, EV_TIMEOUT);
956 periodics_reify (EV_P)
958 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
960 struct ev_periodic *w = periodics [0];
962 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
964 /* first reschedule or stop timer */
965 if (w->reschedule_cb)
967 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
969 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
970 downheap ((WT *)periodics, periodiccnt, 0);
972 else if (w->interval)
974 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
975 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
976 downheap ((WT *)periodics, periodiccnt, 0);
979 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
981 event (EV_A_ (W)w, EV_PERIODIC);
986 periodics_reschedule (EV_P)
990 /* adjust periodics after time jump */
991 for (i = 0; i < periodiccnt; ++i)
993 struct ev_periodic *w = periodics [i];
995 if (w->reschedule_cb)
996 ((WT)w)->at = w->reschedule_cb (w, rt_now);
997 else if (w->interval)
998 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1001 /* now rebuild the heap */
1002 for (i = periodiccnt >> 1; i--; )
1003 downheap ((WT *)periodics, periodiccnt, i);
1007 time_update_monotonic (EV_P)
1009 mn_now = get_clock ();
1011 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1013 rt_now = rtmn_diff + mn_now;
1019 rt_now = ev_time ();
1029 #if EV_USE_MONOTONIC
1030 if (expect_true (have_monotonic))
1032 if (time_update_monotonic (EV_A))
1034 ev_tstamp odiff = rtmn_diff;
1036 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1038 rtmn_diff = rt_now - mn_now;
1040 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1041 return; /* all is well */
1043 rt_now = ev_time ();
1044 mn_now = get_clock ();
1048 periodics_reschedule (EV_A);
1049 /* no timer adjustment, as the monotonic clock doesn't jump */
1050 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1056 rt_now = ev_time ();
1058 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1060 periodics_reschedule (EV_A);
1062 /* adjust timers. this is easy, as the offset is the same for all */
1063 for (i = 0; i < timercnt; ++i)
1064 ((WT)timers [i])->at += rt_now - mn_now;
1083 static int loop_done;
1086 ev_loop (EV_P_ int flags)
1089 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1093 /* queue check watchers (and execute them) */
1094 if (expect_false (preparecnt))
1096 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1097 call_pending (EV_A);
1100 /* we might have forked, so reify kernel state if necessary */
1101 if (expect_false (postfork))
1104 /* update fd-related kernel structures */
1107 /* calculate blocking time */
1109 /* we only need this for !monotonic clock or timers, but as we basically
1110 always have timers, we just calculate it always */
1111 #if EV_USE_MONOTONIC
1112 if (expect_true (have_monotonic))
1113 time_update_monotonic (EV_A);
1117 rt_now = ev_time ();
1121 if (flags & EVLOOP_NONBLOCK || idlecnt)
1125 block = MAX_BLOCKTIME;
1129 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1130 if (block > to) block = to;
1135 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
1136 if (block > to) block = to;
1139 if (block < 0.) block = 0.;
1142 method_poll (EV_A_ block);
1144 /* update rt_now, do magic */
1147 /* queue pending timers and reschedule them */
1148 timers_reify (EV_A); /* relative timers called last */
1149 periodics_reify (EV_A); /* absolute timers called first */
1151 /* queue idle watchers unless io or timers are pending */
1152 if (idlecnt && !any_pending (EV_A))
1153 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1155 /* queue check watchers, to be executed first */
1157 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1159 call_pending (EV_A);
1161 while (activecnt && !loop_done);
1168 ev_unloop (EV_P_ int how)
1173 /*****************************************************************************/
1176 wlist_add (WL *head, WL elem)
1183 wlist_del (WL *head, WL elem)
1193 head = &(*head)->next;
1198 ev_clear_pending (EV_P_ W w)
1202 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1208 ev_start (EV_P_ W w, int active)
1210 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1211 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1224 /*****************************************************************************/
1227 ev_io_start (EV_P_ struct ev_io *w)
1231 if (ev_is_active (w))
1234 assert (("ev_io_start called with negative fd", fd >= 0));
1236 ev_start (EV_A_ (W)w, 1);
1237 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1238 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1240 fd_change (EV_A_ fd);
1244 ev_io_stop (EV_P_ struct ev_io *w)
1246 ev_clear_pending (EV_A_ (W)w);
1247 if (!ev_is_active (w))
1250 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1251 ev_stop (EV_A_ (W)w);
1253 fd_change (EV_A_ w->fd);
1257 ev_timer_start (EV_P_ struct ev_timer *w)
1259 if (ev_is_active (w))
1262 ((WT)w)->at += mn_now;
1264 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1266 ev_start (EV_A_ (W)w, ++timercnt);
1267 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1268 timers [timercnt - 1] = w;
1269 upheap ((WT *)timers, timercnt - 1);
1271 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1275 ev_timer_stop (EV_P_ struct ev_timer *w)
1277 ev_clear_pending (EV_A_ (W)w);
1278 if (!ev_is_active (w))
1281 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1283 if (((W)w)->active < timercnt--)
1285 timers [((W)w)->active - 1] = timers [timercnt];
1286 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1289 ((WT)w)->at = w->repeat;
1291 ev_stop (EV_A_ (W)w);
1295 ev_timer_again (EV_P_ struct ev_timer *w)
1297 if (ev_is_active (w))
1301 ((WT)w)->at = mn_now + w->repeat;
1302 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1305 ev_timer_stop (EV_A_ w);
1308 ev_timer_start (EV_A_ w);
1312 ev_periodic_start (EV_P_ struct ev_periodic *w)
1314 if (ev_is_active (w))
1317 if (w->reschedule_cb)
1318 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1319 else if (w->interval)
1321 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1322 /* this formula differs from the one in periodic_reify because we do not always round up */
1323 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1326 ev_start (EV_A_ (W)w, ++periodiccnt);
1327 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1328 periodics [periodiccnt - 1] = w;
1329 upheap ((WT *)periodics, periodiccnt - 1);
1331 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1335 ev_periodic_stop (EV_P_ struct ev_periodic *w)
1337 ev_clear_pending (EV_A_ (W)w);
1338 if (!ev_is_active (w))
1341 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1343 if (((W)w)->active < periodiccnt--)
1345 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1346 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1349 ev_stop (EV_A_ (W)w);
1353 ev_periodic_again (EV_P_ struct ev_periodic *w)
1355 ev_periodic_stop (EV_A_ w);
1356 ev_periodic_start (EV_A_ w);
1360 ev_idle_start (EV_P_ struct ev_idle *w)
1362 if (ev_is_active (w))
1365 ev_start (EV_A_ (W)w, ++idlecnt);
1366 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1367 idles [idlecnt - 1] = w;
1371 ev_idle_stop (EV_P_ struct ev_idle *w)
1373 ev_clear_pending (EV_A_ (W)w);
1374 if (ev_is_active (w))
1377 idles [((W)w)->active - 1] = idles [--idlecnt];
1378 ev_stop (EV_A_ (W)w);
1382 ev_prepare_start (EV_P_ struct ev_prepare *w)
1384 if (ev_is_active (w))
1387 ev_start (EV_A_ (W)w, ++preparecnt);
1388 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1389 prepares [preparecnt - 1] = w;
1393 ev_prepare_stop (EV_P_ struct ev_prepare *w)
1395 ev_clear_pending (EV_A_ (W)w);
1396 if (ev_is_active (w))
1399 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1400 ev_stop (EV_A_ (W)w);
1404 ev_check_start (EV_P_ struct ev_check *w)
1406 if (ev_is_active (w))
1409 ev_start (EV_A_ (W)w, ++checkcnt);
1410 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1411 checks [checkcnt - 1] = w;
1415 ev_check_stop (EV_P_ struct ev_check *w)
1417 ev_clear_pending (EV_A_ (W)w);
1418 if (ev_is_active (w))
1421 checks [((W)w)->active - 1] = checks [--checkcnt];
1422 ev_stop (EV_A_ (W)w);
1426 # define SA_RESTART 0
1430 ev_signal_start (EV_P_ struct ev_signal *w)
1433 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1435 if (ev_is_active (w))
1438 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1440 ev_start (EV_A_ (W)w, 1);
1441 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1442 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1447 signal (w->signum, sighandler);
1449 struct sigaction sa;
1450 sa.sa_handler = sighandler;
1451 sigfillset (&sa.sa_mask);
1452 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1453 sigaction (w->signum, &sa, 0);
1459 ev_signal_stop (EV_P_ struct ev_signal *w)
1461 ev_clear_pending (EV_A_ (W)w);
1462 if (!ev_is_active (w))
1465 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1466 ev_stop (EV_A_ (W)w);
1468 if (!signals [w->signum - 1].head)
1469 signal (w->signum, SIG_DFL);
1473 ev_child_start (EV_P_ struct ev_child *w)
1476 assert (("child watchers are only supported in the default loop", loop == default_loop));
1478 if (ev_is_active (w))
1481 ev_start (EV_A_ (W)w, 1);
1482 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1486 ev_child_stop (EV_P_ struct ev_child *w)
1488 ev_clear_pending (EV_A_ (W)w);
1489 if (ev_is_active (w))
1492 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1493 ev_stop (EV_A_ (W)w);
1496 /*****************************************************************************/
1502 void (*cb)(int revents, void *arg);
1507 once_cb (EV_P_ struct ev_once *once, int revents)
1509 void (*cb)(int revents, void *arg) = once->cb;
1510 void *arg = once->arg;
1512 ev_io_stop (EV_A_ &once->io);
1513 ev_timer_stop (EV_A_ &once->to);
1520 once_cb_io (EV_P_ struct ev_io *w, int revents)
1522 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1526 once_cb_to (EV_P_ struct ev_timer *w, int revents)
1528 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1532 ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1534 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1537 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1543 ev_watcher_init (&once->io, once_cb_io);
1546 ev_io_set (&once->io, fd, events);
1547 ev_io_start (EV_A_ &once->io);
1550 ev_watcher_init (&once->to, once_cb_to);
1553 ev_timer_set (&once->to, timeout, 0.);
1554 ev_timer_start (EV_A_ &once->to);