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

Comparing libev/ev.c (file contents):
Revision 1.53 by root, Sat Nov 3 22:31:11 2007 UTC vs.
Revision 1.55 by root, Sun Nov 4 00:39:24 2007 UTC

113 113
114typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
115typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
117 117
118static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119
118/*****************************************************************************/ 120/*****************************************************************************/
119 121
120typedef struct 122typedef struct
121{ 123{
122 struct ev_watcher_list *head; 124 struct ev_watcher_list *head;
128{ 130{
129 W w; 131 W w;
130 int events; 132 int events;
131} ANPENDING; 133} ANPENDING;
132 134
133#ifdef EV_MULTIPLICITY 135#if EV_MULTIPLICITY
136
134struct ev_loop 137struct ev_loop
135{ 138{
136# define VAR(name,decl) decl 139# define VAR(name,decl) decl;
137# include "ev_vars.h" 140# include "ev_vars.h"
138}; 141};
142# undef VAR
143# include "ev_wrap.h"
144
139#else 145#else
146
140# define VAR(name,decl) static decl 147# define VAR(name,decl) static decl;
141# include "ev_vars.h" 148# include "ev_vars.h"
142#endif
143#undef VAR 149# undef VAR
150
151#endif
144 152
145/*****************************************************************************/ 153/*****************************************************************************/
146 154
147inline ev_tstamp 155inline ev_tstamp
148ev_time (void) 156ev_time (void)
333} 341}
334 342
335/*****************************************************************************/ 343/*****************************************************************************/
336 344
337static void 345static void
338upheap (WT *timers, int k) 346upheap (WT *heap, int k)
339{ 347{
340 WT w = timers [k]; 348 WT w = heap [k];
341 349
342 while (k && timers [k >> 1]->at > w->at) 350 while (k && heap [k >> 1]->at > w->at)
343 { 351 {
344 timers [k] = timers [k >> 1]; 352 heap [k] = heap [k >> 1];
345 timers [k]->active = k + 1; 353 heap [k]->active = k + 1;
346 k >>= 1; 354 k >>= 1;
347 } 355 }
348 356
349 timers [k] = w; 357 heap [k] = w;
350 timers [k]->active = k + 1; 358 heap [k]->active = k + 1;
351 359
352} 360}
353 361
354static void 362static void
355downheap (WT *timers, int N, int k) 363downheap (WT *heap, int N, int k)
356{ 364{
357 WT w = timers [k]; 365 WT w = heap [k];
358 366
359 while (k < (N >> 1)) 367 while (k < (N >> 1))
360 { 368 {
361 int j = k << 1; 369 int j = k << 1;
362 370
363 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 371 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
364 ++j; 372 ++j;
365 373
366 if (w->at <= timers [j]->at) 374 if (w->at <= heap [j]->at)
367 break; 375 break;
368 376
369 timers [k] = timers [j]; 377 heap [k] = heap [j];
370 timers [k]->active = k + 1; 378 heap [k]->active = k + 1;
371 k = j; 379 k = j;
372 } 380 }
373 381
374 timers [k] = w; 382 heap [k] = w;
375 timers [k]->active = k + 1; 383 heap [k]->active = k + 1;
376} 384}
377 385
378/*****************************************************************************/ 386/*****************************************************************************/
379 387
380typedef struct 388typedef struct
445 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 453 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 454 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
447#endif 455#endif
448 456
449 ev_io_set (&sigev, sigpipe [0], EV_READ); 457 ev_io_set (&sigev, sigpipe [0], EV_READ);
450 ev_io_start (&sigev); 458 ev_io_start (EV_A_ &sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */ 459 ev_unref (EV_A); /* child watcher should not keep loop alive */
452} 460}
453 461
454/*****************************************************************************/ 462/*****************************************************************************/
455 463
534ev_method (EV_P) 542ev_method (EV_P)
535{ 543{
536 return method; 544 return method;
537} 545}
538 546
539int 547inline int
540ev_init (EV_P_ int methods) 548loop_init (EV_P_ int methods)
541{ 549{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
546 if (!method) 550 if (!method)
547 { 551 {
548#if EV_USE_MONOTONIC 552#if EV_USE_MONOTONIC
549 { 553 {
550 struct timespec ts; 554 struct timespec ts;
554#endif 558#endif
555 559
556 rt_now = ev_time (); 560 rt_now = ev_time ();
557 mn_now = get_clock (); 561 mn_now = get_clock ();
558 now_floor = mn_now; 562 now_floor = mn_now;
559 diff = rt_now - mn_now; 563 rtmn_diff = rt_now - mn_now;
560 564
561 if (pipe (sigpipe)) 565 if (pipe (sigpipe))
562 return 0; 566 return 0;
563 567
564 if (methods == EVMETHOD_AUTO) 568 if (methods == EVMETHOD_AUTO)
597 } 601 }
598 602
599 return method; 603 return method;
600} 604}
601 605
606#if EV_MULTIPLICITY
607
608struct ev_loop *
609ev_loop_new (int methods)
610{
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
612
613 if (loop_init (EV_A_ methods))
614 return loop;
615
616 ev_loop_delete (loop);
617
618 return 0;
619}
620
621void
622ev_loop_delete (EV_P)
623{
624 /*TODO*/
625 free (loop);
626}
627
628#else
629
630int
631ev_init (int methods)
632{
633 return loop_init (methods);
634}
635
636#endif
637
602/*****************************************************************************/ 638/*****************************************************************************/
603 639
604void 640void
605ev_fork_prepare (void) 641ev_fork_prepare (void)
606{ 642{
614} 650}
615 651
616void 652void
617ev_fork_child (void) 653ev_fork_child (void)
618{ 654{
655 /*TODO*/
656#if !EV_MULTIPLICITY
619#if EV_USE_EPOLL 657#if EV_USE_EPOLL
620 if (method == EVMETHOD_EPOLL) 658 if (method == EVMETHOD_EPOLL)
621 epoll_postfork_child (); 659 epoll_postfork_child (EV_A);
622#endif 660#endif
623 661
624 ev_io_stop (&sigev); 662 ev_io_stop (EV_A_ &sigev);
625 close (sigpipe [0]); 663 close (sigpipe [0]);
626 close (sigpipe [1]); 664 close (sigpipe [1]);
627 pipe (sigpipe); 665 pipe (sigpipe);
628 siginit (); 666 siginit (EV_A);
667#endif
629} 668}
630 669
631/*****************************************************************************/ 670/*****************************************************************************/
632 671
633static void 672static void
663 downheap ((WT *)timers, timercnt, 0); 702 downheap ((WT *)timers, timercnt, 0);
664 } 703 }
665 else 704 else
666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 705 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
667 706
668 event ((W)w, EV_TIMEOUT); 707 event (EV_A_ (W)w, EV_TIMEOUT);
669 } 708 }
670} 709}
671 710
672static void 711static void
673periodics_reify (EV_P) 712periodics_reify (EV_P)
689 event (EV_A_ (W)w, EV_PERIODIC); 728 event (EV_A_ (W)w, EV_PERIODIC);
690 } 729 }
691} 730}
692 731
693static void 732static void
694periodics_reschedule (EV_P_ ev_tstamp diff) 733periodics_reschedule (EV_P)
695{ 734{
696 int i; 735 int i;
697 736
698 /* adjust periodics after time jump */ 737 /* adjust periodics after time jump */
699 for (i = 0; i < periodiccnt; ++i) 738 for (i = 0; i < periodiccnt; ++i)
720{ 759{
721 mn_now = get_clock (); 760 mn_now = get_clock ();
722 761
723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 762 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
724 { 763 {
725 rt_now = mn_now + diff; 764 rt_now = rtmn_diff + mn_now;
726 return 0; 765 return 0;
727 } 766 }
728 else 767 else
729 { 768 {
730 now_floor = mn_now; 769 now_floor = mn_now;
741#if EV_USE_MONOTONIC 780#if EV_USE_MONOTONIC
742 if (expect_true (have_monotonic)) 781 if (expect_true (have_monotonic))
743 { 782 {
744 if (time_update_monotonic (EV_A)) 783 if (time_update_monotonic (EV_A))
745 { 784 {
746 ev_tstamp odiff = diff; 785 ev_tstamp odiff = rtmn_diff;
747 786
748 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 787 for (i = 4; --i; ) /* loop a few times, before making important decisions */
749 { 788 {
750 diff = rt_now - mn_now; 789 rtmn_diff = rt_now - mn_now;
751 790
752 if (fabs (odiff - diff) < MIN_TIMEJUMP) 791 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
753 return; /* all is well */ 792 return; /* all is well */
754 793
755 rt_now = ev_time (); 794 rt_now = ev_time ();
756 mn_now = get_clock (); 795 mn_now = get_clock ();
757 now_floor = mn_now; 796 now_floor = mn_now;
758 } 797 }
759 798
760 periodics_reschedule (EV_A_ diff - odiff); 799 periodics_reschedule (EV_A);
761 /* no timer adjustment, as the monotonic clock doesn't jump */ 800 /* no timer adjustment, as the monotonic clock doesn't jump */
801 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
762 } 802 }
763 } 803 }
764 else 804 else
765#endif 805#endif
766 { 806 {
767 rt_now = ev_time (); 807 rt_now = ev_time ();
768 808
769 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 809 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
770 { 810 {
771 periodics_reschedule (EV_A_ rt_now - mn_now); 811 periodics_reschedule (EV_A);
772 812
773 /* adjust timers. this is easy, as the offset is the same for all */ 813 /* adjust timers. this is easy, as the offset is the same for all */
774 for (i = 0; i < timercnt; ++i) 814 for (i = 0; i < timercnt; ++i)
775 timers [i]->at += diff; 815 timers [i]->at += rt_now - mn_now;
776 } 816 }
777 817
778 mn_now = rt_now; 818 mn_now = rt_now;
779 } 819 }
780} 820}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines