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.115 by root, Wed Nov 14 04:53:21 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
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)
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;
887#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
888struct ev_loop * 895struct ev_loop *
889#else 896#else
890int 897int
891#endif 898#endif
892ev_default_loop (int methods) 899ev_default_loop (unsigned int flags)
893{ 900{
894 if (sigpipe [0] == sigpipe [1]) 901 if (sigpipe [0] == sigpipe [1])
895 if (pipe (sigpipe)) 902 if (pipe (sigpipe))
896 return 0; 903 return 0;
897 904
901 struct ev_loop *loop = default_loop = &default_loop_struct; 908 struct ev_loop *loop = default_loop = &default_loop_struct;
902#else 909#else
903 default_loop = 1; 910 default_loop = 1;
904#endif 911#endif
905 912
906 loop_init (EV_A_ methods); 913 loop_init (EV_A_ flags);
907 914
908 if (ev_method (EV_A)) 915 if (ev_method (EV_A))
909 { 916 {
910 siginit (EV_A); 917 siginit (EV_A);
911 918
1025 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1032 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1026 1033
1027 /* first reschedule or stop timer */ 1034 /* first reschedule or stop timer */
1028 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1029 { 1036 {
1030 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);
1031
1032 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));
1033 downheap ((WT *)periodics, periodiccnt, 0); 1039 downheap ((WT *)periodics, periodiccnt, 0);
1034 } 1040 }
1035 else if (w->interval) 1041 else if (w->interval)
1036 { 1042 {
1154ev_loop (EV_P_ int flags) 1160ev_loop (EV_P_ int flags)
1155{ 1161{
1156 double block; 1162 double block;
1157 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1158 1164
1159 do 1165 while (activecnt)
1160 { 1166 {
1161 /* queue check watchers (and execute them) */ 1167 /* queue check watchers (and execute them) */
1162 if (expect_false (preparecnt)) 1168 if (expect_false (preparecnt))
1163 { 1169 {
1164 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1170 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1227 /* queue check watchers, to be executed first */ 1233 /* queue check watchers, to be executed first */
1228 if (checkcnt) 1234 if (checkcnt)
1229 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1230 1236
1231 call_pending (EV_A); 1237 call_pending (EV_A);
1238
1239 if (loop_done)
1240 break;
1232 } 1241 }
1233 while (activecnt && !loop_done);
1234 1242
1235 if (loop_done != 2) 1243 if (loop_done != 2)
1236 loop_done = 0; 1244 loop_done = 0;
1237} 1245}
1238 1246
1336 ((WT)w)->at += mn_now; 1344 ((WT)w)->at += mn_now;
1337 1345
1338 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1346 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1339 1347
1340 ev_start (EV_A_ (W)w, ++timercnt); 1348 ev_start (EV_A_ (W)w, ++timercnt);
1341 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1349 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1342 timers [timercnt - 1] = w; 1350 timers [timercnt - 1] = w;
1343 upheap ((WT *)timers, timercnt - 1); 1351 upheap ((WT *)timers, timercnt - 1);
1344 1352
1345 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1353 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1346} 1354}
1377 } 1385 }
1378 else 1386 else
1379 ev_timer_stop (EV_A_ w); 1387 ev_timer_stop (EV_A_ w);
1380 } 1388 }
1381 else if (w->repeat) 1389 else if (w->repeat)
1390 {
1391 w->at = w->repeat;
1382 ev_timer_start (EV_A_ w); 1392 ev_timer_start (EV_A_ w);
1393 }
1383} 1394}
1384 1395
1385#if EV_PERIODICS 1396#if EV_PERIODICS
1386void 1397void
1387ev_periodic_start (EV_P_ struct ev_periodic *w) 1398ev_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 */ 1408 /* 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; 1409 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1399 } 1410 }
1400 1411
1401 ev_start (EV_A_ (W)w, ++periodiccnt); 1412 ev_start (EV_A_ (W)w, ++periodiccnt);
1402 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1413 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1403 periodics [periodiccnt - 1] = w; 1414 periodics [periodiccnt - 1] = w;
1404 upheap ((WT *)periodics, periodiccnt - 1); 1415 upheap ((WT *)periodics, periodiccnt - 1);
1405 1416
1406 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1417 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1407} 1418}
1438{ 1449{
1439 if (ev_is_active (w)) 1450 if (ev_is_active (w))
1440 return; 1451 return;
1441 1452
1442 ev_start (EV_A_ (W)w, ++idlecnt); 1453 ev_start (EV_A_ (W)w, ++idlecnt);
1443 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1454 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1444 idles [idlecnt - 1] = w; 1455 idles [idlecnt - 1] = w;
1445} 1456}
1446 1457
1447void 1458void
1448ev_idle_stop (EV_P_ struct ev_idle *w) 1459ev_idle_stop (EV_P_ struct ev_idle *w)
1460{ 1471{
1461 if (ev_is_active (w)) 1472 if (ev_is_active (w))
1462 return; 1473 return;
1463 1474
1464 ev_start (EV_A_ (W)w, ++preparecnt); 1475 ev_start (EV_A_ (W)w, ++preparecnt);
1465 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1476 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1466 prepares [preparecnt - 1] = w; 1477 prepares [preparecnt - 1] = w;
1467} 1478}
1468 1479
1469void 1480void
1470ev_prepare_stop (EV_P_ struct ev_prepare *w) 1481ev_prepare_stop (EV_P_ struct ev_prepare *w)
1482{ 1493{
1483 if (ev_is_active (w)) 1494 if (ev_is_active (w))
1484 return; 1495 return;
1485 1496
1486 ev_start (EV_A_ (W)w, ++checkcnt); 1497 ev_start (EV_A_ (W)w, ++checkcnt);
1487 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1498 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1488 checks [checkcnt - 1] = w; 1499 checks [checkcnt - 1] = w;
1489} 1500}
1490 1501
1491void 1502void
1492ev_check_stop (EV_P_ struct ev_check *w) 1503ev_check_stop (EV_P_ struct ev_check *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines