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.60 by root, Sun Nov 4 18:29:44 2007 UTC

28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
113 135
114typedef struct ev_watcher *W; 136typedef struct ev_watcher *W;
115typedef struct ev_watcher_list *WL; 137typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 138typedef struct ev_watcher_time *WT;
117 139
118static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ 140static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119static ev_tstamp rt_now;
120static int method;
121 141
122static int have_monotonic; /* runtime */ 142/*****************************************************************************/
123 143
124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 144typedef struct
125static void (*method_modify)(EV_P_ int fd, int oev, int nev); 145{
126static void (*method_poll)(EV_P_ ev_tstamp timeout); 146 struct ev_watcher_list *head;
147 unsigned char events;
148 unsigned char reify;
149} ANFD;
127 150
128static int activecnt; /* number of active events */ 151typedef struct
152{
153 W w;
154 int events;
155} ANPENDING;
129 156
130#if EV_USE_SELECT 157#if EV_MULTIPLICITY
131static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
132static int vec_max;
133#endif
134 158
135#if EV_USEV_POLL 159struct ev_loop
136static struct pollfd *polls; 160{
137static int pollmax, pollcnt; 161# define VAR(name,decl) decl;
138static int *pollidxs; /* maps fds into structure indices */ 162# include "ev_vars.h"
139static int pollidxmax; 163};
140#endif 164# undef VAR
165# include "ev_wrap.h"
141 166
142#if EV_USE_EPOLL 167#else
143static int epoll_fd = -1;
144 168
145static struct epoll_event *events; 169# define VAR(name,decl) static decl;
146static int eventmax; 170# include "ev_vars.h"
147#endif 171# undef VAR
148 172
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 173#endif
156 174
157/*****************************************************************************/ 175/*****************************************************************************/
158 176
159inline ev_tstamp 177inline ev_tstamp
208 cur = newcnt; \ 226 cur = newcnt; \
209 } 227 }
210 228
211/*****************************************************************************/ 229/*****************************************************************************/
212 230
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 231static void
224anfds_init (ANFD *base, int count) 232anfds_init (ANFD *base, int count)
225{ 233{
226 while (count--) 234 while (count--)
227 { 235 {
230 base->reify = 0; 238 base->reify = 0;
231 239
232 ++base; 240 ++base;
233 } 241 }
234} 242}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 243
245static void 244static void
246event (EV_P_ W w, int events) 245event (EV_P_ W w, int events)
247{ 246{
248 if (w->pending) 247 if (w->pending)
280 event (EV_A_ (W)w, ev); 279 event (EV_A_ (W)w, ev);
281 } 280 }
282} 281}
283 282
284/*****************************************************************************/ 283/*****************************************************************************/
285
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288 284
289static void 285static void
290fd_reify (EV_P) 286fd_reify (EV_P)
291{ 287{
292 int i; 288 int i;
364 fd_kill (EV_A_ fd); 360 fd_kill (EV_A_ fd);
365 return; 361 return;
366 } 362 }
367} 363}
368 364
365/* susually called after fork if method needs to re-arm all fds from scratch */
366static void
367fd_rearm_all (EV_P)
368{
369 int fd;
370
371 /* this should be highly optimised to not do anything but set a flag */
372 for (fd = 0; fd < anfdmax; ++fd)
373 if (anfds [fd].events)
374 {
375 anfds [fd].events = 0;
376 fd_change (EV_A_ fd);
377 }
378}
379
369/*****************************************************************************/ 380/*****************************************************************************/
370 381
371static struct ev_timer **timers;
372static int timermax, timercnt;
373
374static struct ev_periodic **periodics;
375static int periodicmax, periodiccnt;
376
377static void 382static void
378upheap (WT *timers, int k) 383upheap (WT *heap, int k)
379{ 384{
380 WT w = timers [k]; 385 WT w = heap [k];
381 386
382 while (k && timers [k >> 1]->at > w->at) 387 while (k && heap [k >> 1]->at > w->at)
383 { 388 {
384 timers [k] = timers [k >> 1]; 389 heap [k] = heap [k >> 1];
385 timers [k]->active = k + 1; 390 heap [k]->active = k + 1;
386 k >>= 1; 391 k >>= 1;
387 } 392 }
388 393
389 timers [k] = w; 394 heap [k] = w;
390 timers [k]->active = k + 1; 395 heap [k]->active = k + 1;
391 396
392} 397}
393 398
394static void 399static void
395downheap (WT *timers, int N, int k) 400downheap (WT *heap, int N, int k)
396{ 401{
397 WT w = timers [k]; 402 WT w = heap [k];
398 403
399 while (k < (N >> 1)) 404 while (k < (N >> 1))
400 { 405 {
401 int j = k << 1; 406 int j = k << 1;
402 407
403 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 408 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
404 ++j; 409 ++j;
405 410
406 if (w->at <= timers [j]->at) 411 if (w->at <= heap [j]->at)
407 break; 412 break;
408 413
409 timers [k] = timers [j]; 414 heap [k] = heap [j];
410 timers [k]->active = k + 1; 415 heap [k]->active = k + 1;
411 k = j; 416 k = j;
412 } 417 }
413 418
414 timers [k] = w; 419 heap [k] = w;
415 timers [k]->active = k + 1; 420 heap [k]->active = k + 1;
416} 421}
417 422
418/*****************************************************************************/ 423/*****************************************************************************/
419 424
420typedef struct 425typedef struct
486 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 491 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
487 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 492 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
488#endif 493#endif
489 494
490 ev_io_set (&sigev, sigpipe [0], EV_READ); 495 ev_io_set (&sigev, sigpipe [0], EV_READ);
491 ev_io_start (&sigev); 496 ev_io_start (EV_A_ &sigev);
497 ev_unref (EV_A); /* child watcher should not keep loop alive */
492} 498}
493 499
494/*****************************************************************************/ 500/*****************************************************************************/
495 501
496static struct ev_idle **idles; 502#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 503
507static struct ev_child *childs [PID_HASHSIZE]; 504static struct ev_child *childs [PID_HASHSIZE];
508static struct ev_signal childev; 505static struct ev_signal childev;
509
510#ifndef WIN32
511 506
512#ifndef WCONTINUED 507#ifndef WCONTINUED
513# define WCONTINUED 0 508# define WCONTINUED 0
514#endif 509#endif
515 510
551# include "ev_kqueue.c" 546# include "ev_kqueue.c"
552#endif 547#endif
553#if EV_USE_EPOLL 548#if EV_USE_EPOLL
554# include "ev_epoll.c" 549# include "ev_epoll.c"
555#endif 550#endif
556#if EV_USEV_POLL 551#if EV_USE_POLL
557# include "ev_poll.c" 552# include "ev_poll.c"
558#endif 553#endif
559#if EV_USE_SELECT 554#if EV_USE_SELECT
560# include "ev_select.c" 555# include "ev_select.c"
561#endif 556#endif
588ev_method (EV_P) 583ev_method (EV_P)
589{ 584{
590 return method; 585 return method;
591} 586}
592 587
593int 588static void
594ev_init (EV_P_ int methods) 589loop_init (EV_P_ int methods)
595{ 590{
596 if (!method) 591 if (!method)
597 { 592 {
598#if EV_USE_MONOTONIC 593#if EV_USE_MONOTONIC
599 { 594 {
604#endif 599#endif
605 600
606 rt_now = ev_time (); 601 rt_now = ev_time ();
607 mn_now = get_clock (); 602 mn_now = get_clock ();
608 now_floor = mn_now; 603 now_floor = mn_now;
609 diff = rt_now - mn_now; 604 rtmn_diff = rt_now - mn_now;
610
611 if (pipe (sigpipe))
612 return 0;
613 605
614 if (methods == EVMETHOD_AUTO) 606 if (methods == EVMETHOD_AUTO)
615 if (!enable_secure () && getenv ("LIBmethodS")) 607 if (!enable_secure () && getenv ("LIBEV_METHODS"))
616 methods = atoi (getenv ("LIBmethodS")); 608 methods = atoi (getenv ("LIBEV_METHODS"));
617 else 609 else
618 methods = EVMETHOD_ANY; 610 methods = EVMETHOD_ANY;
619 611
620 method = 0; 612 method = 0;
621#if EV_USE_KQUEUE 613#if EV_USE_KQUEUE
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
623#endif 615#endif
624#if EV_USE_EPOLL 616#if EV_USE_EPOLL
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626#endif 618#endif
627#if EV_USEV_POLL 619#if EV_USE_POLL
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 620 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
629#endif 621#endif
630#if EV_USE_SELECT 622#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 623 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
632#endif 624#endif
625 }
626}
633 627
628void
629loop_destroy (EV_P)
630{
631#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif
634#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
636#endif
637#if EV_USE_POLL
638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
639#endif
640#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#endif
643
644 method = 0;
645 /*TODO*/
646}
647
648void
649loop_fork (EV_P)
650{
651 /*TODO*/
652#if EV_USE_EPOLL
653 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
654#endif
655#if EV_USE_KQUEUE
656 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
657#endif
658}
659
660#if EV_MULTIPLICITY
661struct ev_loop *
662ev_loop_new (int methods)
663{
664 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
665
666 loop_init (EV_A_ methods);
667
668 if (ev_method (EV_A))
669 return loop;
670
671 return 0;
672}
673
674void
675ev_loop_destroy (EV_P)
676{
677 loop_destroy (EV_A);
678 free (loop);
679}
680
681void
682ev_loop_fork (EV_P)
683{
684 loop_fork (EV_A);
685}
686
687#endif
688
689#if EV_MULTIPLICITY
690struct ev_loop default_loop_struct;
691static struct ev_loop *default_loop;
692
693struct ev_loop *
694#else
695static int default_loop;
696
697int
698#endif
699ev_default_loop (int methods)
700{
701 if (sigpipe [0] == sigpipe [1])
702 if (pipe (sigpipe))
703 return 0;
704
705 if (!default_loop)
706 {
707#if EV_MULTIPLICITY
708 struct ev_loop *loop = default_loop = &default_loop_struct;
709#else
710 default_loop = 1;
711#endif
712
713 loop_init (EV_A_ methods);
714
634 if (method) 715 if (ev_method (EV_A))
635 { 716 {
636 ev_watcher_init (&sigev, sigcb); 717 ev_watcher_init (&sigev, sigcb);
637 ev_set_priority (&sigev, EV_MAXPRI); 718 ev_set_priority (&sigev, EV_MAXPRI);
638 siginit (EV_A); 719 siginit (EV_A);
639 720
640#ifndef WIN32 721#ifndef WIN32
641 ev_signal_init (&childev, childcb, SIGCHLD); 722 ev_signal_init (&childev, childcb, SIGCHLD);
642 ev_set_priority (&childev, EV_MAXPRI); 723 ev_set_priority (&childev, EV_MAXPRI);
643 ev_signal_start (EV_A_ &childev); 724 ev_signal_start (EV_A_ &childev);
725 ev_unref (EV_A); /* child watcher should not keep loop alive */
644#endif 726#endif
645 } 727 }
728 else
729 default_loop = 0;
646 } 730 }
647 731
648 return method; 732 return default_loop;
649} 733}
650 734
651/*****************************************************************************/
652
653void 735void
654ev_fork_prepare (void) 736ev_default_destroy (void)
655{ 737{
656 /* nop */ 738#if EV_MULTIPLICITY
657} 739 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 740#endif
672 741
742 ev_ref (EV_A); /* child watcher */
743 ev_signal_stop (EV_A_ &childev);
744
745 ev_ref (EV_A); /* signal watcher */
673 ev_io_stop (&sigev); 746 ev_io_stop (EV_A_ &sigev);
747
748 close (sigpipe [0]); sigpipe [0] = 0;
749 close (sigpipe [1]); sigpipe [1] = 0;
750
751 loop_destroy (EV_A);
752}
753
754void
755ev_default_fork (void)
756{
757#if EV_MULTIPLICITY
758 struct ev_loop *loop = default_loop;
759#endif
760
761 loop_fork (EV_A);
762
763 ev_io_stop (EV_A_ &sigev);
674 close (sigpipe [0]); 764 close (sigpipe [0]);
675 close (sigpipe [1]); 765 close (sigpipe [1]);
676 pipe (sigpipe); 766 pipe (sigpipe);
767
768 ev_ref (EV_A); /* signal watcher */
677 siginit (); 769 siginit (EV_A);
678} 770}
679 771
680/*****************************************************************************/ 772/*****************************************************************************/
681 773
682static void 774static void
712 downheap ((WT *)timers, timercnt, 0); 804 downheap ((WT *)timers, timercnt, 0);
713 } 805 }
714 else 806 else
715 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 807 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
716 808
717 event ((W)w, EV_TIMEOUT); 809 event (EV_A_ (W)w, EV_TIMEOUT);
718 } 810 }
719} 811}
720 812
721static void 813static void
722periodics_reify (EV_P) 814periodics_reify (EV_P)
738 event (EV_A_ (W)w, EV_PERIODIC); 830 event (EV_A_ (W)w, EV_PERIODIC);
739 } 831 }
740} 832}
741 833
742static void 834static void
743periodics_reschedule (EV_P_ ev_tstamp diff) 835periodics_reschedule (EV_P)
744{ 836{
745 int i; 837 int i;
746 838
747 /* adjust periodics after time jump */ 839 /* adjust periodics after time jump */
748 for (i = 0; i < periodiccnt; ++i) 840 for (i = 0; i < periodiccnt; ++i)
769{ 861{
770 mn_now = get_clock (); 862 mn_now = get_clock ();
771 863
772 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 864 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
773 { 865 {
774 rt_now = mn_now + diff; 866 rt_now = rtmn_diff + mn_now;
775 return 0; 867 return 0;
776 } 868 }
777 else 869 else
778 { 870 {
779 now_floor = mn_now; 871 now_floor = mn_now;
790#if EV_USE_MONOTONIC 882#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic)) 883 if (expect_true (have_monotonic))
792 { 884 {
793 if (time_update_monotonic (EV_A)) 885 if (time_update_monotonic (EV_A))
794 { 886 {
795 ev_tstamp odiff = diff; 887 ev_tstamp odiff = rtmn_diff;
796 888
797 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 889 for (i = 4; --i; ) /* loop a few times, before making important decisions */
798 { 890 {
799 diff = rt_now - mn_now; 891 rtmn_diff = rt_now - mn_now;
800 892
801 if (fabs (odiff - diff) < MIN_TIMEJUMP) 893 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
802 return; /* all is well */ 894 return; /* all is well */
803 895
804 rt_now = ev_time (); 896 rt_now = ev_time ();
805 mn_now = get_clock (); 897 mn_now = get_clock ();
806 now_floor = mn_now; 898 now_floor = mn_now;
807 } 899 }
808 900
809 periodics_reschedule (EV_A_ diff - odiff); 901 periodics_reschedule (EV_A);
810 /* no timer adjustment, as the monotonic clock doesn't jump */ 902 /* no timer adjustment, as the monotonic clock doesn't jump */
903 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
811 } 904 }
812 } 905 }
813 else 906 else
814#endif 907#endif
815 { 908 {
816 rt_now = ev_time (); 909 rt_now = ev_time ();
817 910
818 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 911 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
819 { 912 {
820 periodics_reschedule (EV_A_ rt_now - mn_now); 913 periodics_reschedule (EV_A);
821 914
822 /* adjust timers. this is easy, as the offset is the same for all */ 915 /* adjust timers. this is easy, as the offset is the same for all */
823 for (i = 0; i < timercnt; ++i) 916 for (i = 0; i < timercnt; ++i)
824 timers [i]->at += diff; 917 timers [i]->at += rt_now - mn_now;
825 } 918 }
826 919
827 mn_now = rt_now; 920 mn_now = rt_now;
828 } 921 }
829} 922}
911 /* queue check watchers, to be executed first */ 1004 /* queue check watchers, to be executed first */
912 if (checkcnt) 1005 if (checkcnt)
913 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1006 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
914 1007
915 call_pending (EV_A); 1008 call_pending (EV_A);
916 printf ("activecnt %d\n", activecnt);//D
917 } 1009 }
918 while (activecnt && !loop_done); 1010 while (activecnt && !loop_done);
919 1011
920 if (loop_done != 2) 1012 if (loop_done != 2)
921 loop_done = 0; 1013 loop_done = 0;
1093 } 1185 }
1094 1186
1095 ev_stop (EV_A_ (W)w); 1187 ev_stop (EV_A_ (W)w);
1096} 1188}
1097 1189
1190void
1191ev_idle_start (EV_P_ struct ev_idle *w)
1192{
1193 if (ev_is_active (w))
1194 return;
1195
1196 ev_start (EV_A_ (W)w, ++idlecnt);
1197 array_needsize (idles, idlemax, idlecnt, );
1198 idles [idlecnt - 1] = w;
1199}
1200
1201void
1202ev_idle_stop (EV_P_ struct ev_idle *w)
1203{
1204 ev_clear_pending (EV_A_ (W)w);
1205 if (ev_is_active (w))
1206 return;
1207
1208 idles [w->active - 1] = idles [--idlecnt];
1209 ev_stop (EV_A_ (W)w);
1210}
1211
1212void
1213ev_prepare_start (EV_P_ struct ev_prepare *w)
1214{
1215 if (ev_is_active (w))
1216 return;
1217
1218 ev_start (EV_A_ (W)w, ++preparecnt);
1219 array_needsize (prepares, preparemax, preparecnt, );
1220 prepares [preparecnt - 1] = w;
1221}
1222
1223void
1224ev_prepare_stop (EV_P_ struct ev_prepare *w)
1225{
1226 ev_clear_pending (EV_A_ (W)w);
1227 if (ev_is_active (w))
1228 return;
1229
1230 prepares [w->active - 1] = prepares [--preparecnt];
1231 ev_stop (EV_A_ (W)w);
1232}
1233
1234void
1235ev_check_start (EV_P_ struct ev_check *w)
1236{
1237 if (ev_is_active (w))
1238 return;
1239
1240 ev_start (EV_A_ (W)w, ++checkcnt);
1241 array_needsize (checks, checkmax, checkcnt, );
1242 checks [checkcnt - 1] = w;
1243}
1244
1245void
1246ev_check_stop (EV_P_ struct ev_check *w)
1247{
1248 ev_clear_pending (EV_A_ (W)w);
1249 if (ev_is_active (w))
1250 return;
1251
1252 checks [w->active - 1] = checks [--checkcnt];
1253 ev_stop (EV_A_ (W)w);
1254}
1255
1098#ifndef SA_RESTART 1256#ifndef SA_RESTART
1099# define SA_RESTART 0 1257# define SA_RESTART 0
1100#endif 1258#endif
1101 1259
1102void 1260void
1103ev_signal_start (EV_P_ struct ev_signal *w) 1261ev_signal_start (EV_P_ struct ev_signal *w)
1104{ 1262{
1263#if EV_MULTIPLICITY
1264 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1265#endif
1105 if (ev_is_active (w)) 1266 if (ev_is_active (w))
1106 return; 1267 return;
1107 1268
1108 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1269 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1109 1270
1134 if (!signals [w->signum - 1].head) 1295 if (!signals [w->signum - 1].head)
1135 signal (w->signum, SIG_DFL); 1296 signal (w->signum, SIG_DFL);
1136} 1297}
1137 1298
1138void 1299void
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) 1300ev_child_start (EV_P_ struct ev_child *w)
1206{ 1301{
1302#if EV_MULTIPLICITY
1303 assert (("child watchers are only supported in the default loop", loop == default_loop));
1304#endif
1207 if (ev_is_active (w)) 1305 if (ev_is_active (w))
1208 return; 1306 return;
1209 1307
1210 ev_start (EV_A_ (W)w, 1); 1308 ev_start (EV_A_ (W)w, 1);
1211 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1309 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1283 ev_timer_start (EV_A_ &once->to); 1381 ev_timer_start (EV_A_ &once->to);
1284 } 1382 }
1285 } 1383 }
1286} 1384}
1287 1385
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