1e3a38431SPaul Bohm /*
2e3a38431SPaul Bohm * libev event processing core, watcher management
3e3a38431SPaul Bohm *
4*93823e6cSPaul Bohm * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 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 /* this big block deduces configuration from config.h */
41e3a38431SPaul Bohm #ifndef EV_STANDALONE
42e3a38431SPaul Bohm # ifdef EV_CONFIG_H
43e3a38431SPaul Bohm # include EV_CONFIG_H
44e3a38431SPaul Bohm # else
45e3a38431SPaul Bohm # include "config.h"
46e3a38431SPaul Bohm # endif
47e3a38431SPaul Bohm
48*93823e6cSPaul Bohm # if HAVE_FLOOR
49*93823e6cSPaul Bohm # ifndef EV_USE_FLOOR
50*93823e6cSPaul Bohm # define EV_USE_FLOOR 1
51*93823e6cSPaul Bohm # endif
52*93823e6cSPaul Bohm # endif
53*93823e6cSPaul Bohm
54e3a38431SPaul Bohm # if HAVE_CLOCK_SYSCALL
55e3a38431SPaul Bohm # ifndef EV_USE_CLOCK_SYSCALL
56e3a38431SPaul Bohm # define EV_USE_CLOCK_SYSCALL 1
57e3a38431SPaul Bohm # ifndef EV_USE_REALTIME
58e3a38431SPaul Bohm # define EV_USE_REALTIME 0
59e3a38431SPaul Bohm # endif
60e3a38431SPaul Bohm # ifndef EV_USE_MONOTONIC
61e3a38431SPaul Bohm # define EV_USE_MONOTONIC 1
62e3a38431SPaul Bohm # endif
63e3a38431SPaul Bohm # endif
64*93823e6cSPaul Bohm # elif !defined EV_USE_CLOCK_SYSCALL
65e3a38431SPaul Bohm # define EV_USE_CLOCK_SYSCALL 0
66e3a38431SPaul Bohm # endif
67e3a38431SPaul Bohm
68e3a38431SPaul Bohm # if HAVE_CLOCK_GETTIME
69e3a38431SPaul Bohm # ifndef EV_USE_MONOTONIC
70e3a38431SPaul Bohm # define EV_USE_MONOTONIC 1
71e3a38431SPaul Bohm # endif
72e3a38431SPaul Bohm # ifndef EV_USE_REALTIME
73e3a38431SPaul Bohm # define EV_USE_REALTIME 0
74e3a38431SPaul Bohm # endif
75e3a38431SPaul Bohm # else
76e3a38431SPaul Bohm # ifndef EV_USE_MONOTONIC
77e3a38431SPaul Bohm # define EV_USE_MONOTONIC 0
78e3a38431SPaul Bohm # endif
79e3a38431SPaul Bohm # ifndef EV_USE_REALTIME
80e3a38431SPaul Bohm # define EV_USE_REALTIME 0
81e3a38431SPaul Bohm # endif
82e3a38431SPaul Bohm # endif
83e3a38431SPaul Bohm
84e3a38431SPaul Bohm # if HAVE_NANOSLEEP
85e3a38431SPaul Bohm # ifndef EV_USE_NANOSLEEP
86e3a38431SPaul Bohm # define EV_USE_NANOSLEEP EV_FEATURE_OS
87e3a38431SPaul Bohm # endif
88e3a38431SPaul Bohm # else
89e3a38431SPaul Bohm # undef EV_USE_NANOSLEEP
90e3a38431SPaul Bohm # define EV_USE_NANOSLEEP 0
91e3a38431SPaul Bohm # endif
92e3a38431SPaul Bohm
93e3a38431SPaul Bohm # if HAVE_SELECT && HAVE_SYS_SELECT_H
94e3a38431SPaul Bohm # ifndef EV_USE_SELECT
95e3a38431SPaul Bohm # define EV_USE_SELECT EV_FEATURE_BACKENDS
96e3a38431SPaul Bohm # endif
97e3a38431SPaul Bohm # else
98e3a38431SPaul Bohm # undef EV_USE_SELECT
99e3a38431SPaul Bohm # define EV_USE_SELECT 0
100e3a38431SPaul Bohm # endif
101e3a38431SPaul Bohm
102e3a38431SPaul Bohm # if HAVE_POLL && HAVE_POLL_H
103e3a38431SPaul Bohm # ifndef EV_USE_POLL
104e3a38431SPaul Bohm # define EV_USE_POLL EV_FEATURE_BACKENDS
105e3a38431SPaul Bohm # endif
106e3a38431SPaul Bohm # else
107e3a38431SPaul Bohm # undef EV_USE_POLL
108e3a38431SPaul Bohm # define EV_USE_POLL 0
109e3a38431SPaul Bohm # endif
110e3a38431SPaul Bohm
111e3a38431SPaul Bohm # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
112e3a38431SPaul Bohm # ifndef EV_USE_EPOLL
113e3a38431SPaul Bohm # define EV_USE_EPOLL EV_FEATURE_BACKENDS
114e3a38431SPaul Bohm # endif
115e3a38431SPaul Bohm # else
116e3a38431SPaul Bohm # undef EV_USE_EPOLL
117e3a38431SPaul Bohm # define EV_USE_EPOLL 0
118e3a38431SPaul Bohm # endif
119e3a38431SPaul Bohm
120e3a38431SPaul Bohm # if HAVE_KQUEUE && HAVE_SYS_EVENT_H
121e3a38431SPaul Bohm # ifndef EV_USE_KQUEUE
122e3a38431SPaul Bohm # define EV_USE_KQUEUE EV_FEATURE_BACKENDS
123e3a38431SPaul Bohm # endif
124e3a38431SPaul Bohm # else
125e3a38431SPaul Bohm # undef EV_USE_KQUEUE
126e3a38431SPaul Bohm # define EV_USE_KQUEUE 0
127e3a38431SPaul Bohm # endif
128e3a38431SPaul Bohm
129e3a38431SPaul Bohm # if HAVE_PORT_H && HAVE_PORT_CREATE
130e3a38431SPaul Bohm # ifndef EV_USE_PORT
131e3a38431SPaul Bohm # define EV_USE_PORT EV_FEATURE_BACKENDS
132e3a38431SPaul Bohm # endif
133e3a38431SPaul Bohm # else
134e3a38431SPaul Bohm # undef EV_USE_PORT
135e3a38431SPaul Bohm # define EV_USE_PORT 0
136e3a38431SPaul Bohm # endif
137e3a38431SPaul Bohm
138e3a38431SPaul Bohm # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
139e3a38431SPaul Bohm # ifndef EV_USE_INOTIFY
140e3a38431SPaul Bohm # define EV_USE_INOTIFY EV_FEATURE_OS
141e3a38431SPaul Bohm # endif
142e3a38431SPaul Bohm # else
143e3a38431SPaul Bohm # undef EV_USE_INOTIFY
144e3a38431SPaul Bohm # define EV_USE_INOTIFY 0
145e3a38431SPaul Bohm # endif
146e3a38431SPaul Bohm
147e3a38431SPaul Bohm # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
148e3a38431SPaul Bohm # ifndef EV_USE_SIGNALFD
149e3a38431SPaul Bohm # define EV_USE_SIGNALFD EV_FEATURE_OS
150e3a38431SPaul Bohm # endif
151e3a38431SPaul Bohm # else
152e3a38431SPaul Bohm # undef EV_USE_SIGNALFD
153e3a38431SPaul Bohm # define EV_USE_SIGNALFD 0
154e3a38431SPaul Bohm # endif
155e3a38431SPaul Bohm
156e3a38431SPaul Bohm # if HAVE_EVENTFD
157e3a38431SPaul Bohm # ifndef EV_USE_EVENTFD
158e3a38431SPaul Bohm # define EV_USE_EVENTFD EV_FEATURE_OS
159e3a38431SPaul Bohm # endif
160e3a38431SPaul Bohm # else
161e3a38431SPaul Bohm # undef EV_USE_EVENTFD
162e3a38431SPaul Bohm # define EV_USE_EVENTFD 0
163e3a38431SPaul Bohm # endif
164e3a38431SPaul Bohm
165e3a38431SPaul Bohm #endif
166e3a38431SPaul Bohm
167e3a38431SPaul Bohm #include <stdlib.h>
168e3a38431SPaul Bohm #include <string.h>
169e3a38431SPaul Bohm #include <fcntl.h>
170e3a38431SPaul Bohm #include <stddef.h>
171e3a38431SPaul Bohm
172e3a38431SPaul Bohm #include <stdio.h>
173e3a38431SPaul Bohm
174e3a38431SPaul Bohm #include <assert.h>
175e3a38431SPaul Bohm #include <errno.h>
176e3a38431SPaul Bohm #include <sys/types.h>
177e3a38431SPaul Bohm #include <time.h>
178e3a38431SPaul Bohm #include <limits.h>
179e3a38431SPaul Bohm
180e3a38431SPaul Bohm #include <signal.h>
181e3a38431SPaul Bohm
182e3a38431SPaul Bohm #ifdef EV_H
183e3a38431SPaul Bohm # include EV_H
184e3a38431SPaul Bohm #else
185e3a38431SPaul Bohm # include "ev.h"
186e3a38431SPaul Bohm #endif
187e3a38431SPaul Bohm
188*93823e6cSPaul Bohm #if EV_NO_THREADS
189*93823e6cSPaul Bohm # undef EV_NO_SMP
190*93823e6cSPaul Bohm # define EV_NO_SMP 1
191*93823e6cSPaul Bohm # undef ECB_NO_THREADS
192*93823e6cSPaul Bohm # define ECB_NO_THREADS 1
193*93823e6cSPaul Bohm #endif
194*93823e6cSPaul Bohm #if EV_NO_SMP
195*93823e6cSPaul Bohm # undef EV_NO_SMP
196*93823e6cSPaul Bohm # define ECB_NO_SMP 1
197*93823e6cSPaul Bohm #endif
198e3a38431SPaul Bohm
199e3a38431SPaul Bohm #ifndef _WIN32
200e3a38431SPaul Bohm # include <sys/time.h>
201e3a38431SPaul Bohm # include <sys/wait.h>
202e3a38431SPaul Bohm # include <unistd.h>
203e3a38431SPaul Bohm #else
204e3a38431SPaul Bohm # include <io.h>
205e3a38431SPaul Bohm # define WIN32_LEAN_AND_MEAN
206*93823e6cSPaul Bohm # include <winsock2.h>
207e3a38431SPaul Bohm # include <windows.h>
208e3a38431SPaul Bohm # ifndef EV_SELECT_IS_WINSOCKET
209e3a38431SPaul Bohm # define EV_SELECT_IS_WINSOCKET 1
210e3a38431SPaul Bohm # endif
211e3a38431SPaul Bohm # undef EV_AVOID_STDIO
212e3a38431SPaul Bohm #endif
213e3a38431SPaul Bohm
214e3a38431SPaul Bohm /* OS X, in its infinite idiocy, actually HARDCODES
215e3a38431SPaul Bohm * a limit of 1024 into their select. Where people have brains,
216e3a38431SPaul Bohm * OS X engineers apparently have a vacuum. Or maybe they were
217e3a38431SPaul Bohm * ordered to have a vacuum, or they do anything for money.
218e3a38431SPaul Bohm * This might help. Or not.
219e3a38431SPaul Bohm */
220e3a38431SPaul Bohm #define _DARWIN_UNLIMITED_SELECT 1
221e3a38431SPaul Bohm
222e3a38431SPaul Bohm /* this block tries to deduce configuration from header-defined symbols and defaults */
223e3a38431SPaul Bohm
224e3a38431SPaul Bohm /* try to deduce the maximum number of signals on this platform */
225*93823e6cSPaul Bohm #if defined EV_NSIG
226e3a38431SPaul Bohm /* use what's provided */
227*93823e6cSPaul Bohm #elif defined NSIG
228e3a38431SPaul Bohm # define EV_NSIG (NSIG)
229*93823e6cSPaul Bohm #elif defined _NSIG
230e3a38431SPaul Bohm # define EV_NSIG (_NSIG)
231*93823e6cSPaul Bohm #elif defined SIGMAX
232e3a38431SPaul Bohm # define EV_NSIG (SIGMAX+1)
233*93823e6cSPaul Bohm #elif defined SIG_MAX
234e3a38431SPaul Bohm # define EV_NSIG (SIG_MAX+1)
235*93823e6cSPaul Bohm #elif defined _SIG_MAX
236e3a38431SPaul Bohm # define EV_NSIG (_SIG_MAX+1)
237*93823e6cSPaul Bohm #elif defined MAXSIG
238e3a38431SPaul Bohm # define EV_NSIG (MAXSIG+1)
239*93823e6cSPaul Bohm #elif defined MAX_SIG
240e3a38431SPaul Bohm # define EV_NSIG (MAX_SIG+1)
241*93823e6cSPaul Bohm #elif defined SIGARRAYSIZE
242e3a38431SPaul Bohm # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
243*93823e6cSPaul Bohm #elif defined _sys_nsig
244e3a38431SPaul Bohm # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
245e3a38431SPaul Bohm #else
246*93823e6cSPaul Bohm # define EV_NSIG (8 * sizeof (sigset_t) + 1)
247*93823e6cSPaul Bohm #endif
248*93823e6cSPaul Bohm
249*93823e6cSPaul Bohm #ifndef EV_USE_FLOOR
250*93823e6cSPaul Bohm # define EV_USE_FLOOR 0
251e3a38431SPaul Bohm #endif
252e3a38431SPaul Bohm
253e3a38431SPaul Bohm #ifndef EV_USE_CLOCK_SYSCALL
254*93823e6cSPaul Bohm # if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17
255e3a38431SPaul Bohm # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
256e3a38431SPaul Bohm # else
257e3a38431SPaul Bohm # define EV_USE_CLOCK_SYSCALL 0
258e3a38431SPaul Bohm # endif
259e3a38431SPaul Bohm #endif
260e3a38431SPaul Bohm
261*93823e6cSPaul Bohm #if !(_POSIX_TIMERS > 0)
262e3a38431SPaul Bohm # ifndef EV_USE_MONOTONIC
263*93823e6cSPaul Bohm # define EV_USE_MONOTONIC 0
264*93823e6cSPaul Bohm # endif
265*93823e6cSPaul Bohm # ifndef EV_USE_REALTIME
266*93823e6cSPaul Bohm # define EV_USE_REALTIME 0
267*93823e6cSPaul Bohm # endif
268*93823e6cSPaul Bohm #endif
269*93823e6cSPaul Bohm
270*93823e6cSPaul Bohm #ifndef EV_USE_MONOTONIC
271*93823e6cSPaul Bohm # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
272e3a38431SPaul Bohm # define EV_USE_MONOTONIC EV_FEATURE_OS
273e3a38431SPaul Bohm # else
274e3a38431SPaul Bohm # define EV_USE_MONOTONIC 0
275e3a38431SPaul Bohm # endif
276e3a38431SPaul Bohm #endif
277e3a38431SPaul Bohm
278e3a38431SPaul Bohm #ifndef EV_USE_REALTIME
279e3a38431SPaul Bohm # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
280e3a38431SPaul Bohm #endif
281e3a38431SPaul Bohm
282e3a38431SPaul Bohm #ifndef EV_USE_NANOSLEEP
283e3a38431SPaul Bohm # if _POSIX_C_SOURCE >= 199309L
284e3a38431SPaul Bohm # define EV_USE_NANOSLEEP EV_FEATURE_OS
285e3a38431SPaul Bohm # else
286e3a38431SPaul Bohm # define EV_USE_NANOSLEEP 0
287e3a38431SPaul Bohm # endif
288e3a38431SPaul Bohm #endif
289e3a38431SPaul Bohm
290e3a38431SPaul Bohm #ifndef EV_USE_SELECT
291e3a38431SPaul Bohm # define EV_USE_SELECT EV_FEATURE_BACKENDS
292e3a38431SPaul Bohm #endif
293e3a38431SPaul Bohm
294e3a38431SPaul Bohm #ifndef EV_USE_POLL
295e3a38431SPaul Bohm # ifdef _WIN32
296e3a38431SPaul Bohm # define EV_USE_POLL 0
297e3a38431SPaul Bohm # else
298e3a38431SPaul Bohm # define EV_USE_POLL EV_FEATURE_BACKENDS
299e3a38431SPaul Bohm # endif
300e3a38431SPaul Bohm #endif
301e3a38431SPaul Bohm
302e3a38431SPaul Bohm #ifndef EV_USE_EPOLL
303e3a38431SPaul Bohm # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
304e3a38431SPaul Bohm # define EV_USE_EPOLL EV_FEATURE_BACKENDS
305e3a38431SPaul Bohm # else
306e3a38431SPaul Bohm # define EV_USE_EPOLL 0
307e3a38431SPaul Bohm # endif
308e3a38431SPaul Bohm #endif
309e3a38431SPaul Bohm
310e3a38431SPaul Bohm #ifndef EV_USE_KQUEUE
311e3a38431SPaul Bohm # define EV_USE_KQUEUE 0
312e3a38431SPaul Bohm #endif
313e3a38431SPaul Bohm
314e3a38431SPaul Bohm #ifndef EV_USE_PORT
315e3a38431SPaul Bohm # define EV_USE_PORT 0
316e3a38431SPaul Bohm #endif
317e3a38431SPaul Bohm
318e3a38431SPaul Bohm #ifndef EV_USE_INOTIFY
319e3a38431SPaul Bohm # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
320e3a38431SPaul Bohm # define EV_USE_INOTIFY EV_FEATURE_OS
321e3a38431SPaul Bohm # else
322e3a38431SPaul Bohm # define EV_USE_INOTIFY 0
323e3a38431SPaul Bohm # endif
324e3a38431SPaul Bohm #endif
325e3a38431SPaul Bohm
326e3a38431SPaul Bohm #ifndef EV_PID_HASHSIZE
327e3a38431SPaul Bohm # define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
328e3a38431SPaul Bohm #endif
329e3a38431SPaul Bohm
330e3a38431SPaul Bohm #ifndef EV_INOTIFY_HASHSIZE
331e3a38431SPaul Bohm # define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
332e3a38431SPaul Bohm #endif
333e3a38431SPaul Bohm
334e3a38431SPaul Bohm #ifndef EV_USE_EVENTFD
335e3a38431SPaul Bohm # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
336e3a38431SPaul Bohm # define EV_USE_EVENTFD EV_FEATURE_OS
337e3a38431SPaul Bohm # else
338e3a38431SPaul Bohm # define EV_USE_EVENTFD 0
339e3a38431SPaul Bohm # endif
340e3a38431SPaul Bohm #endif
341e3a38431SPaul Bohm
342e3a38431SPaul Bohm #ifndef EV_USE_SIGNALFD
343e3a38431SPaul Bohm # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
344e3a38431SPaul Bohm # define EV_USE_SIGNALFD EV_FEATURE_OS
345e3a38431SPaul Bohm # else
346e3a38431SPaul Bohm # define EV_USE_SIGNALFD 0
347e3a38431SPaul Bohm # endif
348e3a38431SPaul Bohm #endif
349e3a38431SPaul Bohm
350e3a38431SPaul Bohm #if 0 /* debugging */
351e3a38431SPaul Bohm # define EV_VERIFY 3
352e3a38431SPaul Bohm # define EV_USE_4HEAP 1
353e3a38431SPaul Bohm # define EV_HEAP_CACHE_AT 1
354e3a38431SPaul Bohm #endif
355e3a38431SPaul Bohm
356e3a38431SPaul Bohm #ifndef EV_VERIFY
357e3a38431SPaul Bohm # define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
358e3a38431SPaul Bohm #endif
359e3a38431SPaul Bohm
360e3a38431SPaul Bohm #ifndef EV_USE_4HEAP
361e3a38431SPaul Bohm # define EV_USE_4HEAP EV_FEATURE_DATA
362e3a38431SPaul Bohm #endif
363e3a38431SPaul Bohm
364e3a38431SPaul Bohm #ifndef EV_HEAP_CACHE_AT
365e3a38431SPaul Bohm # define EV_HEAP_CACHE_AT EV_FEATURE_DATA
366e3a38431SPaul Bohm #endif
367e3a38431SPaul Bohm
368*93823e6cSPaul Bohm #ifdef ANDROID
369*93823e6cSPaul Bohm /* supposedly, android doesn't typedef fd_mask */
370*93823e6cSPaul Bohm # undef EV_USE_SELECT
371*93823e6cSPaul Bohm # define EV_USE_SELECT 0
372*93823e6cSPaul Bohm /* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */
373*93823e6cSPaul Bohm # undef EV_USE_CLOCK_SYSCALL
374*93823e6cSPaul Bohm # define EV_USE_CLOCK_SYSCALL 0
375*93823e6cSPaul Bohm #endif
376*93823e6cSPaul Bohm
377*93823e6cSPaul Bohm /* aix's poll.h seems to cause lots of trouble */
378*93823e6cSPaul Bohm #ifdef _AIX
379*93823e6cSPaul Bohm /* AIX has a completely broken poll.h header */
380*93823e6cSPaul Bohm # undef EV_USE_POLL
381*93823e6cSPaul Bohm # define EV_USE_POLL 0
382*93823e6cSPaul Bohm #endif
383*93823e6cSPaul Bohm
384e3a38431SPaul Bohm /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
385e3a38431SPaul Bohm /* which makes programs even slower. might work on other unices, too. */
386e3a38431SPaul Bohm #if EV_USE_CLOCK_SYSCALL
387*93823e6cSPaul Bohm # include <sys/syscall.h>
388e3a38431SPaul Bohm # ifdef SYS_clock_gettime
389e3a38431SPaul Bohm # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
390e3a38431SPaul Bohm # undef EV_USE_MONOTONIC
391e3a38431SPaul Bohm # define EV_USE_MONOTONIC 1
392e3a38431SPaul Bohm # else
393e3a38431SPaul Bohm # undef EV_USE_CLOCK_SYSCALL
394e3a38431SPaul Bohm # define EV_USE_CLOCK_SYSCALL 0
395e3a38431SPaul Bohm # endif
396e3a38431SPaul Bohm #endif
397e3a38431SPaul Bohm
398e3a38431SPaul Bohm /* this block fixes any misconfiguration where we know we run into trouble otherwise */
399e3a38431SPaul Bohm
400e3a38431SPaul Bohm #ifndef CLOCK_MONOTONIC
401e3a38431SPaul Bohm # undef EV_USE_MONOTONIC
402e3a38431SPaul Bohm # define EV_USE_MONOTONIC 0
403e3a38431SPaul Bohm #endif
404e3a38431SPaul Bohm
405e3a38431SPaul Bohm #ifndef CLOCK_REALTIME
406e3a38431SPaul Bohm # undef EV_USE_REALTIME
407e3a38431SPaul Bohm # define EV_USE_REALTIME 0
408e3a38431SPaul Bohm #endif
409e3a38431SPaul Bohm
410e3a38431SPaul Bohm #if !EV_STAT_ENABLE
411e3a38431SPaul Bohm # undef EV_USE_INOTIFY
412e3a38431SPaul Bohm # define EV_USE_INOTIFY 0
413e3a38431SPaul Bohm #endif
414e3a38431SPaul Bohm
415e3a38431SPaul Bohm #if !EV_USE_NANOSLEEP
416e3a38431SPaul Bohm /* hp-ux has it in sys/time.h, which we unconditionally include above */
417*93823e6cSPaul Bohm # if !defined _WIN32 && !defined __hpux
418e3a38431SPaul Bohm # include <sys/select.h>
419e3a38431SPaul Bohm # endif
420e3a38431SPaul Bohm #endif
421e3a38431SPaul Bohm
422e3a38431SPaul Bohm #if EV_USE_INOTIFY
423e3a38431SPaul Bohm # include <sys/statfs.h>
424e3a38431SPaul Bohm # include <sys/inotify.h>
425e3a38431SPaul Bohm /* some very old inotify.h headers don't have IN_DONT_FOLLOW */
426e3a38431SPaul Bohm # ifndef IN_DONT_FOLLOW
427e3a38431SPaul Bohm # undef EV_USE_INOTIFY
428e3a38431SPaul Bohm # define EV_USE_INOTIFY 0
429e3a38431SPaul Bohm # endif
430e3a38431SPaul Bohm #endif
431e3a38431SPaul Bohm
432e3a38431SPaul Bohm #if EV_USE_EVENTFD
433e3a38431SPaul Bohm /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
434e3a38431SPaul Bohm # include <stdint.h>
435e3a38431SPaul Bohm # ifndef EFD_NONBLOCK
436e3a38431SPaul Bohm # define EFD_NONBLOCK O_NONBLOCK
437e3a38431SPaul Bohm # endif
438e3a38431SPaul Bohm # ifndef EFD_CLOEXEC
439e3a38431SPaul Bohm # ifdef O_CLOEXEC
440e3a38431SPaul Bohm # define EFD_CLOEXEC O_CLOEXEC
441e3a38431SPaul Bohm # else
442e3a38431SPaul Bohm # define EFD_CLOEXEC 02000000
443e3a38431SPaul Bohm # endif
444e3a38431SPaul Bohm # endif
445e3a38431SPaul Bohm EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
446e3a38431SPaul Bohm #endif
447e3a38431SPaul Bohm
448e3a38431SPaul Bohm #if EV_USE_SIGNALFD
449e3a38431SPaul Bohm /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
450e3a38431SPaul Bohm # include <stdint.h>
451e3a38431SPaul Bohm # ifndef SFD_NONBLOCK
452e3a38431SPaul Bohm # define SFD_NONBLOCK O_NONBLOCK
453e3a38431SPaul Bohm # endif
454e3a38431SPaul Bohm # ifndef SFD_CLOEXEC
455e3a38431SPaul Bohm # ifdef O_CLOEXEC
456e3a38431SPaul Bohm # define SFD_CLOEXEC O_CLOEXEC
457e3a38431SPaul Bohm # else
458e3a38431SPaul Bohm # define SFD_CLOEXEC 02000000
459e3a38431SPaul Bohm # endif
460e3a38431SPaul Bohm # endif
461e3a38431SPaul Bohm EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
462e3a38431SPaul Bohm
463e3a38431SPaul Bohm struct signalfd_siginfo
464e3a38431SPaul Bohm {
465e3a38431SPaul Bohm uint32_t ssi_signo;
466e3a38431SPaul Bohm char pad[128 - sizeof (uint32_t)];
467e3a38431SPaul Bohm };
468e3a38431SPaul Bohm #endif
469e3a38431SPaul Bohm
470e3a38431SPaul Bohm /**/
471e3a38431SPaul Bohm
472e3a38431SPaul Bohm #if EV_VERIFY >= 3
473e3a38431SPaul Bohm # define EV_FREQUENT_CHECK ev_verify (EV_A)
474e3a38431SPaul Bohm #else
475e3a38431SPaul Bohm # define EV_FREQUENT_CHECK do { } while (0)
476e3a38431SPaul Bohm #endif
477e3a38431SPaul Bohm
478e3a38431SPaul Bohm /*
479*93823e6cSPaul Bohm * This is used to work around floating point rounding problems.
480e3a38431SPaul Bohm * This value is good at least till the year 4000.
481e3a38431SPaul Bohm */
482*93823e6cSPaul Bohm #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
483*93823e6cSPaul Bohm /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
484e3a38431SPaul Bohm
485e3a38431SPaul Bohm #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
486e3a38431SPaul Bohm #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
487e3a38431SPaul Bohm
488e3a38431SPaul Bohm #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
489e3a38431SPaul Bohm #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
490e3a38431SPaul Bohm
491*93823e6cSPaul Bohm /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
492*93823e6cSPaul Bohm /* ECB.H BEGIN */
493*93823e6cSPaul Bohm /*
494*93823e6cSPaul Bohm * libecb - http://software.schmorp.de/pkg/libecb
495*93823e6cSPaul Bohm *
496*93823e6cSPaul Bohm * Copyright (©) 2009-2015 Marc Alexander Lehmann <[email protected]>
497*93823e6cSPaul Bohm * Copyright (©) 2011 Emanuele Giaquinta
498*93823e6cSPaul Bohm * All rights reserved.
499*93823e6cSPaul Bohm *
500*93823e6cSPaul Bohm * Redistribution and use in source and binary forms, with or without modifica-
501*93823e6cSPaul Bohm * tion, are permitted provided that the following conditions are met:
502*93823e6cSPaul Bohm *
503*93823e6cSPaul Bohm * 1. Redistributions of source code must retain the above copyright notice,
504*93823e6cSPaul Bohm * this list of conditions and the following disclaimer.
505*93823e6cSPaul Bohm *
506*93823e6cSPaul Bohm * 2. Redistributions in binary form must reproduce the above copyright
507*93823e6cSPaul Bohm * notice, this list of conditions and the following disclaimer in the
508*93823e6cSPaul Bohm * documentation and/or other materials provided with the distribution.
509*93823e6cSPaul Bohm *
510*93823e6cSPaul Bohm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
511*93823e6cSPaul Bohm * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
512*93823e6cSPaul Bohm * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
513*93823e6cSPaul Bohm * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
514*93823e6cSPaul Bohm * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
515*93823e6cSPaul Bohm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
516*93823e6cSPaul Bohm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
517*93823e6cSPaul Bohm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
518*93823e6cSPaul Bohm * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
519*93823e6cSPaul Bohm * OF THE POSSIBILITY OF SUCH DAMAGE.
520*93823e6cSPaul Bohm *
521*93823e6cSPaul Bohm * Alternatively, the contents of this file may be used under the terms of
522*93823e6cSPaul Bohm * the GNU General Public License ("GPL") version 2 or any later version,
523*93823e6cSPaul Bohm * in which case the provisions of the GPL are applicable instead of
524*93823e6cSPaul Bohm * the above. If you wish to allow the use of your version of this file
525*93823e6cSPaul Bohm * only under the terms of the GPL and not to allow others to use your
526*93823e6cSPaul Bohm * version of this file under the BSD license, indicate your decision
527*93823e6cSPaul Bohm * by deleting the provisions above and replace them with the notice
528*93823e6cSPaul Bohm * and other provisions required by the GPL. If you do not delete the
529*93823e6cSPaul Bohm * provisions above, a recipient may use your version of this file under
530*93823e6cSPaul Bohm * either the BSD or the GPL.
531*93823e6cSPaul Bohm */
532*93823e6cSPaul Bohm
533*93823e6cSPaul Bohm #ifndef ECB_H
534*93823e6cSPaul Bohm #define ECB_H
535*93823e6cSPaul Bohm
536*93823e6cSPaul Bohm /* 16 bits major, 16 bits minor */
537*93823e6cSPaul Bohm #define ECB_VERSION 0x00010005
538*93823e6cSPaul Bohm
539*93823e6cSPaul Bohm #ifdef _WIN32
540*93823e6cSPaul Bohm typedef signed char int8_t;
541*93823e6cSPaul Bohm typedef unsigned char uint8_t;
542*93823e6cSPaul Bohm typedef signed short int16_t;
543*93823e6cSPaul Bohm typedef unsigned short uint16_t;
544*93823e6cSPaul Bohm typedef signed int int32_t;
545*93823e6cSPaul Bohm typedef unsigned int uint32_t;
546*93823e6cSPaul Bohm #if __GNUC__
547*93823e6cSPaul Bohm typedef signed long long int64_t;
548*93823e6cSPaul Bohm typedef unsigned long long uint64_t;
549*93823e6cSPaul Bohm #else /* _MSC_VER || __BORLANDC__ */
550*93823e6cSPaul Bohm typedef signed __int64 int64_t;
551*93823e6cSPaul Bohm typedef unsigned __int64 uint64_t;
552*93823e6cSPaul Bohm #endif
553*93823e6cSPaul Bohm #ifdef _WIN64
554*93823e6cSPaul Bohm #define ECB_PTRSIZE 8
555*93823e6cSPaul Bohm typedef uint64_t uintptr_t;
556*93823e6cSPaul Bohm typedef int64_t intptr_t;
557e3a38431SPaul Bohm #else
558*93823e6cSPaul Bohm #define ECB_PTRSIZE 4
559*93823e6cSPaul Bohm typedef uint32_t uintptr_t;
560*93823e6cSPaul Bohm typedef int32_t intptr_t;
561*93823e6cSPaul Bohm #endif
562*93823e6cSPaul Bohm #else
563*93823e6cSPaul Bohm #include <inttypes.h>
564*93823e6cSPaul Bohm #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU
565*93823e6cSPaul Bohm #define ECB_PTRSIZE 8
566*93823e6cSPaul Bohm #else
567*93823e6cSPaul Bohm #define ECB_PTRSIZE 4
568e3a38431SPaul Bohm #endif
569e3a38431SPaul Bohm #endif
570e3a38431SPaul Bohm
571*93823e6cSPaul Bohm #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__)
572*93823e6cSPaul Bohm #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64)
573*93823e6cSPaul Bohm
574*93823e6cSPaul Bohm /* work around x32 idiocy by defining proper macros */
575*93823e6cSPaul Bohm #if ECB_GCC_AMD64 || ECB_MSVC_AMD64
576*93823e6cSPaul Bohm #if _ILP32
577*93823e6cSPaul Bohm #define ECB_AMD64_X32 1
578*93823e6cSPaul Bohm #else
579*93823e6cSPaul Bohm #define ECB_AMD64 1
580*93823e6cSPaul Bohm #endif
581*93823e6cSPaul Bohm #endif
582*93823e6cSPaul Bohm
583*93823e6cSPaul Bohm /* many compilers define _GNUC_ to some versions but then only implement
584*93823e6cSPaul Bohm * what their idiot authors think are the "more important" extensions,
585*93823e6cSPaul Bohm * causing enormous grief in return for some better fake benchmark numbers.
586*93823e6cSPaul Bohm * or so.
587*93823e6cSPaul Bohm * we try to detect these and simply assume they are not gcc - if they have
588*93823e6cSPaul Bohm * an issue with that they should have done it right in the first place.
589*93823e6cSPaul Bohm */
590*93823e6cSPaul Bohm #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
591*93823e6cSPaul Bohm #define ECB_GCC_VERSION(major,minor) 0
592*93823e6cSPaul Bohm #else
593*93823e6cSPaul Bohm #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
594*93823e6cSPaul Bohm #endif
595*93823e6cSPaul Bohm
596*93823e6cSPaul Bohm #define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor)))
597*93823e6cSPaul Bohm
598*93823e6cSPaul Bohm #if __clang__ && defined __has_builtin
599*93823e6cSPaul Bohm #define ECB_CLANG_BUILTIN(x) __has_builtin (x)
600*93823e6cSPaul Bohm #else
601*93823e6cSPaul Bohm #define ECB_CLANG_BUILTIN(x) 0
602*93823e6cSPaul Bohm #endif
603*93823e6cSPaul Bohm
604*93823e6cSPaul Bohm #if __clang__ && defined __has_extension
605*93823e6cSPaul Bohm #define ECB_CLANG_EXTENSION(x) __has_extension (x)
606*93823e6cSPaul Bohm #else
607*93823e6cSPaul Bohm #define ECB_CLANG_EXTENSION(x) 0
608*93823e6cSPaul Bohm #endif
609*93823e6cSPaul Bohm
610*93823e6cSPaul Bohm #define ECB_CPP (__cplusplus+0)
611*93823e6cSPaul Bohm #define ECB_CPP11 (__cplusplus >= 201103L)
612*93823e6cSPaul Bohm
613*93823e6cSPaul Bohm #if ECB_CPP
614*93823e6cSPaul Bohm #define ECB_C 0
615*93823e6cSPaul Bohm #define ECB_STDC_VERSION 0
616*93823e6cSPaul Bohm #else
617*93823e6cSPaul Bohm #define ECB_C 1
618*93823e6cSPaul Bohm #define ECB_STDC_VERSION __STDC_VERSION__
619*93823e6cSPaul Bohm #endif
620*93823e6cSPaul Bohm
621*93823e6cSPaul Bohm #define ECB_C99 (ECB_STDC_VERSION >= 199901L)
622*93823e6cSPaul Bohm #define ECB_C11 (ECB_STDC_VERSION >= 201112L)
623*93823e6cSPaul Bohm
624*93823e6cSPaul Bohm #if ECB_CPP
625*93823e6cSPaul Bohm #define ECB_EXTERN_C extern "C"
626*93823e6cSPaul Bohm #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
627*93823e6cSPaul Bohm #define ECB_EXTERN_C_END }
628*93823e6cSPaul Bohm #else
629*93823e6cSPaul Bohm #define ECB_EXTERN_C extern
630*93823e6cSPaul Bohm #define ECB_EXTERN_C_BEG
631*93823e6cSPaul Bohm #define ECB_EXTERN_C_END
632*93823e6cSPaul Bohm #endif
633*93823e6cSPaul Bohm
634*93823e6cSPaul Bohm /*****************************************************************************/
635*93823e6cSPaul Bohm
636*93823e6cSPaul Bohm /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
637*93823e6cSPaul Bohm /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
638*93823e6cSPaul Bohm
639*93823e6cSPaul Bohm #if ECB_NO_THREADS
640*93823e6cSPaul Bohm #define ECB_NO_SMP 1
641*93823e6cSPaul Bohm #endif
642*93823e6cSPaul Bohm
643*93823e6cSPaul Bohm #if ECB_NO_SMP
644*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE do { } while (0)
645*93823e6cSPaul Bohm #endif
646*93823e6cSPaul Bohm
647*93823e6cSPaul Bohm /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */
648*93823e6cSPaul Bohm #if __xlC__ && ECB_CPP
649*93823e6cSPaul Bohm #include <builtins.h>
650*93823e6cSPaul Bohm #endif
651*93823e6cSPaul Bohm
652*93823e6cSPaul Bohm #if 1400 <= _MSC_VER
653*93823e6cSPaul Bohm #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
654*93823e6cSPaul Bohm #endif
655*93823e6cSPaul Bohm
656*93823e6cSPaul Bohm #ifndef ECB_MEMORY_FENCE
657*93823e6cSPaul Bohm #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
658*93823e6cSPaul Bohm #if __i386 || __i386__
659*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
660*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
661*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
662*93823e6cSPaul Bohm #elif ECB_GCC_AMD64
663*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
664*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
665*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
666*93823e6cSPaul Bohm #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
667*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
668*93823e6cSPaul Bohm #elif defined __ARM_ARCH_2__ \
669*93823e6cSPaul Bohm || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \
670*93823e6cSPaul Bohm || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \
671*93823e6cSPaul Bohm || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \
672*93823e6cSPaul Bohm || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \
673*93823e6cSPaul Bohm || defined __ARM_ARCH_5TEJ__
674*93823e6cSPaul Bohm /* should not need any, unless running old code on newer cpu - arm doesn't support that */
675*93823e6cSPaul Bohm #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
676*93823e6cSPaul Bohm || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \
677*93823e6cSPaul Bohm || defined __ARM_ARCH_6T2__
678*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
679*93823e6cSPaul Bohm #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
680*93823e6cSPaul Bohm || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
681*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
682*93823e6cSPaul Bohm #elif __aarch64__
683*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
684*93823e6cSPaul Bohm #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
685*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
686*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
687*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
688*93823e6cSPaul Bohm #elif defined __s390__ || defined __s390x__
689*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
690*93823e6cSPaul Bohm #elif defined __mips__
691*93823e6cSPaul Bohm /* GNU/Linux emulates sync on mips1 architectures, so we force its use */
692*93823e6cSPaul Bohm /* anybody else who still uses mips1 is supposed to send in their version, with detection code. */
693*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory")
694*93823e6cSPaul Bohm #elif defined __alpha__
695*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
696*93823e6cSPaul Bohm #elif defined __hppa__
697*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
698*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
699*93823e6cSPaul Bohm #elif defined __ia64__
700*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
701*93823e6cSPaul Bohm #elif defined __m68k__
702*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
703*93823e6cSPaul Bohm #elif defined __m88k__
704*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory")
705*93823e6cSPaul Bohm #elif defined __sh__
706*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
707*93823e6cSPaul Bohm #endif
708*93823e6cSPaul Bohm #endif
709*93823e6cSPaul Bohm #endif
710*93823e6cSPaul Bohm
711*93823e6cSPaul Bohm #ifndef ECB_MEMORY_FENCE
712*93823e6cSPaul Bohm #if ECB_GCC_VERSION(4,7)
713*93823e6cSPaul Bohm /* see comment below (stdatomic.h) about the C11 memory model. */
714*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
715*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE)
716*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE)
717*93823e6cSPaul Bohm
718*93823e6cSPaul Bohm #elif ECB_CLANG_EXTENSION(c_atomic)
719*93823e6cSPaul Bohm /* see comment below (stdatomic.h) about the C11 memory model. */
720*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
721*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE)
722*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE)
723*93823e6cSPaul Bohm
724*93823e6cSPaul Bohm #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
725*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __sync_synchronize ()
726*93823e6cSPaul Bohm #elif _MSC_VER >= 1500 /* VC++ 2008 */
727*93823e6cSPaul Bohm /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */
728*93823e6cSPaul Bohm #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
729*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE _ReadWriteBarrier (); MemoryBarrier()
730*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier (); MemoryBarrier() /* according to msdn, _ReadBarrier is not a load fence */
731*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier (); MemoryBarrier()
732*93823e6cSPaul Bohm #elif _MSC_VER >= 1400 /* VC++ 2005 */
733*93823e6cSPaul Bohm #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
734*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
735*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
736*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
737*93823e6cSPaul Bohm #elif defined _WIN32
738*93823e6cSPaul Bohm #include <WinNT.h>
739*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
740*93823e6cSPaul Bohm #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
741*93823e6cSPaul Bohm #include <mbarrier.h>
742*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __machine_rw_barrier ()
743*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
744*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
745*93823e6cSPaul Bohm #elif __xlC__
746*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE __sync ()
747*93823e6cSPaul Bohm #endif
748*93823e6cSPaul Bohm #endif
749*93823e6cSPaul Bohm
750*93823e6cSPaul Bohm #ifndef ECB_MEMORY_FENCE
751*93823e6cSPaul Bohm #if ECB_C11 && !defined __STDC_NO_ATOMICS__
752*93823e6cSPaul Bohm /* we assume that these memory fences work on all variables/all memory accesses, */
753*93823e6cSPaul Bohm /* not just C11 atomics and atomic accesses */
754*93823e6cSPaul Bohm #include <stdatomic.h>
755*93823e6cSPaul Bohm /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
756*93823e6cSPaul Bohm /* any fence other than seq_cst, which isn't very efficient for us. */
757*93823e6cSPaul Bohm /* Why that is, we don't know - either the C11 memory model is quite useless */
758*93823e6cSPaul Bohm /* for most usages, or gcc and clang have a bug */
759*93823e6cSPaul Bohm /* I *currently* lean towards the latter, and inefficiently implement */
760*93823e6cSPaul Bohm /* all three of ecb's fences as a seq_cst fence */
761*93823e6cSPaul Bohm /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */
762*93823e6cSPaul Bohm /* for all __atomic_thread_fence's except seq_cst */
763*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
764*93823e6cSPaul Bohm #endif
765*93823e6cSPaul Bohm #endif
766*93823e6cSPaul Bohm
767*93823e6cSPaul Bohm #ifndef ECB_MEMORY_FENCE
768*93823e6cSPaul Bohm #if !ECB_AVOID_PTHREADS
769*93823e6cSPaul Bohm /*
770*93823e6cSPaul Bohm * if you get undefined symbol references to pthread_mutex_lock,
771*93823e6cSPaul Bohm * or failure to find pthread.h, then you should implement
772*93823e6cSPaul Bohm * the ECB_MEMORY_FENCE operations for your cpu/compiler
773*93823e6cSPaul Bohm * OR provide pthread.h and link against the posix thread library
774*93823e6cSPaul Bohm * of your system.
775*93823e6cSPaul Bohm */
776*93823e6cSPaul Bohm #include <pthread.h>
777*93823e6cSPaul Bohm #define ECB_NEEDS_PTHREADS 1
778*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
779*93823e6cSPaul Bohm
780*93823e6cSPaul Bohm static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
781*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
782*93823e6cSPaul Bohm #endif
783*93823e6cSPaul Bohm #endif
784*93823e6cSPaul Bohm
785*93823e6cSPaul Bohm #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
786*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
787*93823e6cSPaul Bohm #endif
788*93823e6cSPaul Bohm
789*93823e6cSPaul Bohm #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
790*93823e6cSPaul Bohm #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
791*93823e6cSPaul Bohm #endif
792*93823e6cSPaul Bohm
793*93823e6cSPaul Bohm /*****************************************************************************/
794*93823e6cSPaul Bohm
795*93823e6cSPaul Bohm #if ECB_CPP
796*93823e6cSPaul Bohm #define ecb_inline static inline
797*93823e6cSPaul Bohm #elif ECB_GCC_VERSION(2,5)
798*93823e6cSPaul Bohm #define ecb_inline static __inline__
799*93823e6cSPaul Bohm #elif ECB_C99
800*93823e6cSPaul Bohm #define ecb_inline static inline
801*93823e6cSPaul Bohm #else
802*93823e6cSPaul Bohm #define ecb_inline static
803*93823e6cSPaul Bohm #endif
804*93823e6cSPaul Bohm
805*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,3)
806*93823e6cSPaul Bohm #define ecb_restrict __restrict__
807*93823e6cSPaul Bohm #elif ECB_C99
808*93823e6cSPaul Bohm #define ecb_restrict restrict
809*93823e6cSPaul Bohm #else
810*93823e6cSPaul Bohm #define ecb_restrict
811*93823e6cSPaul Bohm #endif
812*93823e6cSPaul Bohm
813*93823e6cSPaul Bohm typedef int ecb_bool;
814*93823e6cSPaul Bohm
815*93823e6cSPaul Bohm #define ECB_CONCAT_(a, b) a ## b
816*93823e6cSPaul Bohm #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
817*93823e6cSPaul Bohm #define ECB_STRINGIFY_(a) # a
818*93823e6cSPaul Bohm #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
819*93823e6cSPaul Bohm #define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr))
820*93823e6cSPaul Bohm
821*93823e6cSPaul Bohm #define ecb_function_ ecb_inline
822*93823e6cSPaul Bohm
823*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8)
824*93823e6cSPaul Bohm #define ecb_attribute(attrlist) __attribute__ (attrlist)
825*93823e6cSPaul Bohm #else
826*93823e6cSPaul Bohm #define ecb_attribute(attrlist)
827*93823e6cSPaul Bohm #endif
828*93823e6cSPaul Bohm
829*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_constant_p)
830*93823e6cSPaul Bohm #define ecb_is_constant(expr) __builtin_constant_p (expr)
831*93823e6cSPaul Bohm #else
832*93823e6cSPaul Bohm /* possible C11 impl for integral types
833*93823e6cSPaul Bohm typedef struct ecb_is_constant_struct ecb_is_constant_struct;
834*93823e6cSPaul Bohm #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */
835*93823e6cSPaul Bohm
836*93823e6cSPaul Bohm #define ecb_is_constant(expr) 0
837*93823e6cSPaul Bohm #endif
838*93823e6cSPaul Bohm
839*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_expect)
840*93823e6cSPaul Bohm #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
841*93823e6cSPaul Bohm #else
842*93823e6cSPaul Bohm #define ecb_expect(expr,value) (expr)
843*93823e6cSPaul Bohm #endif
844*93823e6cSPaul Bohm
845*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_prefetch)
846*93823e6cSPaul Bohm #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
847*93823e6cSPaul Bohm #else
848*93823e6cSPaul Bohm #define ecb_prefetch(addr,rw,locality)
849*93823e6cSPaul Bohm #endif
850*93823e6cSPaul Bohm
851*93823e6cSPaul Bohm /* no emulation for ecb_decltype */
852*93823e6cSPaul Bohm #if ECB_CPP11
853*93823e6cSPaul Bohm // older implementations might have problems with decltype(x)::type, work around it
854*93823e6cSPaul Bohm template<class T> struct ecb_decltype_t { typedef T type; };
855*93823e6cSPaul Bohm #define ecb_decltype(x) ecb_decltype_t<decltype (x)>::type
856*93823e6cSPaul Bohm #elif ECB_GCC_VERSION(3,0) || ECB_CLANG_VERSION(2,8)
857*93823e6cSPaul Bohm #define ecb_decltype(x) __typeof__ (x)
858*93823e6cSPaul Bohm #endif
859*93823e6cSPaul Bohm
860*93823e6cSPaul Bohm #if _MSC_VER >= 1300
861*93823e6cSPaul Bohm #define ecb_deprecated __declspec (deprecated)
862*93823e6cSPaul Bohm #else
863*93823e6cSPaul Bohm #define ecb_deprecated ecb_attribute ((__deprecated__))
864*93823e6cSPaul Bohm #endif
865*93823e6cSPaul Bohm
866*93823e6cSPaul Bohm #if _MSC_VER >= 1500
867*93823e6cSPaul Bohm #define ecb_deprecated_message(msg) __declspec (deprecated (msg))
868*93823e6cSPaul Bohm #elif ECB_GCC_VERSION(4,5)
869*93823e6cSPaul Bohm #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg))
870*93823e6cSPaul Bohm #else
871*93823e6cSPaul Bohm #define ecb_deprecated_message(msg) ecb_deprecated
872*93823e6cSPaul Bohm #endif
873*93823e6cSPaul Bohm
874*93823e6cSPaul Bohm #if _MSC_VER >= 1400
875*93823e6cSPaul Bohm #define ecb_noinline __declspec (noinline)
876*93823e6cSPaul Bohm #else
877*93823e6cSPaul Bohm #define ecb_noinline ecb_attribute ((__noinline__))
878*93823e6cSPaul Bohm #endif
879*93823e6cSPaul Bohm
880*93823e6cSPaul Bohm #define ecb_unused ecb_attribute ((__unused__))
881*93823e6cSPaul Bohm #define ecb_const ecb_attribute ((__const__))
882*93823e6cSPaul Bohm #define ecb_pure ecb_attribute ((__pure__))
883*93823e6cSPaul Bohm
884*93823e6cSPaul Bohm #if ECB_C11 || __IBMC_NORETURN
885*93823e6cSPaul Bohm /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */
886*93823e6cSPaul Bohm #define ecb_noreturn _Noreturn
887*93823e6cSPaul Bohm #elif ECB_CPP11
888*93823e6cSPaul Bohm #define ecb_noreturn [[noreturn]]
889*93823e6cSPaul Bohm #elif _MSC_VER >= 1200
890*93823e6cSPaul Bohm /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */
891*93823e6cSPaul Bohm #define ecb_noreturn __declspec (noreturn)
892*93823e6cSPaul Bohm #else
893*93823e6cSPaul Bohm #define ecb_noreturn ecb_attribute ((__noreturn__))
894*93823e6cSPaul Bohm #endif
895*93823e6cSPaul Bohm
896*93823e6cSPaul Bohm #if ECB_GCC_VERSION(4,3)
897*93823e6cSPaul Bohm #define ecb_artificial ecb_attribute ((__artificial__))
898*93823e6cSPaul Bohm #define ecb_hot ecb_attribute ((__hot__))
899*93823e6cSPaul Bohm #define ecb_cold ecb_attribute ((__cold__))
900*93823e6cSPaul Bohm #else
901*93823e6cSPaul Bohm #define ecb_artificial
902*93823e6cSPaul Bohm #define ecb_hot
903*93823e6cSPaul Bohm #define ecb_cold
904*93823e6cSPaul Bohm #endif
905*93823e6cSPaul Bohm
906*93823e6cSPaul Bohm /* put around conditional expressions if you are very sure that the */
907*93823e6cSPaul Bohm /* expression is mostly true or mostly false. note that these return */
908*93823e6cSPaul Bohm /* booleans, not the expression. */
909*93823e6cSPaul Bohm #define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
910*93823e6cSPaul Bohm #define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
911*93823e6cSPaul Bohm /* for compatibility to the rest of the world */
912*93823e6cSPaul Bohm #define ecb_likely(expr) ecb_expect_true (expr)
913*93823e6cSPaul Bohm #define ecb_unlikely(expr) ecb_expect_false (expr)
914*93823e6cSPaul Bohm
915*93823e6cSPaul Bohm /* count trailing zero bits and count # of one bits */
916*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,4) \
917*93823e6cSPaul Bohm || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \
918*93823e6cSPaul Bohm && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \
919*93823e6cSPaul Bohm && ECB_CLANG_BUILTIN(__builtin_popcount))
920*93823e6cSPaul Bohm /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
921*93823e6cSPaul Bohm #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
922*93823e6cSPaul Bohm #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
923*93823e6cSPaul Bohm #define ecb_ctz32(x) __builtin_ctz (x)
924*93823e6cSPaul Bohm #define ecb_ctz64(x) __builtin_ctzll (x)
925*93823e6cSPaul Bohm #define ecb_popcount32(x) __builtin_popcount (x)
926*93823e6cSPaul Bohm /* no popcountll */
927*93823e6cSPaul Bohm #else
928*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_ctz32 (uint32_t x);
929*93823e6cSPaul Bohm ecb_function_ ecb_const int
ecb_ctz32(uint32_t x)930*93823e6cSPaul Bohm ecb_ctz32 (uint32_t x)
931*93823e6cSPaul Bohm {
932*93823e6cSPaul Bohm #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
933*93823e6cSPaul Bohm unsigned long r;
934*93823e6cSPaul Bohm _BitScanForward (&r, x);
935*93823e6cSPaul Bohm return (int)r;
936*93823e6cSPaul Bohm #else
937*93823e6cSPaul Bohm int r = 0;
938*93823e6cSPaul Bohm
939*93823e6cSPaul Bohm x &= ~x + 1; /* this isolates the lowest bit */
940*93823e6cSPaul Bohm
941*93823e6cSPaul Bohm #if ECB_branchless_on_i386
942*93823e6cSPaul Bohm r += !!(x & 0xaaaaaaaa) << 0;
943*93823e6cSPaul Bohm r += !!(x & 0xcccccccc) << 1;
944*93823e6cSPaul Bohm r += !!(x & 0xf0f0f0f0) << 2;
945*93823e6cSPaul Bohm r += !!(x & 0xff00ff00) << 3;
946*93823e6cSPaul Bohm r += !!(x & 0xffff0000) << 4;
947*93823e6cSPaul Bohm #else
948*93823e6cSPaul Bohm if (x & 0xaaaaaaaa) r += 1;
949*93823e6cSPaul Bohm if (x & 0xcccccccc) r += 2;
950*93823e6cSPaul Bohm if (x & 0xf0f0f0f0) r += 4;
951*93823e6cSPaul Bohm if (x & 0xff00ff00) r += 8;
952*93823e6cSPaul Bohm if (x & 0xffff0000) r += 16;
953*93823e6cSPaul Bohm #endif
954*93823e6cSPaul Bohm
955*93823e6cSPaul Bohm return r;
956*93823e6cSPaul Bohm #endif
957*93823e6cSPaul Bohm }
958*93823e6cSPaul Bohm
959*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_ctz64 (uint64_t x);
960*93823e6cSPaul Bohm ecb_function_ ecb_const int
ecb_ctz64(uint64_t x)961*93823e6cSPaul Bohm ecb_ctz64 (uint64_t x)
962*93823e6cSPaul Bohm {
963*93823e6cSPaul Bohm #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
964*93823e6cSPaul Bohm unsigned long r;
965*93823e6cSPaul Bohm _BitScanForward64 (&r, x);
966*93823e6cSPaul Bohm return (int)r;
967*93823e6cSPaul Bohm #else
968*93823e6cSPaul Bohm int shift = x & 0xffffffff ? 0 : 32;
969*93823e6cSPaul Bohm return ecb_ctz32 (x >> shift) + shift;
970*93823e6cSPaul Bohm #endif
971*93823e6cSPaul Bohm }
972*93823e6cSPaul Bohm
973*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_popcount32 (uint32_t x);
974*93823e6cSPaul Bohm ecb_function_ ecb_const int
ecb_popcount32(uint32_t x)975*93823e6cSPaul Bohm ecb_popcount32 (uint32_t x)
976*93823e6cSPaul Bohm {
977*93823e6cSPaul Bohm x -= (x >> 1) & 0x55555555;
978*93823e6cSPaul Bohm x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
979*93823e6cSPaul Bohm x = ((x >> 4) + x) & 0x0f0f0f0f;
980*93823e6cSPaul Bohm x *= 0x01010101;
981*93823e6cSPaul Bohm
982*93823e6cSPaul Bohm return x >> 24;
983*93823e6cSPaul Bohm }
984*93823e6cSPaul Bohm
985*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_ld32 (uint32_t x);
ecb_ld32(uint32_t x)986*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_ld32 (uint32_t x)
987*93823e6cSPaul Bohm {
988*93823e6cSPaul Bohm #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
989*93823e6cSPaul Bohm unsigned long r;
990*93823e6cSPaul Bohm _BitScanReverse (&r, x);
991*93823e6cSPaul Bohm return (int)r;
992*93823e6cSPaul Bohm #else
993*93823e6cSPaul Bohm int r = 0;
994*93823e6cSPaul Bohm
995*93823e6cSPaul Bohm if (x >> 16) { x >>= 16; r += 16; }
996*93823e6cSPaul Bohm if (x >> 8) { x >>= 8; r += 8; }
997*93823e6cSPaul Bohm if (x >> 4) { x >>= 4; r += 4; }
998*93823e6cSPaul Bohm if (x >> 2) { x >>= 2; r += 2; }
999*93823e6cSPaul Bohm if (x >> 1) { r += 1; }
1000*93823e6cSPaul Bohm
1001*93823e6cSPaul Bohm return r;
1002*93823e6cSPaul Bohm #endif
1003*93823e6cSPaul Bohm }
1004*93823e6cSPaul Bohm
1005*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_ld64 (uint64_t x);
ecb_ld64(uint64_t x)1006*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_ld64 (uint64_t x)
1007*93823e6cSPaul Bohm {
1008*93823e6cSPaul Bohm #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
1009*93823e6cSPaul Bohm unsigned long r;
1010*93823e6cSPaul Bohm _BitScanReverse64 (&r, x);
1011*93823e6cSPaul Bohm return (int)r;
1012*93823e6cSPaul Bohm #else
1013*93823e6cSPaul Bohm int r = 0;
1014*93823e6cSPaul Bohm
1015*93823e6cSPaul Bohm if (x >> 32) { x >>= 32; r += 32; }
1016*93823e6cSPaul Bohm
1017*93823e6cSPaul Bohm return r + ecb_ld32 (x);
1018*93823e6cSPaul Bohm #endif
1019*93823e6cSPaul Bohm }
1020*93823e6cSPaul Bohm #endif
1021*93823e6cSPaul Bohm
1022*93823e6cSPaul Bohm ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x);
ecb_is_pot32(uint32_t x)1023*93823e6cSPaul Bohm ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
1024*93823e6cSPaul Bohm ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x);
ecb_is_pot64(uint64_t x)1025*93823e6cSPaul Bohm ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
1026*93823e6cSPaul Bohm
1027*93823e6cSPaul Bohm ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x);
ecb_bitrev8(uint8_t x)1028*93823e6cSPaul Bohm ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x)
1029*93823e6cSPaul Bohm {
1030*93823e6cSPaul Bohm return ( (x * 0x0802U & 0x22110U)
1031*93823e6cSPaul Bohm | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
1032*93823e6cSPaul Bohm }
1033*93823e6cSPaul Bohm
1034*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x);
ecb_bitrev16(uint16_t x)1035*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x)
1036*93823e6cSPaul Bohm {
1037*93823e6cSPaul Bohm x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
1038*93823e6cSPaul Bohm x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
1039*93823e6cSPaul Bohm x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
1040*93823e6cSPaul Bohm x = ( x >> 8 ) | ( x << 8);
1041*93823e6cSPaul Bohm
1042*93823e6cSPaul Bohm return x;
1043*93823e6cSPaul Bohm }
1044*93823e6cSPaul Bohm
1045*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x);
ecb_bitrev32(uint32_t x)1046*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x)
1047*93823e6cSPaul Bohm {
1048*93823e6cSPaul Bohm x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
1049*93823e6cSPaul Bohm x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
1050*93823e6cSPaul Bohm x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
1051*93823e6cSPaul Bohm x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
1052*93823e6cSPaul Bohm x = ( x >> 16 ) | ( x << 16);
1053*93823e6cSPaul Bohm
1054*93823e6cSPaul Bohm return x;
1055*93823e6cSPaul Bohm }
1056*93823e6cSPaul Bohm
1057*93823e6cSPaul Bohm /* popcount64 is only available on 64 bit cpus as gcc builtin */
1058*93823e6cSPaul Bohm /* so for this version we are lazy */
1059*93823e6cSPaul Bohm ecb_function_ ecb_const int ecb_popcount64 (uint64_t x);
1060*93823e6cSPaul Bohm ecb_function_ ecb_const int
ecb_popcount64(uint64_t x)1061*93823e6cSPaul Bohm ecb_popcount64 (uint64_t x)
1062*93823e6cSPaul Bohm {
1063*93823e6cSPaul Bohm return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
1064*93823e6cSPaul Bohm }
1065*93823e6cSPaul Bohm
1066*93823e6cSPaul Bohm ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count);
1067*93823e6cSPaul Bohm ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count);
1068*93823e6cSPaul Bohm ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count);
1069*93823e6cSPaul Bohm ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count);
1070*93823e6cSPaul Bohm ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count);
1071*93823e6cSPaul Bohm ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count);
1072*93823e6cSPaul Bohm ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count);
1073*93823e6cSPaul Bohm ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count);
1074*93823e6cSPaul Bohm
ecb_rotl8(uint8_t x,unsigned int count)1075*93823e6cSPaul Bohm ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
ecb_rotr8(uint8_t x,unsigned int count)1076*93823e6cSPaul Bohm ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
ecb_rotl16(uint16_t x,unsigned int count)1077*93823e6cSPaul Bohm ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
ecb_rotr16(uint16_t x,unsigned int count)1078*93823e6cSPaul Bohm ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
ecb_rotl32(uint32_t x,unsigned int count)1079*93823e6cSPaul Bohm ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
ecb_rotr32(uint32_t x,unsigned int count)1080*93823e6cSPaul Bohm ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
ecb_rotl64(uint64_t x,unsigned int count)1081*93823e6cSPaul Bohm ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
ecb_rotr64(uint64_t x,unsigned int count)1082*93823e6cSPaul Bohm ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
1083*93823e6cSPaul Bohm
1084*93823e6cSPaul Bohm #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64))
1085*93823e6cSPaul Bohm #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16)
1086*93823e6cSPaul Bohm #define ecb_bswap16(x) __builtin_bswap16 (x)
1087*93823e6cSPaul Bohm #else
1088*93823e6cSPaul Bohm #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
1089*93823e6cSPaul Bohm #endif
1090*93823e6cSPaul Bohm #define ecb_bswap32(x) __builtin_bswap32 (x)
1091*93823e6cSPaul Bohm #define ecb_bswap64(x) __builtin_bswap64 (x)
1092*93823e6cSPaul Bohm #elif _MSC_VER
1093*93823e6cSPaul Bohm #include <stdlib.h>
1094*93823e6cSPaul Bohm #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x)))
1095*93823e6cSPaul Bohm #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x)))
1096*93823e6cSPaul Bohm #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x)))
1097*93823e6cSPaul Bohm #else
1098*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x);
1099*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t
ecb_bswap16(uint16_t x)1100*93823e6cSPaul Bohm ecb_bswap16 (uint16_t x)
1101*93823e6cSPaul Bohm {
1102*93823e6cSPaul Bohm return ecb_rotl16 (x, 8);
1103*93823e6cSPaul Bohm }
1104*93823e6cSPaul Bohm
1105*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x);
1106*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t
ecb_bswap32(uint32_t x)1107*93823e6cSPaul Bohm ecb_bswap32 (uint32_t x)
1108*93823e6cSPaul Bohm {
1109*93823e6cSPaul Bohm return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
1110*93823e6cSPaul Bohm }
1111*93823e6cSPaul Bohm
1112*93823e6cSPaul Bohm ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x);
1113*93823e6cSPaul Bohm ecb_function_ ecb_const uint64_t
ecb_bswap64(uint64_t x)1114*93823e6cSPaul Bohm ecb_bswap64 (uint64_t x)
1115*93823e6cSPaul Bohm {
1116*93823e6cSPaul Bohm return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
1117*93823e6cSPaul Bohm }
1118*93823e6cSPaul Bohm #endif
1119*93823e6cSPaul Bohm
1120*93823e6cSPaul Bohm #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable)
1121*93823e6cSPaul Bohm #define ecb_unreachable() __builtin_unreachable ()
1122*93823e6cSPaul Bohm #else
1123*93823e6cSPaul Bohm /* this seems to work fine, but gcc always emits a warning for it :/ */
1124*93823e6cSPaul Bohm ecb_inline ecb_noreturn void ecb_unreachable (void);
ecb_unreachable(void)1125*93823e6cSPaul Bohm ecb_inline ecb_noreturn void ecb_unreachable (void) { }
1126*93823e6cSPaul Bohm #endif
1127*93823e6cSPaul Bohm
1128*93823e6cSPaul Bohm /* try to tell the compiler that some condition is definitely true */
1129*93823e6cSPaul Bohm #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
1130*93823e6cSPaul Bohm
1131*93823e6cSPaul Bohm ecb_inline ecb_const uint32_t ecb_byteorder_helper (void);
1132*93823e6cSPaul Bohm ecb_inline ecb_const uint32_t
ecb_byteorder_helper(void)1133*93823e6cSPaul Bohm ecb_byteorder_helper (void)
1134*93823e6cSPaul Bohm {
1135*93823e6cSPaul Bohm /* the union code still generates code under pressure in gcc, */
1136*93823e6cSPaul Bohm /* but less than using pointers, and always seems to */
1137*93823e6cSPaul Bohm /* successfully return a constant. */
1138*93823e6cSPaul Bohm /* the reason why we have this horrible preprocessor mess */
1139*93823e6cSPaul Bohm /* is to avoid it in all cases, at least on common architectures */
1140*93823e6cSPaul Bohm /* or when using a recent enough gcc version (>= 4.6) */
1141*93823e6cSPaul Bohm #if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
1142*93823e6cSPaul Bohm || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__)
1143*93823e6cSPaul Bohm #define ECB_LITTLE_ENDIAN 1
1144*93823e6cSPaul Bohm return 0x44332211;
1145*93823e6cSPaul Bohm #elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
1146*93823e6cSPaul Bohm || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__)
1147*93823e6cSPaul Bohm #define ECB_BIG_ENDIAN 1
1148*93823e6cSPaul Bohm return 0x11223344;
1149*93823e6cSPaul Bohm #else
1150*93823e6cSPaul Bohm union
1151*93823e6cSPaul Bohm {
1152*93823e6cSPaul Bohm uint8_t c[4];
1153*93823e6cSPaul Bohm uint32_t u;
1154*93823e6cSPaul Bohm } u = { 0x11, 0x22, 0x33, 0x44 };
1155*93823e6cSPaul Bohm return u.u;
1156*93823e6cSPaul Bohm #endif
1157*93823e6cSPaul Bohm }
1158*93823e6cSPaul Bohm
1159*93823e6cSPaul Bohm ecb_inline ecb_const ecb_bool ecb_big_endian (void);
ecb_big_endian(void)1160*93823e6cSPaul Bohm ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
1161*93823e6cSPaul Bohm ecb_inline ecb_const ecb_bool ecb_little_endian (void);
ecb_little_endian(void)1162*93823e6cSPaul Bohm ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
1163*93823e6cSPaul Bohm
1164*93823e6cSPaul Bohm #if ECB_GCC_VERSION(3,0) || ECB_C99
1165*93823e6cSPaul Bohm #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
1166*93823e6cSPaul Bohm #else
1167*93823e6cSPaul Bohm #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
1168*93823e6cSPaul Bohm #endif
1169*93823e6cSPaul Bohm
1170*93823e6cSPaul Bohm #if ECB_CPP
1171*93823e6cSPaul Bohm template<typename T>
ecb_div_rd(T val,T div)1172*93823e6cSPaul Bohm static inline T ecb_div_rd (T val, T div)
1173*93823e6cSPaul Bohm {
1174*93823e6cSPaul Bohm return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
1175*93823e6cSPaul Bohm }
1176*93823e6cSPaul Bohm template<typename T>
ecb_div_ru(T val,T div)1177*93823e6cSPaul Bohm static inline T ecb_div_ru (T val, T div)
1178*93823e6cSPaul Bohm {
1179*93823e6cSPaul Bohm return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
1180*93823e6cSPaul Bohm }
1181*93823e6cSPaul Bohm #else
1182*93823e6cSPaul Bohm #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
1183*93823e6cSPaul Bohm #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
1184*93823e6cSPaul Bohm #endif
1185*93823e6cSPaul Bohm
1186*93823e6cSPaul Bohm #if ecb_cplusplus_does_not_suck
1187*93823e6cSPaul Bohm /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
1188*93823e6cSPaul Bohm template<typename T, int N>
ecb_array_length(const T (& arr)[N])1189*93823e6cSPaul Bohm static inline int ecb_array_length (const T (&arr)[N])
1190*93823e6cSPaul Bohm {
1191*93823e6cSPaul Bohm return N;
1192*93823e6cSPaul Bohm }
1193*93823e6cSPaul Bohm #else
1194*93823e6cSPaul Bohm #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
1195*93823e6cSPaul Bohm #endif
1196*93823e6cSPaul Bohm
1197*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
1198*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t
ecb_binary16_to_binary32(uint32_t x)1199*93823e6cSPaul Bohm ecb_binary16_to_binary32 (uint32_t x)
1200*93823e6cSPaul Bohm {
1201*93823e6cSPaul Bohm unsigned int s = (x & 0x8000) << (31 - 15);
1202*93823e6cSPaul Bohm int e = (x >> 10) & 0x001f;
1203*93823e6cSPaul Bohm unsigned int m = x & 0x03ff;
1204*93823e6cSPaul Bohm
1205*93823e6cSPaul Bohm if (ecb_expect_false (e == 31))
1206*93823e6cSPaul Bohm /* infinity or NaN */
1207*93823e6cSPaul Bohm e = 255 - (127 - 15);
1208*93823e6cSPaul Bohm else if (ecb_expect_false (!e))
1209*93823e6cSPaul Bohm {
1210*93823e6cSPaul Bohm if (ecb_expect_true (!m))
1211*93823e6cSPaul Bohm /* zero, handled by code below by forcing e to 0 */
1212*93823e6cSPaul Bohm e = 0 - (127 - 15);
1213*93823e6cSPaul Bohm else
1214*93823e6cSPaul Bohm {
1215*93823e6cSPaul Bohm /* subnormal, renormalise */
1216*93823e6cSPaul Bohm unsigned int s = 10 - ecb_ld32 (m);
1217*93823e6cSPaul Bohm
1218*93823e6cSPaul Bohm m = (m << s) & 0x3ff; /* mask implicit bit */
1219*93823e6cSPaul Bohm e -= s - 1;
1220*93823e6cSPaul Bohm }
1221*93823e6cSPaul Bohm }
1222*93823e6cSPaul Bohm
1223*93823e6cSPaul Bohm /* e and m now are normalised, or zero, (or inf or nan) */
1224*93823e6cSPaul Bohm e += 127 - 15;
1225*93823e6cSPaul Bohm
1226*93823e6cSPaul Bohm return s | (e << 23) | (m << (23 - 10));
1227*93823e6cSPaul Bohm }
1228*93823e6cSPaul Bohm
1229*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x);
1230*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t
ecb_binary32_to_binary16(uint32_t x)1231*93823e6cSPaul Bohm ecb_binary32_to_binary16 (uint32_t x)
1232*93823e6cSPaul Bohm {
1233*93823e6cSPaul Bohm unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */
1234*93823e6cSPaul Bohm unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */
1235*93823e6cSPaul Bohm unsigned int m = x & 0x007fffff;
1236*93823e6cSPaul Bohm
1237*93823e6cSPaul Bohm x &= 0x7fffffff;
1238*93823e6cSPaul Bohm
1239*93823e6cSPaul Bohm /* if it's within range of binary16 normals, use fast path */
1240*93823e6cSPaul Bohm if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff))
1241*93823e6cSPaul Bohm {
1242*93823e6cSPaul Bohm /* mantissa round-to-even */
1243*93823e6cSPaul Bohm m += 0x00000fff + ((m >> (23 - 10)) & 1);
1244*93823e6cSPaul Bohm
1245*93823e6cSPaul Bohm /* handle overflow */
1246*93823e6cSPaul Bohm if (ecb_expect_false (m >= 0x00800000))
1247*93823e6cSPaul Bohm {
1248*93823e6cSPaul Bohm m >>= 1;
1249*93823e6cSPaul Bohm e += 1;
1250*93823e6cSPaul Bohm }
1251*93823e6cSPaul Bohm
1252*93823e6cSPaul Bohm return s | (e << 10) | (m >> (23 - 10));
1253*93823e6cSPaul Bohm }
1254*93823e6cSPaul Bohm
1255*93823e6cSPaul Bohm /* handle large numbers and infinity */
1256*93823e6cSPaul Bohm if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000))
1257*93823e6cSPaul Bohm return s | 0x7c00;
1258*93823e6cSPaul Bohm
1259*93823e6cSPaul Bohm /* handle zero, subnormals and small numbers */
1260*93823e6cSPaul Bohm if (ecb_expect_true (x < 0x38800000))
1261*93823e6cSPaul Bohm {
1262*93823e6cSPaul Bohm /* zero */
1263*93823e6cSPaul Bohm if (ecb_expect_true (!x))
1264*93823e6cSPaul Bohm return s;
1265*93823e6cSPaul Bohm
1266*93823e6cSPaul Bohm /* handle subnormals */
1267*93823e6cSPaul Bohm
1268*93823e6cSPaul Bohm /* too small, will be zero */
1269*93823e6cSPaul Bohm if (e < (14 - 24)) /* might not be sharp, but is good enough */
1270*93823e6cSPaul Bohm return s;
1271*93823e6cSPaul Bohm
1272*93823e6cSPaul Bohm m |= 0x00800000; /* make implicit bit explicit */
1273*93823e6cSPaul Bohm
1274*93823e6cSPaul Bohm /* very tricky - we need to round to the nearest e (+10) bit value */
1275*93823e6cSPaul Bohm {
1276*93823e6cSPaul Bohm unsigned int bits = 14 - e;
1277*93823e6cSPaul Bohm unsigned int half = (1 << (bits - 1)) - 1;
1278*93823e6cSPaul Bohm unsigned int even = (m >> bits) & 1;
1279*93823e6cSPaul Bohm
1280*93823e6cSPaul Bohm /* if this overflows, we will end up with a normalised number */
1281*93823e6cSPaul Bohm m = (m + half + even) >> bits;
1282*93823e6cSPaul Bohm }
1283*93823e6cSPaul Bohm
1284*93823e6cSPaul Bohm return s | m;
1285*93823e6cSPaul Bohm }
1286*93823e6cSPaul Bohm
1287*93823e6cSPaul Bohm /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */
1288*93823e6cSPaul Bohm m >>= 13;
1289*93823e6cSPaul Bohm
1290*93823e6cSPaul Bohm return s | 0x7c00 | m | !m;
1291*93823e6cSPaul Bohm }
1292*93823e6cSPaul Bohm
1293*93823e6cSPaul Bohm /*******************************************************************************/
1294*93823e6cSPaul Bohm /* floating point stuff, can be disabled by defining ECB_NO_LIBM */
1295*93823e6cSPaul Bohm
1296*93823e6cSPaul Bohm /* basically, everything uses "ieee pure-endian" floating point numbers */
1297*93823e6cSPaul Bohm /* the only noteworthy exception is ancient armle, which uses order 43218765 */
1298*93823e6cSPaul Bohm #if 0 \
1299*93823e6cSPaul Bohm || __i386 || __i386__ \
1300*93823e6cSPaul Bohm || ECB_GCC_AMD64 \
1301*93823e6cSPaul Bohm || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
1302*93823e6cSPaul Bohm || defined __s390__ || defined __s390x__ \
1303*93823e6cSPaul Bohm || defined __mips__ \
1304*93823e6cSPaul Bohm || defined __alpha__ \
1305*93823e6cSPaul Bohm || defined __hppa__ \
1306*93823e6cSPaul Bohm || defined __ia64__ \
1307*93823e6cSPaul Bohm || defined __m68k__ \
1308*93823e6cSPaul Bohm || defined __m88k__ \
1309*93823e6cSPaul Bohm || defined __sh__ \
1310*93823e6cSPaul Bohm || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \
1311*93823e6cSPaul Bohm || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \
1312*93823e6cSPaul Bohm || defined __aarch64__
1313*93823e6cSPaul Bohm #define ECB_STDFP 1
1314*93823e6cSPaul Bohm #include <string.h> /* for memcpy */
1315*93823e6cSPaul Bohm #else
1316*93823e6cSPaul Bohm #define ECB_STDFP 0
1317*93823e6cSPaul Bohm #endif
1318*93823e6cSPaul Bohm
1319*93823e6cSPaul Bohm #ifndef ECB_NO_LIBM
1320*93823e6cSPaul Bohm
1321*93823e6cSPaul Bohm #include <math.h> /* for frexp*, ldexp*, INFINITY, NAN */
1322*93823e6cSPaul Bohm
1323*93823e6cSPaul Bohm /* only the oldest of old doesn't have this one. solaris. */
1324*93823e6cSPaul Bohm #ifdef INFINITY
1325*93823e6cSPaul Bohm #define ECB_INFINITY INFINITY
1326*93823e6cSPaul Bohm #else
1327*93823e6cSPaul Bohm #define ECB_INFINITY HUGE_VAL
1328*93823e6cSPaul Bohm #endif
1329*93823e6cSPaul Bohm
1330*93823e6cSPaul Bohm #ifdef NAN
1331*93823e6cSPaul Bohm #define ECB_NAN NAN
1332*93823e6cSPaul Bohm #else
1333*93823e6cSPaul Bohm #define ECB_NAN ECB_INFINITY
1334*93823e6cSPaul Bohm #endif
1335*93823e6cSPaul Bohm
1336*93823e6cSPaul Bohm #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L
1337*93823e6cSPaul Bohm #define ecb_ldexpf(x,e) ldexpf ((x), (e))
1338*93823e6cSPaul Bohm #define ecb_frexpf(x,e) frexpf ((x), (e))
1339*93823e6cSPaul Bohm #else
1340*93823e6cSPaul Bohm #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e))
1341*93823e6cSPaul Bohm #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e))
1342*93823e6cSPaul Bohm #endif
1343*93823e6cSPaul Bohm
1344*93823e6cSPaul Bohm /* convert a float to ieee single/binary32 */
1345*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x);
1346*93823e6cSPaul Bohm ecb_function_ ecb_const uint32_t
ecb_float_to_binary32(float x)1347*93823e6cSPaul Bohm ecb_float_to_binary32 (float x)
1348*93823e6cSPaul Bohm {
1349*93823e6cSPaul Bohm uint32_t r;
1350*93823e6cSPaul Bohm
1351*93823e6cSPaul Bohm #if ECB_STDFP
1352*93823e6cSPaul Bohm memcpy (&r, &x, 4);
1353*93823e6cSPaul Bohm #else
1354*93823e6cSPaul Bohm /* slow emulation, works for anything but -0 */
1355*93823e6cSPaul Bohm uint32_t m;
1356*93823e6cSPaul Bohm int e;
1357*93823e6cSPaul Bohm
1358*93823e6cSPaul Bohm if (x == 0e0f ) return 0x00000000U;
1359*93823e6cSPaul Bohm if (x > +3.40282346638528860e+38f) return 0x7f800000U;
1360*93823e6cSPaul Bohm if (x < -3.40282346638528860e+38f) return 0xff800000U;
1361*93823e6cSPaul Bohm if (x != x ) return 0x7fbfffffU;
1362*93823e6cSPaul Bohm
1363*93823e6cSPaul Bohm m = ecb_frexpf (x, &e) * 0x1000000U;
1364*93823e6cSPaul Bohm
1365*93823e6cSPaul Bohm r = m & 0x80000000U;
1366*93823e6cSPaul Bohm
1367*93823e6cSPaul Bohm if (r)
1368*93823e6cSPaul Bohm m = -m;
1369*93823e6cSPaul Bohm
1370*93823e6cSPaul Bohm if (e <= -126)
1371*93823e6cSPaul Bohm {
1372*93823e6cSPaul Bohm m &= 0xffffffU;
1373*93823e6cSPaul Bohm m >>= (-125 - e);
1374*93823e6cSPaul Bohm e = -126;
1375*93823e6cSPaul Bohm }
1376*93823e6cSPaul Bohm
1377*93823e6cSPaul Bohm r |= (e + 126) << 23;
1378*93823e6cSPaul Bohm r |= m & 0x7fffffU;
1379*93823e6cSPaul Bohm #endif
1380*93823e6cSPaul Bohm
1381*93823e6cSPaul Bohm return r;
1382*93823e6cSPaul Bohm }
1383*93823e6cSPaul Bohm
1384*93823e6cSPaul Bohm /* converts an ieee single/binary32 to a float */
1385*93823e6cSPaul Bohm ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x);
1386*93823e6cSPaul Bohm ecb_function_ ecb_const float
ecb_binary32_to_float(uint32_t x)1387*93823e6cSPaul Bohm ecb_binary32_to_float (uint32_t x)
1388*93823e6cSPaul Bohm {
1389*93823e6cSPaul Bohm float r;
1390*93823e6cSPaul Bohm
1391*93823e6cSPaul Bohm #if ECB_STDFP
1392*93823e6cSPaul Bohm memcpy (&r, &x, 4);
1393*93823e6cSPaul Bohm #else
1394*93823e6cSPaul Bohm /* emulation, only works for normals and subnormals and +0 */
1395*93823e6cSPaul Bohm int neg = x >> 31;
1396*93823e6cSPaul Bohm int e = (x >> 23) & 0xffU;
1397*93823e6cSPaul Bohm
1398*93823e6cSPaul Bohm x &= 0x7fffffU;
1399*93823e6cSPaul Bohm
1400*93823e6cSPaul Bohm if (e)
1401*93823e6cSPaul Bohm x |= 0x800000U;
1402*93823e6cSPaul Bohm else
1403*93823e6cSPaul Bohm e = 1;
1404*93823e6cSPaul Bohm
1405*93823e6cSPaul Bohm /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
1406*93823e6cSPaul Bohm r = ecb_ldexpf (x * (0.5f / 0x800000U), e - 126);
1407*93823e6cSPaul Bohm
1408*93823e6cSPaul Bohm r = neg ? -r : r;
1409*93823e6cSPaul Bohm #endif
1410*93823e6cSPaul Bohm
1411*93823e6cSPaul Bohm return r;
1412*93823e6cSPaul Bohm }
1413*93823e6cSPaul Bohm
1414*93823e6cSPaul Bohm /* convert a double to ieee double/binary64 */
1415*93823e6cSPaul Bohm ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x);
1416*93823e6cSPaul Bohm ecb_function_ ecb_const uint64_t
ecb_double_to_binary64(double x)1417*93823e6cSPaul Bohm ecb_double_to_binary64 (double x)
1418*93823e6cSPaul Bohm {
1419*93823e6cSPaul Bohm uint64_t r;
1420*93823e6cSPaul Bohm
1421*93823e6cSPaul Bohm #if ECB_STDFP
1422*93823e6cSPaul Bohm memcpy (&r, &x, 8);
1423*93823e6cSPaul Bohm #else
1424*93823e6cSPaul Bohm /* slow emulation, works for anything but -0 */
1425*93823e6cSPaul Bohm uint64_t m;
1426*93823e6cSPaul Bohm int e;
1427*93823e6cSPaul Bohm
1428*93823e6cSPaul Bohm if (x == 0e0 ) return 0x0000000000000000U;
1429*93823e6cSPaul Bohm if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
1430*93823e6cSPaul Bohm if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
1431*93823e6cSPaul Bohm if (x != x ) return 0X7ff7ffffffffffffU;
1432*93823e6cSPaul Bohm
1433*93823e6cSPaul Bohm m = frexp (x, &e) * 0x20000000000000U;
1434*93823e6cSPaul Bohm
1435*93823e6cSPaul Bohm r = m & 0x8000000000000000;;
1436*93823e6cSPaul Bohm
1437*93823e6cSPaul Bohm if (r)
1438*93823e6cSPaul Bohm m = -m;
1439*93823e6cSPaul Bohm
1440*93823e6cSPaul Bohm if (e <= -1022)
1441*93823e6cSPaul Bohm {
1442*93823e6cSPaul Bohm m &= 0x1fffffffffffffU;
1443*93823e6cSPaul Bohm m >>= (-1021 - e);
1444*93823e6cSPaul Bohm e = -1022;
1445*93823e6cSPaul Bohm }
1446*93823e6cSPaul Bohm
1447*93823e6cSPaul Bohm r |= ((uint64_t)(e + 1022)) << 52;
1448*93823e6cSPaul Bohm r |= m & 0xfffffffffffffU;
1449*93823e6cSPaul Bohm #endif
1450*93823e6cSPaul Bohm
1451*93823e6cSPaul Bohm return r;
1452*93823e6cSPaul Bohm }
1453*93823e6cSPaul Bohm
1454*93823e6cSPaul Bohm /* converts an ieee double/binary64 to a double */
1455*93823e6cSPaul Bohm ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x);
1456*93823e6cSPaul Bohm ecb_function_ ecb_const double
ecb_binary64_to_double(uint64_t x)1457*93823e6cSPaul Bohm ecb_binary64_to_double (uint64_t x)
1458*93823e6cSPaul Bohm {
1459*93823e6cSPaul Bohm double r;
1460*93823e6cSPaul Bohm
1461*93823e6cSPaul Bohm #if ECB_STDFP
1462*93823e6cSPaul Bohm memcpy (&r, &x, 8);
1463*93823e6cSPaul Bohm #else
1464*93823e6cSPaul Bohm /* emulation, only works for normals and subnormals and +0 */
1465*93823e6cSPaul Bohm int neg = x >> 63;
1466*93823e6cSPaul Bohm int e = (x >> 52) & 0x7ffU;
1467*93823e6cSPaul Bohm
1468*93823e6cSPaul Bohm x &= 0xfffffffffffffU;
1469*93823e6cSPaul Bohm
1470*93823e6cSPaul Bohm if (e)
1471*93823e6cSPaul Bohm x |= 0x10000000000000U;
1472*93823e6cSPaul Bohm else
1473*93823e6cSPaul Bohm e = 1;
1474*93823e6cSPaul Bohm
1475*93823e6cSPaul Bohm /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
1476*93823e6cSPaul Bohm r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
1477*93823e6cSPaul Bohm
1478*93823e6cSPaul Bohm r = neg ? -r : r;
1479*93823e6cSPaul Bohm #endif
1480*93823e6cSPaul Bohm
1481*93823e6cSPaul Bohm return r;
1482*93823e6cSPaul Bohm }
1483*93823e6cSPaul Bohm
1484*93823e6cSPaul Bohm /* convert a float to ieee half/binary16 */
1485*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x);
1486*93823e6cSPaul Bohm ecb_function_ ecb_const uint16_t
ecb_float_to_binary16(float x)1487*93823e6cSPaul Bohm ecb_float_to_binary16 (float x)
1488*93823e6cSPaul Bohm {
1489*93823e6cSPaul Bohm return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x));
1490*93823e6cSPaul Bohm }
1491*93823e6cSPaul Bohm
1492*93823e6cSPaul Bohm /* convert an ieee half/binary16 to float */
1493*93823e6cSPaul Bohm ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
1494*93823e6cSPaul Bohm ecb_function_ ecb_const float
ecb_binary16_to_float(uint16_t x)1495*93823e6cSPaul Bohm ecb_binary16_to_float (uint16_t x)
1496*93823e6cSPaul Bohm {
1497*93823e6cSPaul Bohm return ecb_binary32_to_float (ecb_binary16_to_binary32 (x));
1498*93823e6cSPaul Bohm }
1499*93823e6cSPaul Bohm
1500*93823e6cSPaul Bohm #endif
1501*93823e6cSPaul Bohm
1502*93823e6cSPaul Bohm #endif
1503*93823e6cSPaul Bohm
1504*93823e6cSPaul Bohm /* ECB.H END */
1505*93823e6cSPaul Bohm
1506*93823e6cSPaul Bohm #if ECB_MEMORY_FENCE_NEEDS_PTHREADS
1507*93823e6cSPaul Bohm /* if your architecture doesn't need memory fences, e.g. because it is
1508*93823e6cSPaul Bohm * single-cpu/core, or if you use libev in a project that doesn't use libev
1509*93823e6cSPaul Bohm * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
1510*93823e6cSPaul Bohm * libev, in which cases the memory fences become nops.
1511*93823e6cSPaul Bohm * alternatively, you can remove this #error and link against libpthread,
1512*93823e6cSPaul Bohm * which will then provide the memory fences.
1513*93823e6cSPaul Bohm */
1514*93823e6cSPaul Bohm # error "memory fences not defined for your architecture, please report"
1515*93823e6cSPaul Bohm #endif
1516*93823e6cSPaul Bohm
1517*93823e6cSPaul Bohm #ifndef ECB_MEMORY_FENCE
1518*93823e6cSPaul Bohm # define ECB_MEMORY_FENCE do { } while (0)
1519*93823e6cSPaul Bohm # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
1520*93823e6cSPaul Bohm # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
1521*93823e6cSPaul Bohm #endif
1522*93823e6cSPaul Bohm
1523*93823e6cSPaul Bohm #define expect_false(cond) ecb_expect_false (cond)
1524*93823e6cSPaul Bohm #define expect_true(cond) ecb_expect_true (cond)
1525*93823e6cSPaul Bohm #define noinline ecb_noinline
1526*93823e6cSPaul Bohm
1527*93823e6cSPaul Bohm #define inline_size ecb_inline
1528e3a38431SPaul Bohm
1529e3a38431SPaul Bohm #if EV_FEATURE_CODE
1530*93823e6cSPaul Bohm # define inline_speed ecb_inline
1531e3a38431SPaul Bohm #else
1532e3a38431SPaul Bohm # define inline_speed static noinline
1533e3a38431SPaul Bohm #endif
1534e3a38431SPaul Bohm
1535e3a38431SPaul Bohm #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
1536e3a38431SPaul Bohm
1537e3a38431SPaul Bohm #if EV_MINPRI == EV_MAXPRI
1538e3a38431SPaul Bohm # define ABSPRI(w) (((W)w), 0)
1539e3a38431SPaul Bohm #else
1540e3a38431SPaul Bohm # define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
1541e3a38431SPaul Bohm #endif
1542e3a38431SPaul Bohm
1543e3a38431SPaul Bohm #define EMPTY /* required for microsofts broken pseudo-c compiler */
1544e3a38431SPaul Bohm #define EMPTY2(a,b) /* used to suppress some warnings */
1545e3a38431SPaul Bohm
1546e3a38431SPaul Bohm typedef ev_watcher *W;
1547e3a38431SPaul Bohm typedef ev_watcher_list *WL;
1548e3a38431SPaul Bohm typedef ev_watcher_time *WT;
1549e3a38431SPaul Bohm
1550e3a38431SPaul Bohm #define ev_active(w) ((W)(w))->active
1551e3a38431SPaul Bohm #define ev_at(w) ((WT)(w))->at
1552e3a38431SPaul Bohm
1553e3a38431SPaul Bohm #if EV_USE_REALTIME
1554e3a38431SPaul Bohm /* sig_atomic_t is used to avoid per-thread variables or locking but still */
1555e3a38431SPaul Bohm /* giving it a reasonably high chance of working on typical architectures */
1556e3a38431SPaul Bohm static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
1557e3a38431SPaul Bohm #endif
1558e3a38431SPaul Bohm
1559e3a38431SPaul Bohm #if EV_USE_MONOTONIC
1560e3a38431SPaul Bohm static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
1561e3a38431SPaul Bohm #endif
1562e3a38431SPaul Bohm
1563e3a38431SPaul Bohm #ifndef EV_FD_TO_WIN32_HANDLE
1564e3a38431SPaul Bohm # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
1565e3a38431SPaul Bohm #endif
1566e3a38431SPaul Bohm #ifndef EV_WIN32_HANDLE_TO_FD
1567e3a38431SPaul Bohm # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
1568e3a38431SPaul Bohm #endif
1569e3a38431SPaul Bohm #ifndef EV_WIN32_CLOSE_FD
1570e3a38431SPaul Bohm # define EV_WIN32_CLOSE_FD(fd) close (fd)
1571e3a38431SPaul Bohm #endif
1572e3a38431SPaul Bohm
1573e3a38431SPaul Bohm #ifdef _WIN32
1574e3a38431SPaul Bohm # include "ev_win32.c"
1575e3a38431SPaul Bohm #endif
1576e3a38431SPaul Bohm
1577e3a38431SPaul Bohm /*****************************************************************************/
1578e3a38431SPaul Bohm
1579*93823e6cSPaul Bohm /* define a suitable floor function (only used by periodics atm) */
1580*93823e6cSPaul Bohm
1581*93823e6cSPaul Bohm #if EV_USE_FLOOR
1582*93823e6cSPaul Bohm # include <math.h>
1583*93823e6cSPaul Bohm # define ev_floor(v) floor (v)
1584*93823e6cSPaul Bohm #else
1585*93823e6cSPaul Bohm
1586*93823e6cSPaul Bohm #include <float.h>
1587*93823e6cSPaul Bohm
1588*93823e6cSPaul Bohm /* a floor() replacement function, should be independent of ev_tstamp type */
1589*93823e6cSPaul Bohm static ev_tstamp noinline
ev_floor(ev_tstamp v)1590*93823e6cSPaul Bohm ev_floor (ev_tstamp v)
1591*93823e6cSPaul Bohm {
1592*93823e6cSPaul Bohm /* the choice of shift factor is not terribly important */
1593*93823e6cSPaul Bohm #if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
1594*93823e6cSPaul Bohm const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
1595*93823e6cSPaul Bohm #else
1596*93823e6cSPaul Bohm const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
1597*93823e6cSPaul Bohm #endif
1598*93823e6cSPaul Bohm
1599*93823e6cSPaul Bohm /* argument too large for an unsigned long? */
1600*93823e6cSPaul Bohm if (expect_false (v >= shift))
1601*93823e6cSPaul Bohm {
1602*93823e6cSPaul Bohm ev_tstamp f;
1603*93823e6cSPaul Bohm
1604*93823e6cSPaul Bohm if (v == v - 1.)
1605*93823e6cSPaul Bohm return v; /* very large number */
1606*93823e6cSPaul Bohm
1607*93823e6cSPaul Bohm f = shift * ev_floor (v * (1. / shift));
1608*93823e6cSPaul Bohm return f + ev_floor (v - f);
1609*93823e6cSPaul Bohm }
1610*93823e6cSPaul Bohm
1611*93823e6cSPaul Bohm /* special treatment for negative args? */
1612*93823e6cSPaul Bohm if (expect_false (v < 0.))
1613*93823e6cSPaul Bohm {
1614*93823e6cSPaul Bohm ev_tstamp f = -ev_floor (-v);
1615*93823e6cSPaul Bohm
1616*93823e6cSPaul Bohm return f - (f == v ? 0 : 1);
1617*93823e6cSPaul Bohm }
1618*93823e6cSPaul Bohm
1619*93823e6cSPaul Bohm /* fits into an unsigned long */
1620*93823e6cSPaul Bohm return (unsigned long)v;
1621*93823e6cSPaul Bohm }
1622*93823e6cSPaul Bohm
1623*93823e6cSPaul Bohm #endif
1624*93823e6cSPaul Bohm
1625*93823e6cSPaul Bohm /*****************************************************************************/
1626*93823e6cSPaul Bohm
1627e3a38431SPaul Bohm #ifdef __linux
1628e3a38431SPaul Bohm # include <sys/utsname.h>
1629e3a38431SPaul Bohm #endif
1630e3a38431SPaul Bohm
1631*93823e6cSPaul Bohm static unsigned int noinline ecb_cold
ev_linux_version(void)1632e3a38431SPaul Bohm ev_linux_version (void)
1633e3a38431SPaul Bohm {
1634e3a38431SPaul Bohm #ifdef __linux
1635e3a38431SPaul Bohm unsigned int v = 0;
1636e3a38431SPaul Bohm struct utsname buf;
1637e3a38431SPaul Bohm int i;
1638e3a38431SPaul Bohm char *p = buf.release;
1639e3a38431SPaul Bohm
1640e3a38431SPaul Bohm if (uname (&buf))
1641e3a38431SPaul Bohm return 0;
1642e3a38431SPaul Bohm
1643e3a38431SPaul Bohm for (i = 3+1; --i; )
1644e3a38431SPaul Bohm {
1645e3a38431SPaul Bohm unsigned int c = 0;
1646e3a38431SPaul Bohm
1647e3a38431SPaul Bohm for (;;)
1648e3a38431SPaul Bohm {
1649e3a38431SPaul Bohm if (*p >= '0' && *p <= '9')
1650e3a38431SPaul Bohm c = c * 10 + *p++ - '0';
1651e3a38431SPaul Bohm else
1652e3a38431SPaul Bohm {
1653e3a38431SPaul Bohm p += *p == '.';
1654e3a38431SPaul Bohm break;
1655e3a38431SPaul Bohm }
1656e3a38431SPaul Bohm }
1657e3a38431SPaul Bohm
1658e3a38431SPaul Bohm v = (v << 8) | c;
1659e3a38431SPaul Bohm }
1660e3a38431SPaul Bohm
1661e3a38431SPaul Bohm return v;
1662e3a38431SPaul Bohm #else
1663e3a38431SPaul Bohm return 0;
1664e3a38431SPaul Bohm #endif
1665e3a38431SPaul Bohm }
1666e3a38431SPaul Bohm
1667e3a38431SPaul Bohm /*****************************************************************************/
1668e3a38431SPaul Bohm
1669e3a38431SPaul Bohm #if EV_AVOID_STDIO
1670*93823e6cSPaul Bohm static void noinline ecb_cold
ev_printerr(const char * msg)1671e3a38431SPaul Bohm ev_printerr (const char *msg)
1672e3a38431SPaul Bohm {
1673e3a38431SPaul Bohm write (STDERR_FILENO, msg, strlen (msg));
1674e3a38431SPaul Bohm }
1675e3a38431SPaul Bohm #endif
1676e3a38431SPaul Bohm
1677*93823e6cSPaul Bohm static void (*syserr_cb)(const char *msg) EV_THROW;
1678e3a38431SPaul Bohm
1679*93823e6cSPaul Bohm void ecb_cold
ev_set_syserr_cb(void (* cb)(const char * msg)EV_THROW)1680*93823e6cSPaul Bohm ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
1681e3a38431SPaul Bohm {
1682e3a38431SPaul Bohm syserr_cb = cb;
1683e3a38431SPaul Bohm }
1684e3a38431SPaul Bohm
1685*93823e6cSPaul Bohm static void noinline ecb_cold
ev_syserr(const char * msg)1686e3a38431SPaul Bohm ev_syserr (const char *msg)
1687e3a38431SPaul Bohm {
1688e3a38431SPaul Bohm if (!msg)
1689e3a38431SPaul Bohm msg = "(libev) system error";
1690e3a38431SPaul Bohm
1691e3a38431SPaul Bohm if (syserr_cb)
1692e3a38431SPaul Bohm syserr_cb (msg);
1693e3a38431SPaul Bohm else
1694e3a38431SPaul Bohm {
1695e3a38431SPaul Bohm #if EV_AVOID_STDIO
1696e3a38431SPaul Bohm ev_printerr (msg);
1697e3a38431SPaul Bohm ev_printerr (": ");
1698e3a38431SPaul Bohm ev_printerr (strerror (errno));
1699e3a38431SPaul Bohm ev_printerr ("\n");
1700e3a38431SPaul Bohm #else
1701e3a38431SPaul Bohm perror (msg);
1702e3a38431SPaul Bohm #endif
1703e3a38431SPaul Bohm abort ();
1704e3a38431SPaul Bohm }
1705e3a38431SPaul Bohm }
1706e3a38431SPaul Bohm
1707e3a38431SPaul Bohm static void *
ev_realloc_emul(void * ptr,long size)1708*93823e6cSPaul Bohm ev_realloc_emul (void *ptr, long size) EV_THROW
1709e3a38431SPaul Bohm {
1710e3a38431SPaul Bohm /* some systems, notably openbsd and darwin, fail to properly
1711e3a38431SPaul Bohm * implement realloc (x, 0) (as required by both ansi c-89 and
1712e3a38431SPaul Bohm * the single unix specification, so work around them here.
1713*93823e6cSPaul Bohm * recently, also (at least) fedora and debian started breaking it,
1714*93823e6cSPaul Bohm * despite documenting it otherwise.
1715e3a38431SPaul Bohm */
1716e3a38431SPaul Bohm
1717e3a38431SPaul Bohm if (size)
1718e3a38431SPaul Bohm return realloc (ptr, size);
1719e3a38431SPaul Bohm
1720e3a38431SPaul Bohm free (ptr);
1721e3a38431SPaul Bohm return 0;
1722e3a38431SPaul Bohm }
1723e3a38431SPaul Bohm
1724*93823e6cSPaul Bohm static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1725e3a38431SPaul Bohm
1726*93823e6cSPaul Bohm void ecb_cold
ev_set_allocator(void * (* cb)(void * ptr,long size)EV_THROW)1727*93823e6cSPaul Bohm ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1728e3a38431SPaul Bohm {
1729e3a38431SPaul Bohm alloc = cb;
1730e3a38431SPaul Bohm }
1731e3a38431SPaul Bohm
1732e3a38431SPaul Bohm inline_speed void *
ev_realloc(void * ptr,long size)1733e3a38431SPaul Bohm ev_realloc (void *ptr, long size)
1734e3a38431SPaul Bohm {
1735e3a38431SPaul Bohm ptr = alloc (ptr, size);
1736e3a38431SPaul Bohm
1737e3a38431SPaul Bohm if (!ptr && size)
1738e3a38431SPaul Bohm {
1739e3a38431SPaul Bohm #if EV_AVOID_STDIO
1740e3a38431SPaul Bohm ev_printerr ("(libev) memory allocation failed, aborting.\n");
1741e3a38431SPaul Bohm #else
1742e3a38431SPaul Bohm fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
1743e3a38431SPaul Bohm #endif
1744e3a38431SPaul Bohm abort ();
1745e3a38431SPaul Bohm }
1746e3a38431SPaul Bohm
1747e3a38431SPaul Bohm return ptr;
1748e3a38431SPaul Bohm }
1749e3a38431SPaul Bohm
1750e3a38431SPaul Bohm #define ev_malloc(size) ev_realloc (0, (size))
1751e3a38431SPaul Bohm #define ev_free(ptr) ev_realloc ((ptr), 0)
1752e3a38431SPaul Bohm
1753e3a38431SPaul Bohm /*****************************************************************************/
1754e3a38431SPaul Bohm
1755e3a38431SPaul Bohm /* set in reify when reification needed */
1756e3a38431SPaul Bohm #define EV_ANFD_REIFY 1
1757e3a38431SPaul Bohm
1758e3a38431SPaul Bohm /* file descriptor info structure */
1759e3a38431SPaul Bohm typedef struct
1760e3a38431SPaul Bohm {
1761e3a38431SPaul Bohm WL head;
1762e3a38431SPaul Bohm unsigned char events; /* the events watched for */
1763e3a38431SPaul Bohm unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
1764e3a38431SPaul Bohm unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
1765e3a38431SPaul Bohm unsigned char unused;
1766e3a38431SPaul Bohm #if EV_USE_EPOLL
1767e3a38431SPaul Bohm unsigned int egen; /* generation counter to counter epoll bugs */
1768e3a38431SPaul Bohm #endif
1769e3a38431SPaul Bohm #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1770e3a38431SPaul Bohm SOCKET handle;
1771e3a38431SPaul Bohm #endif
1772e3a38431SPaul Bohm #if EV_USE_IOCP
1773e3a38431SPaul Bohm OVERLAPPED or, ow;
1774e3a38431SPaul Bohm #endif
1775e3a38431SPaul Bohm } ANFD;
1776e3a38431SPaul Bohm
1777e3a38431SPaul Bohm /* stores the pending event set for a given watcher */
1778e3a38431SPaul Bohm typedef struct
1779e3a38431SPaul Bohm {
1780e3a38431SPaul Bohm W w;
1781e3a38431SPaul Bohm int events; /* the pending event set for the given watcher */
1782e3a38431SPaul Bohm } ANPENDING;
1783e3a38431SPaul Bohm
1784e3a38431SPaul Bohm #if EV_USE_INOTIFY
1785e3a38431SPaul Bohm /* hash table entry per inotify-id */
1786e3a38431SPaul Bohm typedef struct
1787e3a38431SPaul Bohm {
1788e3a38431SPaul Bohm WL head;
1789e3a38431SPaul Bohm } ANFS;
1790e3a38431SPaul Bohm #endif
1791e3a38431SPaul Bohm
1792e3a38431SPaul Bohm /* Heap Entry */
1793e3a38431SPaul Bohm #if EV_HEAP_CACHE_AT
1794e3a38431SPaul Bohm /* a heap element */
1795e3a38431SPaul Bohm typedef struct {
1796e3a38431SPaul Bohm ev_tstamp at;
1797e3a38431SPaul Bohm WT w;
1798e3a38431SPaul Bohm } ANHE;
1799e3a38431SPaul Bohm
1800e3a38431SPaul Bohm #define ANHE_w(he) (he).w /* access watcher, read-write */
1801e3a38431SPaul Bohm #define ANHE_at(he) (he).at /* access cached at, read-only */
1802e3a38431SPaul Bohm #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
1803e3a38431SPaul Bohm #else
1804e3a38431SPaul Bohm /* a heap element */
1805e3a38431SPaul Bohm typedef WT ANHE;
1806e3a38431SPaul Bohm
1807e3a38431SPaul Bohm #define ANHE_w(he) (he)
1808e3a38431SPaul Bohm #define ANHE_at(he) (he)->at
1809e3a38431SPaul Bohm #define ANHE_at_cache(he)
1810e3a38431SPaul Bohm #endif
1811e3a38431SPaul Bohm
1812e3a38431SPaul Bohm #if EV_MULTIPLICITY
1813e3a38431SPaul Bohm
1814e3a38431SPaul Bohm struct ev_loop
1815e3a38431SPaul Bohm {
1816e3a38431SPaul Bohm ev_tstamp ev_rt_now;
1817e3a38431SPaul Bohm #define ev_rt_now ((loop)->ev_rt_now)
1818e3a38431SPaul Bohm #define VAR(name,decl) decl;
1819e3a38431SPaul Bohm #include "ev_vars.h"
1820e3a38431SPaul Bohm #undef VAR
1821e3a38431SPaul Bohm };
1822e3a38431SPaul Bohm #include "ev_wrap.h"
1823e3a38431SPaul Bohm
1824e3a38431SPaul Bohm static struct ev_loop default_loop_struct;
1825*93823e6cSPaul Bohm EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
1826e3a38431SPaul Bohm
1827e3a38431SPaul Bohm #else
1828e3a38431SPaul Bohm
1829*93823e6cSPaul Bohm EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
1830e3a38431SPaul Bohm #define VAR(name,decl) static decl;
1831e3a38431SPaul Bohm #include "ev_vars.h"
1832e3a38431SPaul Bohm #undef VAR
1833e3a38431SPaul Bohm
1834e3a38431SPaul Bohm static int ev_default_loop_ptr;
1835e3a38431SPaul Bohm
1836e3a38431SPaul Bohm #endif
1837e3a38431SPaul Bohm
1838e3a38431SPaul Bohm #if EV_FEATURE_API
1839e3a38431SPaul Bohm # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
1840e3a38431SPaul Bohm # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
1841e3a38431SPaul Bohm # define EV_INVOKE_PENDING invoke_cb (EV_A)
1842e3a38431SPaul Bohm #else
1843e3a38431SPaul Bohm # define EV_RELEASE_CB (void)0
1844e3a38431SPaul Bohm # define EV_ACQUIRE_CB (void)0
1845e3a38431SPaul Bohm # define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
1846e3a38431SPaul Bohm #endif
1847e3a38431SPaul Bohm
1848e3a38431SPaul Bohm #define EVBREAK_RECURSE 0x80
1849e3a38431SPaul Bohm
1850e3a38431SPaul Bohm /*****************************************************************************/
1851e3a38431SPaul Bohm
1852e3a38431SPaul Bohm #ifndef EV_HAVE_EV_TIME
1853e3a38431SPaul Bohm ev_tstamp
ev_time(void)1854*93823e6cSPaul Bohm ev_time (void) EV_THROW
1855e3a38431SPaul Bohm {
1856e3a38431SPaul Bohm #if EV_USE_REALTIME
1857e3a38431SPaul Bohm if (expect_true (have_realtime))
1858e3a38431SPaul Bohm {
1859e3a38431SPaul Bohm struct timespec ts;
1860e3a38431SPaul Bohm clock_gettime (CLOCK_REALTIME, &ts);
1861e3a38431SPaul Bohm return ts.tv_sec + ts.tv_nsec * 1e-9;
1862e3a38431SPaul Bohm }
1863e3a38431SPaul Bohm #endif
1864e3a38431SPaul Bohm
1865e3a38431SPaul Bohm struct timeval tv;
1866e3a38431SPaul Bohm gettimeofday (&tv, 0);
1867e3a38431SPaul Bohm return tv.tv_sec + tv.tv_usec * 1e-6;
1868e3a38431SPaul Bohm }
1869e3a38431SPaul Bohm #endif
1870e3a38431SPaul Bohm
1871e3a38431SPaul Bohm inline_size ev_tstamp
get_clock(void)1872e3a38431SPaul Bohm get_clock (void)
1873e3a38431SPaul Bohm {
1874e3a38431SPaul Bohm #if EV_USE_MONOTONIC
1875e3a38431SPaul Bohm if (expect_true (have_monotonic))
1876e3a38431SPaul Bohm {
1877e3a38431SPaul Bohm struct timespec ts;
1878e3a38431SPaul Bohm clock_gettime (CLOCK_MONOTONIC, &ts);
1879e3a38431SPaul Bohm return ts.tv_sec + ts.tv_nsec * 1e-9;
1880e3a38431SPaul Bohm }
1881e3a38431SPaul Bohm #endif
1882e3a38431SPaul Bohm
1883e3a38431SPaul Bohm return ev_time ();
1884e3a38431SPaul Bohm }
1885e3a38431SPaul Bohm
1886e3a38431SPaul Bohm #if EV_MULTIPLICITY
1887e3a38431SPaul Bohm ev_tstamp
ev_now(EV_P)1888*93823e6cSPaul Bohm ev_now (EV_P) EV_THROW
1889e3a38431SPaul Bohm {
1890e3a38431SPaul Bohm return ev_rt_now;
1891e3a38431SPaul Bohm }
1892e3a38431SPaul Bohm #endif
1893e3a38431SPaul Bohm
1894e3a38431SPaul Bohm void
ev_sleep(ev_tstamp delay)1895*93823e6cSPaul Bohm ev_sleep (ev_tstamp delay) EV_THROW
1896e3a38431SPaul Bohm {
1897e3a38431SPaul Bohm if (delay > 0.)
1898e3a38431SPaul Bohm {
1899e3a38431SPaul Bohm #if EV_USE_NANOSLEEP
1900e3a38431SPaul Bohm struct timespec ts;
1901e3a38431SPaul Bohm
1902e3a38431SPaul Bohm EV_TS_SET (ts, delay);
1903e3a38431SPaul Bohm nanosleep (&ts, 0);
1904*93823e6cSPaul Bohm #elif defined _WIN32
1905e3a38431SPaul Bohm Sleep ((unsigned long)(delay * 1e3));
1906e3a38431SPaul Bohm #else
1907e3a38431SPaul Bohm struct timeval tv;
1908e3a38431SPaul Bohm
1909e3a38431SPaul Bohm /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
1910e3a38431SPaul Bohm /* something not guaranteed by newer posix versions, but guaranteed */
1911e3a38431SPaul Bohm /* by older ones */
1912e3a38431SPaul Bohm EV_TV_SET (tv, delay);
1913e3a38431SPaul Bohm select (0, 0, 0, 0, &tv);
1914e3a38431SPaul Bohm #endif
1915e3a38431SPaul Bohm }
1916e3a38431SPaul Bohm }
1917e3a38431SPaul Bohm
1918e3a38431SPaul Bohm /*****************************************************************************/
1919e3a38431SPaul Bohm
1920e3a38431SPaul Bohm #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
1921e3a38431SPaul Bohm
1922e3a38431SPaul Bohm /* find a suitable new size for the given array, */
1923e3a38431SPaul Bohm /* hopefully by rounding to a nice-to-malloc size */
1924e3a38431SPaul Bohm inline_size int
array_nextsize(int elem,int cur,int cnt)1925e3a38431SPaul Bohm array_nextsize (int elem, int cur, int cnt)
1926e3a38431SPaul Bohm {
1927e3a38431SPaul Bohm int ncur = cur + 1;
1928e3a38431SPaul Bohm
1929e3a38431SPaul Bohm do
1930e3a38431SPaul Bohm ncur <<= 1;
1931e3a38431SPaul Bohm while (cnt > ncur);
1932e3a38431SPaul Bohm
1933*93823e6cSPaul Bohm /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
1934e3a38431SPaul Bohm if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
1935e3a38431SPaul Bohm {
1936e3a38431SPaul Bohm ncur *= elem;
1937e3a38431SPaul Bohm ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
1938e3a38431SPaul Bohm ncur = ncur - sizeof (void *) * 4;
1939e3a38431SPaul Bohm ncur /= elem;
1940e3a38431SPaul Bohm }
1941e3a38431SPaul Bohm
1942e3a38431SPaul Bohm return ncur;
1943e3a38431SPaul Bohm }
1944e3a38431SPaul Bohm
1945*93823e6cSPaul Bohm static void * noinline ecb_cold
array_realloc(int elem,void * base,int * cur,int cnt)1946e3a38431SPaul Bohm array_realloc (int elem, void *base, int *cur, int cnt)
1947e3a38431SPaul Bohm {
1948e3a38431SPaul Bohm *cur = array_nextsize (elem, *cur, cnt);
1949e3a38431SPaul Bohm return ev_realloc (base, elem * *cur);
1950e3a38431SPaul Bohm }
1951e3a38431SPaul Bohm
1952e3a38431SPaul Bohm #define array_init_zero(base,count) \
1953e3a38431SPaul Bohm memset ((void *)(base), 0, sizeof (*(base)) * (count))
1954e3a38431SPaul Bohm
1955e3a38431SPaul Bohm #define array_needsize(type,base,cur,cnt,init) \
1956e3a38431SPaul Bohm if (expect_false ((cnt) > (cur))) \
1957e3a38431SPaul Bohm { \
1958*93823e6cSPaul Bohm int ecb_unused ocur_ = (cur); \
1959e3a38431SPaul Bohm (base) = (type *)array_realloc \
1960e3a38431SPaul Bohm (sizeof (type), (base), &(cur), (cnt)); \
1961e3a38431SPaul Bohm init ((base) + (ocur_), (cur) - ocur_); \
1962e3a38431SPaul Bohm }
1963e3a38431SPaul Bohm
1964e3a38431SPaul Bohm #if 0
1965e3a38431SPaul Bohm #define array_slim(type,stem) \
1966e3a38431SPaul Bohm if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
1967e3a38431SPaul Bohm { \
1968e3a38431SPaul Bohm stem ## max = array_roundsize (stem ## cnt >> 1); \
1969e3a38431SPaul Bohm base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
1970e3a38431SPaul Bohm fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
1971e3a38431SPaul Bohm }
1972e3a38431SPaul Bohm #endif
1973e3a38431SPaul Bohm
1974e3a38431SPaul Bohm #define array_free(stem, idx) \
1975e3a38431SPaul Bohm ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
1976e3a38431SPaul Bohm
1977e3a38431SPaul Bohm /*****************************************************************************/
1978e3a38431SPaul Bohm
1979e3a38431SPaul Bohm /* dummy callback for pending events */
1980e3a38431SPaul Bohm static void noinline
pendingcb(EV_P_ ev_prepare * w,int revents)1981e3a38431SPaul Bohm pendingcb (EV_P_ ev_prepare *w, int revents)
1982e3a38431SPaul Bohm {
1983e3a38431SPaul Bohm }
1984e3a38431SPaul Bohm
1985e3a38431SPaul Bohm void noinline
ev_feed_event(EV_P_ void * w,int revents)1986*93823e6cSPaul Bohm ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1987e3a38431SPaul Bohm {
1988e3a38431SPaul Bohm W w_ = (W)w;
1989e3a38431SPaul Bohm int pri = ABSPRI (w_);
1990e3a38431SPaul Bohm
1991e3a38431SPaul Bohm if (expect_false (w_->pending))
1992e3a38431SPaul Bohm pendings [pri][w_->pending - 1].events |= revents;
1993e3a38431SPaul Bohm else
1994e3a38431SPaul Bohm {
1995e3a38431SPaul Bohm w_->pending = ++pendingcnt [pri];
1996e3a38431SPaul Bohm array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1997e3a38431SPaul Bohm pendings [pri][w_->pending - 1].w = w_;
1998e3a38431SPaul Bohm pendings [pri][w_->pending - 1].events = revents;
1999e3a38431SPaul Bohm }
2000*93823e6cSPaul Bohm
2001*93823e6cSPaul Bohm pendingpri = NUMPRI - 1;
2002e3a38431SPaul Bohm }
2003e3a38431SPaul Bohm
2004e3a38431SPaul Bohm inline_speed void
feed_reverse(EV_P_ W w)2005e3a38431SPaul Bohm feed_reverse (EV_P_ W w)
2006e3a38431SPaul Bohm {
2007e3a38431SPaul Bohm array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
2008e3a38431SPaul Bohm rfeeds [rfeedcnt++] = w;
2009e3a38431SPaul Bohm }
2010e3a38431SPaul Bohm
2011e3a38431SPaul Bohm inline_size void
feed_reverse_done(EV_P_ int revents)2012e3a38431SPaul Bohm feed_reverse_done (EV_P_ int revents)
2013e3a38431SPaul Bohm {
2014e3a38431SPaul Bohm do
2015e3a38431SPaul Bohm ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
2016e3a38431SPaul Bohm while (rfeedcnt);
2017e3a38431SPaul Bohm }
2018e3a38431SPaul Bohm
2019e3a38431SPaul Bohm inline_speed void
queue_events(EV_P_ W * events,int eventcnt,int type)2020e3a38431SPaul Bohm queue_events (EV_P_ W *events, int eventcnt, int type)
2021e3a38431SPaul Bohm {
2022e3a38431SPaul Bohm int i;
2023e3a38431SPaul Bohm
2024e3a38431SPaul Bohm for (i = 0; i < eventcnt; ++i)
2025e3a38431SPaul Bohm ev_feed_event (EV_A_ events [i], type);
2026e3a38431SPaul Bohm }
2027e3a38431SPaul Bohm
2028e3a38431SPaul Bohm /*****************************************************************************/
2029e3a38431SPaul Bohm
2030e3a38431SPaul Bohm inline_speed void
fd_event_nocheck(EV_P_ int fd,int revents)2031e3a38431SPaul Bohm fd_event_nocheck (EV_P_ int fd, int revents)
2032e3a38431SPaul Bohm {
2033e3a38431SPaul Bohm ANFD *anfd = anfds + fd;
2034e3a38431SPaul Bohm ev_io *w;
2035e3a38431SPaul Bohm
2036e3a38431SPaul Bohm for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
2037e3a38431SPaul Bohm {
2038e3a38431SPaul Bohm int ev = w->events & revents;
2039e3a38431SPaul Bohm
2040e3a38431SPaul Bohm if (ev)
2041e3a38431SPaul Bohm ev_feed_event (EV_A_ (W)w, ev);
2042e3a38431SPaul Bohm }
2043e3a38431SPaul Bohm }
2044e3a38431SPaul Bohm
2045e3a38431SPaul Bohm /* do not submit kernel events for fds that have reify set */
2046e3a38431SPaul Bohm /* because that means they changed while we were polling for new events */
2047e3a38431SPaul Bohm inline_speed void
fd_event(EV_P_ int fd,int revents)2048e3a38431SPaul Bohm fd_event (EV_P_ int fd, int revents)
2049e3a38431SPaul Bohm {
2050e3a38431SPaul Bohm ANFD *anfd = anfds + fd;
2051e3a38431SPaul Bohm
2052e3a38431SPaul Bohm if (expect_true (!anfd->reify))
2053e3a38431SPaul Bohm fd_event_nocheck (EV_A_ fd, revents);
2054e3a38431SPaul Bohm }
2055e3a38431SPaul Bohm
2056e3a38431SPaul Bohm void
ev_feed_fd_event(EV_P_ int fd,int revents)2057*93823e6cSPaul Bohm ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
2058e3a38431SPaul Bohm {
2059e3a38431SPaul Bohm if (fd >= 0 && fd < anfdmax)
2060e3a38431SPaul Bohm fd_event_nocheck (EV_A_ fd, revents);
2061e3a38431SPaul Bohm }
2062e3a38431SPaul Bohm
2063e3a38431SPaul Bohm /* make sure the external fd watch events are in-sync */
2064e3a38431SPaul Bohm /* with the kernel/libev internal state */
2065e3a38431SPaul Bohm inline_size void
fd_reify(EV_P)2066e3a38431SPaul Bohm fd_reify (EV_P)
2067e3a38431SPaul Bohm {
2068e3a38431SPaul Bohm int i;
2069e3a38431SPaul Bohm
2070e3a38431SPaul Bohm #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
2071e3a38431SPaul Bohm for (i = 0; i < fdchangecnt; ++i)
2072e3a38431SPaul Bohm {
2073e3a38431SPaul Bohm int fd = fdchanges [i];
2074e3a38431SPaul Bohm ANFD *anfd = anfds + fd;
2075e3a38431SPaul Bohm
2076*93823e6cSPaul Bohm if (anfd->reify & EV__IOFDSET && anfd->head)
2077e3a38431SPaul Bohm {
2078e3a38431SPaul Bohm SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
2079e3a38431SPaul Bohm
2080e3a38431SPaul Bohm if (handle != anfd->handle)
2081e3a38431SPaul Bohm {
2082e3a38431SPaul Bohm unsigned long arg;
2083e3a38431SPaul Bohm
2084e3a38431SPaul Bohm assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
2085e3a38431SPaul Bohm
2086e3a38431SPaul Bohm /* handle changed, but fd didn't - we need to do it in two steps */
2087e3a38431SPaul Bohm backend_modify (EV_A_ fd, anfd->events, 0);
2088e3a38431SPaul Bohm anfd->events = 0;
2089e3a38431SPaul Bohm anfd->handle = handle;
2090e3a38431SPaul Bohm }
2091e3a38431SPaul Bohm }
2092e3a38431SPaul Bohm }
2093e3a38431SPaul Bohm #endif
2094e3a38431SPaul Bohm
2095e3a38431SPaul Bohm for (i = 0; i < fdchangecnt; ++i)
2096e3a38431SPaul Bohm {
2097e3a38431SPaul Bohm int fd = fdchanges [i];
2098e3a38431SPaul Bohm ANFD *anfd = anfds + fd;
2099e3a38431SPaul Bohm ev_io *w;
2100e3a38431SPaul Bohm
2101e3a38431SPaul Bohm unsigned char o_events = anfd->events;
2102e3a38431SPaul Bohm unsigned char o_reify = anfd->reify;
2103e3a38431SPaul Bohm
2104e3a38431SPaul Bohm anfd->reify = 0;
2105e3a38431SPaul Bohm
2106e3a38431SPaul Bohm /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
2107e3a38431SPaul Bohm {
2108e3a38431SPaul Bohm anfd->events = 0;
2109e3a38431SPaul Bohm
2110e3a38431SPaul Bohm for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
2111e3a38431SPaul Bohm anfd->events |= (unsigned char)w->events;
2112e3a38431SPaul Bohm
2113e3a38431SPaul Bohm if (o_events != anfd->events)
2114e3a38431SPaul Bohm o_reify = EV__IOFDSET; /* actually |= */
2115e3a38431SPaul Bohm }
2116e3a38431SPaul Bohm
2117e3a38431SPaul Bohm if (o_reify & EV__IOFDSET)
2118e3a38431SPaul Bohm backend_modify (EV_A_ fd, o_events, anfd->events);
2119e3a38431SPaul Bohm }
2120e3a38431SPaul Bohm
2121e3a38431SPaul Bohm fdchangecnt = 0;
2122e3a38431SPaul Bohm }
2123e3a38431SPaul Bohm
2124e3a38431SPaul Bohm /* something about the given fd changed */
2125e3a38431SPaul Bohm inline_size void
fd_change(EV_P_ int fd,int flags)2126e3a38431SPaul Bohm fd_change (EV_P_ int fd, int flags)
2127e3a38431SPaul Bohm {
2128e3a38431SPaul Bohm unsigned char reify = anfds [fd].reify;
2129e3a38431SPaul Bohm anfds [fd].reify |= flags;
2130e3a38431SPaul Bohm
2131e3a38431SPaul Bohm if (expect_true (!reify))
2132e3a38431SPaul Bohm {
2133e3a38431SPaul Bohm ++fdchangecnt;
2134e3a38431SPaul Bohm array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
2135e3a38431SPaul Bohm fdchanges [fdchangecnt - 1] = fd;
2136e3a38431SPaul Bohm }
2137e3a38431SPaul Bohm }
2138e3a38431SPaul Bohm
2139e3a38431SPaul Bohm /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
2140*93823e6cSPaul Bohm inline_speed void ecb_cold
fd_kill(EV_P_ int fd)2141e3a38431SPaul Bohm fd_kill (EV_P_ int fd)
2142e3a38431SPaul Bohm {
2143e3a38431SPaul Bohm ev_io *w;
2144e3a38431SPaul Bohm
2145e3a38431SPaul Bohm while ((w = (ev_io *)anfds [fd].head))
2146e3a38431SPaul Bohm {
2147e3a38431SPaul Bohm ev_io_stop (EV_A_ w);
2148e3a38431SPaul Bohm ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
2149e3a38431SPaul Bohm }
2150e3a38431SPaul Bohm }
2151e3a38431SPaul Bohm
2152e3a38431SPaul Bohm /* check whether the given fd is actually valid, for error recovery */
2153*93823e6cSPaul Bohm inline_size int ecb_cold
fd_valid(int fd)2154e3a38431SPaul Bohm fd_valid (int fd)
2155e3a38431SPaul Bohm {
2156e3a38431SPaul Bohm #ifdef _WIN32
2157e3a38431SPaul Bohm return EV_FD_TO_WIN32_HANDLE (fd) != -1;
2158e3a38431SPaul Bohm #else
2159e3a38431SPaul Bohm return fcntl (fd, F_GETFD) != -1;
2160e3a38431SPaul Bohm #endif
2161e3a38431SPaul Bohm }
2162e3a38431SPaul Bohm
2163e3a38431SPaul Bohm /* called on EBADF to verify fds */
2164*93823e6cSPaul Bohm static void noinline ecb_cold
fd_ebadf(EV_P)2165e3a38431SPaul Bohm fd_ebadf (EV_P)
2166e3a38431SPaul Bohm {
2167e3a38431SPaul Bohm int fd;
2168e3a38431SPaul Bohm
2169e3a38431SPaul Bohm for (fd = 0; fd < anfdmax; ++fd)
2170e3a38431SPaul Bohm if (anfds [fd].events)
2171e3a38431SPaul Bohm if (!fd_valid (fd) && errno == EBADF)
2172e3a38431SPaul Bohm fd_kill (EV_A_ fd);
2173e3a38431SPaul Bohm }
2174e3a38431SPaul Bohm
2175e3a38431SPaul Bohm /* called on ENOMEM in select/poll to kill some fds and retry */
2176*93823e6cSPaul Bohm static void noinline ecb_cold
fd_enomem(EV_P)2177e3a38431SPaul Bohm fd_enomem (EV_P)
2178e3a38431SPaul Bohm {
2179e3a38431SPaul Bohm int fd;
2180e3a38431SPaul Bohm
2181e3a38431SPaul Bohm for (fd = anfdmax; fd--; )
2182e3a38431SPaul Bohm if (anfds [fd].events)
2183e3a38431SPaul Bohm {
2184e3a38431SPaul Bohm fd_kill (EV_A_ fd);
2185e3a38431SPaul Bohm break;
2186e3a38431SPaul Bohm }
2187e3a38431SPaul Bohm }
2188e3a38431SPaul Bohm
2189e3a38431SPaul Bohm /* usually called after fork if backend needs to re-arm all fds from scratch */
2190e3a38431SPaul Bohm static void noinline
fd_rearm_all(EV_P)2191e3a38431SPaul Bohm fd_rearm_all (EV_P)
2192e3a38431SPaul Bohm {
2193e3a38431SPaul Bohm int fd;
2194e3a38431SPaul Bohm
2195e3a38431SPaul Bohm for (fd = 0; fd < anfdmax; ++fd)
2196e3a38431SPaul Bohm if (anfds [fd].events)
2197e3a38431SPaul Bohm {
2198e3a38431SPaul Bohm anfds [fd].events = 0;
2199e3a38431SPaul Bohm anfds [fd].emask = 0;
2200e3a38431SPaul Bohm fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
2201e3a38431SPaul Bohm }
2202e3a38431SPaul Bohm }
2203e3a38431SPaul Bohm
2204e3a38431SPaul Bohm /* used to prepare libev internal fd's */
2205e3a38431SPaul Bohm /* this is not fork-safe */
2206e3a38431SPaul Bohm inline_speed void
fd_intern(int fd)2207e3a38431SPaul Bohm fd_intern (int fd)
2208e3a38431SPaul Bohm {
2209e3a38431SPaul Bohm #ifdef _WIN32
2210e3a38431SPaul Bohm unsigned long arg = 1;
2211e3a38431SPaul Bohm ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
2212e3a38431SPaul Bohm #else
2213e3a38431SPaul Bohm fcntl (fd, F_SETFD, FD_CLOEXEC);
2214e3a38431SPaul Bohm fcntl (fd, F_SETFL, O_NONBLOCK);
2215e3a38431SPaul Bohm #endif
2216e3a38431SPaul Bohm }
2217e3a38431SPaul Bohm
2218e3a38431SPaul Bohm /*****************************************************************************/
2219e3a38431SPaul Bohm
2220e3a38431SPaul Bohm /*
2221e3a38431SPaul Bohm * the heap functions want a real array index. array index 0 is guaranteed to not
2222e3a38431SPaul Bohm * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
2223e3a38431SPaul Bohm * the branching factor of the d-tree.
2224e3a38431SPaul Bohm */
2225e3a38431SPaul Bohm
2226e3a38431SPaul Bohm /*
2227e3a38431SPaul Bohm * at the moment we allow libev the luxury of two heaps,
2228e3a38431SPaul Bohm * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
2229e3a38431SPaul Bohm * which is more cache-efficient.
2230e3a38431SPaul Bohm * the difference is about 5% with 50000+ watchers.
2231e3a38431SPaul Bohm */
2232e3a38431SPaul Bohm #if EV_USE_4HEAP
2233e3a38431SPaul Bohm
2234e3a38431SPaul Bohm #define DHEAP 4
2235e3a38431SPaul Bohm #define HEAP0 (DHEAP - 1) /* index of first element in heap */
2236e3a38431SPaul Bohm #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
2237e3a38431SPaul Bohm #define UPHEAP_DONE(p,k) ((p) == (k))
2238e3a38431SPaul Bohm
2239e3a38431SPaul Bohm /* away from the root */
2240e3a38431SPaul Bohm inline_speed void
downheap(ANHE * heap,int N,int k)2241e3a38431SPaul Bohm downheap (ANHE *heap, int N, int k)
2242e3a38431SPaul Bohm {
2243e3a38431SPaul Bohm ANHE he = heap [k];
2244e3a38431SPaul Bohm ANHE *E = heap + N + HEAP0;
2245e3a38431SPaul Bohm
2246e3a38431SPaul Bohm for (;;)
2247e3a38431SPaul Bohm {
2248e3a38431SPaul Bohm ev_tstamp minat;
2249e3a38431SPaul Bohm ANHE *minpos;
2250e3a38431SPaul Bohm ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
2251e3a38431SPaul Bohm
2252e3a38431SPaul Bohm /* find minimum child */
2253e3a38431SPaul Bohm if (expect_true (pos + DHEAP - 1 < E))
2254e3a38431SPaul Bohm {
2255e3a38431SPaul Bohm /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
2256e3a38431SPaul Bohm if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
2257e3a38431SPaul Bohm if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
2258e3a38431SPaul Bohm if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
2259e3a38431SPaul Bohm }
2260e3a38431SPaul Bohm else if (pos < E)
2261e3a38431SPaul Bohm {
2262e3a38431SPaul Bohm /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
2263e3a38431SPaul Bohm if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
2264e3a38431SPaul Bohm if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
2265e3a38431SPaul Bohm if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
2266e3a38431SPaul Bohm }
2267e3a38431SPaul Bohm else
2268e3a38431SPaul Bohm break;
2269e3a38431SPaul Bohm
2270e3a38431SPaul Bohm if (ANHE_at (he) <= minat)
2271e3a38431SPaul Bohm break;
2272e3a38431SPaul Bohm
2273e3a38431SPaul Bohm heap [k] = *minpos;
2274e3a38431SPaul Bohm ev_active (ANHE_w (*minpos)) = k;
2275e3a38431SPaul Bohm
2276e3a38431SPaul Bohm k = minpos - heap;
2277e3a38431SPaul Bohm }
2278e3a38431SPaul Bohm
2279e3a38431SPaul Bohm heap [k] = he;
2280e3a38431SPaul Bohm ev_active (ANHE_w (he)) = k;
2281e3a38431SPaul Bohm }
2282e3a38431SPaul Bohm
2283e3a38431SPaul Bohm #else /* 4HEAP */
2284e3a38431SPaul Bohm
2285e3a38431SPaul Bohm #define HEAP0 1
2286e3a38431SPaul Bohm #define HPARENT(k) ((k) >> 1)
2287e3a38431SPaul Bohm #define UPHEAP_DONE(p,k) (!(p))
2288e3a38431SPaul Bohm
2289e3a38431SPaul Bohm /* away from the root */
2290e3a38431SPaul Bohm inline_speed void
downheap(ANHE * heap,int N,int k)2291e3a38431SPaul Bohm downheap (ANHE *heap, int N, int k)
2292e3a38431SPaul Bohm {
2293e3a38431SPaul Bohm ANHE he = heap [k];
2294e3a38431SPaul Bohm
2295e3a38431SPaul Bohm for (;;)
2296e3a38431SPaul Bohm {
2297e3a38431SPaul Bohm int c = k << 1;
2298e3a38431SPaul Bohm
2299e3a38431SPaul Bohm if (c >= N + HEAP0)
2300e3a38431SPaul Bohm break;
2301e3a38431SPaul Bohm
2302e3a38431SPaul Bohm c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
2303e3a38431SPaul Bohm ? 1 : 0;
2304e3a38431SPaul Bohm
2305e3a38431SPaul Bohm if (ANHE_at (he) <= ANHE_at (heap [c]))
2306e3a38431SPaul Bohm break;
2307e3a38431SPaul Bohm
2308e3a38431SPaul Bohm heap [k] = heap [c];
2309e3a38431SPaul Bohm ev_active (ANHE_w (heap [k])) = k;
2310e3a38431SPaul Bohm
2311e3a38431SPaul Bohm k = c;
2312e3a38431SPaul Bohm }
2313e3a38431SPaul Bohm
2314e3a38431SPaul Bohm heap [k] = he;
2315e3a38431SPaul Bohm ev_active (ANHE_w (he)) = k;
2316e3a38431SPaul Bohm }
2317e3a38431SPaul Bohm #endif
2318e3a38431SPaul Bohm
2319e3a38431SPaul Bohm /* towards the root */
2320e3a38431SPaul Bohm inline_speed void
upheap(ANHE * heap,int k)2321e3a38431SPaul Bohm upheap (ANHE *heap, int k)
2322e3a38431SPaul Bohm {
2323e3a38431SPaul Bohm ANHE he = heap [k];
2324e3a38431SPaul Bohm
2325e3a38431SPaul Bohm for (;;)
2326e3a38431SPaul Bohm {
2327e3a38431SPaul Bohm int p = HPARENT (k);
2328e3a38431SPaul Bohm
2329e3a38431SPaul Bohm if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he))
2330e3a38431SPaul Bohm break;
2331e3a38431SPaul Bohm
2332e3a38431SPaul Bohm heap [k] = heap [p];
2333e3a38431SPaul Bohm ev_active (ANHE_w (heap [k])) = k;
2334e3a38431SPaul Bohm k = p;
2335e3a38431SPaul Bohm }
2336e3a38431SPaul Bohm
2337e3a38431SPaul Bohm heap [k] = he;
2338e3a38431SPaul Bohm ev_active (ANHE_w (he)) = k;
2339e3a38431SPaul Bohm }
2340e3a38431SPaul Bohm
2341e3a38431SPaul Bohm /* move an element suitably so it is in a correct place */
2342e3a38431SPaul Bohm inline_size void
adjustheap(ANHE * heap,int N,int k)2343e3a38431SPaul Bohm adjustheap (ANHE *heap, int N, int k)
2344e3a38431SPaul Bohm {
2345e3a38431SPaul Bohm if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
2346e3a38431SPaul Bohm upheap (heap, k);
2347e3a38431SPaul Bohm else
2348e3a38431SPaul Bohm downheap (heap, N, k);
2349e3a38431SPaul Bohm }
2350e3a38431SPaul Bohm
2351e3a38431SPaul Bohm /* rebuild the heap: this function is used only once and executed rarely */
2352e3a38431SPaul Bohm inline_size void
reheap(ANHE * heap,int N)2353e3a38431SPaul Bohm reheap (ANHE *heap, int N)
2354e3a38431SPaul Bohm {
2355e3a38431SPaul Bohm int i;
2356e3a38431SPaul Bohm
2357e3a38431SPaul Bohm /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
2358e3a38431SPaul Bohm /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
2359e3a38431SPaul Bohm for (i = 0; i < N; ++i)
2360e3a38431SPaul Bohm upheap (heap, i + HEAP0);
2361e3a38431SPaul Bohm }
2362e3a38431SPaul Bohm
2363e3a38431SPaul Bohm /*****************************************************************************/
2364e3a38431SPaul Bohm
2365e3a38431SPaul Bohm /* associate signal watchers to a signal signal */
2366e3a38431SPaul Bohm typedef struct
2367e3a38431SPaul Bohm {
2368e3a38431SPaul Bohm EV_ATOMIC_T pending;
2369e3a38431SPaul Bohm #if EV_MULTIPLICITY
2370e3a38431SPaul Bohm EV_P;
2371e3a38431SPaul Bohm #endif
2372e3a38431SPaul Bohm WL head;
2373e3a38431SPaul Bohm } ANSIG;
2374e3a38431SPaul Bohm
2375e3a38431SPaul Bohm static ANSIG signals [EV_NSIG - 1];
2376e3a38431SPaul Bohm
2377e3a38431SPaul Bohm /*****************************************************************************/
2378e3a38431SPaul Bohm
2379e3a38431SPaul Bohm #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2380e3a38431SPaul Bohm
2381*93823e6cSPaul Bohm static void noinline ecb_cold
evpipe_init(EV_P)2382e3a38431SPaul Bohm evpipe_init (EV_P)
2383e3a38431SPaul Bohm {
2384e3a38431SPaul Bohm if (!ev_is_active (&pipe_w))
2385e3a38431SPaul Bohm {
2386*93823e6cSPaul Bohm int fds [2];
2387e3a38431SPaul Bohm
2388*93823e6cSPaul Bohm # if EV_USE_EVENTFD
2389*93823e6cSPaul Bohm fds [0] = -1;
2390*93823e6cSPaul Bohm fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
2391*93823e6cSPaul Bohm if (fds [1] < 0 && errno == EINVAL)
2392*93823e6cSPaul Bohm fds [1] = eventfd (0, 0);
2393*93823e6cSPaul Bohm
2394*93823e6cSPaul Bohm if (fds [1] < 0)
2395e3a38431SPaul Bohm # endif
2396e3a38431SPaul Bohm {
2397*93823e6cSPaul Bohm while (pipe (fds))
2398e3a38431SPaul Bohm ev_syserr ("(libev) error creating signal/async pipe");
2399e3a38431SPaul Bohm
2400*93823e6cSPaul Bohm fd_intern (fds [0]);
2401e3a38431SPaul Bohm }
2402e3a38431SPaul Bohm
2403*93823e6cSPaul Bohm evpipe [0] = fds [0];
2404*93823e6cSPaul Bohm
2405*93823e6cSPaul Bohm if (evpipe [1] < 0)
2406*93823e6cSPaul Bohm evpipe [1] = fds [1]; /* first call, set write fd */
2407*93823e6cSPaul Bohm else
2408*93823e6cSPaul Bohm {
2409*93823e6cSPaul Bohm /* on subsequent calls, do not change evpipe [1] */
2410*93823e6cSPaul Bohm /* so that evpipe_write can always rely on its value. */
2411*93823e6cSPaul Bohm /* this branch does not do anything sensible on windows, */
2412*93823e6cSPaul Bohm /* so must not be executed on windows */
2413*93823e6cSPaul Bohm
2414*93823e6cSPaul Bohm dup2 (fds [1], evpipe [1]);
2415*93823e6cSPaul Bohm close (fds [1]);
2416*93823e6cSPaul Bohm }
2417*93823e6cSPaul Bohm
2418*93823e6cSPaul Bohm fd_intern (evpipe [1]);
2419*93823e6cSPaul Bohm
2420*93823e6cSPaul Bohm ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
2421e3a38431SPaul Bohm ev_io_start (EV_A_ &pipe_w);
2422e3a38431SPaul Bohm ev_unref (EV_A); /* watcher should not keep loop alive */
2423e3a38431SPaul Bohm }
2424e3a38431SPaul Bohm }
2425e3a38431SPaul Bohm
2426*93823e6cSPaul Bohm inline_speed void
evpipe_write(EV_P_ EV_ATOMIC_T * flag)2427e3a38431SPaul Bohm evpipe_write (EV_P_ EV_ATOMIC_T *flag)
2428e3a38431SPaul Bohm {
2429*93823e6cSPaul Bohm ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
2430*93823e6cSPaul Bohm
2431*93823e6cSPaul Bohm if (expect_true (*flag))
2432*93823e6cSPaul Bohm return;
2433e3a38431SPaul Bohm
2434e3a38431SPaul Bohm *flag = 1;
2435*93823e6cSPaul Bohm ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
2436*93823e6cSPaul Bohm
2437*93823e6cSPaul Bohm pipe_write_skipped = 1;
2438*93823e6cSPaul Bohm
2439*93823e6cSPaul Bohm ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
2440*93823e6cSPaul Bohm
2441*93823e6cSPaul Bohm if (pipe_write_wanted)
2442*93823e6cSPaul Bohm {
2443*93823e6cSPaul Bohm int old_errno;
2444*93823e6cSPaul Bohm
2445*93823e6cSPaul Bohm pipe_write_skipped = 0;
2446*93823e6cSPaul Bohm ECB_MEMORY_FENCE_RELEASE;
2447*93823e6cSPaul Bohm
2448*93823e6cSPaul Bohm old_errno = errno; /* save errno because write will clobber it */
2449e3a38431SPaul Bohm
2450e3a38431SPaul Bohm #if EV_USE_EVENTFD
2451*93823e6cSPaul Bohm if (evpipe [0] < 0)
2452e3a38431SPaul Bohm {
2453e3a38431SPaul Bohm uint64_t counter = 1;
2454*93823e6cSPaul Bohm write (evpipe [1], &counter, sizeof (uint64_t));
2455e3a38431SPaul Bohm }
2456e3a38431SPaul Bohm else
2457e3a38431SPaul Bohm #endif
2458*93823e6cSPaul Bohm {
2459*93823e6cSPaul Bohm #ifdef _WIN32
2460*93823e6cSPaul Bohm WSABUF buf;
2461*93823e6cSPaul Bohm DWORD sent;
2462*93823e6cSPaul Bohm buf.buf = &buf;
2463*93823e6cSPaul Bohm buf.len = 1;
2464*93823e6cSPaul Bohm WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
2465*93823e6cSPaul Bohm #else
2466*93823e6cSPaul Bohm write (evpipe [1], &(evpipe [1]), 1);
2467*93823e6cSPaul Bohm #endif
2468*93823e6cSPaul Bohm }
2469e3a38431SPaul Bohm
2470e3a38431SPaul Bohm errno = old_errno;
2471e3a38431SPaul Bohm }
2472e3a38431SPaul Bohm }
2473e3a38431SPaul Bohm
2474e3a38431SPaul Bohm /* called whenever the libev signal pipe */
2475e3a38431SPaul Bohm /* got some events (signal, async) */
2476e3a38431SPaul Bohm static void
pipecb(EV_P_ ev_io * iow,int revents)2477e3a38431SPaul Bohm pipecb (EV_P_ ev_io *iow, int revents)
2478e3a38431SPaul Bohm {
2479e3a38431SPaul Bohm int i;
2480e3a38431SPaul Bohm
2481*93823e6cSPaul Bohm if (revents & EV_READ)
2482*93823e6cSPaul Bohm {
2483e3a38431SPaul Bohm #if EV_USE_EVENTFD
2484*93823e6cSPaul Bohm if (evpipe [0] < 0)
2485e3a38431SPaul Bohm {
2486e3a38431SPaul Bohm uint64_t counter;
2487*93823e6cSPaul Bohm read (evpipe [1], &counter, sizeof (uint64_t));
2488e3a38431SPaul Bohm }
2489e3a38431SPaul Bohm else
2490e3a38431SPaul Bohm #endif
2491e3a38431SPaul Bohm {
2492*93823e6cSPaul Bohm char dummy[4];
2493*93823e6cSPaul Bohm #ifdef _WIN32
2494*93823e6cSPaul Bohm WSABUF buf;
2495*93823e6cSPaul Bohm DWORD recvd;
2496*93823e6cSPaul Bohm DWORD flags = 0;
2497*93823e6cSPaul Bohm buf.buf = dummy;
2498*93823e6cSPaul Bohm buf.len = sizeof (dummy);
2499*93823e6cSPaul Bohm WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
2500*93823e6cSPaul Bohm #else
2501*93823e6cSPaul Bohm read (evpipe [0], &dummy, sizeof (dummy));
2502*93823e6cSPaul Bohm #endif
2503e3a38431SPaul Bohm }
2504*93823e6cSPaul Bohm }
2505*93823e6cSPaul Bohm
2506*93823e6cSPaul Bohm pipe_write_skipped = 0;
2507*93823e6cSPaul Bohm
2508*93823e6cSPaul Bohm ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
2509e3a38431SPaul Bohm
2510e3a38431SPaul Bohm #if EV_SIGNAL_ENABLE
2511e3a38431SPaul Bohm if (sig_pending)
2512e3a38431SPaul Bohm {
2513e3a38431SPaul Bohm sig_pending = 0;
2514e3a38431SPaul Bohm
2515*93823e6cSPaul Bohm ECB_MEMORY_FENCE;
2516*93823e6cSPaul Bohm
2517e3a38431SPaul Bohm for (i = EV_NSIG - 1; i--; )
2518e3a38431SPaul Bohm if (expect_false (signals [i].pending))
2519e3a38431SPaul Bohm ev_feed_signal_event (EV_A_ i + 1);
2520e3a38431SPaul Bohm }
2521e3a38431SPaul Bohm #endif
2522e3a38431SPaul Bohm
2523e3a38431SPaul Bohm #if EV_ASYNC_ENABLE
2524e3a38431SPaul Bohm if (async_pending)
2525e3a38431SPaul Bohm {
2526e3a38431SPaul Bohm async_pending = 0;
2527e3a38431SPaul Bohm
2528*93823e6cSPaul Bohm ECB_MEMORY_FENCE;
2529*93823e6cSPaul Bohm
2530e3a38431SPaul Bohm for (i = asynccnt; i--; )
2531e3a38431SPaul Bohm if (asyncs [i]->sent)
2532e3a38431SPaul Bohm {
2533e3a38431SPaul Bohm asyncs [i]->sent = 0;
2534*93823e6cSPaul Bohm ECB_MEMORY_FENCE_RELEASE;
2535e3a38431SPaul Bohm ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
2536e3a38431SPaul Bohm }
2537e3a38431SPaul Bohm }
2538e3a38431SPaul Bohm #endif
2539e3a38431SPaul Bohm }
2540e3a38431SPaul Bohm
2541e3a38431SPaul Bohm /*****************************************************************************/
2542e3a38431SPaul Bohm
2543e3a38431SPaul Bohm void
ev_feed_signal(int signum)2544*93823e6cSPaul Bohm ev_feed_signal (int signum) EV_THROW
2545e3a38431SPaul Bohm {
2546e3a38431SPaul Bohm #if EV_MULTIPLICITY
2547*93823e6cSPaul Bohm EV_P;
2548*93823e6cSPaul Bohm ECB_MEMORY_FENCE_ACQUIRE;
2549*93823e6cSPaul Bohm EV_A = signals [signum - 1].loop;
2550e3a38431SPaul Bohm
2551e3a38431SPaul Bohm if (!EV_A)
2552e3a38431SPaul Bohm return;
2553e3a38431SPaul Bohm #endif
2554e3a38431SPaul Bohm
2555e3a38431SPaul Bohm signals [signum - 1].pending = 1;
2556e3a38431SPaul Bohm evpipe_write (EV_A_ &sig_pending);
2557e3a38431SPaul Bohm }
2558e3a38431SPaul Bohm
2559e3a38431SPaul Bohm static void
ev_sighandler(int signum)2560e3a38431SPaul Bohm ev_sighandler (int signum)
2561e3a38431SPaul Bohm {
2562e3a38431SPaul Bohm #ifdef _WIN32
2563e3a38431SPaul Bohm signal (signum, ev_sighandler);
2564e3a38431SPaul Bohm #endif
2565e3a38431SPaul Bohm
2566e3a38431SPaul Bohm ev_feed_signal (signum);
2567e3a38431SPaul Bohm }
2568e3a38431SPaul Bohm
2569e3a38431SPaul Bohm void noinline
ev_feed_signal_event(EV_P_ int signum)2570*93823e6cSPaul Bohm ev_feed_signal_event (EV_P_ int signum) EV_THROW
2571e3a38431SPaul Bohm {
2572e3a38431SPaul Bohm WL w;
2573e3a38431SPaul Bohm
2574*93823e6cSPaul Bohm if (expect_false (signum <= 0 || signum >= EV_NSIG))
2575e3a38431SPaul Bohm return;
2576e3a38431SPaul Bohm
2577e3a38431SPaul Bohm --signum;
2578e3a38431SPaul Bohm
2579e3a38431SPaul Bohm #if EV_MULTIPLICITY
2580e3a38431SPaul Bohm /* it is permissible to try to feed a signal to the wrong loop */
2581e3a38431SPaul Bohm /* or, likely more useful, feeding a signal nobody is waiting for */
2582e3a38431SPaul Bohm
2583e3a38431SPaul Bohm if (expect_false (signals [signum].loop != EV_A))
2584e3a38431SPaul Bohm return;
2585e3a38431SPaul Bohm #endif
2586e3a38431SPaul Bohm
2587e3a38431SPaul Bohm signals [signum].pending = 0;
2588*93823e6cSPaul Bohm ECB_MEMORY_FENCE_RELEASE;
2589e3a38431SPaul Bohm
2590e3a38431SPaul Bohm for (w = signals [signum].head; w; w = w->next)
2591e3a38431SPaul Bohm ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
2592e3a38431SPaul Bohm }
2593e3a38431SPaul Bohm
2594e3a38431SPaul Bohm #if EV_USE_SIGNALFD
2595e3a38431SPaul Bohm static void
sigfdcb(EV_P_ ev_io * iow,int revents)2596e3a38431SPaul Bohm sigfdcb (EV_P_ ev_io *iow, int revents)
2597e3a38431SPaul Bohm {
2598e3a38431SPaul Bohm struct signalfd_siginfo si[2], *sip; /* these structs are big */
2599e3a38431SPaul Bohm
2600e3a38431SPaul Bohm for (;;)
2601e3a38431SPaul Bohm {
2602e3a38431SPaul Bohm ssize_t res = read (sigfd, si, sizeof (si));
2603e3a38431SPaul Bohm
2604e3a38431SPaul Bohm /* not ISO-C, as res might be -1, but works with SuS */
2605e3a38431SPaul Bohm for (sip = si; (char *)sip < (char *)si + res; ++sip)
2606e3a38431SPaul Bohm ev_feed_signal_event (EV_A_ sip->ssi_signo);
2607e3a38431SPaul Bohm
2608e3a38431SPaul Bohm if (res < (ssize_t)sizeof (si))
2609e3a38431SPaul Bohm break;
2610e3a38431SPaul Bohm }
2611e3a38431SPaul Bohm }
2612e3a38431SPaul Bohm #endif
2613e3a38431SPaul Bohm
2614e3a38431SPaul Bohm #endif
2615e3a38431SPaul Bohm
2616e3a38431SPaul Bohm /*****************************************************************************/
2617e3a38431SPaul Bohm
2618e3a38431SPaul Bohm #if EV_CHILD_ENABLE
2619e3a38431SPaul Bohm static WL childs [EV_PID_HASHSIZE];
2620e3a38431SPaul Bohm
2621e3a38431SPaul Bohm static ev_signal childev;
2622e3a38431SPaul Bohm
2623e3a38431SPaul Bohm #ifndef WIFCONTINUED
2624e3a38431SPaul Bohm # define WIFCONTINUED(status) 0
2625e3a38431SPaul Bohm #endif
2626e3a38431SPaul Bohm
2627e3a38431SPaul Bohm /* handle a single child status event */
2628e3a38431SPaul Bohm inline_speed void
child_reap(EV_P_ int chain,int pid,int status)2629e3a38431SPaul Bohm child_reap (EV_P_ int chain, int pid, int status)
2630e3a38431SPaul Bohm {
2631e3a38431SPaul Bohm ev_child *w;
2632e3a38431SPaul Bohm int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
2633e3a38431SPaul Bohm
2634e3a38431SPaul Bohm for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
2635e3a38431SPaul Bohm {
2636e3a38431SPaul Bohm if ((w->pid == pid || !w->pid)
2637e3a38431SPaul Bohm && (!traced || (w->flags & 1)))
2638e3a38431SPaul Bohm {
2639e3a38431SPaul Bohm ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
2640e3a38431SPaul Bohm w->rpid = pid;
2641e3a38431SPaul Bohm w->rstatus = status;
2642e3a38431SPaul Bohm ev_feed_event (EV_A_ (W)w, EV_CHILD);
2643e3a38431SPaul Bohm }
2644e3a38431SPaul Bohm }
2645e3a38431SPaul Bohm }
2646e3a38431SPaul Bohm
2647e3a38431SPaul Bohm #ifndef WCONTINUED
2648e3a38431SPaul Bohm # define WCONTINUED 0
2649e3a38431SPaul Bohm #endif
2650e3a38431SPaul Bohm
2651e3a38431SPaul Bohm /* called on sigchld etc., calls waitpid */
2652e3a38431SPaul Bohm static void
childcb(EV_P_ ev_signal * sw,int revents)2653e3a38431SPaul Bohm childcb (EV_P_ ev_signal *sw, int revents)
2654e3a38431SPaul Bohm {
2655e3a38431SPaul Bohm int pid, status;
2656e3a38431SPaul Bohm
2657e3a38431SPaul Bohm /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
2658e3a38431SPaul Bohm if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
2659e3a38431SPaul Bohm if (!WCONTINUED
2660e3a38431SPaul Bohm || errno != EINVAL
2661e3a38431SPaul Bohm || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
2662e3a38431SPaul Bohm return;
2663e3a38431SPaul Bohm
2664e3a38431SPaul Bohm /* make sure we are called again until all children have been reaped */
2665e3a38431SPaul Bohm /* we need to do it this way so that the callback gets called before we continue */
2666e3a38431SPaul Bohm ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
2667e3a38431SPaul Bohm
2668e3a38431SPaul Bohm child_reap (EV_A_ pid, pid, status);
2669e3a38431SPaul Bohm if ((EV_PID_HASHSIZE) > 1)
2670e3a38431SPaul Bohm child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
2671e3a38431SPaul Bohm }
2672e3a38431SPaul Bohm
2673e3a38431SPaul Bohm #endif
2674e3a38431SPaul Bohm
2675e3a38431SPaul Bohm /*****************************************************************************/
2676e3a38431SPaul Bohm
2677e3a38431SPaul Bohm #if EV_USE_IOCP
2678e3a38431SPaul Bohm # include "ev_iocp.c"
2679e3a38431SPaul Bohm #endif
2680e3a38431SPaul Bohm #if EV_USE_PORT
2681e3a38431SPaul Bohm # include "ev_port.c"
2682e3a38431SPaul Bohm #endif
2683e3a38431SPaul Bohm #if EV_USE_KQUEUE
2684e3a38431SPaul Bohm # include "ev_kqueue.c"
2685e3a38431SPaul Bohm #endif
2686e3a38431SPaul Bohm #if EV_USE_EPOLL
2687e3a38431SPaul Bohm # include "ev_epoll.c"
2688e3a38431SPaul Bohm #endif
2689e3a38431SPaul Bohm #if EV_USE_POLL
2690e3a38431SPaul Bohm # include "ev_poll.c"
2691e3a38431SPaul Bohm #endif
2692e3a38431SPaul Bohm #if EV_USE_SELECT
2693e3a38431SPaul Bohm # include "ev_select.c"
2694e3a38431SPaul Bohm #endif
2695e3a38431SPaul Bohm
2696*93823e6cSPaul Bohm int ecb_cold
ev_version_major(void)2697*93823e6cSPaul Bohm ev_version_major (void) EV_THROW
2698e3a38431SPaul Bohm {
2699e3a38431SPaul Bohm return EV_VERSION_MAJOR;
2700e3a38431SPaul Bohm }
2701e3a38431SPaul Bohm
2702*93823e6cSPaul Bohm int ecb_cold
ev_version_minor(void)2703*93823e6cSPaul Bohm ev_version_minor (void) EV_THROW
2704e3a38431SPaul Bohm {
2705e3a38431SPaul Bohm return EV_VERSION_MINOR;
2706e3a38431SPaul Bohm }
2707e3a38431SPaul Bohm
2708e3a38431SPaul Bohm /* return true if we are running with elevated privileges and should ignore env variables */
2709*93823e6cSPaul Bohm int inline_size ecb_cold
enable_secure(void)2710e3a38431SPaul Bohm enable_secure (void)
2711e3a38431SPaul Bohm {
2712e3a38431SPaul Bohm #ifdef _WIN32
2713e3a38431SPaul Bohm return 0;
2714e3a38431SPaul Bohm #else
2715e3a38431SPaul Bohm return getuid () != geteuid ()
2716e3a38431SPaul Bohm || getgid () != getegid ();
2717e3a38431SPaul Bohm #endif
2718e3a38431SPaul Bohm }
2719e3a38431SPaul Bohm
2720*93823e6cSPaul Bohm unsigned int ecb_cold
ev_supported_backends(void)2721*93823e6cSPaul Bohm ev_supported_backends (void) EV_THROW
2722e3a38431SPaul Bohm {
2723e3a38431SPaul Bohm unsigned int flags = 0;
2724e3a38431SPaul Bohm
2725e3a38431SPaul Bohm if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2726e3a38431SPaul Bohm if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2727e3a38431SPaul Bohm if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
2728e3a38431SPaul Bohm if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
2729e3a38431SPaul Bohm if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
2730e3a38431SPaul Bohm
2731e3a38431SPaul Bohm return flags;
2732e3a38431SPaul Bohm }
2733e3a38431SPaul Bohm
2734*93823e6cSPaul Bohm unsigned int ecb_cold
ev_recommended_backends(void)2735*93823e6cSPaul Bohm ev_recommended_backends (void) EV_THROW
2736e3a38431SPaul Bohm {
2737e3a38431SPaul Bohm unsigned int flags = ev_supported_backends ();
2738e3a38431SPaul Bohm
2739e3a38431SPaul Bohm #ifndef __NetBSD__
2740e3a38431SPaul Bohm /* kqueue is borked on everything but netbsd apparently */
2741e3a38431SPaul Bohm /* it usually doesn't work correctly on anything but sockets and pipes */
2742e3a38431SPaul Bohm flags &= ~EVBACKEND_KQUEUE;
2743e3a38431SPaul Bohm #endif
2744e3a38431SPaul Bohm #ifdef __APPLE__
2745e3a38431SPaul Bohm /* only select works correctly on that "unix-certified" platform */
2746e3a38431SPaul Bohm flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
2747e3a38431SPaul Bohm flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
2748e3a38431SPaul Bohm #endif
2749e3a38431SPaul Bohm #ifdef __FreeBSD__
2750e3a38431SPaul Bohm flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
2751e3a38431SPaul Bohm #endif
2752e3a38431SPaul Bohm
2753e3a38431SPaul Bohm return flags;
2754e3a38431SPaul Bohm }
2755e3a38431SPaul Bohm
2756*93823e6cSPaul Bohm unsigned int ecb_cold
ev_embeddable_backends(void)2757*93823e6cSPaul Bohm ev_embeddable_backends (void) EV_THROW
2758e3a38431SPaul Bohm {
2759e3a38431SPaul Bohm int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2760e3a38431SPaul Bohm
2761e3a38431SPaul Bohm /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2762e3a38431SPaul Bohm if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2763e3a38431SPaul Bohm flags &= ~EVBACKEND_EPOLL;
2764e3a38431SPaul Bohm
2765e3a38431SPaul Bohm return flags;
2766e3a38431SPaul Bohm }
2767e3a38431SPaul Bohm
2768e3a38431SPaul Bohm unsigned int
ev_backend(EV_P)2769*93823e6cSPaul Bohm ev_backend (EV_P) EV_THROW
2770e3a38431SPaul Bohm {
2771e3a38431SPaul Bohm return backend;
2772e3a38431SPaul Bohm }
2773e3a38431SPaul Bohm
2774e3a38431SPaul Bohm #if EV_FEATURE_API
2775e3a38431SPaul Bohm unsigned int
ev_iteration(EV_P)2776*93823e6cSPaul Bohm ev_iteration (EV_P) EV_THROW
2777e3a38431SPaul Bohm {
2778e3a38431SPaul Bohm return loop_count;
2779e3a38431SPaul Bohm }
2780e3a38431SPaul Bohm
2781e3a38431SPaul Bohm unsigned int
ev_depth(EV_P)2782*93823e6cSPaul Bohm ev_depth (EV_P) EV_THROW
2783e3a38431SPaul Bohm {
2784e3a38431SPaul Bohm return loop_depth;
2785e3a38431SPaul Bohm }
2786e3a38431SPaul Bohm
2787e3a38431SPaul Bohm void
ev_set_io_collect_interval(EV_P_ ev_tstamp interval)2788*93823e6cSPaul Bohm ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2789e3a38431SPaul Bohm {
2790e3a38431SPaul Bohm io_blocktime = interval;
2791e3a38431SPaul Bohm }
2792e3a38431SPaul Bohm
2793e3a38431SPaul Bohm void
ev_set_timeout_collect_interval(EV_P_ ev_tstamp interval)2794*93823e6cSPaul Bohm ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2795e3a38431SPaul Bohm {
2796e3a38431SPaul Bohm timeout_blocktime = interval;
2797e3a38431SPaul Bohm }
2798e3a38431SPaul Bohm
2799e3a38431SPaul Bohm void
ev_set_userdata(EV_P_ void * data)2800*93823e6cSPaul Bohm ev_set_userdata (EV_P_ void *data) EV_THROW
2801e3a38431SPaul Bohm {
2802e3a38431SPaul Bohm userdata = data;
2803e3a38431SPaul Bohm }
2804e3a38431SPaul Bohm
2805e3a38431SPaul Bohm void *
ev_userdata(EV_P)2806*93823e6cSPaul Bohm ev_userdata (EV_P) EV_THROW
2807e3a38431SPaul Bohm {
2808e3a38431SPaul Bohm return userdata;
2809e3a38431SPaul Bohm }
2810e3a38431SPaul Bohm
2811*93823e6cSPaul Bohm void
ev_set_invoke_pending_cb(EV_P_ ev_loop_callback invoke_pending_cb)2812*93823e6cSPaul Bohm ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW
2813e3a38431SPaul Bohm {
2814e3a38431SPaul Bohm invoke_cb = invoke_pending_cb;
2815e3a38431SPaul Bohm }
2816e3a38431SPaul Bohm
2817*93823e6cSPaul Bohm void
ev_set_loop_release_cb(EV_P_ void (* release)(EV_P)EV_THROW,void (* acquire)(EV_P)EV_THROW)2818*93823e6cSPaul Bohm ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2819e3a38431SPaul Bohm {
2820e3a38431SPaul Bohm release_cb = release;
2821e3a38431SPaul Bohm acquire_cb = acquire;
2822e3a38431SPaul Bohm }
2823e3a38431SPaul Bohm #endif
2824e3a38431SPaul Bohm
2825e3a38431SPaul Bohm /* initialise a loop structure, must be zero-initialised */
2826*93823e6cSPaul Bohm static void noinline ecb_cold
loop_init(EV_P_ unsigned int flags)2827*93823e6cSPaul Bohm loop_init (EV_P_ unsigned int flags) EV_THROW
2828e3a38431SPaul Bohm {
2829e3a38431SPaul Bohm if (!backend)
2830e3a38431SPaul Bohm {
2831e3a38431SPaul Bohm origflags = flags;
2832e3a38431SPaul Bohm
2833e3a38431SPaul Bohm #if EV_USE_REALTIME
2834e3a38431SPaul Bohm if (!have_realtime)
2835e3a38431SPaul Bohm {
2836e3a38431SPaul Bohm struct timespec ts;
2837e3a38431SPaul Bohm
2838e3a38431SPaul Bohm if (!clock_gettime (CLOCK_REALTIME, &ts))
2839e3a38431SPaul Bohm have_realtime = 1;
2840e3a38431SPaul Bohm }
2841e3a38431SPaul Bohm #endif
2842e3a38431SPaul Bohm
2843e3a38431SPaul Bohm #if EV_USE_MONOTONIC
2844e3a38431SPaul Bohm if (!have_monotonic)
2845e3a38431SPaul Bohm {
2846e3a38431SPaul Bohm struct timespec ts;
2847e3a38431SPaul Bohm
2848e3a38431SPaul Bohm if (!clock_gettime (CLOCK_MONOTONIC, &ts))
2849e3a38431SPaul Bohm have_monotonic = 1;
2850e3a38431SPaul Bohm }
2851e3a38431SPaul Bohm #endif
2852e3a38431SPaul Bohm
2853e3a38431SPaul Bohm /* pid check not overridable via env */
2854e3a38431SPaul Bohm #ifndef _WIN32
2855e3a38431SPaul Bohm if (flags & EVFLAG_FORKCHECK)
2856e3a38431SPaul Bohm curpid = getpid ();
2857e3a38431SPaul Bohm #endif
2858e3a38431SPaul Bohm
2859e3a38431SPaul Bohm if (!(flags & EVFLAG_NOENV)
2860e3a38431SPaul Bohm && !enable_secure ()
2861e3a38431SPaul Bohm && getenv ("LIBEV_FLAGS"))
2862e3a38431SPaul Bohm flags = atoi (getenv ("LIBEV_FLAGS"));
2863e3a38431SPaul Bohm
2864e3a38431SPaul Bohm ev_rt_now = ev_time ();
2865e3a38431SPaul Bohm mn_now = get_clock ();
2866e3a38431SPaul Bohm now_floor = mn_now;
2867e3a38431SPaul Bohm rtmn_diff = ev_rt_now - mn_now;
2868e3a38431SPaul Bohm #if EV_FEATURE_API
2869e3a38431SPaul Bohm invoke_cb = ev_invoke_pending;
2870e3a38431SPaul Bohm #endif
2871e3a38431SPaul Bohm
2872e3a38431SPaul Bohm io_blocktime = 0.;
2873e3a38431SPaul Bohm timeout_blocktime = 0.;
2874e3a38431SPaul Bohm backend = 0;
2875e3a38431SPaul Bohm backend_fd = -1;
2876e3a38431SPaul Bohm sig_pending = 0;
2877e3a38431SPaul Bohm #if EV_ASYNC_ENABLE
2878e3a38431SPaul Bohm async_pending = 0;
2879e3a38431SPaul Bohm #endif
2880*93823e6cSPaul Bohm pipe_write_skipped = 0;
2881*93823e6cSPaul Bohm pipe_write_wanted = 0;
2882*93823e6cSPaul Bohm evpipe [0] = -1;
2883*93823e6cSPaul Bohm evpipe [1] = -1;
2884e3a38431SPaul Bohm #if EV_USE_INOTIFY
2885e3a38431SPaul Bohm fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
2886e3a38431SPaul Bohm #endif
2887e3a38431SPaul Bohm #if EV_USE_SIGNALFD
2888e3a38431SPaul Bohm sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
2889e3a38431SPaul Bohm #endif
2890e3a38431SPaul Bohm
2891e3a38431SPaul Bohm if (!(flags & EVBACKEND_MASK))
2892e3a38431SPaul Bohm flags |= ev_recommended_backends ();
2893e3a38431SPaul Bohm
2894e3a38431SPaul Bohm #if EV_USE_IOCP
2895e3a38431SPaul Bohm if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
2896e3a38431SPaul Bohm #endif
2897e3a38431SPaul Bohm #if EV_USE_PORT
2898e3a38431SPaul Bohm if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
2899e3a38431SPaul Bohm #endif
2900e3a38431SPaul Bohm #if EV_USE_KQUEUE
2901e3a38431SPaul Bohm if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
2902e3a38431SPaul Bohm #endif
2903e3a38431SPaul Bohm #if EV_USE_EPOLL
2904e3a38431SPaul Bohm if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
2905e3a38431SPaul Bohm #endif
2906e3a38431SPaul Bohm #if EV_USE_POLL
2907e3a38431SPaul Bohm if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
2908e3a38431SPaul Bohm #endif
2909e3a38431SPaul Bohm #if EV_USE_SELECT
2910e3a38431SPaul Bohm if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
2911e3a38431SPaul Bohm #endif
2912e3a38431SPaul Bohm
2913e3a38431SPaul Bohm ev_prepare_init (&pending_w, pendingcb);
2914e3a38431SPaul Bohm
2915e3a38431SPaul Bohm #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2916e3a38431SPaul Bohm ev_init (&pipe_w, pipecb);
2917e3a38431SPaul Bohm ev_set_priority (&pipe_w, EV_MAXPRI);
2918e3a38431SPaul Bohm #endif
2919e3a38431SPaul Bohm }
2920e3a38431SPaul Bohm }
2921e3a38431SPaul Bohm
2922e3a38431SPaul Bohm /* free up a loop structure */
2923*93823e6cSPaul Bohm void ecb_cold
ev_loop_destroy(EV_P)2924e3a38431SPaul Bohm ev_loop_destroy (EV_P)
2925e3a38431SPaul Bohm {
2926e3a38431SPaul Bohm int i;
2927e3a38431SPaul Bohm
2928e3a38431SPaul Bohm #if EV_MULTIPLICITY
2929e3a38431SPaul Bohm /* mimic free (0) */
2930e3a38431SPaul Bohm if (!EV_A)
2931e3a38431SPaul Bohm return;
2932e3a38431SPaul Bohm #endif
2933e3a38431SPaul Bohm
2934e3a38431SPaul Bohm #if EV_CLEANUP_ENABLE
2935e3a38431SPaul Bohm /* queue cleanup watchers (and execute them) */
2936e3a38431SPaul Bohm if (expect_false (cleanupcnt))
2937e3a38431SPaul Bohm {
2938e3a38431SPaul Bohm queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
2939e3a38431SPaul Bohm EV_INVOKE_PENDING;
2940e3a38431SPaul Bohm }
2941e3a38431SPaul Bohm #endif
2942e3a38431SPaul Bohm
2943e3a38431SPaul Bohm #if EV_CHILD_ENABLE
2944*93823e6cSPaul Bohm if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2945e3a38431SPaul Bohm {
2946e3a38431SPaul Bohm ev_ref (EV_A); /* child watcher */
2947e3a38431SPaul Bohm ev_signal_stop (EV_A_ &childev);
2948e3a38431SPaul Bohm }
2949e3a38431SPaul Bohm #endif
2950e3a38431SPaul Bohm
2951e3a38431SPaul Bohm if (ev_is_active (&pipe_w))
2952e3a38431SPaul Bohm {
2953e3a38431SPaul Bohm /*ev_ref (EV_A);*/
2954e3a38431SPaul Bohm /*ev_io_stop (EV_A_ &pipe_w);*/
2955e3a38431SPaul Bohm
2956*93823e6cSPaul Bohm if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
2957*93823e6cSPaul Bohm if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
2958e3a38431SPaul Bohm }
2959e3a38431SPaul Bohm
2960e3a38431SPaul Bohm #if EV_USE_SIGNALFD
2961e3a38431SPaul Bohm if (ev_is_active (&sigfd_w))
2962e3a38431SPaul Bohm close (sigfd);
2963e3a38431SPaul Bohm #endif
2964e3a38431SPaul Bohm
2965e3a38431SPaul Bohm #if EV_USE_INOTIFY
2966e3a38431SPaul Bohm if (fs_fd >= 0)
2967e3a38431SPaul Bohm close (fs_fd);
2968e3a38431SPaul Bohm #endif
2969e3a38431SPaul Bohm
2970e3a38431SPaul Bohm if (backend_fd >= 0)
2971e3a38431SPaul Bohm close (backend_fd);
2972e3a38431SPaul Bohm
2973e3a38431SPaul Bohm #if EV_USE_IOCP
2974e3a38431SPaul Bohm if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
2975e3a38431SPaul Bohm #endif
2976e3a38431SPaul Bohm #if EV_USE_PORT
2977e3a38431SPaul Bohm if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
2978e3a38431SPaul Bohm #endif
2979e3a38431SPaul Bohm #if EV_USE_KQUEUE
2980e3a38431SPaul Bohm if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
2981e3a38431SPaul Bohm #endif
2982e3a38431SPaul Bohm #if EV_USE_EPOLL
2983e3a38431SPaul Bohm if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
2984e3a38431SPaul Bohm #endif
2985e3a38431SPaul Bohm #if EV_USE_POLL
2986e3a38431SPaul Bohm if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
2987e3a38431SPaul Bohm #endif
2988e3a38431SPaul Bohm #if EV_USE_SELECT
2989e3a38431SPaul Bohm if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
2990e3a38431SPaul Bohm #endif
2991e3a38431SPaul Bohm
2992e3a38431SPaul Bohm for (i = NUMPRI; i--; )
2993e3a38431SPaul Bohm {
2994e3a38431SPaul Bohm array_free (pending, [i]);
2995e3a38431SPaul Bohm #if EV_IDLE_ENABLE
2996e3a38431SPaul Bohm array_free (idle, [i]);
2997e3a38431SPaul Bohm #endif
2998e3a38431SPaul Bohm }
2999e3a38431SPaul Bohm
3000e3a38431SPaul Bohm ev_free (anfds); anfds = 0; anfdmax = 0;
3001e3a38431SPaul Bohm
3002e3a38431SPaul Bohm /* have to use the microsoft-never-gets-it-right macro */
3003e3a38431SPaul Bohm array_free (rfeed, EMPTY);
3004e3a38431SPaul Bohm array_free (fdchange, EMPTY);
3005e3a38431SPaul Bohm array_free (timer, EMPTY);
3006e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3007e3a38431SPaul Bohm array_free (periodic, EMPTY);
3008e3a38431SPaul Bohm #endif
3009e3a38431SPaul Bohm #if EV_FORK_ENABLE
3010e3a38431SPaul Bohm array_free (fork, EMPTY);
3011e3a38431SPaul Bohm #endif
3012e3a38431SPaul Bohm #if EV_CLEANUP_ENABLE
3013e3a38431SPaul Bohm array_free (cleanup, EMPTY);
3014e3a38431SPaul Bohm #endif
3015e3a38431SPaul Bohm array_free (prepare, EMPTY);
3016e3a38431SPaul Bohm array_free (check, EMPTY);
3017e3a38431SPaul Bohm #if EV_ASYNC_ENABLE
3018e3a38431SPaul Bohm array_free (async, EMPTY);
3019e3a38431SPaul Bohm #endif
3020e3a38431SPaul Bohm
3021e3a38431SPaul Bohm backend = 0;
3022e3a38431SPaul Bohm
3023e3a38431SPaul Bohm #if EV_MULTIPLICITY
3024e3a38431SPaul Bohm if (ev_is_default_loop (EV_A))
3025e3a38431SPaul Bohm #endif
3026e3a38431SPaul Bohm ev_default_loop_ptr = 0;
3027e3a38431SPaul Bohm #if EV_MULTIPLICITY
3028e3a38431SPaul Bohm else
3029e3a38431SPaul Bohm ev_free (EV_A);
3030e3a38431SPaul Bohm #endif
3031e3a38431SPaul Bohm }
3032e3a38431SPaul Bohm
3033e3a38431SPaul Bohm #if EV_USE_INOTIFY
3034e3a38431SPaul Bohm inline_size void infy_fork (EV_P);
3035e3a38431SPaul Bohm #endif
3036e3a38431SPaul Bohm
3037e3a38431SPaul Bohm inline_size void
loop_fork(EV_P)3038e3a38431SPaul Bohm loop_fork (EV_P)
3039e3a38431SPaul Bohm {
3040e3a38431SPaul Bohm #if EV_USE_PORT
3041e3a38431SPaul Bohm if (backend == EVBACKEND_PORT ) port_fork (EV_A);
3042e3a38431SPaul Bohm #endif
3043e3a38431SPaul Bohm #if EV_USE_KQUEUE
3044e3a38431SPaul Bohm if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
3045e3a38431SPaul Bohm #endif
3046e3a38431SPaul Bohm #if EV_USE_EPOLL
3047e3a38431SPaul Bohm if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
3048e3a38431SPaul Bohm #endif
3049e3a38431SPaul Bohm #if EV_USE_INOTIFY
3050e3a38431SPaul Bohm infy_fork (EV_A);
3051e3a38431SPaul Bohm #endif
3052e3a38431SPaul Bohm
3053*93823e6cSPaul Bohm #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
3054*93823e6cSPaul Bohm if (ev_is_active (&pipe_w) && postfork != 2)
3055e3a38431SPaul Bohm {
3056*93823e6cSPaul Bohm /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
3057e3a38431SPaul Bohm
3058e3a38431SPaul Bohm ev_ref (EV_A);
3059e3a38431SPaul Bohm ev_io_stop (EV_A_ &pipe_w);
3060e3a38431SPaul Bohm
3061e3a38431SPaul Bohm if (evpipe [0] >= 0)
3062e3a38431SPaul Bohm EV_WIN32_CLOSE_FD (evpipe [0]);
3063e3a38431SPaul Bohm
3064e3a38431SPaul Bohm evpipe_init (EV_A);
3065*93823e6cSPaul Bohm /* iterate over everything, in case we missed something before */
3066*93823e6cSPaul Bohm ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3067e3a38431SPaul Bohm }
3068*93823e6cSPaul Bohm #endif
3069e3a38431SPaul Bohm
3070e3a38431SPaul Bohm postfork = 0;
3071e3a38431SPaul Bohm }
3072e3a38431SPaul Bohm
3073e3a38431SPaul Bohm #if EV_MULTIPLICITY
3074e3a38431SPaul Bohm
3075*93823e6cSPaul Bohm struct ev_loop * ecb_cold
ev_loop_new(unsigned int flags)3076*93823e6cSPaul Bohm ev_loop_new (unsigned int flags) EV_THROW
3077e3a38431SPaul Bohm {
3078e3a38431SPaul Bohm EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
3079e3a38431SPaul Bohm
3080e3a38431SPaul Bohm memset (EV_A, 0, sizeof (struct ev_loop));
3081e3a38431SPaul Bohm loop_init (EV_A_ flags);
3082e3a38431SPaul Bohm
3083e3a38431SPaul Bohm if (ev_backend (EV_A))
3084e3a38431SPaul Bohm return EV_A;
3085e3a38431SPaul Bohm
3086e3a38431SPaul Bohm ev_free (EV_A);
3087e3a38431SPaul Bohm return 0;
3088e3a38431SPaul Bohm }
3089e3a38431SPaul Bohm
3090e3a38431SPaul Bohm #endif /* multiplicity */
3091e3a38431SPaul Bohm
3092e3a38431SPaul Bohm #if EV_VERIFY
3093*93823e6cSPaul Bohm static void noinline ecb_cold
verify_watcher(EV_P_ W w)3094e3a38431SPaul Bohm verify_watcher (EV_P_ W w)
3095e3a38431SPaul Bohm {
3096e3a38431SPaul Bohm assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
3097e3a38431SPaul Bohm
3098e3a38431SPaul Bohm if (w->pending)
3099e3a38431SPaul Bohm assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
3100e3a38431SPaul Bohm }
3101e3a38431SPaul Bohm
3102*93823e6cSPaul Bohm static void noinline ecb_cold
verify_heap(EV_P_ ANHE * heap,int N)3103e3a38431SPaul Bohm verify_heap (EV_P_ ANHE *heap, int N)
3104e3a38431SPaul Bohm {
3105e3a38431SPaul Bohm int i;
3106e3a38431SPaul Bohm
3107e3a38431SPaul Bohm for (i = HEAP0; i < N + HEAP0; ++i)
3108e3a38431SPaul Bohm {
3109e3a38431SPaul Bohm assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
3110e3a38431SPaul Bohm assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
3111e3a38431SPaul Bohm assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
3112e3a38431SPaul Bohm
3113e3a38431SPaul Bohm verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
3114e3a38431SPaul Bohm }
3115e3a38431SPaul Bohm }
3116e3a38431SPaul Bohm
3117*93823e6cSPaul Bohm static void noinline ecb_cold
array_verify(EV_P_ W * ws,int cnt)3118e3a38431SPaul Bohm array_verify (EV_P_ W *ws, int cnt)
3119e3a38431SPaul Bohm {
3120e3a38431SPaul Bohm while (cnt--)
3121e3a38431SPaul Bohm {
3122e3a38431SPaul Bohm assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
3123e3a38431SPaul Bohm verify_watcher (EV_A_ ws [cnt]);
3124e3a38431SPaul Bohm }
3125e3a38431SPaul Bohm }
3126e3a38431SPaul Bohm #endif
3127e3a38431SPaul Bohm
3128e3a38431SPaul Bohm #if EV_FEATURE_API
3129*93823e6cSPaul Bohm void ecb_cold
ev_verify(EV_P)3130*93823e6cSPaul Bohm ev_verify (EV_P) EV_THROW
3131e3a38431SPaul Bohm {
3132e3a38431SPaul Bohm #if EV_VERIFY
3133e3a38431SPaul Bohm int i;
3134*93823e6cSPaul Bohm WL w, w2;
3135e3a38431SPaul Bohm
3136e3a38431SPaul Bohm assert (activecnt >= -1);
3137e3a38431SPaul Bohm
3138e3a38431SPaul Bohm assert (fdchangemax >= fdchangecnt);
3139e3a38431SPaul Bohm for (i = 0; i < fdchangecnt; ++i)
3140e3a38431SPaul Bohm assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
3141e3a38431SPaul Bohm
3142e3a38431SPaul Bohm assert (anfdmax >= 0);
3143e3a38431SPaul Bohm for (i = 0; i < anfdmax; ++i)
3144*93823e6cSPaul Bohm {
3145*93823e6cSPaul Bohm int j = 0;
3146*93823e6cSPaul Bohm
3147*93823e6cSPaul Bohm for (w = w2 = anfds [i].head; w; w = w->next)
3148e3a38431SPaul Bohm {
3149e3a38431SPaul Bohm verify_watcher (EV_A_ (W)w);
3150*93823e6cSPaul Bohm
3151*93823e6cSPaul Bohm if (j++ & 1)
3152*93823e6cSPaul Bohm {
3153*93823e6cSPaul Bohm assert (("libev: io watcher list contains a loop", w != w2));
3154*93823e6cSPaul Bohm w2 = w2->next;
3155*93823e6cSPaul Bohm }
3156*93823e6cSPaul Bohm
3157e3a38431SPaul Bohm assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
3158e3a38431SPaul Bohm assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
3159e3a38431SPaul Bohm }
3160*93823e6cSPaul Bohm }
3161e3a38431SPaul Bohm
3162e3a38431SPaul Bohm assert (timermax >= timercnt);
3163e3a38431SPaul Bohm verify_heap (EV_A_ timers, timercnt);
3164e3a38431SPaul Bohm
3165e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3166e3a38431SPaul Bohm assert (periodicmax >= periodiccnt);
3167e3a38431SPaul Bohm verify_heap (EV_A_ periodics, periodiccnt);
3168e3a38431SPaul Bohm #endif
3169e3a38431SPaul Bohm
3170e3a38431SPaul Bohm for (i = NUMPRI; i--; )
3171e3a38431SPaul Bohm {
3172e3a38431SPaul Bohm assert (pendingmax [i] >= pendingcnt [i]);
3173e3a38431SPaul Bohm #if EV_IDLE_ENABLE
3174e3a38431SPaul Bohm assert (idleall >= 0);
3175e3a38431SPaul Bohm assert (idlemax [i] >= idlecnt [i]);
3176e3a38431SPaul Bohm array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
3177e3a38431SPaul Bohm #endif
3178e3a38431SPaul Bohm }
3179e3a38431SPaul Bohm
3180e3a38431SPaul Bohm #if EV_FORK_ENABLE
3181e3a38431SPaul Bohm assert (forkmax >= forkcnt);
3182e3a38431SPaul Bohm array_verify (EV_A_ (W *)forks, forkcnt);
3183e3a38431SPaul Bohm #endif
3184e3a38431SPaul Bohm
3185e3a38431SPaul Bohm #if EV_CLEANUP_ENABLE
3186e3a38431SPaul Bohm assert (cleanupmax >= cleanupcnt);
3187e3a38431SPaul Bohm array_verify (EV_A_ (W *)cleanups, cleanupcnt);
3188e3a38431SPaul Bohm #endif
3189e3a38431SPaul Bohm
3190e3a38431SPaul Bohm #if EV_ASYNC_ENABLE
3191e3a38431SPaul Bohm assert (asyncmax >= asynccnt);
3192e3a38431SPaul Bohm array_verify (EV_A_ (W *)asyncs, asynccnt);
3193e3a38431SPaul Bohm #endif
3194e3a38431SPaul Bohm
3195e3a38431SPaul Bohm #if EV_PREPARE_ENABLE
3196e3a38431SPaul Bohm assert (preparemax >= preparecnt);
3197e3a38431SPaul Bohm array_verify (EV_A_ (W *)prepares, preparecnt);
3198e3a38431SPaul Bohm #endif
3199e3a38431SPaul Bohm
3200e3a38431SPaul Bohm #if EV_CHECK_ENABLE
3201e3a38431SPaul Bohm assert (checkmax >= checkcnt);
3202e3a38431SPaul Bohm array_verify (EV_A_ (W *)checks, checkcnt);
3203e3a38431SPaul Bohm #endif
3204e3a38431SPaul Bohm
3205e3a38431SPaul Bohm # if 0
3206e3a38431SPaul Bohm #if EV_CHILD_ENABLE
3207e3a38431SPaul Bohm for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
3208e3a38431SPaul Bohm for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
3209e3a38431SPaul Bohm #endif
3210e3a38431SPaul Bohm # endif
3211e3a38431SPaul Bohm #endif
3212e3a38431SPaul Bohm }
3213e3a38431SPaul Bohm #endif
3214e3a38431SPaul Bohm
3215e3a38431SPaul Bohm #if EV_MULTIPLICITY
3216*93823e6cSPaul Bohm struct ev_loop * ecb_cold
3217e3a38431SPaul Bohm #else
3218e3a38431SPaul Bohm int
3219e3a38431SPaul Bohm #endif
ev_default_loop(unsigned int flags)3220*93823e6cSPaul Bohm ev_default_loop (unsigned int flags) EV_THROW
3221e3a38431SPaul Bohm {
3222e3a38431SPaul Bohm if (!ev_default_loop_ptr)
3223e3a38431SPaul Bohm {
3224e3a38431SPaul Bohm #if EV_MULTIPLICITY
3225e3a38431SPaul Bohm EV_P = ev_default_loop_ptr = &default_loop_struct;
3226e3a38431SPaul Bohm #else
3227e3a38431SPaul Bohm ev_default_loop_ptr = 1;
3228e3a38431SPaul Bohm #endif
3229e3a38431SPaul Bohm
3230e3a38431SPaul Bohm loop_init (EV_A_ flags);
3231e3a38431SPaul Bohm
3232e3a38431SPaul Bohm if (ev_backend (EV_A))
3233e3a38431SPaul Bohm {
3234e3a38431SPaul Bohm #if EV_CHILD_ENABLE
3235e3a38431SPaul Bohm ev_signal_init (&childev, childcb, SIGCHLD);
3236e3a38431SPaul Bohm ev_set_priority (&childev, EV_MAXPRI);
3237e3a38431SPaul Bohm ev_signal_start (EV_A_ &childev);
3238e3a38431SPaul Bohm ev_unref (EV_A); /* child watcher should not keep loop alive */
3239e3a38431SPaul Bohm #endif
3240e3a38431SPaul Bohm }
3241e3a38431SPaul Bohm else
3242e3a38431SPaul Bohm ev_default_loop_ptr = 0;
3243e3a38431SPaul Bohm }
3244e3a38431SPaul Bohm
3245e3a38431SPaul Bohm return ev_default_loop_ptr;
3246e3a38431SPaul Bohm }
3247e3a38431SPaul Bohm
3248e3a38431SPaul Bohm void
ev_loop_fork(EV_P)3249*93823e6cSPaul Bohm ev_loop_fork (EV_P) EV_THROW
3250e3a38431SPaul Bohm {
3251*93823e6cSPaul Bohm postfork = 1;
3252e3a38431SPaul Bohm }
3253e3a38431SPaul Bohm
3254e3a38431SPaul Bohm /*****************************************************************************/
3255e3a38431SPaul Bohm
3256e3a38431SPaul Bohm void
ev_invoke(EV_P_ void * w,int revents)3257e3a38431SPaul Bohm ev_invoke (EV_P_ void *w, int revents)
3258e3a38431SPaul Bohm {
3259e3a38431SPaul Bohm EV_CB_INVOKE ((W)w, revents);
3260e3a38431SPaul Bohm }
3261e3a38431SPaul Bohm
3262e3a38431SPaul Bohm unsigned int
ev_pending_count(EV_P)3263*93823e6cSPaul Bohm ev_pending_count (EV_P) EV_THROW
3264e3a38431SPaul Bohm {
3265e3a38431SPaul Bohm int pri;
3266e3a38431SPaul Bohm unsigned int count = 0;
3267e3a38431SPaul Bohm
3268e3a38431SPaul Bohm for (pri = NUMPRI; pri--; )
3269e3a38431SPaul Bohm count += pendingcnt [pri];
3270e3a38431SPaul Bohm
3271e3a38431SPaul Bohm return count;
3272e3a38431SPaul Bohm }
3273e3a38431SPaul Bohm
3274e3a38431SPaul Bohm void noinline
ev_invoke_pending(EV_P)3275e3a38431SPaul Bohm ev_invoke_pending (EV_P)
3276e3a38431SPaul Bohm {
3277*93823e6cSPaul Bohm pendingpri = NUMPRI;
3278e3a38431SPaul Bohm
3279*93823e6cSPaul Bohm while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
3280e3a38431SPaul Bohm {
3281*93823e6cSPaul Bohm --pendingpri;
3282*93823e6cSPaul Bohm
3283*93823e6cSPaul Bohm while (pendingcnt [pendingpri])
3284*93823e6cSPaul Bohm {
3285*93823e6cSPaul Bohm ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
3286e3a38431SPaul Bohm
3287e3a38431SPaul Bohm p->w->pending = 0;
3288e3a38431SPaul Bohm EV_CB_INVOKE (p->w, p->events);
3289e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3290e3a38431SPaul Bohm }
3291e3a38431SPaul Bohm }
3292*93823e6cSPaul Bohm }
3293e3a38431SPaul Bohm
3294e3a38431SPaul Bohm #if EV_IDLE_ENABLE
3295e3a38431SPaul Bohm /* make idle watchers pending. this handles the "call-idle */
3296e3a38431SPaul Bohm /* only when higher priorities are idle" logic */
3297e3a38431SPaul Bohm inline_size void
idle_reify(EV_P)3298e3a38431SPaul Bohm idle_reify (EV_P)
3299e3a38431SPaul Bohm {
3300e3a38431SPaul Bohm if (expect_false (idleall))
3301e3a38431SPaul Bohm {
3302e3a38431SPaul Bohm int pri;
3303e3a38431SPaul Bohm
3304e3a38431SPaul Bohm for (pri = NUMPRI; pri--; )
3305e3a38431SPaul Bohm {
3306e3a38431SPaul Bohm if (pendingcnt [pri])
3307e3a38431SPaul Bohm break;
3308e3a38431SPaul Bohm
3309e3a38431SPaul Bohm if (idlecnt [pri])
3310e3a38431SPaul Bohm {
3311e3a38431SPaul Bohm queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
3312e3a38431SPaul Bohm break;
3313e3a38431SPaul Bohm }
3314e3a38431SPaul Bohm }
3315e3a38431SPaul Bohm }
3316e3a38431SPaul Bohm }
3317e3a38431SPaul Bohm #endif
3318e3a38431SPaul Bohm
3319e3a38431SPaul Bohm /* make timers pending */
3320e3a38431SPaul Bohm inline_size void
timers_reify(EV_P)3321e3a38431SPaul Bohm timers_reify (EV_P)
3322e3a38431SPaul Bohm {
3323e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3324e3a38431SPaul Bohm
3325e3a38431SPaul Bohm if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
3326e3a38431SPaul Bohm {
3327e3a38431SPaul Bohm do
3328e3a38431SPaul Bohm {
3329e3a38431SPaul Bohm ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
3330e3a38431SPaul Bohm
3331e3a38431SPaul Bohm /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
3332e3a38431SPaul Bohm
3333e3a38431SPaul Bohm /* first reschedule or stop timer */
3334e3a38431SPaul Bohm if (w->repeat)
3335e3a38431SPaul Bohm {
3336e3a38431SPaul Bohm ev_at (w) += w->repeat;
3337e3a38431SPaul Bohm if (ev_at (w) < mn_now)
3338e3a38431SPaul Bohm ev_at (w) = mn_now;
3339e3a38431SPaul Bohm
3340e3a38431SPaul Bohm assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
3341e3a38431SPaul Bohm
3342e3a38431SPaul Bohm ANHE_at_cache (timers [HEAP0]);
3343e3a38431SPaul Bohm downheap (timers, timercnt, HEAP0);
3344e3a38431SPaul Bohm }
3345e3a38431SPaul Bohm else
3346e3a38431SPaul Bohm ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
3347e3a38431SPaul Bohm
3348e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3349e3a38431SPaul Bohm feed_reverse (EV_A_ (W)w);
3350e3a38431SPaul Bohm }
3351e3a38431SPaul Bohm while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
3352e3a38431SPaul Bohm
3353e3a38431SPaul Bohm feed_reverse_done (EV_A_ EV_TIMER);
3354e3a38431SPaul Bohm }
3355e3a38431SPaul Bohm }
3356e3a38431SPaul Bohm
3357e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3358e3a38431SPaul Bohm
3359*93823e6cSPaul Bohm static void noinline
periodic_recalc(EV_P_ ev_periodic * w)3360e3a38431SPaul Bohm periodic_recalc (EV_P_ ev_periodic *w)
3361e3a38431SPaul Bohm {
3362*93823e6cSPaul Bohm ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL;
3363*93823e6cSPaul Bohm ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval);
3364*93823e6cSPaul Bohm
3365*93823e6cSPaul Bohm /* the above almost always errs on the low side */
3366*93823e6cSPaul Bohm while (at <= ev_rt_now)
3367*93823e6cSPaul Bohm {
3368*93823e6cSPaul Bohm ev_tstamp nat = at + w->interval;
3369*93823e6cSPaul Bohm
3370*93823e6cSPaul Bohm /* when resolution fails us, we use ev_rt_now */
3371*93823e6cSPaul Bohm if (expect_false (nat == at))
3372*93823e6cSPaul Bohm {
3373*93823e6cSPaul Bohm at = ev_rt_now;
3374*93823e6cSPaul Bohm break;
3375*93823e6cSPaul Bohm }
3376*93823e6cSPaul Bohm
3377*93823e6cSPaul Bohm at = nat;
3378*93823e6cSPaul Bohm }
3379*93823e6cSPaul Bohm
3380*93823e6cSPaul Bohm ev_at (w) = at;
3381e3a38431SPaul Bohm }
3382e3a38431SPaul Bohm
3383e3a38431SPaul Bohm /* make periodics pending */
3384e3a38431SPaul Bohm inline_size void
periodics_reify(EV_P)3385e3a38431SPaul Bohm periodics_reify (EV_P)
3386e3a38431SPaul Bohm {
3387e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3388e3a38431SPaul Bohm
3389e3a38431SPaul Bohm while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
3390e3a38431SPaul Bohm {
3391e3a38431SPaul Bohm do
3392e3a38431SPaul Bohm {
3393e3a38431SPaul Bohm ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
3394e3a38431SPaul Bohm
3395e3a38431SPaul Bohm /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
3396e3a38431SPaul Bohm
3397e3a38431SPaul Bohm /* first reschedule or stop timer */
3398e3a38431SPaul Bohm if (w->reschedule_cb)
3399e3a38431SPaul Bohm {
3400e3a38431SPaul Bohm ev_at (w) = w->reschedule_cb (w, ev_rt_now);
3401e3a38431SPaul Bohm
3402e3a38431SPaul Bohm assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
3403e3a38431SPaul Bohm
3404e3a38431SPaul Bohm ANHE_at_cache (periodics [HEAP0]);
3405e3a38431SPaul Bohm downheap (periodics, periodiccnt, HEAP0);
3406e3a38431SPaul Bohm }
3407e3a38431SPaul Bohm else if (w->interval)
3408e3a38431SPaul Bohm {
3409e3a38431SPaul Bohm periodic_recalc (EV_A_ w);
3410e3a38431SPaul Bohm ANHE_at_cache (periodics [HEAP0]);
3411e3a38431SPaul Bohm downheap (periodics, periodiccnt, HEAP0);
3412e3a38431SPaul Bohm }
3413e3a38431SPaul Bohm else
3414e3a38431SPaul Bohm ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
3415e3a38431SPaul Bohm
3416e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3417e3a38431SPaul Bohm feed_reverse (EV_A_ (W)w);
3418e3a38431SPaul Bohm }
3419e3a38431SPaul Bohm while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now);
3420e3a38431SPaul Bohm
3421e3a38431SPaul Bohm feed_reverse_done (EV_A_ EV_PERIODIC);
3422e3a38431SPaul Bohm }
3423e3a38431SPaul Bohm }
3424e3a38431SPaul Bohm
3425e3a38431SPaul Bohm /* simply recalculate all periodics */
3426e3a38431SPaul Bohm /* TODO: maybe ensure that at least one event happens when jumping forward? */
3427*93823e6cSPaul Bohm static void noinline ecb_cold
periodics_reschedule(EV_P)3428e3a38431SPaul Bohm periodics_reschedule (EV_P)
3429e3a38431SPaul Bohm {
3430e3a38431SPaul Bohm int i;
3431e3a38431SPaul Bohm
3432e3a38431SPaul Bohm /* adjust periodics after time jump */
3433e3a38431SPaul Bohm for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
3434e3a38431SPaul Bohm {
3435e3a38431SPaul Bohm ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
3436e3a38431SPaul Bohm
3437e3a38431SPaul Bohm if (w->reschedule_cb)
3438e3a38431SPaul Bohm ev_at (w) = w->reschedule_cb (w, ev_rt_now);
3439e3a38431SPaul Bohm else if (w->interval)
3440e3a38431SPaul Bohm periodic_recalc (EV_A_ w);
3441e3a38431SPaul Bohm
3442e3a38431SPaul Bohm ANHE_at_cache (periodics [i]);
3443e3a38431SPaul Bohm }
3444e3a38431SPaul Bohm
3445e3a38431SPaul Bohm reheap (periodics, periodiccnt);
3446e3a38431SPaul Bohm }
3447e3a38431SPaul Bohm #endif
3448e3a38431SPaul Bohm
3449e3a38431SPaul Bohm /* adjust all timers by a given offset */
3450*93823e6cSPaul Bohm static void noinline ecb_cold
timers_reschedule(EV_P_ ev_tstamp adjust)3451e3a38431SPaul Bohm timers_reschedule (EV_P_ ev_tstamp adjust)
3452e3a38431SPaul Bohm {
3453e3a38431SPaul Bohm int i;
3454e3a38431SPaul Bohm
3455e3a38431SPaul Bohm for (i = 0; i < timercnt; ++i)
3456e3a38431SPaul Bohm {
3457e3a38431SPaul Bohm ANHE *he = timers + i + HEAP0;
3458e3a38431SPaul Bohm ANHE_w (*he)->at += adjust;
3459e3a38431SPaul Bohm ANHE_at_cache (*he);
3460e3a38431SPaul Bohm }
3461e3a38431SPaul Bohm }
3462e3a38431SPaul Bohm
3463e3a38431SPaul Bohm /* fetch new monotonic and realtime times from the kernel */
3464e3a38431SPaul Bohm /* also detect if there was a timejump, and act accordingly */
3465e3a38431SPaul Bohm inline_speed void
time_update(EV_P_ ev_tstamp max_block)3466e3a38431SPaul Bohm time_update (EV_P_ ev_tstamp max_block)
3467e3a38431SPaul Bohm {
3468e3a38431SPaul Bohm #if EV_USE_MONOTONIC
3469e3a38431SPaul Bohm if (expect_true (have_monotonic))
3470e3a38431SPaul Bohm {
3471e3a38431SPaul Bohm int i;
3472e3a38431SPaul Bohm ev_tstamp odiff = rtmn_diff;
3473e3a38431SPaul Bohm
3474e3a38431SPaul Bohm mn_now = get_clock ();
3475e3a38431SPaul Bohm
3476e3a38431SPaul Bohm /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
3477e3a38431SPaul Bohm /* interpolate in the meantime */
3478e3a38431SPaul Bohm if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
3479e3a38431SPaul Bohm {
3480e3a38431SPaul Bohm ev_rt_now = rtmn_diff + mn_now;
3481e3a38431SPaul Bohm return;
3482e3a38431SPaul Bohm }
3483e3a38431SPaul Bohm
3484e3a38431SPaul Bohm now_floor = mn_now;
3485e3a38431SPaul Bohm ev_rt_now = ev_time ();
3486e3a38431SPaul Bohm
3487e3a38431SPaul Bohm /* loop a few times, before making important decisions.
3488e3a38431SPaul Bohm * on the choice of "4": one iteration isn't enough,
3489e3a38431SPaul Bohm * in case we get preempted during the calls to
3490e3a38431SPaul Bohm * ev_time and get_clock. a second call is almost guaranteed
3491e3a38431SPaul Bohm * to succeed in that case, though. and looping a few more times
3492e3a38431SPaul Bohm * doesn't hurt either as we only do this on time-jumps or
3493e3a38431SPaul Bohm * in the unlikely event of having been preempted here.
3494e3a38431SPaul Bohm */
3495e3a38431SPaul Bohm for (i = 4; --i; )
3496e3a38431SPaul Bohm {
3497*93823e6cSPaul Bohm ev_tstamp diff;
3498e3a38431SPaul Bohm rtmn_diff = ev_rt_now - mn_now;
3499e3a38431SPaul Bohm
3500*93823e6cSPaul Bohm diff = odiff - rtmn_diff;
3501*93823e6cSPaul Bohm
3502*93823e6cSPaul Bohm if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP))
3503e3a38431SPaul Bohm return; /* all is well */
3504e3a38431SPaul Bohm
3505e3a38431SPaul Bohm ev_rt_now = ev_time ();
3506e3a38431SPaul Bohm mn_now = get_clock ();
3507e3a38431SPaul Bohm now_floor = mn_now;
3508e3a38431SPaul Bohm }
3509e3a38431SPaul Bohm
3510e3a38431SPaul Bohm /* no timer adjustment, as the monotonic clock doesn't jump */
3511e3a38431SPaul Bohm /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
3512e3a38431SPaul Bohm # if EV_PERIODIC_ENABLE
3513e3a38431SPaul Bohm periodics_reschedule (EV_A);
3514e3a38431SPaul Bohm # endif
3515e3a38431SPaul Bohm }
3516e3a38431SPaul Bohm else
3517e3a38431SPaul Bohm #endif
3518e3a38431SPaul Bohm {
3519e3a38431SPaul Bohm ev_rt_now = ev_time ();
3520e3a38431SPaul Bohm
3521e3a38431SPaul Bohm if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
3522e3a38431SPaul Bohm {
3523e3a38431SPaul Bohm /* adjust timers. this is easy, as the offset is the same for all of them */
3524e3a38431SPaul Bohm timers_reschedule (EV_A_ ev_rt_now - mn_now);
3525e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3526e3a38431SPaul Bohm periodics_reschedule (EV_A);
3527e3a38431SPaul Bohm #endif
3528e3a38431SPaul Bohm }
3529e3a38431SPaul Bohm
3530e3a38431SPaul Bohm mn_now = ev_rt_now;
3531e3a38431SPaul Bohm }
3532e3a38431SPaul Bohm }
3533e3a38431SPaul Bohm
3534*93823e6cSPaul Bohm int
ev_run(EV_P_ int flags)3535e3a38431SPaul Bohm ev_run (EV_P_ int flags)
3536e3a38431SPaul Bohm {
3537e3a38431SPaul Bohm #if EV_FEATURE_API
3538e3a38431SPaul Bohm ++loop_depth;
3539e3a38431SPaul Bohm #endif
3540e3a38431SPaul Bohm
3541e3a38431SPaul Bohm assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
3542e3a38431SPaul Bohm
3543e3a38431SPaul Bohm loop_done = EVBREAK_CANCEL;
3544e3a38431SPaul Bohm
3545e3a38431SPaul Bohm EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
3546e3a38431SPaul Bohm
3547e3a38431SPaul Bohm do
3548e3a38431SPaul Bohm {
3549e3a38431SPaul Bohm #if EV_VERIFY >= 2
3550e3a38431SPaul Bohm ev_verify (EV_A);
3551e3a38431SPaul Bohm #endif
3552e3a38431SPaul Bohm
3553e3a38431SPaul Bohm #ifndef _WIN32
3554e3a38431SPaul Bohm if (expect_false (curpid)) /* penalise the forking check even more */
3555e3a38431SPaul Bohm if (expect_false (getpid () != curpid))
3556e3a38431SPaul Bohm {
3557e3a38431SPaul Bohm curpid = getpid ();
3558e3a38431SPaul Bohm postfork = 1;
3559e3a38431SPaul Bohm }
3560e3a38431SPaul Bohm #endif
3561e3a38431SPaul Bohm
3562e3a38431SPaul Bohm #if EV_FORK_ENABLE
3563e3a38431SPaul Bohm /* we might have forked, so queue fork handlers */
3564e3a38431SPaul Bohm if (expect_false (postfork))
3565e3a38431SPaul Bohm if (forkcnt)
3566e3a38431SPaul Bohm {
3567e3a38431SPaul Bohm queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
3568e3a38431SPaul Bohm EV_INVOKE_PENDING;
3569e3a38431SPaul Bohm }
3570e3a38431SPaul Bohm #endif
3571e3a38431SPaul Bohm
3572e3a38431SPaul Bohm #if EV_PREPARE_ENABLE
3573e3a38431SPaul Bohm /* queue prepare watchers (and execute them) */
3574e3a38431SPaul Bohm if (expect_false (preparecnt))
3575e3a38431SPaul Bohm {
3576e3a38431SPaul Bohm queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
3577e3a38431SPaul Bohm EV_INVOKE_PENDING;
3578e3a38431SPaul Bohm }
3579e3a38431SPaul Bohm #endif
3580e3a38431SPaul Bohm
3581e3a38431SPaul Bohm if (expect_false (loop_done))
3582e3a38431SPaul Bohm break;
3583e3a38431SPaul Bohm
3584e3a38431SPaul Bohm /* we might have forked, so reify kernel state if necessary */
3585e3a38431SPaul Bohm if (expect_false (postfork))
3586e3a38431SPaul Bohm loop_fork (EV_A);
3587e3a38431SPaul Bohm
3588e3a38431SPaul Bohm /* update fd-related kernel structures */
3589e3a38431SPaul Bohm fd_reify (EV_A);
3590e3a38431SPaul Bohm
3591e3a38431SPaul Bohm /* calculate blocking time */
3592e3a38431SPaul Bohm {
3593e3a38431SPaul Bohm ev_tstamp waittime = 0.;
3594e3a38431SPaul Bohm ev_tstamp sleeptime = 0.;
3595e3a38431SPaul Bohm
3596e3a38431SPaul Bohm /* remember old timestamp for io_blocktime calculation */
3597e3a38431SPaul Bohm ev_tstamp prev_mn_now = mn_now;
3598e3a38431SPaul Bohm
3599e3a38431SPaul Bohm /* update time to cancel out callback processing overhead */
3600e3a38431SPaul Bohm time_update (EV_A_ 1e100);
3601e3a38431SPaul Bohm
3602*93823e6cSPaul Bohm /* from now on, we want a pipe-wake-up */
3603*93823e6cSPaul Bohm pipe_write_wanted = 1;
3604*93823e6cSPaul Bohm
3605*93823e6cSPaul Bohm ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
3606*93823e6cSPaul Bohm
3607*93823e6cSPaul Bohm if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
3608e3a38431SPaul Bohm {
3609e3a38431SPaul Bohm waittime = MAX_BLOCKTIME;
3610e3a38431SPaul Bohm
3611e3a38431SPaul Bohm if (timercnt)
3612e3a38431SPaul Bohm {
3613*93823e6cSPaul Bohm ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
3614e3a38431SPaul Bohm if (waittime > to) waittime = to;
3615e3a38431SPaul Bohm }
3616e3a38431SPaul Bohm
3617e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3618e3a38431SPaul Bohm if (periodiccnt)
3619e3a38431SPaul Bohm {
3620*93823e6cSPaul Bohm ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
3621e3a38431SPaul Bohm if (waittime > to) waittime = to;
3622e3a38431SPaul Bohm }
3623e3a38431SPaul Bohm #endif
3624e3a38431SPaul Bohm
3625e3a38431SPaul Bohm /* don't let timeouts decrease the waittime below timeout_blocktime */
3626e3a38431SPaul Bohm if (expect_false (waittime < timeout_blocktime))
3627e3a38431SPaul Bohm waittime = timeout_blocktime;
3628e3a38431SPaul Bohm
3629*93823e6cSPaul Bohm /* at this point, we NEED to wait, so we have to ensure */
3630*93823e6cSPaul Bohm /* to pass a minimum nonzero value to the backend */
3631*93823e6cSPaul Bohm if (expect_false (waittime < backend_mintime))
3632*93823e6cSPaul Bohm waittime = backend_mintime;
3633*93823e6cSPaul Bohm
3634e3a38431SPaul Bohm /* extra check because io_blocktime is commonly 0 */
3635e3a38431SPaul Bohm if (expect_false (io_blocktime))
3636e3a38431SPaul Bohm {
3637e3a38431SPaul Bohm sleeptime = io_blocktime - (mn_now - prev_mn_now);
3638e3a38431SPaul Bohm
3639*93823e6cSPaul Bohm if (sleeptime > waittime - backend_mintime)
3640*93823e6cSPaul Bohm sleeptime = waittime - backend_mintime;
3641e3a38431SPaul Bohm
3642e3a38431SPaul Bohm if (expect_true (sleeptime > 0.))
3643e3a38431SPaul Bohm {
3644e3a38431SPaul Bohm ev_sleep (sleeptime);
3645e3a38431SPaul Bohm waittime -= sleeptime;
3646e3a38431SPaul Bohm }
3647e3a38431SPaul Bohm }
3648e3a38431SPaul Bohm }
3649e3a38431SPaul Bohm
3650e3a38431SPaul Bohm #if EV_FEATURE_API
3651e3a38431SPaul Bohm ++loop_count;
3652e3a38431SPaul Bohm #endif
3653e3a38431SPaul Bohm assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
3654e3a38431SPaul Bohm backend_poll (EV_A_ waittime);
3655e3a38431SPaul Bohm assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3656e3a38431SPaul Bohm
3657*93823e6cSPaul Bohm pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3658*93823e6cSPaul Bohm
3659*93823e6cSPaul Bohm ECB_MEMORY_FENCE_ACQUIRE;
3660*93823e6cSPaul Bohm if (pipe_write_skipped)
3661*93823e6cSPaul Bohm {
3662*93823e6cSPaul Bohm assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3663*93823e6cSPaul Bohm ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3664*93823e6cSPaul Bohm }
3665*93823e6cSPaul Bohm
3666*93823e6cSPaul Bohm
3667e3a38431SPaul Bohm /* update ev_rt_now, do magic */
3668e3a38431SPaul Bohm time_update (EV_A_ waittime + sleeptime);
3669e3a38431SPaul Bohm }
3670e3a38431SPaul Bohm
3671e3a38431SPaul Bohm /* queue pending timers and reschedule them */
3672e3a38431SPaul Bohm timers_reify (EV_A); /* relative timers called last */
3673e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3674e3a38431SPaul Bohm periodics_reify (EV_A); /* absolute timers called first */
3675e3a38431SPaul Bohm #endif
3676e3a38431SPaul Bohm
3677e3a38431SPaul Bohm #if EV_IDLE_ENABLE
3678e3a38431SPaul Bohm /* queue idle watchers unless other events are pending */
3679e3a38431SPaul Bohm idle_reify (EV_A);
3680e3a38431SPaul Bohm #endif
3681e3a38431SPaul Bohm
3682e3a38431SPaul Bohm #if EV_CHECK_ENABLE
3683e3a38431SPaul Bohm /* queue check watchers, to be executed first */
3684e3a38431SPaul Bohm if (expect_false (checkcnt))
3685e3a38431SPaul Bohm queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
3686e3a38431SPaul Bohm #endif
3687e3a38431SPaul Bohm
3688e3a38431SPaul Bohm EV_INVOKE_PENDING;
3689e3a38431SPaul Bohm }
3690e3a38431SPaul Bohm while (expect_true (
3691e3a38431SPaul Bohm activecnt
3692e3a38431SPaul Bohm && !loop_done
3693e3a38431SPaul Bohm && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
3694e3a38431SPaul Bohm ));
3695e3a38431SPaul Bohm
3696e3a38431SPaul Bohm if (loop_done == EVBREAK_ONE)
3697e3a38431SPaul Bohm loop_done = EVBREAK_CANCEL;
3698e3a38431SPaul Bohm
3699e3a38431SPaul Bohm #if EV_FEATURE_API
3700e3a38431SPaul Bohm --loop_depth;
3701e3a38431SPaul Bohm #endif
3702*93823e6cSPaul Bohm
3703*93823e6cSPaul Bohm return activecnt;
3704e3a38431SPaul Bohm }
3705e3a38431SPaul Bohm
3706e3a38431SPaul Bohm void
ev_break(EV_P_ int how)3707*93823e6cSPaul Bohm ev_break (EV_P_ int how) EV_THROW
3708e3a38431SPaul Bohm {
3709e3a38431SPaul Bohm loop_done = how;
3710e3a38431SPaul Bohm }
3711e3a38431SPaul Bohm
3712e3a38431SPaul Bohm void
ev_ref(EV_P)3713*93823e6cSPaul Bohm ev_ref (EV_P) EV_THROW
3714e3a38431SPaul Bohm {
3715e3a38431SPaul Bohm ++activecnt;
3716e3a38431SPaul Bohm }
3717e3a38431SPaul Bohm
3718e3a38431SPaul Bohm void
ev_unref(EV_P)3719*93823e6cSPaul Bohm ev_unref (EV_P) EV_THROW
3720e3a38431SPaul Bohm {
3721e3a38431SPaul Bohm --activecnt;
3722e3a38431SPaul Bohm }
3723e3a38431SPaul Bohm
3724e3a38431SPaul Bohm void
ev_now_update(EV_P)3725*93823e6cSPaul Bohm ev_now_update (EV_P) EV_THROW
3726e3a38431SPaul Bohm {
3727e3a38431SPaul Bohm time_update (EV_A_ 1e100);
3728e3a38431SPaul Bohm }
3729e3a38431SPaul Bohm
3730e3a38431SPaul Bohm void
ev_suspend(EV_P)3731*93823e6cSPaul Bohm ev_suspend (EV_P) EV_THROW
3732e3a38431SPaul Bohm {
3733e3a38431SPaul Bohm ev_now_update (EV_A);
3734e3a38431SPaul Bohm }
3735e3a38431SPaul Bohm
3736e3a38431SPaul Bohm void
ev_resume(EV_P)3737*93823e6cSPaul Bohm ev_resume (EV_P) EV_THROW
3738e3a38431SPaul Bohm {
3739e3a38431SPaul Bohm ev_tstamp mn_prev = mn_now;
3740e3a38431SPaul Bohm
3741e3a38431SPaul Bohm ev_now_update (EV_A);
3742e3a38431SPaul Bohm timers_reschedule (EV_A_ mn_now - mn_prev);
3743e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3744e3a38431SPaul Bohm /* TODO: really do this? */
3745e3a38431SPaul Bohm periodics_reschedule (EV_A);
3746e3a38431SPaul Bohm #endif
3747e3a38431SPaul Bohm }
3748e3a38431SPaul Bohm
3749e3a38431SPaul Bohm /*****************************************************************************/
3750e3a38431SPaul Bohm /* singly-linked list management, used when the expected list length is short */
3751e3a38431SPaul Bohm
3752e3a38431SPaul Bohm inline_size void
wlist_add(WL * head,WL elem)3753e3a38431SPaul Bohm wlist_add (WL *head, WL elem)
3754e3a38431SPaul Bohm {
3755e3a38431SPaul Bohm elem->next = *head;
3756e3a38431SPaul Bohm *head = elem;
3757e3a38431SPaul Bohm }
3758e3a38431SPaul Bohm
3759e3a38431SPaul Bohm inline_size void
wlist_del(WL * head,WL elem)3760e3a38431SPaul Bohm wlist_del (WL *head, WL elem)
3761e3a38431SPaul Bohm {
3762e3a38431SPaul Bohm while (*head)
3763e3a38431SPaul Bohm {
3764e3a38431SPaul Bohm if (expect_true (*head == elem))
3765e3a38431SPaul Bohm {
3766e3a38431SPaul Bohm *head = elem->next;
3767e3a38431SPaul Bohm break;
3768e3a38431SPaul Bohm }
3769e3a38431SPaul Bohm
3770e3a38431SPaul Bohm head = &(*head)->next;
3771e3a38431SPaul Bohm }
3772e3a38431SPaul Bohm }
3773e3a38431SPaul Bohm
3774e3a38431SPaul Bohm /* internal, faster, version of ev_clear_pending */
3775e3a38431SPaul Bohm inline_speed void
clear_pending(EV_P_ W w)3776e3a38431SPaul Bohm clear_pending (EV_P_ W w)
3777e3a38431SPaul Bohm {
3778e3a38431SPaul Bohm if (w->pending)
3779e3a38431SPaul Bohm {
3780e3a38431SPaul Bohm pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
3781e3a38431SPaul Bohm w->pending = 0;
3782e3a38431SPaul Bohm }
3783e3a38431SPaul Bohm }
3784e3a38431SPaul Bohm
3785e3a38431SPaul Bohm int
ev_clear_pending(EV_P_ void * w)3786*93823e6cSPaul Bohm ev_clear_pending (EV_P_ void *w) EV_THROW
3787e3a38431SPaul Bohm {
3788e3a38431SPaul Bohm W w_ = (W)w;
3789e3a38431SPaul Bohm int pending = w_->pending;
3790e3a38431SPaul Bohm
3791e3a38431SPaul Bohm if (expect_true (pending))
3792e3a38431SPaul Bohm {
3793e3a38431SPaul Bohm ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
3794e3a38431SPaul Bohm p->w = (W)&pending_w;
3795e3a38431SPaul Bohm w_->pending = 0;
3796e3a38431SPaul Bohm return p->events;
3797e3a38431SPaul Bohm }
3798e3a38431SPaul Bohm else
3799e3a38431SPaul Bohm return 0;
3800e3a38431SPaul Bohm }
3801e3a38431SPaul Bohm
3802e3a38431SPaul Bohm inline_size void
pri_adjust(EV_P_ W w)3803e3a38431SPaul Bohm pri_adjust (EV_P_ W w)
3804e3a38431SPaul Bohm {
3805e3a38431SPaul Bohm int pri = ev_priority (w);
3806e3a38431SPaul Bohm pri = pri < EV_MINPRI ? EV_MINPRI : pri;
3807e3a38431SPaul Bohm pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
3808e3a38431SPaul Bohm ev_set_priority (w, pri);
3809e3a38431SPaul Bohm }
3810e3a38431SPaul Bohm
3811e3a38431SPaul Bohm inline_speed void
ev_start(EV_P_ W w,int active)3812e3a38431SPaul Bohm ev_start (EV_P_ W w, int active)
3813e3a38431SPaul Bohm {
3814e3a38431SPaul Bohm pri_adjust (EV_A_ w);
3815e3a38431SPaul Bohm w->active = active;
3816e3a38431SPaul Bohm ev_ref (EV_A);
3817e3a38431SPaul Bohm }
3818e3a38431SPaul Bohm
3819e3a38431SPaul Bohm inline_size void
ev_stop(EV_P_ W w)3820e3a38431SPaul Bohm ev_stop (EV_P_ W w)
3821e3a38431SPaul Bohm {
3822e3a38431SPaul Bohm ev_unref (EV_A);
3823e3a38431SPaul Bohm w->active = 0;
3824e3a38431SPaul Bohm }
3825e3a38431SPaul Bohm
3826e3a38431SPaul Bohm /*****************************************************************************/
3827e3a38431SPaul Bohm
3828e3a38431SPaul Bohm void noinline
ev_io_start(EV_P_ ev_io * w)3829*93823e6cSPaul Bohm ev_io_start (EV_P_ ev_io *w) EV_THROW
3830e3a38431SPaul Bohm {
3831e3a38431SPaul Bohm int fd = w->fd;
3832e3a38431SPaul Bohm
3833e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
3834e3a38431SPaul Bohm return;
3835e3a38431SPaul Bohm
3836e3a38431SPaul Bohm assert (("libev: ev_io_start called with negative fd", fd >= 0));
3837e3a38431SPaul Bohm assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
3838e3a38431SPaul Bohm
3839e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3840e3a38431SPaul Bohm
3841e3a38431SPaul Bohm ev_start (EV_A_ (W)w, 1);
3842e3a38431SPaul Bohm array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3843e3a38431SPaul Bohm wlist_add (&anfds[fd].head, (WL)w);
3844e3a38431SPaul Bohm
3845*93823e6cSPaul Bohm /* common bug, apparently */
3846*93823e6cSPaul Bohm assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3847*93823e6cSPaul Bohm
3848e3a38431SPaul Bohm fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3849e3a38431SPaul Bohm w->events &= ~EV__IOFDSET;
3850e3a38431SPaul Bohm
3851e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3852e3a38431SPaul Bohm }
3853e3a38431SPaul Bohm
3854e3a38431SPaul Bohm void noinline
ev_io_stop(EV_P_ ev_io * w)3855*93823e6cSPaul Bohm ev_io_stop (EV_P_ ev_io *w) EV_THROW
3856e3a38431SPaul Bohm {
3857e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
3858e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
3859e3a38431SPaul Bohm return;
3860e3a38431SPaul Bohm
3861e3a38431SPaul Bohm assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
3862e3a38431SPaul Bohm
3863e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3864e3a38431SPaul Bohm
3865e3a38431SPaul Bohm wlist_del (&anfds[w->fd].head, (WL)w);
3866e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
3867e3a38431SPaul Bohm
3868e3a38431SPaul Bohm fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
3869e3a38431SPaul Bohm
3870e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3871e3a38431SPaul Bohm }
3872e3a38431SPaul Bohm
3873e3a38431SPaul Bohm void noinline
ev_timer_start(EV_P_ ev_timer * w)3874*93823e6cSPaul Bohm ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3875e3a38431SPaul Bohm {
3876e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
3877e3a38431SPaul Bohm return;
3878e3a38431SPaul Bohm
3879e3a38431SPaul Bohm ev_at (w) += mn_now;
3880e3a38431SPaul Bohm
3881e3a38431SPaul Bohm assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
3882e3a38431SPaul Bohm
3883e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3884e3a38431SPaul Bohm
3885e3a38431SPaul Bohm ++timercnt;
3886e3a38431SPaul Bohm ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1);
3887e3a38431SPaul Bohm array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
3888e3a38431SPaul Bohm ANHE_w (timers [ev_active (w)]) = (WT)w;
3889e3a38431SPaul Bohm ANHE_at_cache (timers [ev_active (w)]);
3890e3a38431SPaul Bohm upheap (timers, ev_active (w));
3891e3a38431SPaul Bohm
3892e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3893e3a38431SPaul Bohm
3894e3a38431SPaul Bohm /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3895e3a38431SPaul Bohm }
3896e3a38431SPaul Bohm
3897e3a38431SPaul Bohm void noinline
ev_timer_stop(EV_P_ ev_timer * w)3898*93823e6cSPaul Bohm ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3899e3a38431SPaul Bohm {
3900e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
3901e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
3902e3a38431SPaul Bohm return;
3903e3a38431SPaul Bohm
3904e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3905e3a38431SPaul Bohm
3906e3a38431SPaul Bohm {
3907e3a38431SPaul Bohm int active = ev_active (w);
3908e3a38431SPaul Bohm
3909e3a38431SPaul Bohm assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
3910e3a38431SPaul Bohm
3911e3a38431SPaul Bohm --timercnt;
3912e3a38431SPaul Bohm
3913e3a38431SPaul Bohm if (expect_true (active < timercnt + HEAP0))
3914e3a38431SPaul Bohm {
3915e3a38431SPaul Bohm timers [active] = timers [timercnt + HEAP0];
3916e3a38431SPaul Bohm adjustheap (timers, timercnt, active);
3917e3a38431SPaul Bohm }
3918e3a38431SPaul Bohm }
3919e3a38431SPaul Bohm
3920e3a38431SPaul Bohm ev_at (w) -= mn_now;
3921e3a38431SPaul Bohm
3922e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
3923e3a38431SPaul Bohm
3924e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3925e3a38431SPaul Bohm }
3926e3a38431SPaul Bohm
3927e3a38431SPaul Bohm void noinline
ev_timer_again(EV_P_ ev_timer * w)3928*93823e6cSPaul Bohm ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3929e3a38431SPaul Bohm {
3930e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3931e3a38431SPaul Bohm
3932*93823e6cSPaul Bohm clear_pending (EV_A_ (W)w);
3933*93823e6cSPaul Bohm
3934e3a38431SPaul Bohm if (ev_is_active (w))
3935e3a38431SPaul Bohm {
3936e3a38431SPaul Bohm if (w->repeat)
3937e3a38431SPaul Bohm {
3938e3a38431SPaul Bohm ev_at (w) = mn_now + w->repeat;
3939e3a38431SPaul Bohm ANHE_at_cache (timers [ev_active (w)]);
3940e3a38431SPaul Bohm adjustheap (timers, timercnt, ev_active (w));
3941e3a38431SPaul Bohm }
3942e3a38431SPaul Bohm else
3943e3a38431SPaul Bohm ev_timer_stop (EV_A_ w);
3944e3a38431SPaul Bohm }
3945e3a38431SPaul Bohm else if (w->repeat)
3946e3a38431SPaul Bohm {
3947e3a38431SPaul Bohm ev_at (w) = w->repeat;
3948e3a38431SPaul Bohm ev_timer_start (EV_A_ w);
3949e3a38431SPaul Bohm }
3950e3a38431SPaul Bohm
3951e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3952e3a38431SPaul Bohm }
3953e3a38431SPaul Bohm
3954e3a38431SPaul Bohm ev_tstamp
ev_timer_remaining(EV_P_ ev_timer * w)3955*93823e6cSPaul Bohm ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3956e3a38431SPaul Bohm {
3957e3a38431SPaul Bohm return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3958e3a38431SPaul Bohm }
3959e3a38431SPaul Bohm
3960e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
3961e3a38431SPaul Bohm void noinline
ev_periodic_start(EV_P_ ev_periodic * w)3962*93823e6cSPaul Bohm ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3963e3a38431SPaul Bohm {
3964e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
3965e3a38431SPaul Bohm return;
3966e3a38431SPaul Bohm
3967e3a38431SPaul Bohm if (w->reschedule_cb)
3968e3a38431SPaul Bohm ev_at (w) = w->reschedule_cb (w, ev_rt_now);
3969e3a38431SPaul Bohm else if (w->interval)
3970e3a38431SPaul Bohm {
3971e3a38431SPaul Bohm assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
3972e3a38431SPaul Bohm periodic_recalc (EV_A_ w);
3973e3a38431SPaul Bohm }
3974e3a38431SPaul Bohm else
3975e3a38431SPaul Bohm ev_at (w) = w->offset;
3976e3a38431SPaul Bohm
3977e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3978e3a38431SPaul Bohm
3979e3a38431SPaul Bohm ++periodiccnt;
3980e3a38431SPaul Bohm ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1);
3981e3a38431SPaul Bohm array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
3982e3a38431SPaul Bohm ANHE_w (periodics [ev_active (w)]) = (WT)w;
3983e3a38431SPaul Bohm ANHE_at_cache (periodics [ev_active (w)]);
3984e3a38431SPaul Bohm upheap (periodics, ev_active (w));
3985e3a38431SPaul Bohm
3986e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3987e3a38431SPaul Bohm
3988e3a38431SPaul Bohm /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3989e3a38431SPaul Bohm }
3990e3a38431SPaul Bohm
3991e3a38431SPaul Bohm void noinline
ev_periodic_stop(EV_P_ ev_periodic * w)3992*93823e6cSPaul Bohm ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3993e3a38431SPaul Bohm {
3994e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
3995e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
3996e3a38431SPaul Bohm return;
3997e3a38431SPaul Bohm
3998e3a38431SPaul Bohm EV_FREQUENT_CHECK;
3999e3a38431SPaul Bohm
4000e3a38431SPaul Bohm {
4001e3a38431SPaul Bohm int active = ev_active (w);
4002e3a38431SPaul Bohm
4003e3a38431SPaul Bohm assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
4004e3a38431SPaul Bohm
4005e3a38431SPaul Bohm --periodiccnt;
4006e3a38431SPaul Bohm
4007e3a38431SPaul Bohm if (expect_true (active < periodiccnt + HEAP0))
4008e3a38431SPaul Bohm {
4009e3a38431SPaul Bohm periodics [active] = periodics [periodiccnt + HEAP0];
4010e3a38431SPaul Bohm adjustheap (periodics, periodiccnt, active);
4011e3a38431SPaul Bohm }
4012e3a38431SPaul Bohm }
4013e3a38431SPaul Bohm
4014e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4015e3a38431SPaul Bohm
4016e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4017e3a38431SPaul Bohm }
4018e3a38431SPaul Bohm
4019e3a38431SPaul Bohm void noinline
ev_periodic_again(EV_P_ ev_periodic * w)4020*93823e6cSPaul Bohm ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
4021e3a38431SPaul Bohm {
4022e3a38431SPaul Bohm /* TODO: use adjustheap and recalculation */
4023e3a38431SPaul Bohm ev_periodic_stop (EV_A_ w);
4024e3a38431SPaul Bohm ev_periodic_start (EV_A_ w);
4025e3a38431SPaul Bohm }
4026e3a38431SPaul Bohm #endif
4027e3a38431SPaul Bohm
4028e3a38431SPaul Bohm #ifndef SA_RESTART
4029e3a38431SPaul Bohm # define SA_RESTART 0
4030e3a38431SPaul Bohm #endif
4031e3a38431SPaul Bohm
4032e3a38431SPaul Bohm #if EV_SIGNAL_ENABLE
4033e3a38431SPaul Bohm
4034e3a38431SPaul Bohm void noinline
ev_signal_start(EV_P_ ev_signal * w)4035*93823e6cSPaul Bohm ev_signal_start (EV_P_ ev_signal *w) EV_THROW
4036e3a38431SPaul Bohm {
4037e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4038e3a38431SPaul Bohm return;
4039e3a38431SPaul Bohm
4040e3a38431SPaul Bohm assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
4041e3a38431SPaul Bohm
4042e3a38431SPaul Bohm #if EV_MULTIPLICITY
4043e3a38431SPaul Bohm assert (("libev: a signal must not be attached to two different loops",
4044e3a38431SPaul Bohm !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
4045e3a38431SPaul Bohm
4046e3a38431SPaul Bohm signals [w->signum - 1].loop = EV_A;
4047*93823e6cSPaul Bohm ECB_MEMORY_FENCE_RELEASE;
4048e3a38431SPaul Bohm #endif
4049e3a38431SPaul Bohm
4050e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4051e3a38431SPaul Bohm
4052e3a38431SPaul Bohm #if EV_USE_SIGNALFD
4053e3a38431SPaul Bohm if (sigfd == -2)
4054e3a38431SPaul Bohm {
4055e3a38431SPaul Bohm sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
4056e3a38431SPaul Bohm if (sigfd < 0 && errno == EINVAL)
4057e3a38431SPaul Bohm sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
4058e3a38431SPaul Bohm
4059e3a38431SPaul Bohm if (sigfd >= 0)
4060e3a38431SPaul Bohm {
4061e3a38431SPaul Bohm fd_intern (sigfd); /* doing it twice will not hurt */
4062e3a38431SPaul Bohm
4063e3a38431SPaul Bohm sigemptyset (&sigfd_set);
4064e3a38431SPaul Bohm
4065e3a38431SPaul Bohm ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
4066e3a38431SPaul Bohm ev_set_priority (&sigfd_w, EV_MAXPRI);
4067e3a38431SPaul Bohm ev_io_start (EV_A_ &sigfd_w);
4068e3a38431SPaul Bohm ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
4069e3a38431SPaul Bohm }
4070e3a38431SPaul Bohm }
4071e3a38431SPaul Bohm
4072e3a38431SPaul Bohm if (sigfd >= 0)
4073e3a38431SPaul Bohm {
4074e3a38431SPaul Bohm /* TODO: check .head */
4075e3a38431SPaul Bohm sigaddset (&sigfd_set, w->signum);
4076e3a38431SPaul Bohm sigprocmask (SIG_BLOCK, &sigfd_set, 0);
4077e3a38431SPaul Bohm
4078e3a38431SPaul Bohm signalfd (sigfd, &sigfd_set, 0);
4079e3a38431SPaul Bohm }
4080e3a38431SPaul Bohm #endif
4081e3a38431SPaul Bohm
4082e3a38431SPaul Bohm ev_start (EV_A_ (W)w, 1);
4083e3a38431SPaul Bohm wlist_add (&signals [w->signum - 1].head, (WL)w);
4084e3a38431SPaul Bohm
4085e3a38431SPaul Bohm if (!((WL)w)->next)
4086e3a38431SPaul Bohm # if EV_USE_SIGNALFD
4087e3a38431SPaul Bohm if (sigfd < 0) /*TODO*/
4088e3a38431SPaul Bohm # endif
4089e3a38431SPaul Bohm {
4090e3a38431SPaul Bohm # ifdef _WIN32
4091e3a38431SPaul Bohm evpipe_init (EV_A);
4092e3a38431SPaul Bohm
4093e3a38431SPaul Bohm signal (w->signum, ev_sighandler);
4094e3a38431SPaul Bohm # else
4095e3a38431SPaul Bohm struct sigaction sa;
4096e3a38431SPaul Bohm
4097e3a38431SPaul Bohm evpipe_init (EV_A);
4098e3a38431SPaul Bohm
4099e3a38431SPaul Bohm sa.sa_handler = ev_sighandler;
4100e3a38431SPaul Bohm sigfillset (&sa.sa_mask);
4101e3a38431SPaul Bohm sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
4102e3a38431SPaul Bohm sigaction (w->signum, &sa, 0);
4103e3a38431SPaul Bohm
4104e3a38431SPaul Bohm if (origflags & EVFLAG_NOSIGMASK)
4105e3a38431SPaul Bohm {
4106e3a38431SPaul Bohm sigemptyset (&sa.sa_mask);
4107e3a38431SPaul Bohm sigaddset (&sa.sa_mask, w->signum);
4108e3a38431SPaul Bohm sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
4109e3a38431SPaul Bohm }
4110e3a38431SPaul Bohm #endif
4111e3a38431SPaul Bohm }
4112e3a38431SPaul Bohm
4113e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4114e3a38431SPaul Bohm }
4115e3a38431SPaul Bohm
4116e3a38431SPaul Bohm void noinline
ev_signal_stop(EV_P_ ev_signal * w)4117*93823e6cSPaul Bohm ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
4118e3a38431SPaul Bohm {
4119e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4120e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4121e3a38431SPaul Bohm return;
4122e3a38431SPaul Bohm
4123e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4124e3a38431SPaul Bohm
4125e3a38431SPaul Bohm wlist_del (&signals [w->signum - 1].head, (WL)w);
4126e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4127e3a38431SPaul Bohm
4128e3a38431SPaul Bohm if (!signals [w->signum - 1].head)
4129e3a38431SPaul Bohm {
4130e3a38431SPaul Bohm #if EV_MULTIPLICITY
4131e3a38431SPaul Bohm signals [w->signum - 1].loop = 0; /* unattach from signal */
4132e3a38431SPaul Bohm #endif
4133e3a38431SPaul Bohm #if EV_USE_SIGNALFD
4134e3a38431SPaul Bohm if (sigfd >= 0)
4135e3a38431SPaul Bohm {
4136e3a38431SPaul Bohm sigset_t ss;
4137e3a38431SPaul Bohm
4138e3a38431SPaul Bohm sigemptyset (&ss);
4139e3a38431SPaul Bohm sigaddset (&ss, w->signum);
4140e3a38431SPaul Bohm sigdelset (&sigfd_set, w->signum);
4141e3a38431SPaul Bohm
4142e3a38431SPaul Bohm signalfd (sigfd, &sigfd_set, 0);
4143e3a38431SPaul Bohm sigprocmask (SIG_UNBLOCK, &ss, 0);
4144e3a38431SPaul Bohm }
4145e3a38431SPaul Bohm else
4146e3a38431SPaul Bohm #endif
4147e3a38431SPaul Bohm signal (w->signum, SIG_DFL);
4148e3a38431SPaul Bohm }
4149e3a38431SPaul Bohm
4150e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4151e3a38431SPaul Bohm }
4152e3a38431SPaul Bohm
4153e3a38431SPaul Bohm #endif
4154e3a38431SPaul Bohm
4155e3a38431SPaul Bohm #if EV_CHILD_ENABLE
4156e3a38431SPaul Bohm
4157e3a38431SPaul Bohm void
ev_child_start(EV_P_ ev_child * w)4158*93823e6cSPaul Bohm ev_child_start (EV_P_ ev_child *w) EV_THROW
4159e3a38431SPaul Bohm {
4160e3a38431SPaul Bohm #if EV_MULTIPLICITY
4161e3a38431SPaul Bohm assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
4162e3a38431SPaul Bohm #endif
4163e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4164e3a38431SPaul Bohm return;
4165e3a38431SPaul Bohm
4166e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4167e3a38431SPaul Bohm
4168e3a38431SPaul Bohm ev_start (EV_A_ (W)w, 1);
4169e3a38431SPaul Bohm wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
4170e3a38431SPaul Bohm
4171e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4172e3a38431SPaul Bohm }
4173e3a38431SPaul Bohm
4174e3a38431SPaul Bohm void
ev_child_stop(EV_P_ ev_child * w)4175*93823e6cSPaul Bohm ev_child_stop (EV_P_ ev_child *w) EV_THROW
4176e3a38431SPaul Bohm {
4177e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4178e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4179e3a38431SPaul Bohm return;
4180e3a38431SPaul Bohm
4181e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4182e3a38431SPaul Bohm
4183e3a38431SPaul Bohm wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
4184e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4185e3a38431SPaul Bohm
4186e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4187e3a38431SPaul Bohm }
4188e3a38431SPaul Bohm
4189e3a38431SPaul Bohm #endif
4190e3a38431SPaul Bohm
4191e3a38431SPaul Bohm #if EV_STAT_ENABLE
4192e3a38431SPaul Bohm
4193e3a38431SPaul Bohm # ifdef _WIN32
4194e3a38431SPaul Bohm # undef lstat
4195e3a38431SPaul Bohm # define lstat(a,b) _stati64 (a,b)
4196e3a38431SPaul Bohm # endif
4197e3a38431SPaul Bohm
4198e3a38431SPaul Bohm #define DEF_STAT_INTERVAL 5.0074891
4199e3a38431SPaul Bohm #define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */
4200e3a38431SPaul Bohm #define MIN_STAT_INTERVAL 0.1074891
4201e3a38431SPaul Bohm
4202e3a38431SPaul Bohm static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
4203e3a38431SPaul Bohm
4204e3a38431SPaul Bohm #if EV_USE_INOTIFY
4205e3a38431SPaul Bohm
4206e3a38431SPaul Bohm /* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
4207e3a38431SPaul Bohm # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
4208e3a38431SPaul Bohm
4209e3a38431SPaul Bohm static void noinline
infy_add(EV_P_ ev_stat * w)4210e3a38431SPaul Bohm infy_add (EV_P_ ev_stat *w)
4211e3a38431SPaul Bohm {
4212*93823e6cSPaul Bohm w->wd = inotify_add_watch (fs_fd, w->path,
4213*93823e6cSPaul Bohm IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
4214*93823e6cSPaul Bohm | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
4215*93823e6cSPaul Bohm | IN_DONT_FOLLOW | IN_MASK_ADD);
4216e3a38431SPaul Bohm
4217e3a38431SPaul Bohm if (w->wd >= 0)
4218e3a38431SPaul Bohm {
4219e3a38431SPaul Bohm struct statfs sfs;
4220e3a38431SPaul Bohm
4221e3a38431SPaul Bohm /* now local changes will be tracked by inotify, but remote changes won't */
4222e3a38431SPaul Bohm /* unless the filesystem is known to be local, we therefore still poll */
4223e3a38431SPaul Bohm /* also do poll on <2.6.25, but with normal frequency */
4224e3a38431SPaul Bohm
4225e3a38431SPaul Bohm if (!fs_2625)
4226e3a38431SPaul Bohm w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
4227e3a38431SPaul Bohm else if (!statfs (w->path, &sfs)
4228e3a38431SPaul Bohm && (sfs.f_type == 0x1373 /* devfs */
4229*93823e6cSPaul Bohm || sfs.f_type == 0x4006 /* fat */
4230*93823e6cSPaul Bohm || sfs.f_type == 0x4d44 /* msdos */
4231e3a38431SPaul Bohm || sfs.f_type == 0xEF53 /* ext2/3 */
4232*93823e6cSPaul Bohm || sfs.f_type == 0x72b6 /* jffs2 */
4233*93823e6cSPaul Bohm || sfs.f_type == 0x858458f6 /* ramfs */
4234*93823e6cSPaul Bohm || sfs.f_type == 0x5346544e /* ntfs */
4235e3a38431SPaul Bohm || sfs.f_type == 0x3153464a /* jfs */
4236*93823e6cSPaul Bohm || sfs.f_type == 0x9123683e /* btrfs */
4237e3a38431SPaul Bohm || sfs.f_type == 0x52654973 /* reiser3 */
4238*93823e6cSPaul Bohm || sfs.f_type == 0x01021994 /* tmpfs */
4239e3a38431SPaul Bohm || sfs.f_type == 0x58465342 /* xfs */))
4240e3a38431SPaul Bohm w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
4241e3a38431SPaul Bohm else
4242e3a38431SPaul Bohm w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
4243e3a38431SPaul Bohm }
4244e3a38431SPaul Bohm else
4245e3a38431SPaul Bohm {
4246e3a38431SPaul Bohm /* can't use inotify, continue to stat */
4247e3a38431SPaul Bohm w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
4248e3a38431SPaul Bohm
4249e3a38431SPaul Bohm /* if path is not there, monitor some parent directory for speedup hints */
4250e3a38431SPaul Bohm /* note that exceeding the hardcoded path limit is not a correctness issue, */
4251e3a38431SPaul Bohm /* but an efficiency issue only */
4252e3a38431SPaul Bohm if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
4253e3a38431SPaul Bohm {
4254e3a38431SPaul Bohm char path [4096];
4255e3a38431SPaul Bohm strcpy (path, w->path);
4256e3a38431SPaul Bohm
4257e3a38431SPaul Bohm do
4258e3a38431SPaul Bohm {
4259e3a38431SPaul Bohm int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
4260e3a38431SPaul Bohm | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
4261e3a38431SPaul Bohm
4262e3a38431SPaul Bohm char *pend = strrchr (path, '/');
4263e3a38431SPaul Bohm
4264e3a38431SPaul Bohm if (!pend || pend == path)
4265e3a38431SPaul Bohm break;
4266e3a38431SPaul Bohm
4267e3a38431SPaul Bohm *pend = 0;
4268e3a38431SPaul Bohm w->wd = inotify_add_watch (fs_fd, path, mask);
4269e3a38431SPaul Bohm }
4270e3a38431SPaul Bohm while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
4271e3a38431SPaul Bohm }
4272e3a38431SPaul Bohm }
4273e3a38431SPaul Bohm
4274e3a38431SPaul Bohm if (w->wd >= 0)
4275e3a38431SPaul Bohm wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
4276e3a38431SPaul Bohm
4277e3a38431SPaul Bohm /* now re-arm timer, if required */
4278e3a38431SPaul Bohm if (ev_is_active (&w->timer)) ev_ref (EV_A);
4279e3a38431SPaul Bohm ev_timer_again (EV_A_ &w->timer);
4280e3a38431SPaul Bohm if (ev_is_active (&w->timer)) ev_unref (EV_A);
4281e3a38431SPaul Bohm }
4282e3a38431SPaul Bohm
4283e3a38431SPaul Bohm static void noinline
infy_del(EV_P_ ev_stat * w)4284e3a38431SPaul Bohm infy_del (EV_P_ ev_stat *w)
4285e3a38431SPaul Bohm {
4286e3a38431SPaul Bohm int slot;
4287e3a38431SPaul Bohm int wd = w->wd;
4288e3a38431SPaul Bohm
4289e3a38431SPaul Bohm if (wd < 0)
4290e3a38431SPaul Bohm return;
4291e3a38431SPaul Bohm
4292e3a38431SPaul Bohm w->wd = -2;
4293e3a38431SPaul Bohm slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
4294e3a38431SPaul Bohm wlist_del (&fs_hash [slot].head, (WL)w);
4295e3a38431SPaul Bohm
4296e3a38431SPaul Bohm /* remove this watcher, if others are watching it, they will rearm */
4297e3a38431SPaul Bohm inotify_rm_watch (fs_fd, wd);
4298e3a38431SPaul Bohm }
4299e3a38431SPaul Bohm
4300e3a38431SPaul Bohm static void noinline
infy_wd(EV_P_ int slot,int wd,struct inotify_event * ev)4301e3a38431SPaul Bohm infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
4302e3a38431SPaul Bohm {
4303e3a38431SPaul Bohm if (slot < 0)
4304e3a38431SPaul Bohm /* overflow, need to check for all hash slots */
4305e3a38431SPaul Bohm for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
4306e3a38431SPaul Bohm infy_wd (EV_A_ slot, wd, ev);
4307e3a38431SPaul Bohm else
4308e3a38431SPaul Bohm {
4309e3a38431SPaul Bohm WL w_;
4310e3a38431SPaul Bohm
4311e3a38431SPaul Bohm for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
4312e3a38431SPaul Bohm {
4313e3a38431SPaul Bohm ev_stat *w = (ev_stat *)w_;
4314e3a38431SPaul Bohm w_ = w_->next; /* lets us remove this watcher and all before it */
4315e3a38431SPaul Bohm
4316e3a38431SPaul Bohm if (w->wd == wd || wd == -1)
4317e3a38431SPaul Bohm {
4318e3a38431SPaul Bohm if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
4319e3a38431SPaul Bohm {
4320e3a38431SPaul Bohm wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
4321e3a38431SPaul Bohm w->wd = -1;
4322e3a38431SPaul Bohm infy_add (EV_A_ w); /* re-add, no matter what */
4323e3a38431SPaul Bohm }
4324e3a38431SPaul Bohm
4325e3a38431SPaul Bohm stat_timer_cb (EV_A_ &w->timer, 0);
4326e3a38431SPaul Bohm }
4327e3a38431SPaul Bohm }
4328e3a38431SPaul Bohm }
4329e3a38431SPaul Bohm }
4330e3a38431SPaul Bohm
4331e3a38431SPaul Bohm static void
infy_cb(EV_P_ ev_io * w,int revents)4332e3a38431SPaul Bohm infy_cb (EV_P_ ev_io *w, int revents)
4333e3a38431SPaul Bohm {
4334e3a38431SPaul Bohm char buf [EV_INOTIFY_BUFSIZE];
4335e3a38431SPaul Bohm int ofs;
4336e3a38431SPaul Bohm int len = read (fs_fd, buf, sizeof (buf));
4337e3a38431SPaul Bohm
4338e3a38431SPaul Bohm for (ofs = 0; ofs < len; )
4339e3a38431SPaul Bohm {
4340e3a38431SPaul Bohm struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
4341e3a38431SPaul Bohm infy_wd (EV_A_ ev->wd, ev->wd, ev);
4342e3a38431SPaul Bohm ofs += sizeof (struct inotify_event) + ev->len;
4343e3a38431SPaul Bohm }
4344e3a38431SPaul Bohm }
4345e3a38431SPaul Bohm
4346*93823e6cSPaul Bohm inline_size void ecb_cold
ev_check_2625(EV_P)4347e3a38431SPaul Bohm ev_check_2625 (EV_P)
4348e3a38431SPaul Bohm {
4349e3a38431SPaul Bohm /* kernels < 2.6.25 are borked
4350e3a38431SPaul Bohm * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
4351e3a38431SPaul Bohm */
4352e3a38431SPaul Bohm if (ev_linux_version () < 0x020619)
4353e3a38431SPaul Bohm return;
4354e3a38431SPaul Bohm
4355e3a38431SPaul Bohm fs_2625 = 1;
4356e3a38431SPaul Bohm }
4357e3a38431SPaul Bohm
4358e3a38431SPaul Bohm inline_size int
infy_newfd(void)4359e3a38431SPaul Bohm infy_newfd (void)
4360e3a38431SPaul Bohm {
4361*93823e6cSPaul Bohm #if defined IN_CLOEXEC && defined IN_NONBLOCK
4362e3a38431SPaul Bohm int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
4363e3a38431SPaul Bohm if (fd >= 0)
4364e3a38431SPaul Bohm return fd;
4365e3a38431SPaul Bohm #endif
4366e3a38431SPaul Bohm return inotify_init ();
4367e3a38431SPaul Bohm }
4368e3a38431SPaul Bohm
4369e3a38431SPaul Bohm inline_size void
infy_init(EV_P)4370e3a38431SPaul Bohm infy_init (EV_P)
4371e3a38431SPaul Bohm {
4372e3a38431SPaul Bohm if (fs_fd != -2)
4373e3a38431SPaul Bohm return;
4374e3a38431SPaul Bohm
4375e3a38431SPaul Bohm fs_fd = -1;
4376e3a38431SPaul Bohm
4377e3a38431SPaul Bohm ev_check_2625 (EV_A);
4378e3a38431SPaul Bohm
4379e3a38431SPaul Bohm fs_fd = infy_newfd ();
4380e3a38431SPaul Bohm
4381e3a38431SPaul Bohm if (fs_fd >= 0)
4382e3a38431SPaul Bohm {
4383e3a38431SPaul Bohm fd_intern (fs_fd);
4384e3a38431SPaul Bohm ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
4385e3a38431SPaul Bohm ev_set_priority (&fs_w, EV_MAXPRI);
4386e3a38431SPaul Bohm ev_io_start (EV_A_ &fs_w);
4387e3a38431SPaul Bohm ev_unref (EV_A);
4388e3a38431SPaul Bohm }
4389e3a38431SPaul Bohm }
4390e3a38431SPaul Bohm
4391e3a38431SPaul Bohm inline_size void
infy_fork(EV_P)4392e3a38431SPaul Bohm infy_fork (EV_P)
4393e3a38431SPaul Bohm {
4394e3a38431SPaul Bohm int slot;
4395e3a38431SPaul Bohm
4396e3a38431SPaul Bohm if (fs_fd < 0)
4397e3a38431SPaul Bohm return;
4398e3a38431SPaul Bohm
4399e3a38431SPaul Bohm ev_ref (EV_A);
4400e3a38431SPaul Bohm ev_io_stop (EV_A_ &fs_w);
4401e3a38431SPaul Bohm close (fs_fd);
4402e3a38431SPaul Bohm fs_fd = infy_newfd ();
4403e3a38431SPaul Bohm
4404e3a38431SPaul Bohm if (fs_fd >= 0)
4405e3a38431SPaul Bohm {
4406e3a38431SPaul Bohm fd_intern (fs_fd);
4407e3a38431SPaul Bohm ev_io_set (&fs_w, fs_fd, EV_READ);
4408e3a38431SPaul Bohm ev_io_start (EV_A_ &fs_w);
4409e3a38431SPaul Bohm ev_unref (EV_A);
4410e3a38431SPaul Bohm }
4411e3a38431SPaul Bohm
4412e3a38431SPaul Bohm for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
4413e3a38431SPaul Bohm {
4414e3a38431SPaul Bohm WL w_ = fs_hash [slot].head;
4415e3a38431SPaul Bohm fs_hash [slot].head = 0;
4416e3a38431SPaul Bohm
4417e3a38431SPaul Bohm while (w_)
4418e3a38431SPaul Bohm {
4419e3a38431SPaul Bohm ev_stat *w = (ev_stat *)w_;
4420e3a38431SPaul Bohm w_ = w_->next; /* lets us add this watcher */
4421e3a38431SPaul Bohm
4422e3a38431SPaul Bohm w->wd = -1;
4423e3a38431SPaul Bohm
4424e3a38431SPaul Bohm if (fs_fd >= 0)
4425e3a38431SPaul Bohm infy_add (EV_A_ w); /* re-add, no matter what */
4426e3a38431SPaul Bohm else
4427e3a38431SPaul Bohm {
4428e3a38431SPaul Bohm w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
4429e3a38431SPaul Bohm if (ev_is_active (&w->timer)) ev_ref (EV_A);
4430e3a38431SPaul Bohm ev_timer_again (EV_A_ &w->timer);
4431e3a38431SPaul Bohm if (ev_is_active (&w->timer)) ev_unref (EV_A);
4432e3a38431SPaul Bohm }
4433e3a38431SPaul Bohm }
4434e3a38431SPaul Bohm }
4435e3a38431SPaul Bohm }
4436e3a38431SPaul Bohm
4437e3a38431SPaul Bohm #endif
4438e3a38431SPaul Bohm
4439e3a38431SPaul Bohm #ifdef _WIN32
4440e3a38431SPaul Bohm # define EV_LSTAT(p,b) _stati64 (p, b)
4441e3a38431SPaul Bohm #else
4442e3a38431SPaul Bohm # define EV_LSTAT(p,b) lstat (p, b)
4443e3a38431SPaul Bohm #endif
4444e3a38431SPaul Bohm
4445e3a38431SPaul Bohm void
ev_stat_stat(EV_P_ ev_stat * w)4446*93823e6cSPaul Bohm ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
4447e3a38431SPaul Bohm {
4448e3a38431SPaul Bohm if (lstat (w->path, &w->attr) < 0)
4449e3a38431SPaul Bohm w->attr.st_nlink = 0;
4450e3a38431SPaul Bohm else if (!w->attr.st_nlink)
4451e3a38431SPaul Bohm w->attr.st_nlink = 1;
4452e3a38431SPaul Bohm }
4453e3a38431SPaul Bohm
4454e3a38431SPaul Bohm static void noinline
stat_timer_cb(EV_P_ ev_timer * w_,int revents)4455e3a38431SPaul Bohm stat_timer_cb (EV_P_ ev_timer *w_, int revents)
4456e3a38431SPaul Bohm {
4457e3a38431SPaul Bohm ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
4458e3a38431SPaul Bohm
4459e3a38431SPaul Bohm ev_statdata prev = w->attr;
4460e3a38431SPaul Bohm ev_stat_stat (EV_A_ w);
4461e3a38431SPaul Bohm
4462e3a38431SPaul Bohm /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
4463e3a38431SPaul Bohm if (
4464e3a38431SPaul Bohm prev.st_dev != w->attr.st_dev
4465e3a38431SPaul Bohm || prev.st_ino != w->attr.st_ino
4466e3a38431SPaul Bohm || prev.st_mode != w->attr.st_mode
4467e3a38431SPaul Bohm || prev.st_nlink != w->attr.st_nlink
4468e3a38431SPaul Bohm || prev.st_uid != w->attr.st_uid
4469e3a38431SPaul Bohm || prev.st_gid != w->attr.st_gid
4470e3a38431SPaul Bohm || prev.st_rdev != w->attr.st_rdev
4471e3a38431SPaul Bohm || prev.st_size != w->attr.st_size
4472e3a38431SPaul Bohm || prev.st_atime != w->attr.st_atime
4473e3a38431SPaul Bohm || prev.st_mtime != w->attr.st_mtime
4474e3a38431SPaul Bohm || prev.st_ctime != w->attr.st_ctime
4475e3a38431SPaul Bohm ) {
4476e3a38431SPaul Bohm /* we only update w->prev on actual differences */
4477e3a38431SPaul Bohm /* in case we test more often than invoke the callback, */
4478e3a38431SPaul Bohm /* to ensure that prev is always different to attr */
4479e3a38431SPaul Bohm w->prev = prev;
4480e3a38431SPaul Bohm
4481e3a38431SPaul Bohm #if EV_USE_INOTIFY
4482e3a38431SPaul Bohm if (fs_fd >= 0)
4483e3a38431SPaul Bohm {
4484e3a38431SPaul Bohm infy_del (EV_A_ w);
4485e3a38431SPaul Bohm infy_add (EV_A_ w);
4486e3a38431SPaul Bohm ev_stat_stat (EV_A_ w); /* avoid race... */
4487e3a38431SPaul Bohm }
4488e3a38431SPaul Bohm #endif
4489e3a38431SPaul Bohm
4490e3a38431SPaul Bohm ev_feed_event (EV_A_ w, EV_STAT);
4491e3a38431SPaul Bohm }
4492e3a38431SPaul Bohm }
4493e3a38431SPaul Bohm
4494e3a38431SPaul Bohm void
ev_stat_start(EV_P_ ev_stat * w)4495*93823e6cSPaul Bohm ev_stat_start (EV_P_ ev_stat *w) EV_THROW
4496e3a38431SPaul Bohm {
4497e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4498e3a38431SPaul Bohm return;
4499e3a38431SPaul Bohm
4500e3a38431SPaul Bohm ev_stat_stat (EV_A_ w);
4501e3a38431SPaul Bohm
4502e3a38431SPaul Bohm if (w->interval < MIN_STAT_INTERVAL && w->interval)
4503e3a38431SPaul Bohm w->interval = MIN_STAT_INTERVAL;
4504e3a38431SPaul Bohm
4505e3a38431SPaul Bohm ev_timer_init (&w->timer, stat_timer_cb, 0., w->interval ? w->interval : DEF_STAT_INTERVAL);
4506e3a38431SPaul Bohm ev_set_priority (&w->timer, ev_priority (w));
4507e3a38431SPaul Bohm
4508e3a38431SPaul Bohm #if EV_USE_INOTIFY
4509e3a38431SPaul Bohm infy_init (EV_A);
4510e3a38431SPaul Bohm
4511e3a38431SPaul Bohm if (fs_fd >= 0)
4512e3a38431SPaul Bohm infy_add (EV_A_ w);
4513e3a38431SPaul Bohm else
4514e3a38431SPaul Bohm #endif
4515e3a38431SPaul Bohm {
4516e3a38431SPaul Bohm ev_timer_again (EV_A_ &w->timer);
4517e3a38431SPaul Bohm ev_unref (EV_A);
4518e3a38431SPaul Bohm }
4519e3a38431SPaul Bohm
4520e3a38431SPaul Bohm ev_start (EV_A_ (W)w, 1);
4521e3a38431SPaul Bohm
4522e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4523e3a38431SPaul Bohm }
4524e3a38431SPaul Bohm
4525e3a38431SPaul Bohm void
ev_stat_stop(EV_P_ ev_stat * w)4526*93823e6cSPaul Bohm ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
4527e3a38431SPaul Bohm {
4528e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4529e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4530e3a38431SPaul Bohm return;
4531e3a38431SPaul Bohm
4532e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4533e3a38431SPaul Bohm
4534e3a38431SPaul Bohm #if EV_USE_INOTIFY
4535e3a38431SPaul Bohm infy_del (EV_A_ w);
4536e3a38431SPaul Bohm #endif
4537e3a38431SPaul Bohm
4538e3a38431SPaul Bohm if (ev_is_active (&w->timer))
4539e3a38431SPaul Bohm {
4540e3a38431SPaul Bohm ev_ref (EV_A);
4541e3a38431SPaul Bohm ev_timer_stop (EV_A_ &w->timer);
4542e3a38431SPaul Bohm }
4543e3a38431SPaul Bohm
4544e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4545e3a38431SPaul Bohm
4546e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4547e3a38431SPaul Bohm }
4548e3a38431SPaul Bohm #endif
4549e3a38431SPaul Bohm
4550e3a38431SPaul Bohm #if EV_IDLE_ENABLE
4551e3a38431SPaul Bohm void
ev_idle_start(EV_P_ ev_idle * w)4552*93823e6cSPaul Bohm ev_idle_start (EV_P_ ev_idle *w) EV_THROW
4553e3a38431SPaul Bohm {
4554e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4555e3a38431SPaul Bohm return;
4556e3a38431SPaul Bohm
4557e3a38431SPaul Bohm pri_adjust (EV_A_ (W)w);
4558e3a38431SPaul Bohm
4559e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4560e3a38431SPaul Bohm
4561e3a38431SPaul Bohm {
4562e3a38431SPaul Bohm int active = ++idlecnt [ABSPRI (w)];
4563e3a38431SPaul Bohm
4564e3a38431SPaul Bohm ++idleall;
4565e3a38431SPaul Bohm ev_start (EV_A_ (W)w, active);
4566e3a38431SPaul Bohm
4567e3a38431SPaul Bohm array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
4568e3a38431SPaul Bohm idles [ABSPRI (w)][active - 1] = w;
4569e3a38431SPaul Bohm }
4570e3a38431SPaul Bohm
4571e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4572e3a38431SPaul Bohm }
4573e3a38431SPaul Bohm
4574e3a38431SPaul Bohm void
ev_idle_stop(EV_P_ ev_idle * w)4575*93823e6cSPaul Bohm ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
4576e3a38431SPaul Bohm {
4577e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4578e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4579e3a38431SPaul Bohm return;
4580e3a38431SPaul Bohm
4581e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4582e3a38431SPaul Bohm
4583e3a38431SPaul Bohm {
4584e3a38431SPaul Bohm int active = ev_active (w);
4585e3a38431SPaul Bohm
4586e3a38431SPaul Bohm idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
4587e3a38431SPaul Bohm ev_active (idles [ABSPRI (w)][active - 1]) = active;
4588e3a38431SPaul Bohm
4589e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4590e3a38431SPaul Bohm --idleall;
4591e3a38431SPaul Bohm }
4592e3a38431SPaul Bohm
4593e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4594e3a38431SPaul Bohm }
4595e3a38431SPaul Bohm #endif
4596e3a38431SPaul Bohm
4597e3a38431SPaul Bohm #if EV_PREPARE_ENABLE
4598e3a38431SPaul Bohm void
ev_prepare_start(EV_P_ ev_prepare * w)4599*93823e6cSPaul Bohm ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
4600e3a38431SPaul Bohm {
4601e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4602e3a38431SPaul Bohm return;
4603e3a38431SPaul Bohm
4604e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4605e3a38431SPaul Bohm
4606e3a38431SPaul Bohm ev_start (EV_A_ (W)w, ++preparecnt);
4607e3a38431SPaul Bohm array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
4608e3a38431SPaul Bohm prepares [preparecnt - 1] = w;
4609e3a38431SPaul Bohm
4610e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4611e3a38431SPaul Bohm }
4612e3a38431SPaul Bohm
4613e3a38431SPaul Bohm void
ev_prepare_stop(EV_P_ ev_prepare * w)4614*93823e6cSPaul Bohm ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
4615e3a38431SPaul Bohm {
4616e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4617e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4618e3a38431SPaul Bohm return;
4619e3a38431SPaul Bohm
4620e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4621e3a38431SPaul Bohm
4622e3a38431SPaul Bohm {
4623e3a38431SPaul Bohm int active = ev_active (w);
4624e3a38431SPaul Bohm
4625e3a38431SPaul Bohm prepares [active - 1] = prepares [--preparecnt];
4626e3a38431SPaul Bohm ev_active (prepares [active - 1]) = active;
4627e3a38431SPaul Bohm }
4628e3a38431SPaul Bohm
4629e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4630e3a38431SPaul Bohm
4631e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4632e3a38431SPaul Bohm }
4633e3a38431SPaul Bohm #endif
4634e3a38431SPaul Bohm
4635e3a38431SPaul Bohm #if EV_CHECK_ENABLE
4636e3a38431SPaul Bohm void
ev_check_start(EV_P_ ev_check * w)4637*93823e6cSPaul Bohm ev_check_start (EV_P_ ev_check *w) EV_THROW
4638e3a38431SPaul Bohm {
4639e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4640e3a38431SPaul Bohm return;
4641e3a38431SPaul Bohm
4642e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4643e3a38431SPaul Bohm
4644e3a38431SPaul Bohm ev_start (EV_A_ (W)w, ++checkcnt);
4645e3a38431SPaul Bohm array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
4646e3a38431SPaul Bohm checks [checkcnt - 1] = w;
4647e3a38431SPaul Bohm
4648e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4649e3a38431SPaul Bohm }
4650e3a38431SPaul Bohm
4651e3a38431SPaul Bohm void
ev_check_stop(EV_P_ ev_check * w)4652*93823e6cSPaul Bohm ev_check_stop (EV_P_ ev_check *w) EV_THROW
4653e3a38431SPaul Bohm {
4654e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4655e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4656e3a38431SPaul Bohm return;
4657e3a38431SPaul Bohm
4658e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4659e3a38431SPaul Bohm
4660e3a38431SPaul Bohm {
4661e3a38431SPaul Bohm int active = ev_active (w);
4662e3a38431SPaul Bohm
4663e3a38431SPaul Bohm checks [active - 1] = checks [--checkcnt];
4664e3a38431SPaul Bohm ev_active (checks [active - 1]) = active;
4665e3a38431SPaul Bohm }
4666e3a38431SPaul Bohm
4667e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4668e3a38431SPaul Bohm
4669e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4670e3a38431SPaul Bohm }
4671e3a38431SPaul Bohm #endif
4672e3a38431SPaul Bohm
4673e3a38431SPaul Bohm #if EV_EMBED_ENABLE
4674e3a38431SPaul Bohm void noinline
ev_embed_sweep(EV_P_ ev_embed * w)4675*93823e6cSPaul Bohm ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
4676e3a38431SPaul Bohm {
4677e3a38431SPaul Bohm ev_run (w->other, EVRUN_NOWAIT);
4678e3a38431SPaul Bohm }
4679e3a38431SPaul Bohm
4680e3a38431SPaul Bohm static void
embed_io_cb(EV_P_ ev_io * io,int revents)4681e3a38431SPaul Bohm embed_io_cb (EV_P_ ev_io *io, int revents)
4682e3a38431SPaul Bohm {
4683e3a38431SPaul Bohm ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
4684e3a38431SPaul Bohm
4685e3a38431SPaul Bohm if (ev_cb (w))
4686e3a38431SPaul Bohm ev_feed_event (EV_A_ (W)w, EV_EMBED);
4687e3a38431SPaul Bohm else
4688e3a38431SPaul Bohm ev_run (w->other, EVRUN_NOWAIT);
4689e3a38431SPaul Bohm }
4690e3a38431SPaul Bohm
4691e3a38431SPaul Bohm static void
embed_prepare_cb(EV_P_ ev_prepare * prepare,int revents)4692e3a38431SPaul Bohm embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
4693e3a38431SPaul Bohm {
4694e3a38431SPaul Bohm ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
4695e3a38431SPaul Bohm
4696e3a38431SPaul Bohm {
4697e3a38431SPaul Bohm EV_P = w->other;
4698e3a38431SPaul Bohm
4699e3a38431SPaul Bohm while (fdchangecnt)
4700e3a38431SPaul Bohm {
4701e3a38431SPaul Bohm fd_reify (EV_A);
4702e3a38431SPaul Bohm ev_run (EV_A_ EVRUN_NOWAIT);
4703e3a38431SPaul Bohm }
4704e3a38431SPaul Bohm }
4705e3a38431SPaul Bohm }
4706e3a38431SPaul Bohm
4707e3a38431SPaul Bohm static void
embed_fork_cb(EV_P_ ev_fork * fork_w,int revents)4708e3a38431SPaul Bohm embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
4709e3a38431SPaul Bohm {
4710e3a38431SPaul Bohm ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
4711e3a38431SPaul Bohm
4712e3a38431SPaul Bohm ev_embed_stop (EV_A_ w);
4713e3a38431SPaul Bohm
4714e3a38431SPaul Bohm {
4715e3a38431SPaul Bohm EV_P = w->other;
4716e3a38431SPaul Bohm
4717e3a38431SPaul Bohm ev_loop_fork (EV_A);
4718e3a38431SPaul Bohm ev_run (EV_A_ EVRUN_NOWAIT);
4719e3a38431SPaul Bohm }
4720e3a38431SPaul Bohm
4721e3a38431SPaul Bohm ev_embed_start (EV_A_ w);
4722e3a38431SPaul Bohm }
4723e3a38431SPaul Bohm
4724e3a38431SPaul Bohm #if 0
4725e3a38431SPaul Bohm static void
4726e3a38431SPaul Bohm embed_idle_cb (EV_P_ ev_idle *idle, int revents)
4727e3a38431SPaul Bohm {
4728e3a38431SPaul Bohm ev_idle_stop (EV_A_ idle);
4729e3a38431SPaul Bohm }
4730e3a38431SPaul Bohm #endif
4731e3a38431SPaul Bohm
4732e3a38431SPaul Bohm void
ev_embed_start(EV_P_ ev_embed * w)4733*93823e6cSPaul Bohm ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4734e3a38431SPaul Bohm {
4735e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4736e3a38431SPaul Bohm return;
4737e3a38431SPaul Bohm
4738e3a38431SPaul Bohm {
4739e3a38431SPaul Bohm EV_P = w->other;
4740e3a38431SPaul Bohm assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
4741e3a38431SPaul Bohm ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
4742e3a38431SPaul Bohm }
4743e3a38431SPaul Bohm
4744e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4745e3a38431SPaul Bohm
4746e3a38431SPaul Bohm ev_set_priority (&w->io, ev_priority (w));
4747e3a38431SPaul Bohm ev_io_start (EV_A_ &w->io);
4748e3a38431SPaul Bohm
4749e3a38431SPaul Bohm ev_prepare_init (&w->prepare, embed_prepare_cb);
4750e3a38431SPaul Bohm ev_set_priority (&w->prepare, EV_MINPRI);
4751e3a38431SPaul Bohm ev_prepare_start (EV_A_ &w->prepare);
4752e3a38431SPaul Bohm
4753e3a38431SPaul Bohm ev_fork_init (&w->fork, embed_fork_cb);
4754e3a38431SPaul Bohm ev_fork_start (EV_A_ &w->fork);
4755e3a38431SPaul Bohm
4756e3a38431SPaul Bohm /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
4757e3a38431SPaul Bohm
4758e3a38431SPaul Bohm ev_start (EV_A_ (W)w, 1);
4759e3a38431SPaul Bohm
4760e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4761e3a38431SPaul Bohm }
4762e3a38431SPaul Bohm
4763e3a38431SPaul Bohm void
ev_embed_stop(EV_P_ ev_embed * w)4764*93823e6cSPaul Bohm ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4765e3a38431SPaul Bohm {
4766e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4767e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4768e3a38431SPaul Bohm return;
4769e3a38431SPaul Bohm
4770e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4771e3a38431SPaul Bohm
4772e3a38431SPaul Bohm ev_io_stop (EV_A_ &w->io);
4773e3a38431SPaul Bohm ev_prepare_stop (EV_A_ &w->prepare);
4774e3a38431SPaul Bohm ev_fork_stop (EV_A_ &w->fork);
4775e3a38431SPaul Bohm
4776e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4777e3a38431SPaul Bohm
4778e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4779e3a38431SPaul Bohm }
4780e3a38431SPaul Bohm #endif
4781e3a38431SPaul Bohm
4782e3a38431SPaul Bohm #if EV_FORK_ENABLE
4783e3a38431SPaul Bohm void
ev_fork_start(EV_P_ ev_fork * w)4784*93823e6cSPaul Bohm ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4785e3a38431SPaul Bohm {
4786e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4787e3a38431SPaul Bohm return;
4788e3a38431SPaul Bohm
4789e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4790e3a38431SPaul Bohm
4791e3a38431SPaul Bohm ev_start (EV_A_ (W)w, ++forkcnt);
4792e3a38431SPaul Bohm array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
4793e3a38431SPaul Bohm forks [forkcnt - 1] = w;
4794e3a38431SPaul Bohm
4795e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4796e3a38431SPaul Bohm }
4797e3a38431SPaul Bohm
4798e3a38431SPaul Bohm void
ev_fork_stop(EV_P_ ev_fork * w)4799*93823e6cSPaul Bohm ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4800e3a38431SPaul Bohm {
4801e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4802e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4803e3a38431SPaul Bohm return;
4804e3a38431SPaul Bohm
4805e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4806e3a38431SPaul Bohm
4807e3a38431SPaul Bohm {
4808e3a38431SPaul Bohm int active = ev_active (w);
4809e3a38431SPaul Bohm
4810e3a38431SPaul Bohm forks [active - 1] = forks [--forkcnt];
4811e3a38431SPaul Bohm ev_active (forks [active - 1]) = active;
4812e3a38431SPaul Bohm }
4813e3a38431SPaul Bohm
4814e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4815e3a38431SPaul Bohm
4816e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4817e3a38431SPaul Bohm }
4818e3a38431SPaul Bohm #endif
4819e3a38431SPaul Bohm
4820e3a38431SPaul Bohm #if EV_CLEANUP_ENABLE
4821e3a38431SPaul Bohm void
ev_cleanup_start(EV_P_ ev_cleanup * w)4822*93823e6cSPaul Bohm ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4823e3a38431SPaul Bohm {
4824e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4825e3a38431SPaul Bohm return;
4826e3a38431SPaul Bohm
4827e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4828e3a38431SPaul Bohm
4829e3a38431SPaul Bohm ev_start (EV_A_ (W)w, ++cleanupcnt);
4830e3a38431SPaul Bohm array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2);
4831e3a38431SPaul Bohm cleanups [cleanupcnt - 1] = w;
4832e3a38431SPaul Bohm
4833e3a38431SPaul Bohm /* cleanup watchers should never keep a refcount on the loop */
4834e3a38431SPaul Bohm ev_unref (EV_A);
4835e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4836e3a38431SPaul Bohm }
4837e3a38431SPaul Bohm
4838e3a38431SPaul Bohm void
ev_cleanup_stop(EV_P_ ev_cleanup * w)4839*93823e6cSPaul Bohm ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4840e3a38431SPaul Bohm {
4841e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4842e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4843e3a38431SPaul Bohm return;
4844e3a38431SPaul Bohm
4845e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4846e3a38431SPaul Bohm ev_ref (EV_A);
4847e3a38431SPaul Bohm
4848e3a38431SPaul Bohm {
4849e3a38431SPaul Bohm int active = ev_active (w);
4850e3a38431SPaul Bohm
4851e3a38431SPaul Bohm cleanups [active - 1] = cleanups [--cleanupcnt];
4852e3a38431SPaul Bohm ev_active (cleanups [active - 1]) = active;
4853e3a38431SPaul Bohm }
4854e3a38431SPaul Bohm
4855e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4856e3a38431SPaul Bohm
4857e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4858e3a38431SPaul Bohm }
4859e3a38431SPaul Bohm #endif
4860e3a38431SPaul Bohm
4861e3a38431SPaul Bohm #if EV_ASYNC_ENABLE
4862e3a38431SPaul Bohm void
ev_async_start(EV_P_ ev_async * w)4863*93823e6cSPaul Bohm ev_async_start (EV_P_ ev_async *w) EV_THROW
4864e3a38431SPaul Bohm {
4865e3a38431SPaul Bohm if (expect_false (ev_is_active (w)))
4866e3a38431SPaul Bohm return;
4867e3a38431SPaul Bohm
4868e3a38431SPaul Bohm w->sent = 0;
4869e3a38431SPaul Bohm
4870e3a38431SPaul Bohm evpipe_init (EV_A);
4871e3a38431SPaul Bohm
4872e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4873e3a38431SPaul Bohm
4874e3a38431SPaul Bohm ev_start (EV_A_ (W)w, ++asynccnt);
4875e3a38431SPaul Bohm array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2);
4876e3a38431SPaul Bohm asyncs [asynccnt - 1] = w;
4877e3a38431SPaul Bohm
4878e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4879e3a38431SPaul Bohm }
4880e3a38431SPaul Bohm
4881e3a38431SPaul Bohm void
ev_async_stop(EV_P_ ev_async * w)4882*93823e6cSPaul Bohm ev_async_stop (EV_P_ ev_async *w) EV_THROW
4883e3a38431SPaul Bohm {
4884e3a38431SPaul Bohm clear_pending (EV_A_ (W)w);
4885e3a38431SPaul Bohm if (expect_false (!ev_is_active (w)))
4886e3a38431SPaul Bohm return;
4887e3a38431SPaul Bohm
4888e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4889e3a38431SPaul Bohm
4890e3a38431SPaul Bohm {
4891e3a38431SPaul Bohm int active = ev_active (w);
4892e3a38431SPaul Bohm
4893e3a38431SPaul Bohm asyncs [active - 1] = asyncs [--asynccnt];
4894e3a38431SPaul Bohm ev_active (asyncs [active - 1]) = active;
4895e3a38431SPaul Bohm }
4896e3a38431SPaul Bohm
4897e3a38431SPaul Bohm ev_stop (EV_A_ (W)w);
4898e3a38431SPaul Bohm
4899e3a38431SPaul Bohm EV_FREQUENT_CHECK;
4900e3a38431SPaul Bohm }
4901e3a38431SPaul Bohm
4902e3a38431SPaul Bohm void
ev_async_send(EV_P_ ev_async * w)4903*93823e6cSPaul Bohm ev_async_send (EV_P_ ev_async *w) EV_THROW
4904e3a38431SPaul Bohm {
4905e3a38431SPaul Bohm w->sent = 1;
4906e3a38431SPaul Bohm evpipe_write (EV_A_ &async_pending);
4907e3a38431SPaul Bohm }
4908e3a38431SPaul Bohm #endif
4909e3a38431SPaul Bohm
4910e3a38431SPaul Bohm /*****************************************************************************/
4911e3a38431SPaul Bohm
4912e3a38431SPaul Bohm struct ev_once
4913e3a38431SPaul Bohm {
4914e3a38431SPaul Bohm ev_io io;
4915e3a38431SPaul Bohm ev_timer to;
4916e3a38431SPaul Bohm void (*cb)(int revents, void *arg);
4917e3a38431SPaul Bohm void *arg;
4918e3a38431SPaul Bohm };
4919e3a38431SPaul Bohm
4920e3a38431SPaul Bohm static void
once_cb(EV_P_ struct ev_once * once,int revents)4921e3a38431SPaul Bohm once_cb (EV_P_ struct ev_once *once, int revents)
4922e3a38431SPaul Bohm {
4923e3a38431SPaul Bohm void (*cb)(int revents, void *arg) = once->cb;
4924e3a38431SPaul Bohm void *arg = once->arg;
4925e3a38431SPaul Bohm
4926e3a38431SPaul Bohm ev_io_stop (EV_A_ &once->io);
4927e3a38431SPaul Bohm ev_timer_stop (EV_A_ &once->to);
4928e3a38431SPaul Bohm ev_free (once);
4929e3a38431SPaul Bohm
4930e3a38431SPaul Bohm cb (revents, arg);
4931e3a38431SPaul Bohm }
4932e3a38431SPaul Bohm
4933e3a38431SPaul Bohm static void
once_cb_io(EV_P_ ev_io * w,int revents)4934e3a38431SPaul Bohm once_cb_io (EV_P_ ev_io *w, int revents)
4935e3a38431SPaul Bohm {
4936e3a38431SPaul Bohm struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io));
4937e3a38431SPaul Bohm
4938e3a38431SPaul Bohm once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to));
4939e3a38431SPaul Bohm }
4940e3a38431SPaul Bohm
4941e3a38431SPaul Bohm static void
once_cb_to(EV_P_ ev_timer * w,int revents)4942e3a38431SPaul Bohm once_cb_to (EV_P_ ev_timer *w, int revents)
4943e3a38431SPaul Bohm {
4944e3a38431SPaul Bohm struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to));
4945e3a38431SPaul Bohm
4946e3a38431SPaul Bohm once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4947e3a38431SPaul Bohm }
4948e3a38431SPaul Bohm
4949e3a38431SPaul Bohm void
ev_once(EV_P_ int fd,int events,ev_tstamp timeout,void (* cb)(int revents,void * arg),void * arg)4950*93823e6cSPaul Bohm ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4951e3a38431SPaul Bohm {
4952e3a38431SPaul Bohm struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4953e3a38431SPaul Bohm
4954e3a38431SPaul Bohm if (expect_false (!once))
4955e3a38431SPaul Bohm {
4956e3a38431SPaul Bohm cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg);
4957e3a38431SPaul Bohm return;
4958e3a38431SPaul Bohm }
4959e3a38431SPaul Bohm
4960e3a38431SPaul Bohm once->cb = cb;
4961e3a38431SPaul Bohm once->arg = arg;
4962e3a38431SPaul Bohm
4963e3a38431SPaul Bohm ev_init (&once->io, once_cb_io);
4964e3a38431SPaul Bohm if (fd >= 0)
4965e3a38431SPaul Bohm {
4966e3a38431SPaul Bohm ev_io_set (&once->io, fd, events);
4967e3a38431SPaul Bohm ev_io_start (EV_A_ &once->io);
4968e3a38431SPaul Bohm }
4969e3a38431SPaul Bohm
4970e3a38431SPaul Bohm ev_init (&once->to, once_cb_to);
4971e3a38431SPaul Bohm if (timeout >= 0.)
4972e3a38431SPaul Bohm {
4973e3a38431SPaul Bohm ev_timer_set (&once->to, timeout, 0.);
4974e3a38431SPaul Bohm ev_timer_start (EV_A_ &once->to);
4975e3a38431SPaul Bohm }
4976e3a38431SPaul Bohm }
4977e3a38431SPaul Bohm
4978e3a38431SPaul Bohm /*****************************************************************************/
4979e3a38431SPaul Bohm
4980e3a38431SPaul Bohm #if EV_WALK_ENABLE
4981*93823e6cSPaul Bohm void ecb_cold
ev_walk(EV_P_ int types,void (* cb)(EV_P_ int type,void * w))4982*93823e6cSPaul Bohm ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4983e3a38431SPaul Bohm {
4984e3a38431SPaul Bohm int i, j;
4985e3a38431SPaul Bohm ev_watcher_list *wl, *wn;
4986e3a38431SPaul Bohm
4987e3a38431SPaul Bohm if (types & (EV_IO | EV_EMBED))
4988e3a38431SPaul Bohm for (i = 0; i < anfdmax; ++i)
4989e3a38431SPaul Bohm for (wl = anfds [i].head; wl; )
4990e3a38431SPaul Bohm {
4991e3a38431SPaul Bohm wn = wl->next;
4992e3a38431SPaul Bohm
4993e3a38431SPaul Bohm #if EV_EMBED_ENABLE
4994e3a38431SPaul Bohm if (ev_cb ((ev_io *)wl) == embed_io_cb)
4995e3a38431SPaul Bohm {
4996e3a38431SPaul Bohm if (types & EV_EMBED)
4997e3a38431SPaul Bohm cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io));
4998e3a38431SPaul Bohm }
4999e3a38431SPaul Bohm else
5000e3a38431SPaul Bohm #endif
5001e3a38431SPaul Bohm #if EV_USE_INOTIFY
5002e3a38431SPaul Bohm if (ev_cb ((ev_io *)wl) == infy_cb)
5003e3a38431SPaul Bohm ;
5004e3a38431SPaul Bohm else
5005e3a38431SPaul Bohm #endif
5006e3a38431SPaul Bohm if ((ev_io *)wl != &pipe_w)
5007e3a38431SPaul Bohm if (types & EV_IO)
5008e3a38431SPaul Bohm cb (EV_A_ EV_IO, wl);
5009e3a38431SPaul Bohm
5010e3a38431SPaul Bohm wl = wn;
5011e3a38431SPaul Bohm }
5012e3a38431SPaul Bohm
5013e3a38431SPaul Bohm if (types & (EV_TIMER | EV_STAT))
5014e3a38431SPaul Bohm for (i = timercnt + HEAP0; i-- > HEAP0; )
5015e3a38431SPaul Bohm #if EV_STAT_ENABLE
5016e3a38431SPaul Bohm /*TODO: timer is not always active*/
5017e3a38431SPaul Bohm if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb)
5018e3a38431SPaul Bohm {
5019e3a38431SPaul Bohm if (types & EV_STAT)
5020e3a38431SPaul Bohm cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer));
5021e3a38431SPaul Bohm }
5022e3a38431SPaul Bohm else
5023e3a38431SPaul Bohm #endif
5024e3a38431SPaul Bohm if (types & EV_TIMER)
5025e3a38431SPaul Bohm cb (EV_A_ EV_TIMER, ANHE_w (timers [i]));
5026e3a38431SPaul Bohm
5027e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE
5028e3a38431SPaul Bohm if (types & EV_PERIODIC)
5029e3a38431SPaul Bohm for (i = periodiccnt + HEAP0; i-- > HEAP0; )
5030e3a38431SPaul Bohm cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
5031e3a38431SPaul Bohm #endif
5032e3a38431SPaul Bohm
5033e3a38431SPaul Bohm #if EV_IDLE_ENABLE
5034e3a38431SPaul Bohm if (types & EV_IDLE)
5035*93823e6cSPaul Bohm for (j = NUMPRI; j--; )
5036e3a38431SPaul Bohm for (i = idlecnt [j]; i--; )
5037e3a38431SPaul Bohm cb (EV_A_ EV_IDLE, idles [j][i]);
5038e3a38431SPaul Bohm #endif
5039e3a38431SPaul Bohm
5040e3a38431SPaul Bohm #if EV_FORK_ENABLE
5041e3a38431SPaul Bohm if (types & EV_FORK)
5042e3a38431SPaul Bohm for (i = forkcnt; i--; )
5043e3a38431SPaul Bohm if (ev_cb (forks [i]) != embed_fork_cb)
5044e3a38431SPaul Bohm cb (EV_A_ EV_FORK, forks [i]);
5045e3a38431SPaul Bohm #endif
5046e3a38431SPaul Bohm
5047e3a38431SPaul Bohm #if EV_ASYNC_ENABLE
5048e3a38431SPaul Bohm if (types & EV_ASYNC)
5049e3a38431SPaul Bohm for (i = asynccnt; i--; )
5050e3a38431SPaul Bohm cb (EV_A_ EV_ASYNC, asyncs [i]);
5051e3a38431SPaul Bohm #endif
5052e3a38431SPaul Bohm
5053e3a38431SPaul Bohm #if EV_PREPARE_ENABLE
5054e3a38431SPaul Bohm if (types & EV_PREPARE)
5055e3a38431SPaul Bohm for (i = preparecnt; i--; )
5056e3a38431SPaul Bohm # if EV_EMBED_ENABLE
5057e3a38431SPaul Bohm if (ev_cb (prepares [i]) != embed_prepare_cb)
5058e3a38431SPaul Bohm # endif
5059e3a38431SPaul Bohm cb (EV_A_ EV_PREPARE, prepares [i]);
5060e3a38431SPaul Bohm #endif
5061e3a38431SPaul Bohm
5062e3a38431SPaul Bohm #if EV_CHECK_ENABLE
5063e3a38431SPaul Bohm if (types & EV_CHECK)
5064e3a38431SPaul Bohm for (i = checkcnt; i--; )
5065e3a38431SPaul Bohm cb (EV_A_ EV_CHECK, checks [i]);
5066e3a38431SPaul Bohm #endif
5067e3a38431SPaul Bohm
5068e3a38431SPaul Bohm #if EV_SIGNAL_ENABLE
5069e3a38431SPaul Bohm if (types & EV_SIGNAL)
5070e3a38431SPaul Bohm for (i = 0; i < EV_NSIG - 1; ++i)
5071e3a38431SPaul Bohm for (wl = signals [i].head; wl; )
5072e3a38431SPaul Bohm {
5073e3a38431SPaul Bohm wn = wl->next;
5074e3a38431SPaul Bohm cb (EV_A_ EV_SIGNAL, wl);
5075e3a38431SPaul Bohm wl = wn;
5076e3a38431SPaul Bohm }
5077e3a38431SPaul Bohm #endif
5078e3a38431SPaul Bohm
5079e3a38431SPaul Bohm #if EV_CHILD_ENABLE
5080e3a38431SPaul Bohm if (types & EV_CHILD)
5081e3a38431SPaul Bohm for (i = (EV_PID_HASHSIZE); i--; )
5082e3a38431SPaul Bohm for (wl = childs [i]; wl; )
5083e3a38431SPaul Bohm {
5084e3a38431SPaul Bohm wn = wl->next;
5085e3a38431SPaul Bohm cb (EV_A_ EV_CHILD, wl);
5086e3a38431SPaul Bohm wl = wn;
5087e3a38431SPaul Bohm }
5088e3a38431SPaul Bohm #endif
5089e3a38431SPaul Bohm /* EV_STAT 0x00001000 /* stat data changed */
5090e3a38431SPaul Bohm /* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
5091e3a38431SPaul Bohm }
5092e3a38431SPaul Bohm #endif
5093e3a38431SPaul Bohm
5094e3a38431SPaul Bohm #if EV_MULTIPLICITY
5095e3a38431SPaul Bohm #include "ev_wrap.h"
5096e3a38431SPaul Bohm #endif
5097e3a38431SPaul Bohm
5098