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

Comparing libev/ev.c (file contents):
Revision 1.33 by root, Thu Nov 1 11:11:22 2007 UTC vs.
Revision 1.44 by root, Fri Nov 2 20:59:14 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
44#include <sys/types.h> 46#include <sys/types.h>
45#include <sys/wait.h> 47#include <sys/wait.h>
46#include <sys/time.h> 48#include <sys/time.h>
47#include <time.h> 49#include <time.h>
48 50
51/**/
52
49#ifndef EV_USE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 54# define EV_USE_MONOTONIC 1
52# endif
53#endif 55#endif
54 56
55#ifndef EV_USE_SELECT 57#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 58# define EV_USE_SELECT 1
57#endif 59#endif
58 60
61#ifndef EV_USE_POLL
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
63#endif
64
59#ifndef EV_USE_EPOLL 65#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 66# define EV_USE_EPOLL 0
61#endif 67#endif
62 68
69#ifndef EV_USE_KQUEUE
70# define EV_USE_KQUEUE 0
71#endif
72
73#ifndef EV_USE_REALTIME
74# define EV_USE_REALTIME 1
75#endif
76
77/**/
78
79#ifndef CLOCK_MONOTONIC
80# undef EV_USE_MONOTONIC
81# define EV_USE_MONOTONIC 0
82#endif
83
63#ifndef CLOCK_REALTIME 84#ifndef CLOCK_REALTIME
85# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 86# define EV_USE_REALTIME 0
65#endif 87#endif
66#ifndef EV_USE_REALTIME 88
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 89/**/
68#endif
69 90
70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 91#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 92#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 93#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 94/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
74 95
75#include "ev.h" 96#include "ev.h"
97
98#if __GNUC__ >= 3
99# define expect(expr,value) __builtin_expect ((expr),(value))
100# define inline inline
101#else
102# define expect(expr,value) (expr)
103# define inline static
104#endif
105
106#define expect_false(expr) expect ((expr) != 0, 0)
107#define expect_true(expr) expect ((expr) != 0, 1)
108
109#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
110#define ABSPRI(w) ((w)->priority - EV_MINPRI)
76 111
77typedef struct ev_watcher *W; 112typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 113typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 114typedef struct ev_watcher_time *WT;
80 115
81static ev_tstamp now, diff; /* monotonic clock */ 116static ev_tstamp now_floor, now, diff; /* monotonic clock */
82ev_tstamp ev_now; 117ev_tstamp ev_now;
83int ev_method; 118int ev_method;
84 119
85static int have_monotonic; /* runtime */ 120static int have_monotonic; /* runtime */
86 121
106 141
107static ev_tstamp 142static ev_tstamp
108get_clock (void) 143get_clock (void)
109{ 144{
110#if EV_USE_MONOTONIC 145#if EV_USE_MONOTONIC
111 if (have_monotonic) 146 if (expect_true (have_monotonic))
112 { 147 {
113 struct timespec ts; 148 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 149 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 150 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 151 }
120} 155}
121 156
122#define array_roundsize(base,n) ((n) | 4 & ~3) 157#define array_roundsize(base,n) ((n) | 4 & ~3)
123 158
124#define array_needsize(base,cur,cnt,init) \ 159#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 160 if (expect_false ((cnt) > cur)) \
126 { \ 161 { \
127 int newcnt = cur; \ 162 int newcnt = cur; \
128 do \ 163 do \
129 { \ 164 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 165 newcnt = array_roundsize (base, newcnt << 1); \
165{ 200{
166 W w; 201 W w;
167 int events; 202 int events;
168} ANPENDING; 203} ANPENDING;
169 204
170static ANPENDING *pendings; 205static ANPENDING *pendings [NUMPRI];
171static int pendingmax, pendingcnt; 206static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
172 207
173static void 208static void
174event (W w, int events) 209event (W w, int events)
175{ 210{
176 if (w->pending) 211 if (w->pending)
177 { 212 {
178 pendings [w->pending - 1].events |= events; 213 pendings [ABSPRI (w)][w->pending - 1].events |= events;
179 return; 214 return;
180 } 215 }
181 216
182 w->pending = ++pendingcnt; 217 w->pending = ++pendingcnt [ABSPRI (w)];
183 array_needsize (pendings, pendingmax, pendingcnt, ); 218 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
184 pendings [pendingcnt - 1].w = w; 219 pendings [ABSPRI (w)][w->pending - 1].w = w;
185 pendings [pendingcnt - 1].events = events; 220 pendings [ABSPRI (w)][w->pending - 1].events = events;
186} 221}
187 222
188static void 223static void
189queue_events (W *events, int eventcnt, int type) 224queue_events (W *events, int eventcnt, int type)
190{ 225{
253 ++fdchangecnt; 288 ++fdchangecnt;
254 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 289 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
255 fdchanges [fdchangecnt - 1] = fd; 290 fdchanges [fdchangecnt - 1] = fd;
256} 291}
257 292
293static void
294fd_kill (int fd)
295{
296 struct ev_io *w;
297
298 printf ("killing fd %d\n", fd);//D
299 while ((w = anfds [fd].head))
300 {
301 ev_io_stop (w);
302 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
303 }
304}
305
258/* called on EBADF to verify fds */ 306/* called on EBADF to verify fds */
259static void 307static void
260fd_recheck (void) 308fd_ebadf (void)
261{ 309{
262 int fd; 310 int fd;
263 311
264 for (fd = 0; fd < anfdmax; ++fd) 312 for (fd = 0; fd < anfdmax; ++fd)
265 if (anfds [fd].events) 313 if (anfds [fd].events)
266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 314 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
267 while (anfds [fd].head) 315 fd_kill (fd);
316}
317
318/* called on ENOMEM in select/poll to kill some fds and retry */
319static void
320fd_enomem (void)
321{
322 int fd = anfdmax;
323
324 while (fd--)
325 if (anfds [fd].events)
268 { 326 {
269 ev_io_stop (anfds [fd].head); 327 close (fd);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 328 fd_kill (fd);
329 return;
271 } 330 }
272} 331}
273 332
274/*****************************************************************************/ 333/*****************************************************************************/
275 334
276static struct ev_timer **timers; 335static struct ev_timer **timers;
323/*****************************************************************************/ 382/*****************************************************************************/
324 383
325typedef struct 384typedef struct
326{ 385{
327 struct ev_signal *head; 386 struct ev_signal *head;
328 sig_atomic_t gotsig; 387 sig_atomic_t volatile gotsig;
329} ANSIG; 388} ANSIG;
330 389
331static ANSIG *signals; 390static ANSIG *signals;
332static int signalmax; 391static int signalmax;
333 392
334static int sigpipe [2]; 393static int sigpipe [2];
335static sig_atomic_t gotsig; 394static sig_atomic_t volatile gotsig;
336static struct ev_io sigev; 395static struct ev_io sigev;
337 396
338static void 397static void
339signals_init (ANSIG *base, int count) 398signals_init (ANSIG *base, int count)
340{ 399{
353 signals [signum - 1].gotsig = 1; 412 signals [signum - 1].gotsig = 1;
354 413
355 if (!gotsig) 414 if (!gotsig)
356 { 415 {
357 gotsig = 1; 416 gotsig = 1;
358 write (sigpipe [1], &gotsig, 1); 417 write (sigpipe [1], &signum, 1);
359 } 418 }
360} 419}
361 420
362static void 421static void
363sigcb (struct ev_io *iow, int revents) 422sigcb (struct ev_io *iow, int revents)
364{ 423{
365 struct ev_signal *w; 424 struct ev_signal *w;
366 int sig; 425 int signum;
367 426
427 read (sigpipe [0], &revents, 1);
368 gotsig = 0; 428 gotsig = 0;
369 read (sigpipe [0], &revents, 1);
370 429
371 for (sig = signalmax; sig--; ) 430 for (signum = signalmax; signum--; )
372 if (signals [sig].gotsig) 431 if (signals [signum].gotsig)
373 { 432 {
374 signals [sig].gotsig = 0; 433 signals [signum].gotsig = 0;
375 434
376 for (w = signals [sig].head; w; w = w->next) 435 for (w = signals [signum].head; w; w = w->next)
377 event ((W)w, EV_SIGNAL); 436 event ((W)w, EV_SIGNAL);
378 } 437 }
379} 438}
380 439
381static void 440static void
418 struct ev_child *w; 477 struct ev_child *w;
419 int pid, status; 478 int pid, status;
420 479
421 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 480 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
422 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 481 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
423 if (w->pid == pid || w->pid == -1) 482 if (w->pid == pid || !w->pid)
424 { 483 {
425 w->status = status; 484 w->status = status;
426 event ((W)w, EV_CHILD); 485 event ((W)w, EV_CHILD);
427 } 486 }
428} 487}
429 488
430/*****************************************************************************/ 489/*****************************************************************************/
431 490
491#if EV_USE_KQUEUE
492# include "ev_kqueue.c"
493#endif
432#if EV_USE_EPOLL 494#if EV_USE_EPOLL
433# include "ev_epoll.c" 495# include "ev_epoll.c"
434#endif 496#endif
497#if EV_USE_POLL
498# include "ev_poll.c"
499#endif
435#if EV_USE_SELECT 500#if EV_USE_SELECT
436# include "ev_select.c" 501# include "ev_select.c"
437#endif 502#endif
438 503
439int 504int
446ev_version_minor (void) 511ev_version_minor (void)
447{ 512{
448 return EV_VERSION_MINOR; 513 return EV_VERSION_MINOR;
449} 514}
450 515
516/* return true if we are running with elevated privileges and ignore env variables */
517static int
518enable_secure ()
519{
520 return getuid () != geteuid ()
521 || getgid () != getegid ();
522}
523
451int ev_init (int flags) 524int ev_init (int methods)
452{ 525{
453 if (!ev_method) 526 if (!ev_method)
454 { 527 {
455#if EV_USE_MONOTONIC 528#if EV_USE_MONOTONIC
456 { 529 {
458 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 531 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
459 have_monotonic = 1; 532 have_monotonic = 1;
460 } 533 }
461#endif 534#endif
462 535
463 ev_now = ev_time (); 536 ev_now = ev_time ();
464 now = get_clock (); 537 now = get_clock ();
538 now_floor = now;
465 diff = ev_now - now; 539 diff = ev_now - now;
466 540
467 if (pipe (sigpipe)) 541 if (pipe (sigpipe))
468 return 0; 542 return 0;
469 543
544 if (methods == EVMETHOD_AUTO)
545 if (!enable_secure () && getenv ("LIBEV_METHODS"))
546 methods = atoi (getenv ("LIBEV_METHODS"));
547 else
470 ev_method = EVMETHOD_NONE; 548 methods = EVMETHOD_ANY;
549
550 ev_method = 0;
551#if EV_USE_KQUEUE
552 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
553#endif
471#if EV_USE_EPOLL 554#if EV_USE_EPOLL
472 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 555 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
556#endif
557#if EV_USE_POLL
558 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
473#endif 559#endif
474#if EV_USE_SELECT 560#if EV_USE_SELECT
475 if (ev_method == EVMETHOD_NONE) select_init (flags); 561 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
476#endif 562#endif
477 563
478 if (ev_method) 564 if (ev_method)
479 { 565 {
480 ev_watcher_init (&sigev, sigcb); 566 ev_watcher_init (&sigev, sigcb);
489} 575}
490 576
491/*****************************************************************************/ 577/*****************************************************************************/
492 578
493void 579void
494ev_prefork (void) 580ev_fork_prepare (void)
495{ 581{
496 /* nop */ 582 /* nop */
497} 583}
498 584
499void 585void
500ev_postfork_parent (void) 586ev_fork_parent (void)
501{ 587{
502 /* nop */ 588 /* nop */
503} 589}
504 590
505void 591void
506ev_postfork_child (void) 592ev_fork_child (void)
507{ 593{
508#if EV_USE_EPOLL 594#if EV_USE_EPOLL
509 if (ev_method == EVMETHOD_EPOLL) 595 if (ev_method == EVMETHOD_EPOLL)
510 epoll_postfork_child (); 596 epoll_postfork_child ();
511#endif 597#endif
520/*****************************************************************************/ 606/*****************************************************************************/
521 607
522static void 608static void
523call_pending (void) 609call_pending (void)
524{ 610{
611 int pri;
612
613 for (pri = NUMPRI; pri--; )
525 while (pendingcnt) 614 while (pendingcnt [pri])
526 { 615 {
527 ANPENDING *p = pendings + --pendingcnt; 616 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
528 617
529 if (p->w) 618 if (p->w)
530 { 619 {
531 p->w->pending = 0; 620 p->w->pending = 0;
532 p->w->cb (p->w, p->events); 621 p->w->cb (p->w, p->events);
533 } 622 }
534 } 623 }
535} 624}
536 625
537static void 626static void
538timers_reify (void) 627timers_reify (void)
539{ 628{
599 } 688 }
600 } 689 }
601 } 690 }
602} 691}
603 692
693static int
694time_update_monotonic (void)
695{
696 now = get_clock ();
697
698 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
699 {
700 ev_now = now + diff;
701 return 0;
702 }
703 else
704 {
705 now_floor = now;
706 ev_now = ev_time ();
707 return 1;
708 }
709}
710
604static void 711static void
605time_update (void) 712time_update (void)
606{ 713{
607 int i; 714 int i;
608 715
609 ev_now = ev_time (); 716#if EV_USE_MONOTONIC
610
611 if (have_monotonic) 717 if (expect_true (have_monotonic))
612 { 718 {
719 if (time_update_monotonic ())
720 {
613 ev_tstamp odiff = diff; 721 ev_tstamp odiff = diff;
614 722
615 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 723 for (i = 4; --i; ) /* loop a few times, before making important decisions */
616 { 724 {
617 now = get_clock ();
618 diff = ev_now - now; 725 diff = ev_now - now;
619 726
620 if (fabs (odiff - diff) < MIN_TIMEJUMP) 727 if (fabs (odiff - diff) < MIN_TIMEJUMP)
621 return; /* all is well */ 728 return; /* all is well */
622 729
623 ev_now = ev_time (); 730 ev_now = ev_time ();
731 now = get_clock ();
732 now_floor = now;
624 } 733 }
625 734
626 periodics_reschedule (diff - odiff); 735 periodics_reschedule (diff - odiff);
627 /* no timer adjustment, as the monotonic clock doesn't jump */ 736 /* no timer adjustment, as the monotonic clock doesn't jump */
737 }
628 } 738 }
629 else 739 else
740#endif
630 { 741 {
742 ev_now = ev_time ();
743
631 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 744 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
632 { 745 {
633 periodics_reschedule (ev_now - now); 746 periodics_reschedule (ev_now - now);
634 747
635 /* adjust timers. this is easy, as the offset is the same for all */ 748 /* adjust timers. this is easy, as the offset is the same for all */
636 for (i = 0; i < timercnt; ++i) 749 for (i = 0; i < timercnt; ++i)
649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 762 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
650 763
651 do 764 do
652 { 765 {
653 /* queue check watchers (and execute them) */ 766 /* queue check watchers (and execute them) */
654 if (preparecnt) 767 if (expect_false (preparecnt))
655 { 768 {
656 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 769 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
657 call_pending (); 770 call_pending ();
658 } 771 }
659 772
662 775
663 /* calculate blocking time */ 776 /* calculate blocking time */
664 777
665 /* we only need this for !monotonic clockor timers, but as we basically 778 /* we only need this for !monotonic clockor timers, but as we basically
666 always have timers, we just calculate it always */ 779 always have timers, we just calculate it always */
780#if EV_USE_MONOTONIC
781 if (expect_true (have_monotonic))
782 time_update_monotonic ();
783 else
784#endif
785 {
667 ev_now = ev_time (); 786 ev_now = ev_time ();
787 now = ev_now;
788 }
668 789
669 if (flags & EVLOOP_NONBLOCK || idlecnt) 790 if (flags & EVLOOP_NONBLOCK || idlecnt)
670 block = 0.; 791 block = 0.;
671 else 792 else
672 { 793 {
673 block = MAX_BLOCKTIME; 794 block = MAX_BLOCKTIME;
674 795
675 if (timercnt) 796 if (timercnt)
676 { 797 {
677 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 798 ev_tstamp to = timers [0]->at - now + method_fudge;
678 if (block > to) block = to; 799 if (block > to) block = to;
679 } 800 }
680 801
681 if (periodiccnt) 802 if (periodiccnt)
682 { 803 {
739static void 860static void
740ev_clear_pending (W w) 861ev_clear_pending (W w)
741{ 862{
742 if (w->pending) 863 if (w->pending)
743 { 864 {
744 pendings [w->pending - 1].w = 0; 865 pendings [ABSPRI (w)][w->pending - 1].w = 0;
745 w->pending = 0; 866 w->pending = 0;
746 } 867 }
747} 868}
748 869
749static void 870static void
750ev_start (W w, int active) 871ev_start (W w, int active)
751{ 872{
873 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
874 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
875
752 w->active = active; 876 w->active = active;
753} 877}
754 878
755static void 879static void
756ev_stop (W w) 880ev_stop (W w)
761/*****************************************************************************/ 885/*****************************************************************************/
762 886
763void 887void
764ev_io_start (struct ev_io *w) 888ev_io_start (struct ev_io *w)
765{ 889{
890 int fd = w->fd;
891
766 if (ev_is_active (w)) 892 if (ev_is_active (w))
767 return; 893 return;
768
769 int fd = w->fd;
770 894
771 assert (("ev_io_start called with negative fd", fd >= 0)); 895 assert (("ev_io_start called with negative fd", fd >= 0));
772 896
773 ev_start ((W)w, 1); 897 ev_start ((W)w, 1);
774 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 898 array_needsize (anfds, anfdmax, fd + 1, anfds_init);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines