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

Comparing libev/ev.c (file contents):
Revision 1.40 by root, Fri Nov 2 11:02:23 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
56 58
57#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
59#endif 61#endif
60 62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
61#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
62# 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
63#endif 73#endif
64 74
65#ifndef EV_USE_REALTIME 75#ifndef EV_USE_REALTIME
66# define EV_USE_REALTIME 1 76# define EV_USE_REALTIME 1
67#endif 77#endif
95# define inline static 105# define inline static
96#endif 106#endif
97 107
98#define expect_false(expr) expect ((expr) != 0, 0) 108#define expect_false(expr) expect ((expr) != 0, 0)
99#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)
100 113
101typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
102typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
103typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
104 117
189{ 202{
190 W w; 203 W w;
191 int events; 204 int events;
192} ANPENDING; 205} ANPENDING;
193 206
194static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
195static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
196 209
197static void 210static void
198event (W w, int events) 211event (W w, int events)
199{ 212{
200 if (w->pending) 213 if (w->pending)
201 { 214 {
202 pendings [w->pending - 1].events |= events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
203 return; 216 return;
204 } 217 }
205 218
206 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
207 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
208 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
209 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
210} 223}
211 224
212static void 225static void
213queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
214{ 227{
277 ++fdchangecnt; 290 ++fdchangecnt;
278 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
279 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
280} 293}
281 294
295static void
296fd_kill (int fd)
297{
298 struct ev_io *w;
299
300 printf ("killing fd %d\n", fd);//D
301 while ((w = anfds [fd].head))
302 {
303 ev_io_stop (w);
304 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
305 }
306}
307
282/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
283static void 309static void
284fd_recheck (void) 310fd_ebadf (void)
285{ 311{
286 int fd; 312 int fd;
287 313
288 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
289 if (anfds [fd].events) 315 if (anfds [fd].events)
290 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
291 while (anfds [fd].head) 317 fd_kill (fd);
318}
319
320/* called on ENOMEM in select/poll to kill some fds and retry */
321static void
322fd_enomem (void)
323{
324 int fd = anfdmax;
325
326 while (fd--)
327 if (anfds [fd].events)
292 { 328 {
293 ev_io_stop (anfds [fd].head); 329 close (fd);
294 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 330 fd_kill (fd);
331 return;
295 } 332 }
296} 333}
297 334
298/*****************************************************************************/ 335/*****************************************************************************/
299 336
300static struct ev_timer **timers; 337static struct ev_timer **timers;
403} 440}
404 441
405static void 442static void
406siginit (void) 443siginit (void)
407{ 444{
445#ifndef WIN32
408 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
409 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
410 448
411 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
412 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
413 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
414 453
415 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
416 ev_io_start (&sigev); 455 ev_io_start (&sigev);
417} 456}
418 457
429 468
430/*****************************************************************************/ 469/*****************************************************************************/
431 470
432static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
433static struct ev_signal childev; 472static struct ev_signal childev;
473
474#ifndef WIN32
434 475
435#ifndef WCONTINUED 476#ifndef WCONTINUED
436# define WCONTINUED 0 477# define WCONTINUED 0
437#endif 478#endif
438 479
449 w->status = status; 490 w->status = status;
450 event ((W)w, EV_CHILD); 491 event ((W)w, EV_CHILD);
451 } 492 }
452} 493}
453 494
454/*****************************************************************************/ 495#endif
455 496
497/*****************************************************************************/
498
499#if EV_USE_KQUEUE
500# include "ev_kqueue.c"
501#endif
456#if EV_USE_EPOLL 502#if EV_USE_EPOLL
457# include "ev_epoll.c" 503# include "ev_epoll.c"
458#endif 504#endif
505#if EV_USE_POLL
506# include "ev_poll.c"
507#endif
459#if EV_USE_SELECT 508#if EV_USE_SELECT
460# include "ev_select.c" 509# include "ev_select.c"
461#endif 510#endif
462 511
463int 512int
470ev_version_minor (void) 519ev_version_minor (void)
471{ 520{
472 return EV_VERSION_MINOR; 521 return EV_VERSION_MINOR;
473} 522}
474 523
524/* return true if we are running with elevated privileges and ignore env variables */
525static int
526enable_secure ()
527{
528 return getuid () != geteuid ()
529 || getgid () != getegid ();
530}
531
475int ev_init (int flags) 532int ev_init (int methods)
476{ 533{
477 if (!ev_method) 534 if (!ev_method)
478 { 535 {
479#if EV_USE_MONOTONIC 536#if EV_USE_MONOTONIC
480 { 537 {
490 diff = ev_now - now; 547 diff = ev_now - now;
491 548
492 if (pipe (sigpipe)) 549 if (pipe (sigpipe))
493 return 0; 550 return 0;
494 551
552 if (methods == EVMETHOD_AUTO)
553 if (!enable_secure () && getenv ("LIBEV_METHODS"))
554 methods = atoi (getenv ("LIBEV_METHODS"));
555 else
495 ev_method = EVMETHOD_NONE; 556 methods = EVMETHOD_ANY;
557
558 ev_method = 0;
559#if EV_USE_KQUEUE
560 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
561#endif
496#if EV_USE_EPOLL 562#if EV_USE_EPOLL
497 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 563 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
564#endif
565#if EV_USE_POLL
566 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
498#endif 567#endif
499#if EV_USE_SELECT 568#if EV_USE_SELECT
500 if (ev_method == EVMETHOD_NONE) select_init (flags); 569 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
501#endif 570#endif
502 571
503 if (ev_method) 572 if (ev_method)
504 { 573 {
505 ev_watcher_init (&sigev, sigcb); 574 ev_watcher_init (&sigev, sigcb);
506 siginit (); 575 siginit ();
507 576
577#ifndef WIN32
508 ev_signal_init (&childev, childcb, SIGCHLD); 578 ev_signal_init (&childev, childcb, SIGCHLD);
509 ev_signal_start (&childev); 579 ev_signal_start (&childev);
580#endif
510 } 581 }
511 } 582 }
512 583
513 return ev_method; 584 return ev_method;
514} 585}
545/*****************************************************************************/ 616/*****************************************************************************/
546 617
547static void 618static void
548call_pending (void) 619call_pending (void)
549{ 620{
621 int pri;
622
623 for (pri = NUMPRI; pri--; )
550 while (pendingcnt) 624 while (pendingcnt [pri])
551 { 625 {
552 ANPENDING *p = pendings + --pendingcnt; 626 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
553 627
554 if (p->w) 628 if (p->w)
555 { 629 {
556 p->w->pending = 0; 630 p->w->pending = 0;
557 p->w->cb (p->w, p->events); 631 p->w->cb (p->w, p->events);
558 } 632 }
559 } 633 }
560} 634}
561 635
562static void 636static void
563timers_reify (void) 637timers_reify (void)
564{ 638{
796static void 870static void
797ev_clear_pending (W w) 871ev_clear_pending (W w)
798{ 872{
799 if (w->pending) 873 if (w->pending)
800 { 874 {
801 pendings [w->pending - 1].w = 0; 875 pendings [ABSPRI (w)][w->pending - 1].w = 0;
802 w->pending = 0; 876 w->pending = 0;
803 } 877 }
804} 878}
805 879
806static void 880static void
807ev_start (W w, int active) 881ev_start (W w, int active)
808{ 882{
883 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
884 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
885
809 w->active = active; 886 w->active = active;
810} 887}
811 888
812static void 889static void
813ev_stop (W w) 890ev_stop (W w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines