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? */
150 /*****************************************************************************/
154 struct ev_watcher_list *head;
155 unsigned char events;
169 # define VAR(name,decl) decl;
170 # include "ev_vars.h"
173 # include "ev_wrap.h"
177 # define VAR(name,decl) static decl;
178 # include "ev_vars.h"
183 /*****************************************************************************/
190 clock_gettime (CLOCK_REALTIME, &ts);
191 return ts.tv_sec + ts.tv_nsec * 1e-9;
194 gettimeofday (&tv, 0);
195 return tv.tv_sec + tv.tv_usec * 1e-6;
203 if (expect_true (have_monotonic))
206 clock_gettime (CLOCK_MONOTONIC, &ts);
207 return ts.tv_sec + ts.tv_nsec * 1e-9;
220 #define array_roundsize(base,n) ((n) | 4 & ~3)
222 #define array_needsize(base,cur,cnt,init) \
223 if (expect_false ((cnt) > cur)) \
228 newcnt = array_roundsize (base, newcnt << 1); \
230 while ((cnt) > newcnt); \
232 base = realloc (base, sizeof (*base) * (newcnt)); \
233 init (base + cur, newcnt - cur); \
237 /*****************************************************************************/
240 anfds_init (ANFD *base, int count)
245 base->events = EV_NONE;
253 event (EV_P_ W w, int events)
257 pendings [ABSPRI (w)][w->pending - 1].events |= events;
261 w->pending = ++pendingcnt [ABSPRI (w)];
262 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
263 pendings [ABSPRI (w)][w->pending - 1].w = w;
264 pendings [ABSPRI (w)][w->pending - 1].events = events;
268 queue_events (EV_P_ W *events, int eventcnt, int type)
272 for (i = 0; i < eventcnt; ++i)
273 event (EV_A_ events [i], type);
277 fd_event (EV_P_ int fd, int events)
279 ANFD *anfd = anfds + fd;
282 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
284 int ev = w->events & events;
287 event (EV_A_ (W)w, ev);
291 /*****************************************************************************/
298 for (i = 0; i < fdchangecnt; ++i)
300 int fd = fdchanges [i];
301 ANFD *anfd = anfds + fd;
306 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
311 method_modify (EV_A_ fd, anfd->events, events);
312 anfd->events = events;
319 fd_change (EV_P_ int fd)
321 if (anfds [fd].reify || fdchangecnt < 0)
324 anfds [fd].reify = 1;
327 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
328 fdchanges [fdchangecnt - 1] = fd;
332 fd_kill (EV_P_ int fd)
336 while ((w = (struct ev_io *)anfds [fd].head))
338 ev_io_stop (EV_A_ w);
339 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
343 /* called on EBADF to verify fds */
349 for (fd = 0; fd < anfdmax; ++fd)
350 if (anfds [fd].events)
351 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
355 /* called on ENOMEM in select/poll to kill some fds and retry */
361 for (fd = anfdmax; fd--; )
362 if (anfds [fd].events)
370 /* susually called after fork if method needs to re-arm all fds from scratch */
376 /* this should be highly optimised to not do anything but set a flag */
377 for (fd = 0; fd < anfdmax; ++fd)
378 if (anfds [fd].events)
380 anfds [fd].events = 0;
381 fd_change (EV_A_ fd);
385 /*****************************************************************************/
388 upheap (WT *heap, int k)
392 while (k && heap [k >> 1]->at > w->at)
394 heap [k] = heap [k >> 1];
395 ((W)heap [k])->active = k + 1;
400 ((W)heap [k])->active = k + 1;
405 downheap (WT *heap, int N, int k)
413 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
416 if (w->at <= heap [j]->at)
420 ((W)heap [k])->active = k + 1;
425 ((W)heap [k])->active = k + 1;
428 /*****************************************************************************/
432 struct ev_watcher_list *head;
433 sig_atomic_t volatile gotsig;
436 static ANSIG *signals;
437 static int signalmax;
439 static int sigpipe [2];
440 static sig_atomic_t volatile gotsig;
441 static struct ev_io sigev;
444 signals_init (ANSIG *base, int count)
456 sighandler (int signum)
458 signals [signum - 1].gotsig = 1;
462 int old_errno = errno;
464 write (sigpipe [1], &signum, 1);
470 sigcb (EV_P_ struct ev_io *iow, int revents)
472 struct ev_watcher_list *w;
475 read (sigpipe [0], &revents, 1);
478 for (signum = signalmax; signum--; )
479 if (signals [signum].gotsig)
481 signals [signum].gotsig = 0;
483 for (w = signals [signum].head; w; w = w->next)
484 event (EV_A_ (W)w, EV_SIGNAL);
492 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
493 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
495 /* rather than sort out wether we really need nb, set it */
496 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
497 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
500 ev_io_set (&sigev, sigpipe [0], EV_READ);
501 ev_io_start (EV_A_ &sigev);
502 ev_unref (EV_A); /* child watcher should not keep loop alive */
505 /*****************************************************************************/
509 static struct ev_child *childs [PID_HASHSIZE];
510 static struct ev_signal childev;
513 # define WCONTINUED 0
517 child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
521 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
522 if (w->pid == pid || !w->pid)
524 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
527 event (EV_A_ (W)w, EV_CHILD);
532 childcb (EV_P_ struct ev_signal *sw, int revents)
536 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
538 /* make sure we are called again until all childs have been reaped */
539 event (EV_A_ (W)sw, EV_SIGNAL);
541 child_reap (EV_A_ sw, pid, pid, status);
542 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
548 /*****************************************************************************/
551 # include "ev_kqueue.c"
554 # include "ev_epoll.c"
557 # include "ev_poll.c"
560 # include "ev_select.c"
564 ev_version_major (void)
566 return EV_VERSION_MAJOR;
570 ev_version_minor (void)
572 return EV_VERSION_MINOR;
575 /* return true if we are running with elevated privileges and should ignore env variables */
582 return getuid () != geteuid ()
583 || getgid () != getegid ();
594 loop_init (EV_P_ int methods)
601 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
607 mn_now = get_clock ();
609 rtmn_diff = rt_now - mn_now;
611 if (methods == EVMETHOD_AUTO)
612 if (!enable_secure () && getenv ("LIBEV_METHODS"))
613 methods = atoi (getenv ("LIBEV_METHODS"));
615 methods = EVMETHOD_ANY;
619 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
640 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
643 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
646 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
649 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
652 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
664 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
667 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
673 ev_loop_new (int methods)
675 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
677 loop_init (EV_A_ methods);
679 if (ev_method (EV_A))
686 ev_loop_destroy (EV_P)
701 struct ev_loop default_loop_struct;
702 static struct ev_loop *default_loop;
706 static int default_loop;
710 ev_default_loop (int methods)
712 if (sigpipe [0] == sigpipe [1])
719 struct ev_loop *loop = default_loop = &default_loop_struct;
724 loop_init (EV_A_ methods);
726 if (ev_method (EV_A))
728 ev_watcher_init (&sigev, sigcb);
729 ev_set_priority (&sigev, EV_MAXPRI);
733 ev_signal_init (&childev, childcb, SIGCHLD);
734 ev_set_priority (&childev, EV_MAXPRI);
735 ev_signal_start (EV_A_ &childev);
736 ev_unref (EV_A); /* child watcher should not keep loop alive */
747 ev_default_destroy (void)
750 struct ev_loop *loop = default_loop;
753 ev_ref (EV_A); /* child watcher */
754 ev_signal_stop (EV_A_ &childev);
756 ev_ref (EV_A); /* signal watcher */
757 ev_io_stop (EV_A_ &sigev);
759 close (sigpipe [0]); sigpipe [0] = 0;
760 close (sigpipe [1]); sigpipe [1] = 0;
766 ev_default_fork (void)
769 struct ev_loop *loop = default_loop;
774 ev_io_stop (EV_A_ &sigev);
779 ev_ref (EV_A); /* signal watcher */
783 /*****************************************************************************/
790 for (pri = NUMPRI; pri--; )
791 while (pendingcnt [pri])
793 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events);
807 while (timercnt && ((WT)timers [0])->at <= mn_now)
809 struct ev_timer *w = timers [0];
811 assert (("inactive timer on timer heap detected", ev_is_active (w)));
813 /* first reschedule or stop timer */
816 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
817 ((WT)w)->at = mn_now + w->repeat;
818 downheap ((WT *)timers, timercnt, 0);
821 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
823 event (EV_A_ (W)w, EV_TIMEOUT);
828 periodics_reify (EV_P)
830 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
832 struct ev_periodic *w = periodics [0];
834 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
836 /* first reschedule or stop timer */
839 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
840 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
841 downheap ((WT *)periodics, periodiccnt, 0);
844 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
846 event (EV_A_ (W)w, EV_PERIODIC);
851 periodics_reschedule (EV_P)
855 /* adjust periodics after time jump */
856 for (i = 0; i < periodiccnt; ++i)
858 struct ev_periodic *w = periodics [i];
862 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
864 if (fabs (diff) >= 1e-4)
866 ev_periodic_stop (EV_A_ w);
867 ev_periodic_start (EV_A_ w);
869 i = 0; /* restart loop, inefficient, but time jumps should be rare */
876 time_update_monotonic (EV_P)
878 mn_now = get_clock ();
880 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
882 rt_now = rtmn_diff + mn_now;
899 if (expect_true (have_monotonic))
901 if (time_update_monotonic (EV_A))
903 ev_tstamp odiff = rtmn_diff;
905 for (i = 4; --i; ) /* loop a few times, before making important decisions */
907 rtmn_diff = rt_now - mn_now;
909 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
910 return; /* all is well */
913 mn_now = get_clock ();
917 periodics_reschedule (EV_A);
918 /* no timer adjustment, as the monotonic clock doesn't jump */
919 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
927 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
929 periodics_reschedule (EV_A);
931 /* adjust timers. this is easy, as the offset is the same for all */
932 for (i = 0; i < timercnt; ++i)
933 ((WT)timers [i])->at += rt_now - mn_now;
952 static int loop_done;
955 ev_loop (EV_P_ int flags)
958 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
962 /* queue check watchers (and execute them) */
963 if (expect_false (preparecnt))
965 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
969 /* update fd-related kernel structures */
972 /* calculate blocking time */
974 /* we only need this for !monotonic clockor timers, but as we basically
975 always have timers, we just calculate it always */
977 if (expect_true (have_monotonic))
978 time_update_monotonic (EV_A);
986 if (flags & EVLOOP_NONBLOCK || idlecnt)
990 block = MAX_BLOCKTIME;
994 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
995 if (block > to) block = to;
1000 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
1001 if (block > to) block = to;
1004 if (block < 0.) block = 0.;
1007 method_poll (EV_A_ block);
1009 /* update rt_now, do magic */
1012 /* queue pending timers and reschedule them */
1013 timers_reify (EV_A); /* relative timers called last */
1014 periodics_reify (EV_A); /* absolute timers called first */
1016 /* queue idle watchers unless io or timers are pending */
1018 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1020 /* queue check watchers, to be executed first */
1022 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1024 call_pending (EV_A);
1026 while (activecnt && !loop_done);
1033 ev_unloop (EV_P_ int how)
1038 /*****************************************************************************/
1041 wlist_add (WL *head, WL elem)
1048 wlist_del (WL *head, WL elem)
1058 head = &(*head)->next;
1063 ev_clear_pending (EV_P_ W w)
1067 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1073 ev_start (EV_P_ W w, int active)
1075 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1076 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1089 /*****************************************************************************/
1092 ev_io_start (EV_P_ struct ev_io *w)
1096 if (ev_is_active (w))
1099 assert (("ev_io_start called with negative fd", fd >= 0));
1101 ev_start (EV_A_ (W)w, 1);
1102 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
1103 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1105 fd_change (EV_A_ fd);
1109 ev_io_stop (EV_P_ struct ev_io *w)
1111 ev_clear_pending (EV_A_ (W)w);
1112 if (!ev_is_active (w))
1115 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1116 ev_stop (EV_A_ (W)w);
1118 fd_change (EV_A_ w->fd);
1122 ev_timer_start (EV_P_ struct ev_timer *w)
1124 if (ev_is_active (w))
1127 ((WT)w)->at += mn_now;
1129 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1131 ev_start (EV_A_ (W)w, ++timercnt);
1132 array_needsize (timers, timermax, timercnt, );
1133 timers [timercnt - 1] = w;
1134 upheap ((WT *)timers, timercnt - 1);
1136 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1140 ev_timer_stop (EV_P_ struct ev_timer *w)
1142 ev_clear_pending (EV_A_ (W)w);
1143 if (!ev_is_active (w))
1146 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1148 if (((W)w)->active < timercnt--)
1150 timers [((W)w)->active - 1] = timers [timercnt];
1151 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1154 ((WT)w)->at = w->repeat;
1156 ev_stop (EV_A_ (W)w);
1160 ev_timer_again (EV_P_ struct ev_timer *w)
1162 if (ev_is_active (w))
1166 ((WT)w)->at = mn_now + w->repeat;
1167 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1170 ev_timer_stop (EV_A_ w);
1173 ev_timer_start (EV_A_ w);
1177 ev_periodic_start (EV_P_ struct ev_periodic *w)
1179 if (ev_is_active (w))
1182 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1184 /* this formula differs from the one in periodic_reify because we do not always round up */
1186 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1188 ev_start (EV_A_ (W)w, ++periodiccnt);
1189 array_needsize (periodics, periodicmax, periodiccnt, );
1190 periodics [periodiccnt - 1] = w;
1191 upheap ((WT *)periodics, periodiccnt - 1);
1193 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1197 ev_periodic_stop (EV_P_ struct ev_periodic *w)
1199 ev_clear_pending (EV_A_ (W)w);
1200 if (!ev_is_active (w))
1203 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1205 if (((W)w)->active < periodiccnt--)
1207 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1208 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1211 ev_stop (EV_A_ (W)w);
1215 ev_idle_start (EV_P_ struct ev_idle *w)
1217 if (ev_is_active (w))
1220 ev_start (EV_A_ (W)w, ++idlecnt);
1221 array_needsize (idles, idlemax, idlecnt, );
1222 idles [idlecnt - 1] = w;
1226 ev_idle_stop (EV_P_ struct ev_idle *w)
1228 ev_clear_pending (EV_A_ (W)w);
1229 if (ev_is_active (w))
1232 idles [((W)w)->active - 1] = idles [--idlecnt];
1233 ev_stop (EV_A_ (W)w);
1237 ev_prepare_start (EV_P_ struct ev_prepare *w)
1239 if (ev_is_active (w))
1242 ev_start (EV_A_ (W)w, ++preparecnt);
1243 array_needsize (prepares, preparemax, preparecnt, );
1244 prepares [preparecnt - 1] = w;
1248 ev_prepare_stop (EV_P_ struct ev_prepare *w)
1250 ev_clear_pending (EV_A_ (W)w);
1251 if (ev_is_active (w))
1254 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1255 ev_stop (EV_A_ (W)w);
1259 ev_check_start (EV_P_ struct ev_check *w)
1261 if (ev_is_active (w))
1264 ev_start (EV_A_ (W)w, ++checkcnt);
1265 array_needsize (checks, checkmax, checkcnt, );
1266 checks [checkcnt - 1] = w;
1270 ev_check_stop (EV_P_ struct ev_check *w)
1272 ev_clear_pending (EV_A_ (W)w);
1273 if (ev_is_active (w))
1276 checks [((W)w)->active - 1] = checks [--checkcnt];
1277 ev_stop (EV_A_ (W)w);
1281 # define SA_RESTART 0
1285 ev_signal_start (EV_P_ struct ev_signal *w)
1288 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1290 if (ev_is_active (w))
1293 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1295 ev_start (EV_A_ (W)w, 1);
1296 array_needsize (signals, signalmax, w->signum, signals_init);
1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1301 struct sigaction sa;
1302 sa.sa_handler = sighandler;
1303 sigfillset (&sa.sa_mask);
1304 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1305 sigaction (w->signum, &sa, 0);
1310 ev_signal_stop (EV_P_ struct ev_signal *w)
1312 ev_clear_pending (EV_A_ (W)w);
1313 if (!ev_is_active (w))
1316 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1317 ev_stop (EV_A_ (W)w);
1319 if (!signals [w->signum - 1].head)
1320 signal (w->signum, SIG_DFL);
1324 ev_child_start (EV_P_ struct ev_child *w)
1327 assert (("child watchers are only supported in the default loop", loop == default_loop));
1329 if (ev_is_active (w))
1332 ev_start (EV_A_ (W)w, 1);
1333 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1337 ev_child_stop (EV_P_ struct ev_child *w)
1339 ev_clear_pending (EV_A_ (W)w);
1340 if (ev_is_active (w))
1343 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1344 ev_stop (EV_A_ (W)w);
1347 /*****************************************************************************/
1353 void (*cb)(int revents, void *arg);
1358 once_cb (EV_P_ struct ev_once *once, int revents)
1360 void (*cb)(int revents, void *arg) = once->cb;
1361 void *arg = once->arg;
1363 ev_io_stop (EV_A_ &once->io);
1364 ev_timer_stop (EV_A_ &once->to);
1371 once_cb_io (EV_P_ struct ev_io *w, int revents)
1373 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1377 once_cb_to (EV_P_ struct ev_timer *w, int revents)
1379 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1383 ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1385 struct ev_once *once = malloc (sizeof (struct ev_once));
1388 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1394 ev_watcher_init (&once->io, once_cb_io);
1397 ev_io_set (&once->io, fd, events);
1398 ev_io_start (EV_A_ &once->io);
1401 ev_watcher_init (&once->to, once_cb_to);
1404 ev_timer_set (&once->to, timeout, 0.);
1405 ev_timer_start (EV_A_ &once->to);