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

Comparing libev/ev.c (file contents):
Revision 1.62 by root, Sun Nov 4 20:38:07 2007 UTC vs.
Revision 1.68 by root, Mon Nov 5 20:19:00 2007 UTC

145typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
147 147
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 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
150/*****************************************************************************/ 156/*****************************************************************************/
151 157
152typedef struct 158typedef struct
153{ 159{
154 struct ev_watcher_list *head; 160 WL head;
155 unsigned char events; 161 unsigned char events;
156 unsigned char reify; 162 unsigned char reify;
157} ANFD; 163} ANFD;
158 164
159typedef struct 165typedef struct
232 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
233 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
234 cur = newcnt; \ 240 cur = newcnt; \
235 } 241 }
236 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
237/*****************************************************************************/ 254/*****************************************************************************/
238 255
239static void 256static void
240anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
241{ 258{
306 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)
307 events |= w->events; 324 events |= w->events;
308 325
309 anfd->reify = 0; 326 anfd->reify = 0;
310 327
311 if (anfd->events != events)
312 {
313 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
314 anfd->events = events; 329 anfd->events = events;
315 }
316 } 330 }
317 331
318 fdchangecnt = 0; 332 fdchangecnt = 0;
319} 333}
320 334
362 int fd; 376 int fd;
363 377
364 for (fd = anfdmax; fd--; ) 378 for (fd = anfdmax; fd--; )
365 if (anfds [fd].events) 379 if (anfds [fd].events)
366 { 380 {
367 close (fd);
368 fd_kill (EV_A_ fd); 381 fd_kill (EV_A_ fd);
369 return; 382 return;
370 } 383 }
371} 384}
372 385
430 443
431/*****************************************************************************/ 444/*****************************************************************************/
432 445
433typedef struct 446typedef struct
434{ 447{
435 struct ev_watcher_list *head; 448 WL head;
436 sig_atomic_t volatile gotsig; 449 sig_atomic_t volatile gotsig;
437} ANSIG; 450} ANSIG;
438 451
439static ANSIG *signals; 452static ANSIG *signals;
440static int signalmax; 453static int signalmax;
456} 469}
457 470
458static void 471static void
459sighandler (int signum) 472sighandler (int signum)
460{ 473{
474#if WIN32
475 signal (signum, sighandler);
476#endif
477
461 signals [signum - 1].gotsig = 1; 478 signals [signum - 1].gotsig = 1;
462 479
463 if (!gotsig) 480 if (!gotsig)
464 { 481 {
465 int old_errno = errno; 482 int old_errno = errno;
470} 487}
471 488
472static void 489static void
473sigcb (EV_P_ struct ev_io *iow, int revents) 490sigcb (EV_P_ struct ev_io *iow, int revents)
474{ 491{
475 struct ev_watcher_list *w; 492 WL w;
476 int signum; 493 int signum;
477 494
478 read (sigpipe [0], &revents, 1); 495 read (sigpipe [0], &revents, 1);
479 gotsig = 0; 496 gotsig = 0;
480 497
522 struct ev_child *w; 539 struct ev_child *w;
523 540
524 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)
525 if (w->pid == pid || !w->pid) 542 if (w->pid == pid || !w->pid)
526 { 543 {
527 w->priority = sw->priority; /* need to do it *now* */ 544 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
528 w->rpid = pid; 545 w->rpid = pid;
529 w->rstatus = status; 546 w->rstatus = status;
530 event (EV_A_ (W)w, EV_CHILD); 547 event (EV_A_ (W)w, EV_CHILD);
531 } 548 }
532} 549}
533 550
534static void 551static void
637} 654}
638 655
639void 656void
640loop_destroy (EV_P) 657loop_destroy (EV_P)
641{ 658{
659 int i;
660
642#if EV_USE_WIN32 661#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 662 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif 663#endif
645#if EV_USE_KQUEUE 664#if EV_USE_KQUEUE
646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 665 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
652 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 671 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
653#endif 672#endif
654#if EV_USE_SELECT 673#if EV_USE_SELECT
655 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 674 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
656#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, );
657 686
658 method = 0; 687 method = 0;
659 /*TODO*/ 688 /*TODO*/
660} 689}
661 690
804} 833}
805 834
806static void 835static void
807timers_reify (EV_P) 836timers_reify (EV_P)
808{ 837{
809 while (timercnt && timers [0]->at <= mn_now) 838 while (timercnt && ((WT)timers [0])->at <= mn_now)
810 { 839 {
811 struct ev_timer *w = timers [0]; 840 struct ev_timer *w = timers [0];
812 841
813 assert (("inactive timer on timer heap detected", ev_is_active (w))); 842 assert (("inactive timer on timer heap detected", ev_is_active (w)));
814 843
815 /* first reschedule or stop timer */ 844 /* first reschedule or stop timer */
816 if (w->repeat) 845 if (w->repeat)
817 { 846 {
818 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.));
819 w->at = mn_now + w->repeat; 848 ((WT)w)->at = mn_now + w->repeat;
820 downheap ((WT *)timers, timercnt, 0); 849 downheap ((WT *)timers, timercnt, 0);
821 } 850 }
822 else 851 else
823 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 852 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
824 853
827} 856}
828 857
829static void 858static void
830periodics_reify (EV_P) 859periodics_reify (EV_P)
831{ 860{
832 while (periodiccnt && periodics [0]->at <= rt_now) 861 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
833 { 862 {
834 struct ev_periodic *w = periodics [0]; 863 struct ev_periodic *w = periodics [0];
835 864
836 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 865 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
837 866
838 /* first reschedule or stop timer */ 867 /* first reschedule or stop timer */
839 if (w->interval) 868 if (w->interval)
840 { 869 {
841 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;
842 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));
843 downheap ((WT *)periodics, periodiccnt, 0); 872 downheap ((WT *)periodics, periodiccnt, 0);
844 } 873 }
845 else 874 else
846 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 875 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
847 876
859 { 888 {
860 struct ev_periodic *w = periodics [i]; 889 struct ev_periodic *w = periodics [i];
861 890
862 if (w->interval) 891 if (w->interval)
863 { 892 {
864 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;
865 894
866 if (fabs (diff) >= 1e-4) 895 if (fabs (diff) >= 1e-4)
867 { 896 {
868 ev_periodic_stop (EV_A_ w); 897 ev_periodic_stop (EV_A_ w);
869 ev_periodic_start (EV_A_ w); 898 ev_periodic_start (EV_A_ w);
930 { 959 {
931 periodics_reschedule (EV_A); 960 periodics_reschedule (EV_A);
932 961
933 /* 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 */
934 for (i = 0; i < timercnt; ++i) 963 for (i = 0; i < timercnt; ++i)
935 timers [i]->at += rt_now - mn_now; 964 ((WT)timers [i])->at += rt_now - mn_now;
936 } 965 }
937 966
938 mn_now = rt_now; 967 mn_now = rt_now;
939 } 968 }
940} 969}
991 { 1020 {
992 block = MAX_BLOCKTIME; 1021 block = MAX_BLOCKTIME;
993 1022
994 if (timercnt) 1023 if (timercnt)
995 { 1024 {
996 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1025 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
997 if (block > to) block = to; 1026 if (block > to) block = to;
998 } 1027 }
999 1028
1000 if (periodiccnt) 1029 if (periodiccnt)
1001 { 1030 {
1002 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1031 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
1003 if (block > to) block = to; 1032 if (block > to) block = to;
1004 } 1033 }
1005 1034
1006 if (block < 0.) block = 0.; 1035 if (block < 0.) block = 0.;
1007 } 1036 }
1124ev_timer_start (EV_P_ struct ev_timer *w) 1153ev_timer_start (EV_P_ struct ev_timer *w)
1125{ 1154{
1126 if (ev_is_active (w)) 1155 if (ev_is_active (w))
1127 return; 1156 return;
1128 1157
1129 w->at += mn_now; 1158 ((WT)w)->at += mn_now;
1130 1159
1131 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.));
1132 1161
1133 ev_start (EV_A_ (W)w, ++timercnt); 1162 ev_start (EV_A_ (W)w, ++timercnt);
1134 array_needsize (timers, timermax, timercnt, ); 1163 array_needsize (timers, timermax, timercnt, );
1151 { 1180 {
1152 timers [((W)w)->active - 1] = timers [timercnt]; 1181 timers [((W)w)->active - 1] = timers [timercnt];
1153 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1182 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1154 } 1183 }
1155 1184
1156 w->at = w->repeat; 1185 ((WT)w)->at = w->repeat;
1157 1186
1158 ev_stop (EV_A_ (W)w); 1187 ev_stop (EV_A_ (W)w);
1159} 1188}
1160 1189
1161void 1190void
1163{ 1192{
1164 if (ev_is_active (w)) 1193 if (ev_is_active (w))
1165 { 1194 {
1166 if (w->repeat) 1195 if (w->repeat)
1167 { 1196 {
1168 w->at = mn_now + w->repeat; 1197 ((WT)w)->at = mn_now + w->repeat;
1169 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1198 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1170 } 1199 }
1171 else 1200 else
1172 ev_timer_stop (EV_A_ w); 1201 ev_timer_stop (EV_A_ w);
1173 } 1202 }
1183 1212
1184 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.));
1185 1214
1186 /* 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 */
1187 if (w->interval) 1216 if (w->interval)
1188 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;
1189 1218
1190 ev_start (EV_A_ (W)w, ++periodiccnt); 1219 ev_start (EV_A_ (W)w, ++periodiccnt);
1191 array_needsize (periodics, periodicmax, periodiccnt, ); 1220 array_needsize (periodics, periodicmax, periodiccnt, );
1192 periodics [periodiccnt - 1] = w; 1221 periodics [periodiccnt - 1] = w;
1193 upheap ((WT *)periodics, periodiccnt - 1); 1222 upheap ((WT *)periodics, periodiccnt - 1);
1296 1325
1297 ev_start (EV_A_ (W)w, 1); 1326 ev_start (EV_A_ (W)w, 1);
1298 array_needsize (signals, signalmax, w->signum, signals_init); 1327 array_needsize (signals, signalmax, w->signum, signals_init);
1299 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1328 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1300 1329
1301 if (!w->next) 1330 if (!((WL)w)->next)
1302 { 1331 {
1332#if WIN32
1333 signal (w->signum, sighandler);
1334#else
1303 struct sigaction sa; 1335 struct sigaction sa;
1304 sa.sa_handler = sighandler; 1336 sa.sa_handler = sighandler;
1305 sigfillset (&sa.sa_mask); 1337 sigfillset (&sa.sa_mask);
1306 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 */
1307 sigaction (w->signum, &sa, 0); 1339 sigaction (w->signum, &sa, 0);
1340#endif
1308 } 1341 }
1309} 1342}
1310 1343
1311void 1344void
1312ev_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