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.
46 #include <sys/types.h>
48 # include <sys/wait.h>
55 #ifndef EV_USE_MONOTONIC
56 # define EV_USE_MONOTONIC 1
60 # define EV_USE_SELECT 1
64 # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */
68 # define EV_USE_EPOLL 0
72 # define EV_USE_KQUEUE 0
75 #ifndef EV_USE_REALTIME
76 # define EV_USE_REALTIME 1
81 #ifndef CLOCK_MONOTONIC
82 # undef EV_USE_MONOTONIC
83 # define EV_USE_MONOTONIC 0
86 #ifndef CLOCK_REALTIME
87 # undef EV_USE_REALTIME
88 # define EV_USE_REALTIME 0
93 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
94 #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95 #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96 /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
101 # define expect(expr,value) __builtin_expect ((expr),(value))
102 # define inline inline
104 # define expect(expr,value) (expr)
105 # define inline static
108 #define expect_false(expr) expect ((expr) != 0, 0)
109 #define expect_true(expr) expect ((expr) != 0, 1)
111 #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112 #define ABSPRI(w) ((w)->priority - EV_MINPRI)
114 typedef struct ev_watcher *W;
115 typedef struct ev_watcher_list *WL;
116 typedef struct ev_watcher_time *WT;
118 static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
120 /*****************************************************************************/
124 struct ev_watcher_list *head;
125 unsigned char events;
135 #ifdef EV_MULTIPLICITY
139 # define VAR(name,decl) decl;
140 # include "ev_vars.h"
143 # include "ev_wrap.h"
147 # define VAR(name,decl) static decl;
148 # include "ev_vars.h"
153 /*****************************************************************************/
160 clock_gettime (CLOCK_REALTIME, &ts);
161 return ts.tv_sec + ts.tv_nsec * 1e-9;
164 gettimeofday (&tv, 0);
165 return tv.tv_sec + tv.tv_usec * 1e-6;
173 if (expect_true (have_monotonic))
176 clock_gettime (CLOCK_MONOTONIC, &ts);
177 return ts.tv_sec + ts.tv_nsec * 1e-9;
190 #define array_roundsize(base,n) ((n) | 4 & ~3)
192 #define array_needsize(base,cur,cnt,init) \
193 if (expect_false ((cnt) > cur)) \
198 newcnt = array_roundsize (base, newcnt << 1); \
200 while ((cnt) > newcnt); \
202 base = realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \
207 /*****************************************************************************/
210 anfds_init (ANFD *base, int count)
215 base->events = EV_NONE;
223 event (EV_P_ W w, int events)
227 pendings [ABSPRI (w)][w->pending - 1].events |= events;
231 w->pending = ++pendingcnt [ABSPRI (w)];
232 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
233 pendings [ABSPRI (w)][w->pending - 1].w = w;
234 pendings [ABSPRI (w)][w->pending - 1].events = events;
238 queue_events (EV_P_ W *events, int eventcnt, int type)
242 for (i = 0; i < eventcnt; ++i)
243 event (EV_A_ events [i], type);
247 fd_event (EV_P_ int fd, int events)
249 ANFD *anfd = anfds + fd;
252 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
254 int ev = w->events & events;
257 event (EV_A_ (W)w, ev);
261 /*****************************************************************************/
268 for (i = 0; i < fdchangecnt; ++i)
270 int fd = fdchanges [i];
271 ANFD *anfd = anfds + fd;
276 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
281 if (anfd->events != events)
283 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events;
292 fd_change (EV_P_ int fd)
294 if (anfds [fd].reify || fdchangecnt < 0)
297 anfds [fd].reify = 1;
300 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
301 fdchanges [fdchangecnt - 1] = fd;
305 fd_kill (EV_P_ int fd)
309 while ((w = (struct ev_io *)anfds [fd].head))
311 ev_io_stop (EV_A_ w);
312 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
316 /* called on EBADF to verify fds */
322 for (fd = 0; fd < anfdmax; ++fd)
323 if (anfds [fd].events)
324 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
328 /* called on ENOMEM in select/poll to kill some fds and retry */
335 if (anfds [fd].events)
343 /*****************************************************************************/
346 upheap (WT *heap, int k)
350 while (k && heap [k >> 1]->at > w->at)
352 heap [k] = heap [k >> 1];
353 heap [k]->active = k + 1;
358 heap [k]->active = k + 1;
363 downheap (WT *heap, int N, int k)
371 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
374 if (w->at <= heap [j]->at)
378 heap [k]->active = k + 1;
383 heap [k]->active = k + 1;
386 /*****************************************************************************/
390 struct ev_watcher_list *head;
391 sig_atomic_t volatile gotsig;
394 static ANSIG *signals;
395 static int signalmax;
397 static int sigpipe [2];
398 static sig_atomic_t volatile gotsig;
401 signals_init (ANSIG *base, int count)
413 sighandler (int signum)
415 signals [signum - 1].gotsig = 1;
419 int old_errno = errno;
421 write (sigpipe [1], &signum, 1);
427 sigcb (EV_P_ struct ev_io *iow, int revents)
429 struct ev_watcher_list *w;
432 read (sigpipe [0], &revents, 1);
435 for (signum = signalmax; signum--; )
436 if (signals [signum].gotsig)
438 signals [signum].gotsig = 0;
440 for (w = signals [signum].head; w; w = w->next)
441 event (EV_A_ (W)w, EV_SIGNAL);
449 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
450 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
452 /* rather than sort out wether we really need nb, set it */
453 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
454 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
457 ev_io_set (&sigev, sigpipe [0], EV_READ);
458 ev_io_start (EV_A_ &sigev);
459 ev_unref (EV_A); /* child watcher should not keep loop alive */
462 /*****************************************************************************/
467 # define WCONTINUED 0
471 child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
475 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
476 if (w->pid == pid || !w->pid)
478 w->priority = sw->priority; /* need to do it *now* */
481 event (EV_A_ (W)w, EV_CHILD);
486 childcb (EV_P_ struct ev_signal *sw, int revents)
490 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
492 /* make sure we are called again until all childs have been reaped */
493 event (EV_A_ (W)sw, EV_SIGNAL);
495 child_reap (EV_A_ sw, pid, pid, status);
496 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
502 /*****************************************************************************/
505 # include "ev_kqueue.c"
508 # include "ev_epoll.c"
511 # include "ev_poll.c"
514 # include "ev_select.c"
518 ev_version_major (void)
520 return EV_VERSION_MAJOR;
524 ev_version_minor (void)
526 return EV_VERSION_MINOR;
529 /* return true if we are running with elevated privileges and should ignore env variables */
536 return getuid () != geteuid ()
537 || getgid () != getegid ();
548 loop_init (EV_P_ int methods)
555 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
561 mn_now = get_clock ();
563 rtmn_diff = rt_now - mn_now;
568 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS"))
570 methods = atoi (getenv ("LIBmethodS"));
572 methods = EVMETHOD_ANY;
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
590 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI);
595 ev_signal_init (&childev, childcb, SIGCHLD);
596 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */
606 #ifdef EV_MULTIPLICITY
609 ev_loop_new (int methods)
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
613 loop_init (EV_A_ methods);
619 ev_loop_delete (EV_P)
628 ev_init (int methods)
635 /*****************************************************************************/
638 ev_fork_prepare (void)
644 ev_fork_parent (void)
655 if (method == EVMETHOD_EPOLL)
656 epoll_postfork_child (EV_A);
659 ev_io_stop (EV_A_ &sigev);
667 /*****************************************************************************/
674 for (pri = NUMPRI; pri--; )
675 while (pendingcnt [pri])
677 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
682 p->w->cb (EV_A_ p->w, p->events);
690 while (timercnt && timers [0]->at <= mn_now)
692 struct ev_timer *w = timers [0];
694 /* first reschedule or stop timer */
697 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
698 w->at = mn_now + w->repeat;
699 downheap ((WT *)timers, timercnt, 0);
702 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
704 event (EV_A_ (W)w, EV_TIMEOUT);
709 periodics_reify (EV_P)
711 while (periodiccnt && periodics [0]->at <= rt_now)
713 struct ev_periodic *w = periodics [0];
715 /* first reschedule or stop timer */
718 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
719 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));
720 downheap ((WT *)periodics, periodiccnt, 0);
723 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
725 event (EV_A_ (W)w, EV_PERIODIC);
730 periodics_reschedule (EV_P)
734 /* adjust periodics after time jump */
735 for (i = 0; i < periodiccnt; ++i)
737 struct ev_periodic *w = periodics [i];
741 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval;
743 if (fabs (diff) >= 1e-4)
745 ev_periodic_stop (EV_A_ w);
746 ev_periodic_start (EV_A_ w);
748 i = 0; /* restart loop, inefficient, but time jumps should be rare */
755 time_update_monotonic (EV_P)
757 mn_now = get_clock ();
759 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
761 rt_now = rtmn_diff + mn_now;
778 if (expect_true (have_monotonic))
780 if (time_update_monotonic (EV_A))
782 ev_tstamp odiff = rtmn_diff;
784 for (i = 4; --i; ) /* loop a few times, before making important decisions */
786 rtmn_diff = rt_now - mn_now;
788 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
789 return; /* all is well */
792 mn_now = get_clock ();
796 periodics_reschedule (EV_A);
797 /* no timer adjustment, as the monotonic clock doesn't jump */
798 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
806 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
808 periodics_reschedule (EV_A);
810 /* adjust timers. this is easy, as the offset is the same for all */
811 for (i = 0; i < timercnt; ++i)
812 timers [i]->at += rt_now - mn_now;
831 static int loop_done;
834 ev_loop (EV_P_ int flags)
837 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
841 /* queue check watchers (and execute them) */
842 if (expect_false (preparecnt))
844 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
848 /* update fd-related kernel structures */
851 /* calculate blocking time */
853 /* we only need this for !monotonic clockor timers, but as we basically
854 always have timers, we just calculate it always */
856 if (expect_true (have_monotonic))
857 time_update_monotonic (EV_A);
865 if (flags & EVLOOP_NONBLOCK || idlecnt)
869 block = MAX_BLOCKTIME;
873 ev_tstamp to = timers [0]->at - mn_now + method_fudge;
874 if (block > to) block = to;
879 ev_tstamp to = periodics [0]->at - rt_now + method_fudge;
880 if (block > to) block = to;
883 if (block < 0.) block = 0.;
886 method_poll (EV_A_ block);
888 /* update rt_now, do magic */
891 /* queue pending timers and reschedule them */
892 timers_reify (EV_A); /* relative timers called last */
893 periodics_reify (EV_A); /* absolute timers called first */
895 /* queue idle watchers unless io or timers are pending */
897 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
899 /* queue check watchers, to be executed first */
901 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
905 while (activecnt && !loop_done);
912 ev_unloop (EV_P_ int how)
917 /*****************************************************************************/
920 wlist_add (WL *head, WL elem)
927 wlist_del (WL *head, WL elem)
937 head = &(*head)->next;
942 ev_clear_pending (EV_P_ W w)
946 pendings [ABSPRI (w)][w->pending - 1].w = 0;
952 ev_start (EV_P_ W w, int active)
954 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
955 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
968 /*****************************************************************************/
971 ev_io_start (EV_P_ struct ev_io *w)
975 if (ev_is_active (w))
978 assert (("ev_io_start called with negative fd", fd >= 0));
980 ev_start (EV_A_ (W)w, 1);
981 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
982 wlist_add ((WL *)&anfds[fd].head, (WL)w);
984 fd_change (EV_A_ fd);
988 ev_io_stop (EV_P_ struct ev_io *w)
990 ev_clear_pending (EV_A_ (W)w);
991 if (!ev_is_active (w))
994 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
995 ev_stop (EV_A_ (W)w);
997 fd_change (EV_A_ w->fd);
1001 ev_timer_start (EV_P_ struct ev_timer *w)
1003 if (ev_is_active (w))
1008 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1010 ev_start (EV_A_ (W)w, ++timercnt);
1011 array_needsize (timers, timermax, timercnt, );
1012 timers [timercnt - 1] = w;
1013 upheap ((WT *)timers, timercnt - 1);
1017 ev_timer_stop (EV_P_ struct ev_timer *w)
1019 ev_clear_pending (EV_A_ (W)w);
1020 if (!ev_is_active (w))
1023 if (w->active < timercnt--)
1025 timers [w->active - 1] = timers [timercnt];
1026 downheap ((WT *)timers, timercnt, w->active - 1);
1031 ev_stop (EV_A_ (W)w);
1035 ev_timer_again (EV_P_ struct ev_timer *w)
1037 if (ev_is_active (w))
1041 w->at = mn_now + w->repeat;
1042 downheap ((WT *)timers, timercnt, w->active - 1);
1045 ev_timer_stop (EV_A_ w);
1048 ev_timer_start (EV_A_ w);
1052 ev_periodic_start (EV_P_ struct ev_periodic *w)
1054 if (ev_is_active (w))
1057 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1059 /* this formula differs from the one in periodic_reify because we do not always round up */
1061 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval;
1063 ev_start (EV_A_ (W)w, ++periodiccnt);
1064 array_needsize (periodics, periodicmax, periodiccnt, );
1065 periodics [periodiccnt - 1] = w;
1066 upheap ((WT *)periodics, periodiccnt - 1);
1070 ev_periodic_stop (EV_P_ struct ev_periodic *w)
1072 ev_clear_pending (EV_A_ (W)w);
1073 if (!ev_is_active (w))
1076 if (w->active < periodiccnt--)
1078 periodics [w->active - 1] = periodics [periodiccnt];
1079 downheap ((WT *)periodics, periodiccnt, w->active - 1);
1082 ev_stop (EV_A_ (W)w);
1086 # define SA_RESTART 0
1090 ev_signal_start (EV_P_ struct ev_signal *w)
1092 if (ev_is_active (w))
1095 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1097 ev_start (EV_A_ (W)w, 1);
1098 array_needsize (signals, signalmax, w->signum, signals_init);
1099 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1103 struct sigaction sa;
1104 sa.sa_handler = sighandler;
1105 sigfillset (&sa.sa_mask);
1106 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1107 sigaction (w->signum, &sa, 0);
1112 ev_signal_stop (EV_P_ struct ev_signal *w)
1114 ev_clear_pending (EV_A_ (W)w);
1115 if (!ev_is_active (w))
1118 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1119 ev_stop (EV_A_ (W)w);
1121 if (!signals [w->signum - 1].head)
1122 signal (w->signum, SIG_DFL);
1126 ev_idle_start (EV_P_ struct ev_idle *w)
1128 if (ev_is_active (w))
1131 ev_start (EV_A_ (W)w, ++idlecnt);
1132 array_needsize (idles, idlemax, idlecnt, );
1133 idles [idlecnt - 1] = w;
1137 ev_idle_stop (EV_P_ struct ev_idle *w)
1139 ev_clear_pending (EV_A_ (W)w);
1140 if (ev_is_active (w))
1143 idles [w->active - 1] = idles [--idlecnt];
1144 ev_stop (EV_A_ (W)w);
1148 ev_prepare_start (EV_P_ struct ev_prepare *w)
1150 if (ev_is_active (w))
1153 ev_start (EV_A_ (W)w, ++preparecnt);
1154 array_needsize (prepares, preparemax, preparecnt, );
1155 prepares [preparecnt - 1] = w;
1159 ev_prepare_stop (EV_P_ struct ev_prepare *w)
1161 ev_clear_pending (EV_A_ (W)w);
1162 if (ev_is_active (w))
1165 prepares [w->active - 1] = prepares [--preparecnt];
1166 ev_stop (EV_A_ (W)w);
1170 ev_check_start (EV_P_ struct ev_check *w)
1172 if (ev_is_active (w))
1175 ev_start (EV_A_ (W)w, ++checkcnt);
1176 array_needsize (checks, checkmax, checkcnt, );
1177 checks [checkcnt - 1] = w;
1181 ev_check_stop (EV_P_ struct ev_check *w)
1183 ev_clear_pending (EV_A_ (W)w);
1184 if (ev_is_active (w))
1187 checks [w->active - 1] = checks [--checkcnt];
1188 ev_stop (EV_A_ (W)w);
1192 ev_child_start (EV_P_ struct ev_child *w)
1194 if (ev_is_active (w))
1197 ev_start (EV_A_ (W)w, 1);
1198 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1202 ev_child_stop (EV_P_ struct ev_child *w)
1204 ev_clear_pending (EV_A_ (W)w);
1205 if (ev_is_active (w))
1208 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1209 ev_stop (EV_A_ (W)w);
1212 /*****************************************************************************/
1218 void (*cb)(int revents, void *arg);
1223 once_cb (EV_P_ struct ev_once *once, int revents)
1225 void (*cb)(int revents, void *arg) = once->cb;
1226 void *arg = once->arg;
1228 ev_io_stop (EV_A_ &once->io);
1229 ev_timer_stop (EV_A_ &once->to);
1236 once_cb_io (EV_P_ struct ev_io *w, int revents)
1238 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1242 once_cb_to (EV_P_ struct ev_timer *w, int revents)
1244 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1248 ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1250 struct ev_once *once = malloc (sizeof (struct ev_once));
1253 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1259 ev_watcher_init (&once->io, once_cb_io);
1262 ev_io_set (&once->io, fd, events);
1263 ev_io_start (EV_A_ &once->io);
1266 ev_watcher_init (&once->to, once_cb_to);
1269 ev_timer_set (&once->to, timeout, 0.);
1270 ev_timer_start (EV_A_ &once->to);
1275 /*****************************************************************************/
1282 sin_cb (struct ev_io *w, int revents)
1284 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1288 ocb (struct ev_timer *w, int revents)
1290 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1296 scb (struct ev_signal *w, int revents)
1298 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1304 gcb (struct ev_signal *w, int revents)
1306 fprintf (stderr, "generic %x\n", revents);
1314 ev_io_init (&wio, sin_cb, 0, EV_READ);
1317 struct ev_timer t[10000];
1321 for (i = 0; i < 10000; ++i)
1323 struct ev_timer *w = t + i;
1324 ev_watcher_init (w, ocb, i);
1325 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1327 if (drand48 () < 0.5)
1333 ev_timer_init (&t1, ocb, 5, 10);
1334 ev_timer_start (&t1);
1336 struct ev_signal sig;
1337 ev_signal_init (&sig, scb, SIGQUIT);
1338 ev_signal_start (&sig);
1341 ev_check_init (&cw, gcb);
1342 ev_check_start (&cw);
1345 ev_idle_init (&iw, gcb);
1346 ev_idle_start (&iw);