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

Comparing libev/ev.c (file contents):
Revision 1.41 by root, Fri Nov 2 16:54:34 2007 UTC vs.
Revision 1.45 by root, Sat Nov 3 09:19:58 2007 UTC

42#include <stdio.h> 42#include <stdio.h>
43 43
44#include <assert.h> 44#include <assert.h>
45#include <errno.h> 45#include <errno.h>
46#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
47#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
48#include <sys/time.h> 50#include <sys/time.h>
49#include <time.h> 51#include <time.h>
50 52
51/**/ 53/**/
52 54
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
63#endif 65#endif
64 66
65#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
66# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
69#endif
70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
67#endif 73#endif
68 74
69#ifndef EV_USE_REALTIME 75#ifndef EV_USE_REALTIME
70# define EV_USE_REALTIME 1 76# define EV_USE_REALTIME 1
71#endif 77#endif
99# define inline static 105# define inline static
100#endif 106#endif
101 107
102#define expect_false(expr) expect ((expr) != 0, 0) 108#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1) 109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
104 113
105typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
106typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
107typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
108 117
193{ 202{
194 W w; 203 W w;
195 int events; 204 int events;
196} ANPENDING; 205} ANPENDING;
197 206
198static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
199static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
200 209
201static void 210static void
202event (W w, int events) 211event (W w, int events)
203{ 212{
204 if (w->pending) 213 if (w->pending)
205 { 214 {
206 pendings [w->pending - 1].events |= events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
207 return; 216 return;
208 } 217 }
209 218
210 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
211 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
212 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
213 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
214} 223}
215 224
216static void 225static void
217queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
218{ 227{
431} 440}
432 441
433static void 442static void
434siginit (void) 443siginit (void)
435{ 444{
445#ifndef WIN32
436 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
437 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
438 448
439 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
440 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
441 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
442 453
443 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
444 ev_io_start (&sigev); 455 ev_io_start (&sigev);
445} 456}
446 457
457 468
458/*****************************************************************************/ 469/*****************************************************************************/
459 470
460static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
461static struct ev_signal childev; 472static struct ev_signal childev;
473
474#ifndef WIN32
462 475
463#ifndef WCONTINUED 476#ifndef WCONTINUED
464# define WCONTINUED 0 477# define WCONTINUED 0
465#endif 478#endif
466 479
477 w->status = status; 490 w->status = status;
478 event ((W)w, EV_CHILD); 491 event ((W)w, EV_CHILD);
479 } 492 }
480} 493}
481 494
482/*****************************************************************************/ 495#endif
483 496
497/*****************************************************************************/
498
499#if EV_USE_KQUEUE
500# include "ev_kqueue.c"
501#endif
484#if EV_USE_EPOLL 502#if EV_USE_EPOLL
485# include "ev_epoll.c" 503# include "ev_epoll.c"
486#endif 504#endif
487#if EV_USE_POLL 505#if EV_USE_POLL
488# include "ev_poll.c" 506# include "ev_poll.c"
536 methods = atoi (getenv ("LIBEV_METHODS")); 554 methods = atoi (getenv ("LIBEV_METHODS"));
537 else 555 else
538 methods = EVMETHOD_ANY; 556 methods = EVMETHOD_ANY;
539 557
540 ev_method = 0; 558 ev_method = 0;
559#if EV_USE_KQUEUE
560 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
561#endif
541#if EV_USE_EPOLL 562#if EV_USE_EPOLL
542 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 563 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
543#endif 564#endif
544#if EV_USE_POLL 565#if EV_USE_POLL
545 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 566 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
551 if (ev_method) 572 if (ev_method)
552 { 573 {
553 ev_watcher_init (&sigev, sigcb); 574 ev_watcher_init (&sigev, sigcb);
554 siginit (); 575 siginit ();
555 576
577#ifndef WIN32
556 ev_signal_init (&childev, childcb, SIGCHLD); 578 ev_signal_init (&childev, childcb, SIGCHLD);
557 ev_signal_start (&childev); 579 ev_signal_start (&childev);
580#endif
558 } 581 }
559 } 582 }
560 583
561 return ev_method; 584 return ev_method;
562} 585}
593/*****************************************************************************/ 616/*****************************************************************************/
594 617
595static void 618static void
596call_pending (void) 619call_pending (void)
597{ 620{
621 int pri;
622
623 for (pri = NUMPRI; pri--; )
598 while (pendingcnt) 624 while (pendingcnt [pri])
599 { 625 {
600 ANPENDING *p = pendings + --pendingcnt; 626 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
601 627
602 if (p->w) 628 if (p->w)
603 { 629 {
604 p->w->pending = 0; 630 p->w->pending = 0;
605 p->w->cb (p->w, p->events); 631 p->w->cb (p->w, p->events);
606 } 632 }
607 } 633 }
608} 634}
609 635
610static void 636static void
611timers_reify (void) 637timers_reify (void)
612{ 638{
844static void 870static void
845ev_clear_pending (W w) 871ev_clear_pending (W w)
846{ 872{
847 if (w->pending) 873 if (w->pending)
848 { 874 {
849 pendings [w->pending - 1].w = 0; 875 pendings [ABSPRI (w)][w->pending - 1].w = 0;
850 w->pending = 0; 876 w->pending = 0;
851 } 877 }
852} 878}
853 879
854static void 880static void
855ev_start (W w, int active) 881ev_start (W w, int active)
856{ 882{
883 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
884 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
885
857 w->active = active; 886 w->active = active;
858} 887}
859 888
860static void 889static void
861ev_stop (W w) 890ev_stop (W w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines