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

Comparing libev/ev.c (file contents):
Revision 1.43 by root, Fri Nov 2 20:21:33 2007 UTC vs.
Revision 1.47 by root, Sat Nov 3 11:44:44 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
434} 440}
435 441
436static void 442static void
437siginit (void) 443siginit (void)
438{ 444{
445#ifndef WIN32
439 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
440 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
441 448
442 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
443 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
444 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
445 453
446 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
447 ev_io_start (&sigev); 455 ev_io_start (&sigev);
448} 456}
449 457
461/*****************************************************************************/ 469/*****************************************************************************/
462 470
463static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
464static struct ev_signal childev; 472static struct ev_signal childev;
465 473
474#ifndef WIN32
475
466#ifndef WCONTINUED 476#ifndef WCONTINUED
467# define WCONTINUED 0 477# define WCONTINUED 0
468#endif 478#endif
469 479
470static void 480static void
481child_reap (struct ev_signal *sw, int chain, int pid, int status)
482{
483 struct ev_child *w;
484
485 for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next)
486 if (w->pid == pid || !w->pid)
487 {
488 w->priority = sw->priority; /* need to do it *now* */
489 w->rpid = pid;
490 w->rstatus = status;
491 printf ("rpid %p %d %d\n", w, pid, w->pid);//D
492 event ((W)w, EV_CHILD);
493 }
494}
495
496static void
471childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
472{ 498{
473 struct ev_child *w;
474 int pid, status; 499 int pid, status;
475 500
501 printf ("chld %x\n", revents);//D
476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 502 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 503 {
478 if (w->pid == pid || !w->pid) 504 /* make sure we are called again until all childs have been reaped */
479 {
480 w->status = status;
481 event ((W)w, EV_CHILD); 505 event ((W)sw, EV_SIGNAL);
482 } 506
507 child_reap (sw, pid, pid, status);
508 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
509 }
483} 510}
511
512#endif
484 513
485/*****************************************************************************/ 514/*****************************************************************************/
486 515
516#if EV_USE_KQUEUE
517# include "ev_kqueue.c"
518#endif
487#if EV_USE_EPOLL 519#if EV_USE_EPOLL
488# include "ev_epoll.c" 520# include "ev_epoll.c"
489#endif 521#endif
490#if EV_USE_POLL 522#if EV_USE_POLL
491# include "ev_poll.c" 523# include "ev_poll.c"
539 methods = atoi (getenv ("LIBEV_METHODS")); 571 methods = atoi (getenv ("LIBEV_METHODS"));
540 else 572 else
541 methods = EVMETHOD_ANY; 573 methods = EVMETHOD_ANY;
542 574
543 ev_method = 0; 575 ev_method = 0;
576#if EV_USE_KQUEUE
577 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
578#endif
544#if EV_USE_EPOLL 579#if EV_USE_EPOLL
545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 580 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
546#endif 581#endif
547#if EV_USE_POLL 582#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 583 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
552#endif 587#endif
553 588
554 if (ev_method) 589 if (ev_method)
555 { 590 {
556 ev_watcher_init (&sigev, sigcb); 591 ev_watcher_init (&sigev, sigcb);
592 ev_set_priority (&sigev, EV_MAXPRI);
557 siginit (); 593 siginit ();
558 594
595#ifndef WIN32
559 ev_signal_init (&childev, childcb, SIGCHLD); 596 ev_signal_init (&childev, childcb, SIGCHLD);
597 ev_set_priority (&childev, EV_MAXPRI);
560 ev_signal_start (&childev); 598 ev_signal_start (&childev);
599#endif
561 } 600 }
562 } 601 }
563 602
564 return ev_method; 603 return ev_method;
565} 604}
987 } 1026 }
988 1027
989 ev_stop ((W)w); 1028 ev_stop ((W)w);
990} 1029}
991 1030
1031#ifndef SA_RESTART
1032# define SA_RESTART 0
1033#endif
1034
992void 1035void
993ev_signal_start (struct ev_signal *w) 1036ev_signal_start (struct ev_signal *w)
994{ 1037{
995 if (ev_is_active (w)) 1038 if (ev_is_active (w))
996 return; 1039 return;
1004 if (!w->next) 1047 if (!w->next)
1005 { 1048 {
1006 struct sigaction sa; 1049 struct sigaction sa;
1007 sa.sa_handler = sighandler; 1050 sa.sa_handler = sighandler;
1008 sigfillset (&sa.sa_mask); 1051 sigfillset (&sa.sa_mask);
1009 sa.sa_flags = 0; 1052 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1010 sigaction (w->signum, &sa, 0); 1053 sigaction (w->signum, &sa, 0);
1011 } 1054 }
1012} 1055}
1013 1056
1014void 1057void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines