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

Comparing libev/ev.c (file contents):
Revision 1.101 by root, Sun Nov 11 04:04:23 2007 UTC vs.
Revision 1.110 by root, Mon Nov 12 05:56:49 2007 UTC

51 51
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
53# define EV_USE_POLL 1 53# define EV_USE_POLL 1
54# endif 54# endif
55 55
56# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
57# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
58# endif 58# endif
59 59
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
61# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
75#include <sys/types.h> 75#include <sys/types.h>
76#include <time.h> 76#include <time.h>
77 77
78#include <signal.h> 78#include <signal.h>
79 79
80#ifndef WIN32 80#ifndef _WIN32
81# include <unistd.h> 81# include <unistd.h>
82# include <sys/time.h> 82# include <sys/time.h>
83# include <sys/wait.h> 83# include <sys/wait.h>
84#else
85# define WIN32_LEAN_AND_MEAN
86# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1
84#endif 89# endif
90#endif
91
85/**/ 92/**/
86 93
87#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
88# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
89#endif 96#endif
90 97
91#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
92# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
93#endif 101#endif
94 102
95#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
96# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
97#endif 109#endif
98 110
99#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
100# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
101#endif 113#endif
102 114
103#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
104# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
105#endif 117#endif
106 118
107#ifndef EV_USE_WIN32
108# ifdef WIN32
109# define EV_USE_WIN32 0 /* it does not exist, use select */
110# undef EV_USE_SELECT
111# define EV_USE_SELECT 1
112# else
113# define EV_USE_WIN32 0
114# endif
115#endif
116
117#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
118# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
119#endif 121#endif
120 122
121/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
122 130
123#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
124# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
125# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
126#endif 134#endif
127 135
128#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
129# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
130# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
131#endif 143#endif
132 144
133/**/ 145/**/
134 146
135#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
155#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
156 168
157#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
158#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
159 171
172#define EMPTY /* required for microsofts broken pseudo-c compiler */
173
160typedef struct ev_watcher *W; 174typedef struct ev_watcher *W;
161typedef struct ev_watcher_list *WL; 175typedef struct ev_watcher_list *WL;
162typedef struct ev_watcher_time *WT; 176typedef struct ev_watcher_time *WT;
163 177
164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 178static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
165 179
166#ifdef WIN32 180#ifdef _WIN32
167# include "ev_win32.c" 181# include "ev_win32.c"
168#endif 182#endif
169 183
170/*****************************************************************************/ 184/*****************************************************************************/
171 185
220typedef struct 234typedef struct
221{ 235{
222 WL head; 236 WL head;
223 unsigned char events; 237 unsigned char events;
224 unsigned char reify; 238 unsigned char reify;
239#if EV_SELECT_IS_WINSOCKET
240 SOCKET handle;
241#endif
225} ANFD; 242} ANFD;
226 243
227typedef struct 244typedef struct
228{ 245{
229 W w; 246 W w;
293{ 310{
294 return ev_rt_now; 311 return ev_rt_now;
295} 312}
296#endif 313#endif
297 314
298#define array_roundsize(type,n) ((n) | 4 & ~3) 315#define array_roundsize(type,n) (((n) | 4) & ~3)
299 316
300#define array_needsize(type,base,cur,cnt,init) \ 317#define array_needsize(type,base,cur,cnt,init) \
301 if (expect_false ((cnt) > cur)) \ 318 if (expect_false ((cnt) > cur)) \
302 { \ 319 { \
303 int newcnt = cur; \ 320 int newcnt = cur; \
318 stem ## max = array_roundsize (stem ## cnt >> 1); \ 335 stem ## max = array_roundsize (stem ## cnt >> 1); \
319 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 336 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
320 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 337 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
321 } 338 }
322 339
323/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
324/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
325#define array_free_microshit(stem) \
326 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
327
328#define array_free(stem, idx) \ 340#define array_free(stem, idx) \
329 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 341 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
330 342
331/*****************************************************************************/ 343/*****************************************************************************/
332 344
406 int events = 0; 418 int events = 0;
407 419
408 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 420 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
409 events |= w->events; 421 events |= w->events;
410 422
423#if EV_SELECT_IS_WINSOCKET
424 if (events)
425 {
426 unsigned long argp;
427 anfd->handle = _get_osfhandle (fd);
428 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
429 }
430#endif
431
411 anfd->reify = 0; 432 anfd->reify = 0;
412 433
413 method_modify (EV_A_ fd, anfd->events, events); 434 method_modify (EV_A_ fd, anfd->events, events);
414 anfd->events = events; 435 anfd->events = events;
415 } 436 }
443} 464}
444 465
445static int 466static int
446fd_valid (int fd) 467fd_valid (int fd)
447{ 468{
448#ifdef WIN32 469#ifdef _WIN32
449 return !!win32_get_osfhandle (fd); 470 return _get_osfhandle (fd) != -1;
450#else 471#else
451 return fcntl (fd, F_GETFD) != -1; 472 return fcntl (fd, F_GETFD) != -1;
452#endif 473#endif
453} 474}
454 475
571} 592}
572 593
573static void 594static void
574sighandler (int signum) 595sighandler (int signum)
575{ 596{
576#if WIN32 597#if _WIN32
577 signal (signum, sighandler); 598 signal (signum, sighandler);
578#endif 599#endif
579 600
580 signals [signum - 1].gotsig = 1; 601 signals [signum - 1].gotsig = 1;
581 602
582 if (!gotsig) 603 if (!gotsig)
583 { 604 {
584 int old_errno = errno; 605 int old_errno = errno;
585 gotsig = 1; 606 gotsig = 1;
586#ifdef WIN32
587 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
588#else
589 write (sigpipe [1], &signum, 1); 607 write (sigpipe [1], &signum, 1);
590#endif
591 errno = old_errno; 608 errno = old_errno;
592 } 609 }
593} 610}
594 611
595void 612void
615static void 632static void
616sigcb (EV_P_ struct ev_io *iow, int revents) 633sigcb (EV_P_ struct ev_io *iow, int revents)
617{ 634{
618 int signum; 635 int signum;
619 636
620#ifdef WIN32
621 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
622#else
623 read (sigpipe [0], &revents, 1); 637 read (sigpipe [0], &revents, 1);
624#endif
625 gotsig = 0; 638 gotsig = 0;
626 639
627 for (signum = signalmax; signum--; ) 640 for (signum = signalmax; signum--; )
628 if (signals [signum].gotsig) 641 if (signals [signum].gotsig)
629 ev_feed_signal_event (EV_A_ signum + 1); 642 ev_feed_signal_event (EV_A_ signum + 1);
630} 643}
631 644
645inline void
646fd_intern (int fd)
647{
648#ifdef _WIN32
649 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
651#else
652 fcntl (fd, F_SETFD, FD_CLOEXEC);
653 fcntl (fd, F_SETFL, O_NONBLOCK);
654#endif
655}
656
632static void 657static void
633siginit (EV_P) 658siginit (EV_P)
634{ 659{
635#ifndef WIN32 660 fd_intern (sigpipe [0]);
636 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 661 fd_intern (sigpipe [1]);
637 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
638
639 /* rather than sort out wether we really need nb, set it */
640 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
641 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
642#endif
643 662
644 ev_io_set (&sigev, sigpipe [0], EV_READ); 663 ev_io_set (&sigev, sigpipe [0], EV_READ);
645 ev_io_start (EV_A_ &sigev); 664 ev_io_start (EV_A_ &sigev);
646 ev_unref (EV_A); /* child watcher should not keep loop alive */ 665 ev_unref (EV_A); /* child watcher should not keep loop alive */
647} 666}
648 667
649/*****************************************************************************/ 668/*****************************************************************************/
650 669
651static struct ev_child *childs [PID_HASHSIZE]; 670static struct ev_child *childs [PID_HASHSIZE];
652 671
653#ifndef WIN32 672#ifndef _WIN32
654 673
655static struct ev_signal childev; 674static struct ev_signal childev;
656 675
657#ifndef WCONTINUED 676#ifndef WCONTINUED
658# define WCONTINUED 0 677# define WCONTINUED 0
719 738
720/* return true if we are running with elevated privileges and should ignore env variables */ 739/* return true if we are running with elevated privileges and should ignore env variables */
721static int 740static int
722enable_secure (void) 741enable_secure (void)
723{ 742{
724#ifdef WIN32 743#ifdef _WIN32
725 return 0; 744 return 0;
726#else 745#else
727 return getuid () != geteuid () 746 return getuid () != geteuid ()
728 || getgid () != getegid (); 747 || getgid () != getegid ();
729#endif 748#endif
734{ 753{
735 return method; 754 return method;
736} 755}
737 756
738static void 757static void
739loop_init (EV_P_ int methods) 758loop_init (EV_P_ unsigned int flags)
740{ 759{
741 if (!method) 760 if (!method)
742 { 761 {
743#if EV_USE_MONOTONIC 762#if EV_USE_MONOTONIC
744 { 763 {
751 ev_rt_now = ev_time (); 770 ev_rt_now = ev_time ();
752 mn_now = get_clock (); 771 mn_now = get_clock ();
753 now_floor = mn_now; 772 now_floor = mn_now;
754 rtmn_diff = ev_rt_now - mn_now; 773 rtmn_diff = ev_rt_now - mn_now;
755 774
756 if (methods == EVMETHOD_AUTO) 775 if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
757 if (!enable_secure () && getenv ("LIBEV_METHODS"))
758 methods = atoi (getenv ("LIBEV_METHODS")); 776 flags = atoi (getenv ("LIBEV_FLAGS"));
759 else 777
760 methods = EVMETHOD_ANY; 778 if (!(flags & 0x0000ffff))
779 flags |= 0x0000ffff;
761 780
762 method = 0; 781 method = 0;
763#if EV_USE_WIN32
764 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
765#endif
766#if EV_USE_KQUEUE 782#if EV_USE_KQUEUE
767 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 783 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
768#endif 784#endif
769#if EV_USE_EPOLL 785#if EV_USE_EPOLL
770 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 786 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
771#endif 787#endif
772#if EV_USE_POLL 788#if EV_USE_POLL
773 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 789 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
774#endif 790#endif
775#if EV_USE_SELECT 791#if EV_USE_SELECT
776 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 792 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
777#endif 793#endif
778 794
779 ev_init (&sigev, sigcb); 795 ev_init (&sigev, sigcb);
780 ev_set_priority (&sigev, EV_MAXPRI); 796 ev_set_priority (&sigev, EV_MAXPRI);
781 } 797 }
784void 800void
785loop_destroy (EV_P) 801loop_destroy (EV_P)
786{ 802{
787 int i; 803 int i;
788 804
789#if EV_USE_WIN32
790 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
791#endif
792#if EV_USE_KQUEUE 805#if EV_USE_KQUEUE
793 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
794#endif 807#endif
795#if EV_USE_EPOLL 808#if EV_USE_EPOLL
796 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
804 817
805 for (i = NUMPRI; i--; ) 818 for (i = NUMPRI; i--; )
806 array_free (pending, [i]); 819 array_free (pending, [i]);
807 820
808 /* have to use the microsoft-never-gets-it-right macro */ 821 /* have to use the microsoft-never-gets-it-right macro */
809 array_free_microshit (fdchange); 822 array_free (fdchange, EMPTY);
810 array_free_microshit (timer); 823 array_free (timer, EMPTY);
811#if EV_PERIODICS 824#if EV_PERIODICS
812 array_free_microshit (periodic); 825 array_free (periodic, EMPTY);
813#endif 826#endif
814 array_free_microshit (idle); 827 array_free (idle, EMPTY);
815 array_free_microshit (prepare); 828 array_free (prepare, EMPTY);
816 array_free_microshit (check); 829 array_free (check, EMPTY);
817 830
818 method = 0; 831 method = 0;
819} 832}
820 833
821static void 834static void
846 postfork = 0; 859 postfork = 0;
847} 860}
848 861
849#if EV_MULTIPLICITY 862#if EV_MULTIPLICITY
850struct ev_loop * 863struct ev_loop *
851ev_loop_new (int methods) 864ev_loop_new (unsigned int flags)
852{ 865{
853 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 866 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
854 867
855 memset (loop, 0, sizeof (struct ev_loop)); 868 memset (loop, 0, sizeof (struct ev_loop));
856 869
857 loop_init (EV_A_ methods); 870 loop_init (EV_A_ flags);
858 871
859 if (ev_method (EV_A)) 872 if (ev_method (EV_A))
860 return loop; 873 return loop;
861 874
862 return 0; 875 return 0;
880#if EV_MULTIPLICITY 893#if EV_MULTIPLICITY
881struct ev_loop * 894struct ev_loop *
882#else 895#else
883int 896int
884#endif 897#endif
885ev_default_loop (int methods) 898ev_default_loop (unsigned int flags)
886{ 899{
887 if (sigpipe [0] == sigpipe [1]) 900 if (sigpipe [0] == sigpipe [1])
888 if (pipe (sigpipe)) 901 if (pipe (sigpipe))
889 return 0; 902 return 0;
890 903
894 struct ev_loop *loop = default_loop = &default_loop_struct; 907 struct ev_loop *loop = default_loop = &default_loop_struct;
895#else 908#else
896 default_loop = 1; 909 default_loop = 1;
897#endif 910#endif
898 911
899 loop_init (EV_A_ methods); 912 loop_init (EV_A_ flags);
900 913
901 if (ev_method (EV_A)) 914 if (ev_method (EV_A))
902 { 915 {
903 siginit (EV_A); 916 siginit (EV_A);
904 917
905#ifndef WIN32 918#ifndef _WIN32
906 ev_signal_init (&childev, childcb, SIGCHLD); 919 ev_signal_init (&childev, childcb, SIGCHLD);
907 ev_set_priority (&childev, EV_MAXPRI); 920 ev_set_priority (&childev, EV_MAXPRI);
908 ev_signal_start (EV_A_ &childev); 921 ev_signal_start (EV_A_ &childev);
909 ev_unref (EV_A); /* child watcher should not keep loop alive */ 922 ev_unref (EV_A); /* child watcher should not keep loop alive */
910#endif 923#endif
921{ 934{
922#if EV_MULTIPLICITY 935#if EV_MULTIPLICITY
923 struct ev_loop *loop = default_loop; 936 struct ev_loop *loop = default_loop;
924#endif 937#endif
925 938
926#ifndef WIN32 939#ifndef _WIN32
927 ev_ref (EV_A); /* child watcher */ 940 ev_ref (EV_A); /* child watcher */
928 ev_signal_stop (EV_A_ &childev); 941 ev_signal_stop (EV_A_ &childev);
929#endif 942#endif
930 943
931 ev_ref (EV_A); /* signal watcher */ 944 ev_ref (EV_A); /* signal watcher */
1018 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1031 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1019 1032
1020 /* first reschedule or stop timer */ 1033 /* first reschedule or stop timer */
1021 if (w->reschedule_cb) 1034 if (w->reschedule_cb)
1022 { 1035 {
1023 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1036 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1024
1025 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1037 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1026 downheap ((WT *)periodics, periodiccnt, 0); 1038 downheap ((WT *)periodics, periodiccnt, 0);
1027 } 1039 }
1028 else if (w->interval) 1040 else if (w->interval)
1029 { 1041 {
1511 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1523 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1512 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1524 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1513 1525
1514 if (!((WL)w)->next) 1526 if (!((WL)w)->next)
1515 { 1527 {
1516#if WIN32 1528#if _WIN32
1517 signal (w->signum, sighandler); 1529 signal (w->signum, sighandler);
1518#else 1530#else
1519 struct sigaction sa; 1531 struct sigaction sa;
1520 sa.sa_handler = sighandler; 1532 sa.sa_handler = sighandler;
1521 sigfillset (&sa.sa_mask); 1533 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines