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.50 by root, Sat Nov 3 19:41:55 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#if EV_USE_CONFIG_H 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33#endif 33#endif
34 34
35#include <math.h> 35#include <math.h>
36#include <stdlib.h> 36#include <stdlib.h>
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
166 175
167/*****************************************************************************/ 176/*****************************************************************************/
168 177
169typedef struct 178typedef struct
170{ 179{
171 struct ev_io *head; 180 struct ev_watcher_list *head;
172 unsigned char events; 181 unsigned char events;
173 unsigned char reify; 182 unsigned char reify;
174} ANFD; 183} ANFD;
175 184
176static ANFD *anfds; 185static ANFD *anfds;
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{
226fd_event (int fd, int events) 235fd_event (int fd, int events)
227{ 236{
228 ANFD *anfd = anfds + fd; 237 ANFD *anfd = anfds + fd;
229 struct ev_io *w; 238 struct ev_io *w;
230 239
231 for (w = anfd->head; w; w = w->next) 240 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
232 { 241 {
233 int ev = w->events & events; 242 int ev = w->events & events;
234 243
235 if (ev) 244 if (ev)
236 event ((W)w, ev); 245 event ((W)w, ev);
253 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
254 struct ev_io *w; 263 struct ev_io *w;
255 264
256 int events = 0; 265 int events = 0;
257 266
258 for (w = anfd->head; w; w = w->next) 267 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
259 events |= w->events; 268 events |= w->events;
260 269
261 anfd->reify = 0; 270 anfd->reify = 0;
262 271
263 if (anfd->events != events) 272 if (anfd->events != events)
286static void 295static void
287fd_kill (int fd) 296fd_kill (int fd)
288{ 297{
289 struct ev_io *w; 298 struct ev_io *w;
290 299
291 printf ("killing fd %d\n", fd);//D
292 while ((w = anfds [fd].head)) 300 while ((w = (struct ev_io *)anfds [fd].head))
293 { 301 {
294 ev_io_stop (w); 302 ev_io_stop (w);
295 event ((W)w, EV_ERROR | EV_READ | EV_WRITE); 303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
296 } 304 }
297} 305}
374 382
375/*****************************************************************************/ 383/*****************************************************************************/
376 384
377typedef struct 385typedef struct
378{ 386{
379 struct ev_signal *head; 387 struct ev_watcher_list *head;
380 sig_atomic_t volatile gotsig; 388 sig_atomic_t volatile gotsig;
381} ANSIG; 389} ANSIG;
382 390
383static ANSIG *signals; 391static ANSIG *signals;
384static int signalmax; 392static int signalmax;
404{ 412{
405 signals [signum - 1].gotsig = 1; 413 signals [signum - 1].gotsig = 1;
406 414
407 if (!gotsig) 415 if (!gotsig)
408 { 416 {
417 int old_errno = errno;
409 gotsig = 1; 418 gotsig = 1;
410 write (sigpipe [1], &signum, 1); 419 write (sigpipe [1], &signum, 1);
420 errno = old_errno;
411 } 421 }
412} 422}
413 423
414static void 424static void
415sigcb (struct ev_io *iow, int revents) 425sigcb (struct ev_io *iow, int revents)
416{ 426{
417 struct ev_signal *w; 427 struct ev_watcher_list *w;
418 int signum; 428 int signum;
419 429
420 read (sigpipe [0], &revents, 1); 430 read (sigpipe [0], &revents, 1);
421 gotsig = 0; 431 gotsig = 0;
422 432
431} 441}
432 442
433static void 443static void
434siginit (void) 444siginit (void)
435{ 445{
446#ifndef WIN32
436 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
437 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
438 449
439 /* rather than sort out wether we really need nb, set it */ 450 /* rather than sort out wether we really need nb, set it */
440 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
441 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
453#endif
442 454
443 ev_io_set (&sigev, sigpipe [0], EV_READ); 455 ev_io_set (&sigev, sigpipe [0], EV_READ);
444 ev_io_start (&sigev); 456 ev_io_start (&sigev);
445} 457}
446 458
458/*****************************************************************************/ 470/*****************************************************************************/
459 471
460static struct ev_child *childs [PID_HASHSIZE]; 472static struct ev_child *childs [PID_HASHSIZE];
461static struct ev_signal childev; 473static struct ev_signal childev;
462 474
475#ifndef WIN32
476
463#ifndef WCONTINUED 477#ifndef WCONTINUED
464# define WCONTINUED 0 478# define WCONTINUED 0
465#endif 479#endif
466 480
467static void 481static void
482child_reap (struct ev_signal *sw, int chain, int pid, int status)
483{
484 struct ev_child *w;
485
486 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
487 if (w->pid == pid || !w->pid)
488 {
489 w->priority = sw->priority; /* need to do it *now* */
490 w->rpid = pid;
491 w->rstatus = status;
492 event ((W)w, EV_CHILD);
493 }
494}
495
496static void
468childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
469{ 498{
470 struct ev_child *w;
471 int pid, status; 499 int pid, status;
472 500
473 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
474 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 502 {
475 if (w->pid == pid || !w->pid) 503 /* make sure we are called again until all childs have been reaped */
476 {
477 w->status = status;
478 event ((W)w, EV_CHILD); 504 event ((W)sw, EV_SIGNAL);
479 } 505
506 child_reap (sw, pid, pid, status);
507 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
508 }
480} 509}
510
511#endif
481 512
482/*****************************************************************************/ 513/*****************************************************************************/
483 514
515#if EV_USE_KQUEUE
516# include "ev_kqueue.c"
517#endif
484#if EV_USE_EPOLL 518#if EV_USE_EPOLL
485# include "ev_epoll.c" 519# include "ev_epoll.c"
486#endif 520#endif
487#if EV_USE_POLL 521#if EV_USE_POLL
488# include "ev_poll.c" 522# include "ev_poll.c"
501ev_version_minor (void) 535ev_version_minor (void)
502{ 536{
503 return EV_VERSION_MINOR; 537 return EV_VERSION_MINOR;
504} 538}
505 539
506/* return true if we are running with elevated privileges and ignore env variables */ 540/* return true if we are running with elevated privileges and should ignore env variables */
507static int 541static int
508enable_secure () 542enable_secure ()
509{ 543{
544#ifdef WIN32
545 return 0;
546#else
510 return getuid () != geteuid () 547 return getuid () != geteuid ()
511 || getgid () != getegid (); 548 || getgid () != getegid ();
549#endif
512} 550}
513 551
514int ev_init (int methods) 552int ev_init (int methods)
515{ 553{
516 if (!ev_method) 554 if (!ev_method)
530 568
531 if (pipe (sigpipe)) 569 if (pipe (sigpipe))
532 return 0; 570 return 0;
533 571
534 if (methods == EVMETHOD_AUTO) 572 if (methods == EVMETHOD_AUTO)
535 if (!enable_secure () && getenv ("LIBEV_METHODS")) 573 if (!enable_secure () && getenv ("LIBEV_METHODS"))
536 methods = atoi (getenv ("LIBEV_METHODS")); 574 methods = atoi (getenv ("LIBEV_METHODS"));
537 else 575 else
538 methods = EVMETHOD_ANY; 576 methods = EVMETHOD_ANY;
539 577
540 ev_method = 0; 578 ev_method = 0;
579#if EV_USE_KQUEUE
580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
581#endif
541#if EV_USE_EPOLL 582#if EV_USE_EPOLL
542 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 583 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
543#endif 584#endif
544#if EV_USE_POLL 585#if EV_USE_POLL
545 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 586 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
549#endif 590#endif
550 591
551 if (ev_method) 592 if (ev_method)
552 { 593 {
553 ev_watcher_init (&sigev, sigcb); 594 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI);
554 siginit (); 596 siginit ();
555 597
598#ifndef WIN32
556 ev_signal_init (&childev, childcb, SIGCHLD); 599 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI);
557 ev_signal_start (&childev); 601 ev_signal_start (&childev);
602#endif
558 } 603 }
559 } 604 }
560 605
561 return ev_method; 606 return ev_method;
562} 607}
593/*****************************************************************************/ 638/*****************************************************************************/
594 639
595static void 640static void
596call_pending (void) 641call_pending (void)
597{ 642{
643 int pri;
644
645 for (pri = NUMPRI; pri--; )
598 while (pendingcnt) 646 while (pendingcnt [pri])
599 { 647 {
600 ANPENDING *p = pendings + --pendingcnt; 648 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
601 649
602 if (p->w) 650 if (p->w)
603 { 651 {
604 p->w->pending = 0; 652 p->w->pending = 0;
605 p->w->cb (p->w, p->events); 653 p->w->cb (p->w, p->events);
606 } 654 }
607 } 655 }
608} 656}
609 657
610static void 658static void
611timers_reify (void) 659timers_reify (void)
612{ 660{
844static void 892static void
845ev_clear_pending (W w) 893ev_clear_pending (W w)
846{ 894{
847 if (w->pending) 895 if (w->pending)
848 { 896 {
849 pendings [w->pending - 1].w = 0; 897 pendings [ABSPRI (w)][w->pending - 1].w = 0;
850 w->pending = 0; 898 w->pending = 0;
851 } 899 }
852} 900}
853 901
854static void 902static void
855ev_start (W w, int active) 903ev_start (W w, int active)
856{ 904{
905 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
906 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
907
857 w->active = active; 908 w->active = active;
858} 909}
859 910
860static void 911static void
861ev_stop (W w) 912ev_stop (W w)
978 } 1029 }
979 1030
980 ev_stop ((W)w); 1031 ev_stop ((W)w);
981} 1032}
982 1033
1034#ifndef SA_RESTART
1035# define SA_RESTART 0
1036#endif
1037
983void 1038void
984ev_signal_start (struct ev_signal *w) 1039ev_signal_start (struct ev_signal *w)
985{ 1040{
986 if (ev_is_active (w)) 1041 if (ev_is_active (w))
987 return; 1042 return;
995 if (!w->next) 1050 if (!w->next)
996 { 1051 {
997 struct sigaction sa; 1052 struct sigaction sa;
998 sa.sa_handler = sighandler; 1053 sa.sa_handler = sighandler;
999 sigfillset (&sa.sa_mask); 1054 sigfillset (&sa.sa_mask);
1000 sa.sa_flags = 0; 1055 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1001 sigaction (w->signum, &sa, 0); 1056 sigaction (w->signum, &sa, 0);
1002 } 1057 }
1003} 1058}
1004 1059
1005void 1060void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines