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

Comparing libev/ev.c (file contents):
Revision 1.104 by root, Mon Nov 12 00:39:45 2007 UTC vs.
Revision 1.116 by root, Thu Nov 15 09:19:42 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 cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
130
125#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
126# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
127# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
128#endif 134#endif
129 135
161#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
162 168
163#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
164#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
165 171
166#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 */
167 174
168typedef struct ev_watcher *W; 175typedef struct ev_watcher *W;
169typedef struct ev_watcher_list *WL; 176typedef struct ev_watcher_list *WL;
170typedef struct ev_watcher_time *WT; 177typedef struct ev_watcher_time *WT;
171 178
251 #include "ev_vars.h" 258 #include "ev_vars.h"
252 #undef VAR 259 #undef VAR
253 }; 260 };
254 #include "ev_wrap.h" 261 #include "ev_wrap.h"
255 262
256 struct ev_loop default_loop_struct; 263 static struct ev_loop default_loop_struct;
257 static struct ev_loop *default_loop; 264 struct ev_loop *ev_default_loop_ptr;
258 265
259#else 266#else
260 267
261 ev_tstamp ev_rt_now; 268 ev_tstamp ev_rt_now;
262 #define VAR(name,decl) static decl; 269 #define VAR(name,decl) static decl;
263 #include "ev_vars.h" 270 #include "ev_vars.h"
264 #undef VAR 271 #undef VAR
265 272
266 static int default_loop; 273 static int ev_default_loop_ptr;
267 274
268#endif 275#endif
269 276
270/*****************************************************************************/ 277/*****************************************************************************/
271 278
304{ 311{
305 return ev_rt_now; 312 return ev_rt_now;
306} 313}
307#endif 314#endif
308 315
309#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
310 317
311#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
312 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
313 { \ 320 { \
314 int newcnt = cur; \ 321 int newcnt = cur; \
359 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
360 return; 367 return;
361 } 368 }
362 369
363 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
364 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);
365 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
366 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
367} 374}
368 375
369static void 376static void
439 return; 446 return;
440 447
441 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
442 449
443 ++fdchangecnt; 450 ++fdchangecnt;
444 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
445 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
446} 453}
447 454
448static void 455static void
449fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
607ev_feed_signal_event (EV_P_ int signum) 614ev_feed_signal_event (EV_P_ int signum)
608{ 615{
609 WL w; 616 WL w;
610 617
611#if EV_MULTIPLICITY 618#if EV_MULTIPLICITY
612 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));
613#endif 620#endif
614 621
615 --signum; 622 --signum;
616 623
617 if (signum < 0 || signum >= signalmax) 624 if (signum < 0 || signum >= signalmax)
740 return getuid () != geteuid () 747 return getuid () != geteuid ()
741 || getgid () != getegid (); 748 || getgid () != getegid ();
742#endif 749#endif
743} 750}
744 751
745int 752unsigned int
746ev_method (EV_P) 753ev_method (EV_P)
747{ 754{
748 return method; 755 return method;
749} 756}
750 757
751static void 758static void
752loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
753{ 760{
754 if (!method) 761 if (!method)
755 { 762 {
756#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
757 { 764 {
764 ev_rt_now = ev_time (); 771 ev_rt_now = ev_time ();
765 mn_now = get_clock (); 772 mn_now = get_clock ();
766 now_floor = mn_now; 773 now_floor = mn_now;
767 rtmn_diff = ev_rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
768 775
769 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
770 if (!enable_secure () && getenv ("LIBEV_METHODS"))
771 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
772 else 778
773 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
774 781
775 method = 0; 782 method = 0;
776#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
777 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
778#endif 785#endif
779#if EV_USE_EPOLL 786#if EV_USE_EPOLL
780 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
781#endif 788#endif
782#if EV_USE_POLL 789#if EV_USE_POLL
783 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
784#endif 791#endif
785#if EV_USE_SELECT 792#if EV_USE_SELECT
786 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
787#endif 794#endif
788 795
789 ev_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
790 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
791 } 798 }
811 818
812 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
813 array_free (pending, [i]); 820 array_free (pending, [i]);
814 821
815 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
816 array_free (fdchange, EMPTY); 823 array_free (fdchange, EMPTY0);
817 array_free (timer, EMPTY); 824 array_free (timer, EMPTY0);
818#if EV_PERIODICS 825#if EV_PERIODICS
819 array_free (periodic, EMPTY); 826 array_free (periodic, EMPTY0);
820#endif 827#endif
821 array_free (idle, EMPTY); 828 array_free (idle, EMPTY0);
822 array_free (prepare, EMPTY); 829 array_free (prepare, EMPTY0);
823 array_free (check, EMPTY); 830 array_free (check, EMPTY0);
824 831
825 method = 0; 832 method = 0;
826} 833}
827 834
828static void 835static void
853 postfork = 0; 860 postfork = 0;
854} 861}
855 862
856#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
857struct ev_loop * 864struct ev_loop *
858ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
859{ 866{
860 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));
861 868
862 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
863 870
864 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
865 872
866 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
867 return loop; 874 return loop;
868 875
869 return 0; 876 return 0;
884 891
885#endif 892#endif
886 893
887#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
888struct ev_loop * 895struct ev_loop *
896ev_default_loop_ (unsigned int flags)
889#else 897#else
890int 898int
899ev_default_loop (unsigned int flags)
891#endif 900#endif
892ev_default_loop (int methods)
893{ 901{
894 if (sigpipe [0] == sigpipe [1]) 902 if (sigpipe [0] == sigpipe [1])
895 if (pipe (sigpipe)) 903 if (pipe (sigpipe))
896 return 0; 904 return 0;
897 905
898 if (!default_loop) 906 if (!ev_default_loop_ptr)
899 { 907 {
900#if EV_MULTIPLICITY 908#if EV_MULTIPLICITY
901 struct ev_loop *loop = default_loop = &default_loop_struct; 909 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
902#else 910#else
903 default_loop = 1; 911 ev_default_default_loop_ptr = 1;
904#endif 912#endif
905 913
906 loop_init (EV_A_ methods); 914 loop_init (EV_A_ flags);
907 915
908 if (ev_method (EV_A)) 916 if (ev_method (EV_A))
909 { 917 {
910 siginit (EV_A); 918 siginit (EV_A);
911 919
915 ev_signal_start (EV_A_ &childev); 923 ev_signal_start (EV_A_ &childev);
916 ev_unref (EV_A); /* child watcher should not keep loop alive */ 924 ev_unref (EV_A); /* child watcher should not keep loop alive */
917#endif 925#endif
918 } 926 }
919 else 927 else
920 default_loop = 0; 928 ev_default_loop_ptr = 0;
921 } 929 }
922 930
923 return default_loop; 931 return ev_default_loop_ptr;
924} 932}
925 933
926void 934void
927ev_default_destroy (void) 935ev_default_destroy (void)
928{ 936{
929#if EV_MULTIPLICITY 937#if EV_MULTIPLICITY
930 struct ev_loop *loop = default_loop; 938 struct ev_loop *loop = ev_default_loop_ptr;
931#endif 939#endif
932 940
933#ifndef _WIN32 941#ifndef _WIN32
934 ev_ref (EV_A); /* child watcher */ 942 ev_ref (EV_A); /* child watcher */
935 ev_signal_stop (EV_A_ &childev); 943 ev_signal_stop (EV_A_ &childev);
946 954
947void 955void
948ev_default_fork (void) 956ev_default_fork (void)
949{ 957{
950#if EV_MULTIPLICITY 958#if EV_MULTIPLICITY
951 struct ev_loop *loop = default_loop; 959 struct ev_loop *loop = ev_default_loop_ptr;
952#endif 960#endif
953 961
954 if (method) 962 if (method)
955 postfork = 1; 963 postfork = 1;
956} 964}
1025 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1033 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1026 1034
1027 /* first reschedule or stop timer */ 1035 /* first reschedule or stop timer */
1028 if (w->reschedule_cb) 1036 if (w->reschedule_cb)
1029 { 1037 {
1030 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);
1031
1032 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));
1033 downheap ((WT *)periodics, periodiccnt, 0); 1040 downheap ((WT *)periodics, periodiccnt, 0);
1034 } 1041 }
1035 else if (w->interval) 1042 else if (w->interval)
1036 { 1043 {
1154ev_loop (EV_P_ int flags) 1161ev_loop (EV_P_ int flags)
1155{ 1162{
1156 double block; 1163 double block;
1157 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1164 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1158 1165
1159 do 1166 while (activecnt)
1160 { 1167 {
1161 /* queue check watchers (and execute them) */ 1168 /* queue check watchers (and execute them) */
1162 if (expect_false (preparecnt)) 1169 if (expect_false (preparecnt))
1163 { 1170 {
1164 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1171 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1227 /* queue check watchers, to be executed first */ 1234 /* queue check watchers, to be executed first */
1228 if (checkcnt) 1235 if (checkcnt)
1229 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1236 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1230 1237
1231 call_pending (EV_A); 1238 call_pending (EV_A);
1239
1240 if (loop_done)
1241 break;
1232 } 1242 }
1233 while (activecnt && !loop_done);
1234 1243
1235 if (loop_done != 2) 1244 if (loop_done != 2)
1236 loop_done = 0; 1245 loop_done = 0;
1237} 1246}
1238 1247
1336 ((WT)w)->at += mn_now; 1345 ((WT)w)->at += mn_now;
1337 1346
1338 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.));
1339 1348
1340 ev_start (EV_A_ (W)w, ++timercnt); 1349 ev_start (EV_A_ (W)w, ++timercnt);
1341 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1350 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1342 timers [timercnt - 1] = w; 1351 timers [timercnt - 1] = w;
1343 upheap ((WT *)timers, timercnt - 1); 1352 upheap ((WT *)timers, timercnt - 1);
1344 1353
1345 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1354 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1346} 1355}
1377 } 1386 }
1378 else 1387 else
1379 ev_timer_stop (EV_A_ w); 1388 ev_timer_stop (EV_A_ w);
1380 } 1389 }
1381 else if (w->repeat) 1390 else if (w->repeat)
1391 {
1392 w->at = w->repeat;
1382 ev_timer_start (EV_A_ w); 1393 ev_timer_start (EV_A_ w);
1394 }
1383} 1395}
1384 1396
1385#if EV_PERIODICS 1397#if EV_PERIODICS
1386void 1398void
1387ev_periodic_start (EV_P_ struct ev_periodic *w) 1399ev_periodic_start (EV_P_ struct ev_periodic *w)
1397 /* 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 */
1398 ((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;
1399 } 1411 }
1400 1412
1401 ev_start (EV_A_ (W)w, ++periodiccnt); 1413 ev_start (EV_A_ (W)w, ++periodiccnt);
1402 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1414 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1403 periodics [periodiccnt - 1] = w; 1415 periodics [periodiccnt - 1] = w;
1404 upheap ((WT *)periodics, periodiccnt - 1); 1416 upheap ((WT *)periodics, periodiccnt - 1);
1405 1417
1406 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1418 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1407} 1419}
1438{ 1450{
1439 if (ev_is_active (w)) 1451 if (ev_is_active (w))
1440 return; 1452 return;
1441 1453
1442 ev_start (EV_A_ (W)w, ++idlecnt); 1454 ev_start (EV_A_ (W)w, ++idlecnt);
1443 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1455 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1444 idles [idlecnt - 1] = w; 1456 idles [idlecnt - 1] = w;
1445} 1457}
1446 1458
1447void 1459void
1448ev_idle_stop (EV_P_ struct ev_idle *w) 1460ev_idle_stop (EV_P_ struct ev_idle *w)
1460{ 1472{
1461 if (ev_is_active (w)) 1473 if (ev_is_active (w))
1462 return; 1474 return;
1463 1475
1464 ev_start (EV_A_ (W)w, ++preparecnt); 1476 ev_start (EV_A_ (W)w, ++preparecnt);
1465 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1477 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1466 prepares [preparecnt - 1] = w; 1478 prepares [preparecnt - 1] = w;
1467} 1479}
1468 1480
1469void 1481void
1470ev_prepare_stop (EV_P_ struct ev_prepare *w) 1482ev_prepare_stop (EV_P_ struct ev_prepare *w)
1482{ 1494{
1483 if (ev_is_active (w)) 1495 if (ev_is_active (w))
1484 return; 1496 return;
1485 1497
1486 ev_start (EV_A_ (W)w, ++checkcnt); 1498 ev_start (EV_A_ (W)w, ++checkcnt);
1487 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1499 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1488 checks [checkcnt - 1] = w; 1500 checks [checkcnt - 1] = w;
1489} 1501}
1490 1502
1491void 1503void
1492ev_check_stop (EV_P_ struct ev_check *w) 1504ev_check_stop (EV_P_ struct ev_check *w)
1505 1517
1506void 1518void
1507ev_signal_start (EV_P_ struct ev_signal *w) 1519ev_signal_start (EV_P_ struct ev_signal *w)
1508{ 1520{
1509#if EV_MULTIPLICITY 1521#if EV_MULTIPLICITY
1510 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));
1511#endif 1523#endif
1512 if (ev_is_active (w)) 1524 if (ev_is_active (w))
1513 return; 1525 return;
1514 1526
1515 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));
1548 1560
1549void 1561void
1550ev_child_start (EV_P_ struct ev_child *w) 1562ev_child_start (EV_P_ struct ev_child *w)
1551{ 1563{
1552#if EV_MULTIPLICITY 1564#if EV_MULTIPLICITY
1553 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));
1554#endif 1566#endif
1555 if (ev_is_active (w)) 1567 if (ev_is_active (w))
1556 return; 1568 return;
1557 1569
1558 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