ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.10
Committed: Wed Oct 31 07:36:03 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +48 -34 lines
Log Message:
introduce MUCH needed internal typedefs, better autoconfiguration

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