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

Comparing libev/ev.c (file contents):
Revision 1.58 by root, Sun Nov 4 16:52:52 2007 UTC vs.
Revision 1.61 by root, Sun Nov 4 19:45:09 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#ifndef EV_EMBED 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 115#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 116#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 117#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 118/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
97 119
98#ifndef EV_EMBED
99# include "ev.h" 120#include "ev.h"
100#endif
101 121
102#if __GNUC__ >= 3 122#if __GNUC__ >= 3
103# define expect(expr,value) __builtin_expect ((expr),(value)) 123# define expect(expr,value) __builtin_expect ((expr),(value))
104# define inline inline 124# define inline inline
105#else 125#else
351 /* this should be highly optimised to not do anything but set a flag */ 371 /* this should be highly optimised to not do anything but set a flag */
352 for (fd = 0; fd < anfdmax; ++fd) 372 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events) 373 if (anfds [fd].events)
354 { 374 {
355 anfds [fd].events = 0; 375 anfds [fd].events = 0;
356 fd_change (fd); 376 fd_change (EV_A_ fd);
357 } 377 }
358} 378}
359 379
360/*****************************************************************************/ 380/*****************************************************************************/
361 381
411static ANSIG *signals; 431static ANSIG *signals;
412static int signalmax; 432static int signalmax;
413 433
414static int sigpipe [2]; 434static int sigpipe [2];
415static sig_atomic_t volatile gotsig; 435static sig_atomic_t volatile gotsig;
436static struct ev_io sigev;
416 437
417static void 438static void
418signals_init (ANSIG *base, int count) 439signals_init (ANSIG *base, int count)
419{ 440{
420 while (count--) 441 while (count--)
477} 498}
478 499
479/*****************************************************************************/ 500/*****************************************************************************/
480 501
481#ifndef WIN32 502#ifndef WIN32
503
504static struct ev_child *childs [PID_HASHSIZE];
505static struct ev_signal childev;
482 506
483#ifndef WCONTINUED 507#ifndef WCONTINUED
484# define WCONTINUED 0 508# define WCONTINUED 0
485#endif 509#endif
486 510
522# include "ev_kqueue.c" 546# include "ev_kqueue.c"
523#endif 547#endif
524#if EV_USE_EPOLL 548#if EV_USE_EPOLL
525# include "ev_epoll.c" 549# include "ev_epoll.c"
526#endif 550#endif
527#if EV_USEV_POLL 551#if EV_USE_POLL
528# include "ev_poll.c" 552# include "ev_poll.c"
529#endif 553#endif
530#if EV_USE_SELECT 554#if EV_USE_SELECT
531# include "ev_select.c" 555# include "ev_select.c"
532#endif 556#endif
590 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
591#endif 615#endif
592#if EV_USE_EPOLL 616#if EV_USE_EPOLL
593 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
594#endif 618#endif
595#if EV_USEV_POLL 619#if EV_USE_POLL
596 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 620 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
597#endif 621#endif
598#if EV_USE_SELECT 622#if EV_USE_SELECT
599 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 623 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
600#endif 624#endif
608 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
609#endif 633#endif
610#if EV_USE_EPOLL 634#if EV_USE_EPOLL
611 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
612#endif 636#endif
613#if EV_USEV_POLL 637#if EV_USE_POLL
614 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
615#endif 639#endif
616#if EV_USE_SELECT 640#if EV_USE_SELECT
617 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 641 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
618#endif 642#endif
639{ 663{
640 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 664 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
641 665
642 loop_init (EV_A_ methods); 666 loop_init (EV_A_ methods);
643 667
644 if (ev_methods (EV_A)) 668 if (ev_method (EV_A))
645 return loop; 669 return loop;
646 670
647 return 0; 671 return 0;
648} 672}
649 673
726 750
727 loop_destroy (EV_A); 751 loop_destroy (EV_A);
728} 752}
729 753
730void 754void
731ev_default_fork (EV_P) 755ev_default_fork (void)
732{ 756{
757#if EV_MULTIPLICITY
758 struct ev_loop *loop = default_loop;
759#endif
760
733 loop_fork (EV_A); 761 loop_fork (EV_A);
734 762
735 ev_io_stop (EV_A_ &sigev); 763 ev_io_stop (EV_A_ &sigev);
736 close (sigpipe [0]); 764 close (sigpipe [0]);
737 close (sigpipe [1]); 765 close (sigpipe [1]);
765timers_reify (EV_P) 793timers_reify (EV_P)
766{ 794{
767 while (timercnt && timers [0]->at <= mn_now) 795 while (timercnt && timers [0]->at <= mn_now)
768 { 796 {
769 struct ev_timer *w = timers [0]; 797 struct ev_timer *w = timers [0];
798
799 assert (("inactive timer on timer heap detected", ev_is_active (w)));
770 800
771 /* first reschedule or stop timer */ 801 /* first reschedule or stop timer */
772 if (w->repeat) 802 if (w->repeat)
773 { 803 {
774 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 804 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
787{ 817{
788 while (periodiccnt && periodics [0]->at <= rt_now) 818 while (periodiccnt && periodics [0]->at <= rt_now)
789 { 819 {
790 struct ev_periodic *w = periodics [0]; 820 struct ev_periodic *w = periodics [0];
791 821
822 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
823
792 /* first reschedule or stop timer */ 824 /* first reschedule or stop timer */
793 if (w->interval) 825 if (w->interval)
794 { 826 {
795 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 827 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
796 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 828 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines