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

Comparing libev/ev.c (file contents):
Revision 1.76 by root, Wed Nov 7 18:47:26 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])
946 downheap ((WT *)timers, timercnt, 0); 968 downheap ((WT *)timers, timercnt, 0);
947 } 969 }
948 else 970 else
949 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
950 972
951 event (EV_A_ (W)w, EV_TIMEOUT); 973 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
952 } 974 }
953} 975}
954 976
955static void 977static void
956periodics_reify (EV_P) 978periodics_reify (EV_P)
960 struct ev_periodic *w = periodics [0]; 982 struct ev_periodic *w = periodics [0];
961 983
962 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 984 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
963 985
964 /* 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 }
965 if (w->interval) 994 else if (w->interval)
966 { 995 {
967 ((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;
968 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));
969 downheap ((WT *)periodics, periodiccnt, 0); 998 downheap ((WT *)periodics, periodiccnt, 0);
970 } 999 }
971 else 1000 else
972 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
973 1002
974 event (EV_A_ (W)w, EV_PERIODIC); 1003 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
975 } 1004 }
976} 1005}
977 1006
978static void 1007static void
979periodics_reschedule (EV_P) 1008periodics_reschedule (EV_P)
983 /* adjust periodics after time jump */ 1012 /* adjust periodics after time jump */
984 for (i = 0; i < periodiccnt; ++i) 1013 for (i = 0; i < periodiccnt; ++i)
985 { 1014 {
986 struct ev_periodic *w = periodics [i]; 1015 struct ev_periodic *w = periodics [i];
987 1016
1017 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now);
988 if (w->interval) 1019 else if (w->interval)
989 {
990 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;
991
992 if (fabs (diff) >= 1e-4)
993 {
994 ev_periodic_stop (EV_A_ w);
995 ev_periodic_start (EV_A_ w);
996
997 i = 0; /* restart loop, inefficient, but time jumps should be rare */
998 }
999 }
1000 } 1021 }
1022
1023 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i);
1001} 1026}
1002 1027
1003inline int 1028inline int
1004time_update_monotonic (EV_P) 1029time_update_monotonic (EV_P)
1005{ 1030{
1309ev_periodic_start (EV_P_ struct ev_periodic *w) 1334ev_periodic_start (EV_P_ struct ev_periodic *w)
1310{ 1335{
1311 if (ev_is_active (w)) 1336 if (ev_is_active (w))
1312 return; 1337 return;
1313 1338
1339 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1341 else if (w->interval)
1342 {
1314 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.));
1315
1316 /* 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 */
1317 if (w->interval)
1318 ((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 }
1319 1347
1320 ev_start (EV_A_ (W)w, ++periodiccnt); 1348 ev_start (EV_A_ (W)w, ++periodiccnt);
1321 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1322 periodics [periodiccnt - 1] = w; 1350 periodics [periodiccnt - 1] = w;
1323 upheap ((WT *)periodics, periodiccnt - 1); 1351 upheap ((WT *)periodics, periodiccnt - 1);
1339 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1367 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1340 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1368 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1341 } 1369 }
1342 1370
1343 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);
1344} 1379}
1345 1380
1346void 1381void
1347ev_idle_start (EV_P_ struct ev_idle *w) 1382ev_idle_start (EV_P_ struct ev_idle *w)
1348{ 1383{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines