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

Comparing libev/ev.c (file contents):
Revision 1.9 by root, Wed Oct 31 07:24:17 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"
29}; 39};
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};
44
45typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL;
34 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
108 } 121 }
109} 122}
110 123
111typedef struct 124typedef struct
112{ 125{
113 struct ev_watcher *w; 126 W w;
114 int events; 127 int events;
115} ANPENDING; 128} ANPENDING;
116 129
117static ANPENDING *pendings; 130static ANPENDING *pendings;
118static int pendingmax, pendingcnt; 131static int pendingmax, pendingcnt;
119 132
120static void 133static void
121event (struct ev_watcher *w, int events) 134event (W w, int events)
122{ 135{
123 w->pending = ++pendingcnt; 136 w->pending = ++pendingcnt;
124 array_needsize (pendings, pendingmax, pendingcnt, ); 137 array_needsize (pendings, pendingmax, pendingcnt, );
125 pendings [pendingcnt - 1].w = w; 138 pendings [pendingcnt - 1].w = w;
126 pendings [pendingcnt - 1].events = events; 139 pendings [pendingcnt - 1].events = events;
135 for (w = anfd->head; w; w = w->next) 148 for (w = anfd->head; w; w = w->next)
136 { 149 {
137 int ev = w->events & events; 150 int ev = w->events & events;
138 151
139 if (ev) 152 if (ev)
140 event ((struct ev_watcher *)w, ev); 153 event ((W)w, ev);
141 } 154 }
142} 155}
143 156
144static void 157static void
145queue_events (struct ev_watcher **events, int eventcnt, int type) 158queue_events (W *events, int eventcnt, int type)
146{ 159{
147 int i; 160 int i;
148 161
149 for (i = 0; i < eventcnt; ++i) 162 for (i = 0; i < eventcnt; ++i)
150 event (events [i], type); 163 event (events [i], type);
250 if (signals [sig].gotsig) 263 if (signals [sig].gotsig)
251 { 264 {
252 signals [sig].gotsig = 0; 265 signals [sig].gotsig = 0;
253 266
254 for (w = signals [sig].head; w; w = w->next) 267 for (w = signals [sig].head; w; w = w->next)
255 event ((struct ev_watcher *)w, EV_SIGNAL); 268 event ((W)w, EV_SIGNAL);
256 } 269 }
257} 270}
258 271
259static void 272static void
260siginit (void) 273siginit (void)
323 336
324/*****************************************************************************/ 337/*****************************************************************************/
325 338
326void ev_prefork (void) 339void ev_prefork (void)
327{ 340{
341 /* nop */
328} 342}
329 343
330void ev_postfork_parent (void) 344void ev_postfork_parent (void)
331{ 345{
346 /* nop */
332} 347}
333 348
334void ev_postfork_child (void) 349void ev_postfork_child (void)
335{ 350{
336#if HAVE_EPOLL 351#if HAVE_EPOLL
415 { 430 {
416 evtimer_stop (w); /* nonrepeating: stop timer */ 431 evtimer_stop (w); /* nonrepeating: stop timer */
417 --timercnt; /* maybe pass by reference instead? */ 432 --timercnt; /* maybe pass by reference instead? */
418 } 433 }
419 434
420 event ((struct ev_watcher *)w, EV_TIMEOUT); 435 event ((W)w, EV_TIMEOUT);
421 } 436 }
422} 437}
423 438
424static void 439static void
425time_update () 440time_update ()
468 double block; 483 double block;
469 ev_loop_done = flags & EVLOOP_ONESHOT; 484 ev_loop_done = flags & EVLOOP_ONESHOT;
470 485
471 if (checkcnt) 486 if (checkcnt)
472 { 487 {
473 queue_events (checks, checkcnt, EV_CHECK); 488 queue_events ((W *)checks, checkcnt, EV_CHECK);
474 call_pending (); 489 call_pending ();
475 } 490 }
476 491
477 do 492 do
478 { 493 {
512 /* relative timers second */ 527 /* relative timers second */
513 timers_reify (rtimers, rtimercnt, now); 528 timers_reify (rtimers, rtimercnt, now);
514 529
515 /* queue idle watchers unless io or timers are pending */ 530 /* queue idle watchers unless io or timers are pending */
516 if (!pendingcnt) 531 if (!pendingcnt)
517 queue_events (idles, idlecnt, EV_IDLE); 532 queue_events ((W *)idles, idlecnt, EV_IDLE);
518 533
519 /* queue check and possibly idle watchers */ 534 /* queue check and possibly idle watchers */
520 queue_events (checks, checkcnt, EV_CHECK); 535 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 536
522 call_pending (); 537 call_pending ();
523 } 538 }
524 while (!ev_loop_done); 539 while (!ev_loop_done);
525} 540}
526 541
527/*****************************************************************************/ 542/*****************************************************************************/
528 543
529static void 544static void
530wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 545wlist_add (WL *head, WL elem)
531{ 546{
532 elem->next = *head; 547 elem->next = *head;
533 *head = elem; 548 *head = elem;
534} 549}
535 550
536static void 551static void
537wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 552wlist_del (WL *head, WL elem)
538{ 553{
539 while (*head) 554 while (*head)
540 { 555 {
541 if (*head == elem) 556 if (*head == elem)
542 { 557 {
547 head = &(*head)->next; 562 head = &(*head)->next;
548 } 563 }
549} 564}
550 565
551static void 566static void
552ev_start (struct ev_watcher *w, int active) 567ev_start (W w, int active)
553{ 568{
554 w->pending = 0; 569 w->pending = 0;
555 w->active = active; 570 w->active = active;
556} 571}
557 572
558static void 573static void
559ev_stop (struct ev_watcher *w) 574ev_stop (W w)
560{ 575{
561 if (w->pending) 576 if (w->pending)
562 pendings [w->pending - 1].w = 0; 577 pendings [w->pending - 1].w = 0;
563 578
564 w->active = 0; 579 w->active = 0;
565 /* nop */
566} 580}
567 581
568/*****************************************************************************/ 582/*****************************************************************************/
569 583
570void 584void
573 if (ev_is_active (w)) 587 if (ev_is_active (w))
574 return; 588 return;
575 589
576 int fd = w->fd; 590 int fd = w->fd;
577 591
578 ev_start ((struct ev_watcher *)w, 1); 592 ev_start ((W)w, 1);
579 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 593 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
580 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 594 wlist_add ((WL *)&anfds[fd].head, (WL)w);
581 595
582 ++fdchangecnt; 596 ++fdchangecnt;
583 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 597 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
584 fdchanges [fdchangecnt - 1] = fd; 598 fdchanges [fdchangecnt - 1] = fd;
585} 599}
588evio_stop (struct ev_io *w) 602evio_stop (struct ev_io *w)
589{ 603{
590 if (!ev_is_active (w)) 604 if (!ev_is_active (w))
591 return; 605 return;
592 606
593 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);
594 ev_stop ((struct ev_watcher *)w); 608 ev_stop ((W)w);
595 609
596 ++fdchangecnt; 610 ++fdchangecnt;
597 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 611 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598 fdchanges [fdchangecnt - 1] = w->fd; 612 fdchanges [fdchangecnt - 1] = w->fd;
599} 613}
608 { 622 {
609 /* 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 */
610 if (w->repeat) 624 if (w->repeat)
611 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
612 626
613 ev_start ((struct ev_watcher *)w, ++atimercnt); 627 ev_start ((W)w, ++atimercnt);
614 array_needsize (atimers, atimermax, atimercnt, ); 628 array_needsize (atimers, atimermax, atimercnt, );
615 atimers [atimercnt - 1] = w; 629 atimers [atimercnt - 1] = w;
616 upheap (atimers, atimercnt - 1); 630 upheap (atimers, atimercnt - 1);
617 } 631 }
618 else 632 else
619 { 633 {
620 w->at += now; 634 w->at += now;
621 635
622 ev_start ((struct ev_watcher *)w, ++rtimercnt); 636 ev_start ((W)w, ++rtimercnt);
623 array_needsize (rtimers, rtimermax, rtimercnt, ); 637 array_needsize (rtimers, rtimermax, rtimercnt, );
624 rtimers [rtimercnt - 1] = w; 638 rtimers [rtimercnt - 1] = w;
625 upheap (rtimers, rtimercnt - 1); 639 upheap (rtimers, rtimercnt - 1);
626 } 640 }
627 641
648 rtimers [w->active - 1] = rtimers [rtimercnt]; 662 rtimers [w->active - 1] = rtimers [rtimercnt];
649 downheap (rtimers, rtimercnt, w->active - 1); 663 downheap (rtimers, rtimercnt, w->active - 1);
650 } 664 }
651 } 665 }
652 666
653 ev_stop ((struct ev_watcher *)w); 667 ev_stop ((W)w);
654} 668}
655 669
656void 670void
657evsignal_start (struct ev_signal *w) 671evsignal_start (struct ev_signal *w)
658{ 672{
659 if (ev_is_active (w)) 673 if (ev_is_active (w))
660 return; 674 return;
661 675
662 ev_start ((struct ev_watcher *)w, 1); 676 ev_start ((W)w, 1);
663 array_needsize (signals, signalmax, w->signum, signals_init); 677 array_needsize (signals, signalmax, w->signum, signals_init);
664 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);
665 679
666 if (!w->next) 680 if (!w->next)
667 { 681 {
668 struct sigaction sa; 682 struct sigaction sa;
669 sa.sa_handler = sighandler; 683 sa.sa_handler = sighandler;
677evsignal_stop (struct ev_signal *w) 691evsignal_stop (struct ev_signal *w)
678{ 692{
679 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
680 return; 694 return;
681 695
682 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);
683 ev_stop ((struct ev_watcher *)w); 697 ev_stop ((W)w);
684 698
685 if (!signals [w->signum - 1].head) 699 if (!signals [w->signum - 1].head)
686 signal (w->signum, SIG_DFL); 700 signal (w->signum, SIG_DFL);
687} 701}
688 702
689void evidle_start (struct ev_idle *w) 703void evidle_start (struct ev_idle *w)
690{ 704{
691 if (ev_is_active (w)) 705 if (ev_is_active (w))
692 return; 706 return;
693 707
694 ev_start ((struct ev_watcher *)w, ++idlecnt); 708 ev_start ((W)w, ++idlecnt);
695 array_needsize (idles, idlemax, idlecnt, ); 709 array_needsize (idles, idlemax, idlecnt, );
696 idles [idlecnt - 1] = w; 710 idles [idlecnt - 1] = w;
697} 711}
698 712
699void evidle_stop (struct ev_idle *w) 713void evidle_stop (struct ev_idle *w)
700{ 714{
701 idles [w->active - 1] = idles [--idlecnt]; 715 idles [w->active - 1] = idles [--idlecnt];
702 ev_stop ((struct ev_watcher *)w); 716 ev_stop ((W)w);
703} 717}
704 718
705void evcheck_start (struct ev_check *w) 719void evcheck_start (struct ev_check *w)
706{ 720{
707 if (ev_is_active (w)) 721 if (ev_is_active (w))
708 return; 722 return;
709 723
710 ev_start ((struct ev_watcher *)w, ++checkcnt); 724 ev_start ((W)w, ++checkcnt);
711 array_needsize (checks, checkmax, checkcnt, ); 725 array_needsize (checks, checkmax, checkcnt, );
712 checks [checkcnt - 1] = w; 726 checks [checkcnt - 1] = w;
713} 727}
714 728
715void evcheck_stop (struct ev_check *w) 729void evcheck_stop (struct ev_check *w)
716{ 730{
717 checks [w->active - 1] = checks [--checkcnt]; 731 checks [w->active - 1] = checks [--checkcnt];
718 ev_stop ((struct ev_watcher *)w); 732 ev_stop ((W)w);
719} 733}
720 734
721/*****************************************************************************/ 735/*****************************************************************************/
736
722#if 1 737#if 0
723 738
724static void 739static void
725sin_cb (struct ev_io *w, int revents) 740sin_cb (struct ev_io *w, int revents)
726{ 741{
727 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines