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

Comparing libev/ev.c (file contents):
Revision 1.283 by root, Wed Apr 15 09:51:19 2009 UTC vs.
Revision 1.313 by root, Wed Aug 19 23:44:51 2009 UTC

57# endif 57# endif
58# ifndef EV_USE_MONOTONIC 58# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 59# define EV_USE_MONOTONIC 1
60# endif 60# endif
61# endif 61# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL)
63# define EV_USE_CLOCK_SYSCALL 0
62# endif 64# endif
63 65
64# if HAVE_CLOCK_GETTIME 66# if HAVE_CLOCK_GETTIME
65# ifndef EV_USE_MONOTONIC 67# ifndef EV_USE_MONOTONIC
66# define EV_USE_MONOTONIC 1 68# define EV_USE_MONOTONIC 1
131# else 133# else
132# define EV_USE_INOTIFY 0 134# define EV_USE_INOTIFY 0
133# endif 135# endif
134# endif 136# endif
135 137
138# ifndef EV_USE_SIGNALFD
139# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
140# define EV_USE_SIGNALFD 1
141# else
142# define EV_USE_SIGNALFD 0
143# endif
144# endif
145
136# ifndef EV_USE_EVENTFD 146# ifndef EV_USE_EVENTFD
137# if HAVE_EVENTFD 147# if HAVE_EVENTFD
138# define EV_USE_EVENTFD 1 148# define EV_USE_EVENTFD 1
139# else 149# else
140# define EV_USE_EVENTFD 0 150# define EV_USE_EVENTFD 0
176# endif 186# endif
177#endif 187#endif
178 188
179/* this block tries to deduce configuration from header-defined symbols and defaults */ 189/* this block tries to deduce configuration from header-defined symbols and defaults */
180 190
191/* try to deduce the maximum number of signals on this platform */
192#if defined (EV_NSIG)
193/* use what's provided */
194#elif defined (NSIG)
195# define EV_NSIG (NSIG)
196#elif defined(_NSIG)
197# define EV_NSIG (_NSIG)
198#elif defined (SIGMAX)
199# define EV_NSIG (SIGMAX+1)
200#elif defined (SIG_MAX)
201# define EV_NSIG (SIG_MAX+1)
202#elif defined (_SIG_MAX)
203# define EV_NSIG (_SIG_MAX+1)
204#elif defined (MAXSIG)
205# define EV_NSIG (MAXSIG+1)
206#elif defined (MAX_SIG)
207# define EV_NSIG (MAX_SIG+1)
208#elif defined (SIGARRAYSIZE)
209# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */
210#elif defined (_sys_nsig)
211# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
212#else
213# error "unable to find value for NSIG, please report"
214/* to make it compile regardless, just remove the above line */
215# define EV_NSIG 65
216#endif
217
181#ifndef EV_USE_CLOCK_SYSCALL 218#ifndef EV_USE_CLOCK_SYSCALL
182# if __linux && __GLIBC__ >= 2 219# if __linux && __GLIBC__ >= 2
183# define EV_USE_CLOCK_SYSCALL 1 220# define EV_USE_CLOCK_SYSCALL 1
184# else 221# else
185# define EV_USE_CLOCK_SYSCALL 0 222# define EV_USE_CLOCK_SYSCALL 0
264# else 301# else
265# define EV_USE_EVENTFD 0 302# define EV_USE_EVENTFD 0
266# endif 303# endif
267#endif 304#endif
268 305
306#ifndef EV_USE_SIGNALFD
307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9))
308# define EV_USE_SIGNALFD 1
309# else
310# define EV_USE_SIGNALFD 0
311# endif
312#endif
313
269#if 0 /* debugging */ 314#if 0 /* debugging */
270# define EV_VERIFY 3 315# define EV_VERIFY 3
271# define EV_USE_4HEAP 1 316# define EV_USE_4HEAP 1
272# define EV_HEAP_CACHE_AT 1 317# define EV_HEAP_CACHE_AT 1
273#endif 318#endif
280# define EV_USE_4HEAP !EV_MINIMAL 325# define EV_USE_4HEAP !EV_MINIMAL
281#endif 326#endif
282 327
283#ifndef EV_HEAP_CACHE_AT 328#ifndef EV_HEAP_CACHE_AT
284# define EV_HEAP_CACHE_AT !EV_MINIMAL 329# define EV_HEAP_CACHE_AT !EV_MINIMAL
330#endif
331
332/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
333/* which makes programs even slower. might work on other unices, too. */
334#if EV_USE_CLOCK_SYSCALL
335# include <syscall.h>
336# ifdef SYS_clock_gettime
337# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
338# undef EV_USE_MONOTONIC
339# define EV_USE_MONOTONIC 1
340# else
341# undef EV_USE_CLOCK_SYSCALL
342# define EV_USE_CLOCK_SYSCALL 0
343# endif
285#endif 344#endif
286 345
287/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 346/* this block fixes any misconfiguration where we know we run into trouble otherwise */
288 347
289#ifndef CLOCK_MONOTONIC 348#ifndef CLOCK_MONOTONIC
320 379
321#if EV_SELECT_IS_WINSOCKET 380#if EV_SELECT_IS_WINSOCKET
322# include <winsock.h> 381# include <winsock.h>
323#endif 382#endif
324 383
325/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
326/* which makes programs even slower. might work on other unices, too. */
327#if EV_USE_CLOCK_SYSCALL
328# include <syscall.h>
329# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
330# undef EV_USE_MONOTONIC
331# define EV_USE_MONOTONIC 1
332#endif
333
334#if EV_USE_EVENTFD 384#if EV_USE_EVENTFD
335/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 385/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
336# include <stdint.h> 386# include <stdint.h>
387# ifndef EFD_NONBLOCK
388# define EFD_NONBLOCK O_NONBLOCK
389# endif
390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
396# endif
337# ifdef __cplusplus 397# ifdef __cplusplus
338extern "C" { 398extern "C" {
339# endif 399# endif
340int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
341# ifdef __cplusplus 401# ifdef __cplusplus
342} 402}
343# endif 403# endif
404#endif
405
406#if EV_USE_SIGNALFD
407# include <sys/signalfd.h>
344#endif 408#endif
345 409
346/**/ 410/**/
347 411
348#if EV_VERIFY >= 3 412#if EV_VERIFY >= 3
384# define inline_speed static noinline 448# define inline_speed static noinline
385#else 449#else
386# define inline_speed static inline 450# define inline_speed static inline
387#endif 451#endif
388 452
389#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 453#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
454
455#if EV_MINPRI == EV_MAXPRI
456# define ABSPRI(w) (((W)w), 0)
457#else
390#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 458# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
459#endif
391 460
392#define EMPTY /* required for microsofts broken pseudo-c compiler */ 461#define EMPTY /* required for microsofts broken pseudo-c compiler */
393#define EMPTY2(a,b) /* used to suppress some warnings */ 462#define EMPTY2(a,b) /* used to suppress some warnings */
394 463
395typedef ev_watcher *W; 464typedef ev_watcher *W;
407 476
408#if EV_USE_MONOTONIC 477#if EV_USE_MONOTONIC
409static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 478static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
410#endif 479#endif
411 480
481#ifndef EV_FD_TO_WIN32_HANDLE
482# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
483#endif
484#ifndef EV_WIN32_HANDLE_TO_FD
485# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0)
486#endif
487#ifndef EV_WIN32_CLOSE_FD
488# define EV_WIN32_CLOSE_FD(fd) close (fd)
489#endif
490
412#ifdef _WIN32 491#ifdef _WIN32
413# include "ev_win32.c" 492# include "ev_win32.c"
414#endif 493#endif
415 494
416/*****************************************************************************/ 495/*****************************************************************************/
478#define ev_malloc(size) ev_realloc (0, (size)) 557#define ev_malloc(size) ev_realloc (0, (size))
479#define ev_free(ptr) ev_realloc ((ptr), 0) 558#define ev_free(ptr) ev_realloc ((ptr), 0)
480 559
481/*****************************************************************************/ 560/*****************************************************************************/
482 561
562/* set in reify when reification needed */
563#define EV_ANFD_REIFY 1
564
565/* file descriptor info structure */
483typedef struct 566typedef struct
484{ 567{
485 WL head; 568 WL head;
486 unsigned char events; 569 unsigned char events; /* the events watched for */
487 unsigned char reify; 570 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
488 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 571 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
489 unsigned char unused; 572 unsigned char unused;
490#if EV_USE_EPOLL 573#if EV_USE_EPOLL
491 unsigned int egen; /* generation counter to counter epoll bugs */ 574 unsigned int egen; /* generation counter to counter epoll bugs */
492#endif 575#endif
493#if EV_SELECT_IS_WINSOCKET 576#if EV_SELECT_IS_WINSOCKET
494 SOCKET handle; 577 SOCKET handle;
495#endif 578#endif
496} ANFD; 579} ANFD;
497 580
581/* stores the pending event set for a given watcher */
498typedef struct 582typedef struct
499{ 583{
500 W w; 584 W w;
501 int events; 585 int events; /* the pending event set for the given watcher */
502} ANPENDING; 586} ANPENDING;
503 587
504#if EV_USE_INOTIFY 588#if EV_USE_INOTIFY
505/* hash table entry per inotify-id */ 589/* hash table entry per inotify-id */
506typedef struct 590typedef struct
509} ANFS; 593} ANFS;
510#endif 594#endif
511 595
512/* Heap Entry */ 596/* Heap Entry */
513#if EV_HEAP_CACHE_AT 597#if EV_HEAP_CACHE_AT
598 /* a heap element */
514 typedef struct { 599 typedef struct {
515 ev_tstamp at; 600 ev_tstamp at;
516 WT w; 601 WT w;
517 } ANHE; 602 } ANHE;
518 603
519 #define ANHE_w(he) (he).w /* access watcher, read-write */ 604 #define ANHE_w(he) (he).w /* access watcher, read-write */
520 #define ANHE_at(he) (he).at /* access cached at, read-only */ 605 #define ANHE_at(he) (he).at /* access cached at, read-only */
521 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ 606 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
522#else 607#else
608 /* a heap element */
523 typedef WT ANHE; 609 typedef WT ANHE;
524 610
525 #define ANHE_w(he) (he) 611 #define ANHE_w(he) (he)
526 #define ANHE_at(he) (he)->at 612 #define ANHE_at(he) (he)->at
527 #define ANHE_at_cache(he) 613 #define ANHE_at_cache(he)
551 637
552 static int ev_default_loop_ptr; 638 static int ev_default_loop_ptr;
553 639
554#endif 640#endif
555 641
642#if EV_MINIMAL < 2
643# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
644# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
645# define EV_INVOKE_PENDING invoke_cb (EV_A)
646#else
647# define EV_RELEASE_CB (void)0
648# define EV_ACQUIRE_CB (void)0
649# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
650#endif
651
652#define EVUNLOOP_RECURSE 0x80
653
556/*****************************************************************************/ 654/*****************************************************************************/
557 655
656#ifndef EV_HAVE_EV_TIME
558ev_tstamp 657ev_tstamp
559ev_time (void) 658ev_time (void)
560{ 659{
561#if EV_USE_REALTIME 660#if EV_USE_REALTIME
562 if (expect_true (have_realtime)) 661 if (expect_true (have_realtime))
569 668
570 struct timeval tv; 669 struct timeval tv;
571 gettimeofday (&tv, 0); 670 gettimeofday (&tv, 0);
572 return tv.tv_sec + tv.tv_usec * 1e-6; 671 return tv.tv_sec + tv.tv_usec * 1e-6;
573} 672}
673#endif
574 674
575ev_tstamp inline_size 675inline_size ev_tstamp
576get_clock (void) 676get_clock (void)
577{ 677{
578#if EV_USE_MONOTONIC 678#if EV_USE_MONOTONIC
579 if (expect_true (have_monotonic)) 679 if (expect_true (have_monotonic))
580 { 680 {
614 714
615 tv.tv_sec = (time_t)delay; 715 tv.tv_sec = (time_t)delay;
616 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 716 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
617 717
618 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 718 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
619 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 719 /* something not guaranteed by newer posix versions, but guaranteed */
620 /* by older ones */ 720 /* by older ones */
621 select (0, 0, 0, 0, &tv); 721 select (0, 0, 0, 0, &tv);
622#endif 722#endif
623 } 723 }
624} 724}
625 725
626/*****************************************************************************/ 726/*****************************************************************************/
627 727
628#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 728#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
629 729
630int inline_size 730/* find a suitable new size for the given array, */
731/* hopefully by rounding to a ncie-to-malloc size */
732inline_size int
631array_nextsize (int elem, int cur, int cnt) 733array_nextsize (int elem, int cur, int cnt)
632{ 734{
633 int ncur = cur + 1; 735 int ncur = cur + 1;
634 736
635 do 737 do
680#define array_free(stem, idx) \ 782#define array_free(stem, idx) \
681 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 783 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
682 784
683/*****************************************************************************/ 785/*****************************************************************************/
684 786
787/* dummy callback for pending events */
788static void noinline
789pendingcb (EV_P_ ev_prepare *w, int revents)
790{
791}
792
685void noinline 793void noinline
686ev_feed_event (EV_P_ void *w, int revents) 794ev_feed_event (EV_P_ void *w, int revents)
687{ 795{
688 W w_ = (W)w; 796 W w_ = (W)w;
689 int pri = ABSPRI (w_); 797 int pri = ABSPRI (w_);
697 pendings [pri][w_->pending - 1].w = w_; 805 pendings [pri][w_->pending - 1].w = w_;
698 pendings [pri][w_->pending - 1].events = revents; 806 pendings [pri][w_->pending - 1].events = revents;
699 } 807 }
700} 808}
701 809
702void inline_speed 810inline_speed void
811feed_reverse (EV_P_ W w)
812{
813 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
814 rfeeds [rfeedcnt++] = w;
815}
816
817inline_size void
818feed_reverse_done (EV_P_ int revents)
819{
820 do
821 ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
822 while (rfeedcnt);
823}
824
825inline_speed void
703queue_events (EV_P_ W *events, int eventcnt, int type) 826queue_events (EV_P_ W *events, int eventcnt, int type)
704{ 827{
705 int i; 828 int i;
706 829
707 for (i = 0; i < eventcnt; ++i) 830 for (i = 0; i < eventcnt; ++i)
708 ev_feed_event (EV_A_ events [i], type); 831 ev_feed_event (EV_A_ events [i], type);
709} 832}
710 833
711/*****************************************************************************/ 834/*****************************************************************************/
712 835
713void inline_speed 836inline_speed void
714fd_event (EV_P_ int fd, int revents) 837fd_event_nc (EV_P_ int fd, int revents)
715{ 838{
716 ANFD *anfd = anfds + fd; 839 ANFD *anfd = anfds + fd;
717 ev_io *w; 840 ev_io *w;
718 841
719 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 842 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
723 if (ev) 846 if (ev)
724 ev_feed_event (EV_A_ (W)w, ev); 847 ev_feed_event (EV_A_ (W)w, ev);
725 } 848 }
726} 849}
727 850
851/* do not submit kernel events for fds that have reify set */
852/* because that means they changed while we were polling for new events */
853inline_speed void
854fd_event (EV_P_ int fd, int revents)
855{
856 ANFD *anfd = anfds + fd;
857
858 if (expect_true (!anfd->reify))
859 fd_event_nc (EV_A_ fd, revents);
860}
861
728void 862void
729ev_feed_fd_event (EV_P_ int fd, int revents) 863ev_feed_fd_event (EV_P_ int fd, int revents)
730{ 864{
731 if (fd >= 0 && fd < anfdmax) 865 if (fd >= 0 && fd < anfdmax)
732 fd_event (EV_A_ fd, revents); 866 fd_event_nc (EV_A_ fd, revents);
733} 867}
734 868
735void inline_size 869/* make sure the external fd watch events are in-sync */
870/* with the kernel/libev internal state */
871inline_size void
736fd_reify (EV_P) 872fd_reify (EV_P)
737{ 873{
738 int i; 874 int i;
739 875
740 for (i = 0; i < fdchangecnt; ++i) 876 for (i = 0; i < fdchangecnt; ++i)
750 886
751#if EV_SELECT_IS_WINSOCKET 887#if EV_SELECT_IS_WINSOCKET
752 if (events) 888 if (events)
753 { 889 {
754 unsigned long arg; 890 unsigned long arg;
755 #ifdef EV_FD_TO_WIN32_HANDLE
756 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 891 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
757 #else
758 anfd->handle = _get_osfhandle (fd);
759 #endif
760 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 892 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
761 } 893 }
762#endif 894#endif
763 895
764 { 896 {
774 } 906 }
775 907
776 fdchangecnt = 0; 908 fdchangecnt = 0;
777} 909}
778 910
779void inline_size 911/* something about the given fd changed */
912inline_size void
780fd_change (EV_P_ int fd, int flags) 913fd_change (EV_P_ int fd, int flags)
781{ 914{
782 unsigned char reify = anfds [fd].reify; 915 unsigned char reify = anfds [fd].reify;
783 anfds [fd].reify |= flags; 916 anfds [fd].reify |= flags;
784 917
788 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 921 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
789 fdchanges [fdchangecnt - 1] = fd; 922 fdchanges [fdchangecnt - 1] = fd;
790 } 923 }
791} 924}
792 925
793void inline_speed 926/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
927inline_speed void
794fd_kill (EV_P_ int fd) 928fd_kill (EV_P_ int fd)
795{ 929{
796 ev_io *w; 930 ev_io *w;
797 931
798 while ((w = (ev_io *)anfds [fd].head)) 932 while ((w = (ev_io *)anfds [fd].head))
800 ev_io_stop (EV_A_ w); 934 ev_io_stop (EV_A_ w);
801 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 935 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
802 } 936 }
803} 937}
804 938
805int inline_size 939/* check whether the given fd is atcually valid, for error recovery */
940inline_size int
806fd_valid (int fd) 941fd_valid (int fd)
807{ 942{
808#ifdef _WIN32 943#ifdef _WIN32
809 return _get_osfhandle (fd) != -1; 944 return _get_osfhandle (fd) != -1;
810#else 945#else
832 967
833 for (fd = anfdmax; fd--; ) 968 for (fd = anfdmax; fd--; )
834 if (anfds [fd].events) 969 if (anfds [fd].events)
835 { 970 {
836 fd_kill (EV_A_ fd); 971 fd_kill (EV_A_ fd);
837 return; 972 break;
838 } 973 }
839} 974}
840 975
841/* usually called after fork if backend needs to re-arm all fds from scratch */ 976/* usually called after fork if backend needs to re-arm all fds from scratch */
842static void noinline 977static void noinline
847 for (fd = 0; fd < anfdmax; ++fd) 982 for (fd = 0; fd < anfdmax; ++fd)
848 if (anfds [fd].events) 983 if (anfds [fd].events)
849 { 984 {
850 anfds [fd].events = 0; 985 anfds [fd].events = 0;
851 anfds [fd].emask = 0; 986 anfds [fd].emask = 0;
852 fd_change (EV_A_ fd, EV__IOFDSET | 1); 987 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
853 } 988 }
854} 989}
855 990
856/*****************************************************************************/ 991/*****************************************************************************/
857 992
873#define HEAP0 (DHEAP - 1) /* index of first element in heap */ 1008#define HEAP0 (DHEAP - 1) /* index of first element in heap */
874#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) 1009#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
875#define UPHEAP_DONE(p,k) ((p) == (k)) 1010#define UPHEAP_DONE(p,k) ((p) == (k))
876 1011
877/* away from the root */ 1012/* away from the root */
878void inline_speed 1013inline_speed void
879downheap (ANHE *heap, int N, int k) 1014downheap (ANHE *heap, int N, int k)
880{ 1015{
881 ANHE he = heap [k]; 1016 ANHE he = heap [k];
882 ANHE *E = heap + N + HEAP0; 1017 ANHE *E = heap + N + HEAP0;
883 1018
923#define HEAP0 1 1058#define HEAP0 1
924#define HPARENT(k) ((k) >> 1) 1059#define HPARENT(k) ((k) >> 1)
925#define UPHEAP_DONE(p,k) (!(p)) 1060#define UPHEAP_DONE(p,k) (!(p))
926 1061
927/* away from the root */ 1062/* away from the root */
928void inline_speed 1063inline_speed void
929downheap (ANHE *heap, int N, int k) 1064downheap (ANHE *heap, int N, int k)
930{ 1065{
931 ANHE he = heap [k]; 1066 ANHE he = heap [k];
932 1067
933 for (;;) 1068 for (;;)
934 { 1069 {
935 int c = k << 1; 1070 int c = k << 1;
936 1071
937 if (c > N + HEAP0 - 1) 1072 if (c >= N + HEAP0)
938 break; 1073 break;
939 1074
940 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) 1075 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
941 ? 1 : 0; 1076 ? 1 : 0;
942 1077
953 ev_active (ANHE_w (he)) = k; 1088 ev_active (ANHE_w (he)) = k;
954} 1089}
955#endif 1090#endif
956 1091
957/* towards the root */ 1092/* towards the root */
958void inline_speed 1093inline_speed void
959upheap (ANHE *heap, int k) 1094upheap (ANHE *heap, int k)
960{ 1095{
961 ANHE he = heap [k]; 1096 ANHE he = heap [k];
962 1097
963 for (;;) 1098 for (;;)
974 1109
975 heap [k] = he; 1110 heap [k] = he;
976 ev_active (ANHE_w (he)) = k; 1111 ev_active (ANHE_w (he)) = k;
977} 1112}
978 1113
979void inline_size 1114/* move an element suitably so it is in a correct place */
1115inline_size void
980adjustheap (ANHE *heap, int N, int k) 1116adjustheap (ANHE *heap, int N, int k)
981{ 1117{
982 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1118 if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
983 upheap (heap, k); 1119 upheap (heap, k);
984 else 1120 else
985 downheap (heap, N, k); 1121 downheap (heap, N, k);
986} 1122}
987 1123
988/* rebuild the heap: this function is used only once and executed rarely */ 1124/* rebuild the heap: this function is used only once and executed rarely */
989void inline_size 1125inline_size void
990reheap (ANHE *heap, int N) 1126reheap (ANHE *heap, int N)
991{ 1127{
992 int i; 1128 int i;
993 1129
994 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 1130 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
997 upheap (heap, i + HEAP0); 1133 upheap (heap, i + HEAP0);
998} 1134}
999 1135
1000/*****************************************************************************/ 1136/*****************************************************************************/
1001 1137
1138/* associate signal watchers to a signal signal */
1002typedef struct 1139typedef struct
1003{ 1140{
1141 EV_ATOMIC_T pending;
1142#if EV_MULTIPLICITY
1143 EV_P;
1144#endif
1004 WL head; 1145 WL head;
1005 EV_ATOMIC_T gotsig;
1006} ANSIG; 1146} ANSIG;
1007 1147
1008static ANSIG *signals; 1148static ANSIG signals [EV_NSIG - 1];
1009static int signalmax;
1010
1011static EV_ATOMIC_T gotsig;
1012 1149
1013/*****************************************************************************/ 1150/*****************************************************************************/
1014 1151
1015void inline_speed 1152/* used to prepare libev internal fd's */
1153/* this is not fork-safe */
1154inline_speed void
1016fd_intern (int fd) 1155fd_intern (int fd)
1017{ 1156{
1018#ifdef _WIN32 1157#ifdef _WIN32
1019 unsigned long arg = 1; 1158 unsigned long arg = 1;
1020 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 1159 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
1025} 1164}
1026 1165
1027static void noinline 1166static void noinline
1028evpipe_init (EV_P) 1167evpipe_init (EV_P)
1029{ 1168{
1030 if (!ev_is_active (&pipeev)) 1169 if (!ev_is_active (&pipe_w))
1031 { 1170 {
1032#if EV_USE_EVENTFD 1171#if EV_USE_EVENTFD
1172 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1173 if (evfd < 0 && errno == EINVAL)
1033 if ((evfd = eventfd (0, 0)) >= 0) 1174 evfd = eventfd (0, 0);
1175
1176 if (evfd >= 0)
1034 { 1177 {
1035 evpipe [0] = -1; 1178 evpipe [0] = -1;
1036 fd_intern (evfd); 1179 fd_intern (evfd); /* doing it twice doesn't hurt */
1037 ev_io_set (&pipeev, evfd, EV_READ); 1180 ev_io_set (&pipe_w, evfd, EV_READ);
1038 } 1181 }
1039 else 1182 else
1040#endif 1183#endif
1041 { 1184 {
1042 while (pipe (evpipe)) 1185 while (pipe (evpipe))
1043 ev_syserr ("(libev) error creating signal/async pipe"); 1186 ev_syserr ("(libev) error creating signal/async pipe");
1044 1187
1045 fd_intern (evpipe [0]); 1188 fd_intern (evpipe [0]);
1046 fd_intern (evpipe [1]); 1189 fd_intern (evpipe [1]);
1047 ev_io_set (&pipeev, evpipe [0], EV_READ); 1190 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1048 } 1191 }
1049 1192
1050 ev_io_start (EV_A_ &pipeev); 1193 ev_io_start (EV_A_ &pipe_w);
1051 ev_unref (EV_A); /* watcher should not keep loop alive */ 1194 ev_unref (EV_A); /* watcher should not keep loop alive */
1052 } 1195 }
1053} 1196}
1054 1197
1055void inline_size 1198inline_size void
1056evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1199evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1057{ 1200{
1058 if (!*flag) 1201 if (!*flag)
1059 { 1202 {
1060 int old_errno = errno; /* save errno because write might clobber it */ 1203 int old_errno = errno; /* save errno because write might clobber it */
1073 1216
1074 errno = old_errno; 1217 errno = old_errno;
1075 } 1218 }
1076} 1219}
1077 1220
1221/* called whenever the libev signal pipe */
1222/* got some events (signal, async) */
1078static void 1223static void
1079pipecb (EV_P_ ev_io *iow, int revents) 1224pipecb (EV_P_ ev_io *iow, int revents)
1080{ 1225{
1226 int i;
1227
1081#if EV_USE_EVENTFD 1228#if EV_USE_EVENTFD
1082 if (evfd >= 0) 1229 if (evfd >= 0)
1083 { 1230 {
1084 uint64_t counter; 1231 uint64_t counter;
1085 read (evfd, &counter, sizeof (uint64_t)); 1232 read (evfd, &counter, sizeof (uint64_t));
1089 { 1236 {
1090 char dummy; 1237 char dummy;
1091 read (evpipe [0], &dummy, 1); 1238 read (evpipe [0], &dummy, 1);
1092 } 1239 }
1093 1240
1094 if (gotsig && ev_is_default_loop (EV_A)) 1241 if (sig_pending)
1095 { 1242 {
1096 int signum; 1243 sig_pending = 0;
1097 gotsig = 0;
1098 1244
1099 for (signum = signalmax; signum--; ) 1245 for (i = EV_NSIG - 1; i--; )
1100 if (signals [signum].gotsig) 1246 if (expect_false (signals [i].pending))
1101 ev_feed_signal_event (EV_A_ signum + 1); 1247 ev_feed_signal_event (EV_A_ i + 1);
1102 } 1248 }
1103 1249
1104#if EV_ASYNC_ENABLE 1250#if EV_ASYNC_ENABLE
1105 if (gotasync) 1251 if (async_pending)
1106 { 1252 {
1107 int i; 1253 async_pending = 0;
1108 gotasync = 0;
1109 1254
1110 for (i = asynccnt; i--; ) 1255 for (i = asynccnt; i--; )
1111 if (asyncs [i]->sent) 1256 if (asyncs [i]->sent)
1112 { 1257 {
1113 asyncs [i]->sent = 0; 1258 asyncs [i]->sent = 0;
1121 1266
1122static void 1267static void
1123ev_sighandler (int signum) 1268ev_sighandler (int signum)
1124{ 1269{
1125#if EV_MULTIPLICITY 1270#if EV_MULTIPLICITY
1126 struct ev_loop *loop = &default_loop_struct; 1271 EV_P = signals [signum - 1].loop;
1127#endif 1272#endif
1128 1273
1129#if _WIN32 1274#if _WIN32
1130 signal (signum, ev_sighandler); 1275 signal (signum, ev_sighandler);
1131#endif 1276#endif
1132 1277
1133 signals [signum - 1].gotsig = 1; 1278 signals [signum - 1].pending = 1;
1134 evpipe_write (EV_A_ &gotsig); 1279 evpipe_write (EV_A_ &sig_pending);
1135} 1280}
1136 1281
1137void noinline 1282void noinline
1138ev_feed_signal_event (EV_P_ int signum) 1283ev_feed_signal_event (EV_P_ int signum)
1139{ 1284{
1140 WL w; 1285 WL w;
1141 1286
1287 if (expect_false (signum <= 0 || signum > EV_NSIG))
1288 return;
1289
1290 --signum;
1291
1142#if EV_MULTIPLICITY 1292#if EV_MULTIPLICITY
1143 assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); 1293 /* it is permissible to try to feed a signal to the wrong loop */
1144#endif 1294 /* or, likely more useful, feeding a signal nobody is waiting for */
1145 1295
1146 --signum; 1296 if (expect_false (signals [signum].loop != EV_A))
1147
1148 if (signum < 0 || signum >= signalmax)
1149 return; 1297 return;
1298#endif
1150 1299
1151 signals [signum].gotsig = 0; 1300 signals [signum].pending = 0;
1152 1301
1153 for (w = signals [signum].head; w; w = w->next) 1302 for (w = signals [signum].head; w; w = w->next)
1154 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1303 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1155} 1304}
1156 1305
1306#if EV_USE_SIGNALFD
1307static void
1308sigfdcb (EV_P_ ev_io *iow, int revents)
1309{
1310 struct signalfd_siginfo si[2], *sip; /* these structs are big */
1311
1312 for (;;)
1313 {
1314 ssize_t res = read (sigfd, si, sizeof (si));
1315
1316 /* not ISO-C, as res might be -1, but works with SuS */
1317 for (sip = si; (char *)sip < (char *)si + res; ++sip)
1318 ev_feed_signal_event (EV_A_ sip->ssi_signo);
1319
1320 if (res < (ssize_t)sizeof (si))
1321 break;
1322 }
1323}
1324#endif
1325
1157/*****************************************************************************/ 1326/*****************************************************************************/
1158 1327
1159static WL childs [EV_PID_HASHSIZE]; 1328static WL childs [EV_PID_HASHSIZE];
1160 1329
1161#ifndef _WIN32 1330#ifndef _WIN32
1164 1333
1165#ifndef WIFCONTINUED 1334#ifndef WIFCONTINUED
1166# define WIFCONTINUED(status) 0 1335# define WIFCONTINUED(status) 0
1167#endif 1336#endif
1168 1337
1169void inline_speed 1338/* handle a single child status event */
1339inline_speed void
1170child_reap (EV_P_ int chain, int pid, int status) 1340child_reap (EV_P_ int chain, int pid, int status)
1171{ 1341{
1172 ev_child *w; 1342 ev_child *w;
1173 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1343 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1174 1344
1187 1357
1188#ifndef WCONTINUED 1358#ifndef WCONTINUED
1189# define WCONTINUED 0 1359# define WCONTINUED 0
1190#endif 1360#endif
1191 1361
1362/* called on sigchld etc., calls waitpid */
1192static void 1363static void
1193childcb (EV_P_ ev_signal *sw, int revents) 1364childcb (EV_P_ ev_signal *sw, int revents)
1194{ 1365{
1195 int pid, status; 1366 int pid, status;
1196 1367
1303ev_backend (EV_P) 1474ev_backend (EV_P)
1304{ 1475{
1305 return backend; 1476 return backend;
1306} 1477}
1307 1478
1479#if EV_MINIMAL < 2
1308unsigned int 1480unsigned int
1309ev_loop_count (EV_P) 1481ev_loop_count (EV_P)
1310{ 1482{
1311 return loop_count; 1483 return loop_count;
1312} 1484}
1313 1485
1486unsigned int
1487ev_loop_depth (EV_P)
1488{
1489 return loop_depth;
1490}
1491
1314void 1492void
1315ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1493ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1316{ 1494{
1317 io_blocktime = interval; 1495 io_blocktime = interval;
1318} 1496}
1321ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1499ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1322{ 1500{
1323 timeout_blocktime = interval; 1501 timeout_blocktime = interval;
1324} 1502}
1325 1503
1504void
1505ev_set_userdata (EV_P_ void *data)
1506{
1507 userdata = data;
1508}
1509
1510void *
1511ev_userdata (EV_P)
1512{
1513 return userdata;
1514}
1515
1516void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1517{
1518 invoke_cb = invoke_pending_cb;
1519}
1520
1521void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1522{
1523 release_cb = release;
1524 acquire_cb = acquire;
1525}
1526#endif
1527
1528/* initialise a loop structure, must be zero-initialised */
1326static void noinline 1529static void noinline
1327loop_init (EV_P_ unsigned int flags) 1530loop_init (EV_P_ unsigned int flags)
1328{ 1531{
1329 if (!backend) 1532 if (!backend)
1330 { 1533 {
1346 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 1549 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1347 have_monotonic = 1; 1550 have_monotonic = 1;
1348 } 1551 }
1349#endif 1552#endif
1350 1553
1554 /* pid check not overridable via env */
1555#ifndef _WIN32
1556 if (flags & EVFLAG_FORKCHECK)
1557 curpid = getpid ();
1558#endif
1559
1560 if (!(flags & EVFLAG_NOENV)
1561 && !enable_secure ()
1562 && getenv ("LIBEV_FLAGS"))
1563 flags = atoi (getenv ("LIBEV_FLAGS"));
1564
1351 ev_rt_now = ev_time (); 1565 ev_rt_now = ev_time ();
1352 mn_now = get_clock (); 1566 mn_now = get_clock ();
1353 now_floor = mn_now; 1567 now_floor = mn_now;
1354 rtmn_diff = ev_rt_now - mn_now; 1568 rtmn_diff = ev_rt_now - mn_now;
1569#if EV_MINIMAL < 2
1570 invoke_cb = ev_invoke_pending;
1571#endif
1355 1572
1356 io_blocktime = 0.; 1573 io_blocktime = 0.;
1357 timeout_blocktime = 0.; 1574 timeout_blocktime = 0.;
1358 backend = 0; 1575 backend = 0;
1359 backend_fd = -1; 1576 backend_fd = -1;
1360 gotasync = 0; 1577 sig_pending = 0;
1578#if EV_ASYNC_ENABLE
1579 async_pending = 0;
1580#endif
1361#if EV_USE_INOTIFY 1581#if EV_USE_INOTIFY
1362 fs_fd = -2; 1582 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1363#endif 1583#endif
1364 1584#if EV_USE_SIGNALFD
1365 /* pid check not overridable via env */ 1585 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1366#ifndef _WIN32
1367 if (flags & EVFLAG_FORKCHECK)
1368 curpid = getpid ();
1369#endif 1586#endif
1370
1371 if (!(flags & EVFLAG_NOENV)
1372 && !enable_secure ()
1373 && getenv ("LIBEV_FLAGS"))
1374 flags = atoi (getenv ("LIBEV_FLAGS"));
1375 1587
1376 if (!(flags & 0x0000ffffU)) 1588 if (!(flags & 0x0000ffffU))
1377 flags |= ev_recommended_backends (); 1589 flags |= ev_recommended_backends ();
1378 1590
1379#if EV_USE_PORT 1591#if EV_USE_PORT
1390#endif 1602#endif
1391#if EV_USE_SELECT 1603#if EV_USE_SELECT
1392 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1604 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1393#endif 1605#endif
1394 1606
1607 ev_prepare_init (&pending_w, pendingcb);
1608
1395 ev_init (&pipeev, pipecb); 1609 ev_init (&pipe_w, pipecb);
1396 ev_set_priority (&pipeev, EV_MAXPRI); 1610 ev_set_priority (&pipe_w, EV_MAXPRI);
1397 } 1611 }
1398} 1612}
1399 1613
1614/* free up a loop structure */
1400static void noinline 1615static void noinline
1401loop_destroy (EV_P) 1616loop_destroy (EV_P)
1402{ 1617{
1403 int i; 1618 int i;
1404 1619
1405 if (ev_is_active (&pipeev)) 1620 if (ev_is_active (&pipe_w))
1406 { 1621 {
1407 ev_ref (EV_A); /* signal watcher */ 1622 /*ev_ref (EV_A);*/
1408 ev_io_stop (EV_A_ &pipeev); 1623 /*ev_io_stop (EV_A_ &pipe_w);*/
1409 1624
1410#if EV_USE_EVENTFD 1625#if EV_USE_EVENTFD
1411 if (evfd >= 0) 1626 if (evfd >= 0)
1412 close (evfd); 1627 close (evfd);
1413#endif 1628#endif
1414 1629
1415 if (evpipe [0] >= 0) 1630 if (evpipe [0] >= 0)
1416 { 1631 {
1417 close (evpipe [0]); 1632 EV_WIN32_CLOSE_FD (evpipe [0]);
1418 close (evpipe [1]); 1633 EV_WIN32_CLOSE_FD (evpipe [1]);
1419 } 1634 }
1420 } 1635 }
1636
1637#if EV_USE_SIGNALFD
1638 if (ev_is_active (&sigfd_w))
1639 {
1640 /*ev_ref (EV_A);*/
1641 /*ev_io_stop (EV_A_ &sigfd_w);*/
1642
1643 close (sigfd);
1644 }
1645#endif
1421 1646
1422#if EV_USE_INOTIFY 1647#if EV_USE_INOTIFY
1423 if (fs_fd >= 0) 1648 if (fs_fd >= 0)
1424 close (fs_fd); 1649 close (fs_fd);
1425#endif 1650#endif
1449#if EV_IDLE_ENABLE 1674#if EV_IDLE_ENABLE
1450 array_free (idle, [i]); 1675 array_free (idle, [i]);
1451#endif 1676#endif
1452 } 1677 }
1453 1678
1454 ev_free (anfds); anfdmax = 0; 1679 ev_free (anfds); anfds = 0; anfdmax = 0;
1455 1680
1456 /* have to use the microsoft-never-gets-it-right macro */ 1681 /* have to use the microsoft-never-gets-it-right macro */
1682 array_free (rfeed, EMPTY);
1457 array_free (fdchange, EMPTY); 1683 array_free (fdchange, EMPTY);
1458 array_free (timer, EMPTY); 1684 array_free (timer, EMPTY);
1459#if EV_PERIODIC_ENABLE 1685#if EV_PERIODIC_ENABLE
1460 array_free (periodic, EMPTY); 1686 array_free (periodic, EMPTY);
1461#endif 1687#endif
1470 1696
1471 backend = 0; 1697 backend = 0;
1472} 1698}
1473 1699
1474#if EV_USE_INOTIFY 1700#if EV_USE_INOTIFY
1475void inline_size infy_fork (EV_P); 1701inline_size void infy_fork (EV_P);
1476#endif 1702#endif
1477 1703
1478void inline_size 1704inline_size void
1479loop_fork (EV_P) 1705loop_fork (EV_P)
1480{ 1706{
1481#if EV_USE_PORT 1707#if EV_USE_PORT
1482 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1708 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
1483#endif 1709#endif
1489#endif 1715#endif
1490#if EV_USE_INOTIFY 1716#if EV_USE_INOTIFY
1491 infy_fork (EV_A); 1717 infy_fork (EV_A);
1492#endif 1718#endif
1493 1719
1494 if (ev_is_active (&pipeev)) 1720 if (ev_is_active (&pipe_w))
1495 { 1721 {
1496 /* this "locks" the handlers against writing to the pipe */ 1722 /* this "locks" the handlers against writing to the pipe */
1497 /* while we modify the fd vars */ 1723 /* while we modify the fd vars */
1498 gotsig = 1; 1724 sig_pending = 1;
1499#if EV_ASYNC_ENABLE 1725#if EV_ASYNC_ENABLE
1500 gotasync = 1; 1726 async_pending = 1;
1501#endif 1727#endif
1502 1728
1503 ev_ref (EV_A); 1729 ev_ref (EV_A);
1504 ev_io_stop (EV_A_ &pipeev); 1730 ev_io_stop (EV_A_ &pipe_w);
1505 1731
1506#if EV_USE_EVENTFD 1732#if EV_USE_EVENTFD
1507 if (evfd >= 0) 1733 if (evfd >= 0)
1508 close (evfd); 1734 close (evfd);
1509#endif 1735#endif
1510 1736
1511 if (evpipe [0] >= 0) 1737 if (evpipe [0] >= 0)
1512 { 1738 {
1513 close (evpipe [0]); 1739 EV_WIN32_CLOSE_FD (evpipe [0]);
1514 close (evpipe [1]); 1740 EV_WIN32_CLOSE_FD (evpipe [1]);
1515 } 1741 }
1516 1742
1517 evpipe_init (EV_A); 1743 evpipe_init (EV_A);
1518 /* now iterate over everything, in case we missed something */ 1744 /* now iterate over everything, in case we missed something */
1519 pipecb (EV_A_ &pipeev, EV_READ); 1745 pipecb (EV_A_ &pipe_w, EV_READ);
1520 } 1746 }
1521 1747
1522 postfork = 0; 1748 postfork = 0;
1523} 1749}
1524 1750
1525#if EV_MULTIPLICITY 1751#if EV_MULTIPLICITY
1526 1752
1527struct ev_loop * 1753struct ev_loop *
1528ev_loop_new (unsigned int flags) 1754ev_loop_new (unsigned int flags)
1529{ 1755{
1530 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1756 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1531 1757
1532 memset (loop, 0, sizeof (struct ev_loop)); 1758 memset (EV_A, 0, sizeof (struct ev_loop));
1533
1534 loop_init (EV_A_ flags); 1759 loop_init (EV_A_ flags);
1535 1760
1536 if (ev_backend (EV_A)) 1761 if (ev_backend (EV_A))
1537 return loop; 1762 return EV_A;
1538 1763
1539 return 0; 1764 return 0;
1540} 1765}
1541 1766
1542void 1767void
1549void 1774void
1550ev_loop_fork (EV_P) 1775ev_loop_fork (EV_P)
1551{ 1776{
1552 postfork = 1; /* must be in line with ev_default_fork */ 1777 postfork = 1; /* must be in line with ev_default_fork */
1553} 1778}
1779#endif /* multiplicity */
1554 1780
1555#if EV_VERIFY 1781#if EV_VERIFY
1556static void noinline 1782static void noinline
1557verify_watcher (EV_P_ W w) 1783verify_watcher (EV_P_ W w)
1558{ 1784{
1586 verify_watcher (EV_A_ ws [cnt]); 1812 verify_watcher (EV_A_ ws [cnt]);
1587 } 1813 }
1588} 1814}
1589#endif 1815#endif
1590 1816
1817#if EV_MINIMAL < 2
1591void 1818void
1592ev_loop_verify (EV_P) 1819ev_loop_verify (EV_P)
1593{ 1820{
1594#if EV_VERIFY 1821#if EV_VERIFY
1595 int i; 1822 int i;
1644 assert (checkmax >= checkcnt); 1871 assert (checkmax >= checkcnt);
1645 array_verify (EV_A_ (W *)checks, checkcnt); 1872 array_verify (EV_A_ (W *)checks, checkcnt);
1646 1873
1647# if 0 1874# if 0
1648 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1875 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1649 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1876 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1650# endif 1877# endif
1651#endif 1878#endif
1652} 1879}
1653 1880#endif
1654#endif /* multiplicity */
1655 1881
1656#if EV_MULTIPLICITY 1882#if EV_MULTIPLICITY
1657struct ev_loop * 1883struct ev_loop *
1658ev_default_loop_init (unsigned int flags) 1884ev_default_loop_init (unsigned int flags)
1659#else 1885#else
1662#endif 1888#endif
1663{ 1889{
1664 if (!ev_default_loop_ptr) 1890 if (!ev_default_loop_ptr)
1665 { 1891 {
1666#if EV_MULTIPLICITY 1892#if EV_MULTIPLICITY
1667 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; 1893 EV_P = ev_default_loop_ptr = &default_loop_struct;
1668#else 1894#else
1669 ev_default_loop_ptr = 1; 1895 ev_default_loop_ptr = 1;
1670#endif 1896#endif
1671 1897
1672 loop_init (EV_A_ flags); 1898 loop_init (EV_A_ flags);
1689 1915
1690void 1916void
1691ev_default_destroy (void) 1917ev_default_destroy (void)
1692{ 1918{
1693#if EV_MULTIPLICITY 1919#if EV_MULTIPLICITY
1694 struct ev_loop *loop = ev_default_loop_ptr; 1920 EV_P = ev_default_loop_ptr;
1695#endif 1921#endif
1696 1922
1697 ev_default_loop_ptr = 0; 1923 ev_default_loop_ptr = 0;
1698 1924
1699#ifndef _WIN32 1925#ifndef _WIN32
1706 1932
1707void 1933void
1708ev_default_fork (void) 1934ev_default_fork (void)
1709{ 1935{
1710#if EV_MULTIPLICITY 1936#if EV_MULTIPLICITY
1711 struct ev_loop *loop = ev_default_loop_ptr; 1937 EV_P = ev_default_loop_ptr;
1712#endif 1938#endif
1713 1939
1714 postfork = 1; /* must be in line with ev_loop_fork */ 1940 postfork = 1; /* must be in line with ev_loop_fork */
1715} 1941}
1716 1942
1720ev_invoke (EV_P_ void *w, int revents) 1946ev_invoke (EV_P_ void *w, int revents)
1721{ 1947{
1722 EV_CB_INVOKE ((W)w, revents); 1948 EV_CB_INVOKE ((W)w, revents);
1723} 1949}
1724 1950
1725void inline_speed 1951unsigned int
1726call_pending (EV_P) 1952ev_pending_count (EV_P)
1953{
1954 int pri;
1955 unsigned int count = 0;
1956
1957 for (pri = NUMPRI; pri--; )
1958 count += pendingcnt [pri];
1959
1960 return count;
1961}
1962
1963void noinline
1964ev_invoke_pending (EV_P)
1727{ 1965{
1728 int pri; 1966 int pri;
1729 1967
1730 for (pri = NUMPRI; pri--; ) 1968 for (pri = NUMPRI; pri--; )
1731 while (pendingcnt [pri]) 1969 while (pendingcnt [pri])
1732 { 1970 {
1733 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1971 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1734 1972
1735 if (expect_true (p->w))
1736 {
1737 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1973 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1974 /* ^ this is no longer true, as pending_w could be here */
1738 1975
1739 p->w->pending = 0; 1976 p->w->pending = 0;
1740 EV_CB_INVOKE (p->w, p->events); 1977 EV_CB_INVOKE (p->w, p->events);
1741 EV_FREQUENT_CHECK; 1978 EV_FREQUENT_CHECK;
1742 }
1743 } 1979 }
1744} 1980}
1745 1981
1746#if EV_IDLE_ENABLE 1982#if EV_IDLE_ENABLE
1747void inline_size 1983/* make idle watchers pending. this handles the "call-idle */
1984/* only when higher priorities are idle" logic */
1985inline_size void
1748idle_reify (EV_P) 1986idle_reify (EV_P)
1749{ 1987{
1750 if (expect_false (idleall)) 1988 if (expect_false (idleall))
1751 { 1989 {
1752 int pri; 1990 int pri;
1764 } 2002 }
1765 } 2003 }
1766} 2004}
1767#endif 2005#endif
1768 2006
1769void inline_size 2007/* make timers pending */
2008inline_size void
1770timers_reify (EV_P) 2009timers_reify (EV_P)
1771{ 2010{
1772 EV_FREQUENT_CHECK; 2011 EV_FREQUENT_CHECK;
1773 2012
1774 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now) 2013 if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
1775 { 2014 {
1776 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); 2015 do
1777
1778 /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
1779
1780 /* first reschedule or stop timer */
1781 if (w->repeat)
1782 { 2016 {
2017 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
2018
2019 /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
2020
2021 /* first reschedule or stop timer */
2022 if (w->repeat)
2023 {
1783 ev_at (w) += w->repeat; 2024 ev_at (w) += w->repeat;
1784 if (ev_at (w) < mn_now) 2025 if (ev_at (w) < mn_now)
1785 ev_at (w) = mn_now; 2026 ev_at (w) = mn_now;
1786 2027
1787 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 2028 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1788 2029
1789 ANHE_at_cache (timers [HEAP0]); 2030 ANHE_at_cache (timers [HEAP0]);
1790 downheap (timers, timercnt, HEAP0); 2031 downheap (timers, timercnt, HEAP0);
2032 }
2033 else
2034 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
2035
2036 EV_FREQUENT_CHECK;
2037 feed_reverse (EV_A_ (W)w);
1791 } 2038 }
1792 else 2039 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
1793 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1794 2040
1795 EV_FREQUENT_CHECK;
1796 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 2041 feed_reverse_done (EV_A_ EV_TIMEOUT);
1797 } 2042 }
1798} 2043}
1799 2044
1800#if EV_PERIODIC_ENABLE 2045#if EV_PERIODIC_ENABLE
1801void inline_size 2046/* make periodics pending */
2047inline_size void
1802periodics_reify (EV_P) 2048periodics_reify (EV_P)
1803{ 2049{
1804 EV_FREQUENT_CHECK; 2050 EV_FREQUENT_CHECK;
1805 2051
1806 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 2052 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1807 { 2053 {
1808 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 2054 int feed_count = 0;
1809 2055
1810 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 2056 do
1811
1812 /* first reschedule or stop timer */
1813 if (w->reschedule_cb)
1814 { 2057 {
2058 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2059
2060 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
2061
2062 /* first reschedule or stop timer */
2063 if (w->reschedule_cb)
2064 {
1815 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 2065 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1816 2066
1817 assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 2067 assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1818 2068
1819 ANHE_at_cache (periodics [HEAP0]); 2069 ANHE_at_cache (periodics [HEAP0]);
1820 downheap (periodics, periodiccnt, HEAP0); 2070 downheap (periodics, periodiccnt, HEAP0);
2071 }
2072 else if (w->interval)
2073 {
2074 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
2075 /* if next trigger time is not sufficiently in the future, put it there */
2076 /* this might happen because of floating point inexactness */
2077 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
2078 {
2079 ev_at (w) += w->interval;
2080
2081 /* if interval is unreasonably low we might still have a time in the past */
2082 /* so correct this. this will make the periodic very inexact, but the user */
2083 /* has effectively asked to get triggered more often than possible */
2084 if (ev_at (w) < ev_rt_now)
2085 ev_at (w) = ev_rt_now;
2086 }
2087
2088 ANHE_at_cache (periodics [HEAP0]);
2089 downheap (periodics, periodiccnt, HEAP0);
2090 }
2091 else
2092 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
2093
2094 EV_FREQUENT_CHECK;
2095 feed_reverse (EV_A_ (W)w);
1821 } 2096 }
1822 else if (w->interval) 2097 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now);
1823 {
1824 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1825 /* if next trigger time is not sufficiently in the future, put it there */
1826 /* this might happen because of floating point inexactness */
1827 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
1828 {
1829 ev_at (w) += w->interval;
1830 2098
1831 /* if interval is unreasonably low we might still have a time in the past */
1832 /* so correct this. this will make the periodic very inexact, but the user */
1833 /* has effectively asked to get triggered more often than possible */
1834 if (ev_at (w) < ev_rt_now)
1835 ev_at (w) = ev_rt_now;
1836 }
1837
1838 ANHE_at_cache (periodics [HEAP0]);
1839 downheap (periodics, periodiccnt, HEAP0);
1840 }
1841 else
1842 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1843
1844 EV_FREQUENT_CHECK;
1845 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 2099 feed_reverse_done (EV_A_ EV_PERIODIC);
1846 } 2100 }
1847} 2101}
1848 2102
2103/* simply recalculate all periodics */
2104/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1849static void noinline 2105static void noinline
1850periodics_reschedule (EV_P) 2106periodics_reschedule (EV_P)
1851{ 2107{
1852 int i; 2108 int i;
1853 2109
1866 2122
1867 reheap (periodics, periodiccnt); 2123 reheap (periodics, periodiccnt);
1868} 2124}
1869#endif 2125#endif
1870 2126
1871void inline_speed 2127/* adjust all timers by a given offset */
2128static void noinline
2129timers_reschedule (EV_P_ ev_tstamp adjust)
2130{
2131 int i;
2132
2133 for (i = 0; i < timercnt; ++i)
2134 {
2135 ANHE *he = timers + i + HEAP0;
2136 ANHE_w (*he)->at += adjust;
2137 ANHE_at_cache (*he);
2138 }
2139}
2140
2141/* fetch new monotonic and realtime times from the kernel */
2142/* also detetc if there was a timejump, and act accordingly */
2143inline_speed void
1872time_update (EV_P_ ev_tstamp max_block) 2144time_update (EV_P_ ev_tstamp max_block)
1873{ 2145{
1874 int i;
1875
1876#if EV_USE_MONOTONIC 2146#if EV_USE_MONOTONIC
1877 if (expect_true (have_monotonic)) 2147 if (expect_true (have_monotonic))
1878 { 2148 {
2149 int i;
1879 ev_tstamp odiff = rtmn_diff; 2150 ev_tstamp odiff = rtmn_diff;
1880 2151
1881 mn_now = get_clock (); 2152 mn_now = get_clock ();
1882 2153
1883 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 2154 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1909 ev_rt_now = ev_time (); 2180 ev_rt_now = ev_time ();
1910 mn_now = get_clock (); 2181 mn_now = get_clock ();
1911 now_floor = mn_now; 2182 now_floor = mn_now;
1912 } 2183 }
1913 2184
2185 /* no timer adjustment, as the monotonic clock doesn't jump */
2186 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1914# if EV_PERIODIC_ENABLE 2187# if EV_PERIODIC_ENABLE
1915 periodics_reschedule (EV_A); 2188 periodics_reschedule (EV_A);
1916# endif 2189# endif
1917 /* no timer adjustment, as the monotonic clock doesn't jump */
1918 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1919 } 2190 }
1920 else 2191 else
1921#endif 2192#endif
1922 { 2193 {
1923 ev_rt_now = ev_time (); 2194 ev_rt_now = ev_time ();
1924 2195
1925 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) 2196 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
1926 { 2197 {
2198 /* adjust timers. this is easy, as the offset is the same for all of them */
2199 timers_reschedule (EV_A_ ev_rt_now - mn_now);
1927#if EV_PERIODIC_ENABLE 2200#if EV_PERIODIC_ENABLE
1928 periodics_reschedule (EV_A); 2201 periodics_reschedule (EV_A);
1929#endif 2202#endif
1930 /* adjust timers. this is easy, as the offset is the same for all of them */
1931 for (i = 0; i < timercnt; ++i)
1932 {
1933 ANHE *he = timers + i + HEAP0;
1934 ANHE_w (*he)->at += ev_rt_now - mn_now;
1935 ANHE_at_cache (*he);
1936 }
1937 } 2203 }
1938 2204
1939 mn_now = ev_rt_now; 2205 mn_now = ev_rt_now;
1940 } 2206 }
1941} 2207}
1942 2208
1943void 2209void
1944ev_ref (EV_P)
1945{
1946 ++activecnt;
1947}
1948
1949void
1950ev_unref (EV_P)
1951{
1952 --activecnt;
1953}
1954
1955void
1956ev_now_update (EV_P)
1957{
1958 time_update (EV_A_ 1e100);
1959}
1960
1961static int loop_done;
1962
1963void
1964ev_loop (EV_P_ int flags) 2210ev_loop (EV_P_ int flags)
1965{ 2211{
2212#if EV_MINIMAL < 2
2213 ++loop_depth;
2214#endif
2215
2216 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2217
1966 loop_done = EVUNLOOP_CANCEL; 2218 loop_done = EVUNLOOP_CANCEL;
1967 2219
1968 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2220 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
1969 2221
1970 do 2222 do
1971 { 2223 {
1972#if EV_VERIFY >= 2 2224#if EV_VERIFY >= 2
1973 ev_loop_verify (EV_A); 2225 ev_loop_verify (EV_A);
1986 /* we might have forked, so queue fork handlers */ 2238 /* we might have forked, so queue fork handlers */
1987 if (expect_false (postfork)) 2239 if (expect_false (postfork))
1988 if (forkcnt) 2240 if (forkcnt)
1989 { 2241 {
1990 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2242 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1991 call_pending (EV_A); 2243 EV_INVOKE_PENDING;
1992 } 2244 }
1993#endif 2245#endif
1994 2246
1995 /* queue prepare watchers (and execute them) */ 2247 /* queue prepare watchers (and execute them) */
1996 if (expect_false (preparecnt)) 2248 if (expect_false (preparecnt))
1997 { 2249 {
1998 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2250 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1999 call_pending (EV_A); 2251 EV_INVOKE_PENDING;
2000 } 2252 }
2253
2254 if (expect_false (loop_done))
2255 break;
2001 2256
2002 /* we might have forked, so reify kernel state if necessary */ 2257 /* we might have forked, so reify kernel state if necessary */
2003 if (expect_false (postfork)) 2258 if (expect_false (postfork))
2004 loop_fork (EV_A); 2259 loop_fork (EV_A);
2005 2260
2011 ev_tstamp waittime = 0.; 2266 ev_tstamp waittime = 0.;
2012 ev_tstamp sleeptime = 0.; 2267 ev_tstamp sleeptime = 0.;
2013 2268
2014 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2269 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2015 { 2270 {
2271 /* remember old timestamp for io_blocktime calculation */
2272 ev_tstamp prev_mn_now = mn_now;
2273
2016 /* update time to cancel out callback processing overhead */ 2274 /* update time to cancel out callback processing overhead */
2017 time_update (EV_A_ 1e100); 2275 time_update (EV_A_ 1e100);
2018 2276
2019 waittime = MAX_BLOCKTIME; 2277 waittime = MAX_BLOCKTIME;
2020 2278
2030 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2288 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2031 if (waittime > to) waittime = to; 2289 if (waittime > to) waittime = to;
2032 } 2290 }
2033#endif 2291#endif
2034 2292
2293 /* don't let timeouts decrease the waittime below timeout_blocktime */
2035 if (expect_false (waittime < timeout_blocktime)) 2294 if (expect_false (waittime < timeout_blocktime))
2036 waittime = timeout_blocktime; 2295 waittime = timeout_blocktime;
2037 2296
2038 sleeptime = waittime - backend_fudge; 2297 /* extra check because io_blocktime is commonly 0 */
2039
2040 if (expect_true (sleeptime > io_blocktime)) 2298 if (expect_false (io_blocktime))
2041 sleeptime = io_blocktime;
2042
2043 if (sleeptime)
2044 { 2299 {
2300 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2301
2302 if (sleeptime > waittime - backend_fudge)
2303 sleeptime = waittime - backend_fudge;
2304
2305 if (expect_true (sleeptime > 0.))
2306 {
2045 ev_sleep (sleeptime); 2307 ev_sleep (sleeptime);
2046 waittime -= sleeptime; 2308 waittime -= sleeptime;
2309 }
2047 } 2310 }
2048 } 2311 }
2049 2312
2313#if EV_MINIMAL < 2
2050 ++loop_count; 2314 ++loop_count;
2315#endif
2316 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2051 backend_poll (EV_A_ waittime); 2317 backend_poll (EV_A_ waittime);
2318 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2052 2319
2053 /* update ev_rt_now, do magic */ 2320 /* update ev_rt_now, do magic */
2054 time_update (EV_A_ waittime + sleeptime); 2321 time_update (EV_A_ waittime + sleeptime);
2055 } 2322 }
2056 2323
2067 2334
2068 /* queue check watchers, to be executed first */ 2335 /* queue check watchers, to be executed first */
2069 if (expect_false (checkcnt)) 2336 if (expect_false (checkcnt))
2070 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2337 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2071 2338
2072 call_pending (EV_A); 2339 EV_INVOKE_PENDING;
2073 } 2340 }
2074 while (expect_true ( 2341 while (expect_true (
2075 activecnt 2342 activecnt
2076 && !loop_done 2343 && !loop_done
2077 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2344 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2078 )); 2345 ));
2079 2346
2080 if (loop_done == EVUNLOOP_ONE) 2347 if (loop_done == EVUNLOOP_ONE)
2081 loop_done = EVUNLOOP_CANCEL; 2348 loop_done = EVUNLOOP_CANCEL;
2349
2350#if EV_MINIMAL < 2
2351 --loop_depth;
2352#endif
2082} 2353}
2083 2354
2084void 2355void
2085ev_unloop (EV_P_ int how) 2356ev_unloop (EV_P_ int how)
2086{ 2357{
2087 loop_done = how; 2358 loop_done = how;
2088} 2359}
2089 2360
2361void
2362ev_ref (EV_P)
2363{
2364 ++activecnt;
2365}
2366
2367void
2368ev_unref (EV_P)
2369{
2370 --activecnt;
2371}
2372
2373void
2374ev_now_update (EV_P)
2375{
2376 time_update (EV_A_ 1e100);
2377}
2378
2379void
2380ev_suspend (EV_P)
2381{
2382 ev_now_update (EV_A);
2383}
2384
2385void
2386ev_resume (EV_P)
2387{
2388 ev_tstamp mn_prev = mn_now;
2389
2390 ev_now_update (EV_A);
2391 timers_reschedule (EV_A_ mn_now - mn_prev);
2392#if EV_PERIODIC_ENABLE
2393 /* TODO: really do this? */
2394 periodics_reschedule (EV_A);
2395#endif
2396}
2397
2090/*****************************************************************************/ 2398/*****************************************************************************/
2399/* singly-linked list management, used when the expected list length is short */
2091 2400
2092void inline_size 2401inline_size void
2093wlist_add (WL *head, WL elem) 2402wlist_add (WL *head, WL elem)
2094{ 2403{
2095 elem->next = *head; 2404 elem->next = *head;
2096 *head = elem; 2405 *head = elem;
2097} 2406}
2098 2407
2099void inline_size 2408inline_size void
2100wlist_del (WL *head, WL elem) 2409wlist_del (WL *head, WL elem)
2101{ 2410{
2102 while (*head) 2411 while (*head)
2103 { 2412 {
2104 if (*head == elem) 2413 if (expect_true (*head == elem))
2105 { 2414 {
2106 *head = elem->next; 2415 *head = elem->next;
2107 return; 2416 break;
2108 } 2417 }
2109 2418
2110 head = &(*head)->next; 2419 head = &(*head)->next;
2111 } 2420 }
2112} 2421}
2113 2422
2114void inline_speed 2423/* internal, faster, version of ev_clear_pending */
2424inline_speed void
2115clear_pending (EV_P_ W w) 2425clear_pending (EV_P_ W w)
2116{ 2426{
2117 if (w->pending) 2427 if (w->pending)
2118 { 2428 {
2119 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2429 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2120 w->pending = 0; 2430 w->pending = 0;
2121 } 2431 }
2122} 2432}
2123 2433
2124int 2434int
2128 int pending = w_->pending; 2438 int pending = w_->pending;
2129 2439
2130 if (expect_true (pending)) 2440 if (expect_true (pending))
2131 { 2441 {
2132 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2442 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2443 p->w = (W)&pending_w;
2133 w_->pending = 0; 2444 w_->pending = 0;
2134 p->w = 0;
2135 return p->events; 2445 return p->events;
2136 } 2446 }
2137 else 2447 else
2138 return 0; 2448 return 0;
2139} 2449}
2140 2450
2141void inline_size 2451inline_size void
2142pri_adjust (EV_P_ W w) 2452pri_adjust (EV_P_ W w)
2143{ 2453{
2144 int pri = w->priority; 2454 int pri = ev_priority (w);
2145 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2455 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2146 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2456 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2147 w->priority = pri; 2457 ev_set_priority (w, pri);
2148} 2458}
2149 2459
2150void inline_speed 2460inline_speed void
2151ev_start (EV_P_ W w, int active) 2461ev_start (EV_P_ W w, int active)
2152{ 2462{
2153 pri_adjust (EV_A_ w); 2463 pri_adjust (EV_A_ w);
2154 w->active = active; 2464 w->active = active;
2155 ev_ref (EV_A); 2465 ev_ref (EV_A);
2156} 2466}
2157 2467
2158void inline_size 2468inline_size void
2159ev_stop (EV_P_ W w) 2469ev_stop (EV_P_ W w)
2160{ 2470{
2161 ev_unref (EV_A); 2471 ev_unref (EV_A);
2162 w->active = 0; 2472 w->active = 0;
2163} 2473}
2179 2489
2180 ev_start (EV_A_ (W)w, 1); 2490 ev_start (EV_A_ (W)w, 1);
2181 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2491 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2182 wlist_add (&anfds[fd].head, (WL)w); 2492 wlist_add (&anfds[fd].head, (WL)w);
2183 2493
2184 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2494 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2185 w->events &= ~EV__IOFDSET; 2495 w->events &= ~EV__IOFDSET;
2186 2496
2187 EV_FREQUENT_CHECK; 2497 EV_FREQUENT_CHECK;
2188} 2498}
2189 2499
2283 } 2593 }
2284 2594
2285 EV_FREQUENT_CHECK; 2595 EV_FREQUENT_CHECK;
2286} 2596}
2287 2597
2598ev_tstamp
2599ev_timer_remaining (EV_P_ ev_timer *w)
2600{
2601 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2602}
2603
2288#if EV_PERIODIC_ENABLE 2604#if EV_PERIODIC_ENABLE
2289void noinline 2605void noinline
2290ev_periodic_start (EV_P_ ev_periodic *w) 2606ev_periodic_start (EV_P_ ev_periodic *w)
2291{ 2607{
2292 if (expect_false (ev_is_active (w))) 2608 if (expect_false (ev_is_active (w)))
2359#endif 2675#endif
2360 2676
2361void noinline 2677void noinline
2362ev_signal_start (EV_P_ ev_signal *w) 2678ev_signal_start (EV_P_ ev_signal *w)
2363{ 2679{
2364#if EV_MULTIPLICITY
2365 assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2366#endif
2367 if (expect_false (ev_is_active (w))) 2680 if (expect_false (ev_is_active (w)))
2368 return; 2681 return;
2369 2682
2370 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); 2683 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2371 2684
2372 evpipe_init (EV_A); 2685#if EV_MULTIPLICITY
2686 assert (("libev: a signal must not be attached to two different loops",
2687 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2373 2688
2374 EV_FREQUENT_CHECK; 2689 signals [w->signum - 1].loop = EV_A;
2690#endif
2375 2691
2692 EV_FREQUENT_CHECK;
2693
2694#if EV_USE_SIGNALFD
2695 if (sigfd == -2)
2376 { 2696 {
2377#ifndef _WIN32 2697 sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
2378 sigset_t full, prev; 2698 if (sigfd < 0 && errno == EINVAL)
2379 sigfillset (&full); 2699 sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
2380 sigprocmask (SIG_SETMASK, &full, &prev);
2381#endif
2382 2700
2383 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2701 if (sigfd >= 0)
2702 {
2703 fd_intern (sigfd); /* doing it twice will not hurt */
2384 2704
2385#ifndef _WIN32 2705 sigemptyset (&sigfd_set);
2386 sigprocmask (SIG_SETMASK, &prev, 0); 2706
2387#endif 2707 ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
2708 ev_set_priority (&sigfd_w, EV_MAXPRI);
2709 ev_io_start (EV_A_ &sigfd_w);
2710 ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
2711 }
2388 } 2712 }
2713
2714 if (sigfd >= 0)
2715 {
2716 /* TODO: check .head */
2717 sigaddset (&sigfd_set, w->signum);
2718 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2719
2720 signalfd (sigfd, &sigfd_set, 0);
2721 }
2722#endif
2389 2723
2390 ev_start (EV_A_ (W)w, 1); 2724 ev_start (EV_A_ (W)w, 1);
2391 wlist_add (&signals [w->signum - 1].head, (WL)w); 2725 wlist_add (&signals [w->signum - 1].head, (WL)w);
2392 2726
2393 if (!((WL)w)->next) 2727 if (!((WL)w)->next)
2728# if EV_USE_SIGNALFD
2729 if (sigfd < 0) /*TODO*/
2730# endif
2394 { 2731 {
2395#if _WIN32 2732# if _WIN32
2396 signal (w->signum, ev_sighandler); 2733 signal (w->signum, ev_sighandler);
2397#else 2734# else
2398 struct sigaction sa; 2735 struct sigaction sa;
2736
2737 evpipe_init (EV_A);
2738
2399 sa.sa_handler = ev_sighandler; 2739 sa.sa_handler = ev_sighandler;
2400 sigfillset (&sa.sa_mask); 2740 sigfillset (&sa.sa_mask);
2401 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2741 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2402 sigaction (w->signum, &sa, 0); 2742 sigaction (w->signum, &sa, 0);
2743
2744 sigemptyset (&sa.sa_mask);
2745 sigaddset (&sa.sa_mask, w->signum);
2746 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
2403#endif 2747#endif
2404 } 2748 }
2405 2749
2406 EV_FREQUENT_CHECK; 2750 EV_FREQUENT_CHECK;
2407} 2751}
2408 2752
2409void noinline 2753void noinline
2417 2761
2418 wlist_del (&signals [w->signum - 1].head, (WL)w); 2762 wlist_del (&signals [w->signum - 1].head, (WL)w);
2419 ev_stop (EV_A_ (W)w); 2763 ev_stop (EV_A_ (W)w);
2420 2764
2421 if (!signals [w->signum - 1].head) 2765 if (!signals [w->signum - 1].head)
2766 {
2767#if EV_MULTIPLICITY
2768 signals [w->signum - 1].loop = 0; /* unattach from signal */
2769#endif
2770#if EV_USE_SIGNALFD
2771 if (sigfd >= 0)
2772 {
2773 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2774 sigdelset (&sigfd_set, w->signum);
2775 signalfd (sigfd, &sigfd_set, 0);
2776 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2777 /*TODO: maybe unblock signal? */
2778 }
2779 else
2780#endif
2422 signal (w->signum, SIG_DFL); 2781 signal (w->signum, SIG_DFL);
2782 }
2423 2783
2424 EV_FREQUENT_CHECK; 2784 EV_FREQUENT_CHECK;
2425} 2785}
2426 2786
2427void 2787void
2507 } 2867 }
2508 } 2868 }
2509 2869
2510 if (w->wd >= 0) 2870 if (w->wd >= 0)
2511 { 2871 {
2872 struct statfs sfs;
2873
2512 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2874 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2513 2875
2514 /* now local changes will be tracked by inotify, but remote changes won't */ 2876 /* now local changes will be tracked by inotify, but remote changes won't */
2515 /* unless the filesystem it known to be local, we therefore still poll */ 2877 /* unless the filesystem it known to be local, we therefore still poll */
2516 /* also do poll on <2.6.25, but with normal frequency */ 2878 /* also do poll on <2.6.25, but with normal frequency */
2517 struct statfs sfs;
2518 2879
2519 if (fs_2625 && !statfs (w->path, &sfs)) 2880 if (fs_2625 && !statfs (w->path, &sfs))
2520 if (sfs.f_type == 0x1373 /* devfs */ 2881 if (sfs.f_type == 0x1373 /* devfs */
2521 || sfs.f_type == 0xEF53 /* ext2/3 */ 2882 || sfs.f_type == 0xEF53 /* ext2/3 */
2522 || sfs.f_type == 0x3153464a /* jfs */ 2883 || sfs.f_type == 0x3153464a /* jfs */
2588 2949
2589 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) 2950 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
2590 infy_wd (EV_A_ ev->wd, ev->wd, ev); 2951 infy_wd (EV_A_ ev->wd, ev->wd, ev);
2591} 2952}
2592 2953
2593void inline_size 2954inline_size void
2594check_2625 (EV_P) 2955check_2625 (EV_P)
2595{ 2956{
2596 /* kernels < 2.6.25 are borked 2957 /* kernels < 2.6.25 are borked
2597 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 2958 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
2598 */ 2959 */
2611 return; 2972 return;
2612 2973
2613 fs_2625 = 1; 2974 fs_2625 = 1;
2614} 2975}
2615 2976
2616void inline_size 2977inline_size void
2617infy_init (EV_P) 2978infy_init (EV_P)
2618{ 2979{
2619 if (fs_fd != -2) 2980 if (fs_fd != -2)
2620 return; 2981 return;
2621 2982
2631 ev_set_priority (&fs_w, EV_MAXPRI); 2992 ev_set_priority (&fs_w, EV_MAXPRI);
2632 ev_io_start (EV_A_ &fs_w); 2993 ev_io_start (EV_A_ &fs_w);
2633 } 2994 }
2634} 2995}
2635 2996
2636void inline_size 2997inline_size void
2637infy_fork (EV_P) 2998infy_fork (EV_P)
2638{ 2999{
2639 int slot; 3000 int slot;
2640 3001
2641 if (fs_fd < 0) 3002 if (fs_fd < 0)
2907embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3268embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
2908{ 3269{
2909 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); 3270 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
2910 3271
2911 { 3272 {
2912 struct ev_loop *loop = w->other; 3273 EV_P = w->other;
2913 3274
2914 while (fdchangecnt) 3275 while (fdchangecnt)
2915 { 3276 {
2916 fd_reify (EV_A); 3277 fd_reify (EV_A);
2917 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3278 ev_loop (EV_A_ EVLOOP_NONBLOCK);
2925 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); 3286 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
2926 3287
2927 ev_embed_stop (EV_A_ w); 3288 ev_embed_stop (EV_A_ w);
2928 3289
2929 { 3290 {
2930 struct ev_loop *loop = w->other; 3291 EV_P = w->other;
2931 3292
2932 ev_loop_fork (EV_A); 3293 ev_loop_fork (EV_A);
2933 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3294 ev_loop (EV_A_ EVLOOP_NONBLOCK);
2934 } 3295 }
2935 3296
2949{ 3310{
2950 if (expect_false (ev_is_active (w))) 3311 if (expect_false (ev_is_active (w)))
2951 return; 3312 return;
2952 3313
2953 { 3314 {
2954 struct ev_loop *loop = w->other; 3315 EV_P = w->other;
2955 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 3316 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
2956 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); 3317 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
2957 } 3318 }
2958 3319
2959 EV_FREQUENT_CHECK; 3320 EV_FREQUENT_CHECK;
3071 3432
3072void 3433void
3073ev_async_send (EV_P_ ev_async *w) 3434ev_async_send (EV_P_ ev_async *w)
3074{ 3435{
3075 w->sent = 1; 3436 w->sent = 1;
3076 evpipe_write (EV_A_ &gotasync); 3437 evpipe_write (EV_A_ &async_pending);
3077} 3438}
3078#endif 3439#endif
3079 3440
3080/*****************************************************************************/ 3441/*****************************************************************************/
3081 3442
3145 } 3506 }
3146} 3507}
3147 3508
3148/*****************************************************************************/ 3509/*****************************************************************************/
3149 3510
3150#if 0 3511#if EV_WALK_ENABLE
3151void 3512void
3152ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3513ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3153{ 3514{
3154 int i, j; 3515 int i, j;
3155 ev_watcher_list *wl, *wn; 3516 ev_watcher_list *wl, *wn;
3171#if EV_USE_INOTIFY 3532#if EV_USE_INOTIFY
3172 if (ev_cb ((ev_io *)wl) == infy_cb) 3533 if (ev_cb ((ev_io *)wl) == infy_cb)
3173 ; 3534 ;
3174 else 3535 else
3175#endif 3536#endif
3176 if ((ev_io *)wl != &pipeev) 3537 if ((ev_io *)wl != &pipe_w)
3177 if (types & EV_IO) 3538 if (types & EV_IO)
3178 cb (EV_A_ EV_IO, wl); 3539 cb (EV_A_ EV_IO, wl);
3179 3540
3180 wl = wn; 3541 wl = wn;
3181 } 3542 }
3230 if (types & EV_CHECK) 3591 if (types & EV_CHECK)
3231 for (i = checkcnt; i--; ) 3592 for (i = checkcnt; i--; )
3232 cb (EV_A_ EV_CHECK, checks [i]); 3593 cb (EV_A_ EV_CHECK, checks [i]);
3233 3594
3234 if (types & EV_SIGNAL) 3595 if (types & EV_SIGNAL)
3235 for (i = 0; i < signalmax; ++i) 3596 for (i = 0; i < EV_NSIG - 1; ++i)
3236 for (wl = signals [i].head; wl; ) 3597 for (wl = signals [i].head; wl; )
3237 { 3598 {
3238 wn = wl->next; 3599 wn = wl->next;
3239 cb (EV_A_ EV_SIGNAL, wl); 3600 cb (EV_A_ EV_SIGNAL, wl);
3240 wl = wn; 3601 wl = wn;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines