ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.15
Committed: Wed Oct 31 11:56:34 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +0 -1 lines
Log Message:
rmeove remaining debugging code

File Contents

# User Rev Content
1 root 1.1 #include <math.h>
2     #include <stdlib.h>
3 root 1.7 #include <unistd.h>
4     #include <fcntl.h>
5     #include <signal.h>
6 root 1.1
7     #include <stdio.h>
8    
9 root 1.4 #include <assert.h>
10 root 1.1 #include <errno.h>
11     #include <sys/time.h>
12     #include <time.h>
13    
14 root 1.12 #define HAVE_EPOLL 1
15    
16 root 1.10 #ifndef HAVE_MONOTONIC
17     # ifdef CLOCK_MONOTONIC
18     # define HAVE_MONOTONIC 1
19     # endif
20 root 1.1 #endif
21    
22 root 1.10 #ifndef HAVE_SELECT
23     # define HAVE_SELECT 1
24     #endif
25    
26     #ifndef HAVE_EPOLL
27     # define HAVE_EPOLL 0
28     #endif
29    
30     #ifndef HAVE_REALTIME
31     # define HAVE_REALTIME 1 /* posix requirement, but might be slower */
32     #endif
33 root 1.1
34 root 1.4 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
35 root 1.1 #define MAX_BLOCKTIME 60.
36    
37     #include "ev.h"
38    
39 root 1.10 typedef struct ev_watcher *W;
40     typedef struct ev_watcher_list *WL;
41 root 1.12 typedef struct ev_watcher_time *WT;
42 root 1.10
43 root 1.4 static ev_tstamp now, diff; /* monotonic clock */
44 root 1.1 ev_tstamp ev_now;
45     int ev_method;
46    
47     static int have_monotonic; /* runtime */
48    
49     static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
50 root 1.5 static void (*method_modify)(int fd, int oev, int nev);
51 root 1.1 static void (*method_poll)(ev_tstamp timeout);
52    
53 root 1.8 /*****************************************************************************/
54    
55 root 1.1 ev_tstamp
56     ev_time (void)
57     {
58     #if HAVE_REALTIME
59     struct timespec ts;
60     clock_gettime (CLOCK_REALTIME, &ts);
61     return ts.tv_sec + ts.tv_nsec * 1e-9;
62     #else
63     struct timeval tv;
64     gettimeofday (&tv, 0);
65     return tv.tv_sec + tv.tv_usec * 1e-6;
66     #endif
67     }
68    
69     static ev_tstamp
70     get_clock (void)
71     {
72     #if HAVE_MONOTONIC
73     if (have_monotonic)
74     {
75     struct timespec ts;
76     clock_gettime (CLOCK_MONOTONIC, &ts);
77     return ts.tv_sec + ts.tv_nsec * 1e-9;
78     }
79     #endif
80    
81     return ev_time ();
82     }
83    
84     #define array_needsize(base,cur,cnt,init) \
85     if ((cnt) > cur) \
86     { \
87 root 1.2 int newcnt = cur ? cur << 1 : 16; \
88 root 1.1 base = realloc (base, sizeof (*base) * (newcnt)); \
89     init (base + cur, newcnt - cur); \
90     cur = newcnt; \
91     }
92    
93 root 1.8 /*****************************************************************************/
94    
95 root 1.1 typedef struct
96     {
97     struct ev_io *head;
98     unsigned char wev, rev; /* want, received event set */
99     } ANFD;
100    
101     static ANFD *anfds;
102     static int anfdmax;
103    
104     static int *fdchanges;
105     static int fdchangemax, fdchangecnt;
106    
107     static void
108     anfds_init (ANFD *base, int count)
109     {
110     while (count--)
111     {
112     base->head = 0;
113     base->wev = base->rev = EV_NONE;
114     ++base;
115     }
116     }
117    
118     typedef struct
119     {
120 root 1.10 W w;
121 root 1.1 int events;
122     } ANPENDING;
123    
124     static ANPENDING *pendings;
125     static int pendingmax, pendingcnt;
126    
127     static void
128 root 1.10 event (W w, int events)
129 root 1.1 {
130     w->pending = ++pendingcnt;
131     array_needsize (pendings, pendingmax, pendingcnt, );
132     pendings [pendingcnt - 1].w = w;
133     pendings [pendingcnt - 1].events = events;
134     }
135    
136     static void
137     fd_event (int fd, int events)
138     {
139     ANFD *anfd = anfds + fd;
140     struct ev_io *w;
141    
142     for (w = anfd->head; w; w = w->next)
143     {
144     int ev = w->events & events;
145    
146     if (ev)
147 root 1.10 event ((W)w, ev);
148 root 1.1 }
149     }
150    
151 root 1.9 static void
152 root 1.10 queue_events (W *events, int eventcnt, int type)
153 root 1.9 {
154     int i;
155    
156     for (i = 0; i < eventcnt; ++i)
157     event (events [i], type);
158     }
159    
160 root 1.8 /*****************************************************************************/
161    
162 root 1.12 static struct ev_timer **timers;
163     static int timermax, timercnt;
164 root 1.4
165 root 1.12 static struct ev_periodic **periodics;
166     static int periodicmax, periodiccnt;
167 root 1.1
168     static void
169 root 1.12 upheap (WT *timers, int k)
170 root 1.1 {
171 root 1.12 WT w = timers [k];
172 root 1.1
173     while (k && timers [k >> 1]->at > w->at)
174     {
175     timers [k] = timers [k >> 1];
176     timers [k]->active = k + 1;
177     k >>= 1;
178     }
179    
180     timers [k] = w;
181     timers [k]->active = k + 1;
182    
183     }
184    
185     static void
186 root 1.12 downheap (WT *timers, int N, int k)
187 root 1.1 {
188 root 1.12 WT w = timers [k];
189 root 1.1
190 root 1.4 while (k < (N >> 1))
191 root 1.1 {
192     int j = k << 1;
193    
194 root 1.4 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
195 root 1.1 ++j;
196    
197     if (w->at <= timers [j]->at)
198     break;
199    
200     timers [k] = timers [j];
201 root 1.2 timers [k]->active = k + 1;
202 root 1.1 k = j;
203     }
204    
205     timers [k] = w;
206     timers [k]->active = k + 1;
207     }
208    
209 root 1.8 /*****************************************************************************/
210    
211 root 1.7 typedef struct
212     {
213     struct ev_signal *head;
214     sig_atomic_t gotsig;
215     } ANSIG;
216    
217     static ANSIG *signals;
218 root 1.4 static int signalmax;
219 root 1.1
220 root 1.7 static int sigpipe [2];
221     static sig_atomic_t gotsig;
222     static struct ev_io sigev;
223    
224 root 1.1 static void
225 root 1.7 signals_init (ANSIG *base, int count)
226 root 1.1 {
227     while (count--)
228 root 1.7 {
229     base->head = 0;
230     base->gotsig = 0;
231     ++base;
232     }
233     }
234    
235     static void
236     sighandler (int signum)
237     {
238     signals [signum - 1].gotsig = 1;
239    
240     if (!gotsig)
241     {
242     gotsig = 1;
243     write (sigpipe [1], &gotsig, 1);
244     }
245     }
246    
247     static void
248     sigcb (struct ev_io *iow, int revents)
249     {
250     struct ev_signal *w;
251     int sig;
252    
253     gotsig = 0;
254     read (sigpipe [0], &revents, 1);
255    
256     for (sig = signalmax; sig--; )
257     if (signals [sig].gotsig)
258     {
259     signals [sig].gotsig = 0;
260    
261     for (w = signals [sig].head; w; w = w->next)
262 root 1.10 event ((W)w, EV_SIGNAL);
263 root 1.7 }
264     }
265    
266     static void
267     siginit (void)
268     {
269     fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
270     fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
271    
272     /* rather than sort out wether we really need nb, set it */
273     fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
274     fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
275    
276     evio_set (&sigev, sigpipe [0], EV_READ);
277     evio_start (&sigev);
278 root 1.1 }
279    
280 root 1.8 /*****************************************************************************/
281    
282 root 1.9 static struct ev_idle **idles;
283     static int idlemax, idlecnt;
284    
285     static struct ev_check **checks;
286     static int checkmax, checkcnt;
287    
288     /*****************************************************************************/
289    
290 root 1.1 #if HAVE_EPOLL
291     # include "ev_epoll.c"
292     #endif
293     #if HAVE_SELECT
294     # include "ev_select.c"
295     #endif
296    
297     int ev_init (int flags)
298     {
299     #if HAVE_MONOTONIC
300     {
301     struct timespec ts;
302     if (!clock_gettime (CLOCK_MONOTONIC, &ts))
303     have_monotonic = 1;
304     }
305     #endif
306    
307     ev_now = ev_time ();
308 root 1.4 now = get_clock ();
309     diff = ev_now - now;
310 root 1.1
311 root 1.7 if (pipe (sigpipe))
312     return 0;
313    
314     ev_method = EVMETHOD_NONE;
315 root 1.1 #if HAVE_EPOLL
316 root 1.7 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
317 root 1.1 #endif
318     #if HAVE_SELECT
319 root 1.7 if (ev_method == EVMETHOD_NONE) select_init (flags);
320 root 1.1 #endif
321    
322 root 1.7 if (ev_method)
323     {
324 root 1.12 evw_init (&sigev, sigcb);
325 root 1.7 siginit ();
326     }
327    
328 root 1.1 return ev_method;
329     }
330    
331 root 1.8 /*****************************************************************************/
332    
333 root 1.1 void ev_prefork (void)
334     {
335 root 1.11 /* nop */
336 root 1.1 }
337    
338     void ev_postfork_parent (void)
339     {
340 root 1.11 /* nop */
341 root 1.1 }
342    
343     void ev_postfork_child (void)
344     {
345     #if HAVE_EPOLL
346 root 1.5 if (ev_method == EVMETHOD_EPOLL)
347     epoll_postfork_child ();
348 root 1.1 #endif
349 root 1.7
350     evio_stop (&sigev);
351     close (sigpipe [0]);
352     close (sigpipe [1]);
353     pipe (sigpipe);
354     siginit ();
355 root 1.1 }
356    
357 root 1.8 /*****************************************************************************/
358    
359 root 1.1 static void
360 root 1.5 fd_reify (void)
361     {
362     int i;
363    
364     for (i = 0; i < fdchangecnt; ++i)
365     {
366     int fd = fdchanges [i];
367     ANFD *anfd = anfds + fd;
368     struct ev_io *w;
369    
370     int wev = 0;
371    
372     for (w = anfd->head; w; w = w->next)
373     wev |= w->events;
374    
375     if (anfd->wev != wev)
376     {
377     method_modify (fd, anfd->wev, wev);
378     anfd->wev = wev;
379     }
380     }
381    
382     fdchangecnt = 0;
383     }
384    
385     static void
386 root 1.1 call_pending ()
387     {
388     int i;
389    
390     for (i = 0; i < pendingcnt; ++i)
391     {
392     ANPENDING *p = pendings + i;
393    
394     if (p->w)
395     {
396     p->w->pending = 0;
397     p->w->cb (p->w, p->events);
398     }
399     }
400    
401     pendingcnt = 0;
402     }
403    
404     static void
405 root 1.12 timers_reify ()
406 root 1.1 {
407 root 1.4 while (timercnt && timers [0]->at <= now)
408 root 1.1 {
409     struct ev_timer *w = timers [0];
410    
411 root 1.4 /* first reschedule or stop timer */
412 root 1.1 if (w->repeat)
413     {
414 root 1.12 w->at = now + w->repeat;
415     assert (("timer timeout in the past, negative repeat?", w->at > now));
416     downheap ((WT *)timers, timercnt, 0);
417     }
418     else
419     evtimer_stop (w); /* nonrepeating: stop timer */
420    
421     event ((W)w, EV_TIMEOUT);
422     }
423     }
424 root 1.4
425 root 1.12 static void
426     periodics_reify ()
427     {
428     while (periodiccnt && periodics [0]->at <= ev_now)
429     {
430     struct ev_periodic *w = periodics [0];
431 root 1.1
432 root 1.12 /* first reschedule or stop timer */
433     if (w->interval)
434     {
435     w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
436     assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
437     downheap ((WT *)periodics, periodiccnt, 0);
438 root 1.1 }
439     else
440 root 1.12 evperiodic_stop (w); /* nonrepeating: stop timer */
441    
442     event ((W)w, EV_TIMEOUT);
443     }
444     }
445    
446     static void
447 root 1.13 periodics_reschedule (ev_tstamp diff)
448 root 1.12 {
449     int i;
450    
451 root 1.13 /* adjust periodics after time jump */
452 root 1.12 for (i = 0; i < periodiccnt; ++i)
453     {
454     struct ev_periodic *w = periodics [i];
455    
456     if (w->interval)
457 root 1.4 {
458 root 1.12 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
459    
460     if (fabs (diff) >= 1e-4)
461     {
462     evperiodic_stop (w);
463     evperiodic_start (w);
464    
465     i = 0; /* restart loop, inefficient, but time jumps should be rare */
466     }
467 root 1.4 }
468 root 1.12 }
469 root 1.1 }
470    
471 root 1.4 static void
472     time_update ()
473     {
474     int i;
475 root 1.12
476 root 1.4 ev_now = ev_time ();
477    
478     if (have_monotonic)
479     {
480     ev_tstamp odiff = diff;
481    
482 root 1.12 for (i = 4; --i; ) /* loop a few times, before making important decisions */
483 root 1.4 {
484     now = get_clock ();
485     diff = ev_now - now;
486    
487     if (fabs (odiff - diff) < MIN_TIMEJUMP)
488     return; /* all is well */
489    
490     ev_now = ev_time ();
491     }
492    
493 root 1.13 periodics_reschedule (diff - odiff);
494     /* no timer adjustment, as the monotonic clock doesn't jump */
495 root 1.4 }
496     else
497     {
498     if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
499 root 1.13 {
500     periodics_reschedule (ev_now - now);
501    
502     /* adjust timers. this is easy, as the offset is the same for all */
503     for (i = 0; i < timercnt; ++i)
504     timers [i]->at += diff;
505     }
506 root 1.4
507     now = ev_now;
508     }
509     }
510    
511 root 1.1 int ev_loop_done;
512    
513 root 1.4 void ev_loop (int flags)
514 root 1.1 {
515     double block;
516 root 1.13 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
517 root 1.1
518 root 1.9 if (checkcnt)
519     {
520 root 1.10 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 root 1.9 call_pending ();
522     }
523    
524 root 1.1 do
525     {
526     /* update fd-related kernel structures */
527 root 1.5 fd_reify ();
528 root 1.1
529     /* calculate blocking time */
530 root 1.12
531     /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
532     ev_now = ev_time ();
533    
534 root 1.9 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 root 1.1 block = 0.;
536     else
537     {
538 root 1.4 block = MAX_BLOCKTIME;
539    
540 root 1.12 if (timercnt)
541 root 1.4 {
542 root 1.14 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
543 root 1.4 if (block > to) block = to;
544     }
545    
546 root 1.12 if (periodiccnt)
547 root 1.4 {
548 root 1.12 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
549 root 1.4 if (block > to) block = to;
550     }
551    
552 root 1.1 if (block < 0.) block = 0.;
553     }
554    
555     method_poll (block);
556    
557 root 1.4 /* update ev_now, do magic */
558     time_update ();
559    
560 root 1.9 /* queue pending timers and reschedule them */
561 root 1.12 periodics_reify (); /* absolute timers first */
562     timers_reify (); /* relative timers second */
563 root 1.1
564 root 1.9 /* queue idle watchers unless io or timers are pending */
565     if (!pendingcnt)
566 root 1.10 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 root 1.9
568     /* queue check and possibly idle watchers */
569 root 1.10 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 root 1.9
571 root 1.1 call_pending ();
572     }
573     while (!ev_loop_done);
574 root 1.13
575     if (ev_loop_done != 2)
576     ev_loop_done = 0;
577 root 1.1 }
578    
579 root 1.8 /*****************************************************************************/
580    
581 root 1.1 static void
582 root 1.10 wlist_add (WL *head, WL elem)
583 root 1.1 {
584     elem->next = *head;
585     *head = elem;
586     }
587    
588     static void
589 root 1.10 wlist_del (WL *head, WL elem)
590 root 1.1 {
591     while (*head)
592     {
593     if (*head == elem)
594     {
595     *head = elem->next;
596     return;
597     }
598    
599     head = &(*head)->next;
600     }
601     }
602    
603     static void
604 root 1.10 ev_start (W w, int active)
605 root 1.1 {
606     w->pending = 0;
607     w->active = active;
608     }
609    
610     static void
611 root 1.10 ev_stop (W w)
612 root 1.1 {
613     if (w->pending)
614     pendings [w->pending - 1].w = 0;
615    
616     w->active = 0;
617     }
618    
619 root 1.8 /*****************************************************************************/
620    
621 root 1.1 void
622     evio_start (struct ev_io *w)
623     {
624     if (ev_is_active (w))
625     return;
626    
627     int fd = w->fd;
628    
629 root 1.10 ev_start ((W)w, 1);
630 root 1.1 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
631 root 1.10 wlist_add ((WL *)&anfds[fd].head, (WL)w);
632 root 1.1
633     ++fdchangecnt;
634     array_needsize (fdchanges, fdchangemax, fdchangecnt, );
635     fdchanges [fdchangecnt - 1] = fd;
636     }
637    
638     void
639     evio_stop (struct ev_io *w)
640     {
641     if (!ev_is_active (w))
642     return;
643    
644 root 1.10 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645     ev_stop ((W)w);
646 root 1.1
647     ++fdchangecnt;
648     array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649     fdchanges [fdchangecnt - 1] = w->fd;
650     }
651    
652 root 1.12
653 root 1.1 void
654     evtimer_start (struct ev_timer *w)
655     {
656     if (ev_is_active (w))
657     return;
658    
659 root 1.12 w->at += now;
660    
661 root 1.13 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
662    
663 root 1.12 ev_start ((W)w, ++timercnt);
664     array_needsize (timers, timermax, timercnt, );
665     timers [timercnt - 1] = w;
666     upheap ((WT *)timers, timercnt - 1);
667     }
668    
669     void
670     evtimer_stop (struct ev_timer *w)
671     {
672     if (!ev_is_active (w))
673     return;
674    
675     if (w->active < timercnt--)
676 root 1.1 {
677 root 1.12 timers [w->active - 1] = timers [timercnt];
678     downheap ((WT *)timers, timercnt, w->active - 1);
679     }
680 root 1.4
681 root 1.14 w->at = w->repeat;
682    
683 root 1.12 ev_stop ((W)w);
684     }
685 root 1.4
686 root 1.12 void
687 root 1.14 evtimer_again (struct ev_timer *w)
688     {
689     if (ev_is_active (w))
690     {
691     if (w->repeat)
692     {
693     w->at = now + w->repeat;
694     downheap ((WT *)timers, timercnt, w->active - 1);
695     }
696     else
697     evtimer_stop (w);
698     }
699     else if (w->repeat)
700     evtimer_start (w);
701     }
702    
703     void
704 root 1.12 evperiodic_start (struct ev_periodic *w)
705     {
706     if (ev_is_active (w))
707     return;
708 root 1.1
709 root 1.13 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
710    
711 root 1.12 /* this formula differs from the one in periodic_reify because we do not always round up */
712     if (w->interval)
713     w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
714    
715     ev_start ((W)w, ++periodiccnt);
716     array_needsize (periodics, periodicmax, periodiccnt, );
717     periodics [periodiccnt - 1] = w;
718     upheap ((WT *)periodics, periodiccnt - 1);
719 root 1.1 }
720    
721     void
722 root 1.12 evperiodic_stop (struct ev_periodic *w)
723 root 1.1 {
724     if (!ev_is_active (w))
725     return;
726    
727 root 1.12 if (w->active < periodiccnt--)
728 root 1.2 {
729 root 1.12 periodics [w->active - 1] = periodics [periodiccnt];
730     downheap ((WT *)periodics, periodiccnt, w->active - 1);
731 root 1.2 }
732    
733 root 1.10 ev_stop ((W)w);
734 root 1.1 }
735    
736     void
737     evsignal_start (struct ev_signal *w)
738     {
739     if (ev_is_active (w))
740     return;
741    
742 root 1.10 ev_start ((W)w, 1);
743 root 1.1 array_needsize (signals, signalmax, w->signum, signals_init);
744 root 1.10 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
745 root 1.7
746     if (!w->next)
747     {
748     struct sigaction sa;
749     sa.sa_handler = sighandler;
750     sigfillset (&sa.sa_mask);
751     sa.sa_flags = 0;
752     sigaction (w->signum, &sa, 0);
753     }
754 root 1.1 }
755    
756     void
757     evsignal_stop (struct ev_signal *w)
758     {
759     if (!ev_is_active (w))
760     return;
761    
762 root 1.10 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763     ev_stop ((W)w);
764 root 1.7
765     if (!signals [w->signum - 1].head)
766     signal (w->signum, SIG_DFL);
767 root 1.1 }
768    
769 root 1.9 void evidle_start (struct ev_idle *w)
770     {
771     if (ev_is_active (w))
772     return;
773    
774 root 1.10 ev_start ((W)w, ++idlecnt);
775 root 1.9 array_needsize (idles, idlemax, idlecnt, );
776     idles [idlecnt - 1] = w;
777     }
778    
779     void evidle_stop (struct ev_idle *w)
780     {
781     idles [w->active - 1] = idles [--idlecnt];
782 root 1.10 ev_stop ((W)w);
783 root 1.9 }
784    
785     void evcheck_start (struct ev_check *w)
786     {
787     if (ev_is_active (w))
788     return;
789    
790 root 1.10 ev_start ((W)w, ++checkcnt);
791 root 1.9 array_needsize (checks, checkmax, checkcnt, );
792     checks [checkcnt - 1] = w;
793     }
794    
795     void evcheck_stop (struct ev_check *w)
796     {
797     checks [w->active - 1] = checks [--checkcnt];
798 root 1.10 ev_stop ((W)w);
799 root 1.9 }
800    
801 root 1.1 /*****************************************************************************/
802 root 1.10
803 root 1.13 #if 0
804 root 1.12
805     struct ev_io wio;
806 root 1.1
807     static void
808     sin_cb (struct ev_io *w, int revents)
809     {
810     fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
811     }
812    
813     static void
814     ocb (struct ev_timer *w, int revents)
815     {
816 root 1.4 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
817     evtimer_stop (w);
818     evtimer_start (w);
819 root 1.1 }
820    
821 root 1.7 static void
822     scb (struct ev_signal *w, int revents)
823     {
824     fprintf (stderr, "signal %x,%d\n", revents, w->signum);
825 root 1.12 evio_stop (&wio);
826     evio_start (&wio);
827 root 1.7 }
828    
829 root 1.9 static void
830     gcb (struct ev_signal *w, int revents)
831     {
832     fprintf (stderr, "generic %x\n", revents);
833 root 1.12
834 root 1.9 }
835    
836 root 1.1 int main (void)
837     {
838     ev_init (0);
839    
840 root 1.12 evio_init (&wio, sin_cb, 0, EV_READ);
841     evio_start (&wio);
842 root 1.1
843 root 1.4 struct ev_timer t[10000];
844 root 1.2
845 root 1.9 #if 0
846 root 1.2 int i;
847 root 1.4 for (i = 0; i < 10000; ++i)
848 root 1.2 {
849     struct ev_timer *w = t + i;
850     evw_init (w, ocb, i);
851 root 1.12 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
852 root 1.2 evtimer_start (w);
853     if (drand48 () < 0.5)
854     evtimer_stop (w);
855     }
856 root 1.4 #endif
857    
858     struct ev_timer t1;
859 root 1.12 evtimer_init (&t1, ocb, 5, 10);
860 root 1.4 evtimer_start (&t1);
861 root 1.1
862 root 1.7 struct ev_signal sig;
863 root 1.12 evsignal_init (&sig, scb, SIGQUIT);
864 root 1.7 evsignal_start (&sig);
865    
866 root 1.9 struct ev_check cw;
867 root 1.12 evcheck_init (&cw, gcb);
868 root 1.9 evcheck_start (&cw);
869    
870     struct ev_idle iw;
871 root 1.12 evidle_init (&iw, gcb);
872 root 1.9 evidle_start (&iw);
873    
874 root 1.1 ev_loop (0);
875    
876     return 0;
877     }
878    
879     #endif
880    
881    
882    
883