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