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 while (pendingcnt [pri])
910 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
915 p->w->cb (EV_A_ p->w, p->events);
923 while (timercnt && ((WT)timers [0])->at <= mn_now)
925 struct ev_timer *w = timers [0];
927 assert (("inactive timer on timer heap detected", ev_is_active (w)));
929 /* first reschedule or stop timer */
932 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
933 ((WT)w)->at = mn_now + w->repeat;
934 downheap ((WT *)timers, timercnt, 0);
937 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
939 event (EV_A_ (W)w, EV_TIMEOUT);
944 periodics_reify (EV_P)
946 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
948 struct ev_periodic *w = periodics [0];
950 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
952 /* first reschedule or stop timer */
955 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
956 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
957 downheap ((WT *)periodics, periodiccnt, 0);
960 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
962 event (EV_A_ (W)w, EV_PERIODIC);
967 periodics_reschedule (EV_P)
971 /* adjust periodics after time jump */
972 for (i = 0; i < periodiccnt; ++i)
974 struct ev_periodic *w = periodics [i];
978 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
980 if (fabs (diff) >= 1e-4)
982 ev_periodic_stop (EV_A_ w);
983 ev_periodic_start (EV_A_ w);
985 i = 0; /* restart loop, inefficient, but time jumps should be rare */
992 time_update_monotonic (EV_P)
994 mn_now = get_clock ();
996 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
998 rt_now = rtmn_diff + mn_now;
1004 rt_now = ev_time ();
1014 #if EV_USE_MONOTONIC
1015 if (expect_true (have_monotonic))
1017 if (time_update_monotonic (EV_A))
1019 ev_tstamp odiff = rtmn_diff;
1021 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1023 rtmn_diff = rt_now - mn_now;
1025 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1026 return; /* all is well */
1028 rt_now = ev_time ();
1029 mn_now = get_clock ();
1033 periodics_reschedule (EV_A);
1034 /* no timer adjustment, as the monotonic clock doesn't jump */
1035 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1041 rt_now = ev_time ();
1043 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1045 periodics_reschedule (EV_A);
1047 /* adjust timers. this is easy, as the offset is the same for all */
1048 for (i = 0; i < timercnt; ++i)
1049 ((WT)timers [i])->at += rt_now - mn_now;
1068 static int loop_done;
1071 ev_loop (EV_P_ int flags)
1074 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1078 /* queue check watchers (and execute them) */
1079 if (expect_false (preparecnt))
1081 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1082 call_pending (EV_A);
1085 /* we might have forked, so reify kernel state if necessary */
1086 if (expect_false (postfork))
1089 /* update fd-related kernel structures */
1092 /* calculate blocking time */
1094 /* we only need this for !monotonic clockor timers, but as we basically
1095 always have timers, we just calculate it always */
1096 #if EV_USE_MONOTONIC
1097 if (expect_true (have_monotonic))
1098 time_update_monotonic (EV_A);
1102 rt_now = ev_time ();
1106 if (flags & EVLOOP_NONBLOCK || idlecnt)
1110 block = MAX_BLOCKTIME;
1114 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1115 if (block > to) block = to;
1120 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
1121 if (block > to) block = to;
1124 if (block < 0.) block = 0.;
1127 method_poll (EV_A_ block);
1129 /* update rt_now, do magic */
1132 /* queue pending timers and reschedule them */
1133 timers_reify (EV_A); /* relative timers called last */
1134 periodics_reify (EV_A); /* absolute timers called first */
1136 /* queue idle watchers unless io or timers are pending */
1138 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1140 /* queue check watchers, to be executed first */
1142 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1144 call_pending (EV_A);
1146 while (activecnt && !loop_done);
1153 ev_unloop (EV_P_ int how)
1158 /*****************************************************************************/
1161 wlist_add (WL *head, WL elem)
1168 wlist_del (WL *head, WL elem)
1178 head = &(*head)->next;
1183 ev_clear_pending (EV_P_ W w)
1187 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1193 ev_start (EV_P_ W w, int active)
1195 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1196 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1209 /*****************************************************************************/
1212 ev_io_start (EV_P_ struct ev_io *w)
1216 if (ev_is_active (w))
1219 assert (("ev_io_start called with negative fd", fd >= 0));
1221 ev_start (EV_A_ (W)w, 1);
1222 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1223 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1225 fd_change (EV_A_ fd);
1229 ev_io_stop (EV_P_ struct ev_io *w)
1231 ev_clear_pending (EV_A_ (W)w);
1232 if (!ev_is_active (w))
1235 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1236 ev_stop (EV_A_ (W)w);
1238 fd_change (EV_A_ w->fd);
1242 ev_timer_start (EV_P_ struct ev_timer *w)
1244 if (ev_is_active (w))
1247 ((WT)w)->at += mn_now;
1249 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1251 ev_start (EV_A_ (W)w, ++timercnt);
1252 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1253 timers [timercnt - 1] = w;
1254 upheap ((WT *)timers, timercnt - 1);
1256 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1260 ev_timer_stop (EV_P_ struct ev_timer *w)
1262 ev_clear_pending (EV_A_ (W)w);
1263 if (!ev_is_active (w))
1266 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1268 if (((W)w)->active < timercnt--)
1270 timers [((W)w)->active - 1] = timers [timercnt];
1271 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1274 ((WT)w)->at = w->repeat;
1276 ev_stop (EV_A_ (W)w);
1280 ev_timer_again (EV_P_ struct ev_timer *w)
1282 if (ev_is_active (w))
1286 ((WT)w)->at = mn_now + w->repeat;
1287 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1290 ev_timer_stop (EV_A_ w);
1293 ev_timer_start (EV_A_ w);
1297 ev_periodic_start (EV_P_ struct ev_periodic *w)
1299 if (ev_is_active (w))
1302 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1304 /* this formula differs from the one in periodic_reify because we do not always round up */
1306 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1308 ev_start (EV_A_ (W)w, ++periodiccnt);
1309 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1310 periodics [periodiccnt - 1] = w;
1311 upheap ((WT *)periodics, periodiccnt - 1);
1313 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1317 ev_periodic_stop (EV_P_ struct ev_periodic *w)
1319 ev_clear_pending (EV_A_ (W)w);
1320 if (!ev_is_active (w))
1323 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1325 if (((W)w)->active < periodiccnt--)
1327 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1328 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1331 ev_stop (EV_A_ (W)w);
1335 ev_idle_start (EV_P_ struct ev_idle *w)
1337 if (ev_is_active (w))
1340 ev_start (EV_A_ (W)w, ++idlecnt);
1341 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1342 idles [idlecnt - 1] = w;
1346 ev_idle_stop (EV_P_ struct ev_idle *w)
1348 ev_clear_pending (EV_A_ (W)w);
1349 if (ev_is_active (w))
1352 idles [((W)w)->active - 1] = idles [--idlecnt];
1353 ev_stop (EV_A_ (W)w);
1357 ev_prepare_start (EV_P_ struct ev_prepare *w)
1359 if (ev_is_active (w))
1362 ev_start (EV_A_ (W)w, ++preparecnt);
1363 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1364 prepares [preparecnt - 1] = w;
1368 ev_prepare_stop (EV_P_ struct ev_prepare *w)
1370 ev_clear_pending (EV_A_ (W)w);
1371 if (ev_is_active (w))
1374 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1375 ev_stop (EV_A_ (W)w);
1379 ev_check_start (EV_P_ struct ev_check *w)
1381 if (ev_is_active (w))
1384 ev_start (EV_A_ (W)w, ++checkcnt);
1385 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1386 checks [checkcnt - 1] = w;
1390 ev_check_stop (EV_P_ struct ev_check *w)
1392 ev_clear_pending (EV_A_ (W)w);
1393 if (ev_is_active (w))
1396 checks [((W)w)->active - 1] = checks [--checkcnt];
1397 ev_stop (EV_A_ (W)w);
1401 # define SA_RESTART 0
1405 ev_signal_start (EV_P_ struct ev_signal *w)
1408 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1410 if (ev_is_active (w))
1413 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1415 ev_start (EV_A_ (W)w, 1);
1416 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1417 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1422 signal (w->signum, sighandler);
1424 struct sigaction sa;
1425 sa.sa_handler = sighandler;
1426 sigfillset (&sa.sa_mask);
1427 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1428 sigaction (w->signum, &sa, 0);
1434 ev_signal_stop (EV_P_ struct ev_signal *w)
1436 ev_clear_pending (EV_A_ (W)w);
1437 if (!ev_is_active (w))
1440 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1441 ev_stop (EV_A_ (W)w);
1443 if (!signals [w->signum - 1].head)
1444 signal (w->signum, SIG_DFL);
1448 ev_child_start (EV_P_ struct ev_child *w)
1451 assert (("child watchers are only supported in the default loop", loop == default_loop));
1453 if (ev_is_active (w))
1456 ev_start (EV_A_ (W)w, 1);
1457 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1461 ev_child_stop (EV_P_ struct ev_child *w)
1463 ev_clear_pending (EV_A_ (W)w);
1464 if (ev_is_active (w))
1467 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1468 ev_stop (EV_A_ (W)w);
1471 /*****************************************************************************/
1477 void (*cb)(int revents, void *arg);
1482 once_cb (EV_P_ struct ev_once *once, int revents)
1484 void (*cb)(int revents, void *arg) = once->cb;
1485 void *arg = once->arg;
1487 ev_io_stop (EV_A_ &once->io);
1488 ev_timer_stop (EV_A_ &once->to);
1495 once_cb_io (EV_P_ struct ev_io *w, int revents)
1497 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1501 once_cb_to (EV_P_ struct ev_timer *w, int revents)
1503 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1507 ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1509 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1512 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1518 ev_watcher_init (&once->io, once_cb_io);
1521 ev_io_set (&once->io, fd, events);
1522 ev_io_start (EV_A_ &once->io);
1525 ev_watcher_init (&once->to, once_cb_to);
1528 ev_timer_set (&once->to, timeout, 0.);
1529 ev_timer_start (EV_A_ &once->to);