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.106 by root, Mon Nov 12 01:07:50 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 cnanot 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;
233#if EV_MULTIPLICITY 250#if EV_MULTIPLICITY
234 251
235 struct ev_loop 252 struct ev_loop
236 { 253 {
237 ev_tstamp ev_rt_now; 254 ev_tstamp ev_rt_now;
255 #define ev_rt_now ((loop)->ev_rt_now)
238 #define VAR(name,decl) decl; 256 #define VAR(name,decl) decl;
239 #include "ev_vars.h" 257 #include "ev_vars.h"
240 #undef VAR 258 #undef VAR
241 }; 259 };
242 #include "ev_wrap.h" 260 #include "ev_wrap.h"
317 stem ## max = array_roundsize (stem ## cnt >> 1); \ 335 stem ## max = array_roundsize (stem ## cnt >> 1); \
318 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 336 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
319 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 337 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
320 } 338 }
321 339
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) \ 340#define array_free(stem, idx) \
328 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;
329 342
330/*****************************************************************************/ 343/*****************************************************************************/
331 344
405 int events = 0; 418 int events = 0;
406 419
407 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)
408 events |= w->events; 421 events |= w->events;
409 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
410 anfd->reify = 0; 432 anfd->reify = 0;
411 433
412 method_modify (EV_A_ fd, anfd->events, events); 434 method_modify (EV_A_ fd, anfd->events, events);
413 anfd->events = events; 435 anfd->events = events;
414 } 436 }
442} 464}
443 465
444static int 466static int
445fd_valid (int fd) 467fd_valid (int fd)
446{ 468{
447#ifdef WIN32 469#ifdef _WIN32
448 return !!win32_get_osfhandle (fd); 470 return _get_osfhandle (fd) != -1;
449#else 471#else
450 return fcntl (fd, F_GETFD) != -1; 472 return fcntl (fd, F_GETFD) != -1;
451#endif 473#endif
452} 474}
453 475
534 heap [k] = w; 556 heap [k] = w;
535 ((W)heap [k])->active = k + 1; 557 ((W)heap [k])->active = k + 1;
536} 558}
537 559
538inline void 560inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at) 561adjustheap (WT *heap, int N, int k)
540{ 562{
541 ev_tstamp old_at = heap [k]->at; 563 upheap (heap, k);
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k); 564 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548} 565}
549 566
550/*****************************************************************************/ 567/*****************************************************************************/
551 568
552typedef struct 569typedef struct
575} 592}
576 593
577static void 594static void
578sighandler (int signum) 595sighandler (int signum)
579{ 596{
580#if WIN32 597#if _WIN32
581 signal (signum, sighandler); 598 signal (signum, sighandler);
582#endif 599#endif
583 600
584 signals [signum - 1].gotsig = 1; 601 signals [signum - 1].gotsig = 1;
585 602
586 if (!gotsig) 603 if (!gotsig)
587 { 604 {
588 int old_errno = errno; 605 int old_errno = errno;
589 gotsig = 1; 606 gotsig = 1;
590#ifdef WIN32
591 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
592#else
593 write (sigpipe [1], &signum, 1); 607 write (sigpipe [1], &signum, 1);
594#endif
595 errno = old_errno; 608 errno = old_errno;
596 } 609 }
597} 610}
598 611
599void 612void
619static void 632static void
620sigcb (EV_P_ struct ev_io *iow, int revents) 633sigcb (EV_P_ struct ev_io *iow, int revents)
621{ 634{
622 int signum; 635 int signum;
623 636
624#ifdef WIN32
625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
626#else
627 read (sigpipe [0], &revents, 1); 637 read (sigpipe [0], &revents, 1);
628#endif
629 gotsig = 0; 638 gotsig = 0;
630 639
631 for (signum = signalmax; signum--; ) 640 for (signum = signalmax; signum--; )
632 if (signals [signum].gotsig) 641 if (signals [signum].gotsig)
633 ev_feed_signal_event (EV_A_ signum + 1); 642 ev_feed_signal_event (EV_A_ signum + 1);
634} 643}
635 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
636static void 657static void
637siginit (EV_P) 658siginit (EV_P)
638{ 659{
639#ifndef WIN32 660 fd_intern (sigpipe [0]);
640 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 661 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 662
648 ev_io_set (&sigev, sigpipe [0], EV_READ); 663 ev_io_set (&sigev, sigpipe [0], EV_READ);
649 ev_io_start (EV_A_ &sigev); 664 ev_io_start (EV_A_ &sigev);
650 ev_unref (EV_A); /* child watcher should not keep loop alive */ 665 ev_unref (EV_A); /* child watcher should not keep loop alive */
651} 666}
652 667
653/*****************************************************************************/ 668/*****************************************************************************/
654 669
655static struct ev_child *childs [PID_HASHSIZE]; 670static struct ev_child *childs [PID_HASHSIZE];
656 671
657#ifndef WIN32 672#ifndef _WIN32
658 673
659static struct ev_signal childev; 674static struct ev_signal childev;
660 675
661#ifndef WCONTINUED 676#ifndef WCONTINUED
662# define WCONTINUED 0 677# define WCONTINUED 0
723 738
724/* 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 */
725static int 740static int
726enable_secure (void) 741enable_secure (void)
727{ 742{
728#ifdef WIN32 743#ifdef _WIN32
729 return 0; 744 return 0;
730#else 745#else
731 return getuid () != geteuid () 746 return getuid () != geteuid ()
732 || getgid () != getegid (); 747 || getgid () != getegid ();
733#endif 748#endif
762 methods = atoi (getenv ("LIBEV_METHODS")); 777 methods = atoi (getenv ("LIBEV_METHODS"));
763 else 778 else
764 methods = EVMETHOD_ANY; 779 methods = EVMETHOD_ANY;
765 780
766 method = 0; 781 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 782#if EV_USE_KQUEUE
771 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 783 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
772#endif 784#endif
773#if EV_USE_EPOLL 785#if EV_USE_EPOLL
774 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 786 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
788void 800void
789loop_destroy (EV_P) 801loop_destroy (EV_P)
790{ 802{
791 int i; 803 int i;
792 804
793#if EV_USE_WIN32
794 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
795#endif
796#if EV_USE_KQUEUE 805#if EV_USE_KQUEUE
797 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
798#endif 807#endif
799#if EV_USE_EPOLL 808#if EV_USE_EPOLL
800 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
808 817
809 for (i = NUMPRI; i--; ) 818 for (i = NUMPRI; i--; )
810 array_free (pending, [i]); 819 array_free (pending, [i]);
811 820
812 /* have to use the microsoft-never-gets-it-right macro */ 821 /* have to use the microsoft-never-gets-it-right macro */
813 array_free_microshit (fdchange); 822 array_free (fdchange, EMPTY);
814 array_free_microshit (timer); 823 array_free (timer, EMPTY);
815#if EV_PERIODICS 824#if EV_PERIODICS
816 array_free_microshit (periodic); 825 array_free (periodic, EMPTY);
817#endif 826#endif
818 array_free_microshit (idle); 827 array_free (idle, EMPTY);
819 array_free_microshit (prepare); 828 array_free (prepare, EMPTY);
820 array_free_microshit (check); 829 array_free (check, EMPTY);
821 830
822 method = 0; 831 method = 0;
823} 832}
824 833
825static void 834static void
904 913
905 if (ev_method (EV_A)) 914 if (ev_method (EV_A))
906 { 915 {
907 siginit (EV_A); 916 siginit (EV_A);
908 917
909#ifndef WIN32 918#ifndef _WIN32
910 ev_signal_init (&childev, childcb, SIGCHLD); 919 ev_signal_init (&childev, childcb, SIGCHLD);
911 ev_set_priority (&childev, EV_MAXPRI); 920 ev_set_priority (&childev, EV_MAXPRI);
912 ev_signal_start (EV_A_ &childev); 921 ev_signal_start (EV_A_ &childev);
913 ev_unref (EV_A); /* child watcher should not keep loop alive */ 922 ev_unref (EV_A); /* child watcher should not keep loop alive */
914#endif 923#endif
925{ 934{
926#if EV_MULTIPLICITY 935#if EV_MULTIPLICITY
927 struct ev_loop *loop = default_loop; 936 struct ev_loop *loop = default_loop;
928#endif 937#endif
929 938
930#ifndef WIN32 939#ifndef _WIN32
931 ev_ref (EV_A); /* child watcher */ 940 ev_ref (EV_A); /* child watcher */
932 ev_signal_stop (EV_A_ &childev); 941 ev_signal_stop (EV_A_ &childev);
933#endif 942#endif
934 943
935 ev_ref (EV_A); /* signal watcher */ 944 ev_ref (EV_A); /* signal watcher */
1352 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1361 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1353 1362
1354 if (((W)w)->active < timercnt--) 1363 if (((W)w)->active < timercnt--)
1355 { 1364 {
1356 timers [((W)w)->active - 1] = timers [timercnt]; 1365 timers [((W)w)->active - 1] = timers [timercnt];
1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1366 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1358 } 1367 }
1359 1368
1360 ((WT)w)->at -= mn_now; 1369 ((WT)w)->at -= mn_now;
1361 1370
1362 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1366ev_timer_again (EV_P_ struct ev_timer *w) 1375ev_timer_again (EV_P_ struct ev_timer *w)
1367{ 1376{
1368 if (ev_is_active (w)) 1377 if (ev_is_active (w))
1369 { 1378 {
1370 if (w->repeat) 1379 if (w->repeat)
1380 {
1381 ((WT)w)->at = mn_now + w->repeat;
1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1382 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1383 }
1372 else 1384 else
1373 ev_timer_stop (EV_A_ w); 1385 ev_timer_stop (EV_A_ w);
1374 } 1386 }
1375 else if (w->repeat) 1387 else if (w->repeat)
1376 ev_timer_start (EV_A_ w); 1388 ev_timer_start (EV_A_ w);
1410 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1422 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1411 1423
1412 if (((W)w)->active < periodiccnt--) 1424 if (((W)w)->active < periodiccnt--)
1413 { 1425 {
1414 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1426 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1415 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1427 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1416 } 1428 }
1417 1429
1418 ev_stop (EV_A_ (W)w); 1430 ev_stop (EV_A_ (W)w);
1419} 1431}
1420 1432
1440 1452
1441void 1453void
1442ev_idle_stop (EV_P_ struct ev_idle *w) 1454ev_idle_stop (EV_P_ struct ev_idle *w)
1443{ 1455{
1444 ev_clear_pending (EV_A_ (W)w); 1456 ev_clear_pending (EV_A_ (W)w);
1445 if (ev_is_active (w)) 1457 if (!ev_is_active (w))
1446 return; 1458 return;
1447 1459
1448 idles [((W)w)->active - 1] = idles [--idlecnt]; 1460 idles [((W)w)->active - 1] = idles [--idlecnt];
1449 ev_stop (EV_A_ (W)w); 1461 ev_stop (EV_A_ (W)w);
1450} 1462}
1462 1474
1463void 1475void
1464ev_prepare_stop (EV_P_ struct ev_prepare *w) 1476ev_prepare_stop (EV_P_ struct ev_prepare *w)
1465{ 1477{
1466 ev_clear_pending (EV_A_ (W)w); 1478 ev_clear_pending (EV_A_ (W)w);
1467 if (ev_is_active (w)) 1479 if (!ev_is_active (w))
1468 return; 1480 return;
1469 1481
1470 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1482 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1471 ev_stop (EV_A_ (W)w); 1483 ev_stop (EV_A_ (W)w);
1472} 1484}
1512 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1524 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1513 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1525 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1514 1526
1515 if (!((WL)w)->next) 1527 if (!((WL)w)->next)
1516 { 1528 {
1517#if WIN32 1529#if _WIN32
1518 signal (w->signum, sighandler); 1530 signal (w->signum, sighandler);
1519#else 1531#else
1520 struct sigaction sa; 1532 struct sigaction sa;
1521 sa.sa_handler = sighandler; 1533 sa.sa_handler = sighandler;
1522 sigfillset (&sa.sa_mask); 1534 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines