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

Comparing libev/ev.c (file contents):
Revision 1.106 by root, Mon Nov 12 01:07:50 2007 UTC vs.
Revision 1.114 by root, Mon Nov 12 20:03:39 2007 UTC

120# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
121#endif 121#endif
122 122
123/**/ 123/**/
124 124
125/* darwin simply cnanot be helped */ 125/* darwin simply cannot be helped */
126#ifdef __APPLE__ 126#ifdef __APPLE__
127# undef EV_USE_POLL 127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE 128# undef EV_USE_KQUEUE
129#endif 129#endif
130 130
167#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
168 168
169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
170#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
171 171
172#define EMPTY /* required for microsofts broken pseudo-c compiler */ 172#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
173#define EMPTY2(a,b) /* used to suppress some warnings */
173 174
174typedef struct ev_watcher *W; 175typedef struct ev_watcher *W;
175typedef struct ev_watcher_list *WL; 176typedef struct ev_watcher_list *WL;
176typedef struct ev_watcher_time *WT; 177typedef struct ev_watcher_time *WT;
177 178
310{ 311{
311 return ev_rt_now; 312 return ev_rt_now;
312} 313}
313#endif 314#endif
314 315
315#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
316 317
317#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
318 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
319 { \ 320 { \
320 int newcnt = cur; \ 321 int newcnt = cur; \
365 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
366 return; 367 return;
367 } 368 }
368 369
369 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
370 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 371 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
371 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
372 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
373} 374}
374 375
375static void 376static void
445 return; 446 return;
446 447
447 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
448 449
449 ++fdchangecnt; 450 ++fdchangecnt;
450 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
451 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
452} 453}
453 454
454static void 455static void
455fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
746 return getuid () != geteuid () 747 return getuid () != geteuid ()
747 || getgid () != getegid (); 748 || getgid () != getegid ();
748#endif 749#endif
749} 750}
750 751
751int 752unsigned int
752ev_method (EV_P) 753ev_method (EV_P)
753{ 754{
754 return method; 755 return method;
755} 756}
756 757
757static void 758static void
758loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
759{ 760{
760 if (!method) 761 if (!method)
761 { 762 {
762#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
763 { 764 {
770 ev_rt_now = ev_time (); 771 ev_rt_now = ev_time ();
771 mn_now = get_clock (); 772 mn_now = get_clock ();
772 now_floor = mn_now; 773 now_floor = mn_now;
773 rtmn_diff = ev_rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
774 775
775 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
776 if (!enable_secure () && getenv ("LIBEV_METHODS"))
777 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
778 else 778
779 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
780 781
781 method = 0; 782 method = 0;
782#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
783 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
784#endif 785#endif
785#if EV_USE_EPOLL 786#if EV_USE_EPOLL
786 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
787#endif 788#endif
788#if EV_USE_POLL 789#if EV_USE_POLL
789 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
790#endif 791#endif
791#if EV_USE_SELECT 792#if EV_USE_SELECT
792 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
793#endif 794#endif
794 795
795 ev_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
796 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
797 } 798 }
817 818
818 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
819 array_free (pending, [i]); 820 array_free (pending, [i]);
820 821
821 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
822 array_free (fdchange, EMPTY); 823 array_free (fdchange, EMPTY0);
823 array_free (timer, EMPTY); 824 array_free (timer, EMPTY0);
824#if EV_PERIODICS 825#if EV_PERIODICS
825 array_free (periodic, EMPTY); 826 array_free (periodic, EMPTY0);
826#endif 827#endif
827 array_free (idle, EMPTY); 828 array_free (idle, EMPTY0);
828 array_free (prepare, EMPTY); 829 array_free (prepare, EMPTY0);
829 array_free (check, EMPTY); 830 array_free (check, EMPTY0);
830 831
831 method = 0; 832 method = 0;
832} 833}
833 834
834static void 835static void
859 postfork = 0; 860 postfork = 0;
860} 861}
861 862
862#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
863struct ev_loop * 864struct ev_loop *
864ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
865{ 866{
866 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 867 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
867 868
868 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
869 870
870 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
871 872
872 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
873 return loop; 874 return loop;
874 875
875 return 0; 876 return 0;
893#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
894struct ev_loop * 895struct ev_loop *
895#else 896#else
896int 897int
897#endif 898#endif
898ev_default_loop (int methods) 899ev_default_loop (unsigned int flags)
899{ 900{
900 if (sigpipe [0] == sigpipe [1]) 901 if (sigpipe [0] == sigpipe [1])
901 if (pipe (sigpipe)) 902 if (pipe (sigpipe))
902 return 0; 903 return 0;
903 904
907 struct ev_loop *loop = default_loop = &default_loop_struct; 908 struct ev_loop *loop = default_loop = &default_loop_struct;
908#else 909#else
909 default_loop = 1; 910 default_loop = 1;
910#endif 911#endif
911 912
912 loop_init (EV_A_ methods); 913 loop_init (EV_A_ flags);
913 914
914 if (ev_method (EV_A)) 915 if (ev_method (EV_A))
915 { 916 {
916 siginit (EV_A); 917 siginit (EV_A);
917 918
1031 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1032 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1032 1033
1033 /* first reschedule or stop timer */ 1034 /* first reschedule or stop timer */
1034 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1035 { 1036 {
1036 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1037 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1037
1038 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1038 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1039 downheap ((WT *)periodics, periodiccnt, 0); 1039 downheap ((WT *)periodics, periodiccnt, 0);
1040 } 1040 }
1041 else if (w->interval) 1041 else if (w->interval)
1042 { 1042 {
1342 ((WT)w)->at += mn_now; 1342 ((WT)w)->at += mn_now;
1343 1343
1344 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1344 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1345 1345
1346 ev_start (EV_A_ (W)w, ++timercnt); 1346 ev_start (EV_A_ (W)w, ++timercnt);
1347 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1347 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1348 timers [timercnt - 1] = w; 1348 timers [timercnt - 1] = w;
1349 upheap ((WT *)timers, timercnt - 1); 1349 upheap ((WT *)timers, timercnt - 1);
1350 1350
1351 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1351 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1352} 1352}
1383 } 1383 }
1384 else 1384 else
1385 ev_timer_stop (EV_A_ w); 1385 ev_timer_stop (EV_A_ w);
1386 } 1386 }
1387 else if (w->repeat) 1387 else if (w->repeat)
1388 {
1389 w->at = w->repeat;
1388 ev_timer_start (EV_A_ w); 1390 ev_timer_start (EV_A_ w);
1391 }
1389} 1392}
1390 1393
1391#if EV_PERIODICS 1394#if EV_PERIODICS
1392void 1395void
1393ev_periodic_start (EV_P_ struct ev_periodic *w) 1396ev_periodic_start (EV_P_ struct ev_periodic *w)
1403 /* this formula differs from the one in periodic_reify because we do not always round up */ 1406 /* this formula differs from the one in periodic_reify because we do not always round up */
1404 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1407 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1405 } 1408 }
1406 1409
1407 ev_start (EV_A_ (W)w, ++periodiccnt); 1410 ev_start (EV_A_ (W)w, ++periodiccnt);
1408 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1411 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1409 periodics [periodiccnt - 1] = w; 1412 periodics [periodiccnt - 1] = w;
1410 upheap ((WT *)periodics, periodiccnt - 1); 1413 upheap ((WT *)periodics, periodiccnt - 1);
1411 1414
1412 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1415 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1413} 1416}
1444{ 1447{
1445 if (ev_is_active (w)) 1448 if (ev_is_active (w))
1446 return; 1449 return;
1447 1450
1448 ev_start (EV_A_ (W)w, ++idlecnt); 1451 ev_start (EV_A_ (W)w, ++idlecnt);
1449 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1452 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1450 idles [idlecnt - 1] = w; 1453 idles [idlecnt - 1] = w;
1451} 1454}
1452 1455
1453void 1456void
1454ev_idle_stop (EV_P_ struct ev_idle *w) 1457ev_idle_stop (EV_P_ struct ev_idle *w)
1466{ 1469{
1467 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1468 return; 1471 return;
1469 1472
1470 ev_start (EV_A_ (W)w, ++preparecnt); 1473 ev_start (EV_A_ (W)w, ++preparecnt);
1471 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1474 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1472 prepares [preparecnt - 1] = w; 1475 prepares [preparecnt - 1] = w;
1473} 1476}
1474 1477
1475void 1478void
1476ev_prepare_stop (EV_P_ struct ev_prepare *w) 1479ev_prepare_stop (EV_P_ struct ev_prepare *w)
1488{ 1491{
1489 if (ev_is_active (w)) 1492 if (ev_is_active (w))
1490 return; 1493 return;
1491 1494
1492 ev_start (EV_A_ (W)w, ++checkcnt); 1495 ev_start (EV_A_ (W)w, ++checkcnt);
1493 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1496 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1494 checks [checkcnt - 1] = w; 1497 checks [checkcnt - 1] = w;
1495} 1498}
1496 1499
1497void 1500void
1498ev_check_stop (EV_P_ struct ev_check *w) 1501ev_check_stop (EV_P_ struct ev_check *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines