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

Comparing libev/ev.c (file contents):
Revision 1.77 by root, Thu Nov 8 00:44:17 2007 UTC vs.
Revision 1.82 by root, Fri Nov 9 20:55:09 2007 UTC

126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 130
131#ifdef EV_H
132# include EV_H
133#else
131#include "ev.h" 134# include "ev.h"
135#endif
132 136
133#if __GNUC__ >= 3 137#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 138# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 139# define inline inline
136#else 140#else
215 int events; 219 int events;
216} ANPENDING; 220} ANPENDING;
217 221
218#if EV_MULTIPLICITY 222#if EV_MULTIPLICITY
219 223
220struct ev_loop 224 struct ev_loop
221{ 225 {
222# define VAR(name,decl) decl; 226 #define VAR(name,decl) decl;
223# include "ev_vars.h" 227 #include "ev_vars.h"
224};
225# undef VAR 228 #undef VAR
229 };
226# include "ev_wrap.h" 230 #include "ev_wrap.h"
231
232 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop;
227 234
228#else 235#else
229 236
230# define VAR(name,decl) static decl; 237 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 238 #include "ev_vars.h"
232# undef VAR 239 #undef VAR
240
241 static int default_loop;
233 242
234#endif 243#endif
235 244
236/*****************************************************************************/ 245/*****************************************************************************/
237 246
316 325
317 ++base; 326 ++base;
318 } 327 }
319} 328}
320 329
321static void 330void
322event (EV_P_ W w, int events) 331ev_feed_event (EV_P_ void *w, int revents)
323{ 332{
333 W w_ = (W)w;
334
324 if (w->pending) 335 if (w_->pending)
325 { 336 {
326 pendings [ABSPRI (w)][w->pending - 1].events |= events; 337 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
327 return; 338 return;
328 } 339 }
329 340
330 w->pending = ++pendingcnt [ABSPRI (w)]; 341 w_->pending = ++pendingcnt [ABSPRI (w_)];
331 array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 342 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w; 343 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
333 pendings [ABSPRI (w)][w->pending - 1].events = events; 344 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
334} 345}
335 346
336static void 347static void
337queue_events (EV_P_ W *events, int eventcnt, int type) 348queue_events (EV_P_ W *events, int eventcnt, int type)
338{ 349{
339 int i; 350 int i;
340 351
341 for (i = 0; i < eventcnt; ++i) 352 for (i = 0; i < eventcnt; ++i)
342 event (EV_A_ events [i], type); 353 ev_feed_event (EV_A_ events [i], type);
343} 354}
344 355
345static void 356inline void
346fd_event (EV_P_ int fd, int events) 357fd_event (EV_P_ int fd, int revents)
347{ 358{
348 ANFD *anfd = anfds + fd; 359 ANFD *anfd = anfds + fd;
349 struct ev_io *w; 360 struct ev_io *w;
350 361
351 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 362 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
352 { 363 {
353 int ev = w->events & events; 364 int ev = w->events & revents;
354 365
355 if (ev) 366 if (ev)
356 event (EV_A_ (W)w, ev); 367 ev_feed_event (EV_A_ (W)w, ev);
357 } 368 }
369}
370
371void
372ev_feed_fd_event (EV_P_ int fd, int revents)
373{
374 fd_event (EV_A_ fd, revents);
358} 375}
359 376
360/*****************************************************************************/ 377/*****************************************************************************/
361 378
362static void 379static void
403 struct ev_io *w; 420 struct ev_io *w;
404 421
405 while ((w = (struct ev_io *)anfds [fd].head)) 422 while ((w = (struct ev_io *)anfds [fd].head))
406 { 423 {
407 ev_io_stop (EV_A_ w); 424 ev_io_stop (EV_A_ w);
408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 425 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
409 } 426 }
410} 427}
411 428
412static int 429static int
413fd_valid (int fd) 430fd_valid (int fd)
550#endif 567#endif
551 errno = old_errno; 568 errno = old_errno;
552 } 569 }
553} 570}
554 571
572void
573ev_feed_signal_event (EV_P_ int signum)
574{
575 WL w;
576
577#if EV_MULTIPLICITY
578 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
579#endif
580
581 --signum;
582
583 if (signum < 0 || signum >= signalmax)
584 return;
585
586 signals [signum].gotsig = 0;
587
588 for (w = signals [signum].head; w; w = w->next)
589 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
590}
591
555static void 592static void
556sigcb (EV_P_ struct ev_io *iow, int revents) 593sigcb (EV_P_ struct ev_io *iow, int revents)
557{ 594{
558 WL w;
559 int signum; 595 int signum;
560 596
561#ifdef WIN32 597#ifdef WIN32
562 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); 598 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
563#else 599#else
565#endif 601#endif
566 gotsig = 0; 602 gotsig = 0;
567 603
568 for (signum = signalmax; signum--; ) 604 for (signum = signalmax; signum--; )
569 if (signals [signum].gotsig) 605 if (signals [signum].gotsig)
570 { 606 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} 607}
577 608
578static void 609static void
579siginit (EV_P) 610siginit (EV_P)
580{ 611{
613 if (w->pid == pid || !w->pid) 644 if (w->pid == pid || !w->pid)
614 { 645 {
615 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 646 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
616 w->rpid = pid; 647 w->rpid = pid;
617 w->rstatus = status; 648 w->rstatus = status;
618 event (EV_A_ (W)w, EV_CHILD); 649 ev_feed_event (EV_A_ (W)w, EV_CHILD);
619 } 650 }
620} 651}
621 652
622static void 653static void
623childcb (EV_P_ struct ev_signal *sw, int revents) 654childcb (EV_P_ struct ev_signal *sw, int revents)
625 int pid, status; 656 int pid, status;
626 657
627 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 658 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
628 { 659 {
629 /* make sure we are called again until all childs have been reaped */ 660 /* make sure we are called again until all childs have been reaped */
630 event (EV_A_ (W)sw, EV_SIGNAL); 661 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
631 662
632 child_reap (EV_A_ sw, pid, pid, status); 663 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 */ 664 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
634 } 665 }
635} 666}
820} 851}
821 852
822#endif 853#endif
823 854
824#if EV_MULTIPLICITY 855#if EV_MULTIPLICITY
825struct ev_loop default_loop_struct;
826static struct ev_loop *default_loop;
827
828struct ev_loop * 856struct ev_loop *
829#else 857#else
830static int default_loop;
831
832int 858int
833#endif 859#endif
834ev_default_loop (int methods) 860ev_default_loop (int methods)
835{ 861{
836 if (sigpipe [0] == sigpipe [1]) 862 if (sigpipe [0] == sigpipe [1])
922 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 948 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
923 949
924 if (p->w) 950 if (p->w)
925 { 951 {
926 p->w->pending = 0; 952 p->w->pending = 0;
927 p->w->cb (EV_A_ p->w, p->events); 953 EV_CB_INVOKE (p->w, p->events);
928 } 954 }
929 } 955 }
930} 956}
931 957
932static void 958static void
946 downheap ((WT *)timers, timercnt, 0); 972 downheap ((WT *)timers, timercnt, 0);
947 } 973 }
948 else 974 else
949 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 975 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
950 976
951 event (EV_A_ (W)w, EV_TIMEOUT); 977 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
952 } 978 }
953} 979}
954 980
955static void 981static void
956periodics_reify (EV_P) 982periodics_reify (EV_P)
976 downheap ((WT *)periodics, periodiccnt, 0); 1002 downheap ((WT *)periodics, periodiccnt, 0);
977 } 1003 }
978 else 1004 else
979 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1005 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
980 1006
981 event (EV_A_ (W)w, EV_PERIODIC); 1007 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
982 } 1008 }
983} 1009}
984 1010
985static void 1011static void
986periodics_reschedule (EV_P) 1012periodics_reschedule (EV_P)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines