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.67 by root, Mon Nov 5 16:42:15 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/**/
136typedef struct ev_watcher *W; 144typedef struct ev_watcher *W;
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? */
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
141 155
142/*****************************************************************************/ 156/*****************************************************************************/
143 157
144typedef struct 158typedef struct
145{ 159{
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); 381 close (fd);
360 fd_kill (EV_A_ fd); 382 fd_kill (EV_A_ fd);
361 return; 383 return;
385 WT w = heap [k]; 407 WT w = heap [k];
386 408
387 while (k && heap [k >> 1]->at > w->at) 409 while (k && heap [k >> 1]->at > w->at)
388 { 410 {
389 heap [k] = heap [k >> 1]; 411 heap [k] = heap [k >> 1];
390 heap [k]->active = k + 1; 412 ((W)heap [k])->active = k + 1;
391 k >>= 1; 413 k >>= 1;
392 } 414 }
393 415
394 heap [k] = w; 416 heap [k] = w;
395 heap [k]->active = k + 1; 417 ((W)heap [k])->active = k + 1;
396 418
397} 419}
398 420
399static void 421static void
400downheap (WT *heap, int N, int k) 422downheap (WT *heap, int N, int k)
410 432
411 if (w->at <= heap [j]->at) 433 if (w->at <= heap [j]->at)
412 break; 434 break;
413 435
414 heap [k] = heap [j]; 436 heap [k] = heap [j];
415 heap [k]->active = k + 1; 437 ((W)heap [k])->active = k + 1;
416 k = j; 438 k = j;
417 } 439 }
418 440
419 heap [k] = w; 441 heap [k] = w;
420 heap [k]->active = k + 1; 442 ((W)heap [k])->active = k + 1;
421} 443}
422 444
423/*****************************************************************************/ 445/*****************************************************************************/
424 446
425typedef struct 447typedef struct
448} 470}
449 471
450static void 472static void
451sighandler (int signum) 473sighandler (int signum)
452{ 474{
475#if WIN32
476 signal (signum, sighandler);
477#endif
478
453 signals [signum - 1].gotsig = 1; 479 signals [signum - 1].gotsig = 1;
454 480
455 if (!gotsig) 481 if (!gotsig)
456 { 482 {
457 int old_errno = errno; 483 int old_errno = errno;
514 struct ev_child *w; 540 struct ev_child *w;
515 541
516 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 542 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) 543 if (w->pid == pid || !w->pid)
518 { 544 {
519 w->priority = sw->priority; /* need to do it *now* */ 545 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
520 w->rpid = pid; 546 w->rpid = pid;
521 w->rstatus = status; 547 w->rstatus = status;
522 event (EV_A_ (W)w, EV_CHILD); 548 event (EV_A_ (W)w, EV_CHILD);
523 } 549 }
524} 550}
525 551
526static void 552static void
608 methods = atoi (getenv ("LIBEV_METHODS")); 634 methods = atoi (getenv ("LIBEV_METHODS"));
609 else 635 else
610 methods = EVMETHOD_ANY; 636 methods = EVMETHOD_ANY;
611 637
612 method = 0; 638 method = 0;
639#if EV_USE_WIN32
640 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
641#endif
613#if EV_USE_KQUEUE 642#if EV_USE_KQUEUE
614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 643 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
615#endif 644#endif
616#if EV_USE_EPOLL 645#if EV_USE_EPOLL
617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 646 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626} 655}
627 656
628void 657void
629loop_destroy (EV_P) 658loop_destroy (EV_P)
630{ 659{
660 int i;
661
662#if EV_USE_WIN32
663 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
664#endif
631#if EV_USE_KQUEUE 665#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 666 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif 667#endif
634#if EV_USE_EPOLL 668#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 669 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 672 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
639#endif 673#endif
640#if EV_USE_SELECT 674#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 675 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#endif 676#endif
677
678 for (i = NUMPRI; i--; )
679 array_free (pending, [i]);
680
681 array_free (fdchange, );
682 array_free (timer, );
683 array_free (periodic, );
684 array_free (idle, );
685 array_free (prepare, );
686 array_free (check, );
643 687
644 method = 0; 688 method = 0;
645 /*TODO*/ 689 /*TODO*/
646} 690}
647 691
790} 834}
791 835
792static void 836static void
793timers_reify (EV_P) 837timers_reify (EV_P)
794{ 838{
795 while (timercnt && timers [0]->at <= mn_now) 839 while (timercnt && ((WT)timers [0])->at <= mn_now)
796 { 840 {
797 struct ev_timer *w = timers [0]; 841 struct ev_timer *w = timers [0];
842
843 assert (("inactive timer on timer heap detected", ev_is_active (w)));
798 844
799 /* first reschedule or stop timer */ 845 /* first reschedule or stop timer */
800 if (w->repeat) 846 if (w->repeat)
801 { 847 {
802 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 848 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
803 w->at = mn_now + w->repeat; 849 ((WT)w)->at = mn_now + w->repeat;
804 downheap ((WT *)timers, timercnt, 0); 850 downheap ((WT *)timers, timercnt, 0);
805 } 851 }
806 else 852 else
807 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 853 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
808 854
811} 857}
812 858
813static void 859static void
814periodics_reify (EV_P) 860periodics_reify (EV_P)
815{ 861{
816 while (periodiccnt && periodics [0]->at <= rt_now) 862 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
817 { 863 {
818 struct ev_periodic *w = periodics [0]; 864 struct ev_periodic *w = periodics [0];
865
866 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
819 867
820 /* first reschedule or stop timer */ 868 /* first reschedule or stop timer */
821 if (w->interval) 869 if (w->interval)
822 { 870 {
823 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 871 ((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)); 872 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
825 downheap ((WT *)periodics, periodiccnt, 0); 873 downheap ((WT *)periodics, periodiccnt, 0);
826 } 874 }
827 else 875 else
828 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 876 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
829 877
841 { 889 {
842 struct ev_periodic *w = periodics [i]; 890 struct ev_periodic *w = periodics [i];
843 891
844 if (w->interval) 892 if (w->interval)
845 { 893 {
846 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 894 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
847 895
848 if (fabs (diff) >= 1e-4) 896 if (fabs (diff) >= 1e-4)
849 { 897 {
850 ev_periodic_stop (EV_A_ w); 898 ev_periodic_stop (EV_A_ w);
851 ev_periodic_start (EV_A_ w); 899 ev_periodic_start (EV_A_ w);
912 { 960 {
913 periodics_reschedule (EV_A); 961 periodics_reschedule (EV_A);
914 962
915 /* adjust timers. this is easy, as the offset is the same for all */ 963 /* adjust timers. this is easy, as the offset is the same for all */
916 for (i = 0; i < timercnt; ++i) 964 for (i = 0; i < timercnt; ++i)
917 timers [i]->at += rt_now - mn_now; 965 ((WT)timers [i])->at += rt_now - mn_now;
918 } 966 }
919 967
920 mn_now = rt_now; 968 mn_now = rt_now;
921 } 969 }
922} 970}
973 { 1021 {
974 block = MAX_BLOCKTIME; 1022 block = MAX_BLOCKTIME;
975 1023
976 if (timercnt) 1024 if (timercnt)
977 { 1025 {
978 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1026 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
979 if (block > to) block = to; 1027 if (block > to) block = to;
980 } 1028 }
981 1029
982 if (periodiccnt) 1030 if (periodiccnt)
983 { 1031 {
984 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1032 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
985 if (block > to) block = to; 1033 if (block > to) block = to;
986 } 1034 }
987 1035
988 if (block < 0.) block = 0.; 1036 if (block < 0.) block = 0.;
989 } 1037 }
1106ev_timer_start (EV_P_ struct ev_timer *w) 1154ev_timer_start (EV_P_ struct ev_timer *w)
1107{ 1155{
1108 if (ev_is_active (w)) 1156 if (ev_is_active (w))
1109 return; 1157 return;
1110 1158
1111 w->at += mn_now; 1159 ((WT)w)->at += mn_now;
1112 1160
1113 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1161 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1114 1162
1115 ev_start (EV_A_ (W)w, ++timercnt); 1163 ev_start (EV_A_ (W)w, ++timercnt);
1116 array_needsize (timers, timermax, timercnt, ); 1164 array_needsize (timers, timermax, timercnt, );
1117 timers [timercnt - 1] = w; 1165 timers [timercnt - 1] = w;
1118 upheap ((WT *)timers, timercnt - 1); 1166 upheap ((WT *)timers, timercnt - 1);
1167
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1119} 1169}
1120 1170
1121void 1171void
1122ev_timer_stop (EV_P_ struct ev_timer *w) 1172ev_timer_stop (EV_P_ struct ev_timer *w)
1123{ 1173{
1124 ev_clear_pending (EV_A_ (W)w); 1174 ev_clear_pending (EV_A_ (W)w);
1125 if (!ev_is_active (w)) 1175 if (!ev_is_active (w))
1126 return; 1176 return;
1127 1177
1178 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1179
1128 if (w->active < timercnt--) 1180 if (((W)w)->active < timercnt--)
1129 { 1181 {
1130 timers [w->active - 1] = timers [timercnt]; 1182 timers [((W)w)->active - 1] = timers [timercnt];
1131 downheap ((WT *)timers, timercnt, w->active - 1); 1183 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1132 } 1184 }
1133 1185
1134 w->at = w->repeat; 1186 ((WT)w)->at = w->repeat;
1135 1187
1136 ev_stop (EV_A_ (W)w); 1188 ev_stop (EV_A_ (W)w);
1137} 1189}
1138 1190
1139void 1191void
1141{ 1193{
1142 if (ev_is_active (w)) 1194 if (ev_is_active (w))
1143 { 1195 {
1144 if (w->repeat) 1196 if (w->repeat)
1145 { 1197 {
1146 w->at = mn_now + w->repeat; 1198 ((WT)w)->at = mn_now + w->repeat;
1147 downheap ((WT *)timers, timercnt, w->active - 1); 1199 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1148 } 1200 }
1149 else 1201 else
1150 ev_timer_stop (EV_A_ w); 1202 ev_timer_stop (EV_A_ w);
1151 } 1203 }
1152 else if (w->repeat) 1204 else if (w->repeat)
1161 1213
1162 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1214 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1163 1215
1164 /* this formula differs from the one in periodic_reify because we do not always round up */ 1216 /* this formula differs from the one in periodic_reify because we do not always round up */
1165 if (w->interval) 1217 if (w->interval)
1166 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1218 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1167 1219
1168 ev_start (EV_A_ (W)w, ++periodiccnt); 1220 ev_start (EV_A_ (W)w, ++periodiccnt);
1169 array_needsize (periodics, periodicmax, periodiccnt, ); 1221 array_needsize (periodics, periodicmax, periodiccnt, );
1170 periodics [periodiccnt - 1] = w; 1222 periodics [periodiccnt - 1] = w;
1171 upheap ((WT *)periodics, periodiccnt - 1); 1223 upheap ((WT *)periodics, periodiccnt - 1);
1224
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1172} 1226}
1173 1227
1174void 1228void
1175ev_periodic_stop (EV_P_ struct ev_periodic *w) 1229ev_periodic_stop (EV_P_ struct ev_periodic *w)
1176{ 1230{
1177 ev_clear_pending (EV_A_ (W)w); 1231 ev_clear_pending (EV_A_ (W)w);
1178 if (!ev_is_active (w)) 1232 if (!ev_is_active (w))
1179 return; 1233 return;
1180 1234
1235 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1236
1181 if (w->active < periodiccnt--) 1237 if (((W)w)->active < periodiccnt--)
1182 { 1238 {
1183 periodics [w->active - 1] = periodics [periodiccnt]; 1239 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1184 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1240 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1185 } 1241 }
1186 1242
1187 ev_stop (EV_A_ (W)w); 1243 ev_stop (EV_A_ (W)w);
1188} 1244}
1189 1245
1203{ 1259{
1204 ev_clear_pending (EV_A_ (W)w); 1260 ev_clear_pending (EV_A_ (W)w);
1205 if (ev_is_active (w)) 1261 if (ev_is_active (w))
1206 return; 1262 return;
1207 1263
1208 idles [w->active - 1] = idles [--idlecnt]; 1264 idles [((W)w)->active - 1] = idles [--idlecnt];
1209 ev_stop (EV_A_ (W)w); 1265 ev_stop (EV_A_ (W)w);
1210} 1266}
1211 1267
1212void 1268void
1213ev_prepare_start (EV_P_ struct ev_prepare *w) 1269ev_prepare_start (EV_P_ struct ev_prepare *w)
1225{ 1281{
1226 ev_clear_pending (EV_A_ (W)w); 1282 ev_clear_pending (EV_A_ (W)w);
1227 if (ev_is_active (w)) 1283 if (ev_is_active (w))
1228 return; 1284 return;
1229 1285
1230 prepares [w->active - 1] = prepares [--preparecnt]; 1286 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1231 ev_stop (EV_A_ (W)w); 1287 ev_stop (EV_A_ (W)w);
1232} 1288}
1233 1289
1234void 1290void
1235ev_check_start (EV_P_ struct ev_check *w) 1291ev_check_start (EV_P_ struct ev_check *w)
1247{ 1303{
1248 ev_clear_pending (EV_A_ (W)w); 1304 ev_clear_pending (EV_A_ (W)w);
1249 if (ev_is_active (w)) 1305 if (ev_is_active (w))
1250 return; 1306 return;
1251 1307
1252 checks [w->active - 1] = checks [--checkcnt]; 1308 checks [((W)w)->active - 1] = checks [--checkcnt];
1253 ev_stop (EV_A_ (W)w); 1309 ev_stop (EV_A_ (W)w);
1254} 1310}
1255 1311
1256#ifndef SA_RESTART 1312#ifndef SA_RESTART
1257# define SA_RESTART 0 1313# define SA_RESTART 0
1270 1326
1271 ev_start (EV_A_ (W)w, 1); 1327 ev_start (EV_A_ (W)w, 1);
1272 array_needsize (signals, signalmax, w->signum, signals_init); 1328 array_needsize (signals, signalmax, w->signum, signals_init);
1273 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1329 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1274 1330
1275 if (!w->next) 1331 if (!((WL)w)->next)
1276 { 1332 {
1333#if WIN32
1334 signal (w->signum, sighandler);
1335#else
1277 struct sigaction sa; 1336 struct sigaction sa;
1278 sa.sa_handler = sighandler; 1337 sa.sa_handler = sighandler;
1279 sigfillset (&sa.sa_mask); 1338 sigfillset (&sa.sa_mask);
1280 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1339 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1281 sigaction (w->signum, &sa, 0); 1340 sigaction (w->signum, &sa, 0);
1341#endif
1282 } 1342 }
1283} 1343}
1284 1344
1285void 1345void
1286ev_signal_stop (EV_P_ struct ev_signal *w) 1346ev_signal_stop (EV_P_ struct ev_signal *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines