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

Comparing libev/ev.c (file contents):
Revision 1.52 by root, Sat Nov 3 22:10:39 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);
492 ev_unref (EV_A); /* child watcher should not keep loop alive */ 497 ev_unref (EV_A); /* child watcher should not keep loop alive */
493} 498}
494 499
495/*****************************************************************************/ 500/*****************************************************************************/
496 501
497static struct ev_idle **idles; 502#ifndef WIN32
498static int idlemax, idlecnt;
499
500static struct ev_prepare **prepares;
501static int preparemax, preparecnt;
502
503static struct ev_check **checks;
504static int checkmax, checkcnt;
505
506/*****************************************************************************/
507 503
508static struct ev_child *childs [PID_HASHSIZE]; 504static struct ev_child *childs [PID_HASHSIZE];
509static struct ev_signal childev; 505static struct ev_signal childev;
510
511#ifndef WIN32
512 506
513#ifndef WCONTINUED 507#ifndef WCONTINUED
514# define WCONTINUED 0 508# define WCONTINUED 0
515#endif 509#endif
516 510
552# include "ev_kqueue.c" 546# include "ev_kqueue.c"
553#endif 547#endif
554#if EV_USE_EPOLL 548#if EV_USE_EPOLL
555# include "ev_epoll.c" 549# include "ev_epoll.c"
556#endif 550#endif
557#if EV_USEV_POLL 551#if EV_USE_POLL
558# include "ev_poll.c" 552# include "ev_poll.c"
559#endif 553#endif
560#if EV_USE_SELECT 554#if EV_USE_SELECT
561# include "ev_select.c" 555# include "ev_select.c"
562#endif 556#endif
589ev_method (EV_P) 583ev_method (EV_P)
590{ 584{
591 return method; 585 return method;
592} 586}
593 587
594int 588static void
595ev_init (EV_P_ int methods) 589loop_init (EV_P_ int methods)
596{ 590{
597 if (!method) 591 if (!method)
598 { 592 {
599#if EV_USE_MONOTONIC 593#if EV_USE_MONOTONIC
600 { 594 {
605#endif 599#endif
606 600
607 rt_now = ev_time (); 601 rt_now = ev_time ();
608 mn_now = get_clock (); 602 mn_now = get_clock ();
609 now_floor = mn_now; 603 now_floor = mn_now;
610 diff = rt_now - mn_now; 604 rtmn_diff = rt_now - mn_now;
611
612 if (pipe (sigpipe))
613 return 0;
614 605
615 if (methods == EVMETHOD_AUTO) 606 if (methods == EVMETHOD_AUTO)
616 if (!enable_secure () && getenv ("LIBmethodS")) 607 if (!enable_secure () && getenv ("LIBEV_METHODS"))
617 methods = atoi (getenv ("LIBmethodS")); 608 methods = atoi (getenv ("LIBEV_METHODS"));
618 else 609 else
619 methods = EVMETHOD_ANY; 610 methods = EVMETHOD_ANY;
620 611
621 method = 0; 612 method = 0;
622#if EV_USE_KQUEUE 613#if EV_USE_KQUEUE
623 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
624#endif 615#endif
625#if EV_USE_EPOLL 616#if EV_USE_EPOLL
626 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
627#endif 618#endif
628#if EV_USEV_POLL 619#if EV_USE_POLL
629 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 620 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
630#endif 621#endif
631#if EV_USE_SELECT 622#if EV_USE_SELECT
632 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 623 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
633#endif 624#endif
625 }
626}
634 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
635 if (method) 715 if (ev_method (EV_A))
636 { 716 {
637 ev_watcher_init (&sigev, sigcb); 717 ev_watcher_init (&sigev, sigcb);
638 ev_set_priority (&sigev, EV_MAXPRI); 718 ev_set_priority (&sigev, EV_MAXPRI);
639 siginit (EV_A); 719 siginit (EV_A);
640 720
643 ev_set_priority (&childev, EV_MAXPRI); 723 ev_set_priority (&childev, EV_MAXPRI);
644 ev_signal_start (EV_A_ &childev); 724 ev_signal_start (EV_A_ &childev);
645 ev_unref (EV_A); /* child watcher should not keep loop alive */ 725 ev_unref (EV_A); /* child watcher should not keep loop alive */
646#endif 726#endif
647 } 727 }
728 else
729 default_loop = 0;
648 } 730 }
649 731
650 return method; 732 return default_loop;
651} 733}
652 734
653/*****************************************************************************/
654
655void 735void
656ev_fork_prepare (void) 736ev_default_destroy (void)
657{ 737{
658 /* nop */ 738#if EV_MULTIPLICITY
659} 739 struct ev_loop *loop = default_loop;
660
661void
662ev_fork_parent (void)
663{
664 /* nop */
665}
666
667void
668ev_fork_child (void)
669{
670#if EV_USE_EPOLL
671 if (method == EVMETHOD_EPOLL)
672 epoll_postfork_child ();
673#endif 740#endif
674 741
742 ev_ref (EV_A); /* child watcher */
743 ev_signal_stop (EV_A_ &childev);
744
745 ev_ref (EV_A); /* signal watcher */
675 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);
676 close (sigpipe [0]); 764 close (sigpipe [0]);
677 close (sigpipe [1]); 765 close (sigpipe [1]);
678 pipe (sigpipe); 766 pipe (sigpipe);
767
768 ev_ref (EV_A); /* signal watcher */
679 siginit (); 769 siginit (EV_A);
680} 770}
681 771
682/*****************************************************************************/ 772/*****************************************************************************/
683 773
684static void 774static void
714 downheap ((WT *)timers, timercnt, 0); 804 downheap ((WT *)timers, timercnt, 0);
715 } 805 }
716 else 806 else
717 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 807 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
718 808
719 event ((W)w, EV_TIMEOUT); 809 event (EV_A_ (W)w, EV_TIMEOUT);
720 } 810 }
721} 811}
722 812
723static void 813static void
724periodics_reify (EV_P) 814periodics_reify (EV_P)
740 event (EV_A_ (W)w, EV_PERIODIC); 830 event (EV_A_ (W)w, EV_PERIODIC);
741 } 831 }
742} 832}
743 833
744static void 834static void
745periodics_reschedule (EV_P_ ev_tstamp diff) 835periodics_reschedule (EV_P)
746{ 836{
747 int i; 837 int i;
748 838
749 /* adjust periodics after time jump */ 839 /* adjust periodics after time jump */
750 for (i = 0; i < periodiccnt; ++i) 840 for (i = 0; i < periodiccnt; ++i)
771{ 861{
772 mn_now = get_clock (); 862 mn_now = get_clock ();
773 863
774 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 864 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
775 { 865 {
776 rt_now = mn_now + diff; 866 rt_now = rtmn_diff + mn_now;
777 return 0; 867 return 0;
778 } 868 }
779 else 869 else
780 { 870 {
781 now_floor = mn_now; 871 now_floor = mn_now;
792#if EV_USE_MONOTONIC 882#if EV_USE_MONOTONIC
793 if (expect_true (have_monotonic)) 883 if (expect_true (have_monotonic))
794 { 884 {
795 if (time_update_monotonic (EV_A)) 885 if (time_update_monotonic (EV_A))
796 { 886 {
797 ev_tstamp odiff = diff; 887 ev_tstamp odiff = rtmn_diff;
798 888
799 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 */
800 { 890 {
801 diff = rt_now - mn_now; 891 rtmn_diff = rt_now - mn_now;
802 892
803 if (fabs (odiff - diff) < MIN_TIMEJUMP) 893 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
804 return; /* all is well */ 894 return; /* all is well */
805 895
806 rt_now = ev_time (); 896 rt_now = ev_time ();
807 mn_now = get_clock (); 897 mn_now = get_clock ();
808 now_floor = mn_now; 898 now_floor = mn_now;
809 } 899 }
810 900
811 periodics_reschedule (EV_A_ diff - odiff); 901 periodics_reschedule (EV_A);
812 /* 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) */
813 } 904 }
814 } 905 }
815 else 906 else
816#endif 907#endif
817 { 908 {
818 rt_now = ev_time (); 909 rt_now = ev_time ();
819 910
820 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))
821 { 912 {
822 periodics_reschedule (EV_A_ rt_now - mn_now); 913 periodics_reschedule (EV_A);
823 914
824 /* 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 */
825 for (i = 0; i < timercnt; ++i) 916 for (i = 0; i < timercnt; ++i)
826 timers [i]->at += diff; 917 timers [i]->at += rt_now - mn_now;
827 } 918 }
828 919
829 mn_now = rt_now; 920 mn_now = rt_now;
830 } 921 }
831} 922}
1094 } 1185 }
1095 1186
1096 ev_stop (EV_A_ (W)w); 1187 ev_stop (EV_A_ (W)w);
1097} 1188}
1098 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
1099#ifndef SA_RESTART 1256#ifndef SA_RESTART
1100# define SA_RESTART 0 1257# define SA_RESTART 0
1101#endif 1258#endif
1102 1259
1103void 1260void
1104ev_signal_start (EV_P_ struct ev_signal *w) 1261ev_signal_start (EV_P_ struct ev_signal *w)
1105{ 1262{
1263#if EV_MULTIPLICITY
1264 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1265#endif
1106 if (ev_is_active (w)) 1266 if (ev_is_active (w))
1107 return; 1267 return;
1108 1268
1109 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));
1110 1270
1135 if (!signals [w->signum - 1].head) 1295 if (!signals [w->signum - 1].head)
1136 signal (w->signum, SIG_DFL); 1296 signal (w->signum, SIG_DFL);
1137} 1297}
1138 1298
1139void 1299void
1140ev_idle_start (EV_P_ struct ev_idle *w)
1141{
1142 if (ev_is_active (w))
1143 return;
1144
1145 ev_start (EV_A_ (W)w, ++idlecnt);
1146 array_needsize (idles, idlemax, idlecnt, );
1147 idles [idlecnt - 1] = w;
1148}
1149
1150void
1151ev_idle_stop (EV_P_ struct ev_idle *w)
1152{
1153 ev_clear_pending (EV_A_ (W)w);
1154 if (ev_is_active (w))
1155 return;
1156
1157 idles [w->active - 1] = idles [--idlecnt];
1158 ev_stop (EV_A_ (W)w);
1159}
1160
1161void
1162ev_prepare_start (EV_P_ struct ev_prepare *w)
1163{
1164 if (ev_is_active (w))
1165 return;
1166
1167 ev_start (EV_A_ (W)w, ++preparecnt);
1168 array_needsize (prepares, preparemax, preparecnt, );
1169 prepares [preparecnt - 1] = w;
1170}
1171
1172void
1173ev_prepare_stop (EV_P_ struct ev_prepare *w)
1174{
1175 ev_clear_pending (EV_A_ (W)w);
1176 if (ev_is_active (w))
1177 return;
1178
1179 prepares [w->active - 1] = prepares [--preparecnt];
1180 ev_stop (EV_A_ (W)w);
1181}
1182
1183void
1184ev_check_start (EV_P_ struct ev_check *w)
1185{
1186 if (ev_is_active (w))
1187 return;
1188
1189 ev_start (EV_A_ (W)w, ++checkcnt);
1190 array_needsize (checks, checkmax, checkcnt, );
1191 checks [checkcnt - 1] = w;
1192}
1193
1194void
1195ev_check_stop (EV_P_ struct ev_check *w)
1196{
1197 ev_clear_pending (EV_A_ (W)w);
1198 if (ev_is_active (w))
1199 return;
1200
1201 checks [w->active - 1] = checks [--checkcnt];
1202 ev_stop (EV_A_ (W)w);
1203}
1204
1205void
1206ev_child_start (EV_P_ struct ev_child *w) 1300ev_child_start (EV_P_ struct ev_child *w)
1207{ 1301{
1302#if EV_MULTIPLICITY
1303 assert (("child watchers are only supported in the default loop", loop == default_loop));
1304#endif
1208 if (ev_is_active (w)) 1305 if (ev_is_active (w))
1209 return; 1306 return;
1210 1307
1211 ev_start (EV_A_ (W)w, 1); 1308 ev_start (EV_A_ (W)w, 1);
1212 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1309 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1284 ev_timer_start (EV_A_ &once->to); 1381 ev_timer_start (EV_A_ &once->to);
1285 } 1382 }
1286 } 1383 }
1287} 1384}
1288 1385
1289/*****************************************************************************/
1290
1291#if 0
1292
1293struct ev_io wio;
1294
1295static void
1296sin_cb (struct ev_io *w, int revents)
1297{
1298 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1299}
1300
1301static void
1302ocb (struct ev_timer *w, int revents)
1303{
1304 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1305 ev_timer_stop (w);
1306 ev_timer_start (w);
1307}
1308
1309static void
1310scb (struct ev_signal *w, int revents)
1311{
1312 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1313 ev_io_stop (&wio);
1314 ev_io_start (&wio);
1315}
1316
1317static void
1318gcb (struct ev_signal *w, int revents)
1319{
1320 fprintf (stderr, "generic %x\n", revents);
1321
1322}
1323
1324int main (void)
1325{
1326 ev_init (0);
1327
1328 ev_io_init (&wio, sin_cb, 0, EV_READ);
1329 ev_io_start (&wio);
1330
1331 struct ev_timer t[10000];
1332
1333#if 0
1334 int i;
1335 for (i = 0; i < 10000; ++i)
1336 {
1337 struct ev_timer *w = t + i;
1338 ev_watcher_init (w, ocb, i);
1339 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1340 ev_timer_start (w);
1341 if (drand48 () < 0.5)
1342 ev_timer_stop (w);
1343 }
1344#endif
1345
1346 struct ev_timer t1;
1347 ev_timer_init (&t1, ocb, 5, 10);
1348 ev_timer_start (&t1);
1349
1350 struct ev_signal sig;
1351 ev_signal_init (&sig, scb, SIGQUIT);
1352 ev_signal_start (&sig);
1353
1354 struct ev_check cw;
1355 ev_check_init (&cw, gcb);
1356 ev_check_start (&cw);
1357
1358 struct ev_idle iw;
1359 ev_idle_init (&iw, gcb);
1360 ev_idle_start (&iw);
1361
1362 ev_loop (0);
1363
1364 return 0;
1365}
1366
1367#endif
1368
1369
1370
1371

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines