xref: /freebsd/crypto/krb5/src/util/verto/ev.h (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /*
2*7f2fe78bSCy Schubert  * libev native API header
3*7f2fe78bSCy Schubert  *
4*7f2fe78bSCy Schubert  * Copyright (c) 2007,2008,2009,2010,2011,2012,2015 Marc Alexander Lehmann <libev@schmorp.de>
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without modifica-
8*7f2fe78bSCy Schubert  * tion, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  *   1.  Redistributions of source code must retain the above copyright notice,
11*7f2fe78bSCy Schubert  *       this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  *   2.  Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer in the
15*7f2fe78bSCy Schubert  *       documentation and/or other materials provided with the distribution.
16*7f2fe78bSCy Schubert  *
17*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18*7f2fe78bSCy Schubert  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19*7f2fe78bSCy Schubert  * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
20*7f2fe78bSCy Schubert  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21*7f2fe78bSCy Schubert  * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22*7f2fe78bSCy Schubert  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23*7f2fe78bSCy Schubert  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24*7f2fe78bSCy Schubert  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25*7f2fe78bSCy Schubert  * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
27*7f2fe78bSCy Schubert  *
28*7f2fe78bSCy Schubert  * Alternatively, the contents of this file may be used under the terms of
29*7f2fe78bSCy Schubert  * the GNU General Public License ("GPL") version 2 or any later version,
30*7f2fe78bSCy Schubert  * in which case the provisions of the GPL are applicable instead of
31*7f2fe78bSCy Schubert  * the above. If you wish to allow the use of your version of this file
32*7f2fe78bSCy Schubert  * only under the terms of the GPL and not to allow others to use your
33*7f2fe78bSCy Schubert  * version of this file under the BSD license, indicate your decision
34*7f2fe78bSCy Schubert  * by deleting the provisions above and replace them with the notice
35*7f2fe78bSCy Schubert  * and other provisions required by the GPL. If you do not delete the
36*7f2fe78bSCy Schubert  * provisions above, a recipient may use your version of this file under
37*7f2fe78bSCy Schubert  * either the BSD or the GPL.
38*7f2fe78bSCy Schubert  */
39*7f2fe78bSCy Schubert 
40*7f2fe78bSCy Schubert #ifndef EV_H_
41*7f2fe78bSCy Schubert #define EV_H_
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert #ifdef __cplusplus
44*7f2fe78bSCy Schubert # define EV_CPP(x) x
45*7f2fe78bSCy Schubert # if __cplusplus >= 201103L
46*7f2fe78bSCy Schubert #  define EV_THROW noexcept
47*7f2fe78bSCy Schubert # else
48*7f2fe78bSCy Schubert #  define EV_THROW throw ()
49*7f2fe78bSCy Schubert # endif
50*7f2fe78bSCy Schubert #else
51*7f2fe78bSCy Schubert # define EV_CPP(x)
52*7f2fe78bSCy Schubert # define EV_THROW
53*7f2fe78bSCy Schubert #endif
54*7f2fe78bSCy Schubert 
55*7f2fe78bSCy Schubert EV_CPP(extern "C" {)
56*7f2fe78bSCy Schubert 
57*7f2fe78bSCy Schubert /*****************************************************************************/
58*7f2fe78bSCy Schubert 
59*7f2fe78bSCy Schubert /* pre-4.0 compatibility */
60*7f2fe78bSCy Schubert #ifndef EV_COMPAT3
61*7f2fe78bSCy Schubert # define EV_COMPAT3 1
62*7f2fe78bSCy Schubert #endif
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert #ifndef EV_FEATURES
65*7f2fe78bSCy Schubert # if defined __OPTIMIZE_SIZE__
66*7f2fe78bSCy Schubert #  define EV_FEATURES 0x7c
67*7f2fe78bSCy Schubert # else
68*7f2fe78bSCy Schubert #  define EV_FEATURES 0x7f
69*7f2fe78bSCy Schubert # endif
70*7f2fe78bSCy Schubert #endif
71*7f2fe78bSCy Schubert 
72*7f2fe78bSCy Schubert #define EV_FEATURE_CODE     ((EV_FEATURES) &  1)
73*7f2fe78bSCy Schubert #define EV_FEATURE_DATA     ((EV_FEATURES) &  2)
74*7f2fe78bSCy Schubert #define EV_FEATURE_CONFIG   ((EV_FEATURES) &  4)
75*7f2fe78bSCy Schubert #define EV_FEATURE_API      ((EV_FEATURES) &  8)
76*7f2fe78bSCy Schubert #define EV_FEATURE_WATCHERS ((EV_FEATURES) & 16)
77*7f2fe78bSCy Schubert #define EV_FEATURE_BACKENDS ((EV_FEATURES) & 32)
78*7f2fe78bSCy Schubert #define EV_FEATURE_OS       ((EV_FEATURES) & 64)
79*7f2fe78bSCy Schubert 
80*7f2fe78bSCy Schubert /* these priorities are inclusive, higher priorities will be invoked earlier */
81*7f2fe78bSCy Schubert #ifndef EV_MINPRI
82*7f2fe78bSCy Schubert # define EV_MINPRI (EV_FEATURE_CONFIG ? -2 : 0)
83*7f2fe78bSCy Schubert #endif
84*7f2fe78bSCy Schubert #ifndef EV_MAXPRI
85*7f2fe78bSCy Schubert # define EV_MAXPRI (EV_FEATURE_CONFIG ? +2 : 0)
86*7f2fe78bSCy Schubert #endif
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert #ifndef EV_MULTIPLICITY
89*7f2fe78bSCy Schubert # define EV_MULTIPLICITY EV_FEATURE_CONFIG
90*7f2fe78bSCy Schubert #endif
91*7f2fe78bSCy Schubert 
92*7f2fe78bSCy Schubert #ifndef EV_PERIODIC_ENABLE
93*7f2fe78bSCy Schubert # define EV_PERIODIC_ENABLE EV_FEATURE_WATCHERS
94*7f2fe78bSCy Schubert #endif
95*7f2fe78bSCy Schubert 
96*7f2fe78bSCy Schubert #ifndef EV_STAT_ENABLE
97*7f2fe78bSCy Schubert # define EV_STAT_ENABLE EV_FEATURE_WATCHERS
98*7f2fe78bSCy Schubert #endif
99*7f2fe78bSCy Schubert 
100*7f2fe78bSCy Schubert #ifndef EV_PREPARE_ENABLE
101*7f2fe78bSCy Schubert # define EV_PREPARE_ENABLE EV_FEATURE_WATCHERS
102*7f2fe78bSCy Schubert #endif
103*7f2fe78bSCy Schubert 
104*7f2fe78bSCy Schubert #ifndef EV_CHECK_ENABLE
105*7f2fe78bSCy Schubert # define EV_CHECK_ENABLE EV_FEATURE_WATCHERS
106*7f2fe78bSCy Schubert #endif
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert #ifndef EV_IDLE_ENABLE
109*7f2fe78bSCy Schubert # define EV_IDLE_ENABLE EV_FEATURE_WATCHERS
110*7f2fe78bSCy Schubert #endif
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert #ifndef EV_FORK_ENABLE
113*7f2fe78bSCy Schubert # define EV_FORK_ENABLE EV_FEATURE_WATCHERS
114*7f2fe78bSCy Schubert #endif
115*7f2fe78bSCy Schubert 
116*7f2fe78bSCy Schubert #ifndef EV_CLEANUP_ENABLE
117*7f2fe78bSCy Schubert # define EV_CLEANUP_ENABLE EV_FEATURE_WATCHERS
118*7f2fe78bSCy Schubert #endif
119*7f2fe78bSCy Schubert 
120*7f2fe78bSCy Schubert #ifndef EV_SIGNAL_ENABLE
121*7f2fe78bSCy Schubert # define EV_SIGNAL_ENABLE EV_FEATURE_WATCHERS
122*7f2fe78bSCy Schubert #endif
123*7f2fe78bSCy Schubert 
124*7f2fe78bSCy Schubert #ifndef EV_CHILD_ENABLE
125*7f2fe78bSCy Schubert # ifdef _WIN32
126*7f2fe78bSCy Schubert #  define EV_CHILD_ENABLE 0
127*7f2fe78bSCy Schubert # else
128*7f2fe78bSCy Schubert #  define EV_CHILD_ENABLE EV_FEATURE_WATCHERS
129*7f2fe78bSCy Schubert #endif
130*7f2fe78bSCy Schubert #endif
131*7f2fe78bSCy Schubert 
132*7f2fe78bSCy Schubert #ifndef EV_ASYNC_ENABLE
133*7f2fe78bSCy Schubert # define EV_ASYNC_ENABLE EV_FEATURE_WATCHERS
134*7f2fe78bSCy Schubert #endif
135*7f2fe78bSCy Schubert 
136*7f2fe78bSCy Schubert #ifndef EV_EMBED_ENABLE
137*7f2fe78bSCy Schubert # define EV_EMBED_ENABLE EV_FEATURE_WATCHERS
138*7f2fe78bSCy Schubert #endif
139*7f2fe78bSCy Schubert 
140*7f2fe78bSCy Schubert #ifndef EV_WALK_ENABLE
141*7f2fe78bSCy Schubert # define EV_WALK_ENABLE 0 /* not yet */
142*7f2fe78bSCy Schubert #endif
143*7f2fe78bSCy Schubert 
144*7f2fe78bSCy Schubert /*****************************************************************************/
145*7f2fe78bSCy Schubert 
146*7f2fe78bSCy Schubert #if EV_CHILD_ENABLE && !EV_SIGNAL_ENABLE
147*7f2fe78bSCy Schubert # undef EV_SIGNAL_ENABLE
148*7f2fe78bSCy Schubert # define EV_SIGNAL_ENABLE 1
149*7f2fe78bSCy Schubert #endif
150*7f2fe78bSCy Schubert 
151*7f2fe78bSCy Schubert /*****************************************************************************/
152*7f2fe78bSCy Schubert 
153*7f2fe78bSCy Schubert typedef double ev_tstamp;
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert #include <string.h> /* for memmove */
156*7f2fe78bSCy Schubert 
157*7f2fe78bSCy Schubert #ifndef EV_ATOMIC_T
158*7f2fe78bSCy Schubert # include <signal.h>
159*7f2fe78bSCy Schubert # define EV_ATOMIC_T sig_atomic_t volatile
160*7f2fe78bSCy Schubert #endif
161*7f2fe78bSCy Schubert 
162*7f2fe78bSCy Schubert #if EV_STAT_ENABLE
163*7f2fe78bSCy Schubert # ifdef _WIN32
164*7f2fe78bSCy Schubert #  include <time.h>
165*7f2fe78bSCy Schubert #  include <sys/types.h>
166*7f2fe78bSCy Schubert # endif
167*7f2fe78bSCy Schubert # include <sys/stat.h>
168*7f2fe78bSCy Schubert #endif
169*7f2fe78bSCy Schubert 
170*7f2fe78bSCy Schubert /* support multiple event loops? */
171*7f2fe78bSCy Schubert #if EV_MULTIPLICITY
172*7f2fe78bSCy Schubert struct ev_loop;
173*7f2fe78bSCy Schubert # define EV_P  struct ev_loop *loop               /* a loop as sole parameter in a declaration */
174*7f2fe78bSCy Schubert # define EV_P_ EV_P,                              /* a loop as first of multiple parameters */
175*7f2fe78bSCy Schubert # define EV_A  loop                               /* a loop as sole argument to a function call */
176*7f2fe78bSCy Schubert # define EV_A_ EV_A,                              /* a loop as first of multiple arguments */
177*7f2fe78bSCy Schubert # define EV_DEFAULT_UC  ev_default_loop_uc_ ()    /* the default loop, if initialised, as sole arg */
178*7f2fe78bSCy Schubert # define EV_DEFAULT_UC_ EV_DEFAULT_UC,            /* the default loop as first of multiple arguments */
179*7f2fe78bSCy Schubert # define EV_DEFAULT  ev_default_loop (0)          /* the default loop as sole arg */
180*7f2fe78bSCy Schubert # define EV_DEFAULT_ EV_DEFAULT,                  /* the default loop as first of multiple arguments */
181*7f2fe78bSCy Schubert #else
182*7f2fe78bSCy Schubert # define EV_P void
183*7f2fe78bSCy Schubert # define EV_P_
184*7f2fe78bSCy Schubert # define EV_A
185*7f2fe78bSCy Schubert # define EV_A_
186*7f2fe78bSCy Schubert # define EV_DEFAULT
187*7f2fe78bSCy Schubert # define EV_DEFAULT_
188*7f2fe78bSCy Schubert # define EV_DEFAULT_UC
189*7f2fe78bSCy Schubert # define EV_DEFAULT_UC_
190*7f2fe78bSCy Schubert # undef EV_EMBED_ENABLE
191*7f2fe78bSCy Schubert #endif
192*7f2fe78bSCy Schubert 
193*7f2fe78bSCy Schubert /* EV_INLINE is used for functions in header files */
194*7f2fe78bSCy Schubert #if __STDC_VERSION__ >= 199901L || __GNUC__ >= 3
195*7f2fe78bSCy Schubert # define EV_INLINE static inline
196*7f2fe78bSCy Schubert #else
197*7f2fe78bSCy Schubert # define EV_INLINE static
198*7f2fe78bSCy Schubert #endif
199*7f2fe78bSCy Schubert 
200*7f2fe78bSCy Schubert #ifdef EV_API_STATIC
201*7f2fe78bSCy Schubert # define EV_API_DECL static
202*7f2fe78bSCy Schubert #else
203*7f2fe78bSCy Schubert # define EV_API_DECL extern
204*7f2fe78bSCy Schubert #endif
205*7f2fe78bSCy Schubert 
206*7f2fe78bSCy Schubert /* EV_PROTOTYPES can be used to switch of prototype declarations */
207*7f2fe78bSCy Schubert #ifndef EV_PROTOTYPES
208*7f2fe78bSCy Schubert # define EV_PROTOTYPES 1
209*7f2fe78bSCy Schubert #endif
210*7f2fe78bSCy Schubert 
211*7f2fe78bSCy Schubert /*****************************************************************************/
212*7f2fe78bSCy Schubert 
213*7f2fe78bSCy Schubert #define EV_VERSION_MAJOR 4
214*7f2fe78bSCy Schubert #define EV_VERSION_MINOR 22
215*7f2fe78bSCy Schubert 
216*7f2fe78bSCy Schubert /* eventmask, revents, events... */
217*7f2fe78bSCy Schubert enum {
218*7f2fe78bSCy Schubert   EV_UNDEF    = (int)0xFFFFFFFF, /* guaranteed to be invalid */
219*7f2fe78bSCy Schubert   EV_NONE     =            0x00, /* no events */
220*7f2fe78bSCy Schubert   EV_READ     =            0x01, /* ev_io detected read will not block */
221*7f2fe78bSCy Schubert   EV_WRITE    =            0x02, /* ev_io detected write will not block */
222*7f2fe78bSCy Schubert   EV__IOFDSET =            0x80, /* internal use only */
223*7f2fe78bSCy Schubert   EV_IO       =         EV_READ, /* alias for type-detection */
224*7f2fe78bSCy Schubert   EV_TIMER    =      0x00000100, /* timer timed out */
225*7f2fe78bSCy Schubert #if EV_COMPAT3
226*7f2fe78bSCy Schubert   EV_TIMEOUT  =        EV_TIMER, /* pre 4.0 API compatibility */
227*7f2fe78bSCy Schubert #endif
228*7f2fe78bSCy Schubert   EV_PERIODIC =      0x00000200, /* periodic timer timed out */
229*7f2fe78bSCy Schubert   EV_SIGNAL   =      0x00000400, /* signal was received */
230*7f2fe78bSCy Schubert   EV_CHILD    =      0x00000800, /* child/pid had status change */
231*7f2fe78bSCy Schubert   EV_STAT     =      0x00001000, /* stat data changed */
232*7f2fe78bSCy Schubert   EV_IDLE     =      0x00002000, /* event loop is idling */
233*7f2fe78bSCy Schubert   EV_PREPARE  =      0x00004000, /* event loop about to poll */
234*7f2fe78bSCy Schubert   EV_CHECK    =      0x00008000, /* event loop finished poll */
235*7f2fe78bSCy Schubert   EV_EMBED    =      0x00010000, /* embedded event loop needs sweep */
236*7f2fe78bSCy Schubert   EV_FORK     =      0x00020000, /* event loop resumed in child */
237*7f2fe78bSCy Schubert   EV_CLEANUP  =      0x00040000, /* event loop resumed in child */
238*7f2fe78bSCy Schubert   EV_ASYNC    =      0x00080000, /* async intra-loop signal */
239*7f2fe78bSCy Schubert   EV_CUSTOM   =      0x01000000, /* for use by user code */
240*7f2fe78bSCy Schubert   EV_ERROR    = (int)0x80000000  /* sent when an error occurs */
241*7f2fe78bSCy Schubert };
242*7f2fe78bSCy Schubert 
243*7f2fe78bSCy Schubert /* can be used to add custom fields to all watchers, while losing binary compatibility */
244*7f2fe78bSCy Schubert #ifndef EV_COMMON
245*7f2fe78bSCy Schubert # define EV_COMMON void *data;
246*7f2fe78bSCy Schubert #endif
247*7f2fe78bSCy Schubert 
248*7f2fe78bSCy Schubert #ifndef EV_CB_DECLARE
249*7f2fe78bSCy Schubert # define EV_CB_DECLARE(type) void (*cb)(EV_P_ struct type *w, int revents);
250*7f2fe78bSCy Schubert #endif
251*7f2fe78bSCy Schubert #ifndef EV_CB_INVOKE
252*7f2fe78bSCy Schubert # define EV_CB_INVOKE(watcher,revents) (watcher)->cb (EV_A_ (watcher), (revents))
253*7f2fe78bSCy Schubert #endif
254*7f2fe78bSCy Schubert 
255*7f2fe78bSCy Schubert /* not official, do not use */
256*7f2fe78bSCy Schubert #define EV_CB(type,name) void name (EV_P_ struct ev_ ## type *w, int revents)
257*7f2fe78bSCy Schubert 
258*7f2fe78bSCy Schubert /*
259*7f2fe78bSCy Schubert  * struct member types:
260*7f2fe78bSCy Schubert  * private: you may look at them, but not change them,
261*7f2fe78bSCy Schubert  *          and they might not mean anything to you.
262*7f2fe78bSCy Schubert  * ro: can be read anytime, but only changed when the watcher isn't active.
263*7f2fe78bSCy Schubert  * rw: can be read and modified anytime, even when the watcher is active.
264*7f2fe78bSCy Schubert  *
265*7f2fe78bSCy Schubert  * some internal details that might be helpful for debugging:
266*7f2fe78bSCy Schubert  *
267*7f2fe78bSCy Schubert  * active is either 0, which means the watcher is not active,
268*7f2fe78bSCy Schubert  *           or the array index of the watcher (periodics, timers)
269*7f2fe78bSCy Schubert  *           or the array index + 1 (most other watchers)
270*7f2fe78bSCy Schubert  *           or simply 1 for watchers that aren't in some array.
271*7f2fe78bSCy Schubert  * pending is either 0, in which case the watcher isn't,
272*7f2fe78bSCy Schubert  *           or the array index + 1 in the pendings array.
273*7f2fe78bSCy Schubert  */
274*7f2fe78bSCy Schubert 
275*7f2fe78bSCy Schubert #if EV_MINPRI == EV_MAXPRI
276*7f2fe78bSCy Schubert # define EV_DECL_PRIORITY
277*7f2fe78bSCy Schubert #elif !defined (EV_DECL_PRIORITY)
278*7f2fe78bSCy Schubert # define EV_DECL_PRIORITY int priority;
279*7f2fe78bSCy Schubert #endif
280*7f2fe78bSCy Schubert 
281*7f2fe78bSCy Schubert /* shared by all watchers */
282*7f2fe78bSCy Schubert #define EV_WATCHER(type)			\
283*7f2fe78bSCy Schubert   int active; /* private */			\
284*7f2fe78bSCy Schubert   int pending; /* private */			\
285*7f2fe78bSCy Schubert   EV_DECL_PRIORITY /* private */		\
286*7f2fe78bSCy Schubert   EV_COMMON /* rw */				\
287*7f2fe78bSCy Schubert   EV_CB_DECLARE (type) /* private */
288*7f2fe78bSCy Schubert 
289*7f2fe78bSCy Schubert #define EV_WATCHER_LIST(type)			\
290*7f2fe78bSCy Schubert   EV_WATCHER (type)				\
291*7f2fe78bSCy Schubert   struct ev_watcher_list *next; /* private */
292*7f2fe78bSCy Schubert 
293*7f2fe78bSCy Schubert #define EV_WATCHER_TIME(type)			\
294*7f2fe78bSCy Schubert   EV_WATCHER (type)				\
295*7f2fe78bSCy Schubert   ev_tstamp at;     /* private */
296*7f2fe78bSCy Schubert 
297*7f2fe78bSCy Schubert /* base class, nothing to see here unless you subclass */
298*7f2fe78bSCy Schubert typedef struct ev_watcher
299*7f2fe78bSCy Schubert {
300*7f2fe78bSCy Schubert   EV_WATCHER (ev_watcher)
301*7f2fe78bSCy Schubert } ev_watcher;
302*7f2fe78bSCy Schubert 
303*7f2fe78bSCy Schubert /* base class, nothing to see here unless you subclass */
304*7f2fe78bSCy Schubert typedef struct ev_watcher_list
305*7f2fe78bSCy Schubert {
306*7f2fe78bSCy Schubert   EV_WATCHER_LIST (ev_watcher_list)
307*7f2fe78bSCy Schubert } ev_watcher_list;
308*7f2fe78bSCy Schubert 
309*7f2fe78bSCy Schubert /* base class, nothing to see here unless you subclass */
310*7f2fe78bSCy Schubert typedef struct ev_watcher_time
311*7f2fe78bSCy Schubert {
312*7f2fe78bSCy Schubert   EV_WATCHER_TIME (ev_watcher_time)
313*7f2fe78bSCy Schubert } ev_watcher_time;
314*7f2fe78bSCy Schubert 
315*7f2fe78bSCy Schubert /* invoked when fd is either EV_READable or EV_WRITEable */
316*7f2fe78bSCy Schubert /* revent EV_READ, EV_WRITE */
317*7f2fe78bSCy Schubert typedef struct ev_io
318*7f2fe78bSCy Schubert {
319*7f2fe78bSCy Schubert   EV_WATCHER_LIST (ev_io)
320*7f2fe78bSCy Schubert 
321*7f2fe78bSCy Schubert   int fd;     /* ro */
322*7f2fe78bSCy Schubert   int events; /* ro */
323*7f2fe78bSCy Schubert } ev_io;
324*7f2fe78bSCy Schubert 
325*7f2fe78bSCy Schubert /* invoked after a specific time, repeatable (based on monotonic clock) */
326*7f2fe78bSCy Schubert /* revent EV_TIMEOUT */
327*7f2fe78bSCy Schubert typedef struct ev_timer
328*7f2fe78bSCy Schubert {
329*7f2fe78bSCy Schubert   EV_WATCHER_TIME (ev_timer)
330*7f2fe78bSCy Schubert 
331*7f2fe78bSCy Schubert   ev_tstamp repeat; /* rw */
332*7f2fe78bSCy Schubert } ev_timer;
333*7f2fe78bSCy Schubert 
334*7f2fe78bSCy Schubert /* invoked at some specific time, possibly repeating at regular intervals (based on UTC) */
335*7f2fe78bSCy Schubert /* revent EV_PERIODIC */
336*7f2fe78bSCy Schubert typedef struct ev_periodic
337*7f2fe78bSCy Schubert {
338*7f2fe78bSCy Schubert   EV_WATCHER_TIME (ev_periodic)
339*7f2fe78bSCy Schubert 
340*7f2fe78bSCy Schubert   ev_tstamp offset; /* rw */
341*7f2fe78bSCy Schubert   ev_tstamp interval; /* rw */
342*7f2fe78bSCy Schubert   ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) EV_THROW; /* rw */
343*7f2fe78bSCy Schubert } ev_periodic;
344*7f2fe78bSCy Schubert 
345*7f2fe78bSCy Schubert /* invoked when the given signal has been received */
346*7f2fe78bSCy Schubert /* revent EV_SIGNAL */
347*7f2fe78bSCy Schubert typedef struct ev_signal
348*7f2fe78bSCy Schubert {
349*7f2fe78bSCy Schubert   EV_WATCHER_LIST (ev_signal)
350*7f2fe78bSCy Schubert 
351*7f2fe78bSCy Schubert   int signum; /* ro */
352*7f2fe78bSCy Schubert } ev_signal;
353*7f2fe78bSCy Schubert 
354*7f2fe78bSCy Schubert /* invoked when sigchld is received and waitpid indicates the given pid */
355*7f2fe78bSCy Schubert /* revent EV_CHILD */
356*7f2fe78bSCy Schubert /* does not support priorities */
357*7f2fe78bSCy Schubert typedef struct ev_child
358*7f2fe78bSCy Schubert {
359*7f2fe78bSCy Schubert   EV_WATCHER_LIST (ev_child)
360*7f2fe78bSCy Schubert 
361*7f2fe78bSCy Schubert   int flags;   /* private */
362*7f2fe78bSCy Schubert   int pid;     /* ro */
363*7f2fe78bSCy Schubert   int rpid;    /* rw, holds the received pid */
364*7f2fe78bSCy Schubert   int rstatus; /* rw, holds the exit status, use the macros from sys/wait.h */
365*7f2fe78bSCy Schubert } ev_child;
366*7f2fe78bSCy Schubert 
367*7f2fe78bSCy Schubert #if EV_STAT_ENABLE
368*7f2fe78bSCy Schubert /* st_nlink = 0 means missing file or other error */
369*7f2fe78bSCy Schubert # ifdef _WIN32
370*7f2fe78bSCy Schubert typedef struct _stati64 ev_statdata;
371*7f2fe78bSCy Schubert # else
372*7f2fe78bSCy Schubert typedef struct stat ev_statdata;
373*7f2fe78bSCy Schubert # endif
374*7f2fe78bSCy Schubert 
375*7f2fe78bSCy Schubert /* invoked each time the stat data changes for a given path */
376*7f2fe78bSCy Schubert /* revent EV_STAT */
377*7f2fe78bSCy Schubert typedef struct ev_stat
378*7f2fe78bSCy Schubert {
379*7f2fe78bSCy Schubert   EV_WATCHER_LIST (ev_stat)
380*7f2fe78bSCy Schubert 
381*7f2fe78bSCy Schubert   ev_timer timer;     /* private */
382*7f2fe78bSCy Schubert   ev_tstamp interval; /* ro */
383*7f2fe78bSCy Schubert   const char *path;   /* ro */
384*7f2fe78bSCy Schubert   ev_statdata prev;   /* ro */
385*7f2fe78bSCy Schubert   ev_statdata attr;   /* ro */
386*7f2fe78bSCy Schubert 
387*7f2fe78bSCy Schubert   int wd; /* wd for inotify, fd for kqueue */
388*7f2fe78bSCy Schubert } ev_stat;
389*7f2fe78bSCy Schubert #endif
390*7f2fe78bSCy Schubert 
391*7f2fe78bSCy Schubert #if EV_IDLE_ENABLE
392*7f2fe78bSCy Schubert /* invoked when the nothing else needs to be done, keeps the process from blocking */
393*7f2fe78bSCy Schubert /* revent EV_IDLE */
394*7f2fe78bSCy Schubert typedef struct ev_idle
395*7f2fe78bSCy Schubert {
396*7f2fe78bSCy Schubert   EV_WATCHER (ev_idle)
397*7f2fe78bSCy Schubert } ev_idle;
398*7f2fe78bSCy Schubert #endif
399*7f2fe78bSCy Schubert 
400*7f2fe78bSCy Schubert /* invoked for each run of the mainloop, just before the blocking call */
401*7f2fe78bSCy Schubert /* you can still change events in any way you like */
402*7f2fe78bSCy Schubert /* revent EV_PREPARE */
403*7f2fe78bSCy Schubert typedef struct ev_prepare
404*7f2fe78bSCy Schubert {
405*7f2fe78bSCy Schubert   EV_WATCHER (ev_prepare)
406*7f2fe78bSCy Schubert } ev_prepare;
407*7f2fe78bSCy Schubert 
408*7f2fe78bSCy Schubert /* invoked for each run of the mainloop, just after the blocking call */
409*7f2fe78bSCy Schubert /* revent EV_CHECK */
410*7f2fe78bSCy Schubert typedef struct ev_check
411*7f2fe78bSCy Schubert {
412*7f2fe78bSCy Schubert   EV_WATCHER (ev_check)
413*7f2fe78bSCy Schubert } ev_check;
414*7f2fe78bSCy Schubert 
415*7f2fe78bSCy Schubert #if EV_FORK_ENABLE
416*7f2fe78bSCy Schubert /* the callback gets invoked before check in the child process when a fork was detected */
417*7f2fe78bSCy Schubert /* revent EV_FORK */
418*7f2fe78bSCy Schubert typedef struct ev_fork
419*7f2fe78bSCy Schubert {
420*7f2fe78bSCy Schubert   EV_WATCHER (ev_fork)
421*7f2fe78bSCy Schubert } ev_fork;
422*7f2fe78bSCy Schubert #endif
423*7f2fe78bSCy Schubert 
424*7f2fe78bSCy Schubert #if EV_CLEANUP_ENABLE
425*7f2fe78bSCy Schubert /* is invoked just before the loop gets destroyed */
426*7f2fe78bSCy Schubert /* revent EV_CLEANUP */
427*7f2fe78bSCy Schubert typedef struct ev_cleanup
428*7f2fe78bSCy Schubert {
429*7f2fe78bSCy Schubert   EV_WATCHER (ev_cleanup)
430*7f2fe78bSCy Schubert } ev_cleanup;
431*7f2fe78bSCy Schubert #endif
432*7f2fe78bSCy Schubert 
433*7f2fe78bSCy Schubert #if EV_EMBED_ENABLE
434*7f2fe78bSCy Schubert /* used to embed an event loop inside another */
435*7f2fe78bSCy Schubert /* the callback gets invoked when the event loop has handled events, and can be 0 */
436*7f2fe78bSCy Schubert typedef struct ev_embed
437*7f2fe78bSCy Schubert {
438*7f2fe78bSCy Schubert   EV_WATCHER (ev_embed)
439*7f2fe78bSCy Schubert 
440*7f2fe78bSCy Schubert   struct ev_loop *other; /* ro */
441*7f2fe78bSCy Schubert   ev_io io;              /* private */
442*7f2fe78bSCy Schubert   ev_prepare prepare;    /* private */
443*7f2fe78bSCy Schubert   ev_check check;        /* unused */
444*7f2fe78bSCy Schubert   ev_timer timer;        /* unused */
445*7f2fe78bSCy Schubert   ev_periodic periodic;  /* unused */
446*7f2fe78bSCy Schubert   ev_idle idle;          /* unused */
447*7f2fe78bSCy Schubert   ev_fork fork;          /* private */
448*7f2fe78bSCy Schubert #if EV_CLEANUP_ENABLE
449*7f2fe78bSCy Schubert   ev_cleanup cleanup;    /* unused */
450*7f2fe78bSCy Schubert #endif
451*7f2fe78bSCy Schubert } ev_embed;
452*7f2fe78bSCy Schubert #endif
453*7f2fe78bSCy Schubert 
454*7f2fe78bSCy Schubert #if EV_ASYNC_ENABLE
455*7f2fe78bSCy Schubert /* invoked when somebody calls ev_async_send on the watcher */
456*7f2fe78bSCy Schubert /* revent EV_ASYNC */
457*7f2fe78bSCy Schubert typedef struct ev_async
458*7f2fe78bSCy Schubert {
459*7f2fe78bSCy Schubert   EV_WATCHER (ev_async)
460*7f2fe78bSCy Schubert 
461*7f2fe78bSCy Schubert   EV_ATOMIC_T sent; /* private */
462*7f2fe78bSCy Schubert } ev_async;
463*7f2fe78bSCy Schubert 
464*7f2fe78bSCy Schubert # define ev_async_pending(w) (+(w)->sent)
465*7f2fe78bSCy Schubert #endif
466*7f2fe78bSCy Schubert 
467*7f2fe78bSCy Schubert /* the presence of this union forces similar struct layout */
468*7f2fe78bSCy Schubert union ev_any_watcher
469*7f2fe78bSCy Schubert {
470*7f2fe78bSCy Schubert   struct ev_watcher w;
471*7f2fe78bSCy Schubert   struct ev_watcher_list wl;
472*7f2fe78bSCy Schubert 
473*7f2fe78bSCy Schubert   struct ev_io io;
474*7f2fe78bSCy Schubert   struct ev_timer timer;
475*7f2fe78bSCy Schubert   struct ev_periodic periodic;
476*7f2fe78bSCy Schubert   struct ev_signal signal;
477*7f2fe78bSCy Schubert   struct ev_child child;
478*7f2fe78bSCy Schubert #if EV_STAT_ENABLE
479*7f2fe78bSCy Schubert   struct ev_stat stat;
480*7f2fe78bSCy Schubert #endif
481*7f2fe78bSCy Schubert #if EV_IDLE_ENABLE
482*7f2fe78bSCy Schubert   struct ev_idle idle;
483*7f2fe78bSCy Schubert #endif
484*7f2fe78bSCy Schubert   struct ev_prepare prepare;
485*7f2fe78bSCy Schubert   struct ev_check check;
486*7f2fe78bSCy Schubert #if EV_FORK_ENABLE
487*7f2fe78bSCy Schubert   struct ev_fork fork;
488*7f2fe78bSCy Schubert #endif
489*7f2fe78bSCy Schubert #if EV_CLEANUP_ENABLE
490*7f2fe78bSCy Schubert   struct ev_cleanup cleanup;
491*7f2fe78bSCy Schubert #endif
492*7f2fe78bSCy Schubert #if EV_EMBED_ENABLE
493*7f2fe78bSCy Schubert   struct ev_embed embed;
494*7f2fe78bSCy Schubert #endif
495*7f2fe78bSCy Schubert #if EV_ASYNC_ENABLE
496*7f2fe78bSCy Schubert   struct ev_async async;
497*7f2fe78bSCy Schubert #endif
498*7f2fe78bSCy Schubert };
499*7f2fe78bSCy Schubert 
500*7f2fe78bSCy Schubert /* flag bits for ev_default_loop and ev_loop_new */
501*7f2fe78bSCy Schubert enum {
502*7f2fe78bSCy Schubert   /* the default */
503*7f2fe78bSCy Schubert   EVFLAG_AUTO      = 0x00000000U, /* not quite a mask */
504*7f2fe78bSCy Schubert   /* flag bits */
505*7f2fe78bSCy Schubert   EVFLAG_NOENV     = 0x01000000U, /* do NOT consult environment */
506*7f2fe78bSCy Schubert   EVFLAG_FORKCHECK = 0x02000000U, /* check for a fork in each iteration */
507*7f2fe78bSCy Schubert   /* debugging/feature disable */
508*7f2fe78bSCy Schubert   EVFLAG_NOINOTIFY = 0x00100000U, /* do not attempt to use inotify */
509*7f2fe78bSCy Schubert #if EV_COMPAT3
510*7f2fe78bSCy Schubert   EVFLAG_NOSIGFD   = 0, /* compatibility to pre-3.9 */
511*7f2fe78bSCy Schubert #endif
512*7f2fe78bSCy Schubert   EVFLAG_SIGNALFD  = 0x00200000U, /* attempt to use signalfd */
513*7f2fe78bSCy Schubert   EVFLAG_NOSIGMASK = 0x00400000U  /* avoid modifying the signal mask */
514*7f2fe78bSCy Schubert };
515*7f2fe78bSCy Schubert 
516*7f2fe78bSCy Schubert /* method bits to be ored together */
517*7f2fe78bSCy Schubert enum {
518*7f2fe78bSCy Schubert   EVBACKEND_SELECT  = 0x00000001U, /* about anywhere */
519*7f2fe78bSCy Schubert   EVBACKEND_POLL    = 0x00000002U, /* !win */
520*7f2fe78bSCy Schubert   EVBACKEND_EPOLL   = 0x00000004U, /* linux */
521*7f2fe78bSCy Schubert   EVBACKEND_KQUEUE  = 0x00000008U, /* bsd */
522*7f2fe78bSCy Schubert   EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */
523*7f2fe78bSCy Schubert   EVBACKEND_PORT    = 0x00000020U, /* solaris 10 */
524*7f2fe78bSCy Schubert   EVBACKEND_ALL     = 0x0000003FU, /* all known backends */
525*7f2fe78bSCy Schubert   EVBACKEND_MASK    = 0x0000FFFFU  /* all future backends */
526*7f2fe78bSCy Schubert };
527*7f2fe78bSCy Schubert 
528*7f2fe78bSCy Schubert #if EV_PROTOTYPES
529*7f2fe78bSCy Schubert EV_API_DECL int ev_version_major (void) EV_THROW;
530*7f2fe78bSCy Schubert EV_API_DECL int ev_version_minor (void) EV_THROW;
531*7f2fe78bSCy Schubert 
532*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_supported_backends (void) EV_THROW;
533*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_recommended_backends (void) EV_THROW;
534*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_embeddable_backends (void) EV_THROW;
535*7f2fe78bSCy Schubert 
536*7f2fe78bSCy Schubert EV_API_DECL ev_tstamp ev_time (void) EV_THROW;
537*7f2fe78bSCy Schubert EV_API_DECL void ev_sleep (ev_tstamp delay) EV_THROW; /* sleep for a while */
538*7f2fe78bSCy Schubert 
539*7f2fe78bSCy Schubert /* Sets the allocation function to use, works like realloc.
540*7f2fe78bSCy Schubert  * It is used to allocate and free memory.
541*7f2fe78bSCy Schubert  * If it returns zero when memory needs to be allocated, the library might abort
542*7f2fe78bSCy Schubert  * or take some potentially destructive action.
543*7f2fe78bSCy Schubert  * The default is your system realloc function.
544*7f2fe78bSCy Schubert  */
545*7f2fe78bSCy Schubert EV_API_DECL void ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW;
546*7f2fe78bSCy Schubert 
547*7f2fe78bSCy Schubert /* set the callback function to call on a
548*7f2fe78bSCy Schubert  * retryable syscall error
549*7f2fe78bSCy Schubert  * (such as failed select, poll, epoll_wait)
550*7f2fe78bSCy Schubert  */
551*7f2fe78bSCy Schubert EV_API_DECL void ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW;
552*7f2fe78bSCy Schubert 
553*7f2fe78bSCy Schubert #if EV_MULTIPLICITY
554*7f2fe78bSCy Schubert 
555*7f2fe78bSCy Schubert /* the default loop is the only one that handles signals and child watchers */
556*7f2fe78bSCy Schubert /* you can call this as often as you like */
557*7f2fe78bSCy Schubert EV_API_DECL struct ev_loop *ev_default_loop (unsigned int flags EV_CPP (= 0)) EV_THROW;
558*7f2fe78bSCy Schubert 
559*7f2fe78bSCy Schubert #ifdef EV_API_STATIC
560*7f2fe78bSCy Schubert EV_API_DECL struct ev_loop *ev_default_loop_ptr;
561*7f2fe78bSCy Schubert #endif
562*7f2fe78bSCy Schubert 
563*7f2fe78bSCy Schubert EV_INLINE struct ev_loop *
564*7f2fe78bSCy Schubert ev_default_loop_uc_ (void) EV_THROW
565*7f2fe78bSCy Schubert {
566*7f2fe78bSCy Schubert   extern struct ev_loop *ev_default_loop_ptr;
567*7f2fe78bSCy Schubert 
568*7f2fe78bSCy Schubert   return ev_default_loop_ptr;
569*7f2fe78bSCy Schubert }
570*7f2fe78bSCy Schubert 
571*7f2fe78bSCy Schubert EV_INLINE int
572*7f2fe78bSCy Schubert ev_is_default_loop (EV_P) EV_THROW
573*7f2fe78bSCy Schubert {
574*7f2fe78bSCy Schubert   return EV_A == EV_DEFAULT_UC;
575*7f2fe78bSCy Schubert }
576*7f2fe78bSCy Schubert 
577*7f2fe78bSCy Schubert /* create and destroy alternative loops that don't handle signals */
578*7f2fe78bSCy Schubert EV_API_DECL struct ev_loop *ev_loop_new (unsigned int flags EV_CPP (= 0)) EV_THROW;
579*7f2fe78bSCy Schubert 
580*7f2fe78bSCy Schubert EV_API_DECL ev_tstamp ev_now (EV_P) EV_THROW; /* time w.r.t. timers and the eventloop, updated after each poll */
581*7f2fe78bSCy Schubert 
582*7f2fe78bSCy Schubert #else
583*7f2fe78bSCy Schubert 
584*7f2fe78bSCy Schubert EV_API_DECL int ev_default_loop (unsigned int flags EV_CPP (= 0)) EV_THROW; /* returns true when successful */
585*7f2fe78bSCy Schubert 
586*7f2fe78bSCy Schubert EV_API_DECL ev_tstamp ev_rt_now;
587*7f2fe78bSCy Schubert 
588*7f2fe78bSCy Schubert EV_INLINE ev_tstamp
589*7f2fe78bSCy Schubert ev_now (void) EV_THROW
590*7f2fe78bSCy Schubert {
591*7f2fe78bSCy Schubert   return ev_rt_now;
592*7f2fe78bSCy Schubert }
593*7f2fe78bSCy Schubert 
594*7f2fe78bSCy Schubert /* looks weird, but ev_is_default_loop (EV_A) still works if this exists */
595*7f2fe78bSCy Schubert EV_INLINE int
596*7f2fe78bSCy Schubert ev_is_default_loop (void) EV_THROW
597*7f2fe78bSCy Schubert {
598*7f2fe78bSCy Schubert   return 1;
599*7f2fe78bSCy Schubert }
600*7f2fe78bSCy Schubert 
601*7f2fe78bSCy Schubert #endif /* multiplicity */
602*7f2fe78bSCy Schubert 
603*7f2fe78bSCy Schubert /* destroy event loops, also works for the default loop */
604*7f2fe78bSCy Schubert EV_API_DECL void ev_loop_destroy (EV_P);
605*7f2fe78bSCy Schubert 
606*7f2fe78bSCy Schubert /* this needs to be called after fork, to duplicate the loop */
607*7f2fe78bSCy Schubert /* when you want to re-use it in the child */
608*7f2fe78bSCy Schubert /* you can call it in either the parent or the child */
609*7f2fe78bSCy Schubert /* you can actually call it at any time, anywhere :) */
610*7f2fe78bSCy Schubert EV_API_DECL void ev_loop_fork (EV_P) EV_THROW;
611*7f2fe78bSCy Schubert 
612*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_backend (EV_P) EV_THROW; /* backend in use by loop */
613*7f2fe78bSCy Schubert 
614*7f2fe78bSCy Schubert EV_API_DECL void ev_now_update (EV_P) EV_THROW; /* update event loop time */
615*7f2fe78bSCy Schubert 
616*7f2fe78bSCy Schubert #if EV_WALK_ENABLE
617*7f2fe78bSCy Schubert /* walk (almost) all watchers in the loop of a given type, invoking the */
618*7f2fe78bSCy Schubert /* callback on every such watcher. The callback might stop the watcher, */
619*7f2fe78bSCy Schubert /* but do nothing else with the loop */
620*7f2fe78bSCy Schubert EV_API_DECL void ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW;
621*7f2fe78bSCy Schubert #endif
622*7f2fe78bSCy Schubert 
623*7f2fe78bSCy Schubert #endif /* prototypes */
624*7f2fe78bSCy Schubert 
625*7f2fe78bSCy Schubert /* ev_run flags values */
626*7f2fe78bSCy Schubert enum {
627*7f2fe78bSCy Schubert   EVRUN_NOWAIT = 1, /* do not block/wait */
628*7f2fe78bSCy Schubert   EVRUN_ONCE   = 2  /* block *once* only */
629*7f2fe78bSCy Schubert };
630*7f2fe78bSCy Schubert 
631*7f2fe78bSCy Schubert /* ev_break how values */
632*7f2fe78bSCy Schubert enum {
633*7f2fe78bSCy Schubert   EVBREAK_CANCEL = 0, /* undo unloop */
634*7f2fe78bSCy Schubert   EVBREAK_ONE    = 1, /* unloop once */
635*7f2fe78bSCy Schubert   EVBREAK_ALL    = 2  /* unloop all loops */
636*7f2fe78bSCy Schubert };
637*7f2fe78bSCy Schubert 
638*7f2fe78bSCy Schubert #if EV_PROTOTYPES
639*7f2fe78bSCy Schubert EV_API_DECL int  ev_run (EV_P_ int flags EV_CPP (= 0));
640*7f2fe78bSCy Schubert EV_API_DECL void ev_break (EV_P_ int how EV_CPP (= EVBREAK_ONE)) EV_THROW; /* break out of the loop */
641*7f2fe78bSCy Schubert 
642*7f2fe78bSCy Schubert /*
643*7f2fe78bSCy Schubert  * ref/unref can be used to add or remove a refcount on the mainloop. every watcher
644*7f2fe78bSCy Schubert  * keeps one reference. if you have a long-running watcher you never unregister that
645*7f2fe78bSCy Schubert  * should not keep ev_loop from running, unref() after starting, and ref() before stopping.
646*7f2fe78bSCy Schubert  */
647*7f2fe78bSCy Schubert EV_API_DECL void ev_ref   (EV_P) EV_THROW;
648*7f2fe78bSCy Schubert EV_API_DECL void ev_unref (EV_P) EV_THROW;
649*7f2fe78bSCy Schubert 
650*7f2fe78bSCy Schubert /*
651*7f2fe78bSCy Schubert  * convenience function, wait for a single event, without registering an event watcher
652*7f2fe78bSCy Schubert  * if timeout is < 0, do wait indefinitely
653*7f2fe78bSCy Schubert  */
654*7f2fe78bSCy Schubert EV_API_DECL void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW;
655*7f2fe78bSCy Schubert 
656*7f2fe78bSCy Schubert # if EV_FEATURE_API
657*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_iteration (EV_P) EV_THROW; /* number of loop iterations */
658*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_depth     (EV_P) EV_THROW; /* #ev_loop enters - #ev_loop leaves */
659*7f2fe78bSCy Schubert EV_API_DECL void         ev_verify    (EV_P) EV_THROW; /* abort if loop data corrupted */
660*7f2fe78bSCy Schubert 
661*7f2fe78bSCy Schubert EV_API_DECL void ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */
662*7f2fe78bSCy Schubert EV_API_DECL void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */
663*7f2fe78bSCy Schubert 
664*7f2fe78bSCy Schubert /* advanced stuff for threading etc. support, see docs */
665*7f2fe78bSCy Schubert EV_API_DECL void ev_set_userdata (EV_P_ void *data) EV_THROW;
666*7f2fe78bSCy Schubert EV_API_DECL void *ev_userdata (EV_P) EV_THROW;
667*7f2fe78bSCy Schubert typedef void (*ev_loop_callback)(EV_P);
668*7f2fe78bSCy Schubert EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW;
669*7f2fe78bSCy Schubert /* C++ doesn't allow the use of the ev_loop_callback typedef here, so we need to spell it out */
670*7f2fe78bSCy Schubert EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW;
671*7f2fe78bSCy Schubert 
672*7f2fe78bSCy Schubert EV_API_DECL unsigned int ev_pending_count (EV_P) EV_THROW; /* number of pending events, if any */
673*7f2fe78bSCy Schubert EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */
674*7f2fe78bSCy Schubert 
675*7f2fe78bSCy Schubert /*
676*7f2fe78bSCy Schubert  * stop/start the timer handling.
677*7f2fe78bSCy Schubert  */
678*7f2fe78bSCy Schubert EV_API_DECL void ev_suspend (EV_P) EV_THROW;
679*7f2fe78bSCy Schubert EV_API_DECL void ev_resume  (EV_P) EV_THROW;
680*7f2fe78bSCy Schubert #endif
681*7f2fe78bSCy Schubert 
682*7f2fe78bSCy Schubert #endif
683*7f2fe78bSCy Schubert 
684*7f2fe78bSCy Schubert /* these may evaluate ev multiple times, and the other arguments at most once */
685*7f2fe78bSCy Schubert /* either use ev_init + ev_TYPE_set, or the ev_TYPE_init macro, below, to first initialise a watcher */
686*7f2fe78bSCy Schubert #define ev_init(ev,cb_) do {			\
687*7f2fe78bSCy Schubert   ((ev_watcher *)(void *)(ev))->active  =	\
688*7f2fe78bSCy Schubert   ((ev_watcher *)(void *)(ev))->pending = 0;	\
689*7f2fe78bSCy Schubert   ev_set_priority ((ev), 0);			\
690*7f2fe78bSCy Schubert   ev_set_cb ((ev), cb_);			\
691*7f2fe78bSCy Schubert } while (0)
692*7f2fe78bSCy Schubert 
693*7f2fe78bSCy Schubert #define ev_io_set(ev,fd_,events_)            do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0)
694*7f2fe78bSCy Schubert #define ev_timer_set(ev,after_,repeat_)      do { ((ev_watcher_time *)(ev))->at = (after_); (ev)->repeat = (repeat_); } while (0)
695*7f2fe78bSCy Schubert #define ev_periodic_set(ev,ofs_,ival_,rcb_)  do { (ev)->offset = (ofs_); (ev)->interval = (ival_); (ev)->reschedule_cb = (rcb_); } while (0)
696*7f2fe78bSCy Schubert #define ev_signal_set(ev,signum_)            do { (ev)->signum = (signum_); } while (0)
697*7f2fe78bSCy Schubert #define ev_child_set(ev,pid_,trace_)         do { (ev)->pid = (pid_); (ev)->flags = !!(trace_); } while (0)
698*7f2fe78bSCy Schubert #define ev_stat_set(ev,path_,interval_)      do { (ev)->path = (path_); (ev)->interval = (interval_); (ev)->wd = -2; } while (0)
699*7f2fe78bSCy Schubert #define ev_idle_set(ev)                      /* nop, yes, this is a serious in-joke */
700*7f2fe78bSCy Schubert #define ev_prepare_set(ev)                   /* nop, yes, this is a serious in-joke */
701*7f2fe78bSCy Schubert #define ev_check_set(ev)                     /* nop, yes, this is a serious in-joke */
702*7f2fe78bSCy Schubert #define ev_embed_set(ev,other_)              do { (ev)->other = (other_); } while (0)
703*7f2fe78bSCy Schubert #define ev_fork_set(ev)                      /* nop, yes, this is a serious in-joke */
704*7f2fe78bSCy Schubert #define ev_cleanup_set(ev)                   /* nop, yes, this is a serious in-joke */
705*7f2fe78bSCy Schubert #define ev_async_set(ev)                     /* nop, yes, this is a serious in-joke */
706*7f2fe78bSCy Schubert 
707*7f2fe78bSCy Schubert #define ev_io_init(ev,cb,fd,events)          do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)
708*7f2fe78bSCy Schubert #define ev_timer_init(ev,cb,after,repeat)    do { ev_init ((ev), (cb)); ev_timer_set ((ev),(after),(repeat)); } while (0)
709*7f2fe78bSCy Schubert #define ev_periodic_init(ev,cb,ofs,ival,rcb) do { ev_init ((ev), (cb)); ev_periodic_set ((ev),(ofs),(ival),(rcb)); } while (0)
710*7f2fe78bSCy Schubert #define ev_signal_init(ev,cb,signum)         do { ev_init ((ev), (cb)); ev_signal_set ((ev), (signum)); } while (0)
711*7f2fe78bSCy Schubert #define ev_child_init(ev,cb,pid,trace)       do { ev_init ((ev), (cb)); ev_child_set ((ev),(pid),(trace)); } while (0)
712*7f2fe78bSCy Schubert #define ev_stat_init(ev,cb,path,interval)    do { ev_init ((ev), (cb)); ev_stat_set ((ev),(path),(interval)); } while (0)
713*7f2fe78bSCy Schubert #define ev_idle_init(ev,cb)                  do { ev_init ((ev), (cb)); ev_idle_set ((ev)); } while (0)
714*7f2fe78bSCy Schubert #define ev_prepare_init(ev,cb)               do { ev_init ((ev), (cb)); ev_prepare_set ((ev)); } while (0)
715*7f2fe78bSCy Schubert #define ev_check_init(ev,cb)                 do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0)
716*7f2fe78bSCy Schubert #define ev_embed_init(ev,cb,other)           do { ev_init ((ev), (cb)); ev_embed_set ((ev),(other)); } while (0)
717*7f2fe78bSCy Schubert #define ev_fork_init(ev,cb)                  do { ev_init ((ev), (cb)); ev_fork_set ((ev)); } while (0)
718*7f2fe78bSCy Schubert #define ev_cleanup_init(ev,cb)               do { ev_init ((ev), (cb)); ev_cleanup_set ((ev)); } while (0)
719*7f2fe78bSCy Schubert #define ev_async_init(ev,cb)                 do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0)
720*7f2fe78bSCy Schubert 
721*7f2fe78bSCy Schubert #define ev_is_pending(ev)                    (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */
722*7f2fe78bSCy Schubert #define ev_is_active(ev)                     (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */
723*7f2fe78bSCy Schubert 
724*7f2fe78bSCy Schubert #define ev_cb_(ev)                           (ev)->cb /* rw */
725*7f2fe78bSCy Schubert #define ev_cb(ev)                            (memmove (&ev_cb_ (ev), &((ev_watcher *)(ev))->cb, sizeof (ev_cb_ (ev))), (ev)->cb)
726*7f2fe78bSCy Schubert 
727*7f2fe78bSCy Schubert #if EV_MINPRI == EV_MAXPRI
728*7f2fe78bSCy Schubert # define ev_priority(ev)                     ((ev), EV_MINPRI)
729*7f2fe78bSCy Schubert # define ev_set_priority(ev,pri)             ((ev), (pri))
730*7f2fe78bSCy Schubert #else
731*7f2fe78bSCy Schubert # define ev_priority(ev)                     (+(((ev_watcher *)(void *)(ev))->priority))
732*7f2fe78bSCy Schubert # define ev_set_priority(ev,pri)             (   (ev_watcher *)(void *)(ev))->priority = (pri)
733*7f2fe78bSCy Schubert #endif
734*7f2fe78bSCy Schubert 
735*7f2fe78bSCy Schubert #define ev_periodic_at(ev)                   (+((ev_watcher_time *)(ev))->at)
736*7f2fe78bSCy Schubert 
737*7f2fe78bSCy Schubert #ifndef ev_set_cb
738*7f2fe78bSCy Schubert # define ev_set_cb(ev,cb_)                   (ev_cb_ (ev) = (cb_), memmove (&((ev_watcher *)(ev))->cb, &ev_cb_ (ev), sizeof (ev_cb_ (ev))))
739*7f2fe78bSCy Schubert #endif
740*7f2fe78bSCy Schubert 
741*7f2fe78bSCy Schubert /* stopping (enabling, adding) a watcher does nothing if it is already running */
742*7f2fe78bSCy Schubert /* stopping (disabling, deleting) a watcher does nothing unless it's already running */
743*7f2fe78bSCy Schubert #if EV_PROTOTYPES
744*7f2fe78bSCy Schubert 
745*7f2fe78bSCy Schubert /* feeds an event into a watcher as if the event actually occurred */
746*7f2fe78bSCy Schubert /* accepts any ev_watcher type */
747*7f2fe78bSCy Schubert EV_API_DECL void ev_feed_event     (EV_P_ void *w, int revents) EV_THROW;
748*7f2fe78bSCy Schubert EV_API_DECL void ev_feed_fd_event  (EV_P_ int fd, int revents) EV_THROW;
749*7f2fe78bSCy Schubert #if EV_SIGNAL_ENABLE
750*7f2fe78bSCy Schubert EV_API_DECL void ev_feed_signal    (int signum) EV_THROW;
751*7f2fe78bSCy Schubert EV_API_DECL void ev_feed_signal_event (EV_P_ int signum) EV_THROW;
752*7f2fe78bSCy Schubert #endif
753*7f2fe78bSCy Schubert EV_API_DECL void ev_invoke         (EV_P_ void *w, int revents);
754*7f2fe78bSCy Schubert EV_API_DECL int  ev_clear_pending  (EV_P_ void *w) EV_THROW;
755*7f2fe78bSCy Schubert 
756*7f2fe78bSCy Schubert EV_API_DECL void ev_io_start       (EV_P_ ev_io *w) EV_THROW;
757*7f2fe78bSCy Schubert EV_API_DECL void ev_io_stop        (EV_P_ ev_io *w) EV_THROW;
758*7f2fe78bSCy Schubert 
759*7f2fe78bSCy Schubert EV_API_DECL void ev_timer_start    (EV_P_ ev_timer *w) EV_THROW;
760*7f2fe78bSCy Schubert EV_API_DECL void ev_timer_stop     (EV_P_ ev_timer *w) EV_THROW;
761*7f2fe78bSCy Schubert /* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */
762*7f2fe78bSCy Schubert EV_API_DECL void ev_timer_again    (EV_P_ ev_timer *w) EV_THROW;
763*7f2fe78bSCy Schubert /* return remaining time */
764*7f2fe78bSCy Schubert EV_API_DECL ev_tstamp ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW;
765*7f2fe78bSCy Schubert 
766*7f2fe78bSCy Schubert #if EV_PERIODIC_ENABLE
767*7f2fe78bSCy Schubert EV_API_DECL void ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW;
768*7f2fe78bSCy Schubert EV_API_DECL void ev_periodic_stop  (EV_P_ ev_periodic *w) EV_THROW;
769*7f2fe78bSCy Schubert EV_API_DECL void ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW;
770*7f2fe78bSCy Schubert #endif
771*7f2fe78bSCy Schubert 
772*7f2fe78bSCy Schubert /* only supported in the default loop */
773*7f2fe78bSCy Schubert #if EV_SIGNAL_ENABLE
774*7f2fe78bSCy Schubert EV_API_DECL void ev_signal_start   (EV_P_ ev_signal *w) EV_THROW;
775*7f2fe78bSCy Schubert EV_API_DECL void ev_signal_stop    (EV_P_ ev_signal *w) EV_THROW;
776*7f2fe78bSCy Schubert #endif
777*7f2fe78bSCy Schubert 
778*7f2fe78bSCy Schubert /* only supported in the default loop */
779*7f2fe78bSCy Schubert # if EV_CHILD_ENABLE
780*7f2fe78bSCy Schubert EV_API_DECL void ev_child_start    (EV_P_ ev_child *w) EV_THROW;
781*7f2fe78bSCy Schubert EV_API_DECL void ev_child_stop     (EV_P_ ev_child *w) EV_THROW;
782*7f2fe78bSCy Schubert # endif
783*7f2fe78bSCy Schubert 
784*7f2fe78bSCy Schubert # if EV_STAT_ENABLE
785*7f2fe78bSCy Schubert EV_API_DECL void ev_stat_start     (EV_P_ ev_stat *w) EV_THROW;
786*7f2fe78bSCy Schubert EV_API_DECL void ev_stat_stop      (EV_P_ ev_stat *w) EV_THROW;
787*7f2fe78bSCy Schubert EV_API_DECL void ev_stat_stat      (EV_P_ ev_stat *w) EV_THROW;
788*7f2fe78bSCy Schubert # endif
789*7f2fe78bSCy Schubert 
790*7f2fe78bSCy Schubert # if EV_IDLE_ENABLE
791*7f2fe78bSCy Schubert EV_API_DECL void ev_idle_start     (EV_P_ ev_idle *w) EV_THROW;
792*7f2fe78bSCy Schubert EV_API_DECL void ev_idle_stop      (EV_P_ ev_idle *w) EV_THROW;
793*7f2fe78bSCy Schubert # endif
794*7f2fe78bSCy Schubert 
795*7f2fe78bSCy Schubert #if EV_PREPARE_ENABLE
796*7f2fe78bSCy Schubert EV_API_DECL void ev_prepare_start  (EV_P_ ev_prepare *w) EV_THROW;
797*7f2fe78bSCy Schubert EV_API_DECL void ev_prepare_stop   (EV_P_ ev_prepare *w) EV_THROW;
798*7f2fe78bSCy Schubert #endif
799*7f2fe78bSCy Schubert 
800*7f2fe78bSCy Schubert #if EV_CHECK_ENABLE
801*7f2fe78bSCy Schubert EV_API_DECL void ev_check_start    (EV_P_ ev_check *w) EV_THROW;
802*7f2fe78bSCy Schubert EV_API_DECL void ev_check_stop     (EV_P_ ev_check *w) EV_THROW;
803*7f2fe78bSCy Schubert #endif
804*7f2fe78bSCy Schubert 
805*7f2fe78bSCy Schubert # if EV_FORK_ENABLE
806*7f2fe78bSCy Schubert EV_API_DECL void ev_fork_start     (EV_P_ ev_fork *w) EV_THROW;
807*7f2fe78bSCy Schubert EV_API_DECL void ev_fork_stop      (EV_P_ ev_fork *w) EV_THROW;
808*7f2fe78bSCy Schubert # endif
809*7f2fe78bSCy Schubert 
810*7f2fe78bSCy Schubert # if EV_CLEANUP_ENABLE
811*7f2fe78bSCy Schubert EV_API_DECL void ev_cleanup_start  (EV_P_ ev_cleanup *w) EV_THROW;
812*7f2fe78bSCy Schubert EV_API_DECL void ev_cleanup_stop   (EV_P_ ev_cleanup *w) EV_THROW;
813*7f2fe78bSCy Schubert # endif
814*7f2fe78bSCy Schubert 
815*7f2fe78bSCy Schubert # if EV_EMBED_ENABLE
816*7f2fe78bSCy Schubert /* only supported when loop to be embedded is in fact embeddable */
817*7f2fe78bSCy Schubert EV_API_DECL void ev_embed_start    (EV_P_ ev_embed *w) EV_THROW;
818*7f2fe78bSCy Schubert EV_API_DECL void ev_embed_stop     (EV_P_ ev_embed *w) EV_THROW;
819*7f2fe78bSCy Schubert EV_API_DECL void ev_embed_sweep    (EV_P_ ev_embed *w) EV_THROW;
820*7f2fe78bSCy Schubert # endif
821*7f2fe78bSCy Schubert 
822*7f2fe78bSCy Schubert # if EV_ASYNC_ENABLE
823*7f2fe78bSCy Schubert EV_API_DECL void ev_async_start    (EV_P_ ev_async *w) EV_THROW;
824*7f2fe78bSCy Schubert EV_API_DECL void ev_async_stop     (EV_P_ ev_async *w) EV_THROW;
825*7f2fe78bSCy Schubert EV_API_DECL void ev_async_send     (EV_P_ ev_async *w) EV_THROW;
826*7f2fe78bSCy Schubert # endif
827*7f2fe78bSCy Schubert 
828*7f2fe78bSCy Schubert #if EV_COMPAT3
829*7f2fe78bSCy Schubert   #define EVLOOP_NONBLOCK EVRUN_NOWAIT
830*7f2fe78bSCy Schubert   #define EVLOOP_ONESHOT  EVRUN_ONCE
831*7f2fe78bSCy Schubert   #define EVUNLOOP_CANCEL EVBREAK_CANCEL
832*7f2fe78bSCy Schubert   #define EVUNLOOP_ONE    EVBREAK_ONE
833*7f2fe78bSCy Schubert   #define EVUNLOOP_ALL    EVBREAK_ALL
834*7f2fe78bSCy Schubert   #if EV_PROTOTYPES
835*7f2fe78bSCy Schubert     EV_INLINE void ev_loop   (EV_P_ int flags) { ev_run   (EV_A_ flags); }
836*7f2fe78bSCy Schubert     EV_INLINE void ev_unloop (EV_P_ int how  ) { ev_break (EV_A_ how  ); }
837*7f2fe78bSCy Schubert     EV_INLINE void ev_default_destroy (void) { ev_loop_destroy (EV_DEFAULT); }
838*7f2fe78bSCy Schubert     EV_INLINE void ev_default_fork    (void) { ev_loop_fork    (EV_DEFAULT); }
839*7f2fe78bSCy Schubert     #if EV_FEATURE_API
840*7f2fe78bSCy Schubert       EV_INLINE unsigned int ev_loop_count  (EV_P) { return ev_iteration  (EV_A); }
841*7f2fe78bSCy Schubert       EV_INLINE unsigned int ev_loop_depth  (EV_P) { return ev_depth      (EV_A); }
842*7f2fe78bSCy Schubert       EV_INLINE void         ev_loop_verify (EV_P) {        ev_verify     (EV_A); }
843*7f2fe78bSCy Schubert     #endif
844*7f2fe78bSCy Schubert   #endif
845*7f2fe78bSCy Schubert #else
846*7f2fe78bSCy Schubert   typedef struct ev_loop ev_loop;
847*7f2fe78bSCy Schubert #endif
848*7f2fe78bSCy Schubert 
849*7f2fe78bSCy Schubert #endif
850*7f2fe78bSCy Schubert 
851*7f2fe78bSCy Schubert EV_CPP(})
852*7f2fe78bSCy Schubert 
853*7f2fe78bSCy Schubert #endif
854*7f2fe78bSCy Schubert 
855