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

Comparing libev/ev.c (file contents):
Revision 1.8 by root, Wed Oct 31 00:32:33 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 }
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);
142} 164}
143 165
144/*****************************************************************************/ 166/*****************************************************************************/
145 167
146static struct ev_timer **atimers; 168static struct ev_timer **atimers;
241 if (signals [sig].gotsig) 263 if (signals [sig].gotsig)
242 { 264 {
243 signals [sig].gotsig = 0; 265 signals [sig].gotsig = 0;
244 266
245 for (w = signals [sig].head; w; w = w->next) 267 for (w = signals [sig].head; w; w = w->next)
246 event ((struct ev_watcher *)w, EV_SIGNAL); 268 event ((W)w, EV_SIGNAL);
247 } 269 }
248} 270}
249 271
250static void 272static void
251siginit (void) 273siginit (void)
258 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 280 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
259 281
260 evio_set (&sigev, sigpipe [0], EV_READ); 282 evio_set (&sigev, sigpipe [0], EV_READ);
261 evio_start (&sigev); 283 evio_start (&sigev);
262} 284}
285
286/*****************************************************************************/
287
288static struct ev_idle **idles;
289static int idlemax, idlecnt;
290
291static struct ev_check **checks;
292static int checkmax, checkcnt;
263 293
264/*****************************************************************************/ 294/*****************************************************************************/
265 295
266#if HAVE_EPOLL 296#if HAVE_EPOLL
267# include "ev_epoll.c" 297# include "ev_epoll.c"
306 336
307/*****************************************************************************/ 337/*****************************************************************************/
308 338
309void ev_prefork (void) 339void ev_prefork (void)
310{ 340{
341 /* nop */
311} 342}
312 343
313void ev_postfork_parent (void) 344void ev_postfork_parent (void)
314{ 345{
346 /* nop */
315} 347}
316 348
317void ev_postfork_child (void) 349void ev_postfork_child (void)
318{ 350{
319#if HAVE_EPOLL 351#if HAVE_EPOLL
328 siginit (); 360 siginit ();
329} 361}
330 362
331/*****************************************************************************/ 363/*****************************************************************************/
332 364
333static ev_hook hooks [EVHOOK_NUM];
334
335void
336ev_hook_register (int type, ev_hook hook)
337{
338 hooks [type] = hook;
339}
340
341void
342ev_hook_unregister (int type, ev_hook hook)
343{
344 hooks [type] = 0;
345}
346
347static void
348hook_call (int type)
349{
350 if (hooks [type])
351 hooks [type] ();
352}
353
354static void 365static void
355fd_reify (void) 366fd_reify (void)
356{ 367{
357 int i; 368 int i;
358 369
419 { 430 {
420 evtimer_stop (w); /* nonrepeating: stop timer */ 431 evtimer_stop (w); /* nonrepeating: stop timer */
421 --timercnt; /* maybe pass by reference instead? */ 432 --timercnt; /* maybe pass by reference instead? */
422 } 433 }
423 434
424 event ((struct ev_watcher *)w, EV_TIMEOUT); 435 event ((W)w, EV_TIMEOUT);
425 } 436 }
426} 437}
427 438
428static void 439static void
429time_update () 440time_update ()
470void ev_loop (int flags) 481void ev_loop (int flags)
471{ 482{
472 double block; 483 double block;
473 ev_loop_done = flags & EVLOOP_ONESHOT; 484 ev_loop_done = flags & EVLOOP_ONESHOT;
474 485
486 if (checkcnt)
487 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending ();
490 }
491
475 do 492 do
476 { 493 {
477 hook_call (EVHOOK_PREPOLL);
478
479 /* update fd-related kernel structures */ 494 /* update fd-related kernel structures */
480 fd_reify (); 495 fd_reify ();
481 496
482 /* calculate blocking time */ 497 /* calculate blocking time */
483 if (flags & EVLOOP_NONBLOCK) 498 if (flags & EVLOOP_NONBLOCK || idlecnt)
484 block = 0.; 499 block = 0.;
485 else 500 else
486 { 501 {
487 block = MAX_BLOCKTIME; 502 block = MAX_BLOCKTIME;
488 503
504 method_poll (block); 519 method_poll (block);
505 520
506 /* update ev_now, do magic */ 521 /* update ev_now, do magic */
507 time_update (); 522 time_update ();
508 523
509 hook_call (EVHOOK_POSTPOLL);
510
511 /* put pending timers into pendign queue and reschedule them */ 524 /* queue pending timers and reschedule them */
512 /* absolute timers first */ 525 /* absolute timers first */
513 timers_reify (atimers, atimercnt, ev_now); 526 timers_reify (atimers, atimercnt, ev_now);
514 /* relative timers second */ 527 /* relative timers second */
515 timers_reify (rtimers, rtimercnt, now); 528 timers_reify (rtimers, rtimercnt, now);
516 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
517 call_pending (); 537 call_pending ();
518 } 538 }
519 while (!ev_loop_done); 539 while (!ev_loop_done);
520} 540}
521 541
522/*****************************************************************************/ 542/*****************************************************************************/
523 543
524static void 544static void
525wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 545wlist_add (WL *head, WL elem)
526{ 546{
527 elem->next = *head; 547 elem->next = *head;
528 *head = elem; 548 *head = elem;
529} 549}
530 550
531static void 551static void
532wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 552wlist_del (WL *head, WL elem)
533{ 553{
534 while (*head) 554 while (*head)
535 { 555 {
536 if (*head == elem) 556 if (*head == elem)
537 { 557 {
542 head = &(*head)->next; 562 head = &(*head)->next;
543 } 563 }
544} 564}
545 565
546static void 566static void
547ev_start (struct ev_watcher *w, int active) 567ev_start (W w, int active)
548{ 568{
549 w->pending = 0; 569 w->pending = 0;
550 w->active = active; 570 w->active = active;
551} 571}
552 572
553static void 573static void
554ev_stop (struct ev_watcher *w) 574ev_stop (W w)
555{ 575{
556 if (w->pending) 576 if (w->pending)
557 pendings [w->pending - 1].w = 0; 577 pendings [w->pending - 1].w = 0;
558 578
559 w->active = 0; 579 w->active = 0;
560 /* nop */
561} 580}
562 581
563/*****************************************************************************/ 582/*****************************************************************************/
564 583
565void 584void
568 if (ev_is_active (w)) 587 if (ev_is_active (w))
569 return; 588 return;
570 589
571 int fd = w->fd; 590 int fd = w->fd;
572 591
573 ev_start ((struct ev_watcher *)w, 1); 592 ev_start ((W)w, 1);
574 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 593 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
575 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 594 wlist_add ((WL *)&anfds[fd].head, (WL)w);
576 595
577 ++fdchangecnt; 596 ++fdchangecnt;
578 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 597 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
579 fdchanges [fdchangecnt - 1] = fd; 598 fdchanges [fdchangecnt - 1] = fd;
580} 599}
583evio_stop (struct ev_io *w) 602evio_stop (struct ev_io *w)
584{ 603{
585 if (!ev_is_active (w)) 604 if (!ev_is_active (w))
586 return; 605 return;
587 606
588 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);
589 ev_stop ((struct ev_watcher *)w); 608 ev_stop ((W)w);
590 609
591 ++fdchangecnt; 610 ++fdchangecnt;
592 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 611 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
593 fdchanges [fdchangecnt - 1] = w->fd; 612 fdchanges [fdchangecnt - 1] = w->fd;
594} 613}
603 { 622 {
604 /* 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 */
605 if (w->repeat) 624 if (w->repeat)
606 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
607 626
608 ev_start ((struct ev_watcher *)w, ++atimercnt); 627 ev_start ((W)w, ++atimercnt);
609 array_needsize (atimers, atimermax, atimercnt, ); 628 array_needsize (atimers, atimermax, atimercnt, );
610 atimers [atimercnt - 1] = w; 629 atimers [atimercnt - 1] = w;
611 upheap (atimers, atimercnt - 1); 630 upheap (atimers, atimercnt - 1);
612 } 631 }
613 else 632 else
614 { 633 {
615 w->at += now; 634 w->at += now;
616 635
617 ev_start ((struct ev_watcher *)w, ++rtimercnt); 636 ev_start ((W)w, ++rtimercnt);
618 array_needsize (rtimers, rtimermax, rtimercnt, ); 637 array_needsize (rtimers, rtimermax, rtimercnt, );
619 rtimers [rtimercnt - 1] = w; 638 rtimers [rtimercnt - 1] = w;
620 upheap (rtimers, rtimercnt - 1); 639 upheap (rtimers, rtimercnt - 1);
621 } 640 }
622 641
643 rtimers [w->active - 1] = rtimers [rtimercnt]; 662 rtimers [w->active - 1] = rtimers [rtimercnt];
644 downheap (rtimers, rtimercnt, w->active - 1); 663 downheap (rtimers, rtimercnt, w->active - 1);
645 } 664 }
646 } 665 }
647 666
648 ev_stop ((struct ev_watcher *)w); 667 ev_stop ((W)w);
649} 668}
650 669
651void 670void
652evsignal_start (struct ev_signal *w) 671evsignal_start (struct ev_signal *w)
653{ 672{
654 if (ev_is_active (w)) 673 if (ev_is_active (w))
655 return; 674 return;
656 675
657 ev_start ((struct ev_watcher *)w, 1); 676 ev_start ((W)w, 1);
658 array_needsize (signals, signalmax, w->signum, signals_init); 677 array_needsize (signals, signalmax, w->signum, signals_init);
659 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);
660 679
661 if (!w->next) 680 if (!w->next)
662 { 681 {
663 struct sigaction sa; 682 struct sigaction sa;
664 sa.sa_handler = sighandler; 683 sa.sa_handler = sighandler;
672evsignal_stop (struct ev_signal *w) 691evsignal_stop (struct ev_signal *w)
673{ 692{
674 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
675 return; 694 return;
676 695
677 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);
678 ev_stop ((struct ev_watcher *)w); 697 ev_stop ((W)w);
679 698
680 if (!signals [w->signum - 1].head) 699 if (!signals [w->signum - 1].head)
681 signal (w->signum, SIG_DFL); 700 signal (w->signum, SIG_DFL);
682} 701}
683 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
684/*****************************************************************************/ 735/*****************************************************************************/
736
685#if 1 737#if 0
686 738
687static void 739static void
688sin_cb (struct ev_io *w, int revents) 740sin_cb (struct ev_io *w, int revents)
689{ 741{
690 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
702scb (struct ev_signal *w, int revents) 754scb (struct ev_signal *w, int revents)
703{ 755{
704 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 756 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
705} 757}
706 758
759static void
760gcb (struct ev_signal *w, int revents)
761{
762 fprintf (stderr, "generic %x\n", revents);
763}
764
707int main (void) 765int main (void)
708{ 766{
709 struct ev_io sin; 767 struct ev_io sin;
710 768
711 ev_init (0); 769 ev_init (0);
714 evio_set (&sin, 0, EV_READ); 772 evio_set (&sin, 0, EV_READ);
715 evio_start (&sin); 773 evio_start (&sin);
716 774
717 struct ev_timer t[10000]; 775 struct ev_timer t[10000];
718 776
719#if 1 777#if 0
720 int i; 778 int i;
721 for (i = 0; i < 10000; ++i) 779 for (i = 0; i < 10000; ++i)
722 { 780 {
723 struct ev_timer *w = t + i; 781 struct ev_timer *w = t + i;
724 evw_init (w, ocb, i); 782 evw_init (w, ocb, i);
737 struct ev_signal sig; 795 struct ev_signal sig;
738 evw_init (&sig, scb, 65535); 796 evw_init (&sig, scb, 65535);
739 evsignal_set (&sig, SIGQUIT); 797 evsignal_set (&sig, SIGQUIT);
740 evsignal_start (&sig); 798 evsignal_start (&sig);
741 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
742 ev_loop (0); 808 ev_loop (0);
743 809
744 return 0; 810 return 0;
745} 811}
746 812

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines