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

# Content
1 #include <math.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <signal.h>
6
7 #include <stdio.h>
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <time.h>
13
14 #define HAVE_EPOLL 1
15
16 #ifndef HAVE_MONOTONIC
17 # ifdef CLOCK_MONOTONIC
18 # define HAVE_MONOTONIC 1
19 # endif
20 #endif
21
22 #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
34 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
35 #define MAX_BLOCKTIME 60.
36
37 #include "ev.h"
38
39 typedef struct ev_watcher *W;
40 typedef struct ev_watcher_list *WL;
41 typedef struct ev_watcher_time *WT;
42
43 static ev_tstamp now, diff; /* monotonic clock */
44 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 static void (*method_modify)(int fd, int oev, int nev);
51 static void (*method_poll)(ev_tstamp timeout);
52
53 /*****************************************************************************/
54
55 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 int newcnt = cur ? cur << 1 : 16; \
88 base = realloc (base, sizeof (*base) * (newcnt)); \
89 init (base + cur, newcnt - cur); \
90 cur = newcnt; \
91 }
92
93 /*****************************************************************************/
94
95 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 W w;
121 int events;
122 } ANPENDING;
123
124 static ANPENDING *pendings;
125 static int pendingmax, pendingcnt;
126
127 static void
128 event (W w, int events)
129 {
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 event ((W)w, ev);
148 }
149 }
150
151 static void
152 queue_events (W *events, int eventcnt, int type)
153 {
154 int i;
155
156 for (i = 0; i < eventcnt; ++i)
157 event (events [i], type);
158 }
159
160 /*****************************************************************************/
161
162 static struct ev_timer **timers;
163 static int timermax, timercnt;
164
165 static struct ev_periodic **periodics;
166 static int periodicmax, periodiccnt;
167
168 static void
169 upheap (WT *timers, int k)
170 {
171 WT w = timers [k];
172
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 downheap (WT *timers, int N, int k)
187 {
188 WT w = timers [k];
189
190 while (k < (N >> 1))
191 {
192 int j = k << 1;
193
194 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
195 ++j;
196
197 if (w->at <= timers [j]->at)
198 break;
199
200 timers [k] = timers [j];
201 timers [k]->active = k + 1;
202 k = j;
203 }
204
205 timers [k] = w;
206 timers [k]->active = k + 1;
207 }
208
209 /*****************************************************************************/
210
211 typedef struct
212 {
213 struct ev_signal *head;
214 sig_atomic_t gotsig;
215 } ANSIG;
216
217 static ANSIG *signals;
218 static int signalmax;
219
220 static int sigpipe [2];
221 static sig_atomic_t gotsig;
222 static struct ev_io sigev;
223
224 static void
225 signals_init (ANSIG *base, int count)
226 {
227 while (count--)
228 {
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 event ((W)w, EV_SIGNAL);
263 }
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 }
279
280 /*****************************************************************************/
281
282 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 #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 now = get_clock ();
309 diff = ev_now - now;
310
311 if (pipe (sigpipe))
312 return 0;
313
314 ev_method = EVMETHOD_NONE;
315 #if HAVE_EPOLL
316 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
317 #endif
318 #if HAVE_SELECT
319 if (ev_method == EVMETHOD_NONE) select_init (flags);
320 #endif
321
322 if (ev_method)
323 {
324 evw_init (&sigev, sigcb);
325 siginit ();
326 }
327
328 return ev_method;
329 }
330
331 /*****************************************************************************/
332
333 void ev_prefork (void)
334 {
335 /* nop */
336 }
337
338 void ev_postfork_parent (void)
339 {
340 /* nop */
341 }
342
343 void ev_postfork_child (void)
344 {
345 #if HAVE_EPOLL
346 if (ev_method == EVMETHOD_EPOLL)
347 epoll_postfork_child ();
348 #endif
349
350 evio_stop (&sigev);
351 close (sigpipe [0]);
352 close (sigpipe [1]);
353 pipe (sigpipe);
354 siginit ();
355 }
356
357 /*****************************************************************************/
358
359 static void
360 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 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 timers_reify ()
406 {
407 while (timercnt && timers [0]->at <= now)
408 {
409 struct ev_timer *w = timers [0];
410
411 /* first reschedule or stop timer */
412 if (w->repeat)
413 {
414 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
425 static void
426 periodics_reify ()
427 {
428 while (periodiccnt && periodics [0]->at <= ev_now)
429 {
430 struct ev_periodic *w = periodics [0];
431
432 /* 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 }
439 else
440 evperiodic_stop (w); /* nonrepeating: stop timer */
441
442 event ((W)w, EV_TIMEOUT);
443 }
444 }
445
446 static void
447 periodics_reschedule (ev_tstamp diff)
448 {
449 int i;
450
451 /* adjust periodics after time jump */
452 for (i = 0; i < periodiccnt; ++i)
453 {
454 struct ev_periodic *w = periodics [i];
455
456 if (w->interval)
457 {
458 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 }
468 }
469 }
470
471 static void
472 time_update ()
473 {
474 int i;
475
476 ev_now = ev_time ();
477
478 if (have_monotonic)
479 {
480 ev_tstamp odiff = diff;
481
482 for (i = 4; --i; ) /* loop a few times, before making important decisions */
483 {
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 periodics_reschedule (diff - odiff);
494 /* no timer adjustment, as the monotonic clock doesn't jump */
495 }
496 else
497 {
498 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
499 {
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
507 now = ev_now;
508 }
509 }
510
511 int ev_loop_done;
512
513 void ev_loop (int flags)
514 {
515 double block;
516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
517
518 if (checkcnt)
519 {
520 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 call_pending ();
522 }
523
524 do
525 {
526 /* update fd-related kernel structures */
527 fd_reify ();
528
529 /* calculate blocking time */
530
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 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 block = 0.;
536 else
537 {
538 block = MAX_BLOCKTIME;
539
540 if (timercnt)
541 {
542 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
543 if (block > to) block = to;
544 }
545
546 if (periodiccnt)
547 {
548 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
549 if (block > to) block = to;
550 }
551
552 if (block < 0.) block = 0.;
553 }
554
555 method_poll (block);
556
557 /* update ev_now, do magic */
558 time_update ();
559
560 /* queue pending timers and reschedule them */
561 periodics_reify (); /* absolute timers first */
562 timers_reify (); /* relative timers second */
563
564 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE);
567
568 /* queue check and possibly idle watchers */
569 queue_events ((W *)checks, checkcnt, EV_CHECK);
570
571 call_pending ();
572 }
573 while (!ev_loop_done);
574
575 if (ev_loop_done != 2)
576 ev_loop_done = 0;
577 }
578
579 /*****************************************************************************/
580
581 static void
582 wlist_add (WL *head, WL elem)
583 {
584 elem->next = *head;
585 *head = elem;
586 }
587
588 static void
589 wlist_del (WL *head, WL elem)
590 {
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 ev_start (W w, int active)
605 {
606 w->pending = 0;
607 w->active = active;
608 }
609
610 static void
611 ev_stop (W w)
612 {
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0;
617 }
618
619 /*****************************************************************************/
620
621 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 ev_start ((W)w, 1);
630 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
631 wlist_add ((WL *)&anfds[fd].head, (WL)w);
632
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 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w);
646
647 ++fdchangecnt;
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd;
650 }
651
652
653 void
654 evtimer_start (struct ev_timer *w)
655 {
656 if (ev_is_active (w))
657 return;
658
659 w->at += now;
660
661 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
662
663 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 {
677 timers [w->active - 1] = timers [timercnt];
678 downheap ((WT *)timers, timercnt, w->active - 1);
679 }
680
681 w->at = w->repeat;
682
683 ev_stop ((W)w);
684 }
685
686 void
687 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 evperiodic_start (struct ev_periodic *w)
705 {
706 if (ev_is_active (w))
707 return;
708
709 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
710
711 /* 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 }
720
721 void
722 evperiodic_stop (struct ev_periodic *w)
723 {
724 if (!ev_is_active (w))
725 return;
726
727 if (w->active < periodiccnt--)
728 {
729 periodics [w->active - 1] = periodics [periodiccnt];
730 downheap ((WT *)periodics, periodiccnt, w->active - 1);
731 }
732
733 ev_stop ((W)w);
734 }
735
736 void
737 evsignal_start (struct ev_signal *w)
738 {
739 if (ev_is_active (w))
740 return;
741
742 ev_start ((W)w, 1);
743 array_needsize (signals, signalmax, w->signum, signals_init);
744 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
745
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 }
755
756 void
757 evsignal_stop (struct ev_signal *w)
758 {
759 if (!ev_is_active (w))
760 return;
761
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w);
764
765 if (!signals [w->signum - 1].head)
766 signal (w->signum, SIG_DFL);
767 }
768
769 void evidle_start (struct ev_idle *w)
770 {
771 if (ev_is_active (w))
772 return;
773
774 ev_start ((W)w, ++idlecnt);
775 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 ev_stop ((W)w);
783 }
784
785 void evcheck_start (struct ev_check *w)
786 {
787 if (ev_is_active (w))
788 return;
789
790 ev_start ((W)w, ++checkcnt);
791 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 ev_stop ((W)w);
799 }
800
801 /*****************************************************************************/
802
803 #if 0
804
805 struct ev_io wio;
806
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 //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 }
820
821 static void
822 scb (struct ev_signal *w, int revents)
823 {
824 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
825 evio_stop (&wio);
826 evio_start (&wio);
827 }
828
829 static void
830 gcb (struct ev_signal *w, int revents)
831 {
832 fprintf (stderr, "generic %x\n", revents);
833
834 }
835
836 int main (void)
837 {
838 ev_init (0);
839
840 evio_init (&wio, sin_cb, 0, EV_READ);
841 evio_start (&wio);
842
843 struct ev_timer t[10000];
844
845 #if 0
846 int i;
847 for (i = 0; i < 10000; ++i)
848 {
849 struct ev_timer *w = t + i;
850 evw_init (w, ocb, i);
851 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
852 evtimer_start (w);
853 if (drand48 () < 0.5)
854 evtimer_stop (w);
855 }
856 #endif
857
858 struct ev_timer t1;
859 evtimer_init (&t1, ocb, 5, 10);
860 evtimer_start (&t1);
861
862 struct ev_signal sig;
863 evsignal_init (&sig, scb, SIGQUIT);
864 evsignal_start (&sig);
865
866 struct ev_check cw;
867 evcheck_init (&cw, gcb);
868 evcheck_start (&cw);
869
870 struct ev_idle iw;
871 evidle_init (&iw, gcb);
872 evidle_start (&iw);
873
874 ev_loop (0);
875
876 return 0;
877 }
878
879 #endif
880
881
882
883