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

Comparing libev/ev.c (file contents):
Revision 1.103 by root, Mon Nov 12 00:31:08 2007 UTC vs.
Revision 1.114 by root, Mon Nov 12 20:03:39 2007 UTC

95# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
96#endif 96#endif
97 97
98#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
99# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
100#endif 101#endif
101 102
102#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
103# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
104#endif 109#endif
105 110
106#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
107# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
108#endif 113#endif
114#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
115# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
116#endif 121#endif
117 122
118/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
119 130
120#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
121# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
122# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
123#endif 134#endif
156#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
157 168
158#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
159#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
160 171
161#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 */
162 174
163typedef struct ev_watcher *W; 175typedef struct ev_watcher *W;
164typedef struct ev_watcher_list *WL; 176typedef struct ev_watcher_list *WL;
165typedef struct ev_watcher_time *WT; 177typedef struct ev_watcher_time *WT;
166 178
299{ 311{
300 return ev_rt_now; 312 return ev_rt_now;
301} 313}
302#endif 314#endif
303 315
304#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
305 317
306#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
307 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
308 { \ 320 { \
309 int newcnt = cur; \ 321 int newcnt = cur; \
354 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
355 return; 367 return;
356 } 368 }
357 369
358 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
359 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);
360 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
361 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
362} 374}
363 375
364static void 376static void
434 return; 446 return;
435 447
436 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
437 449
438 ++fdchangecnt; 450 ++fdchangecnt;
439 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
440 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
441} 453}
442 454
443static void 455static void
444fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
735 return getuid () != geteuid () 747 return getuid () != geteuid ()
736 || getgid () != getegid (); 748 || getgid () != getegid ();
737#endif 749#endif
738} 750}
739 751
740int 752unsigned int
741ev_method (EV_P) 753ev_method (EV_P)
742{ 754{
743 return method; 755 return method;
744} 756}
745 757
746static void 758static void
747loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
748{ 760{
749 if (!method) 761 if (!method)
750 { 762 {
751#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
752 { 764 {
759 ev_rt_now = ev_time (); 771 ev_rt_now = ev_time ();
760 mn_now = get_clock (); 772 mn_now = get_clock ();
761 now_floor = mn_now; 773 now_floor = mn_now;
762 rtmn_diff = ev_rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
763 775
764 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
765 if (!enable_secure () && getenv ("LIBEV_METHODS"))
766 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
767 else 778
768 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
769 781
770 method = 0; 782 method = 0;
771#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
772 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
773#endif 785#endif
774#if EV_USE_EPOLL 786#if EV_USE_EPOLL
775 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
776#endif 788#endif
777#if EV_USE_POLL 789#if EV_USE_POLL
778 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
779#endif 791#endif
780#if EV_USE_SELECT 792#if EV_USE_SELECT
781 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
782#endif 794#endif
783 795
784 ev_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
785 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
786 } 798 }
806 818
807 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
808 array_free (pending, [i]); 820 array_free (pending, [i]);
809 821
810 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
811 array_free (fdchange, EMPTY); 823 array_free (fdchange, EMPTY0);
812 array_free (timer, EMPTY); 824 array_free (timer, EMPTY0);
813#if EV_PERIODICS 825#if EV_PERIODICS
814 array_free (periodic, EMPTY); 826 array_free (periodic, EMPTY0);
815#endif 827#endif
816 array_free (idle, EMPTY); 828 array_free (idle, EMPTY0);
817 array_free (prepare, EMPTY); 829 array_free (prepare, EMPTY0);
818 array_free (check, EMPTY); 830 array_free (check, EMPTY0);
819 831
820 method = 0; 832 method = 0;
821} 833}
822 834
823static void 835static void
848 postfork = 0; 860 postfork = 0;
849} 861}
850 862
851#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
852struct ev_loop * 864struct ev_loop *
853ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
854{ 866{
855 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));
856 868
857 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
858 870
859 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
860 872
861 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
862 return loop; 874 return loop;
863 875
864 return 0; 876 return 0;
882#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
883struct ev_loop * 895struct ev_loop *
884#else 896#else
885int 897int
886#endif 898#endif
887ev_default_loop (int methods) 899ev_default_loop (unsigned int flags)
888{ 900{
889 if (sigpipe [0] == sigpipe [1]) 901 if (sigpipe [0] == sigpipe [1])
890 if (pipe (sigpipe)) 902 if (pipe (sigpipe))
891 return 0; 903 return 0;
892 904
896 struct ev_loop *loop = default_loop = &default_loop_struct; 908 struct ev_loop *loop = default_loop = &default_loop_struct;
897#else 909#else
898 default_loop = 1; 910 default_loop = 1;
899#endif 911#endif
900 912
901 loop_init (EV_A_ methods); 913 loop_init (EV_A_ flags);
902 914
903 if (ev_method (EV_A)) 915 if (ev_method (EV_A))
904 { 916 {
905 siginit (EV_A); 917 siginit (EV_A);
906 918
1020 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1032 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1021 1033
1022 /* first reschedule or stop timer */ 1034 /* first reschedule or stop timer */
1023 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1024 { 1036 {
1025 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);
1026
1027 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));
1028 downheap ((WT *)periodics, periodiccnt, 0); 1039 downheap ((WT *)periodics, periodiccnt, 0);
1029 } 1040 }
1030 else if (w->interval) 1041 else if (w->interval)
1031 { 1042 {
1331 ((WT)w)->at += mn_now; 1342 ((WT)w)->at += mn_now;
1332 1343
1333 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.));
1334 1345
1335 ev_start (EV_A_ (W)w, ++timercnt); 1346 ev_start (EV_A_ (W)w, ++timercnt);
1336 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1347 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1337 timers [timercnt - 1] = w; 1348 timers [timercnt - 1] = w;
1338 upheap ((WT *)timers, timercnt - 1); 1349 upheap ((WT *)timers, timercnt - 1);
1339 1350
1340 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1351 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1341} 1352}
1372 } 1383 }
1373 else 1384 else
1374 ev_timer_stop (EV_A_ w); 1385 ev_timer_stop (EV_A_ w);
1375 } 1386 }
1376 else if (w->repeat) 1387 else if (w->repeat)
1388 {
1389 w->at = w->repeat;
1377 ev_timer_start (EV_A_ w); 1390 ev_timer_start (EV_A_ w);
1391 }
1378} 1392}
1379 1393
1380#if EV_PERIODICS 1394#if EV_PERIODICS
1381void 1395void
1382ev_periodic_start (EV_P_ struct ev_periodic *w) 1396ev_periodic_start (EV_P_ struct ev_periodic *w)
1392 /* 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 */
1393 ((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;
1394 } 1408 }
1395 1409
1396 ev_start (EV_A_ (W)w, ++periodiccnt); 1410 ev_start (EV_A_ (W)w, ++periodiccnt);
1397 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1411 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1398 periodics [periodiccnt - 1] = w; 1412 periodics [periodiccnt - 1] = w;
1399 upheap ((WT *)periodics, periodiccnt - 1); 1413 upheap ((WT *)periodics, periodiccnt - 1);
1400 1414
1401 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1415 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1402} 1416}
1433{ 1447{
1434 if (ev_is_active (w)) 1448 if (ev_is_active (w))
1435 return; 1449 return;
1436 1450
1437 ev_start (EV_A_ (W)w, ++idlecnt); 1451 ev_start (EV_A_ (W)w, ++idlecnt);
1438 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1452 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1439 idles [idlecnt - 1] = w; 1453 idles [idlecnt - 1] = w;
1440} 1454}
1441 1455
1442void 1456void
1443ev_idle_stop (EV_P_ struct ev_idle *w) 1457ev_idle_stop (EV_P_ struct ev_idle *w)
1455{ 1469{
1456 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1457 return; 1471 return;
1458 1472
1459 ev_start (EV_A_ (W)w, ++preparecnt); 1473 ev_start (EV_A_ (W)w, ++preparecnt);
1460 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1474 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1461 prepares [preparecnt - 1] = w; 1475 prepares [preparecnt - 1] = w;
1462} 1476}
1463 1477
1464void 1478void
1465ev_prepare_stop (EV_P_ struct ev_prepare *w) 1479ev_prepare_stop (EV_P_ struct ev_prepare *w)
1477{ 1491{
1478 if (ev_is_active (w)) 1492 if (ev_is_active (w))
1479 return; 1493 return;
1480 1494
1481 ev_start (EV_A_ (W)w, ++checkcnt); 1495 ev_start (EV_A_ (W)w, ++checkcnt);
1482 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1496 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1483 checks [checkcnt - 1] = w; 1497 checks [checkcnt - 1] = w;
1484} 1498}
1485 1499
1486void 1500void
1487ev_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