ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.17
Committed: Wed Oct 31 14:44:15 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.16: +29 -0 lines
Log Message:
legalese

File Contents

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