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.49 by root, Sat Nov 3 16:16: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;
376{ 413{
377 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
378 415
379 if (!gotsig) 416 if (!gotsig)
380 { 417 {
418 int old_errno = errno;
381 gotsig = 1; 419 gotsig = 1;
382 write (sigpipe [1], &signum, 1); 420 write (sigpipe [1], &signum, 1);
421 errno = old_errno;
383 } 422 }
384} 423}
385 424
386static void 425static void
387sigcb (struct ev_io *iow, int revents) 426sigcb (struct ev_io *iow, int revents)
403} 442}
404 443
405static void 444static void
406siginit (void) 445siginit (void)
407{ 446{
447#ifndef WIN32
408 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
409 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 449 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
410 450
411 /* rather than sort out wether we really need nb, set it */ 451 /* rather than sort out wether we really need nb, set it */
412 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
413 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 453 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
454#endif
414 455
415 ev_io_set (&sigev, sigpipe [0], EV_READ); 456 ev_io_set (&sigev, sigpipe [0], EV_READ);
416 ev_io_start (&sigev); 457 ev_io_start (&sigev);
417} 458}
418 459
430/*****************************************************************************/ 471/*****************************************************************************/
431 472
432static struct ev_child *childs [PID_HASHSIZE]; 473static struct ev_child *childs [PID_HASHSIZE];
433static struct ev_signal childev; 474static struct ev_signal childev;
434 475
476#ifndef WIN32
477
435#ifndef WCONTINUED 478#ifndef WCONTINUED
436# define WCONTINUED 0 479# define WCONTINUED 0
437#endif 480#endif
438 481
439static void 482static void
483child_reap (struct ev_signal *sw, int chain, int pid, int status)
484{
485 struct ev_child *w;
486
487 for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next)
488 if (w->pid == pid || !w->pid)
489 {
490 w->priority = sw->priority; /* need to do it *now* */
491 w->rpid = pid;
492 w->rstatus = status;
493 printf ("rpid %p %d %d\n", w, pid, w->pid);//D
494 event ((W)w, EV_CHILD);
495 }
496}
497
498static void
440childcb (struct ev_signal *sw, int revents) 499childcb (struct ev_signal *sw, int revents)
441{ 500{
442 struct ev_child *w;
443 int pid, status; 501 int pid, status;
444 502
503 printf ("chld %x\n", revents);//D
445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 504 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 505 {
447 if (w->pid == pid || !w->pid) 506 /* make sure we are called again until all childs have been reaped */
448 {
449 w->status = status;
450 event ((W)w, EV_CHILD); 507 event ((W)sw, EV_SIGNAL);
451 } 508
509 child_reap (sw, pid, pid, status);
510 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
511 }
452} 512}
513
514#endif
453 515
454/*****************************************************************************/ 516/*****************************************************************************/
455 517
518#if EV_USE_KQUEUE
519# include "ev_kqueue.c"
520#endif
456#if EV_USE_EPOLL 521#if EV_USE_EPOLL
457# include "ev_epoll.c" 522# include "ev_epoll.c"
458#endif 523#endif
524#if EV_USE_POLL
525# include "ev_poll.c"
526#endif
459#if EV_USE_SELECT 527#if EV_USE_SELECT
460# include "ev_select.c" 528# include "ev_select.c"
461#endif 529#endif
462 530
463int 531int
470ev_version_minor (void) 538ev_version_minor (void)
471{ 539{
472 return EV_VERSION_MINOR; 540 return EV_VERSION_MINOR;
473} 541}
474 542
543/* return true if we are running with elevated privileges and should ignore env variables */
544static int
545enable_secure ()
546{
547#ifdef WIN32
548 return 0;
549#else
550 return getuid () != geteuid ()
551 || getgid () != getegid ();
552#endif
553}
554
475int ev_init (int flags) 555int ev_init (int methods)
476{ 556{
477 if (!ev_method) 557 if (!ev_method)
478 { 558 {
479#if EV_USE_MONOTONIC 559#if EV_USE_MONOTONIC
480 { 560 {
490 diff = ev_now - now; 570 diff = ev_now - now;
491 571
492 if (pipe (sigpipe)) 572 if (pipe (sigpipe))
493 return 0; 573 return 0;
494 574
575 if (methods == EVMETHOD_AUTO)
576 if (!enable_secure () && getenv ("LIBEV_METHODS"))
577 methods = atoi (getenv ("LIBEV_METHODS"));
578 else
495 ev_method = EVMETHOD_NONE; 579 methods = EVMETHOD_ANY;
580
581 ev_method = 0;
582#if EV_USE_KQUEUE
583 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
584#endif
496#if EV_USE_EPOLL 585#if EV_USE_EPOLL
497 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 586 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
587#endif
588#if EV_USE_POLL
589 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
498#endif 590#endif
499#if EV_USE_SELECT 591#if EV_USE_SELECT
500 if (ev_method == EVMETHOD_NONE) select_init (flags); 592 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
501#endif 593#endif
502 594
503 if (ev_method) 595 if (ev_method)
504 { 596 {
505 ev_watcher_init (&sigev, sigcb); 597 ev_watcher_init (&sigev, sigcb);
598 ev_set_priority (&sigev, EV_MAXPRI);
506 siginit (); 599 siginit ();
507 600
601#ifndef WIN32
508 ev_signal_init (&childev, childcb, SIGCHLD); 602 ev_signal_init (&childev, childcb, SIGCHLD);
603 ev_set_priority (&childev, EV_MAXPRI);
509 ev_signal_start (&childev); 604 ev_signal_start (&childev);
605#endif
510 } 606 }
511 } 607 }
512 608
513 return ev_method; 609 return ev_method;
514} 610}
545/*****************************************************************************/ 641/*****************************************************************************/
546 642
547static void 643static void
548call_pending (void) 644call_pending (void)
549{ 645{
646 int pri;
647
648 for (pri = NUMPRI; pri--; )
550 while (pendingcnt) 649 while (pendingcnt [pri])
551 { 650 {
552 ANPENDING *p = pendings + --pendingcnt; 651 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
553 652
554 if (p->w) 653 if (p->w)
555 { 654 {
556 p->w->pending = 0; 655 p->w->pending = 0;
557 p->w->cb (p->w, p->events); 656 p->w->cb (p->w, p->events);
558 } 657 }
559 } 658 }
560} 659}
561 660
562static void 661static void
563timers_reify (void) 662timers_reify (void)
564{ 663{
796static void 895static void
797ev_clear_pending (W w) 896ev_clear_pending (W w)
798{ 897{
799 if (w->pending) 898 if (w->pending)
800 { 899 {
801 pendings [w->pending - 1].w = 0; 900 pendings [ABSPRI (w)][w->pending - 1].w = 0;
802 w->pending = 0; 901 w->pending = 0;
803 } 902 }
804} 903}
805 904
806static void 905static void
807ev_start (W w, int active) 906ev_start (W w, int active)
808{ 907{
908 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
909 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
910
809 w->active = active; 911 w->active = active;
810} 912}
811 913
812static void 914static void
813ev_stop (W w) 915ev_stop (W w)
930 } 1032 }
931 1033
932 ev_stop ((W)w); 1034 ev_stop ((W)w);
933} 1035}
934 1036
1037#ifndef SA_RESTART
1038# define SA_RESTART 0
1039#endif
1040
935void 1041void
936ev_signal_start (struct ev_signal *w) 1042ev_signal_start (struct ev_signal *w)
937{ 1043{
938 if (ev_is_active (w)) 1044 if (ev_is_active (w))
939 return; 1045 return;
947 if (!w->next) 1053 if (!w->next)
948 { 1054 {
949 struct sigaction sa; 1055 struct sigaction sa;
950 sa.sa_handler = sighandler; 1056 sa.sa_handler = sighandler;
951 sigfillset (&sa.sa_mask); 1057 sigfillset (&sa.sa_mask);
952 sa.sa_flags = 0; 1058 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
953 sigaction (w->signum, &sa, 0); 1059 sigaction (w->signum, &sa, 0);
954 } 1060 }
955} 1061}
956 1062
957void 1063void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines