1 | /* |
1 | /* |
2 | * libev event processing core, watcher management |
2 | * libev event processing core, watcher management |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
35 | * and other provisions required by the GPL. If you do not delete the |
35 | * and other provisions required by the GPL. If you do not delete the |
36 | * provisions above, a recipient may use your version of this file under |
36 | * provisions above, a recipient may use your version of this file under |
37 | * either the BSD or the GPL. |
37 | * either the BSD or the GPL. |
38 | */ |
38 | */ |
39 | |
39 | |
40 | #ifdef __cplusplus |
|
|
41 | extern "C" { |
|
|
42 | #endif |
|
|
43 | |
|
|
44 | /* this big block deduces configuration from config.h */ |
40 | /* this big block deduces configuration from config.h */ |
45 | #ifndef EV_STANDALONE |
41 | #ifndef EV_STANDALONE |
46 | # ifdef EV_CONFIG_H |
42 | # ifdef EV_CONFIG_H |
47 | # include EV_CONFIG_H |
43 | # include EV_CONFIG_H |
48 | # else |
44 | # else |
49 | # include "config.h" |
45 | # include "config.h" |
50 | # endif |
46 | # endif |
|
|
47 | |
|
|
48 | #if HAVE_FLOOR |
|
|
49 | # ifndef EV_USE_FLOOR |
|
|
50 | # define EV_USE_FLOOR 1 |
|
|
51 | # endif |
|
|
52 | #endif |
51 | |
53 | |
52 | # if HAVE_CLOCK_SYSCALL |
54 | # if HAVE_CLOCK_SYSCALL |
53 | # ifndef EV_USE_CLOCK_SYSCALL |
55 | # ifndef EV_USE_CLOCK_SYSCALL |
54 | # define EV_USE_CLOCK_SYSCALL 1 |
56 | # define EV_USE_CLOCK_SYSCALL 1 |
55 | # ifndef EV_USE_REALTIME |
57 | # ifndef EV_USE_REALTIME |
… | |
… | |
77 | # ifndef EV_USE_REALTIME |
79 | # ifndef EV_USE_REALTIME |
78 | # define EV_USE_REALTIME 0 |
80 | # define EV_USE_REALTIME 0 |
79 | # endif |
81 | # endif |
80 | # endif |
82 | # endif |
81 | |
83 | |
|
|
84 | # if HAVE_NANOSLEEP |
82 | # ifndef EV_USE_NANOSLEEP |
85 | # ifndef EV_USE_NANOSLEEP |
83 | # if HAVE_NANOSLEEP |
|
|
84 | # define EV_USE_NANOSLEEP 1 |
86 | # define EV_USE_NANOSLEEP EV_FEATURE_OS |
|
|
87 | # endif |
85 | # else |
88 | # else |
|
|
89 | # undef EV_USE_NANOSLEEP |
86 | # define EV_USE_NANOSLEEP 0 |
90 | # define EV_USE_NANOSLEEP 0 |
|
|
91 | # endif |
|
|
92 | |
|
|
93 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
94 | # ifndef EV_USE_SELECT |
|
|
95 | # define EV_USE_SELECT EV_FEATURE_BACKENDS |
87 | # endif |
96 | # endif |
|
|
97 | # else |
|
|
98 | # undef EV_USE_SELECT |
|
|
99 | # define EV_USE_SELECT 0 |
88 | # endif |
100 | # endif |
89 | |
101 | |
|
|
102 | # if HAVE_POLL && HAVE_POLL_H |
90 | # ifndef EV_USE_SELECT |
103 | # ifndef EV_USE_POLL |
91 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
104 | # define EV_USE_POLL EV_FEATURE_BACKENDS |
92 | # define EV_USE_SELECT 1 |
|
|
93 | # else |
|
|
94 | # define EV_USE_SELECT 0 |
|
|
95 | # endif |
105 | # endif |
96 | # endif |
|
|
97 | |
|
|
98 | # ifndef EV_USE_POLL |
|
|
99 | # if HAVE_POLL && HAVE_POLL_H |
|
|
100 | # define EV_USE_POLL 1 |
|
|
101 | # else |
106 | # else |
|
|
107 | # undef EV_USE_POLL |
102 | # define EV_USE_POLL 0 |
108 | # define EV_USE_POLL 0 |
103 | # endif |
|
|
104 | # endif |
109 | # endif |
105 | |
110 | |
106 | # ifndef EV_USE_EPOLL |
|
|
107 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
111 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
108 | # define EV_USE_EPOLL 1 |
112 | # ifndef EV_USE_EPOLL |
109 | # else |
113 | # define EV_USE_EPOLL EV_FEATURE_BACKENDS |
110 | # define EV_USE_EPOLL 0 |
|
|
111 | # endif |
114 | # endif |
|
|
115 | # else |
|
|
116 | # undef EV_USE_EPOLL |
|
|
117 | # define EV_USE_EPOLL 0 |
112 | # endif |
118 | # endif |
113 | |
119 | |
|
|
120 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H |
114 | # ifndef EV_USE_KQUEUE |
121 | # ifndef EV_USE_KQUEUE |
115 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
122 | # define EV_USE_KQUEUE EV_FEATURE_BACKENDS |
116 | # define EV_USE_KQUEUE 1 |
|
|
117 | # else |
|
|
118 | # define EV_USE_KQUEUE 0 |
|
|
119 | # endif |
123 | # endif |
|
|
124 | # else |
|
|
125 | # undef EV_USE_KQUEUE |
|
|
126 | # define EV_USE_KQUEUE 0 |
120 | # endif |
127 | # endif |
121 | |
128 | |
122 | # ifndef EV_USE_PORT |
|
|
123 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
129 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
124 | # define EV_USE_PORT 1 |
130 | # ifndef EV_USE_PORT |
125 | # else |
131 | # define EV_USE_PORT EV_FEATURE_BACKENDS |
126 | # define EV_USE_PORT 0 |
|
|
127 | # endif |
132 | # endif |
|
|
133 | # else |
|
|
134 | # undef EV_USE_PORT |
|
|
135 | # define EV_USE_PORT 0 |
128 | # endif |
136 | # endif |
129 | |
137 | |
130 | # ifndef EV_USE_INOTIFY |
|
|
131 | # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H |
138 | # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H |
132 | # define EV_USE_INOTIFY 1 |
139 | # ifndef EV_USE_INOTIFY |
133 | # else |
|
|
134 | # define EV_USE_INOTIFY 0 |
140 | # define EV_USE_INOTIFY EV_FEATURE_OS |
135 | # endif |
141 | # endif |
|
|
142 | # else |
|
|
143 | # undef EV_USE_INOTIFY |
|
|
144 | # define EV_USE_INOTIFY 0 |
136 | # endif |
145 | # endif |
137 | |
146 | |
138 | # ifndef EV_USE_SIGNALFD |
|
|
139 | # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H |
147 | # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H |
140 | # define EV_USE_SIGNALFD 1 |
148 | # ifndef EV_USE_SIGNALFD |
141 | # else |
|
|
142 | # define EV_USE_SIGNALFD 0 |
149 | # define EV_USE_SIGNALFD EV_FEATURE_OS |
143 | # endif |
150 | # endif |
|
|
151 | # else |
|
|
152 | # undef EV_USE_SIGNALFD |
|
|
153 | # define EV_USE_SIGNALFD 0 |
144 | # endif |
154 | # endif |
145 | |
155 | |
|
|
156 | # if HAVE_EVENTFD |
146 | # ifndef EV_USE_EVENTFD |
157 | # ifndef EV_USE_EVENTFD |
147 | # if HAVE_EVENTFD |
|
|
148 | # define EV_USE_EVENTFD 1 |
158 | # define EV_USE_EVENTFD EV_FEATURE_OS |
149 | # else |
|
|
150 | # define EV_USE_EVENTFD 0 |
|
|
151 | # endif |
159 | # endif |
|
|
160 | # else |
|
|
161 | # undef EV_USE_EVENTFD |
|
|
162 | # define EV_USE_EVENTFD 0 |
152 | # endif |
163 | # endif |
153 | |
164 | |
154 | #endif |
165 | #endif |
155 | |
166 | |
156 | #include <math.h> |
|
|
157 | #include <stdlib.h> |
167 | #include <stdlib.h> |
|
|
168 | #include <string.h> |
158 | #include <fcntl.h> |
169 | #include <fcntl.h> |
159 | #include <stddef.h> |
170 | #include <stddef.h> |
160 | |
171 | |
161 | #include <stdio.h> |
172 | #include <stdio.h> |
162 | |
173 | |
163 | #include <assert.h> |
174 | #include <assert.h> |
164 | #include <errno.h> |
175 | #include <errno.h> |
165 | #include <sys/types.h> |
176 | #include <sys/types.h> |
166 | #include <time.h> |
177 | #include <time.h> |
|
|
178 | #include <limits.h> |
167 | |
179 | |
168 | #include <signal.h> |
180 | #include <signal.h> |
169 | |
181 | |
170 | #ifdef EV_H |
182 | #ifdef EV_H |
171 | # include EV_H |
183 | # include EV_H |
172 | #else |
184 | #else |
173 | # include "ev.h" |
185 | # include "ev.h" |
174 | #endif |
186 | #endif |
|
|
187 | |
|
|
188 | EV_CPP(extern "C" {) |
175 | |
189 | |
176 | #ifndef _WIN32 |
190 | #ifndef _WIN32 |
177 | # include <sys/time.h> |
191 | # include <sys/time.h> |
178 | # include <sys/wait.h> |
192 | # include <sys/wait.h> |
179 | # include <unistd.h> |
193 | # include <unistd.h> |
… | |
… | |
182 | # define WIN32_LEAN_AND_MEAN |
196 | # define WIN32_LEAN_AND_MEAN |
183 | # include <windows.h> |
197 | # include <windows.h> |
184 | # ifndef EV_SELECT_IS_WINSOCKET |
198 | # ifndef EV_SELECT_IS_WINSOCKET |
185 | # define EV_SELECT_IS_WINSOCKET 1 |
199 | # define EV_SELECT_IS_WINSOCKET 1 |
186 | # endif |
200 | # endif |
|
|
201 | # undef EV_AVOID_STDIO |
187 | #endif |
202 | #endif |
|
|
203 | |
|
|
204 | /* OS X, in its infinite idiocy, actually HARDCODES |
|
|
205 | * a limit of 1024 into their select. Where people have brains, |
|
|
206 | * OS X engineers apparently have a vacuum. Or maybe they were |
|
|
207 | * ordered to have a vacuum, or they do anything for money. |
|
|
208 | * This might help. Or not. |
|
|
209 | */ |
|
|
210 | #define _DARWIN_UNLIMITED_SELECT 1 |
188 | |
211 | |
189 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
212 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
190 | |
213 | |
191 | /* try to deduce the maximum number of signals on this platform */ |
214 | /* try to deduce the maximum number of signals on this platform */ |
192 | #if defined (EV_NSIG) |
215 | #if defined (EV_NSIG) |
… | |
… | |
204 | #elif defined (MAXSIG) |
227 | #elif defined (MAXSIG) |
205 | # define EV_NSIG (MAXSIG+1) |
228 | # define EV_NSIG (MAXSIG+1) |
206 | #elif defined (MAX_SIG) |
229 | #elif defined (MAX_SIG) |
207 | # define EV_NSIG (MAX_SIG+1) |
230 | # define EV_NSIG (MAX_SIG+1) |
208 | #elif defined (SIGARRAYSIZE) |
231 | #elif defined (SIGARRAYSIZE) |
209 | # define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ |
232 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
210 | #elif defined (_sys_nsig) |
233 | #elif defined (_sys_nsig) |
211 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
234 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
212 | #else |
235 | #else |
213 | # error "unable to find value for NSIG, please report" |
236 | # error "unable to find value for NSIG, please report" |
214 | /* to make it compile regardless, just remove the above line */ |
237 | /* to make it compile regardless, just remove the above line, */ |
|
|
238 | /* but consider reporting it, too! :) */ |
215 | # define EV_NSIG 65 |
239 | # define EV_NSIG 65 |
|
|
240 | #endif |
|
|
241 | |
|
|
242 | #ifndef EV_USE_FLOOR |
|
|
243 | # define EV_USE_FLOOR 0 |
216 | #endif |
244 | #endif |
217 | |
245 | |
218 | #ifndef EV_USE_CLOCK_SYSCALL |
246 | #ifndef EV_USE_CLOCK_SYSCALL |
219 | # if __linux && __GLIBC__ >= 2 |
247 | # if __linux && __GLIBC__ >= 2 |
220 | # define EV_USE_CLOCK_SYSCALL 1 |
248 | # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS |
221 | # else |
249 | # else |
222 | # define EV_USE_CLOCK_SYSCALL 0 |
250 | # define EV_USE_CLOCK_SYSCALL 0 |
223 | # endif |
251 | # endif |
224 | #endif |
252 | #endif |
225 | |
253 | |
226 | #ifndef EV_USE_MONOTONIC |
254 | #ifndef EV_USE_MONOTONIC |
227 | # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
255 | # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
228 | # define EV_USE_MONOTONIC 1 |
256 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
229 | # else |
257 | # else |
230 | # define EV_USE_MONOTONIC 0 |
258 | # define EV_USE_MONOTONIC 0 |
231 | # endif |
259 | # endif |
232 | #endif |
260 | #endif |
233 | |
261 | |
… | |
… | |
235 | # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL |
263 | # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL |
236 | #endif |
264 | #endif |
237 | |
265 | |
238 | #ifndef EV_USE_NANOSLEEP |
266 | #ifndef EV_USE_NANOSLEEP |
239 | # if _POSIX_C_SOURCE >= 199309L |
267 | # if _POSIX_C_SOURCE >= 199309L |
240 | # define EV_USE_NANOSLEEP 1 |
268 | # define EV_USE_NANOSLEEP EV_FEATURE_OS |
241 | # else |
269 | # else |
242 | # define EV_USE_NANOSLEEP 0 |
270 | # define EV_USE_NANOSLEEP 0 |
243 | # endif |
271 | # endif |
244 | #endif |
272 | #endif |
245 | |
273 | |
246 | #ifndef EV_USE_SELECT |
274 | #ifndef EV_USE_SELECT |
247 | # define EV_USE_SELECT 1 |
275 | # define EV_USE_SELECT EV_FEATURE_BACKENDS |
248 | #endif |
276 | #endif |
249 | |
277 | |
250 | #ifndef EV_USE_POLL |
278 | #ifndef EV_USE_POLL |
251 | # ifdef _WIN32 |
279 | # ifdef _WIN32 |
252 | # define EV_USE_POLL 0 |
280 | # define EV_USE_POLL 0 |
253 | # else |
281 | # else |
254 | # define EV_USE_POLL 1 |
282 | # define EV_USE_POLL EV_FEATURE_BACKENDS |
255 | # endif |
283 | # endif |
256 | #endif |
284 | #endif |
257 | |
285 | |
258 | #ifndef EV_USE_EPOLL |
286 | #ifndef EV_USE_EPOLL |
259 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
287 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
260 | # define EV_USE_EPOLL 1 |
288 | # define EV_USE_EPOLL EV_FEATURE_BACKENDS |
261 | # else |
289 | # else |
262 | # define EV_USE_EPOLL 0 |
290 | # define EV_USE_EPOLL 0 |
263 | # endif |
291 | # endif |
264 | #endif |
292 | #endif |
265 | |
293 | |
… | |
… | |
271 | # define EV_USE_PORT 0 |
299 | # define EV_USE_PORT 0 |
272 | #endif |
300 | #endif |
273 | |
301 | |
274 | #ifndef EV_USE_INOTIFY |
302 | #ifndef EV_USE_INOTIFY |
275 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
303 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
276 | # define EV_USE_INOTIFY 1 |
304 | # define EV_USE_INOTIFY EV_FEATURE_OS |
277 | # else |
305 | # else |
278 | # define EV_USE_INOTIFY 0 |
306 | # define EV_USE_INOTIFY 0 |
279 | # endif |
307 | # endif |
280 | #endif |
308 | #endif |
281 | |
309 | |
282 | #ifndef EV_PID_HASHSIZE |
310 | #ifndef EV_PID_HASHSIZE |
283 | # if EV_MINIMAL |
311 | # define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1 |
284 | # define EV_PID_HASHSIZE 1 |
|
|
285 | # else |
|
|
286 | # define EV_PID_HASHSIZE 16 |
|
|
287 | # endif |
|
|
288 | #endif |
312 | #endif |
289 | |
313 | |
290 | #ifndef EV_INOTIFY_HASHSIZE |
314 | #ifndef EV_INOTIFY_HASHSIZE |
291 | # if EV_MINIMAL |
315 | # define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1 |
292 | # define EV_INOTIFY_HASHSIZE 1 |
|
|
293 | # else |
|
|
294 | # define EV_INOTIFY_HASHSIZE 16 |
|
|
295 | # endif |
|
|
296 | #endif |
316 | #endif |
297 | |
317 | |
298 | #ifndef EV_USE_EVENTFD |
318 | #ifndef EV_USE_EVENTFD |
299 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
319 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
300 | # define EV_USE_EVENTFD 1 |
320 | # define EV_USE_EVENTFD EV_FEATURE_OS |
301 | # else |
321 | # else |
302 | # define EV_USE_EVENTFD 0 |
322 | # define EV_USE_EVENTFD 0 |
303 | # endif |
323 | # endif |
304 | #endif |
324 | #endif |
305 | |
325 | |
306 | #ifndef EV_USE_SIGNALFD |
326 | #ifndef EV_USE_SIGNALFD |
307 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
327 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
308 | # define EV_USE_SIGNALFD 1 |
328 | # define EV_USE_SIGNALFD EV_FEATURE_OS |
309 | # else |
329 | # else |
310 | # define EV_USE_SIGNALFD 0 |
330 | # define EV_USE_SIGNALFD 0 |
311 | # endif |
331 | # endif |
312 | #endif |
332 | #endif |
313 | |
333 | |
… | |
… | |
316 | # define EV_USE_4HEAP 1 |
336 | # define EV_USE_4HEAP 1 |
317 | # define EV_HEAP_CACHE_AT 1 |
337 | # define EV_HEAP_CACHE_AT 1 |
318 | #endif |
338 | #endif |
319 | |
339 | |
320 | #ifndef EV_VERIFY |
340 | #ifndef EV_VERIFY |
321 | # define EV_VERIFY !EV_MINIMAL |
341 | # define EV_VERIFY (EV_FEATURE_API ? 1 : 0) |
322 | #endif |
342 | #endif |
323 | |
343 | |
324 | #ifndef EV_USE_4HEAP |
344 | #ifndef EV_USE_4HEAP |
325 | # define EV_USE_4HEAP !EV_MINIMAL |
345 | # define EV_USE_4HEAP EV_FEATURE_DATA |
326 | #endif |
346 | #endif |
327 | |
347 | |
328 | #ifndef EV_HEAP_CACHE_AT |
348 | #ifndef EV_HEAP_CACHE_AT |
329 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
349 | # define EV_HEAP_CACHE_AT EV_FEATURE_DATA |
330 | #endif |
350 | #endif |
331 | |
351 | |
332 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
352 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
333 | /* which makes programs even slower. might work on other unices, too. */ |
353 | /* which makes programs even slower. might work on other unices, too. */ |
334 | #if EV_USE_CLOCK_SYSCALL |
354 | #if EV_USE_CLOCK_SYSCALL |
… | |
… | |
343 | # endif |
363 | # endif |
344 | #endif |
364 | #endif |
345 | |
365 | |
346 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
366 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
347 | |
367 | |
|
|
368 | #ifdef _AIX |
|
|
369 | /* AIX has a completely broken poll.h header */ |
|
|
370 | # undef EV_USE_POLL |
|
|
371 | # define EV_USE_POLL 0 |
|
|
372 | #endif |
|
|
373 | |
348 | #ifndef CLOCK_MONOTONIC |
374 | #ifndef CLOCK_MONOTONIC |
349 | # undef EV_USE_MONOTONIC |
375 | # undef EV_USE_MONOTONIC |
350 | # define EV_USE_MONOTONIC 0 |
376 | # define EV_USE_MONOTONIC 0 |
351 | #endif |
377 | #endif |
352 | |
378 | |
… | |
… | |
359 | # undef EV_USE_INOTIFY |
385 | # undef EV_USE_INOTIFY |
360 | # define EV_USE_INOTIFY 0 |
386 | # define EV_USE_INOTIFY 0 |
361 | #endif |
387 | #endif |
362 | |
388 | |
363 | #if !EV_USE_NANOSLEEP |
389 | #if !EV_USE_NANOSLEEP |
364 | # ifndef _WIN32 |
390 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
|
|
391 | # if !defined(_WIN32) && !defined(__hpux) |
365 | # include <sys/select.h> |
392 | # include <sys/select.h> |
366 | # endif |
393 | # endif |
367 | #endif |
394 | #endif |
368 | |
395 | |
369 | #if EV_USE_INOTIFY |
396 | #if EV_USE_INOTIFY |
370 | # include <sys/utsname.h> |
|
|
371 | # include <sys/statfs.h> |
397 | # include <sys/statfs.h> |
372 | # include <sys/inotify.h> |
398 | # include <sys/inotify.h> |
373 | /* some very old inotify.h headers don't have IN_DONT_FOLLOW */ |
399 | /* some very old inotify.h headers don't have IN_DONT_FOLLOW */ |
374 | # ifndef IN_DONT_FOLLOW |
400 | # ifndef IN_DONT_FOLLOW |
375 | # undef EV_USE_INOTIFY |
401 | # undef EV_USE_INOTIFY |
… | |
… | |
392 | # define EFD_CLOEXEC O_CLOEXEC |
418 | # define EFD_CLOEXEC O_CLOEXEC |
393 | # else |
419 | # else |
394 | # define EFD_CLOEXEC 02000000 |
420 | # define EFD_CLOEXEC 02000000 |
395 | # endif |
421 | # endif |
396 | # endif |
422 | # endif |
397 | # ifdef __cplusplus |
|
|
398 | extern "C" { |
|
|
399 | # endif |
|
|
400 | int eventfd (unsigned int initval, int flags); |
423 | EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); |
401 | # ifdef __cplusplus |
|
|
402 | } |
|
|
403 | # endif |
|
|
404 | #endif |
424 | #endif |
405 | |
425 | |
406 | #if EV_USE_SIGNALFD |
426 | #if EV_USE_SIGNALFD |
407 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
427 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
408 | # include <stdint.h> |
428 | # include <stdint.h> |
… | |
… | |
414 | # define SFD_CLOEXEC O_CLOEXEC |
434 | # define SFD_CLOEXEC O_CLOEXEC |
415 | # else |
435 | # else |
416 | # define SFD_CLOEXEC 02000000 |
436 | # define SFD_CLOEXEC 02000000 |
417 | # endif |
437 | # endif |
418 | # endif |
438 | # endif |
419 | # ifdef __cplusplus |
|
|
420 | extern "C" { |
|
|
421 | # endif |
|
|
422 | int signalfd (int fd, const sigset_t *mask, int flags); |
439 | EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags); |
423 | |
440 | |
424 | struct signalfd_siginfo |
441 | struct signalfd_siginfo |
425 | { |
442 | { |
426 | uint32_t ssi_signo; |
443 | uint32_t ssi_signo; |
427 | char pad[128 - sizeof (uint32_t)]; |
444 | char pad[128 - sizeof (uint32_t)]; |
428 | }; |
445 | }; |
429 | # ifdef __cplusplus |
|
|
430 | } |
|
|
431 | # endif |
446 | #endif |
432 | #endif |
|
|
433 | |
|
|
434 | |
447 | |
435 | /**/ |
448 | /**/ |
436 | |
449 | |
437 | #if EV_VERIFY >= 3 |
450 | #if EV_VERIFY >= 3 |
438 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
451 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
439 | #else |
452 | #else |
440 | # define EV_FREQUENT_CHECK do { } while (0) |
453 | # define EV_FREQUENT_CHECK do { } while (0) |
441 | #endif |
454 | #endif |
442 | |
455 | |
443 | /* |
456 | /* |
444 | * This is used to avoid floating point rounding problems. |
457 | * This is used to work around floating point rounding problems. |
445 | * It is added to ev_rt_now when scheduling periodics |
|
|
446 | * to ensure progress, time-wise, even when rounding |
|
|
447 | * errors are against us. |
|
|
448 | * This value is good at least till the year 4000. |
458 | * This value is good at least till the year 4000. |
449 | * Better solutions welcome. |
|
|
450 | */ |
459 | */ |
451 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
460 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
|
|
461 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
452 | |
462 | |
453 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
463 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
454 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
464 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
455 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ |
465 | |
|
|
466 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
|
|
467 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
456 | |
468 | |
457 | #if __GNUC__ >= 4 |
469 | #if __GNUC__ >= 4 |
458 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
470 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
459 | # define noinline __attribute__ ((noinline)) |
471 | # define noinline __attribute__ ((noinline)) |
460 | #else |
472 | #else |
… | |
… | |
467 | |
479 | |
468 | #define expect_false(expr) expect ((expr) != 0, 0) |
480 | #define expect_false(expr) expect ((expr) != 0, 0) |
469 | #define expect_true(expr) expect ((expr) != 0, 1) |
481 | #define expect_true(expr) expect ((expr) != 0, 1) |
470 | #define inline_size static inline |
482 | #define inline_size static inline |
471 | |
483 | |
472 | #if EV_MINIMAL |
484 | #if EV_FEATURE_CODE |
|
|
485 | # define inline_speed static inline |
|
|
486 | #else |
473 | # define inline_speed static noinline |
487 | # define inline_speed static noinline |
474 | #else |
|
|
475 | # define inline_speed static inline |
|
|
476 | #endif |
488 | #endif |
477 | |
489 | |
478 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
490 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
479 | |
491 | |
480 | #if EV_MINPRI == EV_MAXPRI |
492 | #if EV_MINPRI == EV_MAXPRI |
… | |
… | |
493 | #define ev_active(w) ((W)(w))->active |
505 | #define ev_active(w) ((W)(w))->active |
494 | #define ev_at(w) ((WT)(w))->at |
506 | #define ev_at(w) ((WT)(w))->at |
495 | |
507 | |
496 | #if EV_USE_REALTIME |
508 | #if EV_USE_REALTIME |
497 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
509 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
498 | /* giving it a reasonably high chance of working on typical architetcures */ |
510 | /* giving it a reasonably high chance of working on typical architectures */ |
499 | static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ |
511 | static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ |
500 | #endif |
512 | #endif |
501 | |
513 | |
502 | #if EV_USE_MONOTONIC |
514 | #if EV_USE_MONOTONIC |
503 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
515 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
… | |
… | |
505 | |
517 | |
506 | #ifndef EV_FD_TO_WIN32_HANDLE |
518 | #ifndef EV_FD_TO_WIN32_HANDLE |
507 | # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) |
519 | # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) |
508 | #endif |
520 | #endif |
509 | #ifndef EV_WIN32_HANDLE_TO_FD |
521 | #ifndef EV_WIN32_HANDLE_TO_FD |
510 | # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0) |
522 | # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) |
511 | #endif |
523 | #endif |
512 | #ifndef EV_WIN32_CLOSE_FD |
524 | #ifndef EV_WIN32_CLOSE_FD |
513 | # define EV_WIN32_CLOSE_FD(fd) close (fd) |
525 | # define EV_WIN32_CLOSE_FD(fd) close (fd) |
514 | #endif |
526 | #endif |
515 | |
527 | |
516 | #ifdef _WIN32 |
528 | #ifdef _WIN32 |
517 | # include "ev_win32.c" |
529 | # include "ev_win32.c" |
518 | #endif |
530 | #endif |
519 | |
531 | |
520 | /*****************************************************************************/ |
532 | /*****************************************************************************/ |
|
|
533 | |
|
|
534 | /* define a suitable floor function (only used by periodics atm) */ |
|
|
535 | |
|
|
536 | #if EV_USE_FLOOR |
|
|
537 | # include <math.h> |
|
|
538 | # define ev_floor(v) floor (v) |
|
|
539 | #else |
|
|
540 | |
|
|
541 | #include <float.h> |
|
|
542 | |
|
|
543 | /* a floor() replacement function, should be independent of ev_tstamp type */ |
|
|
544 | static ev_tstamp noinline |
|
|
545 | ev_floor (ev_tstamp v) |
|
|
546 | { |
|
|
547 | /* the choice of shift factor is not terribly important */ |
|
|
548 | #if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */ |
|
|
549 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
|
|
550 | #else |
|
|
551 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
|
|
552 | #endif |
|
|
553 | |
|
|
554 | /* argument too large for an unsigned long? */ |
|
|
555 | if (expect_false (v >= shift)) |
|
|
556 | { |
|
|
557 | ev_tstamp f; |
|
|
558 | |
|
|
559 | if (v == v - 1.) |
|
|
560 | return v; /* very large number */ |
|
|
561 | |
|
|
562 | f = shift * ev_floor (v * (1. / shift)); |
|
|
563 | return f + ev_floor (v - f); |
|
|
564 | } |
|
|
565 | |
|
|
566 | /* special treatment for negative args? */ |
|
|
567 | if (expect_false (v < 0.)) |
|
|
568 | { |
|
|
569 | ev_tstamp f = -ev_floor (-v); |
|
|
570 | |
|
|
571 | return f - (f == v ? 0 : 1); |
|
|
572 | } |
|
|
573 | |
|
|
574 | /* fits into an unsigned long */ |
|
|
575 | return (unsigned long)v; |
|
|
576 | } |
|
|
577 | |
|
|
578 | #endif |
|
|
579 | |
|
|
580 | /*****************************************************************************/ |
|
|
581 | |
|
|
582 | #ifdef __linux |
|
|
583 | # include <sys/utsname.h> |
|
|
584 | #endif |
|
|
585 | |
|
|
586 | static unsigned int noinline |
|
|
587 | ev_linux_version (void) |
|
|
588 | { |
|
|
589 | #ifdef __linux |
|
|
590 | unsigned int v = 0; |
|
|
591 | struct utsname buf; |
|
|
592 | int i; |
|
|
593 | char *p = buf.release; |
|
|
594 | |
|
|
595 | if (uname (&buf)) |
|
|
596 | return 0; |
|
|
597 | |
|
|
598 | for (i = 3+1; --i; ) |
|
|
599 | { |
|
|
600 | unsigned int c = 0; |
|
|
601 | |
|
|
602 | for (;;) |
|
|
603 | { |
|
|
604 | if (*p >= '0' && *p <= '9') |
|
|
605 | c = c * 10 + *p++ - '0'; |
|
|
606 | else |
|
|
607 | { |
|
|
608 | p += *p == '.'; |
|
|
609 | break; |
|
|
610 | } |
|
|
611 | } |
|
|
612 | |
|
|
613 | v = (v << 8) | c; |
|
|
614 | } |
|
|
615 | |
|
|
616 | return v; |
|
|
617 | #else |
|
|
618 | return 0; |
|
|
619 | #endif |
|
|
620 | } |
|
|
621 | |
|
|
622 | /*****************************************************************************/ |
|
|
623 | |
|
|
624 | #if EV_AVOID_STDIO |
|
|
625 | static void noinline |
|
|
626 | ev_printerr (const char *msg) |
|
|
627 | { |
|
|
628 | write (STDERR_FILENO, msg, strlen (msg)); |
|
|
629 | } |
|
|
630 | #endif |
521 | |
631 | |
522 | static void (*syserr_cb)(const char *msg); |
632 | static void (*syserr_cb)(const char *msg); |
523 | |
633 | |
524 | void |
634 | void |
525 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
635 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
… | |
… | |
535 | |
645 | |
536 | if (syserr_cb) |
646 | if (syserr_cb) |
537 | syserr_cb (msg); |
647 | syserr_cb (msg); |
538 | else |
648 | else |
539 | { |
649 | { |
|
|
650 | #if EV_AVOID_STDIO |
|
|
651 | ev_printerr (msg); |
|
|
652 | ev_printerr (": "); |
|
|
653 | ev_printerr (strerror (errno)); |
|
|
654 | ev_printerr ("\n"); |
|
|
655 | #else |
540 | perror (msg); |
656 | perror (msg); |
|
|
657 | #endif |
541 | abort (); |
658 | abort (); |
542 | } |
659 | } |
543 | } |
660 | } |
544 | |
661 | |
545 | static void * |
662 | static void * |
546 | ev_realloc_emul (void *ptr, long size) |
663 | ev_realloc_emul (void *ptr, long size) |
547 | { |
664 | { |
|
|
665 | #if __GLIBC__ |
|
|
666 | return realloc (ptr, size); |
|
|
667 | #else |
548 | /* some systems, notably openbsd and darwin, fail to properly |
668 | /* some systems, notably openbsd and darwin, fail to properly |
549 | * implement realloc (x, 0) (as required by both ansi c-98 and |
669 | * implement realloc (x, 0) (as required by both ansi c-89 and |
550 | * the single unix specification, so work around them here. |
670 | * the single unix specification, so work around them here. |
551 | */ |
671 | */ |
552 | |
672 | |
553 | if (size) |
673 | if (size) |
554 | return realloc (ptr, size); |
674 | return realloc (ptr, size); |
555 | |
675 | |
556 | free (ptr); |
676 | free (ptr); |
557 | return 0; |
677 | return 0; |
|
|
678 | #endif |
558 | } |
679 | } |
559 | |
680 | |
560 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
681 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
561 | |
682 | |
562 | void |
683 | void |
… | |
… | |
570 | { |
691 | { |
571 | ptr = alloc (ptr, size); |
692 | ptr = alloc (ptr, size); |
572 | |
693 | |
573 | if (!ptr && size) |
694 | if (!ptr && size) |
574 | { |
695 | { |
|
|
696 | #if EV_AVOID_STDIO |
|
|
697 | ev_printerr ("(libev) memory allocation failed, aborting.\n"); |
|
|
698 | #else |
575 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
699 | fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); |
|
|
700 | #endif |
576 | abort (); |
701 | abort (); |
577 | } |
702 | } |
578 | |
703 | |
579 | return ptr; |
704 | return ptr; |
580 | } |
705 | } |
… | |
… | |
596 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
721 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
597 | unsigned char unused; |
722 | unsigned char unused; |
598 | #if EV_USE_EPOLL |
723 | #if EV_USE_EPOLL |
599 | unsigned int egen; /* generation counter to counter epoll bugs */ |
724 | unsigned int egen; /* generation counter to counter epoll bugs */ |
600 | #endif |
725 | #endif |
601 | #if EV_SELECT_IS_WINSOCKET |
726 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
602 | SOCKET handle; |
727 | SOCKET handle; |
|
|
728 | #endif |
|
|
729 | #if EV_USE_IOCP |
|
|
730 | OVERLAPPED or, ow; |
603 | #endif |
731 | #endif |
604 | } ANFD; |
732 | } ANFD; |
605 | |
733 | |
606 | /* stores the pending event set for a given watcher */ |
734 | /* stores the pending event set for a given watcher */ |
607 | typedef struct |
735 | typedef struct |
… | |
… | |
662 | |
790 | |
663 | static int ev_default_loop_ptr; |
791 | static int ev_default_loop_ptr; |
664 | |
792 | |
665 | #endif |
793 | #endif |
666 | |
794 | |
667 | #if EV_MINIMAL < 2 |
795 | #if EV_FEATURE_API |
668 | # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) |
796 | # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) |
669 | # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) |
797 | # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) |
670 | # define EV_INVOKE_PENDING invoke_cb (EV_A) |
798 | # define EV_INVOKE_PENDING invoke_cb (EV_A) |
671 | #else |
799 | #else |
672 | # define EV_RELEASE_CB (void)0 |
800 | # define EV_RELEASE_CB (void)0 |
673 | # define EV_ACQUIRE_CB (void)0 |
801 | # define EV_ACQUIRE_CB (void)0 |
674 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
802 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
675 | #endif |
803 | #endif |
676 | |
804 | |
677 | #define EVUNLOOP_RECURSE 0x80 |
805 | #define EVBREAK_RECURSE 0x80 |
678 | |
806 | |
679 | /*****************************************************************************/ |
807 | /*****************************************************************************/ |
680 | |
808 | |
681 | #ifndef EV_HAVE_EV_TIME |
809 | #ifndef EV_HAVE_EV_TIME |
682 | ev_tstamp |
810 | ev_tstamp |
… | |
… | |
726 | if (delay > 0.) |
854 | if (delay > 0.) |
727 | { |
855 | { |
728 | #if EV_USE_NANOSLEEP |
856 | #if EV_USE_NANOSLEEP |
729 | struct timespec ts; |
857 | struct timespec ts; |
730 | |
858 | |
731 | ts.tv_sec = (time_t)delay; |
859 | EV_TS_SET (ts, delay); |
732 | ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); |
|
|
733 | |
|
|
734 | nanosleep (&ts, 0); |
860 | nanosleep (&ts, 0); |
735 | #elif defined(_WIN32) |
861 | #elif defined(_WIN32) |
736 | Sleep ((unsigned long)(delay * 1e3)); |
862 | Sleep ((unsigned long)(delay * 1e3)); |
737 | #else |
863 | #else |
738 | struct timeval tv; |
864 | struct timeval tv; |
739 | |
865 | |
740 | tv.tv_sec = (time_t)delay; |
|
|
741 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
|
|
742 | |
|
|
743 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
866 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
744 | /* something not guaranteed by newer posix versions, but guaranteed */ |
867 | /* something not guaranteed by newer posix versions, but guaranteed */ |
745 | /* by older ones */ |
868 | /* by older ones */ |
|
|
869 | EV_TV_SET (tv, delay); |
746 | select (0, 0, 0, 0, &tv); |
870 | select (0, 0, 0, 0, &tv); |
747 | #endif |
871 | #endif |
748 | } |
872 | } |
749 | } |
873 | } |
750 | |
874 | |
751 | /*****************************************************************************/ |
875 | /*****************************************************************************/ |
752 | |
876 | |
753 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
877 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
754 | |
878 | |
755 | /* find a suitable new size for the given array, */ |
879 | /* find a suitable new size for the given array, */ |
756 | /* hopefully by rounding to a ncie-to-malloc size */ |
880 | /* hopefully by rounding to a nice-to-malloc size */ |
757 | inline_size int |
881 | inline_size int |
758 | array_nextsize (int elem, int cur, int cnt) |
882 | array_nextsize (int elem, int cur, int cnt) |
759 | { |
883 | { |
760 | int ncur = cur + 1; |
884 | int ncur = cur + 1; |
761 | |
885 | |
… | |
… | |
857 | } |
981 | } |
858 | |
982 | |
859 | /*****************************************************************************/ |
983 | /*****************************************************************************/ |
860 | |
984 | |
861 | inline_speed void |
985 | inline_speed void |
862 | fd_event_nc (EV_P_ int fd, int revents) |
986 | fd_event_nocheck (EV_P_ int fd, int revents) |
863 | { |
987 | { |
864 | ANFD *anfd = anfds + fd; |
988 | ANFD *anfd = anfds + fd; |
865 | ev_io *w; |
989 | ev_io *w; |
866 | |
990 | |
867 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
991 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
879 | fd_event (EV_P_ int fd, int revents) |
1003 | fd_event (EV_P_ int fd, int revents) |
880 | { |
1004 | { |
881 | ANFD *anfd = anfds + fd; |
1005 | ANFD *anfd = anfds + fd; |
882 | |
1006 | |
883 | if (expect_true (!anfd->reify)) |
1007 | if (expect_true (!anfd->reify)) |
884 | fd_event_nc (EV_A_ fd, revents); |
1008 | fd_event_nocheck (EV_A_ fd, revents); |
885 | } |
1009 | } |
886 | |
1010 | |
887 | void |
1011 | void |
888 | ev_feed_fd_event (EV_P_ int fd, int revents) |
1012 | ev_feed_fd_event (EV_P_ int fd, int revents) |
889 | { |
1013 | { |
890 | if (fd >= 0 && fd < anfdmax) |
1014 | if (fd >= 0 && fd < anfdmax) |
891 | fd_event_nc (EV_A_ fd, revents); |
1015 | fd_event_nocheck (EV_A_ fd, revents); |
892 | } |
1016 | } |
893 | |
1017 | |
894 | /* make sure the external fd watch events are in-sync */ |
1018 | /* make sure the external fd watch events are in-sync */ |
895 | /* with the kernel/libev internal state */ |
1019 | /* with the kernel/libev internal state */ |
896 | inline_size void |
1020 | inline_size void |
897 | fd_reify (EV_P) |
1021 | fd_reify (EV_P) |
898 | { |
1022 | { |
899 | int i; |
1023 | int i; |
900 | |
1024 | |
|
|
1025 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
1026 | for (i = 0; i < fdchangecnt; ++i) |
|
|
1027 | { |
|
|
1028 | int fd = fdchanges [i]; |
|
|
1029 | ANFD *anfd = anfds + fd; |
|
|
1030 | |
|
|
1031 | if (anfd->reify & EV__IOFDSET && anfd->head) |
|
|
1032 | { |
|
|
1033 | SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
1034 | |
|
|
1035 | if (handle != anfd->handle) |
|
|
1036 | { |
|
|
1037 | unsigned long arg; |
|
|
1038 | |
|
|
1039 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0)); |
|
|
1040 | |
|
|
1041 | /* handle changed, but fd didn't - we need to do it in two steps */ |
|
|
1042 | backend_modify (EV_A_ fd, anfd->events, 0); |
|
|
1043 | anfd->events = 0; |
|
|
1044 | anfd->handle = handle; |
|
|
1045 | } |
|
|
1046 | } |
|
|
1047 | } |
|
|
1048 | #endif |
|
|
1049 | |
901 | for (i = 0; i < fdchangecnt; ++i) |
1050 | for (i = 0; i < fdchangecnt; ++i) |
902 | { |
1051 | { |
903 | int fd = fdchanges [i]; |
1052 | int fd = fdchanges [i]; |
904 | ANFD *anfd = anfds + fd; |
1053 | ANFD *anfd = anfds + fd; |
905 | ev_io *w; |
1054 | ev_io *w; |
906 | |
1055 | |
907 | unsigned char events = 0; |
1056 | unsigned char o_events = anfd->events; |
|
|
1057 | unsigned char o_reify = anfd->reify; |
908 | |
1058 | |
909 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
1059 | anfd->reify = 0; |
910 | events |= (unsigned char)w->events; |
|
|
911 | |
1060 | |
912 | #if EV_SELECT_IS_WINSOCKET |
1061 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
913 | if (events) |
|
|
914 | { |
1062 | { |
915 | unsigned long arg; |
1063 | anfd->events = 0; |
916 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
1064 | |
917 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
1065 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
|
|
1066 | anfd->events |= (unsigned char)w->events; |
|
|
1067 | |
|
|
1068 | if (o_events != anfd->events) |
|
|
1069 | o_reify = EV__IOFDSET; /* actually |= */ |
918 | } |
1070 | } |
919 | #endif |
|
|
920 | |
1071 | |
921 | { |
1072 | if (o_reify & EV__IOFDSET) |
922 | unsigned char o_events = anfd->events; |
|
|
923 | unsigned char o_reify = anfd->reify; |
|
|
924 | |
|
|
925 | anfd->reify = 0; |
|
|
926 | anfd->events = events; |
|
|
927 | |
|
|
928 | if (o_events != events || o_reify & EV__IOFDSET) |
|
|
929 | backend_modify (EV_A_ fd, o_events, events); |
1073 | backend_modify (EV_A_ fd, o_events, anfd->events); |
930 | } |
|
|
931 | } |
1074 | } |
932 | |
1075 | |
933 | fdchangecnt = 0; |
1076 | fdchangecnt = 0; |
934 | } |
1077 | } |
935 | |
1078 | |
… | |
… | |
959 | ev_io_stop (EV_A_ w); |
1102 | ev_io_stop (EV_A_ w); |
960 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1103 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
961 | } |
1104 | } |
962 | } |
1105 | } |
963 | |
1106 | |
964 | /* check whether the given fd is atcually valid, for error recovery */ |
1107 | /* check whether the given fd is actually valid, for error recovery */ |
965 | inline_size int |
1108 | inline_size int |
966 | fd_valid (int fd) |
1109 | fd_valid (int fd) |
967 | { |
1110 | { |
968 | #ifdef _WIN32 |
1111 | #ifdef _WIN32 |
969 | return _get_osfhandle (fd) != -1; |
1112 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
970 | #else |
1113 | #else |
971 | return fcntl (fd, F_GETFD) != -1; |
1114 | return fcntl (fd, F_GETFD) != -1; |
972 | #endif |
1115 | #endif |
973 | } |
1116 | } |
974 | |
1117 | |
… | |
… | |
1011 | anfds [fd].emask = 0; |
1154 | anfds [fd].emask = 0; |
1012 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
1155 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
1013 | } |
1156 | } |
1014 | } |
1157 | } |
1015 | |
1158 | |
|
|
1159 | /* used to prepare libev internal fd's */ |
|
|
1160 | /* this is not fork-safe */ |
|
|
1161 | inline_speed void |
|
|
1162 | fd_intern (int fd) |
|
|
1163 | { |
|
|
1164 | #ifdef _WIN32 |
|
|
1165 | unsigned long arg = 1; |
|
|
1166 | ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg); |
|
|
1167 | #else |
|
|
1168 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
1169 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
1170 | #endif |
|
|
1171 | } |
|
|
1172 | |
1016 | /*****************************************************************************/ |
1173 | /*****************************************************************************/ |
1017 | |
1174 | |
1018 | /* |
1175 | /* |
1019 | * the heap functions want a real array index. array index 0 uis guaranteed to not |
1176 | * the heap functions want a real array index. array index 0 is guaranteed to not |
1020 | * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives |
1177 | * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives |
1021 | * the branching factor of the d-tree. |
1178 | * the branching factor of the d-tree. |
1022 | */ |
1179 | */ |
1023 | |
1180 | |
1024 | /* |
1181 | /* |
… | |
… | |
1172 | |
1329 | |
1173 | static ANSIG signals [EV_NSIG - 1]; |
1330 | static ANSIG signals [EV_NSIG - 1]; |
1174 | |
1331 | |
1175 | /*****************************************************************************/ |
1332 | /*****************************************************************************/ |
1176 | |
1333 | |
1177 | /* used to prepare libev internal fd's */ |
1334 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1178 | /* this is not fork-safe */ |
|
|
1179 | inline_speed void |
|
|
1180 | fd_intern (int fd) |
|
|
1181 | { |
|
|
1182 | #ifdef _WIN32 |
|
|
1183 | unsigned long arg = 1; |
|
|
1184 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
1185 | #else |
|
|
1186 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
1187 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
1188 | #endif |
|
|
1189 | } |
|
|
1190 | |
1335 | |
1191 | static void noinline |
1336 | static void noinline |
1192 | evpipe_init (EV_P) |
1337 | evpipe_init (EV_P) |
1193 | { |
1338 | { |
1194 | if (!ev_is_active (&pipe_w)) |
1339 | if (!ev_is_active (&pipe_w)) |
1195 | { |
1340 | { |
1196 | #if EV_USE_EVENTFD |
1341 | # if EV_USE_EVENTFD |
1197 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1342 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1198 | if (evfd < 0 && errno == EINVAL) |
1343 | if (evfd < 0 && errno == EINVAL) |
1199 | evfd = eventfd (0, 0); |
1344 | evfd = eventfd (0, 0); |
1200 | |
1345 | |
1201 | if (evfd >= 0) |
1346 | if (evfd >= 0) |
… | |
… | |
1203 | evpipe [0] = -1; |
1348 | evpipe [0] = -1; |
1204 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1349 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1205 | ev_io_set (&pipe_w, evfd, EV_READ); |
1350 | ev_io_set (&pipe_w, evfd, EV_READ); |
1206 | } |
1351 | } |
1207 | else |
1352 | else |
1208 | #endif |
1353 | # endif |
1209 | { |
1354 | { |
1210 | while (pipe (evpipe)) |
1355 | while (pipe (evpipe)) |
1211 | ev_syserr ("(libev) error creating signal/async pipe"); |
1356 | ev_syserr ("(libev) error creating signal/async pipe"); |
1212 | |
1357 | |
1213 | fd_intern (evpipe [0]); |
1358 | fd_intern (evpipe [0]); |
… | |
… | |
1224 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1369 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1225 | { |
1370 | { |
1226 | if (!*flag) |
1371 | if (!*flag) |
1227 | { |
1372 | { |
1228 | int old_errno = errno; /* save errno because write might clobber it */ |
1373 | int old_errno = errno; /* save errno because write might clobber it */ |
|
|
1374 | char dummy; |
1229 | |
1375 | |
1230 | *flag = 1; |
1376 | *flag = 1; |
1231 | |
1377 | |
1232 | #if EV_USE_EVENTFD |
1378 | #if EV_USE_EVENTFD |
1233 | if (evfd >= 0) |
1379 | if (evfd >= 0) |
… | |
… | |
1235 | uint64_t counter = 1; |
1381 | uint64_t counter = 1; |
1236 | write (evfd, &counter, sizeof (uint64_t)); |
1382 | write (evfd, &counter, sizeof (uint64_t)); |
1237 | } |
1383 | } |
1238 | else |
1384 | else |
1239 | #endif |
1385 | #endif |
|
|
1386 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1387 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1388 | /* so when you think this write should be a send instead, please find out */ |
|
|
1389 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1390 | /* tell me. thank you. */ |
1240 | write (evpipe [1], &old_errno, 1); |
1391 | write (evpipe [1], &dummy, 1); |
1241 | |
1392 | |
1242 | errno = old_errno; |
1393 | errno = old_errno; |
1243 | } |
1394 | } |
1244 | } |
1395 | } |
1245 | |
1396 | |
… | |
… | |
1258 | } |
1409 | } |
1259 | else |
1410 | else |
1260 | #endif |
1411 | #endif |
1261 | { |
1412 | { |
1262 | char dummy; |
1413 | char dummy; |
|
|
1414 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1263 | read (evpipe [0], &dummy, 1); |
1415 | read (evpipe [0], &dummy, 1); |
1264 | } |
1416 | } |
1265 | |
1417 | |
|
|
1418 | #if EV_SIGNAL_ENABLE |
1266 | if (sig_pending) |
1419 | if (sig_pending) |
1267 | { |
1420 | { |
1268 | sig_pending = 0; |
1421 | sig_pending = 0; |
1269 | |
1422 | |
1270 | for (i = EV_NSIG - 1; i--; ) |
1423 | for (i = EV_NSIG - 1; i--; ) |
1271 | if (expect_false (signals [i].pending)) |
1424 | if (expect_false (signals [i].pending)) |
1272 | ev_feed_signal_event (EV_A_ i + 1); |
1425 | ev_feed_signal_event (EV_A_ i + 1); |
1273 | } |
1426 | } |
|
|
1427 | #endif |
1274 | |
1428 | |
1275 | #if EV_ASYNC_ENABLE |
1429 | #if EV_ASYNC_ENABLE |
1276 | if (async_pending) |
1430 | if (async_pending) |
1277 | { |
1431 | { |
1278 | async_pending = 0; |
1432 | async_pending = 0; |
… | |
… | |
1287 | #endif |
1441 | #endif |
1288 | } |
1442 | } |
1289 | |
1443 | |
1290 | /*****************************************************************************/ |
1444 | /*****************************************************************************/ |
1291 | |
1445 | |
|
|
1446 | void |
|
|
1447 | ev_feed_signal (int signum) |
|
|
1448 | { |
|
|
1449 | #if EV_MULTIPLICITY |
|
|
1450 | EV_P = signals [signum - 1].loop; |
|
|
1451 | |
|
|
1452 | if (!EV_A) |
|
|
1453 | return; |
|
|
1454 | #endif |
|
|
1455 | |
|
|
1456 | signals [signum - 1].pending = 1; |
|
|
1457 | evpipe_write (EV_A_ &sig_pending); |
|
|
1458 | } |
|
|
1459 | |
1292 | static void |
1460 | static void |
1293 | ev_sighandler (int signum) |
1461 | ev_sighandler (int signum) |
1294 | { |
1462 | { |
1295 | #if EV_MULTIPLICITY |
|
|
1296 | EV_P = signals [signum - 1].loop; |
|
|
1297 | #endif |
|
|
1298 | |
|
|
1299 | #if _WIN32 |
1463 | #ifdef _WIN32 |
1300 | signal (signum, ev_sighandler); |
1464 | signal (signum, ev_sighandler); |
1301 | #endif |
1465 | #endif |
1302 | |
1466 | |
1303 | signals [signum - 1].pending = 1; |
1467 | ev_feed_signal (signum); |
1304 | evpipe_write (EV_A_ &sig_pending); |
|
|
1305 | } |
1468 | } |
1306 | |
1469 | |
1307 | void noinline |
1470 | void noinline |
1308 | ev_feed_signal_event (EV_P_ int signum) |
1471 | ev_feed_signal_event (EV_P_ int signum) |
1309 | { |
1472 | { |
… | |
… | |
1346 | break; |
1509 | break; |
1347 | } |
1510 | } |
1348 | } |
1511 | } |
1349 | #endif |
1512 | #endif |
1350 | |
1513 | |
|
|
1514 | #endif |
|
|
1515 | |
1351 | /*****************************************************************************/ |
1516 | /*****************************************************************************/ |
1352 | |
1517 | |
|
|
1518 | #if EV_CHILD_ENABLE |
1353 | static WL childs [EV_PID_HASHSIZE]; |
1519 | static WL childs [EV_PID_HASHSIZE]; |
1354 | |
|
|
1355 | #ifndef _WIN32 |
|
|
1356 | |
1520 | |
1357 | static ev_signal childev; |
1521 | static ev_signal childev; |
1358 | |
1522 | |
1359 | #ifndef WIFCONTINUED |
1523 | #ifndef WIFCONTINUED |
1360 | # define WIFCONTINUED(status) 0 |
1524 | # define WIFCONTINUED(status) 0 |
… | |
… | |
1365 | child_reap (EV_P_ int chain, int pid, int status) |
1529 | child_reap (EV_P_ int chain, int pid, int status) |
1366 | { |
1530 | { |
1367 | ev_child *w; |
1531 | ev_child *w; |
1368 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1532 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1369 | |
1533 | |
1370 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1534 | for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next) |
1371 | { |
1535 | { |
1372 | if ((w->pid == pid || !w->pid) |
1536 | if ((w->pid == pid || !w->pid) |
1373 | && (!traced || (w->flags & 1))) |
1537 | && (!traced || (w->flags & 1))) |
1374 | { |
1538 | { |
1375 | ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ |
1539 | ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ |
… | |
… | |
1400 | /* make sure we are called again until all children have been reaped */ |
1564 | /* make sure we are called again until all children have been reaped */ |
1401 | /* we need to do it this way so that the callback gets called before we continue */ |
1565 | /* we need to do it this way so that the callback gets called before we continue */ |
1402 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
1566 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
1403 | |
1567 | |
1404 | child_reap (EV_A_ pid, pid, status); |
1568 | child_reap (EV_A_ pid, pid, status); |
1405 | if (EV_PID_HASHSIZE > 1) |
1569 | if ((EV_PID_HASHSIZE) > 1) |
1406 | child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
1570 | child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
1407 | } |
1571 | } |
1408 | |
1572 | |
1409 | #endif |
1573 | #endif |
1410 | |
1574 | |
1411 | /*****************************************************************************/ |
1575 | /*****************************************************************************/ |
1412 | |
1576 | |
|
|
1577 | #if EV_USE_IOCP |
|
|
1578 | # include "ev_iocp.c" |
|
|
1579 | #endif |
1413 | #if EV_USE_PORT |
1580 | #if EV_USE_PORT |
1414 | # include "ev_port.c" |
1581 | # include "ev_port.c" |
1415 | #endif |
1582 | #endif |
1416 | #if EV_USE_KQUEUE |
1583 | #if EV_USE_KQUEUE |
1417 | # include "ev_kqueue.c" |
1584 | # include "ev_kqueue.c" |
… | |
… | |
1477 | #ifdef __APPLE__ |
1644 | #ifdef __APPLE__ |
1478 | /* only select works correctly on that "unix-certified" platform */ |
1645 | /* only select works correctly on that "unix-certified" platform */ |
1479 | flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ |
1646 | flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ |
1480 | flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ |
1647 | flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ |
1481 | #endif |
1648 | #endif |
|
|
1649 | #ifdef __FreeBSD__ |
|
|
1650 | flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */ |
|
|
1651 | #endif |
1482 | |
1652 | |
1483 | return flags; |
1653 | return flags; |
1484 | } |
1654 | } |
1485 | |
1655 | |
1486 | unsigned int |
1656 | unsigned int |
1487 | ev_embeddable_backends (void) |
1657 | ev_embeddable_backends (void) |
1488 | { |
1658 | { |
1489 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1659 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1490 | |
1660 | |
1491 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1661 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1492 | /* please fix it and tell me how to detect the fix */ |
1662 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
1493 | flags &= ~EVBACKEND_EPOLL; |
1663 | flags &= ~EVBACKEND_EPOLL; |
1494 | |
1664 | |
1495 | return flags; |
1665 | return flags; |
1496 | } |
1666 | } |
1497 | |
1667 | |
1498 | unsigned int |
1668 | unsigned int |
1499 | ev_backend (EV_P) |
1669 | ev_backend (EV_P) |
1500 | { |
1670 | { |
1501 | return backend; |
1671 | return backend; |
1502 | } |
1672 | } |
1503 | |
1673 | |
1504 | #if EV_MINIMAL < 2 |
1674 | #if EV_FEATURE_API |
1505 | unsigned int |
1675 | unsigned int |
1506 | ev_loop_count (EV_P) |
1676 | ev_iteration (EV_P) |
1507 | { |
1677 | { |
1508 | return loop_count; |
1678 | return loop_count; |
1509 | } |
1679 | } |
1510 | |
1680 | |
1511 | unsigned int |
1681 | unsigned int |
1512 | ev_loop_depth (EV_P) |
1682 | ev_depth (EV_P) |
1513 | { |
1683 | { |
1514 | return loop_depth; |
1684 | return loop_depth; |
1515 | } |
1685 | } |
1516 | |
1686 | |
1517 | void |
1687 | void |
… | |
… | |
1554 | static void noinline |
1724 | static void noinline |
1555 | loop_init (EV_P_ unsigned int flags) |
1725 | loop_init (EV_P_ unsigned int flags) |
1556 | { |
1726 | { |
1557 | if (!backend) |
1727 | if (!backend) |
1558 | { |
1728 | { |
|
|
1729 | origflags = flags; |
|
|
1730 | |
1559 | #if EV_USE_REALTIME |
1731 | #if EV_USE_REALTIME |
1560 | if (!have_realtime) |
1732 | if (!have_realtime) |
1561 | { |
1733 | { |
1562 | struct timespec ts; |
1734 | struct timespec ts; |
1563 | |
1735 | |
… | |
… | |
1589 | |
1761 | |
1590 | ev_rt_now = ev_time (); |
1762 | ev_rt_now = ev_time (); |
1591 | mn_now = get_clock (); |
1763 | mn_now = get_clock (); |
1592 | now_floor = mn_now; |
1764 | now_floor = mn_now; |
1593 | rtmn_diff = ev_rt_now - mn_now; |
1765 | rtmn_diff = ev_rt_now - mn_now; |
1594 | #if EV_MINIMAL < 2 |
1766 | #if EV_FEATURE_API |
1595 | invoke_cb = ev_invoke_pending; |
1767 | invoke_cb = ev_invoke_pending; |
1596 | #endif |
1768 | #endif |
1597 | |
1769 | |
1598 | io_blocktime = 0.; |
1770 | io_blocktime = 0.; |
1599 | timeout_blocktime = 0.; |
1771 | timeout_blocktime = 0.; |
… | |
… | |
1605 | #endif |
1777 | #endif |
1606 | #if EV_USE_INOTIFY |
1778 | #if EV_USE_INOTIFY |
1607 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1779 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1608 | #endif |
1780 | #endif |
1609 | #if EV_USE_SIGNALFD |
1781 | #if EV_USE_SIGNALFD |
1610 | sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; |
1782 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1611 | #endif |
1783 | #endif |
1612 | |
1784 | |
1613 | if (!(flags & 0x0000ffffU)) |
1785 | if (!(flags & EVBACKEND_MASK)) |
1614 | flags |= ev_recommended_backends (); |
1786 | flags |= ev_recommended_backends (); |
1615 | |
1787 | |
|
|
1788 | #if EV_USE_IOCP |
|
|
1789 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
|
|
1790 | #endif |
1616 | #if EV_USE_PORT |
1791 | #if EV_USE_PORT |
1617 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
1792 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
1618 | #endif |
1793 | #endif |
1619 | #if EV_USE_KQUEUE |
1794 | #if EV_USE_KQUEUE |
1620 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
1795 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
… | |
… | |
1629 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1804 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1630 | #endif |
1805 | #endif |
1631 | |
1806 | |
1632 | ev_prepare_init (&pending_w, pendingcb); |
1807 | ev_prepare_init (&pending_w, pendingcb); |
1633 | |
1808 | |
|
|
1809 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1634 | ev_init (&pipe_w, pipecb); |
1810 | ev_init (&pipe_w, pipecb); |
1635 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1811 | ev_set_priority (&pipe_w, EV_MAXPRI); |
|
|
1812 | #endif |
1636 | } |
1813 | } |
1637 | } |
1814 | } |
1638 | |
1815 | |
1639 | /* free up a loop structure */ |
1816 | /* free up a loop structure */ |
1640 | static void noinline |
1817 | void |
1641 | loop_destroy (EV_P) |
1818 | ev_loop_destroy (EV_P) |
1642 | { |
1819 | { |
1643 | int i; |
1820 | int i; |
|
|
1821 | |
|
|
1822 | #if EV_MULTIPLICITY |
|
|
1823 | /* mimic free (0) */ |
|
|
1824 | if (!EV_A) |
|
|
1825 | return; |
|
|
1826 | #endif |
|
|
1827 | |
|
|
1828 | #if EV_CLEANUP_ENABLE |
|
|
1829 | /* queue cleanup watchers (and execute them) */ |
|
|
1830 | if (expect_false (cleanupcnt)) |
|
|
1831 | { |
|
|
1832 | queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); |
|
|
1833 | EV_INVOKE_PENDING; |
|
|
1834 | } |
|
|
1835 | #endif |
|
|
1836 | |
|
|
1837 | #if EV_CHILD_ENABLE |
|
|
1838 | if (ev_is_active (&childev)) |
|
|
1839 | { |
|
|
1840 | ev_ref (EV_A); /* child watcher */ |
|
|
1841 | ev_signal_stop (EV_A_ &childev); |
|
|
1842 | } |
|
|
1843 | #endif |
1644 | |
1844 | |
1645 | if (ev_is_active (&pipe_w)) |
1845 | if (ev_is_active (&pipe_w)) |
1646 | { |
1846 | { |
1647 | /*ev_ref (EV_A);*/ |
1847 | /*ev_ref (EV_A);*/ |
1648 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1848 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
… | |
… | |
1659 | } |
1859 | } |
1660 | } |
1860 | } |
1661 | |
1861 | |
1662 | #if EV_USE_SIGNALFD |
1862 | #if EV_USE_SIGNALFD |
1663 | if (ev_is_active (&sigfd_w)) |
1863 | if (ev_is_active (&sigfd_w)) |
1664 | { |
|
|
1665 | /*ev_ref (EV_A);*/ |
|
|
1666 | /*ev_io_stop (EV_A_ &sigfd_w);*/ |
|
|
1667 | |
|
|
1668 | close (sigfd); |
1864 | close (sigfd); |
1669 | } |
|
|
1670 | #endif |
1865 | #endif |
1671 | |
1866 | |
1672 | #if EV_USE_INOTIFY |
1867 | #if EV_USE_INOTIFY |
1673 | if (fs_fd >= 0) |
1868 | if (fs_fd >= 0) |
1674 | close (fs_fd); |
1869 | close (fs_fd); |
1675 | #endif |
1870 | #endif |
1676 | |
1871 | |
1677 | if (backend_fd >= 0) |
1872 | if (backend_fd >= 0) |
1678 | close (backend_fd); |
1873 | close (backend_fd); |
1679 | |
1874 | |
|
|
1875 | #if EV_USE_IOCP |
|
|
1876 | if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A); |
|
|
1877 | #endif |
1680 | #if EV_USE_PORT |
1878 | #if EV_USE_PORT |
1681 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
1879 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
1682 | #endif |
1880 | #endif |
1683 | #if EV_USE_KQUEUE |
1881 | #if EV_USE_KQUEUE |
1684 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
1882 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
… | |
… | |
1711 | array_free (periodic, EMPTY); |
1909 | array_free (periodic, EMPTY); |
1712 | #endif |
1910 | #endif |
1713 | #if EV_FORK_ENABLE |
1911 | #if EV_FORK_ENABLE |
1714 | array_free (fork, EMPTY); |
1912 | array_free (fork, EMPTY); |
1715 | #endif |
1913 | #endif |
|
|
1914 | #if EV_CLEANUP_ENABLE |
|
|
1915 | array_free (cleanup, EMPTY); |
|
|
1916 | #endif |
1716 | array_free (prepare, EMPTY); |
1917 | array_free (prepare, EMPTY); |
1717 | array_free (check, EMPTY); |
1918 | array_free (check, EMPTY); |
1718 | #if EV_ASYNC_ENABLE |
1919 | #if EV_ASYNC_ENABLE |
1719 | array_free (async, EMPTY); |
1920 | array_free (async, EMPTY); |
1720 | #endif |
1921 | #endif |
1721 | |
1922 | |
1722 | backend = 0; |
1923 | backend = 0; |
|
|
1924 | |
|
|
1925 | #if EV_MULTIPLICITY |
|
|
1926 | if (ev_is_default_loop (EV_A)) |
|
|
1927 | #endif |
|
|
1928 | ev_default_loop_ptr = 0; |
|
|
1929 | #if EV_MULTIPLICITY |
|
|
1930 | else |
|
|
1931 | ev_free (EV_A); |
|
|
1932 | #endif |
1723 | } |
1933 | } |
1724 | |
1934 | |
1725 | #if EV_USE_INOTIFY |
1935 | #if EV_USE_INOTIFY |
1726 | inline_size void infy_fork (EV_P); |
1936 | inline_size void infy_fork (EV_P); |
1727 | #endif |
1937 | #endif |
… | |
… | |
1763 | { |
1973 | { |
1764 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1974 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1765 | EV_WIN32_CLOSE_FD (evpipe [1]); |
1975 | EV_WIN32_CLOSE_FD (evpipe [1]); |
1766 | } |
1976 | } |
1767 | |
1977 | |
|
|
1978 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1768 | evpipe_init (EV_A); |
1979 | evpipe_init (EV_A); |
1769 | /* now iterate over everything, in case we missed something */ |
1980 | /* now iterate over everything, in case we missed something */ |
1770 | pipecb (EV_A_ &pipe_w, EV_READ); |
1981 | pipecb (EV_A_ &pipe_w, EV_READ); |
|
|
1982 | #endif |
1771 | } |
1983 | } |
1772 | |
1984 | |
1773 | postfork = 0; |
1985 | postfork = 0; |
1774 | } |
1986 | } |
1775 | |
1987 | |
… | |
… | |
1784 | loop_init (EV_A_ flags); |
1996 | loop_init (EV_A_ flags); |
1785 | |
1997 | |
1786 | if (ev_backend (EV_A)) |
1998 | if (ev_backend (EV_A)) |
1787 | return EV_A; |
1999 | return EV_A; |
1788 | |
2000 | |
|
|
2001 | ev_free (EV_A); |
1789 | return 0; |
2002 | return 0; |
1790 | } |
2003 | } |
1791 | |
2004 | |
1792 | void |
|
|
1793 | ev_loop_destroy (EV_P) |
|
|
1794 | { |
|
|
1795 | loop_destroy (EV_A); |
|
|
1796 | ev_free (loop); |
|
|
1797 | } |
|
|
1798 | |
|
|
1799 | void |
|
|
1800 | ev_loop_fork (EV_P) |
|
|
1801 | { |
|
|
1802 | postfork = 1; /* must be in line with ev_default_fork */ |
|
|
1803 | } |
|
|
1804 | #endif /* multiplicity */ |
2005 | #endif /* multiplicity */ |
1805 | |
2006 | |
1806 | #if EV_VERIFY |
2007 | #if EV_VERIFY |
1807 | static void noinline |
2008 | static void noinline |
1808 | verify_watcher (EV_P_ W w) |
2009 | verify_watcher (EV_P_ W w) |
… | |
… | |
1837 | verify_watcher (EV_A_ ws [cnt]); |
2038 | verify_watcher (EV_A_ ws [cnt]); |
1838 | } |
2039 | } |
1839 | } |
2040 | } |
1840 | #endif |
2041 | #endif |
1841 | |
2042 | |
1842 | #if EV_MINIMAL < 2 |
2043 | #if EV_FEATURE_API |
1843 | void |
2044 | void |
1844 | ev_loop_verify (EV_P) |
2045 | ev_verify (EV_P) |
1845 | { |
2046 | { |
1846 | #if EV_VERIFY |
2047 | #if EV_VERIFY |
1847 | int i; |
2048 | int i; |
1848 | WL w; |
2049 | WL w; |
1849 | |
2050 | |
… | |
… | |
1883 | #if EV_FORK_ENABLE |
2084 | #if EV_FORK_ENABLE |
1884 | assert (forkmax >= forkcnt); |
2085 | assert (forkmax >= forkcnt); |
1885 | array_verify (EV_A_ (W *)forks, forkcnt); |
2086 | array_verify (EV_A_ (W *)forks, forkcnt); |
1886 | #endif |
2087 | #endif |
1887 | |
2088 | |
|
|
2089 | #if EV_CLEANUP_ENABLE |
|
|
2090 | assert (cleanupmax >= cleanupcnt); |
|
|
2091 | array_verify (EV_A_ (W *)cleanups, cleanupcnt); |
|
|
2092 | #endif |
|
|
2093 | |
1888 | #if EV_ASYNC_ENABLE |
2094 | #if EV_ASYNC_ENABLE |
1889 | assert (asyncmax >= asynccnt); |
2095 | assert (asyncmax >= asynccnt); |
1890 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
2096 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
1891 | #endif |
2097 | #endif |
1892 | |
2098 | |
|
|
2099 | #if EV_PREPARE_ENABLE |
1893 | assert (preparemax >= preparecnt); |
2100 | assert (preparemax >= preparecnt); |
1894 | array_verify (EV_A_ (W *)prepares, preparecnt); |
2101 | array_verify (EV_A_ (W *)prepares, preparecnt); |
|
|
2102 | #endif |
1895 | |
2103 | |
|
|
2104 | #if EV_CHECK_ENABLE |
1896 | assert (checkmax >= checkcnt); |
2105 | assert (checkmax >= checkcnt); |
1897 | array_verify (EV_A_ (W *)checks, checkcnt); |
2106 | array_verify (EV_A_ (W *)checks, checkcnt); |
|
|
2107 | #endif |
1898 | |
2108 | |
1899 | # if 0 |
2109 | # if 0 |
|
|
2110 | #if EV_CHILD_ENABLE |
1900 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
2111 | for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next) |
1901 | for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) |
2112 | for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) |
|
|
2113 | #endif |
1902 | # endif |
2114 | # endif |
1903 | #endif |
2115 | #endif |
1904 | } |
2116 | } |
1905 | #endif |
2117 | #endif |
1906 | |
2118 | |
1907 | #if EV_MULTIPLICITY |
2119 | #if EV_MULTIPLICITY |
1908 | struct ev_loop * |
2120 | struct ev_loop * |
1909 | ev_default_loop_init (unsigned int flags) |
|
|
1910 | #else |
2121 | #else |
1911 | int |
2122 | int |
|
|
2123 | #endif |
1912 | ev_default_loop (unsigned int flags) |
2124 | ev_default_loop (unsigned int flags) |
1913 | #endif |
|
|
1914 | { |
2125 | { |
1915 | if (!ev_default_loop_ptr) |
2126 | if (!ev_default_loop_ptr) |
1916 | { |
2127 | { |
1917 | #if EV_MULTIPLICITY |
2128 | #if EV_MULTIPLICITY |
1918 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
2129 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
… | |
… | |
1922 | |
2133 | |
1923 | loop_init (EV_A_ flags); |
2134 | loop_init (EV_A_ flags); |
1924 | |
2135 | |
1925 | if (ev_backend (EV_A)) |
2136 | if (ev_backend (EV_A)) |
1926 | { |
2137 | { |
1927 | #ifndef _WIN32 |
2138 | #if EV_CHILD_ENABLE |
1928 | ev_signal_init (&childev, childcb, SIGCHLD); |
2139 | ev_signal_init (&childev, childcb, SIGCHLD); |
1929 | ev_set_priority (&childev, EV_MAXPRI); |
2140 | ev_set_priority (&childev, EV_MAXPRI); |
1930 | ev_signal_start (EV_A_ &childev); |
2141 | ev_signal_start (EV_A_ &childev); |
1931 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
2142 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
1932 | #endif |
2143 | #endif |
… | |
… | |
1937 | |
2148 | |
1938 | return ev_default_loop_ptr; |
2149 | return ev_default_loop_ptr; |
1939 | } |
2150 | } |
1940 | |
2151 | |
1941 | void |
2152 | void |
1942 | ev_default_destroy (void) |
2153 | ev_loop_fork (EV_P) |
1943 | { |
2154 | { |
1944 | #if EV_MULTIPLICITY |
|
|
1945 | EV_P = ev_default_loop_ptr; |
|
|
1946 | #endif |
|
|
1947 | |
|
|
1948 | ev_default_loop_ptr = 0; |
|
|
1949 | |
|
|
1950 | #ifndef _WIN32 |
|
|
1951 | ev_ref (EV_A); /* child watcher */ |
|
|
1952 | ev_signal_stop (EV_A_ &childev); |
|
|
1953 | #endif |
|
|
1954 | |
|
|
1955 | loop_destroy (EV_A); |
|
|
1956 | } |
|
|
1957 | |
|
|
1958 | void |
|
|
1959 | ev_default_fork (void) |
|
|
1960 | { |
|
|
1961 | #if EV_MULTIPLICITY |
|
|
1962 | EV_P = ev_default_loop_ptr; |
|
|
1963 | #endif |
|
|
1964 | |
|
|
1965 | postfork = 1; /* must be in line with ev_loop_fork */ |
2155 | postfork = 1; /* must be in line with ev_default_fork */ |
1966 | } |
2156 | } |
1967 | |
2157 | |
1968 | /*****************************************************************************/ |
2158 | /*****************************************************************************/ |
1969 | |
2159 | |
1970 | void |
2160 | void |
… | |
… | |
1992 | |
2182 | |
1993 | for (pri = NUMPRI; pri--; ) |
2183 | for (pri = NUMPRI; pri--; ) |
1994 | while (pendingcnt [pri]) |
2184 | while (pendingcnt [pri]) |
1995 | { |
2185 | { |
1996 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2186 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1997 | |
|
|
1998 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
1999 | /* ^ this is no longer true, as pending_w could be here */ |
|
|
2000 | |
2187 | |
2001 | p->w->pending = 0; |
2188 | p->w->pending = 0; |
2002 | EV_CB_INVOKE (p->w, p->events); |
2189 | EV_CB_INVOKE (p->w, p->events); |
2003 | EV_FREQUENT_CHECK; |
2190 | EV_FREQUENT_CHECK; |
2004 | } |
2191 | } |
… | |
… | |
2061 | EV_FREQUENT_CHECK; |
2248 | EV_FREQUENT_CHECK; |
2062 | feed_reverse (EV_A_ (W)w); |
2249 | feed_reverse (EV_A_ (W)w); |
2063 | } |
2250 | } |
2064 | while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); |
2251 | while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); |
2065 | |
2252 | |
2066 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
2253 | feed_reverse_done (EV_A_ EV_TIMER); |
2067 | } |
2254 | } |
2068 | } |
2255 | } |
2069 | |
2256 | |
2070 | #if EV_PERIODIC_ENABLE |
2257 | #if EV_PERIODIC_ENABLE |
|
|
2258 | |
|
|
2259 | static void noinline |
|
|
2260 | periodic_recalc (EV_P_ ev_periodic *w) |
|
|
2261 | { |
|
|
2262 | ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL; |
|
|
2263 | ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval); |
|
|
2264 | |
|
|
2265 | /* the above almost always errs on the low side */ |
|
|
2266 | while (at <= ev_rt_now) |
|
|
2267 | { |
|
|
2268 | ev_tstamp nat = at + w->interval; |
|
|
2269 | |
|
|
2270 | /* when resolution fails us, we use ev_rt_now */ |
|
|
2271 | if (expect_false (nat == at)) |
|
|
2272 | { |
|
|
2273 | at = ev_rt_now; |
|
|
2274 | break; |
|
|
2275 | } |
|
|
2276 | |
|
|
2277 | at = nat; |
|
|
2278 | } |
|
|
2279 | |
|
|
2280 | ev_at (w) = at; |
|
|
2281 | } |
|
|
2282 | |
2071 | /* make periodics pending */ |
2283 | /* make periodics pending */ |
2072 | inline_size void |
2284 | inline_size void |
2073 | periodics_reify (EV_P) |
2285 | periodics_reify (EV_P) |
2074 | { |
2286 | { |
2075 | EV_FREQUENT_CHECK; |
2287 | EV_FREQUENT_CHECK; |
… | |
… | |
2094 | ANHE_at_cache (periodics [HEAP0]); |
2306 | ANHE_at_cache (periodics [HEAP0]); |
2095 | downheap (periodics, periodiccnt, HEAP0); |
2307 | downheap (periodics, periodiccnt, HEAP0); |
2096 | } |
2308 | } |
2097 | else if (w->interval) |
2309 | else if (w->interval) |
2098 | { |
2310 | { |
2099 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2311 | periodic_recalc (EV_A_ w); |
2100 | /* if next trigger time is not sufficiently in the future, put it there */ |
|
|
2101 | /* this might happen because of floating point inexactness */ |
|
|
2102 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
|
|
2103 | { |
|
|
2104 | ev_at (w) += w->interval; |
|
|
2105 | |
|
|
2106 | /* if interval is unreasonably low we might still have a time in the past */ |
|
|
2107 | /* so correct this. this will make the periodic very inexact, but the user */ |
|
|
2108 | /* has effectively asked to get triggered more often than possible */ |
|
|
2109 | if (ev_at (w) < ev_rt_now) |
|
|
2110 | ev_at (w) = ev_rt_now; |
|
|
2111 | } |
|
|
2112 | |
|
|
2113 | ANHE_at_cache (periodics [HEAP0]); |
2312 | ANHE_at_cache (periodics [HEAP0]); |
2114 | downheap (periodics, periodiccnt, HEAP0); |
2313 | downheap (periodics, periodiccnt, HEAP0); |
2115 | } |
2314 | } |
2116 | else |
2315 | else |
2117 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
2316 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
2124 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2323 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2125 | } |
2324 | } |
2126 | } |
2325 | } |
2127 | |
2326 | |
2128 | /* simply recalculate all periodics */ |
2327 | /* simply recalculate all periodics */ |
2129 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
2328 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2130 | static void noinline |
2329 | static void noinline |
2131 | periodics_reschedule (EV_P) |
2330 | periodics_reschedule (EV_P) |
2132 | { |
2331 | { |
2133 | int i; |
2332 | int i; |
2134 | |
2333 | |
… | |
… | |
2138 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2337 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2139 | |
2338 | |
2140 | if (w->reschedule_cb) |
2339 | if (w->reschedule_cb) |
2141 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2340 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2142 | else if (w->interval) |
2341 | else if (w->interval) |
2143 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2342 | periodic_recalc (EV_A_ w); |
2144 | |
2343 | |
2145 | ANHE_at_cache (periodics [i]); |
2344 | ANHE_at_cache (periodics [i]); |
2146 | } |
2345 | } |
2147 | |
2346 | |
2148 | reheap (periodics, periodiccnt); |
2347 | reheap (periodics, periodiccnt); |
… | |
… | |
2162 | ANHE_at_cache (*he); |
2361 | ANHE_at_cache (*he); |
2163 | } |
2362 | } |
2164 | } |
2363 | } |
2165 | |
2364 | |
2166 | /* fetch new monotonic and realtime times from the kernel */ |
2365 | /* fetch new monotonic and realtime times from the kernel */ |
2167 | /* also detetc if there was a timejump, and act accordingly */ |
2366 | /* also detect if there was a timejump, and act accordingly */ |
2168 | inline_speed void |
2367 | inline_speed void |
2169 | time_update (EV_P_ ev_tstamp max_block) |
2368 | time_update (EV_P_ ev_tstamp max_block) |
2170 | { |
2369 | { |
2171 | #if EV_USE_MONOTONIC |
2370 | #if EV_USE_MONOTONIC |
2172 | if (expect_true (have_monotonic)) |
2371 | if (expect_true (have_monotonic)) |
… | |
… | |
2195 | * doesn't hurt either as we only do this on time-jumps or |
2394 | * doesn't hurt either as we only do this on time-jumps or |
2196 | * in the unlikely event of having been preempted here. |
2395 | * in the unlikely event of having been preempted here. |
2197 | */ |
2396 | */ |
2198 | for (i = 4; --i; ) |
2397 | for (i = 4; --i; ) |
2199 | { |
2398 | { |
|
|
2399 | ev_tstamp diff; |
2200 | rtmn_diff = ev_rt_now - mn_now; |
2400 | rtmn_diff = ev_rt_now - mn_now; |
2201 | |
2401 | |
|
|
2402 | diff = odiff - rtmn_diff; |
|
|
2403 | |
2202 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
2404 | if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) |
2203 | return; /* all is well */ |
2405 | return; /* all is well */ |
2204 | |
2406 | |
2205 | ev_rt_now = ev_time (); |
2407 | ev_rt_now = ev_time (); |
2206 | mn_now = get_clock (); |
2408 | mn_now = get_clock (); |
2207 | now_floor = mn_now; |
2409 | now_floor = mn_now; |
… | |
… | |
2230 | mn_now = ev_rt_now; |
2432 | mn_now = ev_rt_now; |
2231 | } |
2433 | } |
2232 | } |
2434 | } |
2233 | |
2435 | |
2234 | void |
2436 | void |
2235 | ev_loop (EV_P_ int flags) |
2437 | ev_run (EV_P_ int flags) |
2236 | { |
2438 | { |
2237 | #if EV_MINIMAL < 2 |
2439 | #if EV_FEATURE_API |
2238 | ++loop_depth; |
2440 | ++loop_depth; |
2239 | #endif |
2441 | #endif |
2240 | |
2442 | |
2241 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
2443 | assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE)); |
2242 | |
2444 | |
2243 | loop_done = EVUNLOOP_CANCEL; |
2445 | loop_done = EVBREAK_CANCEL; |
2244 | |
2446 | |
2245 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
2447 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
2246 | |
2448 | |
2247 | do |
2449 | do |
2248 | { |
2450 | { |
2249 | #if EV_VERIFY >= 2 |
2451 | #if EV_VERIFY >= 2 |
2250 | ev_loop_verify (EV_A); |
2452 | ev_verify (EV_A); |
2251 | #endif |
2453 | #endif |
2252 | |
2454 | |
2253 | #ifndef _WIN32 |
2455 | #ifndef _WIN32 |
2254 | if (expect_false (curpid)) /* penalise the forking check even more */ |
2456 | if (expect_false (curpid)) /* penalise the forking check even more */ |
2255 | if (expect_false (getpid () != curpid)) |
2457 | if (expect_false (getpid () != curpid)) |
… | |
… | |
2267 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2469 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2268 | EV_INVOKE_PENDING; |
2470 | EV_INVOKE_PENDING; |
2269 | } |
2471 | } |
2270 | #endif |
2472 | #endif |
2271 | |
2473 | |
|
|
2474 | #if EV_PREPARE_ENABLE |
2272 | /* queue prepare watchers (and execute them) */ |
2475 | /* queue prepare watchers (and execute them) */ |
2273 | if (expect_false (preparecnt)) |
2476 | if (expect_false (preparecnt)) |
2274 | { |
2477 | { |
2275 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2478 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2276 | EV_INVOKE_PENDING; |
2479 | EV_INVOKE_PENDING; |
2277 | } |
2480 | } |
|
|
2481 | #endif |
2278 | |
2482 | |
2279 | if (expect_false (loop_done)) |
2483 | if (expect_false (loop_done)) |
2280 | break; |
2484 | break; |
2281 | |
2485 | |
2282 | /* we might have forked, so reify kernel state if necessary */ |
2486 | /* we might have forked, so reify kernel state if necessary */ |
… | |
… | |
2289 | /* calculate blocking time */ |
2493 | /* calculate blocking time */ |
2290 | { |
2494 | { |
2291 | ev_tstamp waittime = 0.; |
2495 | ev_tstamp waittime = 0.; |
2292 | ev_tstamp sleeptime = 0.; |
2496 | ev_tstamp sleeptime = 0.; |
2293 | |
2497 | |
|
|
2498 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2499 | ev_tstamp prev_mn_now = mn_now; |
|
|
2500 | |
|
|
2501 | /* update time to cancel out callback processing overhead */ |
|
|
2502 | time_update (EV_A_ 1e100); |
|
|
2503 | |
2294 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2504 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2295 | { |
2505 | { |
2296 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2297 | ev_tstamp prev_mn_now = mn_now; |
|
|
2298 | |
|
|
2299 | /* update time to cancel out callback processing overhead */ |
|
|
2300 | time_update (EV_A_ 1e100); |
|
|
2301 | |
|
|
2302 | waittime = MAX_BLOCKTIME; |
2506 | waittime = MAX_BLOCKTIME; |
2303 | |
2507 | |
2304 | if (timercnt) |
2508 | if (timercnt) |
2305 | { |
2509 | { |
2306 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2510 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
… | |
… | |
2333 | waittime -= sleeptime; |
2537 | waittime -= sleeptime; |
2334 | } |
2538 | } |
2335 | } |
2539 | } |
2336 | } |
2540 | } |
2337 | |
2541 | |
2338 | #if EV_MINIMAL < 2 |
2542 | #if EV_FEATURE_API |
2339 | ++loop_count; |
2543 | ++loop_count; |
2340 | #endif |
2544 | #endif |
2341 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2545 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2342 | backend_poll (EV_A_ waittime); |
2546 | backend_poll (EV_A_ waittime); |
2343 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2547 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2344 | |
2548 | |
2345 | /* update ev_rt_now, do magic */ |
2549 | /* update ev_rt_now, do magic */ |
2346 | time_update (EV_A_ waittime + sleeptime); |
2550 | time_update (EV_A_ waittime + sleeptime); |
2347 | } |
2551 | } |
2348 | |
2552 | |
… | |
… | |
2355 | #if EV_IDLE_ENABLE |
2559 | #if EV_IDLE_ENABLE |
2356 | /* queue idle watchers unless other events are pending */ |
2560 | /* queue idle watchers unless other events are pending */ |
2357 | idle_reify (EV_A); |
2561 | idle_reify (EV_A); |
2358 | #endif |
2562 | #endif |
2359 | |
2563 | |
|
|
2564 | #if EV_CHECK_ENABLE |
2360 | /* queue check watchers, to be executed first */ |
2565 | /* queue check watchers, to be executed first */ |
2361 | if (expect_false (checkcnt)) |
2566 | if (expect_false (checkcnt)) |
2362 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2567 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
|
|
2568 | #endif |
2363 | |
2569 | |
2364 | EV_INVOKE_PENDING; |
2570 | EV_INVOKE_PENDING; |
2365 | } |
2571 | } |
2366 | while (expect_true ( |
2572 | while (expect_true ( |
2367 | activecnt |
2573 | activecnt |
2368 | && !loop_done |
2574 | && !loop_done |
2369 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2575 | && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT)) |
2370 | )); |
2576 | )); |
2371 | |
2577 | |
2372 | if (loop_done == EVUNLOOP_ONE) |
2578 | if (loop_done == EVBREAK_ONE) |
2373 | loop_done = EVUNLOOP_CANCEL; |
2579 | loop_done = EVBREAK_CANCEL; |
2374 | |
2580 | |
2375 | #if EV_MINIMAL < 2 |
2581 | #if EV_FEATURE_API |
2376 | --loop_depth; |
2582 | --loop_depth; |
2377 | #endif |
2583 | #endif |
2378 | } |
2584 | } |
2379 | |
2585 | |
2380 | void |
2586 | void |
2381 | ev_unloop (EV_P_ int how) |
2587 | ev_break (EV_P_ int how) |
2382 | { |
2588 | { |
2383 | loop_done = how; |
2589 | loop_done = how; |
2384 | } |
2590 | } |
2385 | |
2591 | |
2386 | void |
2592 | void |
… | |
… | |
2506 | |
2712 | |
2507 | if (expect_false (ev_is_active (w))) |
2713 | if (expect_false (ev_is_active (w))) |
2508 | return; |
2714 | return; |
2509 | |
2715 | |
2510 | assert (("libev: ev_io_start called with negative fd", fd >= 0)); |
2716 | assert (("libev: ev_io_start called with negative fd", fd >= 0)); |
2511 | assert (("libev: ev_io start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); |
2717 | assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); |
2512 | |
2718 | |
2513 | EV_FREQUENT_CHECK; |
2719 | EV_FREQUENT_CHECK; |
2514 | |
2720 | |
2515 | ev_start (EV_A_ (W)w, 1); |
2721 | ev_start (EV_A_ (W)w, 1); |
2516 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2722 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
… | |
… | |
2534 | EV_FREQUENT_CHECK; |
2740 | EV_FREQUENT_CHECK; |
2535 | |
2741 | |
2536 | wlist_del (&anfds[w->fd].head, (WL)w); |
2742 | wlist_del (&anfds[w->fd].head, (WL)w); |
2537 | ev_stop (EV_A_ (W)w); |
2743 | ev_stop (EV_A_ (W)w); |
2538 | |
2744 | |
2539 | fd_change (EV_A_ w->fd, 1); |
2745 | fd_change (EV_A_ w->fd, EV_ANFD_REIFY); |
2540 | |
2746 | |
2541 | EV_FREQUENT_CHECK; |
2747 | EV_FREQUENT_CHECK; |
2542 | } |
2748 | } |
2543 | |
2749 | |
2544 | void noinline |
2750 | void noinline |
… | |
… | |
2586 | timers [active] = timers [timercnt + HEAP0]; |
2792 | timers [active] = timers [timercnt + HEAP0]; |
2587 | adjustheap (timers, timercnt, active); |
2793 | adjustheap (timers, timercnt, active); |
2588 | } |
2794 | } |
2589 | } |
2795 | } |
2590 | |
2796 | |
2591 | EV_FREQUENT_CHECK; |
|
|
2592 | |
|
|
2593 | ev_at (w) -= mn_now; |
2797 | ev_at (w) -= mn_now; |
2594 | |
2798 | |
2595 | ev_stop (EV_A_ (W)w); |
2799 | ev_stop (EV_A_ (W)w); |
|
|
2800 | |
|
|
2801 | EV_FREQUENT_CHECK; |
2596 | } |
2802 | } |
2597 | |
2803 | |
2598 | void noinline |
2804 | void noinline |
2599 | ev_timer_again (EV_P_ ev_timer *w) |
2805 | ev_timer_again (EV_P_ ev_timer *w) |
2600 | { |
2806 | { |
… | |
… | |
2636 | if (w->reschedule_cb) |
2842 | if (w->reschedule_cb) |
2637 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2843 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2638 | else if (w->interval) |
2844 | else if (w->interval) |
2639 | { |
2845 | { |
2640 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2846 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2641 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2847 | periodic_recalc (EV_A_ w); |
2642 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2643 | } |
2848 | } |
2644 | else |
2849 | else |
2645 | ev_at (w) = w->offset; |
2850 | ev_at (w) = w->offset; |
2646 | |
2851 | |
2647 | EV_FREQUENT_CHECK; |
2852 | EV_FREQUENT_CHECK; |
… | |
… | |
2679 | periodics [active] = periodics [periodiccnt + HEAP0]; |
2884 | periodics [active] = periodics [periodiccnt + HEAP0]; |
2680 | adjustheap (periodics, periodiccnt, active); |
2885 | adjustheap (periodics, periodiccnt, active); |
2681 | } |
2886 | } |
2682 | } |
2887 | } |
2683 | |
2888 | |
2684 | EV_FREQUENT_CHECK; |
|
|
2685 | |
|
|
2686 | ev_stop (EV_A_ (W)w); |
2889 | ev_stop (EV_A_ (W)w); |
|
|
2890 | |
|
|
2891 | EV_FREQUENT_CHECK; |
2687 | } |
2892 | } |
2688 | |
2893 | |
2689 | void noinline |
2894 | void noinline |
2690 | ev_periodic_again (EV_P_ ev_periodic *w) |
2895 | ev_periodic_again (EV_P_ ev_periodic *w) |
2691 | { |
2896 | { |
… | |
… | |
2696 | #endif |
2901 | #endif |
2697 | |
2902 | |
2698 | #ifndef SA_RESTART |
2903 | #ifndef SA_RESTART |
2699 | # define SA_RESTART 0 |
2904 | # define SA_RESTART 0 |
2700 | #endif |
2905 | #endif |
|
|
2906 | |
|
|
2907 | #if EV_SIGNAL_ENABLE |
2701 | |
2908 | |
2702 | void noinline |
2909 | void noinline |
2703 | ev_signal_start (EV_P_ ev_signal *w) |
2910 | ev_signal_start (EV_P_ ev_signal *w) |
2704 | { |
2911 | { |
2705 | if (expect_false (ev_is_active (w))) |
2912 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2752 | if (!((WL)w)->next) |
2959 | if (!((WL)w)->next) |
2753 | # if EV_USE_SIGNALFD |
2960 | # if EV_USE_SIGNALFD |
2754 | if (sigfd < 0) /*TODO*/ |
2961 | if (sigfd < 0) /*TODO*/ |
2755 | # endif |
2962 | # endif |
2756 | { |
2963 | { |
2757 | # if _WIN32 |
2964 | # ifdef _WIN32 |
|
|
2965 | evpipe_init (EV_A); |
|
|
2966 | |
2758 | signal (w->signum, ev_sighandler); |
2967 | signal (w->signum, ev_sighandler); |
2759 | # else |
2968 | # else |
2760 | struct sigaction sa; |
2969 | struct sigaction sa; |
2761 | |
2970 | |
2762 | evpipe_init (EV_A); |
2971 | evpipe_init (EV_A); |
… | |
… | |
2764 | sa.sa_handler = ev_sighandler; |
2973 | sa.sa_handler = ev_sighandler; |
2765 | sigfillset (&sa.sa_mask); |
2974 | sigfillset (&sa.sa_mask); |
2766 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2975 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2767 | sigaction (w->signum, &sa, 0); |
2976 | sigaction (w->signum, &sa, 0); |
2768 | |
2977 | |
|
|
2978 | if (origflags & EVFLAG_NOSIGMASK) |
|
|
2979 | { |
2769 | sigemptyset (&sa.sa_mask); |
2980 | sigemptyset (&sa.sa_mask); |
2770 | sigaddset (&sa.sa_mask, w->signum); |
2981 | sigaddset (&sa.sa_mask, w->signum); |
2771 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
2982 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
2983 | } |
2772 | #endif |
2984 | #endif |
2773 | } |
2985 | } |
2774 | |
2986 | |
2775 | EV_FREQUENT_CHECK; |
2987 | EV_FREQUENT_CHECK; |
2776 | } |
2988 | } |
… | |
… | |
2793 | signals [w->signum - 1].loop = 0; /* unattach from signal */ |
3005 | signals [w->signum - 1].loop = 0; /* unattach from signal */ |
2794 | #endif |
3006 | #endif |
2795 | #if EV_USE_SIGNALFD |
3007 | #if EV_USE_SIGNALFD |
2796 | if (sigfd >= 0) |
3008 | if (sigfd >= 0) |
2797 | { |
3009 | { |
2798 | sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D |
3010 | sigset_t ss; |
|
|
3011 | |
|
|
3012 | sigemptyset (&ss); |
|
|
3013 | sigaddset (&ss, w->signum); |
2799 | sigdelset (&sigfd_set, w->signum); |
3014 | sigdelset (&sigfd_set, w->signum); |
|
|
3015 | |
2800 | signalfd (sigfd, &sigfd_set, 0); |
3016 | signalfd (sigfd, &sigfd_set, 0); |
2801 | sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D |
3017 | sigprocmask (SIG_UNBLOCK, &ss, 0); |
2802 | /*TODO: maybe unblock signal? */ |
|
|
2803 | } |
3018 | } |
2804 | else |
3019 | else |
2805 | #endif |
3020 | #endif |
2806 | signal (w->signum, SIG_DFL); |
3021 | signal (w->signum, SIG_DFL); |
2807 | } |
3022 | } |
2808 | |
3023 | |
2809 | EV_FREQUENT_CHECK; |
3024 | EV_FREQUENT_CHECK; |
2810 | } |
3025 | } |
2811 | |
3026 | |
|
|
3027 | #endif |
|
|
3028 | |
|
|
3029 | #if EV_CHILD_ENABLE |
|
|
3030 | |
2812 | void |
3031 | void |
2813 | ev_child_start (EV_P_ ev_child *w) |
3032 | ev_child_start (EV_P_ ev_child *w) |
2814 | { |
3033 | { |
2815 | #if EV_MULTIPLICITY |
3034 | #if EV_MULTIPLICITY |
2816 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
3035 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
… | |
… | |
2819 | return; |
3038 | return; |
2820 | |
3039 | |
2821 | EV_FREQUENT_CHECK; |
3040 | EV_FREQUENT_CHECK; |
2822 | |
3041 | |
2823 | ev_start (EV_A_ (W)w, 1); |
3042 | ev_start (EV_A_ (W)w, 1); |
2824 | wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
3043 | wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); |
2825 | |
3044 | |
2826 | EV_FREQUENT_CHECK; |
3045 | EV_FREQUENT_CHECK; |
2827 | } |
3046 | } |
2828 | |
3047 | |
2829 | void |
3048 | void |
… | |
… | |
2833 | if (expect_false (!ev_is_active (w))) |
3052 | if (expect_false (!ev_is_active (w))) |
2834 | return; |
3053 | return; |
2835 | |
3054 | |
2836 | EV_FREQUENT_CHECK; |
3055 | EV_FREQUENT_CHECK; |
2837 | |
3056 | |
2838 | wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
3057 | wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); |
2839 | ev_stop (EV_A_ (W)w); |
3058 | ev_stop (EV_A_ (W)w); |
2840 | |
3059 | |
2841 | EV_FREQUENT_CHECK; |
3060 | EV_FREQUENT_CHECK; |
2842 | } |
3061 | } |
|
|
3062 | |
|
|
3063 | #endif |
2843 | |
3064 | |
2844 | #if EV_STAT_ENABLE |
3065 | #if EV_STAT_ENABLE |
2845 | |
3066 | |
2846 | # ifdef _WIN32 |
3067 | # ifdef _WIN32 |
2847 | # undef lstat |
3068 | # undef lstat |
… | |
… | |
2853 | #define MIN_STAT_INTERVAL 0.1074891 |
3074 | #define MIN_STAT_INTERVAL 0.1074891 |
2854 | |
3075 | |
2855 | static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
3076 | static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
2856 | |
3077 | |
2857 | #if EV_USE_INOTIFY |
3078 | #if EV_USE_INOTIFY |
2858 | # define EV_INOTIFY_BUFSIZE 8192 |
3079 | |
|
|
3080 | /* the * 2 is to allow for alignment padding, which for some reason is >> 8 */ |
|
|
3081 | # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) |
2859 | |
3082 | |
2860 | static void noinline |
3083 | static void noinline |
2861 | infy_add (EV_P_ ev_stat *w) |
3084 | infy_add (EV_P_ ev_stat *w) |
2862 | { |
3085 | { |
2863 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
3086 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
2864 | |
3087 | |
2865 | if (w->wd < 0) |
3088 | if (w->wd >= 0) |
|
|
3089 | { |
|
|
3090 | struct statfs sfs; |
|
|
3091 | |
|
|
3092 | /* now local changes will be tracked by inotify, but remote changes won't */ |
|
|
3093 | /* unless the filesystem is known to be local, we therefore still poll */ |
|
|
3094 | /* also do poll on <2.6.25, but with normal frequency */ |
|
|
3095 | |
|
|
3096 | if (!fs_2625) |
|
|
3097 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
|
|
3098 | else if (!statfs (w->path, &sfs) |
|
|
3099 | && (sfs.f_type == 0x1373 /* devfs */ |
|
|
3100 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
|
|
3101 | || sfs.f_type == 0x3153464a /* jfs */ |
|
|
3102 | || sfs.f_type == 0x52654973 /* reiser3 */ |
|
|
3103 | || sfs.f_type == 0x01021994 /* tempfs */ |
|
|
3104 | || sfs.f_type == 0x58465342 /* xfs */)) |
|
|
3105 | w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ |
|
|
3106 | else |
|
|
3107 | w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ |
2866 | { |
3108 | } |
|
|
3109 | else |
|
|
3110 | { |
|
|
3111 | /* can't use inotify, continue to stat */ |
2867 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
3112 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
2868 | ev_timer_again (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
|
|
2869 | |
3113 | |
2870 | /* monitor some parent directory for speedup hints */ |
3114 | /* if path is not there, monitor some parent directory for speedup hints */ |
2871 | /* note that exceeding the hardcoded path limit is not a correctness issue, */ |
3115 | /* note that exceeding the hardcoded path limit is not a correctness issue, */ |
2872 | /* but an efficiency issue only */ |
3116 | /* but an efficiency issue only */ |
2873 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
3117 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2874 | { |
3118 | { |
2875 | char path [4096]; |
3119 | char path [4096]; |
… | |
… | |
2885 | if (!pend || pend == path) |
3129 | if (!pend || pend == path) |
2886 | break; |
3130 | break; |
2887 | |
3131 | |
2888 | *pend = 0; |
3132 | *pend = 0; |
2889 | w->wd = inotify_add_watch (fs_fd, path, mask); |
3133 | w->wd = inotify_add_watch (fs_fd, path, mask); |
2890 | } |
3134 | } |
2891 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
3135 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
2892 | } |
3136 | } |
2893 | } |
3137 | } |
2894 | |
3138 | |
2895 | if (w->wd >= 0) |
3139 | if (w->wd >= 0) |
2896 | { |
|
|
2897 | struct statfs sfs; |
|
|
2898 | |
|
|
2899 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
3140 | wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w); |
2900 | |
3141 | |
2901 | /* now local changes will be tracked by inotify, but remote changes won't */ |
3142 | /* now re-arm timer, if required */ |
2902 | /* unless the filesystem it known to be local, we therefore still poll */ |
3143 | if (ev_is_active (&w->timer)) ev_ref (EV_A); |
2903 | /* also do poll on <2.6.25, but with normal frequency */ |
|
|
2904 | |
|
|
2905 | if (fs_2625 && !statfs (w->path, &sfs)) |
|
|
2906 | if (sfs.f_type == 0x1373 /* devfs */ |
|
|
2907 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
|
|
2908 | || sfs.f_type == 0x3153464a /* jfs */ |
|
|
2909 | || sfs.f_type == 0x52654973 /* reiser3 */ |
|
|
2910 | || sfs.f_type == 0x01021994 /* tempfs */ |
|
|
2911 | || sfs.f_type == 0x58465342 /* xfs */) |
|
|
2912 | return; |
|
|
2913 | |
|
|
2914 | w->timer.repeat = w->interval ? w->interval : fs_2625 ? NFS_STAT_INTERVAL : DEF_STAT_INTERVAL; |
|
|
2915 | ev_timer_again (EV_A_ &w->timer); |
3144 | ev_timer_again (EV_A_ &w->timer); |
2916 | } |
3145 | if (ev_is_active (&w->timer)) ev_unref (EV_A); |
2917 | } |
3146 | } |
2918 | |
3147 | |
2919 | static void noinline |
3148 | static void noinline |
2920 | infy_del (EV_P_ ev_stat *w) |
3149 | infy_del (EV_P_ ev_stat *w) |
2921 | { |
3150 | { |
… | |
… | |
2924 | |
3153 | |
2925 | if (wd < 0) |
3154 | if (wd < 0) |
2926 | return; |
3155 | return; |
2927 | |
3156 | |
2928 | w->wd = -2; |
3157 | w->wd = -2; |
2929 | slot = wd & (EV_INOTIFY_HASHSIZE - 1); |
3158 | slot = wd & ((EV_INOTIFY_HASHSIZE) - 1); |
2930 | wlist_del (&fs_hash [slot].head, (WL)w); |
3159 | wlist_del (&fs_hash [slot].head, (WL)w); |
2931 | |
3160 | |
2932 | /* remove this watcher, if others are watching it, they will rearm */ |
3161 | /* remove this watcher, if others are watching it, they will rearm */ |
2933 | inotify_rm_watch (fs_fd, wd); |
3162 | inotify_rm_watch (fs_fd, wd); |
2934 | } |
3163 | } |
… | |
… | |
2936 | static void noinline |
3165 | static void noinline |
2937 | infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) |
3166 | infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) |
2938 | { |
3167 | { |
2939 | if (slot < 0) |
3168 | if (slot < 0) |
2940 | /* overflow, need to check for all hash slots */ |
3169 | /* overflow, need to check for all hash slots */ |
2941 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
3170 | for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot) |
2942 | infy_wd (EV_A_ slot, wd, ev); |
3171 | infy_wd (EV_A_ slot, wd, ev); |
2943 | else |
3172 | else |
2944 | { |
3173 | { |
2945 | WL w_; |
3174 | WL w_; |
2946 | |
3175 | |
2947 | for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; ) |
3176 | for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; ) |
2948 | { |
3177 | { |
2949 | ev_stat *w = (ev_stat *)w_; |
3178 | ev_stat *w = (ev_stat *)w_; |
2950 | w_ = w_->next; /* lets us remove this watcher and all before it */ |
3179 | w_ = w_->next; /* lets us remove this watcher and all before it */ |
2951 | |
3180 | |
2952 | if (w->wd == wd || wd == -1) |
3181 | if (w->wd == wd || wd == -1) |
2953 | { |
3182 | { |
2954 | if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) |
3183 | if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) |
2955 | { |
3184 | { |
2956 | wlist_del (&fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
3185 | wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w); |
2957 | w->wd = -1; |
3186 | w->wd = -1; |
2958 | infy_add (EV_A_ w); /* re-add, no matter what */ |
3187 | infy_add (EV_A_ w); /* re-add, no matter what */ |
2959 | } |
3188 | } |
2960 | |
3189 | |
2961 | stat_timer_cb (EV_A_ &w->timer, 0); |
3190 | stat_timer_cb (EV_A_ &w->timer, 0); |
… | |
… | |
2966 | |
3195 | |
2967 | static void |
3196 | static void |
2968 | infy_cb (EV_P_ ev_io *w, int revents) |
3197 | infy_cb (EV_P_ ev_io *w, int revents) |
2969 | { |
3198 | { |
2970 | char buf [EV_INOTIFY_BUFSIZE]; |
3199 | char buf [EV_INOTIFY_BUFSIZE]; |
2971 | struct inotify_event *ev = (struct inotify_event *)buf; |
|
|
2972 | int ofs; |
3200 | int ofs; |
2973 | int len = read (fs_fd, buf, sizeof (buf)); |
3201 | int len = read (fs_fd, buf, sizeof (buf)); |
2974 | |
3202 | |
2975 | for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) |
3203 | for (ofs = 0; ofs < len; ) |
|
|
3204 | { |
|
|
3205 | struct inotify_event *ev = (struct inotify_event *)(buf + ofs); |
2976 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3206 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
|
|
3207 | ofs += sizeof (struct inotify_event) + ev->len; |
|
|
3208 | } |
2977 | } |
3209 | } |
2978 | |
3210 | |
2979 | inline_size void |
3211 | inline_size void |
2980 | check_2625 (EV_P) |
3212 | ev_check_2625 (EV_P) |
2981 | { |
3213 | { |
2982 | /* kernels < 2.6.25 are borked |
3214 | /* kernels < 2.6.25 are borked |
2983 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3215 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
2984 | */ |
3216 | */ |
2985 | struct utsname buf; |
3217 | if (ev_linux_version () < 0x020619) |
2986 | int major, minor, micro; |
|
|
2987 | |
|
|
2988 | if (uname (&buf)) |
|
|
2989 | return; |
|
|
2990 | |
|
|
2991 | if (sscanf (buf.release, "%d.%d.%d", &major, &minor, µ) != 3) |
|
|
2992 | return; |
|
|
2993 | |
|
|
2994 | if (major < 2 |
|
|
2995 | || (major == 2 && minor < 6) |
|
|
2996 | || (major == 2 && minor == 6 && micro < 25)) |
|
|
2997 | return; |
3218 | return; |
2998 | |
3219 | |
2999 | fs_2625 = 1; |
3220 | fs_2625 = 1; |
3000 | } |
3221 | } |
3001 | |
3222 | |
… | |
… | |
3016 | if (fs_fd != -2) |
3237 | if (fs_fd != -2) |
3017 | return; |
3238 | return; |
3018 | |
3239 | |
3019 | fs_fd = -1; |
3240 | fs_fd = -1; |
3020 | |
3241 | |
3021 | check_2625 (EV_A); |
3242 | ev_check_2625 (EV_A); |
3022 | |
3243 | |
3023 | fs_fd = infy_newfd (); |
3244 | fs_fd = infy_newfd (); |
3024 | |
3245 | |
3025 | if (fs_fd >= 0) |
3246 | if (fs_fd >= 0) |
3026 | { |
3247 | { |
3027 | fd_intern (fs_fd); |
3248 | fd_intern (fs_fd); |
3028 | ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); |
3249 | ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); |
3029 | ev_set_priority (&fs_w, EV_MAXPRI); |
3250 | ev_set_priority (&fs_w, EV_MAXPRI); |
3030 | ev_io_start (EV_A_ &fs_w); |
3251 | ev_io_start (EV_A_ &fs_w); |
|
|
3252 | ev_unref (EV_A); |
3031 | } |
3253 | } |
3032 | } |
3254 | } |
3033 | |
3255 | |
3034 | inline_size void |
3256 | inline_size void |
3035 | infy_fork (EV_P) |
3257 | infy_fork (EV_P) |
… | |
… | |
3037 | int slot; |
3259 | int slot; |
3038 | |
3260 | |
3039 | if (fs_fd < 0) |
3261 | if (fs_fd < 0) |
3040 | return; |
3262 | return; |
3041 | |
3263 | |
|
|
3264 | ev_ref (EV_A); |
3042 | ev_io_stop (EV_A_ &fs_w); |
3265 | ev_io_stop (EV_A_ &fs_w); |
3043 | close (fs_fd); |
3266 | close (fs_fd); |
3044 | fs_fd = infy_newfd (); |
3267 | fs_fd = infy_newfd (); |
3045 | |
3268 | |
3046 | if (fs_fd >= 0) |
3269 | if (fs_fd >= 0) |
3047 | { |
3270 | { |
3048 | fd_intern (fs_fd); |
3271 | fd_intern (fs_fd); |
3049 | ev_io_set (&fs_w, fs_fd, EV_READ); |
3272 | ev_io_set (&fs_w, fs_fd, EV_READ); |
3050 | ev_io_start (EV_A_ &fs_w); |
3273 | ev_io_start (EV_A_ &fs_w); |
|
|
3274 | ev_unref (EV_A); |
3051 | } |
3275 | } |
3052 | |
3276 | |
3053 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
3277 | for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot) |
3054 | { |
3278 | { |
3055 | WL w_ = fs_hash [slot].head; |
3279 | WL w_ = fs_hash [slot].head; |
3056 | fs_hash [slot].head = 0; |
3280 | fs_hash [slot].head = 0; |
3057 | |
3281 | |
3058 | while (w_) |
3282 | while (w_) |
… | |
… | |
3063 | w->wd = -1; |
3287 | w->wd = -1; |
3064 | |
3288 | |
3065 | if (fs_fd >= 0) |
3289 | if (fs_fd >= 0) |
3066 | infy_add (EV_A_ w); /* re-add, no matter what */ |
3290 | infy_add (EV_A_ w); /* re-add, no matter what */ |
3067 | else |
3291 | else |
|
|
3292 | { |
|
|
3293 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
|
|
3294 | if (ev_is_active (&w->timer)) ev_ref (EV_A); |
3068 | ev_timer_again (EV_A_ &w->timer); |
3295 | ev_timer_again (EV_A_ &w->timer); |
|
|
3296 | if (ev_is_active (&w->timer)) ev_unref (EV_A); |
|
|
3297 | } |
3069 | } |
3298 | } |
3070 | } |
3299 | } |
3071 | } |
3300 | } |
3072 | |
3301 | |
3073 | #endif |
3302 | #endif |
… | |
… | |
3090 | static void noinline |
3319 | static void noinline |
3091 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
3320 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
3092 | { |
3321 | { |
3093 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
3322 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
3094 | |
3323 | |
3095 | /* we copy this here each the time so that */ |
3324 | ev_statdata prev = w->attr; |
3096 | /* prev has the old value when the callback gets invoked */ |
|
|
3097 | w->prev = w->attr; |
|
|
3098 | ev_stat_stat (EV_A_ w); |
3325 | ev_stat_stat (EV_A_ w); |
3099 | |
3326 | |
3100 | /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ |
3327 | /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ |
3101 | if ( |
3328 | if ( |
3102 | w->prev.st_dev != w->attr.st_dev |
3329 | prev.st_dev != w->attr.st_dev |
3103 | || w->prev.st_ino != w->attr.st_ino |
3330 | || prev.st_ino != w->attr.st_ino |
3104 | || w->prev.st_mode != w->attr.st_mode |
3331 | || prev.st_mode != w->attr.st_mode |
3105 | || w->prev.st_nlink != w->attr.st_nlink |
3332 | || prev.st_nlink != w->attr.st_nlink |
3106 | || w->prev.st_uid != w->attr.st_uid |
3333 | || prev.st_uid != w->attr.st_uid |
3107 | || w->prev.st_gid != w->attr.st_gid |
3334 | || prev.st_gid != w->attr.st_gid |
3108 | || w->prev.st_rdev != w->attr.st_rdev |
3335 | || prev.st_rdev != w->attr.st_rdev |
3109 | || w->prev.st_size != w->attr.st_size |
3336 | || prev.st_size != w->attr.st_size |
3110 | || w->prev.st_atime != w->attr.st_atime |
3337 | || prev.st_atime != w->attr.st_atime |
3111 | || w->prev.st_mtime != w->attr.st_mtime |
3338 | || prev.st_mtime != w->attr.st_mtime |
3112 | || w->prev.st_ctime != w->attr.st_ctime |
3339 | || prev.st_ctime != w->attr.st_ctime |
3113 | ) { |
3340 | ) { |
|
|
3341 | /* we only update w->prev on actual differences */ |
|
|
3342 | /* in case we test more often than invoke the callback, */ |
|
|
3343 | /* to ensure that prev is always different to attr */ |
|
|
3344 | w->prev = prev; |
|
|
3345 | |
3114 | #if EV_USE_INOTIFY |
3346 | #if EV_USE_INOTIFY |
3115 | if (fs_fd >= 0) |
3347 | if (fs_fd >= 0) |
3116 | { |
3348 | { |
3117 | infy_del (EV_A_ w); |
3349 | infy_del (EV_A_ w); |
3118 | infy_add (EV_A_ w); |
3350 | infy_add (EV_A_ w); |
… | |
… | |
3143 | |
3375 | |
3144 | if (fs_fd >= 0) |
3376 | if (fs_fd >= 0) |
3145 | infy_add (EV_A_ w); |
3377 | infy_add (EV_A_ w); |
3146 | else |
3378 | else |
3147 | #endif |
3379 | #endif |
|
|
3380 | { |
3148 | ev_timer_again (EV_A_ &w->timer); |
3381 | ev_timer_again (EV_A_ &w->timer); |
|
|
3382 | ev_unref (EV_A); |
|
|
3383 | } |
3149 | |
3384 | |
3150 | ev_start (EV_A_ (W)w, 1); |
3385 | ev_start (EV_A_ (W)w, 1); |
3151 | |
3386 | |
3152 | EV_FREQUENT_CHECK; |
3387 | EV_FREQUENT_CHECK; |
3153 | } |
3388 | } |
… | |
… | |
3162 | EV_FREQUENT_CHECK; |
3397 | EV_FREQUENT_CHECK; |
3163 | |
3398 | |
3164 | #if EV_USE_INOTIFY |
3399 | #if EV_USE_INOTIFY |
3165 | infy_del (EV_A_ w); |
3400 | infy_del (EV_A_ w); |
3166 | #endif |
3401 | #endif |
|
|
3402 | |
|
|
3403 | if (ev_is_active (&w->timer)) |
|
|
3404 | { |
|
|
3405 | ev_ref (EV_A); |
3167 | ev_timer_stop (EV_A_ &w->timer); |
3406 | ev_timer_stop (EV_A_ &w->timer); |
|
|
3407 | } |
3168 | |
3408 | |
3169 | ev_stop (EV_A_ (W)w); |
3409 | ev_stop (EV_A_ (W)w); |
3170 | |
3410 | |
3171 | EV_FREQUENT_CHECK; |
3411 | EV_FREQUENT_CHECK; |
3172 | } |
3412 | } |
… | |
… | |
3217 | |
3457 | |
3218 | EV_FREQUENT_CHECK; |
3458 | EV_FREQUENT_CHECK; |
3219 | } |
3459 | } |
3220 | #endif |
3460 | #endif |
3221 | |
3461 | |
|
|
3462 | #if EV_PREPARE_ENABLE |
3222 | void |
3463 | void |
3223 | ev_prepare_start (EV_P_ ev_prepare *w) |
3464 | ev_prepare_start (EV_P_ ev_prepare *w) |
3224 | { |
3465 | { |
3225 | if (expect_false (ev_is_active (w))) |
3466 | if (expect_false (ev_is_active (w))) |
3226 | return; |
3467 | return; |
… | |
… | |
3252 | |
3493 | |
3253 | ev_stop (EV_A_ (W)w); |
3494 | ev_stop (EV_A_ (W)w); |
3254 | |
3495 | |
3255 | EV_FREQUENT_CHECK; |
3496 | EV_FREQUENT_CHECK; |
3256 | } |
3497 | } |
|
|
3498 | #endif |
3257 | |
3499 | |
|
|
3500 | #if EV_CHECK_ENABLE |
3258 | void |
3501 | void |
3259 | ev_check_start (EV_P_ ev_check *w) |
3502 | ev_check_start (EV_P_ ev_check *w) |
3260 | { |
3503 | { |
3261 | if (expect_false (ev_is_active (w))) |
3504 | if (expect_false (ev_is_active (w))) |
3262 | return; |
3505 | return; |
… | |
… | |
3288 | |
3531 | |
3289 | ev_stop (EV_A_ (W)w); |
3532 | ev_stop (EV_A_ (W)w); |
3290 | |
3533 | |
3291 | EV_FREQUENT_CHECK; |
3534 | EV_FREQUENT_CHECK; |
3292 | } |
3535 | } |
|
|
3536 | #endif |
3293 | |
3537 | |
3294 | #if EV_EMBED_ENABLE |
3538 | #if EV_EMBED_ENABLE |
3295 | void noinline |
3539 | void noinline |
3296 | ev_embed_sweep (EV_P_ ev_embed *w) |
3540 | ev_embed_sweep (EV_P_ ev_embed *w) |
3297 | { |
3541 | { |
3298 | ev_loop (w->other, EVLOOP_NONBLOCK); |
3542 | ev_run (w->other, EVRUN_NOWAIT); |
3299 | } |
3543 | } |
3300 | |
3544 | |
3301 | static void |
3545 | static void |
3302 | embed_io_cb (EV_P_ ev_io *io, int revents) |
3546 | embed_io_cb (EV_P_ ev_io *io, int revents) |
3303 | { |
3547 | { |
3304 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
3548 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
3305 | |
3549 | |
3306 | if (ev_cb (w)) |
3550 | if (ev_cb (w)) |
3307 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
3551 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
3308 | else |
3552 | else |
3309 | ev_loop (w->other, EVLOOP_NONBLOCK); |
3553 | ev_run (w->other, EVRUN_NOWAIT); |
3310 | } |
3554 | } |
3311 | |
3555 | |
3312 | static void |
3556 | static void |
3313 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3557 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3314 | { |
3558 | { |
… | |
… | |
3318 | EV_P = w->other; |
3562 | EV_P = w->other; |
3319 | |
3563 | |
3320 | while (fdchangecnt) |
3564 | while (fdchangecnt) |
3321 | { |
3565 | { |
3322 | fd_reify (EV_A); |
3566 | fd_reify (EV_A); |
3323 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3567 | ev_run (EV_A_ EVRUN_NOWAIT); |
3324 | } |
3568 | } |
3325 | } |
3569 | } |
3326 | } |
3570 | } |
3327 | |
3571 | |
3328 | static void |
3572 | static void |
… | |
… | |
3334 | |
3578 | |
3335 | { |
3579 | { |
3336 | EV_P = w->other; |
3580 | EV_P = w->other; |
3337 | |
3581 | |
3338 | ev_loop_fork (EV_A); |
3582 | ev_loop_fork (EV_A); |
3339 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3583 | ev_run (EV_A_ EVRUN_NOWAIT); |
3340 | } |
3584 | } |
3341 | |
3585 | |
3342 | ev_embed_start (EV_A_ w); |
3586 | ev_embed_start (EV_A_ w); |
3343 | } |
3587 | } |
3344 | |
3588 | |
… | |
… | |
3392 | |
3636 | |
3393 | ev_io_stop (EV_A_ &w->io); |
3637 | ev_io_stop (EV_A_ &w->io); |
3394 | ev_prepare_stop (EV_A_ &w->prepare); |
3638 | ev_prepare_stop (EV_A_ &w->prepare); |
3395 | ev_fork_stop (EV_A_ &w->fork); |
3639 | ev_fork_stop (EV_A_ &w->fork); |
3396 | |
3640 | |
|
|
3641 | ev_stop (EV_A_ (W)w); |
|
|
3642 | |
3397 | EV_FREQUENT_CHECK; |
3643 | EV_FREQUENT_CHECK; |
3398 | } |
3644 | } |
3399 | #endif |
3645 | #endif |
3400 | |
3646 | |
3401 | #if EV_FORK_ENABLE |
3647 | #if EV_FORK_ENABLE |
… | |
… | |
3434 | |
3680 | |
3435 | EV_FREQUENT_CHECK; |
3681 | EV_FREQUENT_CHECK; |
3436 | } |
3682 | } |
3437 | #endif |
3683 | #endif |
3438 | |
3684 | |
3439 | #if EV_ASYNC_ENABLE |
3685 | #if EV_CLEANUP_ENABLE |
3440 | void |
3686 | void |
3441 | ev_async_start (EV_P_ ev_async *w) |
3687 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
3442 | { |
3688 | { |
3443 | if (expect_false (ev_is_active (w))) |
3689 | if (expect_false (ev_is_active (w))) |
3444 | return; |
3690 | return; |
|
|
3691 | |
|
|
3692 | EV_FREQUENT_CHECK; |
|
|
3693 | |
|
|
3694 | ev_start (EV_A_ (W)w, ++cleanupcnt); |
|
|
3695 | array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); |
|
|
3696 | cleanups [cleanupcnt - 1] = w; |
|
|
3697 | |
|
|
3698 | /* cleanup watchers should never keep a refcount on the loop */ |
|
|
3699 | ev_unref (EV_A); |
|
|
3700 | EV_FREQUENT_CHECK; |
|
|
3701 | } |
|
|
3702 | |
|
|
3703 | void |
|
|
3704 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
|
|
3705 | { |
|
|
3706 | clear_pending (EV_A_ (W)w); |
|
|
3707 | if (expect_false (!ev_is_active (w))) |
|
|
3708 | return; |
|
|
3709 | |
|
|
3710 | EV_FREQUENT_CHECK; |
|
|
3711 | ev_ref (EV_A); |
|
|
3712 | |
|
|
3713 | { |
|
|
3714 | int active = ev_active (w); |
|
|
3715 | |
|
|
3716 | cleanups [active - 1] = cleanups [--cleanupcnt]; |
|
|
3717 | ev_active (cleanups [active - 1]) = active; |
|
|
3718 | } |
|
|
3719 | |
|
|
3720 | ev_stop (EV_A_ (W)w); |
|
|
3721 | |
|
|
3722 | EV_FREQUENT_CHECK; |
|
|
3723 | } |
|
|
3724 | #endif |
|
|
3725 | |
|
|
3726 | #if EV_ASYNC_ENABLE |
|
|
3727 | void |
|
|
3728 | ev_async_start (EV_P_ ev_async *w) |
|
|
3729 | { |
|
|
3730 | if (expect_false (ev_is_active (w))) |
|
|
3731 | return; |
|
|
3732 | |
|
|
3733 | w->sent = 0; |
3445 | |
3734 | |
3446 | evpipe_init (EV_A); |
3735 | evpipe_init (EV_A); |
3447 | |
3736 | |
3448 | EV_FREQUENT_CHECK; |
3737 | EV_FREQUENT_CHECK; |
3449 | |
3738 | |
… | |
… | |
3527 | { |
3816 | { |
3528 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
3817 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
3529 | |
3818 | |
3530 | if (expect_false (!once)) |
3819 | if (expect_false (!once)) |
3531 | { |
3820 | { |
3532 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
3821 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg); |
3533 | return; |
3822 | return; |
3534 | } |
3823 | } |
3535 | |
3824 | |
3536 | once->cb = cb; |
3825 | once->cb = cb; |
3537 | once->arg = arg; |
3826 | once->arg = arg; |
… | |
… | |
3624 | if (types & EV_ASYNC) |
3913 | if (types & EV_ASYNC) |
3625 | for (i = asynccnt; i--; ) |
3914 | for (i = asynccnt; i--; ) |
3626 | cb (EV_A_ EV_ASYNC, asyncs [i]); |
3915 | cb (EV_A_ EV_ASYNC, asyncs [i]); |
3627 | #endif |
3916 | #endif |
3628 | |
3917 | |
|
|
3918 | #if EV_PREPARE_ENABLE |
3629 | if (types & EV_PREPARE) |
3919 | if (types & EV_PREPARE) |
3630 | for (i = preparecnt; i--; ) |
3920 | for (i = preparecnt; i--; ) |
3631 | #if EV_EMBED_ENABLE |
3921 | # if EV_EMBED_ENABLE |
3632 | if (ev_cb (prepares [i]) != embed_prepare_cb) |
3922 | if (ev_cb (prepares [i]) != embed_prepare_cb) |
3633 | #endif |
3923 | # endif |
3634 | cb (EV_A_ EV_PREPARE, prepares [i]); |
3924 | cb (EV_A_ EV_PREPARE, prepares [i]); |
|
|
3925 | #endif |
3635 | |
3926 | |
|
|
3927 | #if EV_CHECK_ENABLE |
3636 | if (types & EV_CHECK) |
3928 | if (types & EV_CHECK) |
3637 | for (i = checkcnt; i--; ) |
3929 | for (i = checkcnt; i--; ) |
3638 | cb (EV_A_ EV_CHECK, checks [i]); |
3930 | cb (EV_A_ EV_CHECK, checks [i]); |
|
|
3931 | #endif |
3639 | |
3932 | |
|
|
3933 | #if EV_SIGNAL_ENABLE |
3640 | if (types & EV_SIGNAL) |
3934 | if (types & EV_SIGNAL) |
3641 | for (i = 0; i < EV_NSIG - 1; ++i) |
3935 | for (i = 0; i < EV_NSIG - 1; ++i) |
3642 | for (wl = signals [i].head; wl; ) |
3936 | for (wl = signals [i].head; wl; ) |
3643 | { |
3937 | { |
3644 | wn = wl->next; |
3938 | wn = wl->next; |
3645 | cb (EV_A_ EV_SIGNAL, wl); |
3939 | cb (EV_A_ EV_SIGNAL, wl); |
3646 | wl = wn; |
3940 | wl = wn; |
3647 | } |
3941 | } |
|
|
3942 | #endif |
3648 | |
3943 | |
|
|
3944 | #if EV_CHILD_ENABLE |
3649 | if (types & EV_CHILD) |
3945 | if (types & EV_CHILD) |
3650 | for (i = EV_PID_HASHSIZE; i--; ) |
3946 | for (i = (EV_PID_HASHSIZE); i--; ) |
3651 | for (wl = childs [i]; wl; ) |
3947 | for (wl = childs [i]; wl; ) |
3652 | { |
3948 | { |
3653 | wn = wl->next; |
3949 | wn = wl->next; |
3654 | cb (EV_A_ EV_CHILD, wl); |
3950 | cb (EV_A_ EV_CHILD, wl); |
3655 | wl = wn; |
3951 | wl = wn; |
3656 | } |
3952 | } |
|
|
3953 | #endif |
3657 | /* EV_STAT 0x00001000 /* stat data changed */ |
3954 | /* EV_STAT 0x00001000 /* stat data changed */ |
3658 | /* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ |
3955 | /* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ |
3659 | } |
3956 | } |
3660 | #endif |
3957 | #endif |
3661 | |
3958 | |
3662 | #if EV_MULTIPLICITY |
3959 | #if EV_MULTIPLICITY |
3663 | #include "ev_wrap.h" |
3960 | #include "ev_wrap.h" |
3664 | #endif |
3961 | #endif |
3665 | |
3962 | |
3666 | #ifdef __cplusplus |
3963 | EV_CPP(}) |
3667 | } |
|
|
3668 | #endif |
|
|
3669 | |
3964 | |