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.116 by root, Thu Nov 15 09:19:42 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
246 #include "ev_vars.h" 258 #include "ev_vars.h"
247 #undef VAR 259 #undef VAR
248 }; 260 };
249 #include "ev_wrap.h" 261 #include "ev_wrap.h"
250 262
251 struct ev_loop default_loop_struct; 263 static struct ev_loop default_loop_struct;
252 static struct ev_loop *default_loop; 264 struct ev_loop *ev_default_loop_ptr;
253 265
254#else 266#else
255 267
256 ev_tstamp ev_rt_now; 268 ev_tstamp ev_rt_now;
257 #define VAR(name,decl) static decl; 269 #define VAR(name,decl) static decl;
258 #include "ev_vars.h" 270 #include "ev_vars.h"
259 #undef VAR 271 #undef VAR
260 272
261 static int default_loop; 273 static int ev_default_loop_ptr;
262 274
263#endif 275#endif
264 276
265/*****************************************************************************/ 277/*****************************************************************************/
266 278
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)
602ev_feed_signal_event (EV_P_ int signum) 614ev_feed_signal_event (EV_P_ int signum)
603{ 615{
604 WL w; 616 WL w;
605 617
606#if EV_MULTIPLICITY 618#if EV_MULTIPLICITY
607 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 619 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
608#endif 620#endif
609 621
610 --signum; 622 --signum;
611 623
612 if (signum < 0 || signum >= signalmax) 624 if (signum < 0 || signum >= signalmax)
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;
879 891
880#endif 892#endif
881 893
882#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
883struct ev_loop * 895struct ev_loop *
896ev_default_loop_ (unsigned int flags)
884#else 897#else
885int 898int
899ev_default_loop (unsigned int flags)
886#endif 900#endif
887ev_default_loop (int methods)
888{ 901{
889 if (sigpipe [0] == sigpipe [1]) 902 if (sigpipe [0] == sigpipe [1])
890 if (pipe (sigpipe)) 903 if (pipe (sigpipe))
891 return 0; 904 return 0;
892 905
893 if (!default_loop) 906 if (!ev_default_loop_ptr)
894 { 907 {
895#if EV_MULTIPLICITY 908#if EV_MULTIPLICITY
896 struct ev_loop *loop = default_loop = &default_loop_struct; 909 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
897#else 910#else
898 default_loop = 1; 911 ev_default_default_loop_ptr = 1;
899#endif 912#endif
900 913
901 loop_init (EV_A_ methods); 914 loop_init (EV_A_ flags);
902 915
903 if (ev_method (EV_A)) 916 if (ev_method (EV_A))
904 { 917 {
905 siginit (EV_A); 918 siginit (EV_A);
906 919
910 ev_signal_start (EV_A_ &childev); 923 ev_signal_start (EV_A_ &childev);
911 ev_unref (EV_A); /* child watcher should not keep loop alive */ 924 ev_unref (EV_A); /* child watcher should not keep loop alive */
912#endif 925#endif
913 } 926 }
914 else 927 else
915 default_loop = 0; 928 ev_default_loop_ptr = 0;
916 } 929 }
917 930
918 return default_loop; 931 return ev_default_loop_ptr;
919} 932}
920 933
921void 934void
922ev_default_destroy (void) 935ev_default_destroy (void)
923{ 936{
924#if EV_MULTIPLICITY 937#if EV_MULTIPLICITY
925 struct ev_loop *loop = default_loop; 938 struct ev_loop *loop = ev_default_loop_ptr;
926#endif 939#endif
927 940
928#ifndef _WIN32 941#ifndef _WIN32
929 ev_ref (EV_A); /* child watcher */ 942 ev_ref (EV_A); /* child watcher */
930 ev_signal_stop (EV_A_ &childev); 943 ev_signal_stop (EV_A_ &childev);
941 954
942void 955void
943ev_default_fork (void) 956ev_default_fork (void)
944{ 957{
945#if EV_MULTIPLICITY 958#if EV_MULTIPLICITY
946 struct ev_loop *loop = default_loop; 959 struct ev_loop *loop = ev_default_loop_ptr;
947#endif 960#endif
948 961
949 if (method) 962 if (method)
950 postfork = 1; 963 postfork = 1;
951} 964}
1020 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1033 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1021 1034
1022 /* first reschedule or stop timer */ 1035 /* first reschedule or stop timer */
1023 if (w->reschedule_cb) 1036 if (w->reschedule_cb)
1024 { 1037 {
1025 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1038 ((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)); 1039 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1028 downheap ((WT *)periodics, periodiccnt, 0); 1040 downheap ((WT *)periodics, periodiccnt, 0);
1029 } 1041 }
1030 else if (w->interval) 1042 else if (w->interval)
1031 { 1043 {
1149ev_loop (EV_P_ int flags) 1161ev_loop (EV_P_ int flags)
1150{ 1162{
1151 double block; 1163 double block;
1152 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1164 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1153 1165
1154 do 1166 while (activecnt)
1155 { 1167 {
1156 /* queue check watchers (and execute them) */ 1168 /* queue check watchers (and execute them) */
1157 if (expect_false (preparecnt)) 1169 if (expect_false (preparecnt))
1158 { 1170 {
1159 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1171 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1222 /* queue check watchers, to be executed first */ 1234 /* queue check watchers, to be executed first */
1223 if (checkcnt) 1235 if (checkcnt)
1224 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1236 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1225 1237
1226 call_pending (EV_A); 1238 call_pending (EV_A);
1239
1240 if (loop_done)
1241 break;
1227 } 1242 }
1228 while (activecnt && !loop_done);
1229 1243
1230 if (loop_done != 2) 1244 if (loop_done != 2)
1231 loop_done = 0; 1245 loop_done = 0;
1232} 1246}
1233 1247
1331 ((WT)w)->at += mn_now; 1345 ((WT)w)->at += mn_now;
1332 1346
1333 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1347 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1334 1348
1335 ev_start (EV_A_ (W)w, ++timercnt); 1349 ev_start (EV_A_ (W)w, ++timercnt);
1336 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1350 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1337 timers [timercnt - 1] = w; 1351 timers [timercnt - 1] = w;
1338 upheap ((WT *)timers, timercnt - 1); 1352 upheap ((WT *)timers, timercnt - 1);
1339 1353
1340 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1354 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1341} 1355}
1372 } 1386 }
1373 else 1387 else
1374 ev_timer_stop (EV_A_ w); 1388 ev_timer_stop (EV_A_ w);
1375 } 1389 }
1376 else if (w->repeat) 1390 else if (w->repeat)
1391 {
1392 w->at = w->repeat;
1377 ev_timer_start (EV_A_ w); 1393 ev_timer_start (EV_A_ w);
1394 }
1378} 1395}
1379 1396
1380#if EV_PERIODICS 1397#if EV_PERIODICS
1381void 1398void
1382ev_periodic_start (EV_P_ struct ev_periodic *w) 1399ev_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 */ 1409 /* 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; 1410 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1394 } 1411 }
1395 1412
1396 ev_start (EV_A_ (W)w, ++periodiccnt); 1413 ev_start (EV_A_ (W)w, ++periodiccnt);
1397 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1414 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1398 periodics [periodiccnt - 1] = w; 1415 periodics [periodiccnt - 1] = w;
1399 upheap ((WT *)periodics, periodiccnt - 1); 1416 upheap ((WT *)periodics, periodiccnt - 1);
1400 1417
1401 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1418 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1402} 1419}
1433{ 1450{
1434 if (ev_is_active (w)) 1451 if (ev_is_active (w))
1435 return; 1452 return;
1436 1453
1437 ev_start (EV_A_ (W)w, ++idlecnt); 1454 ev_start (EV_A_ (W)w, ++idlecnt);
1438 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1455 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1439 idles [idlecnt - 1] = w; 1456 idles [idlecnt - 1] = w;
1440} 1457}
1441 1458
1442void 1459void
1443ev_idle_stop (EV_P_ struct ev_idle *w) 1460ev_idle_stop (EV_P_ struct ev_idle *w)
1455{ 1472{
1456 if (ev_is_active (w)) 1473 if (ev_is_active (w))
1457 return; 1474 return;
1458 1475
1459 ev_start (EV_A_ (W)w, ++preparecnt); 1476 ev_start (EV_A_ (W)w, ++preparecnt);
1460 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1477 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1461 prepares [preparecnt - 1] = w; 1478 prepares [preparecnt - 1] = w;
1462} 1479}
1463 1480
1464void 1481void
1465ev_prepare_stop (EV_P_ struct ev_prepare *w) 1482ev_prepare_stop (EV_P_ struct ev_prepare *w)
1477{ 1494{
1478 if (ev_is_active (w)) 1495 if (ev_is_active (w))
1479 return; 1496 return;
1480 1497
1481 ev_start (EV_A_ (W)w, ++checkcnt); 1498 ev_start (EV_A_ (W)w, ++checkcnt);
1482 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1499 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1483 checks [checkcnt - 1] = w; 1500 checks [checkcnt - 1] = w;
1484} 1501}
1485 1502
1486void 1503void
1487ev_check_stop (EV_P_ struct ev_check *w) 1504ev_check_stop (EV_P_ struct ev_check *w)
1500 1517
1501void 1518void
1502ev_signal_start (EV_P_ struct ev_signal *w) 1519ev_signal_start (EV_P_ struct ev_signal *w)
1503{ 1520{
1504#if EV_MULTIPLICITY 1521#if EV_MULTIPLICITY
1505 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1522 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1506#endif 1523#endif
1507 if (ev_is_active (w)) 1524 if (ev_is_active (w))
1508 return; 1525 return;
1509 1526
1510 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1527 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1543 1560
1544void 1561void
1545ev_child_start (EV_P_ struct ev_child *w) 1562ev_child_start (EV_P_ struct ev_child *w)
1546{ 1563{
1547#if EV_MULTIPLICITY 1564#if EV_MULTIPLICITY
1548 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1565 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1549#endif 1566#endif
1550 if (ev_is_active (w)) 1567 if (ev_is_active (w))
1551 return; 1568 return;
1552 1569
1553 ev_start (EV_A_ (W)w, 1); 1570 ev_start (EV_A_ (W)w, 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines