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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines