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.104 by root, Mon Nov 12 00:39:45 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
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
174#ifdef _WIN32
166#include "ev_win32.c" 175# include "ev_win32.c"
176#endif
167 177
168/*****************************************************************************/ 178/*****************************************************************************/
169 179
170static void (*syserr_cb)(const char *msg); 180static void (*syserr_cb)(const char *msg);
171 181
218typedef struct 228typedef struct
219{ 229{
220 WL head; 230 WL head;
221 unsigned char events; 231 unsigned char events;
222 unsigned char reify; 232 unsigned char reify;
233#if EV_SELECT_IS_WINSOCKET
234 SOCKET handle;
235#endif
223} ANFD; 236} ANFD;
224 237
225typedef struct 238typedef struct
226{ 239{
227 W w; 240 W w;
231#if EV_MULTIPLICITY 244#if EV_MULTIPLICITY
232 245
233 struct ev_loop 246 struct ev_loop
234 { 247 {
235 ev_tstamp ev_rt_now; 248 ev_tstamp ev_rt_now;
249 #define ev_rt_now ((loop)->ev_rt_now)
236 #define VAR(name,decl) decl; 250 #define VAR(name,decl) decl;
237 #include "ev_vars.h" 251 #include "ev_vars.h"
238 #undef VAR 252 #undef VAR
239 }; 253 };
240 #include "ev_wrap.h" 254 #include "ev_wrap.h"
315 stem ## max = array_roundsize (stem ## cnt >> 1); \ 329 stem ## max = array_roundsize (stem ## cnt >> 1); \
316 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 330 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
317 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 331 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
318 } 332 }
319 333
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) \ 334#define array_free(stem, idx) \
326 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;
327 336
328/*****************************************************************************/ 337/*****************************************************************************/
329 338
403 int events = 0; 412 int events = 0;
404 413
405 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)
406 events |= w->events; 415 events |= w->events;
407 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
408 anfd->reify = 0; 426 anfd->reify = 0;
409 427
410 method_modify (EV_A_ fd, anfd->events, events); 428 method_modify (EV_A_ fd, anfd->events, events);
411 anfd->events = events; 429 anfd->events = events;
412 } 430 }
440} 458}
441 459
442static int 460static int
443fd_valid (int fd) 461fd_valid (int fd)
444{ 462{
445#ifdef WIN32 463#ifdef _WIN32
446 return !!win32_get_osfhandle (fd); 464 return _get_osfhandle (fd) != -1;
447#else 465#else
448 return fcntl (fd, F_GETFD) != -1; 466 return fcntl (fd, F_GETFD) != -1;
449#endif 467#endif
450} 468}
451 469
532 heap [k] = w; 550 heap [k] = w;
533 ((W)heap [k])->active = k + 1; 551 ((W)heap [k])->active = k + 1;
534} 552}
535 553
536inline void 554inline void
537adjustheap (WT *heap, int N, int k, ev_tstamp at) 555adjustheap (WT *heap, int N, int k)
538{ 556{
539 ev_tstamp old_at = heap [k]->at; 557 upheap (heap, k);
540 heap [k]->at = at;
541
542 if (old_at < at)
543 downheap (heap, N, k); 558 downheap (heap, N, k);
544 else
545 upheap (heap, k);
546} 559}
547 560
548/*****************************************************************************/ 561/*****************************************************************************/
549 562
550typedef struct 563typedef struct
573} 586}
574 587
575static void 588static void
576sighandler (int signum) 589sighandler (int signum)
577{ 590{
578#if WIN32 591#if _WIN32
579 signal (signum, sighandler); 592 signal (signum, sighandler);
580#endif 593#endif
581 594
582 signals [signum - 1].gotsig = 1; 595 signals [signum - 1].gotsig = 1;
583 596
584 if (!gotsig) 597 if (!gotsig)
585 { 598 {
586 int old_errno = errno; 599 int old_errno = errno;
587 gotsig = 1; 600 gotsig = 1;
588#ifdef WIN32
589 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
590#else
591 write (sigpipe [1], &signum, 1); 601 write (sigpipe [1], &signum, 1);
592#endif
593 errno = old_errno; 602 errno = old_errno;
594 } 603 }
595} 604}
596 605
597void 606void
617static void 626static void
618sigcb (EV_P_ struct ev_io *iow, int revents) 627sigcb (EV_P_ struct ev_io *iow, int revents)
619{ 628{
620 int signum; 629 int signum;
621 630
622#ifdef WIN32
623 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
624#else
625 read (sigpipe [0], &revents, 1); 631 read (sigpipe [0], &revents, 1);
626#endif
627 gotsig = 0; 632 gotsig = 0;
628 633
629 for (signum = signalmax; signum--; ) 634 for (signum = signalmax; signum--; )
630 if (signals [signum].gotsig) 635 if (signals [signum].gotsig)
631 ev_feed_signal_event (EV_A_ signum + 1); 636 ev_feed_signal_event (EV_A_ signum + 1);
632} 637}
633 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
634static void 651static void
635siginit (EV_P) 652siginit (EV_P)
636{ 653{
637#ifndef WIN32 654 fd_intern (sigpipe [0]);
638 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 655 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 656
646 ev_io_set (&sigev, sigpipe [0], EV_READ); 657 ev_io_set (&sigev, sigpipe [0], EV_READ);
647 ev_io_start (EV_A_ &sigev); 658 ev_io_start (EV_A_ &sigev);
648 ev_unref (EV_A); /* child watcher should not keep loop alive */ 659 ev_unref (EV_A); /* child watcher should not keep loop alive */
649} 660}
650 661
651/*****************************************************************************/ 662/*****************************************************************************/
652 663
653static struct ev_child *childs [PID_HASHSIZE]; 664static struct ev_child *childs [PID_HASHSIZE];
654 665
655#ifndef WIN32 666#ifndef _WIN32
656 667
657static struct ev_signal childev; 668static struct ev_signal childev;
658 669
659#ifndef WCONTINUED 670#ifndef WCONTINUED
660# define WCONTINUED 0 671# define WCONTINUED 0
721 732
722/* 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 */
723static int 734static int
724enable_secure (void) 735enable_secure (void)
725{ 736{
726#ifdef WIN32 737#ifdef _WIN32
727 return 0; 738 return 0;
728#else 739#else
729 return getuid () != geteuid () 740 return getuid () != geteuid ()
730 || getgid () != getegid (); 741 || getgid () != getegid ();
731#endif 742#endif
760 methods = atoi (getenv ("LIBEV_METHODS")); 771 methods = atoi (getenv ("LIBEV_METHODS"));
761 else 772 else
762 methods = EVMETHOD_ANY; 773 methods = EVMETHOD_ANY;
763 774
764 method = 0; 775 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 776#if EV_USE_KQUEUE
769 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 777 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
770#endif 778#endif
771#if EV_USE_EPOLL 779#if EV_USE_EPOLL
772 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 780 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
786void 794void
787loop_destroy (EV_P) 795loop_destroy (EV_P)
788{ 796{
789 int i; 797 int i;
790 798
791#if EV_USE_WIN32
792 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
793#endif
794#if EV_USE_KQUEUE 799#if EV_USE_KQUEUE
795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 800 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
796#endif 801#endif
797#if EV_USE_EPOLL 802#if EV_USE_EPOLL
798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 803 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
806 811
807 for (i = NUMPRI; i--; ) 812 for (i = NUMPRI; i--; )
808 array_free (pending, [i]); 813 array_free (pending, [i]);
809 814
810 /* have to use the microsoft-never-gets-it-right macro */ 815 /* have to use the microsoft-never-gets-it-right macro */
811 array_free_microshit (fdchange); 816 array_free (fdchange, EMPTY);
812 array_free_microshit (timer); 817 array_free (timer, EMPTY);
813#if EV_PERIODICS 818#if EV_PERIODICS
814 array_free_microshit (periodic); 819 array_free (periodic, EMPTY);
815#endif 820#endif
816 array_free_microshit (idle); 821 array_free (idle, EMPTY);
817 array_free_microshit (prepare); 822 array_free (prepare, EMPTY);
818 array_free_microshit (check); 823 array_free (check, EMPTY);
819 824
820 method = 0; 825 method = 0;
821} 826}
822 827
823static void 828static void
902 907
903 if (ev_method (EV_A)) 908 if (ev_method (EV_A))
904 { 909 {
905 siginit (EV_A); 910 siginit (EV_A);
906 911
907#ifndef WIN32 912#ifndef _WIN32
908 ev_signal_init (&childev, childcb, SIGCHLD); 913 ev_signal_init (&childev, childcb, SIGCHLD);
909 ev_set_priority (&childev, EV_MAXPRI); 914 ev_set_priority (&childev, EV_MAXPRI);
910 ev_signal_start (EV_A_ &childev); 915 ev_signal_start (EV_A_ &childev);
911 ev_unref (EV_A); /* child watcher should not keep loop alive */ 916 ev_unref (EV_A); /* child watcher should not keep loop alive */
912#endif 917#endif
923{ 928{
924#if EV_MULTIPLICITY 929#if EV_MULTIPLICITY
925 struct ev_loop *loop = default_loop; 930 struct ev_loop *loop = default_loop;
926#endif 931#endif
927 932
928#ifndef WIN32 933#ifndef _WIN32
929 ev_ref (EV_A); /* child watcher */ 934 ev_ref (EV_A); /* child watcher */
930 ev_signal_stop (EV_A_ &childev); 935 ev_signal_stop (EV_A_ &childev);
931#endif 936#endif
932 937
933 ev_ref (EV_A); /* signal watcher */ 938 ev_ref (EV_A); /* signal watcher */
1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1355 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1351 1356
1352 if (((W)w)->active < timercnt--) 1357 if (((W)w)->active < timercnt--)
1353 { 1358 {
1354 timers [((W)w)->active - 1] = timers [timercnt]; 1359 timers [((W)w)->active - 1] = timers [timercnt];
1355 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1360 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1356 } 1361 }
1357 1362
1358 ((WT)w)->at -= mn_now; 1363 ((WT)w)->at -= mn_now;
1359 1364
1360 ev_stop (EV_A_ (W)w); 1365 ev_stop (EV_A_ (W)w);
1364ev_timer_again (EV_P_ struct ev_timer *w) 1369ev_timer_again (EV_P_ struct ev_timer *w)
1365{ 1370{
1366 if (ev_is_active (w)) 1371 if (ev_is_active (w))
1367 { 1372 {
1368 if (w->repeat) 1373 if (w->repeat)
1374 {
1375 ((WT)w)->at = mn_now + w->repeat;
1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1376 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1377 }
1370 else 1378 else
1371 ev_timer_stop (EV_A_ w); 1379 ev_timer_stop (EV_A_ w);
1372 } 1380 }
1373 else if (w->repeat) 1381 else if (w->repeat)
1374 ev_timer_start (EV_A_ w); 1382 ev_timer_start (EV_A_ w);
1408 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1416 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1409 1417
1410 if (((W)w)->active < periodiccnt--) 1418 if (((W)w)->active < periodiccnt--)
1411 { 1419 {
1412 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1420 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1413 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1421 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1414 } 1422 }
1415 1423
1416 ev_stop (EV_A_ (W)w); 1424 ev_stop (EV_A_ (W)w);
1417} 1425}
1418 1426
1438 1446
1439void 1447void
1440ev_idle_stop (EV_P_ struct ev_idle *w) 1448ev_idle_stop (EV_P_ struct ev_idle *w)
1441{ 1449{
1442 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1443 if (ev_is_active (w)) 1451 if (!ev_is_active (w))
1444 return; 1452 return;
1445 1453
1446 idles [((W)w)->active - 1] = idles [--idlecnt]; 1454 idles [((W)w)->active - 1] = idles [--idlecnt];
1447 ev_stop (EV_A_ (W)w); 1455 ev_stop (EV_A_ (W)w);
1448} 1456}
1460 1468
1461void 1469void
1462ev_prepare_stop (EV_P_ struct ev_prepare *w) 1470ev_prepare_stop (EV_P_ struct ev_prepare *w)
1463{ 1471{
1464 ev_clear_pending (EV_A_ (W)w); 1472 ev_clear_pending (EV_A_ (W)w);
1465 if (ev_is_active (w)) 1473 if (!ev_is_active (w))
1466 return; 1474 return;
1467 1475
1468 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1476 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1469 ev_stop (EV_A_ (W)w); 1477 ev_stop (EV_A_ (W)w);
1470} 1478}
1510 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1518 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1511 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1519 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1512 1520
1513 if (!((WL)w)->next) 1521 if (!((WL)w)->next)
1514 { 1522 {
1515#if WIN32 1523#if _WIN32
1516 signal (w->signum, sighandler); 1524 signal (w->signum, sighandler);
1517#else 1525#else
1518 struct sigaction sa; 1526 struct sigaction sa;
1519 sa.sa_handler = sighandler; 1527 sa.sa_handler = sighandler;
1520 sigfillset (&sa.sa_mask); 1528 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines