ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.7 by root, Wed Oct 31 00:24:16 2007 UTC vs.
Revision 1.11 by root, Wed Oct 31 07:40:49 2007 UTC

9#include <assert.h> 9#include <assert.h>
10#include <errno.h> 10#include <errno.h>
11#include <sys/time.h> 11#include <sys/time.h>
12#include <time.h> 12#include <time.h>
13 13
14#ifndef HAVE_MONOTONIC
14#ifdef CLOCK_MONOTONIC 15# ifdef CLOCK_MONOTONIC
15# define HAVE_MONOTONIC 1 16# define HAVE_MONOTONIC 1
16#endif 17# endif
18#endif
17 19
18#define HAVE_REALTIME 1 20#ifndef HAVE_SELECT
19#define HAVE_EPOLL 1
20#define HAVE_SELECT 1 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
21 31
22#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 32#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23#define MAX_BLOCKTIME 60. 33#define MAX_BLOCKTIME 60.
24 34
25#include "ev.h" 35#include "ev.h"
30 40
31struct ev_watcher_list { 41struct ev_watcher_list {
32 EV_WATCHER_LIST (ev_watcher_list); 42 EV_WATCHER_LIST (ev_watcher_list);
33}; 43};
34 44
45typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL;
47
35static ev_tstamp now, diff; /* monotonic clock */ 48static ev_tstamp now, diff; /* monotonic clock */
36ev_tstamp ev_now; 49ev_tstamp ev_now;
37int ev_method; 50int ev_method;
38 51
39static int have_monotonic; /* runtime */ 52static int have_monotonic; /* runtime */
40 53
41static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 54static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42static void (*method_modify)(int fd, int oev, int nev); 55static void (*method_modify)(int fd, int oev, int nev);
43static void (*method_poll)(ev_tstamp timeout); 56static void (*method_poll)(ev_tstamp timeout);
57
58/*****************************************************************************/
44 59
45ev_tstamp 60ev_tstamp
46ev_time (void) 61ev_time (void)
47{ 62{
48#if HAVE_REALTIME 63#if HAVE_REALTIME
79 base = realloc (base, sizeof (*base) * (newcnt)); \ 94 base = realloc (base, sizeof (*base) * (newcnt)); \
80 init (base + cur, newcnt - cur); \ 95 init (base + cur, newcnt - cur); \
81 cur = newcnt; \ 96 cur = newcnt; \
82 } 97 }
83 98
99/*****************************************************************************/
100
84typedef struct 101typedef struct
85{ 102{
86 struct ev_io *head; 103 struct ev_io *head;
87 unsigned char wev, rev; /* want, received event set */ 104 unsigned char wev, rev; /* want, received event set */
88} ANFD; 105} ANFD;
104 } 121 }
105} 122}
106 123
107typedef struct 124typedef struct
108{ 125{
109 struct ev_watcher *w; 126 W w;
110 int events; 127 int events;
111} ANPENDING; 128} ANPENDING;
112 129
113static ANPENDING *pendings; 130static ANPENDING *pendings;
114static int pendingmax, pendingcnt; 131static int pendingmax, pendingcnt;
115 132
116static void 133static void
117event (struct ev_watcher *w, int events) 134event (W w, int events)
118{ 135{
119 w->pending = ++pendingcnt; 136 w->pending = ++pendingcnt;
120 array_needsize (pendings, pendingmax, pendingcnt, ); 137 array_needsize (pendings, pendingmax, pendingcnt, );
121 pendings [pendingcnt - 1].w = w; 138 pendings [pendingcnt - 1].w = w;
122 pendings [pendingcnt - 1].events = events; 139 pendings [pendingcnt - 1].events = events;
131 for (w = anfd->head; w; w = w->next) 148 for (w = anfd->head; w; w = w->next)
132 { 149 {
133 int ev = w->events & events; 150 int ev = w->events & events;
134 151
135 if (ev) 152 if (ev)
136 event ((struct ev_watcher *)w, ev); 153 event ((W)w, ev);
137 } 154 }
138} 155}
156
157static void
158queue_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/*****************************************************************************/
139 167
140static struct ev_timer **atimers; 168static struct ev_timer **atimers;
141static int atimermax, atimercnt; 169static int atimermax, atimercnt;
142 170
143static struct ev_timer **rtimers; 171static struct ev_timer **rtimers;
181 } 209 }
182 210
183 timers [k] = w; 211 timers [k] = w;
184 timers [k]->active = k + 1; 212 timers [k]->active = k + 1;
185} 213}
214
215/*****************************************************************************/
186 216
187typedef struct 217typedef struct
188{ 218{
189 struct ev_signal *head; 219 struct ev_signal *head;
190 sig_atomic_t gotsig; 220 sig_atomic_t gotsig;
233 if (signals [sig].gotsig) 263 if (signals [sig].gotsig)
234 { 264 {
235 signals [sig].gotsig = 0; 265 signals [sig].gotsig = 0;
236 266
237 for (w = signals [sig].head; w; w = w->next) 267 for (w = signals [sig].head; w; w = w->next)
238 event ((struct ev_watcher *)w, EV_SIGNAL); 268 event ((W)w, EV_SIGNAL);
239 } 269 }
240} 270}
241 271
242static void 272static void
243siginit (void) 273siginit (void)
250 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 280 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
251 281
252 evio_set (&sigev, sigpipe [0], EV_READ); 282 evio_set (&sigev, sigpipe [0], EV_READ);
253 evio_start (&sigev); 283 evio_start (&sigev);
254} 284}
285
286/*****************************************************************************/
287
288static struct ev_idle **idles;
289static int idlemax, idlecnt;
290
291static struct ev_check **checks;
292static int checkmax, checkcnt;
293
294/*****************************************************************************/
255 295
256#if HAVE_EPOLL 296#if HAVE_EPOLL
257# include "ev_epoll.c" 297# include "ev_epoll.c"
258#endif 298#endif
259#if HAVE_SELECT 299#if HAVE_SELECT
292 } 332 }
293 333
294 return ev_method; 334 return ev_method;
295} 335}
296 336
337/*****************************************************************************/
338
297void ev_prefork (void) 339void ev_prefork (void)
298{ 340{
341 /* nop */
299} 342}
300 343
301void ev_postfork_parent (void) 344void ev_postfork_parent (void)
302{ 345{
346 /* nop */
303} 347}
304 348
305void ev_postfork_child (void) 349void ev_postfork_child (void)
306{ 350{
307#if HAVE_EPOLL 351#if HAVE_EPOLL
314 close (sigpipe [1]); 358 close (sigpipe [1]);
315 pipe (sigpipe); 359 pipe (sigpipe);
316 siginit (); 360 siginit ();
317} 361}
318 362
363/*****************************************************************************/
364
319static void 365static void
320fd_reify (void) 366fd_reify (void)
321{ 367{
322 int i; 368 int i;
323 369
384 { 430 {
385 evtimer_stop (w); /* nonrepeating: stop timer */ 431 evtimer_stop (w); /* nonrepeating: stop timer */
386 --timercnt; /* maybe pass by reference instead? */ 432 --timercnt; /* maybe pass by reference instead? */
387 } 433 }
388 434
389 event ((struct ev_watcher *)w, EV_TIMEOUT); 435 event ((W)w, EV_TIMEOUT);
390 } 436 }
391} 437}
392 438
393static void 439static void
394time_update () 440time_update ()
435void ev_loop (int flags) 481void ev_loop (int flags)
436{ 482{
437 double block; 483 double block;
438 ev_loop_done = flags & EVLOOP_ONESHOT; 484 ev_loop_done = flags & EVLOOP_ONESHOT;
439 485
486 if (checkcnt)
487 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending ();
490 }
491
440 do 492 do
441 { 493 {
442 /* update fd-related kernel structures */ 494 /* update fd-related kernel structures */
443 fd_reify (); 495 fd_reify ();
444 496
445 /* calculate blocking time */ 497 /* calculate blocking time */
446 if (flags & EVLOOP_NONBLOCK) 498 if (flags & EVLOOP_NONBLOCK || idlecnt)
447 block = 0.; 499 block = 0.;
448 else 500 else
449 { 501 {
450 block = MAX_BLOCKTIME; 502 block = MAX_BLOCKTIME;
451 503
467 method_poll (block); 519 method_poll (block);
468 520
469 /* update ev_now, do magic */ 521 /* update ev_now, do magic */
470 time_update (); 522 time_update ();
471 523
472 /* put pending timers into pendign queue and reschedule them */ 524 /* queue pending timers and reschedule them */
473 /* absolute timers first */ 525 /* absolute timers first */
474 timers_reify (atimers, atimercnt, ev_now); 526 timers_reify (atimers, atimercnt, ev_now);
475 /* relative timers second */ 527 /* relative timers second */
476 timers_reify (rtimers, rtimercnt, now); 528 timers_reify (rtimers, rtimercnt, now);
477 529
530 /* queue idle watchers unless io or timers are pending */
531 if (!pendingcnt)
532 queue_events ((W *)idles, idlecnt, EV_IDLE);
533
534 /* queue check and possibly idle watchers */
535 queue_events ((W *)checks, checkcnt, EV_CHECK);
536
478 call_pending (); 537 call_pending ();
479 } 538 }
480 while (!ev_loop_done); 539 while (!ev_loop_done);
481} 540}
482 541
542/*****************************************************************************/
543
483static void 544static void
484wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 545wlist_add (WL *head, WL elem)
485{ 546{
486 elem->next = *head; 547 elem->next = *head;
487 *head = elem; 548 *head = elem;
488} 549}
489 550
490static void 551static void
491wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 552wlist_del (WL *head, WL elem)
492{ 553{
493 while (*head) 554 while (*head)
494 { 555 {
495 if (*head == elem) 556 if (*head == elem)
496 { 557 {
501 head = &(*head)->next; 562 head = &(*head)->next;
502 } 563 }
503} 564}
504 565
505static void 566static void
506ev_start (struct ev_watcher *w, int active) 567ev_start (W w, int active)
507{ 568{
508 w->pending = 0; 569 w->pending = 0;
509 w->active = active; 570 w->active = active;
510} 571}
511 572
512static void 573static void
513ev_stop (struct ev_watcher *w) 574ev_stop (W w)
514{ 575{
515 if (w->pending) 576 if (w->pending)
516 pendings [w->pending - 1].w = 0; 577 pendings [w->pending - 1].w = 0;
517 578
518 w->active = 0; 579 w->active = 0;
519 /* nop */
520} 580}
581
582/*****************************************************************************/
521 583
522void 584void
523evio_start (struct ev_io *w) 585evio_start (struct ev_io *w)
524{ 586{
525 if (ev_is_active (w)) 587 if (ev_is_active (w))
526 return; 588 return;
527 589
528 int fd = w->fd; 590 int fd = w->fd;
529 591
530 ev_start ((struct ev_watcher *)w, 1); 592 ev_start ((W)w, 1);
531 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 593 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
532 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 594 wlist_add ((WL *)&anfds[fd].head, (WL)w);
533 595
534 ++fdchangecnt; 596 ++fdchangecnt;
535 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 597 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
536 fdchanges [fdchangecnt - 1] = fd; 598 fdchanges [fdchangecnt - 1] = fd;
537} 599}
540evio_stop (struct ev_io *w) 602evio_stop (struct ev_io *w)
541{ 603{
542 if (!ev_is_active (w)) 604 if (!ev_is_active (w))
543 return; 605 return;
544 606
545 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 607 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
546 ev_stop ((struct ev_watcher *)w); 608 ev_stop ((W)w);
547 609
548 ++fdchangecnt; 610 ++fdchangecnt;
549 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 611 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
550 fdchanges [fdchangecnt - 1] = w->fd; 612 fdchanges [fdchangecnt - 1] = w->fd;
551} 613}
560 { 622 {
561 /* this formula differs from the one in timer_reify becuse we do not round up */ 623 /* this formula differs from the one in timer_reify becuse we do not round up */
562 if (w->repeat) 624 if (w->repeat)
563 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
564 626
565 ev_start ((struct ev_watcher *)w, ++atimercnt); 627 ev_start ((W)w, ++atimercnt);
566 array_needsize (atimers, atimermax, atimercnt, ); 628 array_needsize (atimers, atimermax, atimercnt, );
567 atimers [atimercnt - 1] = w; 629 atimers [atimercnt - 1] = w;
568 upheap (atimers, atimercnt - 1); 630 upheap (atimers, atimercnt - 1);
569 } 631 }
570 else 632 else
571 { 633 {
572 w->at += now; 634 w->at += now;
573 635
574 ev_start ((struct ev_watcher *)w, ++rtimercnt); 636 ev_start ((W)w, ++rtimercnt);
575 array_needsize (rtimers, rtimermax, rtimercnt, ); 637 array_needsize (rtimers, rtimermax, rtimercnt, );
576 rtimers [rtimercnt - 1] = w; 638 rtimers [rtimercnt - 1] = w;
577 upheap (rtimers, rtimercnt - 1); 639 upheap (rtimers, rtimercnt - 1);
578 } 640 }
579 641
600 rtimers [w->active - 1] = rtimers [rtimercnt]; 662 rtimers [w->active - 1] = rtimers [rtimercnt];
601 downheap (rtimers, rtimercnt, w->active - 1); 663 downheap (rtimers, rtimercnt, w->active - 1);
602 } 664 }
603 } 665 }
604 666
605 ev_stop ((struct ev_watcher *)w); 667 ev_stop ((W)w);
606} 668}
607 669
608void 670void
609evsignal_start (struct ev_signal *w) 671evsignal_start (struct ev_signal *w)
610{ 672{
611 if (ev_is_active (w)) 673 if (ev_is_active (w))
612 return; 674 return;
613 675
614 ev_start ((struct ev_watcher *)w, 1); 676 ev_start ((W)w, 1);
615 array_needsize (signals, signalmax, w->signum, signals_init); 677 array_needsize (signals, signalmax, w->signum, signals_init);
616 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 678 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
617 679
618 if (!w->next) 680 if (!w->next)
619 { 681 {
620 struct sigaction sa; 682 struct sigaction sa;
621 sa.sa_handler = sighandler; 683 sa.sa_handler = sighandler;
629evsignal_stop (struct ev_signal *w) 691evsignal_stop (struct ev_signal *w)
630{ 692{
631 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
632 return; 694 return;
633 695
634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 696 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
635 ev_stop ((struct ev_watcher *)w); 697 ev_stop ((W)w);
636 698
637 if (!signals [w->signum - 1].head) 699 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL); 700 signal (w->signum, SIG_DFL);
639} 701}
640 702
703void evidle_start (struct ev_idle *w)
704{
705 if (ev_is_active (w))
706 return;
707
708 ev_start ((W)w, ++idlecnt);
709 array_needsize (idles, idlemax, idlecnt, );
710 idles [idlecnt - 1] = w;
711}
712
713void evidle_stop (struct ev_idle *w)
714{
715 idles [w->active - 1] = idles [--idlecnt];
716 ev_stop ((W)w);
717}
718
719void evcheck_start (struct ev_check *w)
720{
721 if (ev_is_active (w))
722 return;
723
724 ev_start ((W)w, ++checkcnt);
725 array_needsize (checks, checkmax, checkcnt, );
726 checks [checkcnt - 1] = w;
727}
728
729void evcheck_stop (struct ev_check *w)
730{
731 checks [w->active - 1] = checks [--checkcnt];
732 ev_stop ((W)w);
733}
734
641/*****************************************************************************/ 735/*****************************************************************************/
736
642#if 1 737#if 0
643 738
644static void 739static void
645sin_cb (struct ev_io *w, int revents) 740sin_cb (struct ev_io *w, int revents)
646{ 741{
647 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
657 752
658static void 753static void
659scb (struct ev_signal *w, int revents) 754scb (struct ev_signal *w, int revents)
660{ 755{
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 756 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
757}
758
759static void
760gcb (struct ev_signal *w, int revents)
761{
762 fprintf (stderr, "generic %x\n", revents);
662} 763}
663 764
664int main (void) 765int main (void)
665{ 766{
666 struct ev_io sin; 767 struct ev_io sin;
694 struct ev_signal sig; 795 struct ev_signal sig;
695 evw_init (&sig, scb, 65535); 796 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT); 797 evsignal_set (&sig, SIGQUIT);
697 evsignal_start (&sig); 798 evsignal_start (&sig);
698 799
800 struct ev_check cw;
801 evw_init (&cw, gcb, 0);
802 evcheck_start (&cw);
803
804 struct ev_idle iw;
805 evw_init (&iw, gcb, 0);
806 evidle_start (&iw);
807
699 ev_loop (0); 808 ev_loop (0);
700 809
701 return 0; 810 return 0;
702} 811}
703 812

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines