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

Comparing libev/ev.c (file contents):
Revision 1.102 by root, Sun Nov 11 17:56:11 2007 UTC vs.
Revision 1.107 by root, Mon Nov 12 01:20:25 2007 UTC

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;
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
758 methods = atoi (getenv ("LIBEV_METHODS")); 777 methods = atoi (getenv ("LIBEV_METHODS"));
759 else 778 else
760 methods = EVMETHOD_ANY; 779 methods = EVMETHOD_ANY;
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 && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
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 && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
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
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 */
1511 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1524 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1512 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1525 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1513 1526
1514 if (!((WL)w)->next) 1527 if (!((WL)w)->next)
1515 { 1528 {
1516#if WIN32 1529#if _WIN32
1517 signal (w->signum, sighandler); 1530 signal (w->signum, sighandler);
1518#else 1531#else
1519 struct sigaction sa; 1532 struct sigaction sa;
1520 sa.sa_handler = sighandler; 1533 sa.sa_handler = sighandler;
1521 sigfillset (&sa.sa_mask); 1534 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines