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

Comparing libev/ev.c (file contents):
Revision 1.74 by root, Tue Nov 6 16:51: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)
541 554
542 if (!gotsig) 555 if (!gotsig)
543 { 556 {
544 int old_errno = errno; 557 int old_errno = errno;
545 gotsig = 1; 558 gotsig = 1;
559#ifdef WIN32
560 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
561#else
546 write (sigpipe [1], &signum, 1); 562 write (sigpipe [1], &signum, 1);
563#endif
547 errno = old_errno; 564 errno = old_errno;
548 } 565 }
549} 566}
550 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
551static void 588static void
552sigcb (EV_P_ struct ev_io *iow, int revents) 589sigcb (EV_P_ struct ev_io *iow, int revents)
553{ 590{
554 WL w;
555 int signum; 591 int signum;
556 592
593#ifdef WIN32
594 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
595#else
557 read (sigpipe [0], &revents, 1); 596 read (sigpipe [0], &revents, 1);
597#endif
558 gotsig = 0; 598 gotsig = 0;
559 599
560 for (signum = signalmax; signum--; ) 600 for (signum = signalmax; signum--; )
561 if (signals [signum].gotsig) 601 if (signals [signum].gotsig)
562 { 602 ev_feed_signal_event (EV_A_ signum + 1);
563 signals [signum].gotsig = 0;
564
565 for (w = signals [signum].head; w; w = w->next)
566 event (EV_A_ (W)w, EV_SIGNAL);
567 }
568} 603}
569 604
570static void 605static void
571siginit (EV_P) 606siginit (EV_P)
572{ 607{
605 if (w->pid == pid || !w->pid) 640 if (w->pid == pid || !w->pid)
606 { 641 {
607 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 642 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
608 w->rpid = pid; 643 w->rpid = pid;
609 w->rstatus = status; 644 w->rstatus = status;
610 event (EV_A_ (W)w, EV_CHILD); 645 ev_feed_event (EV_A_ (W)w, EV_CHILD);
611 } 646 }
612} 647}
613 648
614static void 649static void
615childcb (EV_P_ struct ev_signal *sw, int revents) 650childcb (EV_P_ struct ev_signal *sw, int revents)
617 int pid, status; 652 int pid, status;
618 653
619 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 654 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
620 { 655 {
621 /* 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 */
622 event (EV_A_ (W)sw, EV_SIGNAL); 657 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
623 658
624 child_reap (EV_A_ sw, pid, pid, status); 659 child_reap (EV_A_ sw, pid, pid, status);
625 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 */
626 } 661 }
627} 662}
812} 847}
813 848
814#endif 849#endif
815 850
816#if EV_MULTIPLICITY 851#if EV_MULTIPLICITY
817struct ev_loop default_loop_struct;
818static struct ev_loop *default_loop;
819
820struct ev_loop * 852struct ev_loop *
821#else 853#else
822static int default_loop;
823
824int 854int
825#endif 855#endif
826ev_default_loop (int methods) 856ev_default_loop (int methods)
827{ 857{
828 if (sigpipe [0] == sigpipe [1]) 858 if (sigpipe [0] == sigpipe [1])
888 if (method) 918 if (method)
889 postfork = 1; 919 postfork = 1;
890} 920}
891 921
892/*****************************************************************************/ 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}
893 935
894static void 936static void
895call_pending (EV_P) 937call_pending (EV_P)
896{ 938{
897 int pri; 939 int pri;
926 downheap ((WT *)timers, timercnt, 0); 968 downheap ((WT *)timers, timercnt, 0);
927 } 969 }
928 else 970 else
929 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
930 972
931 event (EV_A_ (W)w, EV_TIMEOUT); 973 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
932 } 974 }
933} 975}
934 976
935static void 977static void
936periodics_reify (EV_P) 978periodics_reify (EV_P)
940 struct ev_periodic *w = periodics [0]; 982 struct ev_periodic *w = periodics [0];
941 983
942 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 984 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
943 985
944 /* 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 }
945 if (w->interval) 994 else if (w->interval)
946 { 995 {
947 ((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;
948 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));
949 downheap ((WT *)periodics, periodiccnt, 0); 998 downheap ((WT *)periodics, periodiccnt, 0);
950 } 999 }
951 else 1000 else
952 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
953 1002
954 event (EV_A_ (W)w, EV_PERIODIC); 1003 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
955 } 1004 }
956} 1005}
957 1006
958static void 1007static void
959periodics_reschedule (EV_P) 1008periodics_reschedule (EV_P)
963 /* adjust periodics after time jump */ 1012 /* adjust periodics after time jump */
964 for (i = 0; i < periodiccnt; ++i) 1013 for (i = 0; i < periodiccnt; ++i)
965 { 1014 {
966 struct ev_periodic *w = periodics [i]; 1015 struct ev_periodic *w = periodics [i];
967 1016
1017 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now);
968 if (w->interval) 1019 else if (w->interval)
969 {
970 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;
971
972 if (fabs (diff) >= 1e-4)
973 {
974 ev_periodic_stop (EV_A_ w);
975 ev_periodic_start (EV_A_ w);
976
977 i = 0; /* restart loop, inefficient, but time jumps should be rare */
978 }
979 }
980 } 1021 }
1022
1023 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i);
981} 1026}
982 1027
983inline int 1028inline int
984time_update_monotonic (EV_P) 1029time_update_monotonic (EV_P)
985{ 1030{
1081 /* update fd-related kernel structures */ 1126 /* update fd-related kernel structures */
1082 fd_reify (EV_A); 1127 fd_reify (EV_A);
1083 1128
1084 /* calculate blocking time */ 1129 /* calculate blocking time */
1085 1130
1086 /* 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
1087 always have timers, we just calculate it always */ 1132 always have timers, we just calculate it always */
1088#if EV_USE_MONOTONIC 1133#if EV_USE_MONOTONIC
1089 if (expect_true (have_monotonic)) 1134 if (expect_true (have_monotonic))
1090 time_update_monotonic (EV_A); 1135 time_update_monotonic (EV_A);
1091 else 1136 else
1124 /* queue pending timers and reschedule them */ 1169 /* queue pending timers and reschedule them */
1125 timers_reify (EV_A); /* relative timers called last */ 1170 timers_reify (EV_A); /* relative timers called last */
1126 periodics_reify (EV_A); /* absolute timers called first */ 1171 periodics_reify (EV_A); /* absolute timers called first */
1127 1172
1128 /* queue idle watchers unless io or timers are pending */ 1173 /* queue idle watchers unless io or timers are pending */
1129 if (!pendingcnt) 1174 if (idlecnt && !any_pending (EV_A))
1130 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1175 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1131 1176
1132 /* queue check watchers, to be executed first */ 1177 /* queue check watchers, to be executed first */
1133 if (checkcnt) 1178 if (checkcnt)
1134 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1179 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1289ev_periodic_start (EV_P_ struct ev_periodic *w) 1334ev_periodic_start (EV_P_ struct ev_periodic *w)
1290{ 1335{
1291 if (ev_is_active (w)) 1336 if (ev_is_active (w))
1292 return; 1337 return;
1293 1338
1339 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1341 else if (w->interval)
1342 {
1294 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.));
1295
1296 /* 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 */
1297 if (w->interval)
1298 ((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 }
1299 1347
1300 ev_start (EV_A_ (W)w, ++periodiccnt); 1348 ev_start (EV_A_ (W)w, ++periodiccnt);
1301 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1302 periodics [periodiccnt - 1] = w; 1350 periodics [periodiccnt - 1] = w;
1303 upheap ((WT *)periodics, periodiccnt - 1); 1351 upheap ((WT *)periodics, periodiccnt - 1);
1319 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1367 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1320 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1368 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1321 } 1369 }
1322 1370
1323 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);
1324} 1379}
1325 1380
1326void 1381void
1327ev_idle_start (EV_P_ struct ev_idle *w) 1382ev_idle_start (EV_P_ struct ev_idle *w)
1328{ 1383{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines