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.105 by root, Mon Nov 12 01:02:09 2007 UTC

47 47
48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
49# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
50# endif 50# endif
51 51
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) && !defined (__APPLE__)
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) && !defined (__APPLE__)
61# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
62# endif 62# endif
63 63
64#endif 64#endif
65 65
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
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 117#endif
116 118
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
126#endif 128#endif
127 129
128#ifndef CLOCK_REALTIME 130#ifndef CLOCK_REALTIME
129# undef EV_USE_REALTIME 131# undef EV_USE_REALTIME
130# define EV_USE_REALTIME 0 132# define EV_USE_REALTIME 0
133#endif
134
135#if EV_SELECT_IS_WINSOCKET
136# include <winsock.h>
131#endif 137#endif
132 138
133/**/ 139/**/
134 140
135#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 141#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
155#define expect_true(expr) expect ((expr) != 0, 1) 161#define expect_true(expr) expect ((expr) != 0, 1)
156 162
157#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 163#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
158#define ABSPRI(w) ((w)->priority - EV_MINPRI) 164#define ABSPRI(w) ((w)->priority - EV_MINPRI)
159 165
166#define EMPTY /* required for microsofts broken pseudo-c compiler */
167
160typedef struct ev_watcher *W; 168typedef struct ev_watcher *W;
161typedef struct ev_watcher_list *WL; 169typedef struct ev_watcher_list *WL;
162typedef struct ev_watcher_time *WT; 170typedef struct ev_watcher_time *WT;
163 171
164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 172static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
165 173
166#ifdef WIN32 174#ifdef _WIN32
167# include "ev_win32.c" 175# include "ev_win32.c"
168#endif 176#endif
169 177
170/*****************************************************************************/ 178/*****************************************************************************/
171 179
220typedef struct 228typedef struct
221{ 229{
222 WL head; 230 WL head;
223 unsigned char events; 231 unsigned char events;
224 unsigned char reify; 232 unsigned char reify;
233#if EV_SELECT_IS_WINSOCKET
234 SOCKET handle;
235#endif
225} ANFD; 236} ANFD;
226 237
227typedef struct 238typedef struct
228{ 239{
229 W w; 240 W w;
233#if EV_MULTIPLICITY 244#if EV_MULTIPLICITY
234 245
235 struct ev_loop 246 struct ev_loop
236 { 247 {
237 ev_tstamp ev_rt_now; 248 ev_tstamp ev_rt_now;
249 #define ev_rt_now ((loop)->ev_rt_now)
238 #define VAR(name,decl) decl; 250 #define VAR(name,decl) decl;
239 #include "ev_vars.h" 251 #include "ev_vars.h"
240 #undef VAR 252 #undef VAR
241 }; 253 };
242 #include "ev_wrap.h" 254 #include "ev_wrap.h"
317 stem ## max = array_roundsize (stem ## cnt >> 1); \ 329 stem ## max = array_roundsize (stem ## cnt >> 1); \
318 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 330 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
319 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 331 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
320 } 332 }
321 333
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) \ 334#define array_free(stem, idx) \
328 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 335 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
329 336
330/*****************************************************************************/ 337/*****************************************************************************/
331 338
405 int events = 0; 412 int events = 0;
406 413
407 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 414 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
408 events |= w->events; 415 events |= w->events;
409 416
417#if EV_SELECT_IS_WINSOCKET
418 if (events)
419 {
420 unsigned long argp;
421 anfd->handle = _get_osfhandle (fd);
422 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
423 }
424#endif
425
410 anfd->reify = 0; 426 anfd->reify = 0;
411 427
412 method_modify (EV_A_ fd, anfd->events, events); 428 method_modify (EV_A_ fd, anfd->events, events);
413 anfd->events = events; 429 anfd->events = events;
414 } 430 }
442} 458}
443 459
444static int 460static int
445fd_valid (int fd) 461fd_valid (int fd)
446{ 462{
447#ifdef WIN32 463#ifdef _WIN32
448 return !!win32_get_osfhandle (fd); 464 return _get_osfhandle (fd) != -1;
449#else 465#else
450 return fcntl (fd, F_GETFD) != -1; 466 return fcntl (fd, F_GETFD) != -1;
451#endif 467#endif
452} 468}
453 469
534 heap [k] = w; 550 heap [k] = w;
535 ((W)heap [k])->active = k + 1; 551 ((W)heap [k])->active = k + 1;
536} 552}
537 553
538inline void 554inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at) 555adjustheap (WT *heap, int N, int k)
540{ 556{
541 ev_tstamp old_at = heap [k]->at; 557 upheap (heap, k);
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k); 558 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548} 559}
549 560
550/*****************************************************************************/ 561/*****************************************************************************/
551 562
552typedef struct 563typedef struct
575} 586}
576 587
577static void 588static void
578sighandler (int signum) 589sighandler (int signum)
579{ 590{
580#if WIN32 591#if _WIN32
581 signal (signum, sighandler); 592 signal (signum, sighandler);
582#endif 593#endif
583 594
584 signals [signum - 1].gotsig = 1; 595 signals [signum - 1].gotsig = 1;
585 596
586 if (!gotsig) 597 if (!gotsig)
587 { 598 {
588 int old_errno = errno; 599 int old_errno = errno;
589 gotsig = 1; 600 gotsig = 1;
590#ifdef WIN32
591 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
592#else
593 write (sigpipe [1], &signum, 1); 601 write (sigpipe [1], &signum, 1);
594#endif
595 errno = old_errno; 602 errno = old_errno;
596 } 603 }
597} 604}
598 605
599void 606void
619static void 626static void
620sigcb (EV_P_ struct ev_io *iow, int revents) 627sigcb (EV_P_ struct ev_io *iow, int revents)
621{ 628{
622 int signum; 629 int signum;
623 630
624#ifdef WIN32
625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
626#else
627 read (sigpipe [0], &revents, 1); 631 read (sigpipe [0], &revents, 1);
628#endif
629 gotsig = 0; 632 gotsig = 0;
630 633
631 for (signum = signalmax; signum--; ) 634 for (signum = signalmax; signum--; )
632 if (signals [signum].gotsig) 635 if (signals [signum].gotsig)
633 ev_feed_signal_event (EV_A_ signum + 1); 636 ev_feed_signal_event (EV_A_ signum + 1);
634} 637}
635 638
639inline void
640fd_intern (int fd)
641{
642#ifdef _WIN32
643 int arg = 1;
644 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
645#else
646 fcntl (fd, F_SETFD, FD_CLOEXEC);
647 fcntl (fd, F_SETFL, O_NONBLOCK);
648#endif
649}
650
636static void 651static void
637siginit (EV_P) 652siginit (EV_P)
638{ 653{
639#ifndef WIN32 654 fd_intern (sigpipe [0]);
640 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 655 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 656
648 ev_io_set (&sigev, sigpipe [0], EV_READ); 657 ev_io_set (&sigev, sigpipe [0], EV_READ);
649 ev_io_start (EV_A_ &sigev); 658 ev_io_start (EV_A_ &sigev);
650 ev_unref (EV_A); /* child watcher should not keep loop alive */ 659 ev_unref (EV_A); /* child watcher should not keep loop alive */
651} 660}
652 661
653/*****************************************************************************/ 662/*****************************************************************************/
654 663
655static struct ev_child *childs [PID_HASHSIZE]; 664static struct ev_child *childs [PID_HASHSIZE];
656 665
657#ifndef WIN32 666#ifndef _WIN32
658 667
659static struct ev_signal childev; 668static struct ev_signal childev;
660 669
661#ifndef WCONTINUED 670#ifndef WCONTINUED
662# define WCONTINUED 0 671# define WCONTINUED 0
723 732
724/* return true if we are running with elevated privileges and should ignore env variables */ 733/* return true if we are running with elevated privileges and should ignore env variables */
725static int 734static int
726enable_secure (void) 735enable_secure (void)
727{ 736{
728#ifdef WIN32 737#ifdef _WIN32
729 return 0; 738 return 0;
730#else 739#else
731 return getuid () != geteuid () 740 return getuid () != geteuid ()
732 || getgid () != getegid (); 741 || getgid () != getegid ();
733#endif 742#endif
762 methods = atoi (getenv ("LIBEV_METHODS")); 771 methods = atoi (getenv ("LIBEV_METHODS"));
763 else 772 else
764 methods = EVMETHOD_ANY; 773 methods = EVMETHOD_ANY;
765 774
766 method = 0; 775 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 776#if EV_USE_KQUEUE
771 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 777 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
772#endif 778#endif
773#if EV_USE_EPOLL 779#if EV_USE_EPOLL
774 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 780 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
788void 794void
789loop_destroy (EV_P) 795loop_destroy (EV_P)
790{ 796{
791 int i; 797 int i;
792 798
793#if EV_USE_WIN32
794 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
795#endif
796#if EV_USE_KQUEUE 799#if EV_USE_KQUEUE
797 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 800 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
798#endif 801#endif
799#if EV_USE_EPOLL 802#if EV_USE_EPOLL
800 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 803 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
808 811
809 for (i = NUMPRI; i--; ) 812 for (i = NUMPRI; i--; )
810 array_free (pending, [i]); 813 array_free (pending, [i]);
811 814
812 /* have to use the microsoft-never-gets-it-right macro */ 815 /* have to use the microsoft-never-gets-it-right macro */
813 array_free_microshit (fdchange); 816 array_free (fdchange, EMPTY);
814 array_free_microshit (timer); 817 array_free (timer, EMPTY);
815#if EV_PERIODICS 818#if EV_PERIODICS
816 array_free_microshit (periodic); 819 array_free (periodic, EMPTY);
817#endif 820#endif
818 array_free_microshit (idle); 821 array_free (idle, EMPTY);
819 array_free_microshit (prepare); 822 array_free (prepare, EMPTY);
820 array_free_microshit (check); 823 array_free (check, EMPTY);
821 824
822 method = 0; 825 method = 0;
823} 826}
824 827
825static void 828static void
904 907
905 if (ev_method (EV_A)) 908 if (ev_method (EV_A))
906 { 909 {
907 siginit (EV_A); 910 siginit (EV_A);
908 911
909#ifndef WIN32 912#ifndef _WIN32
910 ev_signal_init (&childev, childcb, SIGCHLD); 913 ev_signal_init (&childev, childcb, SIGCHLD);
911 ev_set_priority (&childev, EV_MAXPRI); 914 ev_set_priority (&childev, EV_MAXPRI);
912 ev_signal_start (EV_A_ &childev); 915 ev_signal_start (EV_A_ &childev);
913 ev_unref (EV_A); /* child watcher should not keep loop alive */ 916 ev_unref (EV_A); /* child watcher should not keep loop alive */
914#endif 917#endif
925{ 928{
926#if EV_MULTIPLICITY 929#if EV_MULTIPLICITY
927 struct ev_loop *loop = default_loop; 930 struct ev_loop *loop = default_loop;
928#endif 931#endif
929 932
930#ifndef WIN32 933#ifndef _WIN32
931 ev_ref (EV_A); /* child watcher */ 934 ev_ref (EV_A); /* child watcher */
932 ev_signal_stop (EV_A_ &childev); 935 ev_signal_stop (EV_A_ &childev);
933#endif 936#endif
934 937
935 ev_ref (EV_A); /* signal watcher */ 938 ev_ref (EV_A); /* signal watcher */
1352 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1355 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1353 1356
1354 if (((W)w)->active < timercnt--) 1357 if (((W)w)->active < timercnt--)
1355 { 1358 {
1356 timers [((W)w)->active - 1] = timers [timercnt]; 1359 timers [((W)w)->active - 1] = timers [timercnt];
1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1360 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1358 } 1361 }
1359 1362
1360 ((WT)w)->at -= mn_now; 1363 ((WT)w)->at -= mn_now;
1361 1364
1362 ev_stop (EV_A_ (W)w); 1365 ev_stop (EV_A_ (W)w);
1366ev_timer_again (EV_P_ struct ev_timer *w) 1369ev_timer_again (EV_P_ struct ev_timer *w)
1367{ 1370{
1368 if (ev_is_active (w)) 1371 if (ev_is_active (w))
1369 { 1372 {
1370 if (w->repeat) 1373 if (w->repeat)
1374 {
1375 ((WT)w)->at = mn_now + w->repeat;
1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1376 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1377 }
1372 else 1378 else
1373 ev_timer_stop (EV_A_ w); 1379 ev_timer_stop (EV_A_ w);
1374 } 1380 }
1375 else if (w->repeat) 1381 else if (w->repeat)
1376 ev_timer_start (EV_A_ w); 1382 ev_timer_start (EV_A_ w);
1410 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1416 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1411 1417
1412 if (((W)w)->active < periodiccnt--) 1418 if (((W)w)->active < periodiccnt--)
1413 { 1419 {
1414 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1420 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1415 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1421 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1416 } 1422 }
1417 1423
1418 ev_stop (EV_A_ (W)w); 1424 ev_stop (EV_A_ (W)w);
1419} 1425}
1420 1426
1440 1446
1441void 1447void
1442ev_idle_stop (EV_P_ struct ev_idle *w) 1448ev_idle_stop (EV_P_ struct ev_idle *w)
1443{ 1449{
1444 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1445 if (ev_is_active (w)) 1451 if (!ev_is_active (w))
1446 return; 1452 return;
1447 1453
1448 idles [((W)w)->active - 1] = idles [--idlecnt]; 1454 idles [((W)w)->active - 1] = idles [--idlecnt];
1449 ev_stop (EV_A_ (W)w); 1455 ev_stop (EV_A_ (W)w);
1450} 1456}
1462 1468
1463void 1469void
1464ev_prepare_stop (EV_P_ struct ev_prepare *w) 1470ev_prepare_stop (EV_P_ struct ev_prepare *w)
1465{ 1471{
1466 ev_clear_pending (EV_A_ (W)w); 1472 ev_clear_pending (EV_A_ (W)w);
1467 if (ev_is_active (w)) 1473 if (!ev_is_active (w))
1468 return; 1474 return;
1469 1475
1470 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1476 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1471 ev_stop (EV_A_ (W)w); 1477 ev_stop (EV_A_ (W)w);
1472} 1478}
1512 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1518 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1513 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1519 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1514 1520
1515 if (!((WL)w)->next) 1521 if (!((WL)w)->next)
1516 { 1522 {
1517#if WIN32 1523#if _WIN32
1518 signal (w->signum, sighandler); 1524 signal (w->signum, sighandler);
1519#else 1525#else
1520 struct sigaction sa; 1526 struct sigaction sa;
1521 sa.sa_handler = sighandler; 1527 sa.sa_handler = sighandler;
1522 sigfillset (&sa.sa_mask); 1528 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines