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

Comparing libev/ev.c (file contents):
Revision 1.61 by root, Sun Nov 4 19:45:09 2007 UTC vs.
Revision 1.68 by root, Mon Nov 5 20:19:00 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/**/
137typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
138typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
139 147
140static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
141 149
150#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155
142/*****************************************************************************/ 156/*****************************************************************************/
143 157
144typedef struct 158typedef struct
145{ 159{
146 struct ev_watcher_list *head; 160 WL head;
147 unsigned char events; 161 unsigned char events;
148 unsigned char reify; 162 unsigned char reify;
149} ANFD; 163} ANFD;
150 164
151typedef struct 165typedef struct
224 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
225 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
226 cur = newcnt; \ 240 cur = newcnt; \
227 } 241 }
228 242
243#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 }
250
251#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253
229/*****************************************************************************/ 254/*****************************************************************************/
230 255
231static void 256static void
232anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
233{ 258{
298 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 323 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
299 events |= w->events; 324 events |= w->events;
300 325
301 anfd->reify = 0; 326 anfd->reify = 0;
302 327
303 if (anfd->events != events)
304 {
305 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
306 anfd->events = events; 329 anfd->events = events;
307 }
308 } 330 }
309 331
310 fdchangecnt = 0; 332 fdchangecnt = 0;
311} 333}
312 334
349 371
350/* called on ENOMEM in select/poll to kill some fds and retry */ 372/* called on ENOMEM in select/poll to kill some fds and retry */
351static void 373static void
352fd_enomem (EV_P) 374fd_enomem (EV_P)
353{ 375{
354 int fd = anfdmax; 376 int fd;
355 377
356 while (fd--) 378 for (fd = anfdmax; fd--; )
357 if (anfds [fd].events) 379 if (anfds [fd].events)
358 { 380 {
359 close (fd);
360 fd_kill (EV_A_ fd); 381 fd_kill (EV_A_ fd);
361 return; 382 return;
362 } 383 }
363} 384}
364 385
385 WT w = heap [k]; 406 WT w = heap [k];
386 407
387 while (k && heap [k >> 1]->at > w->at) 408 while (k && heap [k >> 1]->at > w->at)
388 { 409 {
389 heap [k] = heap [k >> 1]; 410 heap [k] = heap [k >> 1];
390 heap [k]->active = k + 1; 411 ((W)heap [k])->active = k + 1;
391 k >>= 1; 412 k >>= 1;
392 } 413 }
393 414
394 heap [k] = w; 415 heap [k] = w;
395 heap [k]->active = k + 1; 416 ((W)heap [k])->active = k + 1;
396 417
397} 418}
398 419
399static void 420static void
400downheap (WT *heap, int N, int k) 421downheap (WT *heap, int N, int k)
410 431
411 if (w->at <= heap [j]->at) 432 if (w->at <= heap [j]->at)
412 break; 433 break;
413 434
414 heap [k] = heap [j]; 435 heap [k] = heap [j];
415 heap [k]->active = k + 1; 436 ((W)heap [k])->active = k + 1;
416 k = j; 437 k = j;
417 } 438 }
418 439
419 heap [k] = w; 440 heap [k] = w;
420 heap [k]->active = k + 1; 441 ((W)heap [k])->active = k + 1;
421} 442}
422 443
423/*****************************************************************************/ 444/*****************************************************************************/
424 445
425typedef struct 446typedef struct
426{ 447{
427 struct ev_watcher_list *head; 448 WL head;
428 sig_atomic_t volatile gotsig; 449 sig_atomic_t volatile gotsig;
429} ANSIG; 450} ANSIG;
430 451
431static ANSIG *signals; 452static ANSIG *signals;
432static int signalmax; 453static int signalmax;
448} 469}
449 470
450static void 471static void
451sighandler (int signum) 472sighandler (int signum)
452{ 473{
474#if WIN32
475 signal (signum, sighandler);
476#endif
477
453 signals [signum - 1].gotsig = 1; 478 signals [signum - 1].gotsig = 1;
454 479
455 if (!gotsig) 480 if (!gotsig)
456 { 481 {
457 int old_errno = errno; 482 int old_errno = errno;
462} 487}
463 488
464static void 489static void
465sigcb (EV_P_ struct ev_io *iow, int revents) 490sigcb (EV_P_ struct ev_io *iow, int revents)
466{ 491{
467 struct ev_watcher_list *w; 492 WL w;
468 int signum; 493 int signum;
469 494
470 read (sigpipe [0], &revents, 1); 495 read (sigpipe [0], &revents, 1);
471 gotsig = 0; 496 gotsig = 0;
472 497
514 struct ev_child *w; 539 struct ev_child *w;
515 540
516 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 541 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) 542 if (w->pid == pid || !w->pid)
518 { 543 {
519 w->priority = sw->priority; /* need to do it *now* */ 544 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
520 w->rpid = pid; 545 w->rpid = pid;
521 w->rstatus = status; 546 w->rstatus = status;
522 event (EV_A_ (W)w, EV_CHILD); 547 event (EV_A_ (W)w, EV_CHILD);
523 } 548 }
524} 549}
525 550
526static void 551static void
608 methods = atoi (getenv ("LIBEV_METHODS")); 633 methods = atoi (getenv ("LIBEV_METHODS"));
609 else 634 else
610 methods = EVMETHOD_ANY; 635 methods = EVMETHOD_ANY;
611 636
612 method = 0; 637 method = 0;
638#if EV_USE_WIN32
639 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
640#endif
613#if EV_USE_KQUEUE 641#if EV_USE_KQUEUE
614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 642 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
615#endif 643#endif
616#if EV_USE_EPOLL 644#if EV_USE_EPOLL
617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 645 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626} 654}
627 655
628void 656void
629loop_destroy (EV_P) 657loop_destroy (EV_P)
630{ 658{
659 int i;
660
661#if EV_USE_WIN32
662 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
663#endif
631#if EV_USE_KQUEUE 664#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 665 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif 666#endif
634#if EV_USE_EPOLL 667#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 668 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 671 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
639#endif 672#endif
640#if EV_USE_SELECT 673#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 674 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#endif 675#endif
676
677 for (i = NUMPRI; i--; )
678 array_free (pending, [i]);
679
680 array_free (fdchange, );
681 array_free (timer, );
682 array_free (periodic, );
683 array_free (idle, );
684 array_free (prepare, );
685 array_free (check, );
643 686
644 method = 0; 687 method = 0;
645 /*TODO*/ 688 /*TODO*/
646} 689}
647 690
790} 833}
791 834
792static void 835static void
793timers_reify (EV_P) 836timers_reify (EV_P)
794{ 837{
795 while (timercnt && timers [0]->at <= mn_now) 838 while (timercnt && ((WT)timers [0])->at <= mn_now)
796 { 839 {
797 struct ev_timer *w = timers [0]; 840 struct ev_timer *w = timers [0];
798 841
799 assert (("inactive timer on timer heap detected", ev_is_active (w))); 842 assert (("inactive timer on timer heap detected", ev_is_active (w)));
800 843
801 /* first reschedule or stop timer */ 844 /* first reschedule or stop timer */
802 if (w->repeat) 845 if (w->repeat)
803 { 846 {
804 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 847 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
805 w->at = mn_now + w->repeat; 848 ((WT)w)->at = mn_now + w->repeat;
806 downheap ((WT *)timers, timercnt, 0); 849 downheap ((WT *)timers, timercnt, 0);
807 } 850 }
808 else 851 else
809 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 852 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
810 853
813} 856}
814 857
815static void 858static void
816periodics_reify (EV_P) 859periodics_reify (EV_P)
817{ 860{
818 while (periodiccnt && periodics [0]->at <= rt_now) 861 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
819 { 862 {
820 struct ev_periodic *w = periodics [0]; 863 struct ev_periodic *w = periodics [0];
821 864
822 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 865 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
823 866
824 /* first reschedule or stop timer */ 867 /* first reschedule or stop timer */
825 if (w->interval) 868 if (w->interval)
826 { 869 {
827 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 870 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
828 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 871 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
829 downheap ((WT *)periodics, periodiccnt, 0); 872 downheap ((WT *)periodics, periodiccnt, 0);
830 } 873 }
831 else 874 else
832 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 875 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
833 876
845 { 888 {
846 struct ev_periodic *w = periodics [i]; 889 struct ev_periodic *w = periodics [i];
847 890
848 if (w->interval) 891 if (w->interval)
849 { 892 {
850 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 893 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
851 894
852 if (fabs (diff) >= 1e-4) 895 if (fabs (diff) >= 1e-4)
853 { 896 {
854 ev_periodic_stop (EV_A_ w); 897 ev_periodic_stop (EV_A_ w);
855 ev_periodic_start (EV_A_ w); 898 ev_periodic_start (EV_A_ w);
916 { 959 {
917 periodics_reschedule (EV_A); 960 periodics_reschedule (EV_A);
918 961
919 /* adjust timers. this is easy, as the offset is the same for all */ 962 /* adjust timers. this is easy, as the offset is the same for all */
920 for (i = 0; i < timercnt; ++i) 963 for (i = 0; i < timercnt; ++i)
921 timers [i]->at += rt_now - mn_now; 964 ((WT)timers [i])->at += rt_now - mn_now;
922 } 965 }
923 966
924 mn_now = rt_now; 967 mn_now = rt_now;
925 } 968 }
926} 969}
977 { 1020 {
978 block = MAX_BLOCKTIME; 1021 block = MAX_BLOCKTIME;
979 1022
980 if (timercnt) 1023 if (timercnt)
981 { 1024 {
982 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1025 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
983 if (block > to) block = to; 1026 if (block > to) block = to;
984 } 1027 }
985 1028
986 if (periodiccnt) 1029 if (periodiccnt)
987 { 1030 {
988 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1031 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
989 if (block > to) block = to; 1032 if (block > to) block = to;
990 } 1033 }
991 1034
992 if (block < 0.) block = 0.; 1035 if (block < 0.) block = 0.;
993 } 1036 }
1110ev_timer_start (EV_P_ struct ev_timer *w) 1153ev_timer_start (EV_P_ struct ev_timer *w)
1111{ 1154{
1112 if (ev_is_active (w)) 1155 if (ev_is_active (w))
1113 return; 1156 return;
1114 1157
1115 w->at += mn_now; 1158 ((WT)w)->at += mn_now;
1116 1159
1117 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1160 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1118 1161
1119 ev_start (EV_A_ (W)w, ++timercnt); 1162 ev_start (EV_A_ (W)w, ++timercnt);
1120 array_needsize (timers, timermax, timercnt, ); 1163 array_needsize (timers, timermax, timercnt, );
1121 timers [timercnt - 1] = w; 1164 timers [timercnt - 1] = w;
1122 upheap ((WT *)timers, timercnt - 1); 1165 upheap ((WT *)timers, timercnt - 1);
1166
1167 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1123} 1168}
1124 1169
1125void 1170void
1126ev_timer_stop (EV_P_ struct ev_timer *w) 1171ev_timer_stop (EV_P_ struct ev_timer *w)
1127{ 1172{
1128 ev_clear_pending (EV_A_ (W)w); 1173 ev_clear_pending (EV_A_ (W)w);
1129 if (!ev_is_active (w)) 1174 if (!ev_is_active (w))
1130 return; 1175 return;
1131 1176
1177 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1178
1132 if (w->active < timercnt--) 1179 if (((W)w)->active < timercnt--)
1133 { 1180 {
1134 timers [w->active - 1] = timers [timercnt]; 1181 timers [((W)w)->active - 1] = timers [timercnt];
1135 downheap ((WT *)timers, timercnt, w->active - 1); 1182 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1136 } 1183 }
1137 1184
1138 w->at = w->repeat; 1185 ((WT)w)->at = w->repeat;
1139 1186
1140 ev_stop (EV_A_ (W)w); 1187 ev_stop (EV_A_ (W)w);
1141} 1188}
1142 1189
1143void 1190void
1145{ 1192{
1146 if (ev_is_active (w)) 1193 if (ev_is_active (w))
1147 { 1194 {
1148 if (w->repeat) 1195 if (w->repeat)
1149 { 1196 {
1150 w->at = mn_now + w->repeat; 1197 ((WT)w)->at = mn_now + w->repeat;
1151 downheap ((WT *)timers, timercnt, w->active - 1); 1198 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1152 } 1199 }
1153 else 1200 else
1154 ev_timer_stop (EV_A_ w); 1201 ev_timer_stop (EV_A_ w);
1155 } 1202 }
1156 else if (w->repeat) 1203 else if (w->repeat)
1165 1212
1166 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1213 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1167 1214
1168 /* this formula differs from the one in periodic_reify because we do not always round up */ 1215 /* this formula differs from the one in periodic_reify because we do not always round up */
1169 if (w->interval) 1216 if (w->interval)
1170 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1217 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1171 1218
1172 ev_start (EV_A_ (W)w, ++periodiccnt); 1219 ev_start (EV_A_ (W)w, ++periodiccnt);
1173 array_needsize (periodics, periodicmax, periodiccnt, ); 1220 array_needsize (periodics, periodicmax, periodiccnt, );
1174 periodics [periodiccnt - 1] = w; 1221 periodics [periodiccnt - 1] = w;
1175 upheap ((WT *)periodics, periodiccnt - 1); 1222 upheap ((WT *)periodics, periodiccnt - 1);
1223
1224 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1176} 1225}
1177 1226
1178void 1227void
1179ev_periodic_stop (EV_P_ struct ev_periodic *w) 1228ev_periodic_stop (EV_P_ struct ev_periodic *w)
1180{ 1229{
1181 ev_clear_pending (EV_A_ (W)w); 1230 ev_clear_pending (EV_A_ (W)w);
1182 if (!ev_is_active (w)) 1231 if (!ev_is_active (w))
1183 return; 1232 return;
1184 1233
1234 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1235
1185 if (w->active < periodiccnt--) 1236 if (((W)w)->active < periodiccnt--)
1186 { 1237 {
1187 periodics [w->active - 1] = periodics [periodiccnt]; 1238 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1188 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1239 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1189 } 1240 }
1190 1241
1191 ev_stop (EV_A_ (W)w); 1242 ev_stop (EV_A_ (W)w);
1192} 1243}
1193 1244
1207{ 1258{
1208 ev_clear_pending (EV_A_ (W)w); 1259 ev_clear_pending (EV_A_ (W)w);
1209 if (ev_is_active (w)) 1260 if (ev_is_active (w))
1210 return; 1261 return;
1211 1262
1212 idles [w->active - 1] = idles [--idlecnt]; 1263 idles [((W)w)->active - 1] = idles [--idlecnt];
1213 ev_stop (EV_A_ (W)w); 1264 ev_stop (EV_A_ (W)w);
1214} 1265}
1215 1266
1216void 1267void
1217ev_prepare_start (EV_P_ struct ev_prepare *w) 1268ev_prepare_start (EV_P_ struct ev_prepare *w)
1229{ 1280{
1230 ev_clear_pending (EV_A_ (W)w); 1281 ev_clear_pending (EV_A_ (W)w);
1231 if (ev_is_active (w)) 1282 if (ev_is_active (w))
1232 return; 1283 return;
1233 1284
1234 prepares [w->active - 1] = prepares [--preparecnt]; 1285 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1235 ev_stop (EV_A_ (W)w); 1286 ev_stop (EV_A_ (W)w);
1236} 1287}
1237 1288
1238void 1289void
1239ev_check_start (EV_P_ struct ev_check *w) 1290ev_check_start (EV_P_ struct ev_check *w)
1251{ 1302{
1252 ev_clear_pending (EV_A_ (W)w); 1303 ev_clear_pending (EV_A_ (W)w);
1253 if (ev_is_active (w)) 1304 if (ev_is_active (w))
1254 return; 1305 return;
1255 1306
1256 checks [w->active - 1] = checks [--checkcnt]; 1307 checks [((W)w)->active - 1] = checks [--checkcnt];
1257 ev_stop (EV_A_ (W)w); 1308 ev_stop (EV_A_ (W)w);
1258} 1309}
1259 1310
1260#ifndef SA_RESTART 1311#ifndef SA_RESTART
1261# define SA_RESTART 0 1312# define SA_RESTART 0
1274 1325
1275 ev_start (EV_A_ (W)w, 1); 1326 ev_start (EV_A_ (W)w, 1);
1276 array_needsize (signals, signalmax, w->signum, signals_init); 1327 array_needsize (signals, signalmax, w->signum, signals_init);
1277 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1328 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1278 1329
1279 if (!w->next) 1330 if (!((WL)w)->next)
1280 { 1331 {
1332#if WIN32
1333 signal (w->signum, sighandler);
1334#else
1281 struct sigaction sa; 1335 struct sigaction sa;
1282 sa.sa_handler = sighandler; 1336 sa.sa_handler = sighandler;
1283 sigfillset (&sa.sa_mask); 1337 sigfillset (&sa.sa_mask);
1284 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1338 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1285 sigaction (w->signum, &sa, 0); 1339 sigaction (w->signum, &sa, 0);
1340#endif
1286 } 1341 }
1287} 1342}
1288 1343
1289void 1344void
1290ev_signal_stop (EV_P_ struct ev_signal *w) 1345ev_signal_stop (EV_P_ struct ev_signal *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines