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

Comparing libev/ev.c (file contents):
Revision 1.98 by root, Sun Nov 11 02:05:20 2007 UTC vs.
Revision 1.103 by root, Mon Nov 12 00:31:08 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
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;
233#if EV_MULTIPLICITY 239#if EV_MULTIPLICITY
234 240
235 struct ev_loop 241 struct ev_loop
236 { 242 {
237 ev_tstamp ev_rt_now; 243 ev_tstamp ev_rt_now;
244 #define ev_rt_now ((loop)->ev_rt_now)
238 #define VAR(name,decl) decl; 245 #define VAR(name,decl) decl;
239 #include "ev_vars.h" 246 #include "ev_vars.h"
240 #undef VAR 247 #undef VAR
241 }; 248 };
242 #include "ev_wrap.h" 249 #include "ev_wrap.h"
317 stem ## max = array_roundsize (stem ## cnt >> 1); \ 324 stem ## max = array_roundsize (stem ## cnt >> 1); \
318 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 325 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
319 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 326 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
320 } 327 }
321 328
322/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
323/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
324#define array_free_microshit(stem) \
325 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
326
327#define array_free(stem, idx) \ 329#define array_free(stem, idx) \
328 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;
329 331
330/*****************************************************************************/ 332/*****************************************************************************/
331 333
405 int events = 0; 407 int events = 0;
406 408
407 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)
408 events |= w->events; 410 events |= w->events;
409 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
410 anfd->reify = 0; 421 anfd->reify = 0;
411 422
412 method_modify (EV_A_ fd, anfd->events, events); 423 method_modify (EV_A_ fd, anfd->events, events);
413 anfd->events = events; 424 anfd->events = events;
414 } 425 }
442} 453}
443 454
444static int 455static int
445fd_valid (int fd) 456fd_valid (int fd)
446{ 457{
447#ifdef WIN32 458#ifdef _WIN32
448 return !!win32_get_osfhandle (fd); 459 return _get_osfhandle (fd) != -1;
449#else 460#else
450 return fcntl (fd, F_GETFD) != -1; 461 return fcntl (fd, F_GETFD) != -1;
451#endif 462#endif
452} 463}
453 464
534 heap [k] = w; 545 heap [k] = w;
535 ((W)heap [k])->active = k + 1; 546 ((W)heap [k])->active = k + 1;
536} 547}
537 548
538inline void 549inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at) 550adjustheap (WT *heap, int N, int k)
540{ 551{
541 ev_tstamp old_at = heap [k]->at; 552 upheap (heap, k);
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k); 553 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548} 554}
549 555
550/*****************************************************************************/ 556/*****************************************************************************/
551 557
552typedef struct 558typedef struct
575} 581}
576 582
577static void 583static void
578sighandler (int signum) 584sighandler (int signum)
579{ 585{
580#if WIN32 586#if _WIN32
581 signal (signum, sighandler); 587 signal (signum, sighandler);
582#endif 588#endif
583 589
584 signals [signum - 1].gotsig = 1; 590 signals [signum - 1].gotsig = 1;
585 591
586 if (!gotsig) 592 if (!gotsig)
587 { 593 {
588 int old_errno = errno; 594 int old_errno = errno;
589 gotsig = 1; 595 gotsig = 1;
590#ifdef WIN32
591 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
592#else
593 write (sigpipe [1], &signum, 1); 596 write (sigpipe [1], &signum, 1);
594#endif
595 errno = old_errno; 597 errno = old_errno;
596 } 598 }
597} 599}
598 600
599void 601void
619static void 621static void
620sigcb (EV_P_ struct ev_io *iow, int revents) 622sigcb (EV_P_ struct ev_io *iow, int revents)
621{ 623{
622 int signum; 624 int signum;
623 625
624#ifdef WIN32
625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
626#else
627 read (sigpipe [0], &revents, 1); 626 read (sigpipe [0], &revents, 1);
628#endif
629 gotsig = 0; 627 gotsig = 0;
630 628
631 for (signum = signalmax; signum--; ) 629 for (signum = signalmax; signum--; )
632 if (signals [signum].gotsig) 630 if (signals [signum].gotsig)
633 ev_feed_signal_event (EV_A_ signum + 1); 631 ev_feed_signal_event (EV_A_ signum + 1);
634} 632}
635 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
636static void 646static void
637siginit (EV_P) 647siginit (EV_P)
638{ 648{
639#ifndef WIN32 649 fd_intern (sigpipe [0]);
640 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 650 fd_intern (sigpipe [1]);
641 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
642
643 /* rather than sort out wether we really need nb, set it */
644 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
645 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
646#endif
647 651
648 ev_io_set (&sigev, sigpipe [0], EV_READ); 652 ev_io_set (&sigev, sigpipe [0], EV_READ);
649 ev_io_start (EV_A_ &sigev); 653 ev_io_start (EV_A_ &sigev);
650 ev_unref (EV_A); /* child watcher should not keep loop alive */ 654 ev_unref (EV_A); /* child watcher should not keep loop alive */
651} 655}
652 656
653/*****************************************************************************/ 657/*****************************************************************************/
654 658
655static struct ev_child *childs [PID_HASHSIZE]; 659static struct ev_child *childs [PID_HASHSIZE];
656 660
657#ifndef WIN32 661#ifndef _WIN32
658 662
659static struct ev_signal childev; 663static struct ev_signal childev;
660 664
661#ifndef WCONTINUED 665#ifndef WCONTINUED
662# define WCONTINUED 0 666# define WCONTINUED 0
723 727
724/* 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 */
725static int 729static int
726enable_secure (void) 730enable_secure (void)
727{ 731{
728#ifdef WIN32 732#ifdef _WIN32
729 return 0; 733 return 0;
730#else 734#else
731 return getuid () != geteuid () 735 return getuid () != geteuid ()
732 || getgid () != getegid (); 736 || getgid () != getegid ();
733#endif 737#endif
762 methods = atoi (getenv ("LIBEV_METHODS")); 766 methods = atoi (getenv ("LIBEV_METHODS"));
763 else 767 else
764 methods = EVMETHOD_ANY; 768 methods = EVMETHOD_ANY;
765 769
766 method = 0; 770 method = 0;
767#if EV_USE_WIN32
768 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
769#endif
770#if EV_USE_KQUEUE 771#if EV_USE_KQUEUE
771 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 772 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
772#endif 773#endif
773#if EV_USE_EPOLL 774#if EV_USE_EPOLL
774 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 775 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
788void 789void
789loop_destroy (EV_P) 790loop_destroy (EV_P)
790{ 791{
791 int i; 792 int i;
792 793
793#if EV_USE_WIN32
794 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
795#endif
796#if EV_USE_KQUEUE 794#if EV_USE_KQUEUE
797 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
798#endif 796#endif
799#if EV_USE_EPOLL 797#if EV_USE_EPOLL
800 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
808 806
809 for (i = NUMPRI; i--; ) 807 for (i = NUMPRI; i--; )
810 array_free (pending, [i]); 808 array_free (pending, [i]);
811 809
812 /* have to use the microsoft-never-gets-it-right macro */ 810 /* have to use the microsoft-never-gets-it-right macro */
813 array_free_microshit (fdchange); 811 array_free (fdchange, EMPTY);
814 array_free_microshit (timer); 812 array_free (timer, EMPTY);
815#if EV_PERIODICS 813#if EV_PERIODICS
816 array_free_microshit (periodic); 814 array_free (periodic, EMPTY);
817#endif 815#endif
818 array_free_microshit (idle); 816 array_free (idle, EMPTY);
819 array_free_microshit (prepare); 817 array_free (prepare, EMPTY);
820 array_free_microshit (check); 818 array_free (check, EMPTY);
821 819
822 method = 0; 820 method = 0;
823} 821}
824 822
825static void 823static void
904 902
905 if (ev_method (EV_A)) 903 if (ev_method (EV_A))
906 { 904 {
907 siginit (EV_A); 905 siginit (EV_A);
908 906
909#ifndef WIN32 907#ifndef _WIN32
910 ev_signal_init (&childev, childcb, SIGCHLD); 908 ev_signal_init (&childev, childcb, SIGCHLD);
911 ev_set_priority (&childev, EV_MAXPRI); 909 ev_set_priority (&childev, EV_MAXPRI);
912 ev_signal_start (EV_A_ &childev); 910 ev_signal_start (EV_A_ &childev);
913 ev_unref (EV_A); /* child watcher should not keep loop alive */ 911 ev_unref (EV_A); /* child watcher should not keep loop alive */
914#endif 912#endif
925{ 923{
926#if EV_MULTIPLICITY 924#if EV_MULTIPLICITY
927 struct ev_loop *loop = default_loop; 925 struct ev_loop *loop = default_loop;
928#endif 926#endif
929 927
930#ifndef WIN32 928#ifndef _WIN32
931 ev_ref (EV_A); /* child watcher */ 929 ev_ref (EV_A); /* child watcher */
932 ev_signal_stop (EV_A_ &childev); 930 ev_signal_stop (EV_A_ &childev);
933#endif 931#endif
934 932
935 ev_ref (EV_A); /* signal watcher */ 933 ev_ref (EV_A); /* signal watcher */
1352 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1353 1351
1354 if (((W)w)->active < timercnt--) 1352 if (((W)w)->active < timercnt--)
1355 { 1353 {
1356 timers [((W)w)->active - 1] = timers [timercnt]; 1354 timers [((W)w)->active - 1] = timers [timercnt];
1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1355 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1358 } 1356 }
1359 1357
1360 ((WT)w)->at -= mn_now; 1358 ((WT)w)->at -= mn_now;
1361 1359
1362 ev_stop (EV_A_ (W)w); 1360 ev_stop (EV_A_ (W)w);
1366ev_timer_again (EV_P_ struct ev_timer *w) 1364ev_timer_again (EV_P_ struct ev_timer *w)
1367{ 1365{
1368 if (ev_is_active (w)) 1366 if (ev_is_active (w))
1369 { 1367 {
1370 if (w->repeat) 1368 if (w->repeat)
1369 {
1370 ((WT)w)->at = mn_now + w->repeat;
1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1372 }
1372 else 1373 else
1373 ev_timer_stop (EV_A_ w); 1374 ev_timer_stop (EV_A_ w);
1374 } 1375 }
1375 else if (w->repeat) 1376 else if (w->repeat)
1376 ev_timer_start (EV_A_ w); 1377 ev_timer_start (EV_A_ w);
1410 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1411 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1411 1412
1412 if (((W)w)->active < periodiccnt--) 1413 if (((W)w)->active < periodiccnt--)
1413 { 1414 {
1414 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1415 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1415 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1416 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1416 } 1417 }
1417 1418
1418 ev_stop (EV_A_ (W)w); 1419 ev_stop (EV_A_ (W)w);
1419} 1420}
1420 1421
1440 1441
1441void 1442void
1442ev_idle_stop (EV_P_ struct ev_idle *w) 1443ev_idle_stop (EV_P_ struct ev_idle *w)
1443{ 1444{
1444 ev_clear_pending (EV_A_ (W)w); 1445 ev_clear_pending (EV_A_ (W)w);
1445 if (ev_is_active (w)) 1446 if (!ev_is_active (w))
1446 return; 1447 return;
1447 1448
1448 idles [((W)w)->active - 1] = idles [--idlecnt]; 1449 idles [((W)w)->active - 1] = idles [--idlecnt];
1449 ev_stop (EV_A_ (W)w); 1450 ev_stop (EV_A_ (W)w);
1450} 1451}
1462 1463
1463void 1464void
1464ev_prepare_stop (EV_P_ struct ev_prepare *w) 1465ev_prepare_stop (EV_P_ struct ev_prepare *w)
1465{ 1466{
1466 ev_clear_pending (EV_A_ (W)w); 1467 ev_clear_pending (EV_A_ (W)w);
1467 if (ev_is_active (w)) 1468 if (!ev_is_active (w))
1468 return; 1469 return;
1469 1470
1470 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1471 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1471 ev_stop (EV_A_ (W)w); 1472 ev_stop (EV_A_ (W)w);
1472} 1473}
1512 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1513 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1513 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1514 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1514 1515
1515 if (!((WL)w)->next) 1516 if (!((WL)w)->next)
1516 { 1517 {
1517#if WIN32 1518#if _WIN32
1518 signal (w->signum, sighandler); 1519 signal (w->signum, sighandler);
1519#else 1520#else
1520 struct sigaction sa; 1521 struct sigaction sa;
1521 sa.sa_handler = sighandler; 1522 sa.sa_handler = sighandler;
1522 sigfillset (&sa.sa_mask); 1523 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines