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.46 by root, Sat Nov 3 09:20:12 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
444 485
445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 486 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 487 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
447 if (w->pid == pid || !w->pid) 488 if (w->pid == pid || !w->pid)
448 { 489 {
490 w->rpid = pid;
449 w->status = status; 491 w->rstatus = status;
450 event ((W)w, EV_CHILD); 492 event ((W)w, EV_CHILD);
451 } 493 }
452} 494}
453 495
454/*****************************************************************************/ 496#endif
455 497
498/*****************************************************************************/
499
500#if EV_USE_KQUEUE
501# include "ev_kqueue.c"
502#endif
456#if EV_USE_EPOLL 503#if EV_USE_EPOLL
457# include "ev_epoll.c" 504# include "ev_epoll.c"
458#endif 505#endif
506#if EV_USE_POLL
507# include "ev_poll.c"
508#endif
459#if EV_USE_SELECT 509#if EV_USE_SELECT
460# include "ev_select.c" 510# include "ev_select.c"
461#endif 511#endif
462 512
463int 513int
470ev_version_minor (void) 520ev_version_minor (void)
471{ 521{
472 return EV_VERSION_MINOR; 522 return EV_VERSION_MINOR;
473} 523}
474 524
525/* return true if we are running with elevated privileges and ignore env variables */
526static int
527enable_secure ()
528{
529 return getuid () != geteuid ()
530 || getgid () != getegid ();
531}
532
475int ev_init (int flags) 533int ev_init (int methods)
476{ 534{
477 if (!ev_method) 535 if (!ev_method)
478 { 536 {
479#if EV_USE_MONOTONIC 537#if EV_USE_MONOTONIC
480 { 538 {
490 diff = ev_now - now; 548 diff = ev_now - now;
491 549
492 if (pipe (sigpipe)) 550 if (pipe (sigpipe))
493 return 0; 551 return 0;
494 552
553 if (methods == EVMETHOD_AUTO)
554 if (!enable_secure () && getenv ("LIBEV_METHODS"))
555 methods = atoi (getenv ("LIBEV_METHODS"));
556 else
495 ev_method = EVMETHOD_NONE; 557 methods = EVMETHOD_ANY;
558
559 ev_method = 0;
560#if EV_USE_KQUEUE
561 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
562#endif
496#if EV_USE_EPOLL 563#if EV_USE_EPOLL
497 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 564 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
565#endif
566#if EV_USE_POLL
567 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
498#endif 568#endif
499#if EV_USE_SELECT 569#if EV_USE_SELECT
500 if (ev_method == EVMETHOD_NONE) select_init (flags); 570 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
501#endif 571#endif
502 572
503 if (ev_method) 573 if (ev_method)
504 { 574 {
505 ev_watcher_init (&sigev, sigcb); 575 ev_watcher_init (&sigev, sigcb);
506 siginit (); 576 siginit ();
507 577
578#ifndef WIN32
508 ev_signal_init (&childev, childcb, SIGCHLD); 579 ev_signal_init (&childev, childcb, SIGCHLD);
509 ev_signal_start (&childev); 580 ev_signal_start (&childev);
581#endif
510 } 582 }
511 } 583 }
512 584
513 return ev_method; 585 return ev_method;
514} 586}
545/*****************************************************************************/ 617/*****************************************************************************/
546 618
547static void 619static void
548call_pending (void) 620call_pending (void)
549{ 621{
622 int pri;
623
624 for (pri = NUMPRI; pri--; )
550 while (pendingcnt) 625 while (pendingcnt [pri])
551 { 626 {
552 ANPENDING *p = pendings + --pendingcnt; 627 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
553 628
554 if (p->w) 629 if (p->w)
555 { 630 {
556 p->w->pending = 0; 631 p->w->pending = 0;
557 p->w->cb (p->w, p->events); 632 p->w->cb (p->w, p->events);
558 } 633 }
559 } 634 }
560} 635}
561 636
562static void 637static void
563timers_reify (void) 638timers_reify (void)
564{ 639{
796static void 871static void
797ev_clear_pending (W w) 872ev_clear_pending (W w)
798{ 873{
799 if (w->pending) 874 if (w->pending)
800 { 875 {
801 pendings [w->pending - 1].w = 0; 876 pendings [ABSPRI (w)][w->pending - 1].w = 0;
802 w->pending = 0; 877 w->pending = 0;
803 } 878 }
804} 879}
805 880
806static void 881static void
807ev_start (W w, int active) 882ev_start (W w, int active)
808{ 883{
884 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
885 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
886
809 w->active = active; 887 w->active = active;
810} 888}
811 889
812static void 890static void
813ev_stop (W w) 891ev_stop (W w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines