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

Comparing libev/ev.c (file contents):
Revision 1.489 by root, Sat Dec 29 14:23:20 2018 UTC vs.
Revision 1.490 by root, Thu Jun 20 22:44:59 2019 UTC

1/* 1/*
2 * libev event processing core, watcher management 2 * libev event processing core, watcher management
3 * 3 *
4 * Copyright (c) 2007-2018 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007-2019 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
315 315
316#ifndef EV_USE_PORT 316#ifndef EV_USE_PORT
317# define EV_USE_PORT 0 317# define EV_USE_PORT 0
318#endif 318#endif
319 319
320#ifndef EV_USE_LINUXAIO
321# define EV_USE_LINUXAIO 0
322#endif
323
320#ifndef EV_USE_INOTIFY 324#ifndef EV_USE_INOTIFY
321# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 325# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
322# define EV_USE_INOTIFY EV_FEATURE_OS 326# define EV_USE_INOTIFY EV_FEATURE_OS
323# else 327# else
324# define EV_USE_INOTIFY 0 328# define EV_USE_INOTIFY 0
379/* aix's poll.h seems to cause lots of trouble */ 383/* aix's poll.h seems to cause lots of trouble */
380#ifdef _AIX 384#ifdef _AIX
381/* AIX has a completely broken poll.h header */ 385/* AIX has a completely broken poll.h header */
382# undef EV_USE_POLL 386# undef EV_USE_POLL
383# define EV_USE_POLL 0 387# define EV_USE_POLL 0
388#endif
389
390#if EV_USE_LINUXAIO
391# include <linux/aio_abi.h> /* probably only needed for aio_context_t */
384#endif 392#endif
385 393
386/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 394/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
387/* which makes programs even slower. might work on other unices, too. */ 395/* which makes programs even slower. might work on other unices, too. */
388#if EV_USE_CLOCK_SYSCALL 396#if EV_USE_CLOCK_SYSCALL
1543# define ABSPRI(w) (((W)w), 0) 1551# define ABSPRI(w) (((W)w), 0)
1544#else 1552#else
1545# define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 1553# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
1546#endif 1554#endif
1547 1555
1548#define EMPTY /* required for microsofts broken pseudo-c compiler */ 1556#define EMPTY /* required for microsofts broken pseudo-c compiler */
1549#define EMPTY2(a,b) /* used to suppress some warnings */
1550 1557
1551typedef ev_watcher *W; 1558typedef ev_watcher *W;
1552typedef ev_watcher_list *WL; 1559typedef ev_watcher_list *WL;
1553typedef ev_watcher_time *WT; 1560typedef ev_watcher_time *WT;
1554 1561
1770typedef struct 1777typedef struct
1771{ 1778{
1772 WL head; 1779 WL head;
1773 unsigned char events; /* the events watched for */ 1780 unsigned char events; /* the events watched for */
1774 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ 1781 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
1775 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 1782 unsigned char emask; /* some backends store the actual kernel mask in here */
1776 unsigned char unused; 1783 unsigned char unused;
1777#if EV_USE_EPOLL 1784#if EV_USE_EPOLL
1778 unsigned int egen; /* generation counter to counter epoll bugs */ 1785 unsigned int egen; /* generation counter to counter epoll bugs */
1779#endif 1786#endif
1780#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP 1787#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1961{ 1968{
1962 *cur = array_nextsize (elem, *cur, cnt); 1969 *cur = array_nextsize (elem, *cur, cnt);
1963 return ev_realloc (base, elem * *cur); 1970 return ev_realloc (base, elem * *cur);
1964} 1971}
1965 1972
1973#define array_needsize_noinit(base,count)
1974
1966#define array_init_zero(base,count) \ 1975#define array_needsize_zerofill(base,count) \
1967 memset ((void *)(base), 0, sizeof (*(base)) * (count)) 1976 memset ((void *)(base), 0, sizeof (*(base)) * (count))
1968 1977
1969#define array_needsize(type,base,cur,cnt,init) \ 1978#define array_needsize(type,base,cur,cnt,init) \
1970 if (expect_false ((cnt) > (cur))) \ 1979 if (expect_false ((cnt) > (cur))) \
1971 { \ 1980 { \
2007 if (expect_false (w_->pending)) 2016 if (expect_false (w_->pending))
2008 pendings [pri][w_->pending - 1].events |= revents; 2017 pendings [pri][w_->pending - 1].events |= revents;
2009 else 2018 else
2010 { 2019 {
2011 w_->pending = ++pendingcnt [pri]; 2020 w_->pending = ++pendingcnt [pri];
2012 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 2021 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, array_needsize_noinit);
2013 pendings [pri][w_->pending - 1].w = w_; 2022 pendings [pri][w_->pending - 1].w = w_;
2014 pendings [pri][w_->pending - 1].events = revents; 2023 pendings [pri][w_->pending - 1].events = revents;
2015 } 2024 }
2016 2025
2017 pendingpri = NUMPRI - 1; 2026 pendingpri = NUMPRI - 1;
2018} 2027}
2019 2028
2020inline_speed void 2029inline_speed void
2021feed_reverse (EV_P_ W w) 2030feed_reverse (EV_P_ W w)
2022{ 2031{
2023 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2); 2032 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, array_needsize_noinit);
2024 rfeeds [rfeedcnt++] = w; 2033 rfeeds [rfeedcnt++] = w;
2025} 2034}
2026 2035
2027inline_size void 2036inline_size void
2028feed_reverse_done (EV_P_ int revents) 2037feed_reverse_done (EV_P_ int revents)
2146 anfds [fd].reify |= flags; 2155 anfds [fd].reify |= flags;
2147 2156
2148 if (expect_true (!reify)) 2157 if (expect_true (!reify))
2149 { 2158 {
2150 ++fdchangecnt; 2159 ++fdchangecnt;
2151 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 2160 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, array_needsize_noinit);
2152 fdchanges [fdchangecnt - 1] = fd; 2161 fdchanges [fdchangecnt - 1] = fd;
2153 } 2162 }
2154} 2163}
2155 2164
2156/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ 2165/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
2703# include "ev_port.c" 2712# include "ev_port.c"
2704#endif 2713#endif
2705#if EV_USE_KQUEUE 2714#if EV_USE_KQUEUE
2706# include "ev_kqueue.c" 2715# include "ev_kqueue.c"
2707#endif 2716#endif
2717#if EV_USE_LINUXAIO
2718# include "ev_linuxaio.c"
2719#endif
2708#if EV_USE_EPOLL 2720#if EV_USE_EPOLL
2709# include "ev_epoll.c" 2721# include "ev_epoll.c"
2710#endif 2722#endif
2711#if EV_USE_POLL 2723#if EV_USE_POLL
2712# include "ev_poll.c" 2724# include "ev_poll.c"
2743unsigned int 2755unsigned int
2744ev_supported_backends (void) EV_NOEXCEPT 2756ev_supported_backends (void) EV_NOEXCEPT
2745{ 2757{
2746 unsigned int flags = 0; 2758 unsigned int flags = 0;
2747 2759
2748 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2760 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2749 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2761 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE;
2750 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 2762 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
2763 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO;
2751 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 2764 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
2752 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; 2765 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT;
2753 2766
2754 return flags; 2767 return flags;
2755} 2768}
2756 2769
2757ecb_cold 2770ecb_cold
2916 2929
2917 if (!(flags & EVBACKEND_MASK)) 2930 if (!(flags & EVBACKEND_MASK))
2918 flags |= ev_recommended_backends (); 2931 flags |= ev_recommended_backends ();
2919 2932
2920#if EV_USE_IOCP 2933#if EV_USE_IOCP
2921 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); 2934 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
2922#endif 2935#endif
2923#if EV_USE_PORT 2936#if EV_USE_PORT
2924 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 2937 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
2925#endif 2938#endif
2926#if EV_USE_KQUEUE 2939#if EV_USE_KQUEUE
2927 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 2940 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags);
2941#endif
2942#if EV_USE_LINUXAIO
2943 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags);
2928#endif 2944#endif
2929#if EV_USE_EPOLL 2945#if EV_USE_EPOLL
2930 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); 2946 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
2931#endif 2947#endif
2932#if EV_USE_POLL 2948#if EV_USE_POLL
2933 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); 2949 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
2934#endif 2950#endif
2935#if EV_USE_SELECT 2951#if EV_USE_SELECT
2936 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 2952 if (!backend && (flags & EVBACKEND_SELECT )) backend = select_init (EV_A_ flags);
2937#endif 2953#endif
2938 2954
2939 ev_prepare_init (&pending_w, pendingcb); 2955 ev_prepare_init (&pending_w, pendingcb);
2940 2956
2941#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 2957#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2996 3012
2997 if (backend_fd >= 0) 3013 if (backend_fd >= 0)
2998 close (backend_fd); 3014 close (backend_fd);
2999 3015
3000#if EV_USE_IOCP 3016#if EV_USE_IOCP
3001 if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A); 3017 if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
3002#endif 3018#endif
3003#if EV_USE_PORT 3019#if EV_USE_PORT
3004 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 3020 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
3005#endif 3021#endif
3006#if EV_USE_KQUEUE 3022#if EV_USE_KQUEUE
3007 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 3023 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A);
3024#endif
3025#if EV_USE_LINUXAIO
3026 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A);
3008#endif 3027#endif
3009#if EV_USE_EPOLL 3028#if EV_USE_EPOLL
3010 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); 3029 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
3011#endif 3030#endif
3012#if EV_USE_POLL 3031#if EV_USE_POLL
3013 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); 3032 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
3014#endif 3033#endif
3015#if EV_USE_SELECT 3034#if EV_USE_SELECT
3016 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 3035 if (backend == EVBACKEND_SELECT ) select_destroy (EV_A);
3017#endif 3036#endif
3018 3037
3019 for (i = NUMPRI; i--; ) 3038 for (i = NUMPRI; i--; )
3020 { 3039 {
3021 array_free (pending, [i]); 3040 array_free (pending, [i]);
3063 3082
3064inline_size void 3083inline_size void
3065loop_fork (EV_P) 3084loop_fork (EV_P)
3066{ 3085{
3067#if EV_USE_PORT 3086#if EV_USE_PORT
3068 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 3087 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
3069#endif 3088#endif
3070#if EV_USE_KQUEUE 3089#if EV_USE_KQUEUE
3071 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 3090 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A);
3091#endif
3092#if EV_USE_LINUXAIO
3093 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A);
3072#endif 3094#endif
3073#if EV_USE_EPOLL 3095#if EV_USE_EPOLL
3074 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 3096 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
3075#endif 3097#endif
3076#if EV_USE_INOTIFY 3098#if EV_USE_INOTIFY
3077 infy_fork (EV_A); 3099 infy_fork (EV_A);
3078#endif 3100#endif
3079 3101
3876 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); 3898 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
3877 3899
3878 EV_FREQUENT_CHECK; 3900 EV_FREQUENT_CHECK;
3879 3901
3880 ev_start (EV_A_ (W)w, 1); 3902 ev_start (EV_A_ (W)w, 1);
3881 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3903 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_needsize_zerofill);
3882 wlist_add (&anfds[fd].head, (WL)w); 3904 wlist_add (&anfds[fd].head, (WL)w);
3883 3905
3884 /* common bug, apparently */ 3906 /* common bug, apparently */
3885 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); 3907 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3886 3908
3923 3945
3924 EV_FREQUENT_CHECK; 3946 EV_FREQUENT_CHECK;
3925 3947
3926 ++timercnt; 3948 ++timercnt;
3927 ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1); 3949 ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1);
3928 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2); 3950 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, array_needsize_noinit);
3929 ANHE_w (timers [ev_active (w)]) = (WT)w; 3951 ANHE_w (timers [ev_active (w)]) = (WT)w;
3930 ANHE_at_cache (timers [ev_active (w)]); 3952 ANHE_at_cache (timers [ev_active (w)]);
3931 upheap (timers, ev_active (w)); 3953 upheap (timers, ev_active (w));
3932 3954
3933 EV_FREQUENT_CHECK; 3955 EV_FREQUENT_CHECK;
4020 4042
4021 EV_FREQUENT_CHECK; 4043 EV_FREQUENT_CHECK;
4022 4044
4023 ++periodiccnt; 4045 ++periodiccnt;
4024 ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1); 4046 ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1);
4025 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2); 4047 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, array_needsize_noinit);
4026 ANHE_w (periodics [ev_active (w)]) = (WT)w; 4048 ANHE_w (periodics [ev_active (w)]) = (WT)w;
4027 ANHE_at_cache (periodics [ev_active (w)]); 4049 ANHE_at_cache (periodics [ev_active (w)]);
4028 upheap (periodics, ev_active (w)); 4050 upheap (periodics, ev_active (w));
4029 4051
4030 EV_FREQUENT_CHECK; 4052 EV_FREQUENT_CHECK;
4615 int active = ++idlecnt [ABSPRI (w)]; 4637 int active = ++idlecnt [ABSPRI (w)];
4616 4638
4617 ++idleall; 4639 ++idleall;
4618 ev_start (EV_A_ (W)w, active); 4640 ev_start (EV_A_ (W)w, active);
4619 4641
4620 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); 4642 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, array_needsize_noinit);
4621 idles [ABSPRI (w)][active - 1] = w; 4643 idles [ABSPRI (w)][active - 1] = w;
4622 } 4644 }
4623 4645
4624 EV_FREQUENT_CHECK; 4646 EV_FREQUENT_CHECK;
4625} 4647}
4655 return; 4677 return;
4656 4678
4657 EV_FREQUENT_CHECK; 4679 EV_FREQUENT_CHECK;
4658 4680
4659 ev_start (EV_A_ (W)w, ++preparecnt); 4681 ev_start (EV_A_ (W)w, ++preparecnt);
4660 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 4682 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, array_needsize_noinit);
4661 prepares [preparecnt - 1] = w; 4683 prepares [preparecnt - 1] = w;
4662 4684
4663 EV_FREQUENT_CHECK; 4685 EV_FREQUENT_CHECK;
4664} 4686}
4665 4687
4693 return; 4715 return;
4694 4716
4695 EV_FREQUENT_CHECK; 4717 EV_FREQUENT_CHECK;
4696 4718
4697 ev_start (EV_A_ (W)w, ++checkcnt); 4719 ev_start (EV_A_ (W)w, ++checkcnt);
4698 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); 4720 array_needsize (ev_check *, checks, checkmax, checkcnt, array_needsize_noinit);
4699 checks [checkcnt - 1] = w; 4721 checks [checkcnt - 1] = w;
4700 4722
4701 EV_FREQUENT_CHECK; 4723 EV_FREQUENT_CHECK;
4702} 4724}
4703 4725
4841 return; 4863 return;
4842 4864
4843 EV_FREQUENT_CHECK; 4865 EV_FREQUENT_CHECK;
4844 4866
4845 ev_start (EV_A_ (W)w, ++forkcnt); 4867 ev_start (EV_A_ (W)w, ++forkcnt);
4846 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); 4868 array_needsize (ev_fork *, forks, forkmax, forkcnt, array_needsize_noinit);
4847 forks [forkcnt - 1] = w; 4869 forks [forkcnt - 1] = w;
4848 4870
4849 EV_FREQUENT_CHECK; 4871 EV_FREQUENT_CHECK;
4850} 4872}
4851 4873
4879 return; 4901 return;
4880 4902
4881 EV_FREQUENT_CHECK; 4903 EV_FREQUENT_CHECK;
4882 4904
4883 ev_start (EV_A_ (W)w, ++cleanupcnt); 4905 ev_start (EV_A_ (W)w, ++cleanupcnt);
4884 array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); 4906 array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, array_needsize_noinit);
4885 cleanups [cleanupcnt - 1] = w; 4907 cleanups [cleanupcnt - 1] = w;
4886 4908
4887 /* cleanup watchers should never keep a refcount on the loop */ 4909 /* cleanup watchers should never keep a refcount on the loop */
4888 ev_unref (EV_A); 4910 ev_unref (EV_A);
4889 EV_FREQUENT_CHECK; 4911 EV_FREQUENT_CHECK;
4924 evpipe_init (EV_A); 4946 evpipe_init (EV_A);
4925 4947
4926 EV_FREQUENT_CHECK; 4948 EV_FREQUENT_CHECK;
4927 4949
4928 ev_start (EV_A_ (W)w, ++asynccnt); 4950 ev_start (EV_A_ (W)w, ++asynccnt);
4929 array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2); 4951 array_needsize (ev_async *, asyncs, asyncmax, asynccnt, array_needsize_noinit);
4930 asyncs [asynccnt - 1] = w; 4952 asyncs [asynccnt - 1] = w;
4931 4953
4932 EV_FREQUENT_CHECK; 4954 EV_FREQUENT_CHECK;
4933} 4955}
4934 4956

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines