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.103 by root, Mon Nov 12 00:31:08 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
100# define EV_USE_EPOLL 0 107# define EV_USE_EPOLL 0
101#endif 108#endif
102 109
103#ifndef EV_USE_KQUEUE 110#ifndef EV_USE_KQUEUE
104# define EV_USE_KQUEUE 0 111# define EV_USE_KQUEUE 0
105#endif
106
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 112#endif
116 113
117#ifndef EV_USE_REALTIME 114#ifndef EV_USE_REALTIME
118# define EV_USE_REALTIME 1 115# define EV_USE_REALTIME 1
119#endif 116#endif
126#endif 123#endif
127 124
128#ifndef CLOCK_REALTIME 125#ifndef CLOCK_REALTIME
129# undef EV_USE_REALTIME 126# undef EV_USE_REALTIME
130# define EV_USE_REALTIME 0 127# define EV_USE_REALTIME 0
128#endif
129
130#if EV_SELECT_IS_WINSOCKET
131# include <winsock.h>
131#endif 132#endif
132 133
133/**/ 134/**/
134 135
135#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 136#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
155#define expect_true(expr) expect ((expr) != 0, 1) 156#define expect_true(expr) expect ((expr) != 0, 1)
156 157
157#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 158#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
158#define ABSPRI(w) ((w)->priority - EV_MINPRI) 159#define ABSPRI(w) ((w)->priority - EV_MINPRI)
159 160
161#define EMPTY /* required for microsofts broken pseudo-c compiler */
162
160typedef struct ev_watcher *W; 163typedef struct ev_watcher *W;
161typedef struct ev_watcher_list *WL; 164typedef struct ev_watcher_list *WL;
162typedef struct ev_watcher_time *WT; 165typedef struct ev_watcher_time *WT;
163 166
164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 167static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
165 168
166#ifdef WIN32 169#ifdef _WIN32
167# include "ev_win32.c" 170# include "ev_win32.c"
168#endif 171#endif
169 172
170/*****************************************************************************/ 173/*****************************************************************************/
171 174
220typedef struct 223typedef struct
221{ 224{
222 WL head; 225 WL head;
223 unsigned char events; 226 unsigned char events;
224 unsigned char reify; 227 unsigned char reify;
228#if EV_SELECT_IS_WINSOCKET
229 SOCKET handle;
230#endif
225} ANFD; 231} ANFD;
226 232
227typedef struct 233typedef struct
228{ 234{
229 W w; 235 W w;
318 stem ## max = array_roundsize (stem ## cnt >> 1); \ 324 stem ## max = array_roundsize (stem ## cnt >> 1); \
319 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 325 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
320 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 326 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
321 } 327 }
322 328
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) \ 329#define array_free(stem, idx) \
329 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 330 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
330 331
331/*****************************************************************************/ 332/*****************************************************************************/
332 333
406 int events = 0; 407 int events = 0;
407 408
408 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 409 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
409 events |= w->events; 410 events |= w->events;
410 411
412#if EV_SELECT_IS_WINSOCKET
413 if (events)
414 {
415 unsigned long argp;
416 anfd->handle = _get_osfhandle (fd);
417 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
418 }
419#endif
420
411 anfd->reify = 0; 421 anfd->reify = 0;
412 422
413 method_modify (EV_A_ fd, anfd->events, events); 423 method_modify (EV_A_ fd, anfd->events, events);
414 anfd->events = events; 424 anfd->events = events;
415 } 425 }
443} 453}
444 454
445static int 455static int
446fd_valid (int fd) 456fd_valid (int fd)
447{ 457{
448#ifdef WIN32 458#ifdef _WIN32
449 return !!win32_get_osfhandle (fd); 459 return _get_osfhandle (fd) != -1;
450#else 460#else
451 return fcntl (fd, F_GETFD) != -1; 461 return fcntl (fd, F_GETFD) != -1;
452#endif 462#endif
453} 463}
454 464
571} 581}
572 582
573static void 583static void
574sighandler (int signum) 584sighandler (int signum)
575{ 585{
576#if WIN32 586#if _WIN32
577 signal (signum, sighandler); 587 signal (signum, sighandler);
578#endif 588#endif
579 589
580 signals [signum - 1].gotsig = 1; 590 signals [signum - 1].gotsig = 1;
581 591
582 if (!gotsig) 592 if (!gotsig)
583 { 593 {
584 int old_errno = errno; 594 int old_errno = errno;
585 gotsig = 1; 595 gotsig = 1;
586#ifdef WIN32
587 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
588#else
589 write (sigpipe [1], &signum, 1); 596 write (sigpipe [1], &signum, 1);
590#endif
591 errno = old_errno; 597 errno = old_errno;
592 } 598 }
593} 599}
594 600
595void 601void
615static void 621static void
616sigcb (EV_P_ struct ev_io *iow, int revents) 622sigcb (EV_P_ struct ev_io *iow, int revents)
617{ 623{
618 int signum; 624 int signum;
619 625
620#ifdef WIN32
621 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
622#else
623 read (sigpipe [0], &revents, 1); 626 read (sigpipe [0], &revents, 1);
624#endif
625 gotsig = 0; 627 gotsig = 0;
626 628
627 for (signum = signalmax; signum--; ) 629 for (signum = signalmax; signum--; )
628 if (signals [signum].gotsig) 630 if (signals [signum].gotsig)
629 ev_feed_signal_event (EV_A_ signum + 1); 631 ev_feed_signal_event (EV_A_ signum + 1);
630} 632}
631 633
634inline void
635fd_intern (int fd)
636{
637#ifdef _WIN32
638 int arg = 1;
639 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
640#else
641 fcntl (fd, F_SETFD, FD_CLOEXEC);
642 fcntl (fd, F_SETFL, O_NONBLOCK);
643#endif
644}
645
632static void 646static void
633siginit (EV_P) 647siginit (EV_P)
634{ 648{
635#ifndef WIN32 649 fd_intern (sigpipe [0]);
636 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 650 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 651
644 ev_io_set (&sigev, sigpipe [0], EV_READ); 652 ev_io_set (&sigev, sigpipe [0], EV_READ);
645 ev_io_start (EV_A_ &sigev); 653 ev_io_start (EV_A_ &sigev);
646 ev_unref (EV_A); /* child watcher should not keep loop alive */ 654 ev_unref (EV_A); /* child watcher should not keep loop alive */
647} 655}
648 656
649/*****************************************************************************/ 657/*****************************************************************************/
650 658
651static struct ev_child *childs [PID_HASHSIZE]; 659static struct ev_child *childs [PID_HASHSIZE];
652 660
653#ifndef WIN32 661#ifndef _WIN32
654 662
655static struct ev_signal childev; 663static struct ev_signal childev;
656 664
657#ifndef WCONTINUED 665#ifndef WCONTINUED
658# define WCONTINUED 0 666# define WCONTINUED 0
719 727
720/* return true if we are running with elevated privileges and should ignore env variables */ 728/* return true if we are running with elevated privileges and should ignore env variables */
721static int 729static int
722enable_secure (void) 730enable_secure (void)
723{ 731{
724#ifdef WIN32 732#ifdef _WIN32
725 return 0; 733 return 0;
726#else 734#else
727 return getuid () != geteuid () 735 return getuid () != geteuid ()
728 || getgid () != getegid (); 736 || getgid () != getegid ();
729#endif 737#endif
758 methods = atoi (getenv ("LIBEV_METHODS")); 766 methods = atoi (getenv ("LIBEV_METHODS"));
759 else 767 else
760 methods = EVMETHOD_ANY; 768 methods = EVMETHOD_ANY;
761 769
762 method = 0; 770 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 771#if EV_USE_KQUEUE
767 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 772 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
768#endif 773#endif
769#if EV_USE_EPOLL 774#if EV_USE_EPOLL
770 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 775 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
784void 789void
785loop_destroy (EV_P) 790loop_destroy (EV_P)
786{ 791{
787 int i; 792 int i;
788 793
789#if EV_USE_WIN32
790 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
791#endif
792#if EV_USE_KQUEUE 794#if EV_USE_KQUEUE
793 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
794#endif 796#endif
795#if EV_USE_EPOLL 797#if EV_USE_EPOLL
796 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
804 806
805 for (i = NUMPRI; i--; ) 807 for (i = NUMPRI; i--; )
806 array_free (pending, [i]); 808 array_free (pending, [i]);
807 809
808 /* have to use the microsoft-never-gets-it-right macro */ 810 /* have to use the microsoft-never-gets-it-right macro */
809 array_free_microshit (fdchange); 811 array_free (fdchange, EMPTY);
810 array_free_microshit (timer); 812 array_free (timer, EMPTY);
811#if EV_PERIODICS 813#if EV_PERIODICS
812 array_free_microshit (periodic); 814 array_free (periodic, EMPTY);
813#endif 815#endif
814 array_free_microshit (idle); 816 array_free (idle, EMPTY);
815 array_free_microshit (prepare); 817 array_free (prepare, EMPTY);
816 array_free_microshit (check); 818 array_free (check, EMPTY);
817 819
818 method = 0; 820 method = 0;
819} 821}
820 822
821static void 823static void
900 902
901 if (ev_method (EV_A)) 903 if (ev_method (EV_A))
902 { 904 {
903 siginit (EV_A); 905 siginit (EV_A);
904 906
905#ifndef WIN32 907#ifndef _WIN32
906 ev_signal_init (&childev, childcb, SIGCHLD); 908 ev_signal_init (&childev, childcb, SIGCHLD);
907 ev_set_priority (&childev, EV_MAXPRI); 909 ev_set_priority (&childev, EV_MAXPRI);
908 ev_signal_start (EV_A_ &childev); 910 ev_signal_start (EV_A_ &childev);
909 ev_unref (EV_A); /* child watcher should not keep loop alive */ 911 ev_unref (EV_A); /* child watcher should not keep loop alive */
910#endif 912#endif
921{ 923{
922#if EV_MULTIPLICITY 924#if EV_MULTIPLICITY
923 struct ev_loop *loop = default_loop; 925 struct ev_loop *loop = default_loop;
924#endif 926#endif
925 927
926#ifndef WIN32 928#ifndef _WIN32
927 ev_ref (EV_A); /* child watcher */ 929 ev_ref (EV_A); /* child watcher */
928 ev_signal_stop (EV_A_ &childev); 930 ev_signal_stop (EV_A_ &childev);
929#endif 931#endif
930 932
931 ev_ref (EV_A); /* signal watcher */ 933 ev_ref (EV_A); /* signal watcher */
1511 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1513 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1512 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1514 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1513 1515
1514 if (!((WL)w)->next) 1516 if (!((WL)w)->next)
1515 { 1517 {
1516#if WIN32 1518#if _WIN32
1517 signal (w->signum, sighandler); 1519 signal (w->signum, sighandler);
1518#else 1520#else
1519 struct sigaction sa; 1521 struct sigaction sa;
1520 sa.sa_handler = sighandler; 1522 sa.sa_handler = sighandler;
1521 sigfillset (&sa.sa_mask); 1523 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines