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.80 by root, Fri Nov 9 15:30:59 2007 UTC

215 int events; 215 int events;
216} ANPENDING; 216} ANPENDING;
217 217
218#if EV_MULTIPLICITY 218#if EV_MULTIPLICITY
219 219
220struct ev_loop 220 struct ev_loop
221{ 221 {
222# define VAR(name,decl) decl; 222 #define VAR(name,decl) decl;
223# include "ev_vars.h" 223 #include "ev_vars.h"
224};
225# undef VAR 224 #undef VAR
225 };
226# include "ev_wrap.h" 226 #include "ev_wrap.h"
227
228 struct ev_loop default_loop_struct;
229 static struct ev_loop *default_loop;
227 230
228#else 231#else
229 232
230# define VAR(name,decl) static decl; 233 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 234 #include "ev_vars.h"
232# undef VAR 235 #undef VAR
236
237 static int default_loop;
233 238
234#endif 239#endif
235 240
236/*****************************************************************************/ 241/*****************************************************************************/
237 242
316 321
317 ++base; 322 ++base;
318 } 323 }
319} 324}
320 325
321static void 326void
322event (EV_P_ W w, int events) 327ev_feed_event (EV_P_ void *w, int revents)
323{ 328{
329 W w_ = (W)w;
330
324 if (w->pending) 331 if (w_->pending)
325 { 332 {
326 pendings [ABSPRI (w)][w->pending - 1].events |= events; 333 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
327 return; 334 return;
328 } 335 }
329 336
330 w->pending = ++pendingcnt [ABSPRI (w)]; 337 w_->pending = ++pendingcnt [ABSPRI (w_)];
331 array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 338 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w; 339 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
333 pendings [ABSPRI (w)][w->pending - 1].events = events; 340 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
334} 341}
335 342
336static void 343static void
337queue_events (EV_P_ W *events, int eventcnt, int type) 344queue_events (EV_P_ W *events, int eventcnt, int type)
338{ 345{
339 int i; 346 int i;
340 347
341 for (i = 0; i < eventcnt; ++i) 348 for (i = 0; i < eventcnt; ++i)
342 event (EV_A_ events [i], type); 349 ev_feed_event (EV_A_ events [i], type);
343} 350}
344 351
345static void 352inline void
346fd_event (EV_P_ int fd, int events) 353fd_event (EV_P_ int fd, int revents)
347{ 354{
348 ANFD *anfd = anfds + fd; 355 ANFD *anfd = anfds + fd;
349 struct ev_io *w; 356 struct ev_io *w;
350 357
351 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 358 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
352 { 359 {
353 int ev = w->events & events; 360 int ev = w->events & revents;
354 361
355 if (ev) 362 if (ev)
356 event (EV_A_ (W)w, ev); 363 ev_feed_event (EV_A_ (W)w, ev);
357 } 364 }
365}
366
367void
368ev_feed_fd_event (EV_P_ int fd, int revents)
369{
370 fd_event (EV_A_ fd, revents);
358} 371}
359 372
360/*****************************************************************************/ 373/*****************************************************************************/
361 374
362static void 375static void
403 struct ev_io *w; 416 struct ev_io *w;
404 417
405 while ((w = (struct ev_io *)anfds [fd].head)) 418 while ((w = (struct ev_io *)anfds [fd].head))
406 { 419 {
407 ev_io_stop (EV_A_ w); 420 ev_io_stop (EV_A_ w);
408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 421 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
409 } 422 }
410} 423}
411 424
412static int 425static int
413fd_valid (int fd) 426fd_valid (int fd)
550#endif 563#endif
551 errno = old_errno; 564 errno = old_errno;
552 } 565 }
553} 566}
554 567
568void
569ev_feed_signal_event (EV_P_ int signum)
570{
571 WL w;
572
573#if EV_MULTIPLICITY
574 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
575#endif
576
577 --signum;
578
579 if (signum < 0 || signum >= signalmax)
580 return;
581
582 signals [signum].gotsig = 0;
583
584 for (w = signals [signum].head; w; w = w->next)
585 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
586}
587
555static void 588static void
556sigcb (EV_P_ struct ev_io *iow, int revents) 589sigcb (EV_P_ struct ev_io *iow, int revents)
557{ 590{
558 WL w;
559 int signum; 591 int signum;
560 592
561#ifdef WIN32 593#ifdef WIN32
562 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); 594 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
563#else 595#else
565#endif 597#endif
566 gotsig = 0; 598 gotsig = 0;
567 599
568 for (signum = signalmax; signum--; ) 600 for (signum = signalmax; signum--; )
569 if (signals [signum].gotsig) 601 if (signals [signum].gotsig)
570 { 602 ev_feed_signal_event (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} 603}
577 604
578static void 605static void
579siginit (EV_P) 606siginit (EV_P)
580{ 607{
613 if (w->pid == pid || !w->pid) 640 if (w->pid == pid || !w->pid)
614 { 641 {
615 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 642 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
616 w->rpid = pid; 643 w->rpid = pid;
617 w->rstatus = status; 644 w->rstatus = status;
618 event (EV_A_ (W)w, EV_CHILD); 645 ev_feed_event (EV_A_ (W)w, EV_CHILD);
619 } 646 }
620} 647}
621 648
622static void 649static void
623childcb (EV_P_ struct ev_signal *sw, int revents) 650childcb (EV_P_ struct ev_signal *sw, int revents)
625 int pid, status; 652 int pid, status;
626 653
627 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 654 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
628 { 655 {
629 /* make sure we are called again until all childs have been reaped */ 656 /* make sure we are called again until all childs have been reaped */
630 event (EV_A_ (W)sw, EV_SIGNAL); 657 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
631 658
632 child_reap (EV_A_ sw, pid, pid, status); 659 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 */ 660 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
634 } 661 }
635} 662}
820} 847}
821 848
822#endif 849#endif
823 850
824#if EV_MULTIPLICITY 851#if EV_MULTIPLICITY
825struct ev_loop default_loop_struct;
826static struct ev_loop *default_loop;
827
828struct ev_loop * 852struct ev_loop *
829#else 853#else
830static int default_loop;
831
832int 854int
833#endif 855#endif
834ev_default_loop (int methods) 856ev_default_loop (int methods)
835{ 857{
836 if (sigpipe [0] == sigpipe [1]) 858 if (sigpipe [0] == sigpipe [1])
896 if (method) 918 if (method)
897 postfork = 1; 919 postfork = 1;
898} 920}
899 921
900/*****************************************************************************/ 922/*****************************************************************************/
923
924static int
925any_pending (EV_P)
926{
927 int pri;
928
929 for (pri = NUMPRI; pri--; )
930 if (pendingcnt [pri])
931 return 1;
932
933 return 0;
934}
901 935
902static void 936static void
903call_pending (EV_P) 937call_pending (EV_P)
904{ 938{
905 int pri; 939 int pri;
934 downheap ((WT *)timers, timercnt, 0); 968 downheap ((WT *)timers, timercnt, 0);
935 } 969 }
936 else 970 else
937 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
938 972
939 event (EV_A_ (W)w, EV_TIMEOUT); 973 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
940 } 974 }
941} 975}
942 976
943static void 977static void
944periodics_reify (EV_P) 978periodics_reify (EV_P)
948 struct ev_periodic *w = periodics [0]; 982 struct ev_periodic *w = periodics [0];
949 983
950 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 984 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
951 985
952 /* first reschedule or stop timer */ 986 /* first reschedule or stop timer */
987 if (w->reschedule_cb)
988 {
989 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
990
991 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
992 downheap ((WT *)periodics, periodiccnt, 0);
993 }
953 if (w->interval) 994 else if (w->interval)
954 { 995 {
955 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 996 ((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)); 997 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
957 downheap ((WT *)periodics, periodiccnt, 0); 998 downheap ((WT *)periodics, periodiccnt, 0);
958 } 999 }
959 else 1000 else
960 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
961 1002
962 event (EV_A_ (W)w, EV_PERIODIC); 1003 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
963 } 1004 }
964} 1005}
965 1006
966static void 1007static void
967periodics_reschedule (EV_P) 1008periodics_reschedule (EV_P)
971 /* adjust periodics after time jump */ 1012 /* adjust periodics after time jump */
972 for (i = 0; i < periodiccnt; ++i) 1013 for (i = 0; i < periodiccnt; ++i)
973 { 1014 {
974 struct ev_periodic *w = periodics [i]; 1015 struct ev_periodic *w = periodics [i];
975 1016
1017 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now);
976 if (w->interval) 1019 else if (w->interval)
977 {
978 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1020 ((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 } 1021 }
1022
1023 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i);
989} 1026}
990 1027
991inline int 1028inline int
992time_update_monotonic (EV_P) 1029time_update_monotonic (EV_P)
993{ 1030{
1089 /* update fd-related kernel structures */ 1126 /* update fd-related kernel structures */
1090 fd_reify (EV_A); 1127 fd_reify (EV_A);
1091 1128
1092 /* calculate blocking time */ 1129 /* calculate blocking time */
1093 1130
1094 /* we only need this for !monotonic clockor timers, but as we basically 1131 /* we only need this for !monotonic clock or timers, but as we basically
1095 always have timers, we just calculate it always */ 1132 always have timers, we just calculate it always */
1096#if EV_USE_MONOTONIC 1133#if EV_USE_MONOTONIC
1097 if (expect_true (have_monotonic)) 1134 if (expect_true (have_monotonic))
1098 time_update_monotonic (EV_A); 1135 time_update_monotonic (EV_A);
1099 else 1136 else
1132 /* queue pending timers and reschedule them */ 1169 /* queue pending timers and reschedule them */
1133 timers_reify (EV_A); /* relative timers called last */ 1170 timers_reify (EV_A); /* relative timers called last */
1134 periodics_reify (EV_A); /* absolute timers called first */ 1171 periodics_reify (EV_A); /* absolute timers called first */
1135 1172
1136 /* queue idle watchers unless io or timers are pending */ 1173 /* queue idle watchers unless io or timers are pending */
1137 if (!pendingcnt) 1174 if (idlecnt && !any_pending (EV_A))
1138 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1175 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1139 1176
1140 /* queue check watchers, to be executed first */ 1177 /* queue check watchers, to be executed first */
1141 if (checkcnt) 1178 if (checkcnt)
1142 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1179 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1297ev_periodic_start (EV_P_ struct ev_periodic *w) 1334ev_periodic_start (EV_P_ struct ev_periodic *w)
1298{ 1335{
1299 if (ev_is_active (w)) 1336 if (ev_is_active (w))
1300 return; 1337 return;
1301 1338
1339 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1341 else if (w->interval)
1342 {
1302 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1343 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 */ 1344 /* 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; 1345 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1346 }
1307 1347
1308 ev_start (EV_A_ (W)w, ++periodiccnt); 1348 ev_start (EV_A_ (W)w, ++periodiccnt);
1309 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1310 periodics [periodiccnt - 1] = w; 1350 periodics [periodiccnt - 1] = w;
1311 upheap ((WT *)periodics, periodiccnt - 1); 1351 upheap ((WT *)periodics, periodiccnt - 1);
1327 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1367 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1328 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1368 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1329 } 1369 }
1330 1370
1331 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1372}
1373
1374void
1375ev_periodic_again (EV_P_ struct ev_periodic *w)
1376{
1377 ev_periodic_stop (EV_A_ w);
1378 ev_periodic_start (EV_A_ w);
1332} 1379}
1333 1380
1334void 1381void
1335ev_idle_start (EV_P_ struct ev_idle *w) 1382ev_idle_start (EV_P_ struct ev_idle *w)
1336{ 1383{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines