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

Comparing libev/ev.c (file contents):
Revision 1.60 by root, Sun Nov 4 18:29:44 2007 UTC vs.
Revision 1.65 by root, Sun Nov 4 23:29:48 2007 UTC

92 92
93#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
94# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif 95#endif
96 96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
103#endif
104
97#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
98# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
99#endif 107#endif
100 108
101/**/ 109/**/
224 base = realloc (base, sizeof (*base) * (newcnt)); \ 232 base = realloc (base, sizeof (*base) * (newcnt)); \
225 init (base + cur, newcnt - cur); \ 233 init (base + cur, newcnt - cur); \
226 cur = newcnt; \ 234 cur = newcnt; \
227 } 235 }
228 236
237#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239
229/*****************************************************************************/ 240/*****************************************************************************/
230 241
231static void 242static void
232anfds_init (ANFD *base, int count) 243anfds_init (ANFD *base, int count)
233{ 244{
298 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 309 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
299 events |= w->events; 310 events |= w->events;
300 311
301 anfd->reify = 0; 312 anfd->reify = 0;
302 313
303 if (anfd->events != events)
304 {
305 method_modify (EV_A_ fd, anfd->events, events); 314 method_modify (EV_A_ fd, anfd->events, events);
306 anfd->events = events; 315 anfd->events = events;
307 }
308 } 316 }
309 317
310 fdchangecnt = 0; 318 fdchangecnt = 0;
311} 319}
312 320
349 357
350/* called on ENOMEM in select/poll to kill some fds and retry */ 358/* called on ENOMEM in select/poll to kill some fds and retry */
351static void 359static void
352fd_enomem (EV_P) 360fd_enomem (EV_P)
353{ 361{
354 int fd = anfdmax; 362 int fd;
355 363
356 while (fd--) 364 for (fd = anfdmax; fd--; )
357 if (anfds [fd].events) 365 if (anfds [fd].events)
358 { 366 {
359 close (fd); 367 close (fd);
360 fd_kill (EV_A_ fd); 368 fd_kill (EV_A_ fd);
361 return; 369 return;
385 WT w = heap [k]; 393 WT w = heap [k];
386 394
387 while (k && heap [k >> 1]->at > w->at) 395 while (k && heap [k >> 1]->at > w->at)
388 { 396 {
389 heap [k] = heap [k >> 1]; 397 heap [k] = heap [k >> 1];
390 heap [k]->active = k + 1; 398 ((W)heap [k])->active = k + 1;
391 k >>= 1; 399 k >>= 1;
392 } 400 }
393 401
394 heap [k] = w; 402 heap [k] = w;
395 heap [k]->active = k + 1; 403 ((W)heap [k])->active = k + 1;
396 404
397} 405}
398 406
399static void 407static void
400downheap (WT *heap, int N, int k) 408downheap (WT *heap, int N, int k)
410 418
411 if (w->at <= heap [j]->at) 419 if (w->at <= heap [j]->at)
412 break; 420 break;
413 421
414 heap [k] = heap [j]; 422 heap [k] = heap [j];
415 heap [k]->active = k + 1; 423 ((W)heap [k])->active = k + 1;
416 k = j; 424 k = j;
417 } 425 }
418 426
419 heap [k] = w; 427 heap [k] = w;
420 heap [k]->active = k + 1; 428 ((W)heap [k])->active = k + 1;
421} 429}
422 430
423/*****************************************************************************/ 431/*****************************************************************************/
424 432
425typedef struct 433typedef struct
514 struct ev_child *w; 522 struct ev_child *w;
515 523
516 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 524 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
517 if (w->pid == pid || !w->pid) 525 if (w->pid == pid || !w->pid)
518 { 526 {
519 w->priority = sw->priority; /* need to do it *now* */ 527 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
520 w->rpid = pid; 528 w->rpid = pid;
521 w->rstatus = status; 529 w->rstatus = status;
522 event (EV_A_ (W)w, EV_CHILD); 530 event (EV_A_ (W)w, EV_CHILD);
523 } 531 }
524} 532}
525 533
526static void 534static void
608 methods = atoi (getenv ("LIBEV_METHODS")); 616 methods = atoi (getenv ("LIBEV_METHODS"));
609 else 617 else
610 methods = EVMETHOD_ANY; 618 methods = EVMETHOD_ANY;
611 619
612 method = 0; 620 method = 0;
621#if EV_USE_WIN32
622 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
623#endif
613#if EV_USE_KQUEUE 624#if EV_USE_KQUEUE
614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
615#endif 626#endif
616#if EV_USE_EPOLL 627#if EV_USE_EPOLL
617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626} 637}
627 638
628void 639void
629loop_destroy (EV_P) 640loop_destroy (EV_P)
630{ 641{
642 int i;
643
644#if EV_USE_WIN32
645 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
646#endif
631#if EV_USE_KQUEUE 647#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 648 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif 649#endif
634#if EV_USE_EPOLL 650#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 651 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 654 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
639#endif 655#endif
640#if EV_USE_SELECT 656#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 657 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#endif 658#endif
659
660 for (i = NUMPRI; i--; )
661 array_free (pending, [i]);
662
663 array_free (fdchange, );
664 array_free (timer, );
665 array_free (periodic, );
666 array_free (idle, );
667 array_free (prepare, );
668 array_free (check, );
643 669
644 method = 0; 670 method = 0;
645 /*TODO*/ 671 /*TODO*/
646} 672}
647 673
782 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 808 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
783 809
784 if (p->w) 810 if (p->w)
785 { 811 {
786 p->w->pending = 0; 812 p->w->pending = 0;
813
787 p->w->cb (EV_A_ p->w, p->events); 814 ((void (*)(EV_P_ W, int))p->w->cb) (EV_A_ p->w, p->events);
788 } 815 }
789 } 816 }
790} 817}
791 818
792static void 819static void
793timers_reify (EV_P) 820timers_reify (EV_P)
794{ 821{
795 while (timercnt && timers [0]->at <= mn_now) 822 while (timercnt && ((WT)timers [0])->at <= mn_now)
796 { 823 {
797 struct ev_timer *w = timers [0]; 824 struct ev_timer *w = timers [0];
825
826 assert (("inactive timer on timer heap detected", ev_is_active (w)));
798 827
799 /* first reschedule or stop timer */ 828 /* first reschedule or stop timer */
800 if (w->repeat) 829 if (w->repeat)
801 { 830 {
802 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 831 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
803 w->at = mn_now + w->repeat; 832 ((WT)w)->at = mn_now + w->repeat;
804 downheap ((WT *)timers, timercnt, 0); 833 downheap ((WT *)timers, timercnt, 0);
805 } 834 }
806 else 835 else
807 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 836 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
808 837
811} 840}
812 841
813static void 842static void
814periodics_reify (EV_P) 843periodics_reify (EV_P)
815{ 844{
816 while (periodiccnt && periodics [0]->at <= rt_now) 845 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
817 { 846 {
818 struct ev_periodic *w = periodics [0]; 847 struct ev_periodic *w = periodics [0];
848
849 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
819 850
820 /* first reschedule or stop timer */ 851 /* first reschedule or stop timer */
821 if (w->interval) 852 if (w->interval)
822 { 853 {
823 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 854 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
824 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 855 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
825 downheap ((WT *)periodics, periodiccnt, 0); 856 downheap ((WT *)periodics, periodiccnt, 0);
826 } 857 }
827 else 858 else
828 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 859 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
829 860
841 { 872 {
842 struct ev_periodic *w = periodics [i]; 873 struct ev_periodic *w = periodics [i];
843 874
844 if (w->interval) 875 if (w->interval)
845 { 876 {
846 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 877 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
847 878
848 if (fabs (diff) >= 1e-4) 879 if (fabs (diff) >= 1e-4)
849 { 880 {
850 ev_periodic_stop (EV_A_ w); 881 ev_periodic_stop (EV_A_ w);
851 ev_periodic_start (EV_A_ w); 882 ev_periodic_start (EV_A_ w);
912 { 943 {
913 periodics_reschedule (EV_A); 944 periodics_reschedule (EV_A);
914 945
915 /* adjust timers. this is easy, as the offset is the same for all */ 946 /* adjust timers. this is easy, as the offset is the same for all */
916 for (i = 0; i < timercnt; ++i) 947 for (i = 0; i < timercnt; ++i)
917 timers [i]->at += rt_now - mn_now; 948 ((WT)timers [i])->at += rt_now - mn_now;
918 } 949 }
919 950
920 mn_now = rt_now; 951 mn_now = rt_now;
921 } 952 }
922} 953}
973 { 1004 {
974 block = MAX_BLOCKTIME; 1005 block = MAX_BLOCKTIME;
975 1006
976 if (timercnt) 1007 if (timercnt)
977 { 1008 {
978 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1009 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
979 if (block > to) block = to; 1010 if (block > to) block = to;
980 } 1011 }
981 1012
982 if (periodiccnt) 1013 if (periodiccnt)
983 { 1014 {
984 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1015 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
985 if (block > to) block = to; 1016 if (block > to) block = to;
986 } 1017 }
987 1018
988 if (block < 0.) block = 0.; 1019 if (block < 0.) block = 0.;
989 } 1020 }
1106ev_timer_start (EV_P_ struct ev_timer *w) 1137ev_timer_start (EV_P_ struct ev_timer *w)
1107{ 1138{
1108 if (ev_is_active (w)) 1139 if (ev_is_active (w))
1109 return; 1140 return;
1110 1141
1111 w->at += mn_now; 1142 ((WT)w)->at += mn_now;
1112 1143
1113 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1144 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1114 1145
1115 ev_start (EV_A_ (W)w, ++timercnt); 1146 ev_start (EV_A_ (W)w, ++timercnt);
1116 array_needsize (timers, timermax, timercnt, ); 1147 array_needsize (timers, timermax, timercnt, );
1117 timers [timercnt - 1] = w; 1148 timers [timercnt - 1] = w;
1118 upheap ((WT *)timers, timercnt - 1); 1149 upheap ((WT *)timers, timercnt - 1);
1150
1151 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1119} 1152}
1120 1153
1121void 1154void
1122ev_timer_stop (EV_P_ struct ev_timer *w) 1155ev_timer_stop (EV_P_ struct ev_timer *w)
1123{ 1156{
1124 ev_clear_pending (EV_A_ (W)w); 1157 ev_clear_pending (EV_A_ (W)w);
1125 if (!ev_is_active (w)) 1158 if (!ev_is_active (w))
1126 return; 1159 return;
1127 1160
1161 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1162
1128 if (w->active < timercnt--) 1163 if (((W)w)->active < timercnt--)
1129 { 1164 {
1130 timers [w->active - 1] = timers [timercnt]; 1165 timers [((W)w)->active - 1] = timers [timercnt];
1131 downheap ((WT *)timers, timercnt, w->active - 1); 1166 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1132 } 1167 }
1133 1168
1134 w->at = w->repeat; 1169 ((WT)w)->at = w->repeat;
1135 1170
1136 ev_stop (EV_A_ (W)w); 1171 ev_stop (EV_A_ (W)w);
1137} 1172}
1138 1173
1139void 1174void
1141{ 1176{
1142 if (ev_is_active (w)) 1177 if (ev_is_active (w))
1143 { 1178 {
1144 if (w->repeat) 1179 if (w->repeat)
1145 { 1180 {
1146 w->at = mn_now + w->repeat; 1181 ((WT)w)->at = mn_now + w->repeat;
1147 downheap ((WT *)timers, timercnt, w->active - 1); 1182 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1148 } 1183 }
1149 else 1184 else
1150 ev_timer_stop (EV_A_ w); 1185 ev_timer_stop (EV_A_ w);
1151 } 1186 }
1152 else if (w->repeat) 1187 else if (w->repeat)
1161 1196
1162 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1197 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1163 1198
1164 /* this formula differs from the one in periodic_reify because we do not always round up */ 1199 /* this formula differs from the one in periodic_reify because we do not always round up */
1165 if (w->interval) 1200 if (w->interval)
1166 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1201 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1167 1202
1168 ev_start (EV_A_ (W)w, ++periodiccnt); 1203 ev_start (EV_A_ (W)w, ++periodiccnt);
1169 array_needsize (periodics, periodicmax, periodiccnt, ); 1204 array_needsize (periodics, periodicmax, periodiccnt, );
1170 periodics [periodiccnt - 1] = w; 1205 periodics [periodiccnt - 1] = w;
1171 upheap ((WT *)periodics, periodiccnt - 1); 1206 upheap ((WT *)periodics, periodiccnt - 1);
1207
1208 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1172} 1209}
1173 1210
1174void 1211void
1175ev_periodic_stop (EV_P_ struct ev_periodic *w) 1212ev_periodic_stop (EV_P_ struct ev_periodic *w)
1176{ 1213{
1177 ev_clear_pending (EV_A_ (W)w); 1214 ev_clear_pending (EV_A_ (W)w);
1178 if (!ev_is_active (w)) 1215 if (!ev_is_active (w))
1179 return; 1216 return;
1180 1217
1218 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1219
1181 if (w->active < periodiccnt--) 1220 if (((W)w)->active < periodiccnt--)
1182 { 1221 {
1183 periodics [w->active - 1] = periodics [periodiccnt]; 1222 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1184 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1223 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1185 } 1224 }
1186 1225
1187 ev_stop (EV_A_ (W)w); 1226 ev_stop (EV_A_ (W)w);
1188} 1227}
1189 1228
1203{ 1242{
1204 ev_clear_pending (EV_A_ (W)w); 1243 ev_clear_pending (EV_A_ (W)w);
1205 if (ev_is_active (w)) 1244 if (ev_is_active (w))
1206 return; 1245 return;
1207 1246
1208 idles [w->active - 1] = idles [--idlecnt]; 1247 idles [((W)w)->active - 1] = idles [--idlecnt];
1209 ev_stop (EV_A_ (W)w); 1248 ev_stop (EV_A_ (W)w);
1210} 1249}
1211 1250
1212void 1251void
1213ev_prepare_start (EV_P_ struct ev_prepare *w) 1252ev_prepare_start (EV_P_ struct ev_prepare *w)
1225{ 1264{
1226 ev_clear_pending (EV_A_ (W)w); 1265 ev_clear_pending (EV_A_ (W)w);
1227 if (ev_is_active (w)) 1266 if (ev_is_active (w))
1228 return; 1267 return;
1229 1268
1230 prepares [w->active - 1] = prepares [--preparecnt]; 1269 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1231 ev_stop (EV_A_ (W)w); 1270 ev_stop (EV_A_ (W)w);
1232} 1271}
1233 1272
1234void 1273void
1235ev_check_start (EV_P_ struct ev_check *w) 1274ev_check_start (EV_P_ struct ev_check *w)
1247{ 1286{
1248 ev_clear_pending (EV_A_ (W)w); 1287 ev_clear_pending (EV_A_ (W)w);
1249 if (ev_is_active (w)) 1288 if (ev_is_active (w))
1250 return; 1289 return;
1251 1290
1252 checks [w->active - 1] = checks [--checkcnt]; 1291 checks [((W)w)->active - 1] = checks [--checkcnt];
1253 ev_stop (EV_A_ (W)w); 1292 ev_stop (EV_A_ (W)w);
1254} 1293}
1255 1294
1256#ifndef SA_RESTART 1295#ifndef SA_RESTART
1257# define SA_RESTART 0 1296# define SA_RESTART 0
1270 1309
1271 ev_start (EV_A_ (W)w, 1); 1310 ev_start (EV_A_ (W)w, 1);
1272 array_needsize (signals, signalmax, w->signum, signals_init); 1311 array_needsize (signals, signalmax, w->signum, signals_init);
1273 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1312 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1274 1313
1275 if (!w->next) 1314 if (!((WL)w)->next)
1276 { 1315 {
1277 struct sigaction sa; 1316 struct sigaction sa;
1278 sa.sa_handler = sighandler; 1317 sa.sa_handler = sighandler;
1279 sigfillset (&sa.sa_mask); 1318 sigfillset (&sa.sa_mask);
1280 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1319 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines