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.498 by root, Wed Jun 26 00:01:46 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 *
115# else 115# else
116# undef EV_USE_EPOLL 116# undef EV_USE_EPOLL
117# define EV_USE_EPOLL 0 117# define EV_USE_EPOLL 0
118# endif 118# endif
119 119
120# if HAVE_LINUX_AIO_ABI_H
121# ifndef EV_USE_LINUXAIO
122# define EV_USE_LINUXAIO EV_FEATURE_BACKENDS
123# endif
124# else
125# undef EV_USE_LINUXAIO
126# define EV_USE_LINUXAIO 0
127# endif
128
120# if HAVE_KQUEUE && HAVE_SYS_EVENT_H 129# if HAVE_KQUEUE && HAVE_SYS_EVENT_H
121# ifndef EV_USE_KQUEUE 130# ifndef EV_USE_KQUEUE
122# define EV_USE_KQUEUE EV_FEATURE_BACKENDS 131# define EV_USE_KQUEUE EV_FEATURE_BACKENDS
123# endif 132# endif
124# else 133# else
315 324
316#ifndef EV_USE_PORT 325#ifndef EV_USE_PORT
317# define EV_USE_PORT 0 326# define EV_USE_PORT 0
318#endif 327#endif
319 328
329#ifndef EV_USE_LINUXAIO
330# if __linux /* libev currently assumes linux/aio_abi.h is always available on linux */
331# define EV_USE_LINUXAIO 1
332# else
333# define EV_USE_LINUXAIO 0
334# endif
335#endif
336
320#ifndef EV_USE_INOTIFY 337#ifndef EV_USE_INOTIFY
321# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 338# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
322# define EV_USE_INOTIFY EV_FEATURE_OS 339# define EV_USE_INOTIFY EV_FEATURE_OS
323# else 340# else
324# define EV_USE_INOTIFY 0 341# define EV_USE_INOTIFY 0
416 433
417#if !EV_USE_NANOSLEEP 434#if !EV_USE_NANOSLEEP
418/* hp-ux has it in sys/time.h, which we unconditionally include above */ 435/* hp-ux has it in sys/time.h, which we unconditionally include above */
419# if !defined _WIN32 && !defined __hpux 436# if !defined _WIN32 && !defined __hpux
420# include <sys/select.h> 437# include <sys/select.h>
438# endif
439#endif
440
441#if EV_USE_LINUXAIO
442# include <sys/syscall.h>
443# if !SYS_io_getevents || !EV_USE_EPOLL /* ev_linxaio uses ev_poll.c:ev_epoll_create */
444# undef EV_USE_LINUXAIO
445# define EV_USE_LINUXAIO 0
421# endif 446# endif
422#endif 447#endif
423 448
424#if EV_USE_INOTIFY 449#if EV_USE_INOTIFY
425# include <sys/statfs.h> 450# include <sys/statfs.h>
534 559
535#ifndef ECB_H 560#ifndef ECB_H
536#define ECB_H 561#define ECB_H
537 562
538/* 16 bits major, 16 bits minor */ 563/* 16 bits major, 16 bits minor */
539#define ECB_VERSION 0x00010005 564#define ECB_VERSION 0x00010006
540 565
541#ifdef _WIN32 566#ifdef _WIN32
542 typedef signed char int8_t; 567 typedef signed char int8_t;
543 typedef unsigned char uint8_t; 568 typedef unsigned char uint8_t;
544 typedef signed short int16_t; 569 typedef signed short int16_t;
658 #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ 683 #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
659#endif 684#endif
660 685
661#ifndef ECB_MEMORY_FENCE 686#ifndef ECB_MEMORY_FENCE
662 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 687 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
688 #define ECB_MEMORY_FENCE_RELAXED __asm__ __volatile__ ("" : : : "memory")
663 #if __i386 || __i386__ 689 #if __i386 || __i386__
664 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 690 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
665 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") 691 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
666 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") 692 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory")
667 #elif ECB_GCC_AMD64 693 #elif ECB_GCC_AMD64
717 #if ECB_GCC_VERSION(4,7) 743 #if ECB_GCC_VERSION(4,7)
718 /* see comment below (stdatomic.h) about the C11 memory model. */ 744 /* see comment below (stdatomic.h) about the C11 memory model. */
719 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) 745 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
720 #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) 746 #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE)
721 #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) 747 #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE)
748 #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED)
722 749
723 #elif ECB_CLANG_EXTENSION(c_atomic) 750 #elif ECB_CLANG_EXTENSION(c_atomic)
724 /* see comment below (stdatomic.h) about the C11 memory model. */ 751 /* see comment below (stdatomic.h) about the C11 memory model. */
725 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) 752 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
726 #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) 753 #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE)
727 #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) 754 #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE)
755 #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED)
728 756
729 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 757 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
730 #define ECB_MEMORY_FENCE __sync_synchronize () 758 #define ECB_MEMORY_FENCE __sync_synchronize ()
731 #elif _MSC_VER >= 1500 /* VC++ 2008 */ 759 #elif _MSC_VER >= 1500 /* VC++ 2008 */
732 /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ 760 /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */
742 #elif defined _WIN32 770 #elif defined _WIN32
743 #include <WinNT.h> 771 #include <WinNT.h>
744 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ 772 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
745 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 773 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
746 #include <mbarrier.h> 774 #include <mbarrier.h>
747 #define ECB_MEMORY_FENCE __machine_rw_barrier () 775 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
748 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () 776 #define ECB_MEMORY_FENCE_ACQUIRE __machine_acq_barrier ()
749 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () 777 #define ECB_MEMORY_FENCE_RELEASE __machine_rel_barrier ()
778 #define ECB_MEMORY_FENCE_RELAXED __compiler_barrier ()
750 #elif __xlC__ 779 #elif __xlC__
751 #define ECB_MEMORY_FENCE __sync () 780 #define ECB_MEMORY_FENCE __sync ()
752 #endif 781 #endif
753#endif 782#endif
754 783
755#ifndef ECB_MEMORY_FENCE 784#ifndef ECB_MEMORY_FENCE
756 #if ECB_C11 && !defined __STDC_NO_ATOMICS__ 785 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
757 /* we assume that these memory fences work on all variables/all memory accesses, */ 786 /* we assume that these memory fences work on all variables/all memory accesses, */
758 /* not just C11 atomics and atomic accesses */ 787 /* not just C11 atomics and atomic accesses */
759 #include <stdatomic.h> 788 #include <stdatomic.h>
760 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
761 /* any fence other than seq_cst, which isn't very efficient for us. */
762 /* Why that is, we don't know - either the C11 memory model is quite useless */
763 /* for most usages, or gcc and clang have a bug */
764 /* I *currently* lean towards the latter, and inefficiently implement */
765 /* all three of ecb's fences as a seq_cst fence */
766 /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */
767 /* for all __atomic_thread_fence's except seq_cst */
768 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) 789 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
790 #define ECB_MEMORY_FENCE_ACQUIRE atomic_thread_fence (memory_order_acquire)
791 #define ECB_MEMORY_FENCE_RELEASE atomic_thread_fence (memory_order_release)
769 #endif 792 #endif
770#endif 793#endif
771 794
772#ifndef ECB_MEMORY_FENCE 795#ifndef ECB_MEMORY_FENCE
773 #if !ECB_AVOID_PTHREADS 796 #if !ECB_AVOID_PTHREADS
791 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 814 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
792#endif 815#endif
793 816
794#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE 817#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
795 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 818 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
819#endif
820
821#if !defined ECB_MEMORY_FENCE_RELAXED && defined ECB_MEMORY_FENCE
822 #define ECB_MEMORY_FENCE_RELAXED ECB_MEMORY_FENCE /* very heavy-handed */
796#endif 823#endif
797 824
798/*****************************************************************************/ 825/*****************************************************************************/
799 826
800#if ECB_CPP 827#if ECB_CPP
1543# define ABSPRI(w) (((W)w), 0) 1570# define ABSPRI(w) (((W)w), 0)
1544#else 1571#else
1545# define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 1572# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
1546#endif 1573#endif
1547 1574
1548#define EMPTY /* required for microsofts broken pseudo-c compiler */ 1575#define EMPTY /* required for microsofts broken pseudo-c compiler */
1549#define EMPTY2(a,b) /* used to suppress some warnings */
1550 1576
1551typedef ev_watcher *W; 1577typedef ev_watcher *W;
1552typedef ev_watcher_list *WL; 1578typedef ev_watcher_list *WL;
1553typedef ev_watcher_time *WT; 1579typedef ev_watcher_time *WT;
1554 1580
1578#ifdef _WIN32 1604#ifdef _WIN32
1579# include "ev_win32.c" 1605# include "ev_win32.c"
1580#endif 1606#endif
1581 1607
1582/*****************************************************************************/ 1608/*****************************************************************************/
1609
1610#if EV_USE_LINUXAIO
1611# include <linux/aio_abi.h> /* probably only needed for aio_context_t */
1612#endif
1583 1613
1584/* define a suitable floor function (only used by periodics atm) */ 1614/* define a suitable floor function (only used by periodics atm) */
1585 1615
1586#if EV_USE_FLOOR 1616#if EV_USE_FLOOR
1587# include <math.h> 1617# include <math.h>
1770typedef struct 1800typedef struct
1771{ 1801{
1772 WL head; 1802 WL head;
1773 unsigned char events; /* the events watched for */ 1803 unsigned char events; /* the events watched for */
1774 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ 1804 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 */ 1805 unsigned char emask; /* some backends store the actual kernel mask in here */
1776 unsigned char unused; 1806 unsigned char unused;
1777#if EV_USE_EPOLL 1807#if EV_USE_EPOLL
1778 unsigned int egen; /* generation counter to counter epoll bugs */ 1808 unsigned int egen; /* generation counter to counter epoll bugs */
1779#endif 1809#endif
1780#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP 1810#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1961{ 1991{
1962 *cur = array_nextsize (elem, *cur, cnt); 1992 *cur = array_nextsize (elem, *cur, cnt);
1963 return ev_realloc (base, elem * *cur); 1993 return ev_realloc (base, elem * *cur);
1964} 1994}
1965 1995
1996#define array_needsize_noinit(base,offset,count)
1997
1966#define array_init_zero(base,count) \ 1998#define array_needsize_zerofill(base,offset,count) \
1967 memset ((void *)(base), 0, sizeof (*(base)) * (count)) 1999 memset ((void *)(base + offset), 0, sizeof (*(base)) * (count))
1968 2000
1969#define array_needsize(type,base,cur,cnt,init) \ 2001#define array_needsize(type,base,cur,cnt,init) \
1970 if (expect_false ((cnt) > (cur))) \ 2002 if (expect_false ((cnt) > (cur))) \
1971 { \ 2003 { \
1972 ecb_unused int ocur_ = (cur); \ 2004 ecb_unused int ocur_ = (cur); \
1973 (base) = (type *)array_realloc \ 2005 (base) = (type *)array_realloc \
1974 (sizeof (type), (base), &(cur), (cnt)); \ 2006 (sizeof (type), (base), &(cur), (cnt)); \
1975 init ((base) + (ocur_), (cur) - ocur_); \ 2007 init ((base), ocur_, ((cur) - ocur_)); \
1976 } 2008 }
1977 2009
1978#if 0 2010#if 0
1979#define array_slim(type,stem) \ 2011#define array_slim(type,stem) \
1980 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 2012 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
2007 if (expect_false (w_->pending)) 2039 if (expect_false (w_->pending))
2008 pendings [pri][w_->pending - 1].events |= revents; 2040 pendings [pri][w_->pending - 1].events |= revents;
2009 else 2041 else
2010 { 2042 {
2011 w_->pending = ++pendingcnt [pri]; 2043 w_->pending = ++pendingcnt [pri];
2012 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 2044 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, array_needsize_noinit);
2013 pendings [pri][w_->pending - 1].w = w_; 2045 pendings [pri][w_->pending - 1].w = w_;
2014 pendings [pri][w_->pending - 1].events = revents; 2046 pendings [pri][w_->pending - 1].events = revents;
2015 } 2047 }
2016 2048
2017 pendingpri = NUMPRI - 1; 2049 pendingpri = NUMPRI - 1;
2018} 2050}
2019 2051
2020inline_speed void 2052inline_speed void
2021feed_reverse (EV_P_ W w) 2053feed_reverse (EV_P_ W w)
2022{ 2054{
2023 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2); 2055 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, array_needsize_noinit);
2024 rfeeds [rfeedcnt++] = w; 2056 rfeeds [rfeedcnt++] = w;
2025} 2057}
2026 2058
2027inline_size void 2059inline_size void
2028feed_reverse_done (EV_P_ int revents) 2060feed_reverse_done (EV_P_ int revents)
2115 ev_io *w; 2147 ev_io *w;
2116 2148
2117 unsigned char o_events = anfd->events; 2149 unsigned char o_events = anfd->events;
2118 unsigned char o_reify = anfd->reify; 2150 unsigned char o_reify = anfd->reify;
2119 2151
2120 anfd->reify = 0; 2152 anfd->reify = 0;
2121 2153
2122 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ 2154 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
2123 { 2155 {
2124 anfd->events = 0; 2156 anfd->events = 0;
2125 2157
2146 anfds [fd].reify |= flags; 2178 anfds [fd].reify |= flags;
2147 2179
2148 if (expect_true (!reify)) 2180 if (expect_true (!reify))
2149 { 2181 {
2150 ++fdchangecnt; 2182 ++fdchangecnt;
2151 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 2183 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, array_needsize_noinit);
2152 fdchanges [fdchangecnt - 1] = fd; 2184 fdchanges [fdchangecnt - 1] = fd;
2153 } 2185 }
2154} 2186}
2155 2187
2156/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ 2188/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
2706# include "ev_kqueue.c" 2738# include "ev_kqueue.c"
2707#endif 2739#endif
2708#if EV_USE_EPOLL 2740#if EV_USE_EPOLL
2709# include "ev_epoll.c" 2741# include "ev_epoll.c"
2710#endif 2742#endif
2743#if EV_USE_LINUXAIO
2744# include "ev_linuxaio.c"
2745#endif
2711#if EV_USE_POLL 2746#if EV_USE_POLL
2712# include "ev_poll.c" 2747# include "ev_poll.c"
2713#endif 2748#endif
2714#if EV_USE_SELECT 2749#if EV_USE_SELECT
2715# include "ev_select.c" 2750# include "ev_select.c"
2743unsigned int 2778unsigned int
2744ev_supported_backends (void) EV_NOEXCEPT 2779ev_supported_backends (void) EV_NOEXCEPT
2745{ 2780{
2746 unsigned int flags = 0; 2781 unsigned int flags = 0;
2747 2782
2748 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2783 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2749 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2784 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE;
2750 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 2785 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
2786 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO;
2751 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 2787 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
2752 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; 2788 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT;
2753 2789
2754 return flags; 2790 return flags;
2755} 2791}
2756 2792
2757ecb_cold 2793ecb_cold
2770 flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ 2806 flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
2771 flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ 2807 flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
2772#endif 2808#endif
2773#ifdef __FreeBSD__ 2809#ifdef __FreeBSD__
2774 flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */ 2810 flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
2811#endif
2812
2813 /* TODO: linuxaio is very experimental */
2814#if !EV_RECOMMEND_LINUXAIO
2815 flags &= ~EVBACKEND_LINUXAIO;
2775#endif 2816#endif
2776 2817
2777 return flags; 2818 return flags;
2778} 2819}
2779 2820
2916 2957
2917 if (!(flags & EVBACKEND_MASK)) 2958 if (!(flags & EVBACKEND_MASK))
2918 flags |= ev_recommended_backends (); 2959 flags |= ev_recommended_backends ();
2919 2960
2920#if EV_USE_IOCP 2961#if EV_USE_IOCP
2921 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); 2962 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
2922#endif 2963#endif
2923#if EV_USE_PORT 2964#if EV_USE_PORT
2924 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 2965 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
2925#endif 2966#endif
2926#if EV_USE_KQUEUE 2967#if EV_USE_KQUEUE
2927 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 2968 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags);
2969#endif
2970#if EV_USE_LINUXAIO
2971 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags);
2928#endif 2972#endif
2929#if EV_USE_EPOLL 2973#if EV_USE_EPOLL
2930 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); 2974 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
2931#endif 2975#endif
2932#if EV_USE_POLL 2976#if EV_USE_POLL
2933 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); 2977 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
2934#endif 2978#endif
2935#if EV_USE_SELECT 2979#if EV_USE_SELECT
2936 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 2980 if (!backend && (flags & EVBACKEND_SELECT )) backend = select_init (EV_A_ flags);
2937#endif 2981#endif
2938 2982
2939 ev_prepare_init (&pending_w, pendingcb); 2983 ev_prepare_init (&pending_w, pendingcb);
2940 2984
2941#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 2985#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2996 3040
2997 if (backend_fd >= 0) 3041 if (backend_fd >= 0)
2998 close (backend_fd); 3042 close (backend_fd);
2999 3043
3000#if EV_USE_IOCP 3044#if EV_USE_IOCP
3001 if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A); 3045 if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
3002#endif 3046#endif
3003#if EV_USE_PORT 3047#if EV_USE_PORT
3004 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 3048 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
3005#endif 3049#endif
3006#if EV_USE_KQUEUE 3050#if EV_USE_KQUEUE
3007 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 3051 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A);
3052#endif
3053#if EV_USE_LINUXAIO
3054 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A);
3008#endif 3055#endif
3009#if EV_USE_EPOLL 3056#if EV_USE_EPOLL
3010 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); 3057 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
3011#endif 3058#endif
3012#if EV_USE_POLL 3059#if EV_USE_POLL
3013 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); 3060 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
3014#endif 3061#endif
3015#if EV_USE_SELECT 3062#if EV_USE_SELECT
3016 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 3063 if (backend == EVBACKEND_SELECT ) select_destroy (EV_A);
3017#endif 3064#endif
3018 3065
3019 for (i = NUMPRI; i--; ) 3066 for (i = NUMPRI; i--; )
3020 { 3067 {
3021 array_free (pending, [i]); 3068 array_free (pending, [i]);
3063 3110
3064inline_size void 3111inline_size void
3065loop_fork (EV_P) 3112loop_fork (EV_P)
3066{ 3113{
3067#if EV_USE_PORT 3114#if EV_USE_PORT
3068 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 3115 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
3069#endif 3116#endif
3070#if EV_USE_KQUEUE 3117#if EV_USE_KQUEUE
3071 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 3118 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A);
3119#endif
3120#if EV_USE_LINUXAIO
3121 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A);
3072#endif 3122#endif
3073#if EV_USE_EPOLL 3123#if EV_USE_EPOLL
3074 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 3124 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
3075#endif 3125#endif
3076#if EV_USE_INOTIFY 3126#if EV_USE_INOTIFY
3077 infy_fork (EV_A); 3127 infy_fork (EV_A);
3078#endif 3128#endif
3079 3129
3873 return; 3923 return;
3874 3924
3875 assert (("libev: ev_io_start called with negative fd", fd >= 0)); 3925 assert (("libev: ev_io_start called with negative fd", fd >= 0));
3876 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); 3926 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
3877 3927
3928#if EV_VERIFY >= 2
3929 assert (("libev: ev_io_start called on watcher with invalid fd", fd_valid (fd)));
3930#endif
3878 EV_FREQUENT_CHECK; 3931 EV_FREQUENT_CHECK;
3879 3932
3880 ev_start (EV_A_ (W)w, 1); 3933 ev_start (EV_A_ (W)w, 1);
3881 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3934 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_needsize_zerofill);
3882 wlist_add (&anfds[fd].head, (WL)w); 3935 wlist_add (&anfds[fd].head, (WL)w);
3883 3936
3884 /* common bug, apparently */ 3937 /* common bug, apparently */
3885 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); 3938 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3886 3939
3898 if (expect_false (!ev_is_active (w))) 3951 if (expect_false (!ev_is_active (w)))
3899 return; 3952 return;
3900 3953
3901 assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 3954 assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
3902 3955
3956#if EV_VERIFY >= 2
3957 assert (("libev: ev_io_stop called on watcher with invalid fd", fd_valid (w->fd)));
3958#endif
3903 EV_FREQUENT_CHECK; 3959 EV_FREQUENT_CHECK;
3904 3960
3905 wlist_del (&anfds[w->fd].head, (WL)w); 3961 wlist_del (&anfds[w->fd].head, (WL)w);
3906 ev_stop (EV_A_ (W)w); 3962 ev_stop (EV_A_ (W)w);
3907 3963
3923 3979
3924 EV_FREQUENT_CHECK; 3980 EV_FREQUENT_CHECK;
3925 3981
3926 ++timercnt; 3982 ++timercnt;
3927 ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1); 3983 ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1);
3928 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2); 3984 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, array_needsize_noinit);
3929 ANHE_w (timers [ev_active (w)]) = (WT)w; 3985 ANHE_w (timers [ev_active (w)]) = (WT)w;
3930 ANHE_at_cache (timers [ev_active (w)]); 3986 ANHE_at_cache (timers [ev_active (w)]);
3931 upheap (timers, ev_active (w)); 3987 upheap (timers, ev_active (w));
3932 3988
3933 EV_FREQUENT_CHECK; 3989 EV_FREQUENT_CHECK;
4020 4076
4021 EV_FREQUENT_CHECK; 4077 EV_FREQUENT_CHECK;
4022 4078
4023 ++periodiccnt; 4079 ++periodiccnt;
4024 ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1); 4080 ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1);
4025 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2); 4081 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, array_needsize_noinit);
4026 ANHE_w (periodics [ev_active (w)]) = (WT)w; 4082 ANHE_w (periodics [ev_active (w)]) = (WT)w;
4027 ANHE_at_cache (periodics [ev_active (w)]); 4083 ANHE_at_cache (periodics [ev_active (w)]);
4028 upheap (periodics, ev_active (w)); 4084 upheap (periodics, ev_active (w));
4029 4085
4030 EV_FREQUENT_CHECK; 4086 EV_FREQUENT_CHECK;
4615 int active = ++idlecnt [ABSPRI (w)]; 4671 int active = ++idlecnt [ABSPRI (w)];
4616 4672
4617 ++idleall; 4673 ++idleall;
4618 ev_start (EV_A_ (W)w, active); 4674 ev_start (EV_A_ (W)w, active);
4619 4675
4620 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); 4676 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, array_needsize_noinit);
4621 idles [ABSPRI (w)][active - 1] = w; 4677 idles [ABSPRI (w)][active - 1] = w;
4622 } 4678 }
4623 4679
4624 EV_FREQUENT_CHECK; 4680 EV_FREQUENT_CHECK;
4625} 4681}
4655 return; 4711 return;
4656 4712
4657 EV_FREQUENT_CHECK; 4713 EV_FREQUENT_CHECK;
4658 4714
4659 ev_start (EV_A_ (W)w, ++preparecnt); 4715 ev_start (EV_A_ (W)w, ++preparecnt);
4660 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 4716 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, array_needsize_noinit);
4661 prepares [preparecnt - 1] = w; 4717 prepares [preparecnt - 1] = w;
4662 4718
4663 EV_FREQUENT_CHECK; 4719 EV_FREQUENT_CHECK;
4664} 4720}
4665 4721
4693 return; 4749 return;
4694 4750
4695 EV_FREQUENT_CHECK; 4751 EV_FREQUENT_CHECK;
4696 4752
4697 ev_start (EV_A_ (W)w, ++checkcnt); 4753 ev_start (EV_A_ (W)w, ++checkcnt);
4698 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); 4754 array_needsize (ev_check *, checks, checkmax, checkcnt, array_needsize_noinit);
4699 checks [checkcnt - 1] = w; 4755 checks [checkcnt - 1] = w;
4700 4756
4701 EV_FREQUENT_CHECK; 4757 EV_FREQUENT_CHECK;
4702} 4758}
4703 4759
4841 return; 4897 return;
4842 4898
4843 EV_FREQUENT_CHECK; 4899 EV_FREQUENT_CHECK;
4844 4900
4845 ev_start (EV_A_ (W)w, ++forkcnt); 4901 ev_start (EV_A_ (W)w, ++forkcnt);
4846 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); 4902 array_needsize (ev_fork *, forks, forkmax, forkcnt, array_needsize_noinit);
4847 forks [forkcnt - 1] = w; 4903 forks [forkcnt - 1] = w;
4848 4904
4849 EV_FREQUENT_CHECK; 4905 EV_FREQUENT_CHECK;
4850} 4906}
4851 4907
4879 return; 4935 return;
4880 4936
4881 EV_FREQUENT_CHECK; 4937 EV_FREQUENT_CHECK;
4882 4938
4883 ev_start (EV_A_ (W)w, ++cleanupcnt); 4939 ev_start (EV_A_ (W)w, ++cleanupcnt);
4884 array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); 4940 array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, array_needsize_noinit);
4885 cleanups [cleanupcnt - 1] = w; 4941 cleanups [cleanupcnt - 1] = w;
4886 4942
4887 /* cleanup watchers should never keep a refcount on the loop */ 4943 /* cleanup watchers should never keep a refcount on the loop */
4888 ev_unref (EV_A); 4944 ev_unref (EV_A);
4889 EV_FREQUENT_CHECK; 4945 EV_FREQUENT_CHECK;
4924 evpipe_init (EV_A); 4980 evpipe_init (EV_A);
4925 4981
4926 EV_FREQUENT_CHECK; 4982 EV_FREQUENT_CHECK;
4927 4983
4928 ev_start (EV_A_ (W)w, ++asynccnt); 4984 ev_start (EV_A_ (W)w, ++asynccnt);
4929 array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2); 4985 array_needsize (ev_async *, asyncs, asyncmax, asynccnt, array_needsize_noinit);
4930 asyncs [asynccnt - 1] = w; 4986 asyncs [asynccnt - 1] = w;
4931 4987
4932 EV_FREQUENT_CHECK; 4988 EV_FREQUENT_CHECK;
4933} 4989}
4934 4990

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines