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

Comparing libev/ev.c (file contents):
Revision 1.75 by root, Tue Nov 6 19:29:20 2007 UTC vs.
Revision 1.79 by root, Fri Nov 9 15:15:20 2007 UTC

316 316
317 ++base; 317 ++base;
318 } 318 }
319} 319}
320 320
321static void 321void
322event (EV_P_ W w, int events) 322ev_feed_event (EV_P_ void *w, int revents)
323{ 323{
324 W w_ = (W)w;
325
324 if (w->pending) 326 if (w_->pending)
325 { 327 {
326 pendings [ABSPRI (w)][w->pending - 1].events |= events; 328 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
327 return; 329 return;
328 } 330 }
329 331
330 w->pending = ++pendingcnt [ABSPRI (w)]; 332 w_->pending = ++pendingcnt [ABSPRI (w_)];
331 array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 333 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w; 334 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
333 pendings [ABSPRI (w)][w->pending - 1].events = events; 335 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
334} 336}
335 337
336static void 338static void
337queue_events (EV_P_ W *events, int eventcnt, int type) 339queue_events (EV_P_ W *events, int eventcnt, int type)
338{ 340{
339 int i; 341 int i;
340 342
341 for (i = 0; i < eventcnt; ++i) 343 for (i = 0; i < eventcnt; ++i)
342 event (EV_A_ events [i], type); 344 ev_feed_event (EV_A_ events [i], type);
343} 345}
344 346
345static void 347inline void
346fd_event (EV_P_ int fd, int events) 348fd_event (EV_P_ int fd, int revents)
347{ 349{
348 ANFD *anfd = anfds + fd; 350 ANFD *anfd = anfds + fd;
349 struct ev_io *w; 351 struct ev_io *w;
350 352
351 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 353 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
352 { 354 {
353 int ev = w->events & events; 355 int ev = w->events & revents;
354 356
355 if (ev) 357 if (ev)
356 event (EV_A_ (W)w, ev); 358 ev_feed_event (EV_A_ (W)w, ev);
357 } 359 }
360}
361
362void
363ev_feed_fd_event (EV_P_ int fd, int revents)
364{
365 fd_event (EV_A_ fd, revents);
358} 366}
359 367
360/*****************************************************************************/ 368/*****************************************************************************/
361 369
362static void 370static void
403 struct ev_io *w; 411 struct ev_io *w;
404 412
405 while ((w = (struct ev_io *)anfds [fd].head)) 413 while ((w = (struct ev_io *)anfds [fd].head))
406 { 414 {
407 ev_io_stop (EV_A_ w); 415 ev_io_stop (EV_A_ w);
408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 416 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
409 } 417 }
410} 418}
411 419
412static int 420static int
413fd_valid (int fd) 421fd_valid (int fd)
550#endif 558#endif
551 errno = old_errno; 559 errno = old_errno;
552 } 560 }
553} 561}
554 562
563void
564ev_feed_signal_event (EV_P_ int signum)
565{
566#if EV_MULTIPLICITY
567 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
568#endif
569
570 --signum;
571
572 if (signum < 0 || signum >= signalmax)
573 return;
574
575 signals [signum].gotsig = 0;
576
577 for (w = signals [signum].head; w; w = w->next)
578 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
579}
580
555static void 581static void
556sigcb (EV_P_ struct ev_io *iow, int revents) 582sigcb (EV_P_ struct ev_io *iow, int revents)
557{ 583{
558 WL w; 584 WL w;
559 int signum; 585 int signum;
565#endif 591#endif
566 gotsig = 0; 592 gotsig = 0;
567 593
568 for (signum = signalmax; signum--; ) 594 for (signum = signalmax; signum--; )
569 if (signals [signum].gotsig) 595 if (signals [signum].gotsig)
570 { 596 sigevent (EV_A_ signum + 1);
571 signals [signum].gotsig = 0;
572
573 for (w = signals [signum].head; w; w = w->next)
574 event (EV_A_ (W)w, EV_SIGNAL);
575 }
576} 597}
577 598
578static void 599static void
579siginit (EV_P) 600siginit (EV_P)
580{ 601{
613 if (w->pid == pid || !w->pid) 634 if (w->pid == pid || !w->pid)
614 { 635 {
615 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 636 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
616 w->rpid = pid; 637 w->rpid = pid;
617 w->rstatus = status; 638 w->rstatus = status;
618 event (EV_A_ (W)w, EV_CHILD); 639 ev_feed_event (EV_A_ (W)w, EV_CHILD);
619 } 640 }
620} 641}
621 642
622static void 643static void
623childcb (EV_P_ struct ev_signal *sw, int revents) 644childcb (EV_P_ struct ev_signal *sw, int revents)
625 int pid, status; 646 int pid, status;
626 647
627 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 648 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
628 { 649 {
629 /* make sure we are called again until all childs have been reaped */ 650 /* make sure we are called again until all childs have been reaped */
630 event (EV_A_ (W)sw, EV_SIGNAL); 651 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
631 652
632 child_reap (EV_A_ sw, pid, pid, status); 653 child_reap (EV_A_ sw, pid, pid, status);
633 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 654 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
634 } 655 }
635} 656}
896 if (method) 917 if (method)
897 postfork = 1; 918 postfork = 1;
898} 919}
899 920
900/*****************************************************************************/ 921/*****************************************************************************/
922
923static int
924any_pending (EV_P)
925{
926 int pri;
927
928 for (pri = NUMPRI; pri--; )
929 if (pendingcnt [pri])
930 return 1;
931
932 return 0;
933}
901 934
902static void 935static void
903call_pending (EV_P) 936call_pending (EV_P)
904{ 937{
905 int pri; 938 int pri;
934 downheap ((WT *)timers, timercnt, 0); 967 downheap ((WT *)timers, timercnt, 0);
935 } 968 }
936 else 969 else
937 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 970 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
938 971
939 event (EV_A_ (W)w, EV_TIMEOUT); 972 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
940 } 973 }
941} 974}
942 975
943static void 976static void
944periodics_reify (EV_P) 977periodics_reify (EV_P)
948 struct ev_periodic *w = periodics [0]; 981 struct ev_periodic *w = periodics [0];
949 982
950 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 983 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
951 984
952 /* first reschedule or stop timer */ 985 /* first reschedule or stop timer */
986 if (w->reschedule_cb)
987 {
988 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
989
990 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
991 downheap ((WT *)periodics, periodiccnt, 0);
992 }
953 if (w->interval) 993 else if (w->interval)
954 { 994 {
955 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 995 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
956 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 996 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
957 downheap ((WT *)periodics, periodiccnt, 0); 997 downheap ((WT *)periodics, periodiccnt, 0);
958 } 998 }
959 else 999 else
960 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1000 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
961 1001
962 event (EV_A_ (W)w, EV_PERIODIC); 1002 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
963 } 1003 }
964} 1004}
965 1005
966static void 1006static void
967periodics_reschedule (EV_P) 1007periodics_reschedule (EV_P)
971 /* adjust periodics after time jump */ 1011 /* adjust periodics after time jump */
972 for (i = 0; i < periodiccnt; ++i) 1012 for (i = 0; i < periodiccnt; ++i)
973 { 1013 {
974 struct ev_periodic *w = periodics [i]; 1014 struct ev_periodic *w = periodics [i];
975 1015
1016 if (w->reschedule_cb)
1017 ((WT)w)->at = w->reschedule_cb (w, rt_now);
976 if (w->interval) 1018 else if (w->interval)
977 {
978 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1019 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
979
980 if (fabs (diff) >= 1e-4)
981 {
982 ev_periodic_stop (EV_A_ w);
983 ev_periodic_start (EV_A_ w);
984
985 i = 0; /* restart loop, inefficient, but time jumps should be rare */
986 }
987 }
988 } 1020 }
1021
1022 /* now rebuild the heap */
1023 for (i = periodiccnt >> 1; i--; )
1024 downheap ((WT *)periodics, periodiccnt, i);
989} 1025}
990 1026
991inline int 1027inline int
992time_update_monotonic (EV_P) 1028time_update_monotonic (EV_P)
993{ 1029{
1089 /* update fd-related kernel structures */ 1125 /* update fd-related kernel structures */
1090 fd_reify (EV_A); 1126 fd_reify (EV_A);
1091 1127
1092 /* calculate blocking time */ 1128 /* calculate blocking time */
1093 1129
1094 /* we only need this for !monotonic clockor timers, but as we basically 1130 /* we only need this for !monotonic clock or timers, but as we basically
1095 always have timers, we just calculate it always */ 1131 always have timers, we just calculate it always */
1096#if EV_USE_MONOTONIC 1132#if EV_USE_MONOTONIC
1097 if (expect_true (have_monotonic)) 1133 if (expect_true (have_monotonic))
1098 time_update_monotonic (EV_A); 1134 time_update_monotonic (EV_A);
1099 else 1135 else
1132 /* queue pending timers and reschedule them */ 1168 /* queue pending timers and reschedule them */
1133 timers_reify (EV_A); /* relative timers called last */ 1169 timers_reify (EV_A); /* relative timers called last */
1134 periodics_reify (EV_A); /* absolute timers called first */ 1170 periodics_reify (EV_A); /* absolute timers called first */
1135 1171
1136 /* queue idle watchers unless io or timers are pending */ 1172 /* queue idle watchers unless io or timers are pending */
1137 if (!pendingcnt) 1173 if (idlecnt && !any_pending (EV_A))
1138 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1174 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1139 1175
1140 /* queue check watchers, to be executed first */ 1176 /* queue check watchers, to be executed first */
1141 if (checkcnt) 1177 if (checkcnt)
1142 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1178 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1297ev_periodic_start (EV_P_ struct ev_periodic *w) 1333ev_periodic_start (EV_P_ struct ev_periodic *w)
1298{ 1334{
1299 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1300 return; 1336 return;
1301 1337
1338 if (w->reschedule_cb)
1339 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1340 else if (w->interval)
1341 {
1302 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1342 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1303
1304 /* this formula differs from the one in periodic_reify because we do not always round up */ 1343 /* this formula differs from the one in periodic_reify because we do not always round up */
1305 if (w->interval)
1306 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1344 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1345 }
1307 1346
1308 ev_start (EV_A_ (W)w, ++periodiccnt); 1347 ev_start (EV_A_ (W)w, ++periodiccnt);
1309 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1348 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1310 periodics [periodiccnt - 1] = w; 1349 periodics [periodiccnt - 1] = w;
1311 upheap ((WT *)periodics, periodiccnt - 1); 1350 upheap ((WT *)periodics, periodiccnt - 1);
1327 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1366 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1328 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1367 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1329 } 1368 }
1330 1369
1331 ev_stop (EV_A_ (W)w); 1370 ev_stop (EV_A_ (W)w);
1371}
1372
1373void
1374ev_periodic_again (EV_P_ struct ev_periodic *w)
1375{
1376 ev_periodic_stop (EV_A_ w);
1377 ev_periodic_start (EV_A_ w);
1332} 1378}
1333 1379
1334void 1380void
1335ev_idle_start (EV_P_ struct ev_idle *w) 1381ev_idle_start (EV_P_ struct ev_idle *w)
1336{ 1382{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines