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.48 by root, Sat Nov 3 12:19:31 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{
404{ 413{
405 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
406 415
407 if (!gotsig) 416 if (!gotsig)
408 { 417 {
418 int old_errno = errno;
409 gotsig = 1; 419 gotsig = 1;
410 write (sigpipe [1], &signum, 1); 420 write (sigpipe [1], &signum, 1);
421 errno = old_errno;
411 } 422 }
412} 423}
413 424
414static void 425static void
415sigcb (struct ev_io *iow, int revents) 426sigcb (struct ev_io *iow, int revents)
431} 442}
432 443
433static void 444static void
434siginit (void) 445siginit (void)
435{ 446{
447#ifndef WIN32
436 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
437 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 449 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
438 450
439 /* rather than sort out wether we really need nb, set it */ 451 /* rather than sort out wether we really need nb, set it */
440 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
441 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 453 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
454#endif
442 455
443 ev_io_set (&sigev, sigpipe [0], EV_READ); 456 ev_io_set (&sigev, sigpipe [0], EV_READ);
444 ev_io_start (&sigev); 457 ev_io_start (&sigev);
445} 458}
446 459
458/*****************************************************************************/ 471/*****************************************************************************/
459 472
460static struct ev_child *childs [PID_HASHSIZE]; 473static struct ev_child *childs [PID_HASHSIZE];
461static struct ev_signal childev; 474static struct ev_signal childev;
462 475
476#ifndef WIN32
477
463#ifndef WCONTINUED 478#ifndef WCONTINUED
464# define WCONTINUED 0 479# define WCONTINUED 0
465#endif 480#endif
466 481
467static 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
468childcb (struct ev_signal *sw, int revents) 499childcb (struct ev_signal *sw, int revents)
469{ 500{
470 struct ev_child *w;
471 int pid, status; 501 int pid, status;
472 502
503 printf ("chld %x\n", revents);//D
473 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 504 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
474 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 505 {
475 if (w->pid == pid || !w->pid) 506 /* make sure we are called again until all childs have been reaped */
476 {
477 w->status = status;
478 event ((W)w, EV_CHILD); 507 event ((W)sw, EV_SIGNAL);
479 } 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 }
480} 512}
513
514#endif
481 515
482/*****************************************************************************/ 516/*****************************************************************************/
483 517
518#if EV_USE_KQUEUE
519# include "ev_kqueue.c"
520#endif
484#if EV_USE_EPOLL 521#if EV_USE_EPOLL
485# include "ev_epoll.c" 522# include "ev_epoll.c"
486#endif 523#endif
487#if EV_USE_POLL 524#if EV_USE_POLL
488# include "ev_poll.c" 525# include "ev_poll.c"
536 methods = atoi (getenv ("LIBEV_METHODS")); 573 methods = atoi (getenv ("LIBEV_METHODS"));
537 else 574 else
538 methods = EVMETHOD_ANY; 575 methods = EVMETHOD_ANY;
539 576
540 ev_method = 0; 577 ev_method = 0;
578#if EV_USE_KQUEUE
579 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
580#endif
541#if EV_USE_EPOLL 581#if EV_USE_EPOLL
542 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 582 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
543#endif 583#endif
544#if EV_USE_POLL 584#if EV_USE_POLL
545 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 585 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
549#endif 589#endif
550 590
551 if (ev_method) 591 if (ev_method)
552 { 592 {
553 ev_watcher_init (&sigev, sigcb); 593 ev_watcher_init (&sigev, sigcb);
594 ev_set_priority (&sigev, EV_MAXPRI);
554 siginit (); 595 siginit ();
555 596
597#ifndef WIN32
556 ev_signal_init (&childev, childcb, SIGCHLD); 598 ev_signal_init (&childev, childcb, SIGCHLD);
599 ev_set_priority (&childev, EV_MAXPRI);
557 ev_signal_start (&childev); 600 ev_signal_start (&childev);
601#endif
558 } 602 }
559 } 603 }
560 604
561 return ev_method; 605 return ev_method;
562} 606}
593/*****************************************************************************/ 637/*****************************************************************************/
594 638
595static void 639static void
596call_pending (void) 640call_pending (void)
597{ 641{
642 int pri;
643
644 for (pri = NUMPRI; pri--; )
598 while (pendingcnt) 645 while (pendingcnt [pri])
599 { 646 {
600 ANPENDING *p = pendings + --pendingcnt; 647 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
601 648
602 if (p->w) 649 if (p->w)
603 { 650 {
604 p->w->pending = 0; 651 p->w->pending = 0;
605 p->w->cb (p->w, p->events); 652 p->w->cb (p->w, p->events);
606 } 653 }
607 } 654 }
608} 655}
609 656
610static void 657static void
611timers_reify (void) 658timers_reify (void)
612{ 659{
844static void 891static void
845ev_clear_pending (W w) 892ev_clear_pending (W w)
846{ 893{
847 if (w->pending) 894 if (w->pending)
848 { 895 {
849 pendings [w->pending - 1].w = 0; 896 pendings [ABSPRI (w)][w->pending - 1].w = 0;
850 w->pending = 0; 897 w->pending = 0;
851 } 898 }
852} 899}
853 900
854static void 901static void
855ev_start (W w, int active) 902ev_start (W w, int active)
856{ 903{
904 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
905 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
906
857 w->active = active; 907 w->active = active;
858} 908}
859 909
860static void 910static void
861ev_stop (W w) 911ev_stop (W w)
978 } 1028 }
979 1029
980 ev_stop ((W)w); 1030 ev_stop ((W)w);
981} 1031}
982 1032
1033#ifndef SA_RESTART
1034# define SA_RESTART 0
1035#endif
1036
983void 1037void
984ev_signal_start (struct ev_signal *w) 1038ev_signal_start (struct ev_signal *w)
985{ 1039{
986 if (ev_is_active (w)) 1040 if (ev_is_active (w))
987 return; 1041 return;
995 if (!w->next) 1049 if (!w->next)
996 { 1050 {
997 struct sigaction sa; 1051 struct sigaction sa;
998 sa.sa_handler = sighandler; 1052 sa.sa_handler = sighandler;
999 sigfillset (&sa.sa_mask); 1053 sigfillset (&sa.sa_mask);
1000 sa.sa_flags = 0; 1054 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1001 sigaction (w->signum, &sa, 0); 1055 sigaction (w->signum, &sa, 0);
1002 } 1056 }
1003} 1057}
1004 1058
1005void 1059void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines