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

Comparing libev/ev.c (file contents):
Revision 1.51 by root, Sat Nov 3 21:58:51 2007 UTC vs.
Revision 1.59 by root, Sun Nov 4 18:15:16 2007 UTC

58 58
59#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
61#endif 61#endif
62 62
63#ifndef EV_USEV_POLL 63#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 65#endif
66 66
67#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
69#endif 69#endif
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 ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ 118static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119static ev_tstamp rt_now;
120static int method;
121 119
122static int have_monotonic; /* runtime */ 120/*****************************************************************************/
123 121
124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 122typedef struct
125static void (*method_modify)(EV_P_ int fd, int oev, int nev); 123{
126static void (*method_poll)(EV_P_ ev_tstamp timeout); 124 struct ev_watcher_list *head;
125 unsigned char events;
126 unsigned char reify;
127} ANFD;
127 128
128static int activecnt; /* number of active events */ 129typedef struct
130{
131 W w;
132 int events;
133} ANPENDING;
129 134
130#if EV_USE_SELECT 135#if EV_MULTIPLICITY
131static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
132static int vec_max;
133#endif
134 136
135#if EV_USEV_POLL 137struct ev_loop
136static struct pollfd *polls; 138{
137static int pollmax, pollcnt; 139# define VAR(name,decl) decl;
138static int *pollidxs; /* maps fds into structure indices */ 140# include "ev_vars.h"
139static int pollidxmax; 141};
140#endif 142# undef VAR
143# include "ev_wrap.h"
141 144
142#if EV_USE_EPOLL 145#else
143static int epoll_fd = -1;
144 146
145static struct epoll_event *events; 147# define VAR(name,decl) static decl;
146static int eventmax; 148# include "ev_vars.h"
147#endif 149# undef VAR
148 150
149#if EV_USE_KQUEUE
150static int kqueue_fd;
151static struct kevent *kqueue_changes;
152static int kqueue_changemax, kqueue_changecnt;
153static struct kevent *kqueue_events;
154static int kqueue_eventmax;
155#endif 151#endif
156 152
157/*****************************************************************************/ 153/*****************************************************************************/
158 154
159inline ev_tstamp 155inline ev_tstamp
208 cur = newcnt; \ 204 cur = newcnt; \
209 } 205 }
210 206
211/*****************************************************************************/ 207/*****************************************************************************/
212 208
213typedef struct
214{
215 struct ev_watcher_list *head;
216 unsigned char events;
217 unsigned char reify;
218} ANFD;
219
220static ANFD *anfds;
221static int anfdmax;
222
223static void 209static void
224anfds_init (ANFD *base, int count) 210anfds_init (ANFD *base, int count)
225{ 211{
226 while (count--) 212 while (count--)
227 { 213 {
230 base->reify = 0; 216 base->reify = 0;
231 217
232 ++base; 218 ++base;
233 } 219 }
234} 220}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 221
245static void 222static void
246event (EV_P_ W w, int events) 223event (EV_P_ W w, int events)
247{ 224{
248 if (w->pending) 225 if (w->pending)
280 event (EV_A_ (W)w, ev); 257 event (EV_A_ (W)w, ev);
281 } 258 }
282} 259}
283 260
284/*****************************************************************************/ 261/*****************************************************************************/
285
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288 262
289static void 263static void
290fd_reify (EV_P) 264fd_reify (EV_P)
291{ 265{
292 int i; 266 int i;
364 fd_kill (EV_A_ fd); 338 fd_kill (EV_A_ fd);
365 return; 339 return;
366 } 340 }
367} 341}
368 342
343/* susually called after fork if method needs to re-arm all fds from scratch */
344static void
345fd_rearm_all (EV_P)
346{
347 int fd;
348
349 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events)
352 {
353 anfds [fd].events = 0;
354 fd_change (fd);
355 }
356}
357
369/*****************************************************************************/ 358/*****************************************************************************/
370 359
371static struct ev_timer **timers;
372static int timermax, timercnt;
373
374static struct ev_periodic **periodics;
375static int periodicmax, periodiccnt;
376
377static void 360static void
378upheap (WT *timers, int k) 361upheap (WT *heap, int k)
379{ 362{
380 WT w = timers [k]; 363 WT w = heap [k];
381 364
382 while (k && timers [k >> 1]->at > w->at) 365 while (k && heap [k >> 1]->at > w->at)
383 { 366 {
384 timers [k] = timers [k >> 1]; 367 heap [k] = heap [k >> 1];
385 timers [k]->active = k + 1; 368 heap [k]->active = k + 1;
386 k >>= 1; 369 k >>= 1;
387 } 370 }
388 371
389 timers [k] = w; 372 heap [k] = w;
390 timers [k]->active = k + 1; 373 heap [k]->active = k + 1;
391 374
392} 375}
393 376
394static void 377static void
395downheap (WT *timers, int N, int k) 378downheap (WT *heap, int N, int k)
396{ 379{
397 WT w = timers [k]; 380 WT w = heap [k];
398 381
399 while (k < (N >> 1)) 382 while (k < (N >> 1))
400 { 383 {
401 int j = k << 1; 384 int j = k << 1;
402 385
403 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 386 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
404 ++j; 387 ++j;
405 388
406 if (w->at <= timers [j]->at) 389 if (w->at <= heap [j]->at)
407 break; 390 break;
408 391
409 timers [k] = timers [j]; 392 heap [k] = heap [j];
410 timers [k]->active = k + 1; 393 heap [k]->active = k + 1;
411 k = j; 394 k = j;
412 } 395 }
413 396
414 timers [k] = w; 397 heap [k] = w;
415 timers [k]->active = k + 1; 398 heap [k]->active = k + 1;
416} 399}
417 400
418/*****************************************************************************/ 401/*****************************************************************************/
419 402
420typedef struct 403typedef struct
486 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 469 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
487 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 470 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
488#endif 471#endif
489 472
490 ev_io_set (&sigev, sigpipe [0], EV_READ); 473 ev_io_set (&sigev, sigpipe [0], EV_READ);
491 ev_io_start (&sigev); 474 ev_io_start (EV_A_ &sigev);
475 ev_unref (EV_A); /* child watcher should not keep loop alive */
492} 476}
493 477
494/*****************************************************************************/ 478/*****************************************************************************/
495 479
496static struct ev_idle **idles; 480#ifndef WIN32
497static int idlemax, idlecnt;
498
499static struct ev_prepare **prepares;
500static int preparemax, preparecnt;
501
502static struct ev_check **checks;
503static int checkmax, checkcnt;
504
505/*****************************************************************************/
506 481
507static struct ev_child *childs [PID_HASHSIZE]; 482static struct ev_child *childs [PID_HASHSIZE];
508static struct ev_signal childev; 483static struct ev_signal childev;
509
510#ifndef WIN32
511 484
512#ifndef WCONTINUED 485#ifndef WCONTINUED
513# define WCONTINUED 0 486# define WCONTINUED 0
514#endif 487#endif
515 488
551# include "ev_kqueue.c" 524# include "ev_kqueue.c"
552#endif 525#endif
553#if EV_USE_EPOLL 526#if EV_USE_EPOLL
554# include "ev_epoll.c" 527# include "ev_epoll.c"
555#endif 528#endif
556#if EV_USEV_POLL 529#if EV_USE_POLL
557# include "ev_poll.c" 530# include "ev_poll.c"
558#endif 531#endif
559#if EV_USE_SELECT 532#if EV_USE_SELECT
560# include "ev_select.c" 533# include "ev_select.c"
561#endif 534#endif
588ev_method (EV_P) 561ev_method (EV_P)
589{ 562{
590 return method; 563 return method;
591} 564}
592 565
593int 566static void
594ev_init (EV_P_ int methods) 567loop_init (EV_P_ int methods)
595{ 568{
596 if (!method) 569 if (!method)
597 { 570 {
598#if EV_USE_MONOTONIC 571#if EV_USE_MONOTONIC
599 { 572 {
604#endif 577#endif
605 578
606 rt_now = ev_time (); 579 rt_now = ev_time ();
607 mn_now = get_clock (); 580 mn_now = get_clock ();
608 now_floor = mn_now; 581 now_floor = mn_now;
609 diff = rt_now - mn_now; 582 rtmn_diff = rt_now - mn_now;
610
611 if (pipe (sigpipe))
612 return 0;
613 583
614 if (methods == EVMETHOD_AUTO) 584 if (methods == EVMETHOD_AUTO)
615 if (!enable_secure () && getenv ("LIBmethodS")) 585 if (!enable_secure () && getenv ("LIBEV_METHODS"))
616 methods = atoi (getenv ("LIBmethodS")); 586 methods = atoi (getenv ("LIBEV_METHODS"));
617 else 587 else
618 methods = EVMETHOD_ANY; 588 methods = EVMETHOD_ANY;
619 589
620 method = 0; 590 method = 0;
621#if EV_USE_KQUEUE 591#if EV_USE_KQUEUE
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 592 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
623#endif 593#endif
624#if EV_USE_EPOLL 594#if EV_USE_EPOLL
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 595 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626#endif 596#endif
627#if EV_USEV_POLL 597#if EV_USE_POLL
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 598 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
629#endif 599#endif
630#if EV_USE_SELECT 600#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 601 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
632#endif 602#endif
603 }
604}
633 605
606void
607loop_destroy (EV_P)
608{
609#if EV_USE_KQUEUE
610 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
611#endif
612#if EV_USE_EPOLL
613 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
614#endif
615#if EV_USE_POLL
616 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
617#endif
618#if EV_USE_SELECT
619 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
620#endif
621
622 method = 0;
623 /*TODO*/
624}
625
626void
627loop_fork (EV_P)
628{
629 /*TODO*/
630#if EV_USE_EPOLL
631 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
632#endif
633#if EV_USE_KQUEUE
634 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
635#endif
636}
637
638#if EV_MULTIPLICITY
639struct ev_loop *
640ev_loop_new (int methods)
641{
642 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
643
644 loop_init (EV_A_ methods);
645
646 if (ev_methods (EV_A))
647 return loop;
648
649 return 0;
650}
651
652void
653ev_loop_destroy (EV_P)
654{
655 loop_destroy (EV_A);
656 free (loop);
657}
658
659void
660ev_loop_fork (EV_P)
661{
662 loop_fork (EV_A);
663}
664
665#endif
666
667#if EV_MULTIPLICITY
668struct ev_loop default_loop_struct;
669static struct ev_loop *default_loop;
670
671struct ev_loop *
672#else
673static int default_loop;
674
675int
676#endif
677ev_default_loop (int methods)
678{
679 if (sigpipe [0] == sigpipe [1])
680 if (pipe (sigpipe))
681 return 0;
682
683 if (!default_loop)
684 {
685#if EV_MULTIPLICITY
686 struct ev_loop *loop = default_loop = &default_loop_struct;
687#else
688 default_loop = 1;
689#endif
690
691 loop_init (EV_A_ methods);
692
634 if (method) 693 if (ev_method (EV_A))
635 { 694 {
636 ev_watcher_init (&sigev, sigcb); 695 ev_watcher_init (&sigev, sigcb);
637 ev_set_priority (&sigev, EV_MAXPRI); 696 ev_set_priority (&sigev, EV_MAXPRI);
638 siginit (EV_A); 697 siginit (EV_A);
639 698
640#ifndef WIN32 699#ifndef WIN32
641 ev_signal_init (&childev, childcb, SIGCHLD); 700 ev_signal_init (&childev, childcb, SIGCHLD);
642 ev_set_priority (&childev, EV_MAXPRI); 701 ev_set_priority (&childev, EV_MAXPRI);
643 ev_signal_start (EV_A_ &childev); 702 ev_signal_start (EV_A_ &childev);
703 ev_unref (EV_A); /* child watcher should not keep loop alive */
644#endif 704#endif
645 } 705 }
706 else
707 default_loop = 0;
646 } 708 }
647 709
648 return method; 710 return default_loop;
649} 711}
650 712
651/*****************************************************************************/
652
653void 713void
654ev_fork_prepare (void) 714ev_default_destroy (void)
655{ 715{
656 /* nop */ 716#if EV_MULTIPLICITY
657} 717 struct ev_loop *loop = default_loop;
658
659void
660ev_fork_parent (void)
661{
662 /* nop */
663}
664
665void
666ev_fork_child (void)
667{
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL)
670 epoll_postfork_child ();
671#endif 718#endif
672 719
720 ev_ref (EV_A); /* child watcher */
721 ev_signal_stop (EV_A_ &childev);
722
723 ev_ref (EV_A); /* signal watcher */
673 ev_io_stop (&sigev); 724 ev_io_stop (EV_A_ &sigev);
725
726 close (sigpipe [0]); sigpipe [0] = 0;
727 close (sigpipe [1]); sigpipe [1] = 0;
728
729 loop_destroy (EV_A);
730}
731
732void
733ev_default_fork (EV_P)
734{
735 loop_fork (EV_A);
736
737 ev_io_stop (EV_A_ &sigev);
674 close (sigpipe [0]); 738 close (sigpipe [0]);
675 close (sigpipe [1]); 739 close (sigpipe [1]);
676 pipe (sigpipe); 740 pipe (sigpipe);
741
742 ev_ref (EV_A); /* signal watcher */
677 siginit (); 743 siginit (EV_A);
678} 744}
679 745
680/*****************************************************************************/ 746/*****************************************************************************/
681 747
682static void 748static void
712 downheap ((WT *)timers, timercnt, 0); 778 downheap ((WT *)timers, timercnt, 0);
713 } 779 }
714 else 780 else
715 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 781 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
716 782
717 event ((W)w, EV_TIMEOUT); 783 event (EV_A_ (W)w, EV_TIMEOUT);
718 } 784 }
719} 785}
720 786
721static void 787static void
722periodics_reify (EV_P) 788periodics_reify (EV_P)
738 event (EV_A_ (W)w, EV_PERIODIC); 804 event (EV_A_ (W)w, EV_PERIODIC);
739 } 805 }
740} 806}
741 807
742static void 808static void
743periodics_reschedule (EV_P_ ev_tstamp diff) 809periodics_reschedule (EV_P)
744{ 810{
745 int i; 811 int i;
746 812
747 /* adjust periodics after time jump */ 813 /* adjust periodics after time jump */
748 for (i = 0; i < periodiccnt; ++i) 814 for (i = 0; i < periodiccnt; ++i)
769{ 835{
770 mn_now = get_clock (); 836 mn_now = get_clock ();
771 837
772 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 838 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
773 { 839 {
774 rt_now = mn_now + diff; 840 rt_now = rtmn_diff + mn_now;
775 return 0; 841 return 0;
776 } 842 }
777 else 843 else
778 { 844 {
779 now_floor = mn_now; 845 now_floor = mn_now;
790#if EV_USE_MONOTONIC 856#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic)) 857 if (expect_true (have_monotonic))
792 { 858 {
793 if (time_update_monotonic (EV_A)) 859 if (time_update_monotonic (EV_A))
794 { 860 {
795 ev_tstamp odiff = diff; 861 ev_tstamp odiff = rtmn_diff;
796 862
797 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 863 for (i = 4; --i; ) /* loop a few times, before making important decisions */
798 { 864 {
799 diff = rt_now - mn_now; 865 rtmn_diff = rt_now - mn_now;
800 866
801 if (fabs (odiff - diff) < MIN_TIMEJUMP) 867 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
802 return; /* all is well */ 868 return; /* all is well */
803 869
804 rt_now = ev_time (); 870 rt_now = ev_time ();
805 mn_now = get_clock (); 871 mn_now = get_clock ();
806 now_floor = mn_now; 872 now_floor = mn_now;
807 } 873 }
808 874
809 periodics_reschedule (EV_A_ diff - odiff); 875 periodics_reschedule (EV_A);
810 /* no timer adjustment, as the monotonic clock doesn't jump */ 876 /* no timer adjustment, as the monotonic clock doesn't jump */
877 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
811 } 878 }
812 } 879 }
813 else 880 else
814#endif 881#endif
815 { 882 {
816 rt_now = ev_time (); 883 rt_now = ev_time ();
817 884
818 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 885 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
819 { 886 {
820 periodics_reschedule (EV_A_ rt_now - mn_now); 887 periodics_reschedule (EV_A);
821 888
822 /* adjust timers. this is easy, as the offset is the same for all */ 889 /* adjust timers. this is easy, as the offset is the same for all */
823 for (i = 0; i < timercnt; ++i) 890 for (i = 0; i < timercnt; ++i)
824 timers [i]->at += diff; 891 timers [i]->at += rt_now - mn_now;
825 } 892 }
826 893
827 mn_now = rt_now; 894 mn_now = rt_now;
828 } 895 }
829} 896}
911 /* queue check watchers, to be executed first */ 978 /* queue check watchers, to be executed first */
912 if (checkcnt) 979 if (checkcnt)
913 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 980 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
914 981
915 call_pending (EV_A); 982 call_pending (EV_A);
916 printf ("activecnt %d\n", activecnt);//D
917 } 983 }
918 while (activecnt && !loop_done); 984 while (activecnt && !loop_done);
919 985
920 if (loop_done != 2) 986 if (loop_done != 2)
921 loop_done = 0; 987 loop_done = 0;
1093 } 1159 }
1094 1160
1095 ev_stop (EV_A_ (W)w); 1161 ev_stop (EV_A_ (W)w);
1096} 1162}
1097 1163
1164void
1165ev_idle_start (EV_P_ struct ev_idle *w)
1166{
1167 if (ev_is_active (w))
1168 return;
1169
1170 ev_start (EV_A_ (W)w, ++idlecnt);
1171 array_needsize (idles, idlemax, idlecnt, );
1172 idles [idlecnt - 1] = w;
1173}
1174
1175void
1176ev_idle_stop (EV_P_ struct ev_idle *w)
1177{
1178 ev_clear_pending (EV_A_ (W)w);
1179 if (ev_is_active (w))
1180 return;
1181
1182 idles [w->active - 1] = idles [--idlecnt];
1183 ev_stop (EV_A_ (W)w);
1184}
1185
1186void
1187ev_prepare_start (EV_P_ struct ev_prepare *w)
1188{
1189 if (ev_is_active (w))
1190 return;
1191
1192 ev_start (EV_A_ (W)w, ++preparecnt);
1193 array_needsize (prepares, preparemax, preparecnt, );
1194 prepares [preparecnt - 1] = w;
1195}
1196
1197void
1198ev_prepare_stop (EV_P_ struct ev_prepare *w)
1199{
1200 ev_clear_pending (EV_A_ (W)w);
1201 if (ev_is_active (w))
1202 return;
1203
1204 prepares [w->active - 1] = prepares [--preparecnt];
1205 ev_stop (EV_A_ (W)w);
1206}
1207
1208void
1209ev_check_start (EV_P_ struct ev_check *w)
1210{
1211 if (ev_is_active (w))
1212 return;
1213
1214 ev_start (EV_A_ (W)w, ++checkcnt);
1215 array_needsize (checks, checkmax, checkcnt, );
1216 checks [checkcnt - 1] = w;
1217}
1218
1219void
1220ev_check_stop (EV_P_ struct ev_check *w)
1221{
1222 ev_clear_pending (EV_A_ (W)w);
1223 if (ev_is_active (w))
1224 return;
1225
1226 checks [w->active - 1] = checks [--checkcnt];
1227 ev_stop (EV_A_ (W)w);
1228}
1229
1098#ifndef SA_RESTART 1230#ifndef SA_RESTART
1099# define SA_RESTART 0 1231# define SA_RESTART 0
1100#endif 1232#endif
1101 1233
1102void 1234void
1103ev_signal_start (EV_P_ struct ev_signal *w) 1235ev_signal_start (EV_P_ struct ev_signal *w)
1104{ 1236{
1237#if EV_MULTIPLICITY
1238 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1239#endif
1105 if (ev_is_active (w)) 1240 if (ev_is_active (w))
1106 return; 1241 return;
1107 1242
1108 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1243 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1109 1244
1134 if (!signals [w->signum - 1].head) 1269 if (!signals [w->signum - 1].head)
1135 signal (w->signum, SIG_DFL); 1270 signal (w->signum, SIG_DFL);
1136} 1271}
1137 1272
1138void 1273void
1139ev_idle_start (EV_P_ struct ev_idle *w)
1140{
1141 if (ev_is_active (w))
1142 return;
1143
1144 ev_start (EV_A_ (W)w, ++idlecnt);
1145 array_needsize (idles, idlemax, idlecnt, );
1146 idles [idlecnt - 1] = w;
1147}
1148
1149void
1150ev_idle_stop (EV_P_ struct ev_idle *w)
1151{
1152 ev_clear_pending (EV_A_ (W)w);
1153 if (ev_is_active (w))
1154 return;
1155
1156 idles [w->active - 1] = idles [--idlecnt];
1157 ev_stop (EV_A_ (W)w);
1158}
1159
1160void
1161ev_prepare_start (EV_P_ struct ev_prepare *w)
1162{
1163 if (ev_is_active (w))
1164 return;
1165
1166 ev_start (EV_A_ (W)w, ++preparecnt);
1167 array_needsize (prepares, preparemax, preparecnt, );
1168 prepares [preparecnt - 1] = w;
1169}
1170
1171void
1172ev_prepare_stop (EV_P_ struct ev_prepare *w)
1173{
1174 ev_clear_pending (EV_A_ (W)w);
1175 if (ev_is_active (w))
1176 return;
1177
1178 prepares [w->active - 1] = prepares [--preparecnt];
1179 ev_stop (EV_A_ (W)w);
1180}
1181
1182void
1183ev_check_start (EV_P_ struct ev_check *w)
1184{
1185 if (ev_is_active (w))
1186 return;
1187
1188 ev_start (EV_A_ (W)w, ++checkcnt);
1189 array_needsize (checks, checkmax, checkcnt, );
1190 checks [checkcnt - 1] = w;
1191}
1192
1193void
1194ev_check_stop (EV_P_ struct ev_check *w)
1195{
1196 ev_clear_pending (EV_A_ (W)w);
1197 if (ev_is_active (w))
1198 return;
1199
1200 checks [w->active - 1] = checks [--checkcnt];
1201 ev_stop (EV_A_ (W)w);
1202}
1203
1204void
1205ev_child_start (EV_P_ struct ev_child *w) 1274ev_child_start (EV_P_ struct ev_child *w)
1206{ 1275{
1276#if EV_MULTIPLICITY
1277 assert (("child watchers are only supported in the default loop", loop == default_loop));
1278#endif
1207 if (ev_is_active (w)) 1279 if (ev_is_active (w))
1208 return; 1280 return;
1209 1281
1210 ev_start (EV_A_ (W)w, 1); 1282 ev_start (EV_A_ (W)w, 1);
1211 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1283 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1283 ev_timer_start (EV_A_ &once->to); 1355 ev_timer_start (EV_A_ &once->to);
1284 } 1356 }
1285 } 1357 }
1286} 1358}
1287 1359
1288/*****************************************************************************/
1289
1290#if 0
1291
1292struct ev_io wio;
1293
1294static void
1295sin_cb (struct ev_io *w, int revents)
1296{
1297 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1298}
1299
1300static void
1301ocb (struct ev_timer *w, int revents)
1302{
1303 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1304 ev_timer_stop (w);
1305 ev_timer_start (w);
1306}
1307
1308static void
1309scb (struct ev_signal *w, int revents)
1310{
1311 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1312 ev_io_stop (&wio);
1313 ev_io_start (&wio);
1314}
1315
1316static void
1317gcb (struct ev_signal *w, int revents)
1318{
1319 fprintf (stderr, "generic %x\n", revents);
1320
1321}
1322
1323int main (void)
1324{
1325 ev_init (0);
1326
1327 ev_io_init (&wio, sin_cb, 0, EV_READ);
1328 ev_io_start (&wio);
1329
1330 struct ev_timer t[10000];
1331
1332#if 0
1333 int i;
1334 for (i = 0; i < 10000; ++i)
1335 {
1336 struct ev_timer *w = t + i;
1337 ev_watcher_init (w, ocb, i);
1338 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1339 ev_timer_start (w);
1340 if (drand48 () < 0.5)
1341 ev_timer_stop (w);
1342 }
1343#endif
1344
1345 struct ev_timer t1;
1346 ev_timer_init (&t1, ocb, 5, 10);
1347 ev_timer_start (&t1);
1348
1349 struct ev_signal sig;
1350 ev_signal_init (&sig, scb, SIGQUIT);
1351 ev_signal_start (&sig);
1352
1353 struct ev_check cw;
1354 ev_check_init (&cw, gcb);
1355 ev_check_start (&cw);
1356
1357 struct ev_idle iw;
1358 ev_idle_init (&iw, gcb);
1359 ev_idle_start (&iw);
1360
1361 ev_loop (0);
1362
1363 return 0;
1364}
1365
1366#endif
1367
1368
1369
1370

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines