… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #ifndef EV_EMBED |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
|
|
33 | |
|
|
34 | # if HAVE_CLOCK_GETTIME |
|
|
35 | # define EV_USE_MONOTONIC 1 |
|
|
36 | # define EV_USE_REALTIME 1 |
|
|
37 | # endif |
|
|
38 | |
|
|
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
40 | # define EV_USE_SELECT 1 |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_POLL && HAVE_POLL_H |
|
|
44 | # define EV_USE_POLL 1 |
|
|
45 | # endif |
|
|
46 | |
|
|
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
48 | # define EV_USE_EPOLL 1 |
|
|
49 | # endif |
|
|
50 | |
|
|
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
52 | # define EV_USE_KQUEUE 1 |
|
|
53 | # endif |
|
|
54 | |
33 | #endif |
55 | #endif |
34 | |
56 | |
35 | #include <math.h> |
57 | #include <math.h> |
36 | #include <stdlib.h> |
58 | #include <stdlib.h> |
37 | #include <unistd.h> |
|
|
38 | #include <fcntl.h> |
59 | #include <fcntl.h> |
39 | #include <signal.h> |
|
|
40 | #include <stddef.h> |
60 | #include <stddef.h> |
41 | |
61 | |
42 | #include <stdio.h> |
62 | #include <stdio.h> |
43 | |
63 | |
44 | #include <assert.h> |
64 | #include <assert.h> |
45 | #include <errno.h> |
65 | #include <errno.h> |
46 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #include <signal.h> |
|
|
70 | |
47 | #ifndef WIN32 |
71 | #ifndef WIN32 |
|
|
72 | # include <unistd.h> |
|
|
73 | # include <sys/time.h> |
48 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
49 | #endif |
75 | #endif |
50 | #include <sys/time.h> |
|
|
51 | #include <time.h> |
|
|
52 | |
|
|
53 | /**/ |
76 | /**/ |
54 | |
77 | |
55 | #ifndef EV_USE_MONOTONIC |
78 | #ifndef EV_USE_MONOTONIC |
56 | # define EV_USE_MONOTONIC 1 |
79 | # define EV_USE_MONOTONIC 1 |
57 | #endif |
80 | #endif |
58 | |
81 | |
59 | #ifndef EV_USE_SELECT |
82 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
83 | # define EV_USE_SELECT 1 |
61 | #endif |
84 | #endif |
62 | |
85 | |
63 | #ifndef EV_USEV_POLL |
86 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
87 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
88 | #endif |
66 | |
89 | |
67 | #ifndef EV_USE_EPOLL |
90 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
91 | # define EV_USE_EPOLL 0 |
69 | #endif |
92 | #endif |
70 | |
93 | |
71 | #ifndef EV_USE_KQUEUE |
94 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
95 | # define EV_USE_KQUEUE 0 |
|
|
96 | #endif |
|
|
97 | |
|
|
98 | #ifndef EV_USE_WIN32 |
|
|
99 | # ifdef WIN32 |
|
|
100 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
101 | # undef EV_USE_SELECT |
|
|
102 | # define EV_USE_SELECT 1 |
|
|
103 | # else |
|
|
104 | # define EV_USE_WIN32 0 |
|
|
105 | # endif |
73 | #endif |
106 | #endif |
74 | |
107 | |
75 | #ifndef EV_USE_REALTIME |
108 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
109 | # define EV_USE_REALTIME 1 |
77 | #endif |
110 | #endif |
… | |
… | |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
97 | |
130 | |
98 | #ifndef EV_EMBED |
|
|
99 | # include "ev.h" |
131 | #include "ev.h" |
100 | #endif |
|
|
101 | |
132 | |
102 | #if __GNUC__ >= 3 |
133 | #if __GNUC__ >= 3 |
103 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
104 | # define inline inline |
135 | # define inline inline |
105 | #else |
136 | #else |
… | |
… | |
117 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
118 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
119 | |
150 | |
120 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
121 | |
152 | |
|
|
153 | #include "ev_win32.c" |
|
|
154 | |
122 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
123 | |
156 | |
|
|
157 | static void (*syserr_cb)(const char *msg); |
|
|
158 | |
|
|
159 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
160 | { |
|
|
161 | syserr_cb = cb; |
|
|
162 | } |
|
|
163 | |
|
|
164 | static void |
|
|
165 | syserr (const char *msg) |
|
|
166 | { |
|
|
167 | if (!msg) |
|
|
168 | msg = "(libev) system error"; |
|
|
169 | |
|
|
170 | if (syserr_cb) |
|
|
171 | syserr_cb (msg); |
|
|
172 | else |
|
|
173 | { |
|
|
174 | perror (msg); |
|
|
175 | abort (); |
|
|
176 | } |
|
|
177 | } |
|
|
178 | |
|
|
179 | static void *(*alloc)(void *ptr, long size); |
|
|
180 | |
|
|
181 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
182 | { |
|
|
183 | alloc = cb; |
|
|
184 | } |
|
|
185 | |
|
|
186 | static void * |
|
|
187 | ev_realloc (void *ptr, long size) |
|
|
188 | { |
|
|
189 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
190 | |
|
|
191 | if (!ptr && size) |
|
|
192 | { |
|
|
193 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
194 | abort (); |
|
|
195 | } |
|
|
196 | |
|
|
197 | return ptr; |
|
|
198 | } |
|
|
199 | |
|
|
200 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
201 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
202 | |
|
|
203 | /*****************************************************************************/ |
|
|
204 | |
124 | typedef struct |
205 | typedef struct |
125 | { |
206 | { |
126 | struct ev_watcher_list *head; |
207 | WL head; |
127 | unsigned char events; |
208 | unsigned char events; |
128 | unsigned char reify; |
209 | unsigned char reify; |
129 | } ANFD; |
210 | } ANFD; |
130 | |
211 | |
131 | typedef struct |
212 | typedef struct |
… | |
… | |
134 | int events; |
215 | int events; |
135 | } ANPENDING; |
216 | } ANPENDING; |
136 | |
217 | |
137 | #if EV_MULTIPLICITY |
218 | #if EV_MULTIPLICITY |
138 | |
219 | |
139 | struct ev_loop |
220 | struct ev_loop |
140 | { |
221 | { |
141 | # define VAR(name,decl) decl; |
222 | #define VAR(name,decl) decl; |
142 | # include "ev_vars.h" |
223 | #include "ev_vars.h" |
143 | }; |
|
|
144 | # undef VAR |
224 | #undef VAR |
|
|
225 | }; |
145 | # include "ev_wrap.h" |
226 | #include "ev_wrap.h" |
|
|
227 | |
|
|
228 | struct ev_loop default_loop_struct; |
|
|
229 | static struct ev_loop *default_loop; |
146 | |
230 | |
147 | #else |
231 | #else |
148 | |
232 | |
149 | # define VAR(name,decl) static decl; |
233 | #define VAR(name,decl) static decl; |
150 | # include "ev_vars.h" |
234 | #include "ev_vars.h" |
151 | # undef VAR |
235 | #undef VAR |
|
|
236 | |
|
|
237 | static int default_loop; |
152 | |
238 | |
153 | #endif |
239 | #endif |
154 | |
240 | |
155 | /*****************************************************************************/ |
241 | /*****************************************************************************/ |
156 | |
242 | |
… | |
… | |
187 | ev_now (EV_P) |
273 | ev_now (EV_P) |
188 | { |
274 | { |
189 | return rt_now; |
275 | return rt_now; |
190 | } |
276 | } |
191 | |
277 | |
192 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
278 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
193 | |
279 | |
194 | #define array_needsize(base,cur,cnt,init) \ |
280 | #define array_needsize(type,base,cur,cnt,init) \ |
195 | if (expect_false ((cnt) > cur)) \ |
281 | if (expect_false ((cnt) > cur)) \ |
196 | { \ |
282 | { \ |
197 | int newcnt = cur; \ |
283 | int newcnt = cur; \ |
198 | do \ |
284 | do \ |
199 | { \ |
285 | { \ |
200 | newcnt = array_roundsize (base, newcnt << 1); \ |
286 | newcnt = array_roundsize (type, newcnt << 1); \ |
201 | } \ |
287 | } \ |
202 | while ((cnt) > newcnt); \ |
288 | while ((cnt) > newcnt); \ |
203 | \ |
289 | \ |
204 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
290 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
205 | init (base + cur, newcnt - cur); \ |
291 | init (base + cur, newcnt - cur); \ |
206 | cur = newcnt; \ |
292 | cur = newcnt; \ |
207 | } |
293 | } |
|
|
294 | |
|
|
295 | #define array_slim(type,stem) \ |
|
|
296 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
297 | { \ |
|
|
298 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
299 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
|
|
300 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
301 | } |
|
|
302 | |
|
|
303 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
304 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
305 | #define array_free_microshit(stem) \ |
|
|
306 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
307 | |
|
|
308 | #define array_free(stem, idx) \ |
|
|
309 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
208 | |
310 | |
209 | /*****************************************************************************/ |
311 | /*****************************************************************************/ |
210 | |
312 | |
211 | static void |
313 | static void |
212 | anfds_init (ANFD *base, int count) |
314 | anfds_init (ANFD *base, int count) |
… | |
… | |
219 | |
321 | |
220 | ++base; |
322 | ++base; |
221 | } |
323 | } |
222 | } |
324 | } |
223 | |
325 | |
224 | static void |
326 | void |
225 | event (EV_P_ W w, int events) |
327 | ev_feed_event (EV_P_ void *w, int revents) |
226 | { |
328 | { |
|
|
329 | W w_ = (W)w; |
|
|
330 | |
227 | if (w->pending) |
331 | if (w_->pending) |
228 | { |
332 | { |
229 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
333 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
230 | return; |
334 | return; |
231 | } |
335 | } |
232 | |
336 | |
233 | w->pending = ++pendingcnt [ABSPRI (w)]; |
337 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
234 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
338 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
235 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
339 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
236 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
340 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
237 | } |
341 | } |
238 | |
342 | |
239 | static void |
343 | static void |
240 | queue_events (EV_P_ W *events, int eventcnt, int type) |
344 | queue_events (EV_P_ W *events, int eventcnt, int type) |
241 | { |
345 | { |
242 | int i; |
346 | int i; |
243 | |
347 | |
244 | for (i = 0; i < eventcnt; ++i) |
348 | for (i = 0; i < eventcnt; ++i) |
245 | event (EV_A_ events [i], type); |
349 | ev_feed_event (EV_A_ events [i], type); |
246 | } |
350 | } |
247 | |
351 | |
248 | static void |
352 | inline void |
249 | fd_event (EV_P_ int fd, int events) |
353 | fd_event (EV_P_ int fd, int revents) |
250 | { |
354 | { |
251 | ANFD *anfd = anfds + fd; |
355 | ANFD *anfd = anfds + fd; |
252 | struct ev_io *w; |
356 | struct ev_io *w; |
253 | |
357 | |
254 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
358 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
255 | { |
359 | { |
256 | int ev = w->events & events; |
360 | int ev = w->events & revents; |
257 | |
361 | |
258 | if (ev) |
362 | if (ev) |
259 | event (EV_A_ (W)w, ev); |
363 | ev_feed_event (EV_A_ (W)w, ev); |
260 | } |
364 | } |
|
|
365 | } |
|
|
366 | |
|
|
367 | void |
|
|
368 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
369 | { |
|
|
370 | fd_event (EV_A_ fd, revents); |
261 | } |
371 | } |
262 | |
372 | |
263 | /*****************************************************************************/ |
373 | /*****************************************************************************/ |
264 | |
374 | |
265 | static void |
375 | static void |
… | |
… | |
278 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
388 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
279 | events |= w->events; |
389 | events |= w->events; |
280 | |
390 | |
281 | anfd->reify = 0; |
391 | anfd->reify = 0; |
282 | |
392 | |
283 | if (anfd->events != events) |
|
|
284 | { |
|
|
285 | method_modify (EV_A_ fd, anfd->events, events); |
393 | method_modify (EV_A_ fd, anfd->events, events); |
286 | anfd->events = events; |
394 | anfd->events = events; |
287 | } |
|
|
288 | } |
395 | } |
289 | |
396 | |
290 | fdchangecnt = 0; |
397 | fdchangecnt = 0; |
291 | } |
398 | } |
292 | |
399 | |
293 | static void |
400 | static void |
294 | fd_change (EV_P_ int fd) |
401 | fd_change (EV_P_ int fd) |
295 | { |
402 | { |
296 | if (anfds [fd].reify || fdchangecnt < 0) |
403 | if (anfds [fd].reify) |
297 | return; |
404 | return; |
298 | |
405 | |
299 | anfds [fd].reify = 1; |
406 | anfds [fd].reify = 1; |
300 | |
407 | |
301 | ++fdchangecnt; |
408 | ++fdchangecnt; |
302 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
409 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
303 | fdchanges [fdchangecnt - 1] = fd; |
410 | fdchanges [fdchangecnt - 1] = fd; |
304 | } |
411 | } |
305 | |
412 | |
306 | static void |
413 | static void |
307 | fd_kill (EV_P_ int fd) |
414 | fd_kill (EV_P_ int fd) |
… | |
… | |
309 | struct ev_io *w; |
416 | struct ev_io *w; |
310 | |
417 | |
311 | while ((w = (struct ev_io *)anfds [fd].head)) |
418 | while ((w = (struct ev_io *)anfds [fd].head)) |
312 | { |
419 | { |
313 | ev_io_stop (EV_A_ w); |
420 | ev_io_stop (EV_A_ w); |
314 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
421 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
315 | } |
422 | } |
|
|
423 | } |
|
|
424 | |
|
|
425 | static int |
|
|
426 | fd_valid (int fd) |
|
|
427 | { |
|
|
428 | #ifdef WIN32 |
|
|
429 | return !!win32_get_osfhandle (fd); |
|
|
430 | #else |
|
|
431 | return fcntl (fd, F_GETFD) != -1; |
|
|
432 | #endif |
316 | } |
433 | } |
317 | |
434 | |
318 | /* called on EBADF to verify fds */ |
435 | /* called on EBADF to verify fds */ |
319 | static void |
436 | static void |
320 | fd_ebadf (EV_P) |
437 | fd_ebadf (EV_P) |
321 | { |
438 | { |
322 | int fd; |
439 | int fd; |
323 | |
440 | |
324 | for (fd = 0; fd < anfdmax; ++fd) |
441 | for (fd = 0; fd < anfdmax; ++fd) |
325 | if (anfds [fd].events) |
442 | if (anfds [fd].events) |
326 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
443 | if (!fd_valid (fd) == -1 && errno == EBADF) |
327 | fd_kill (EV_A_ fd); |
444 | fd_kill (EV_A_ fd); |
328 | } |
445 | } |
329 | |
446 | |
330 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
447 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
331 | static void |
448 | static void |
332 | fd_enomem (EV_P) |
449 | fd_enomem (EV_P) |
333 | { |
450 | { |
334 | int fd = anfdmax; |
451 | int fd; |
335 | |
452 | |
336 | while (fd--) |
453 | for (fd = anfdmax; fd--; ) |
337 | if (anfds [fd].events) |
454 | if (anfds [fd].events) |
338 | { |
455 | { |
339 | close (fd); |
|
|
340 | fd_kill (EV_A_ fd); |
456 | fd_kill (EV_A_ fd); |
341 | return; |
457 | return; |
342 | } |
458 | } |
343 | } |
459 | } |
344 | |
460 | |
345 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
461 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
346 | static void |
462 | static void |
347 | fd_rearm_all (EV_P) |
463 | fd_rearm_all (EV_P) |
348 | { |
464 | { |
349 | int fd; |
465 | int fd; |
350 | |
466 | |
351 | /* this should be highly optimised to not do anything but set a flag */ |
467 | /* this should be highly optimised to not do anything but set a flag */ |
352 | for (fd = 0; fd < anfdmax; ++fd) |
468 | for (fd = 0; fd < anfdmax; ++fd) |
353 | if (anfds [fd].events) |
469 | if (anfds [fd].events) |
354 | { |
470 | { |
355 | anfds [fd].events = 0; |
471 | anfds [fd].events = 0; |
356 | fd_change (fd); |
472 | fd_change (EV_A_ fd); |
357 | } |
473 | } |
358 | } |
474 | } |
359 | |
475 | |
360 | /*****************************************************************************/ |
476 | /*****************************************************************************/ |
361 | |
477 | |
… | |
… | |
365 | WT w = heap [k]; |
481 | WT w = heap [k]; |
366 | |
482 | |
367 | while (k && heap [k >> 1]->at > w->at) |
483 | while (k && heap [k >> 1]->at > w->at) |
368 | { |
484 | { |
369 | heap [k] = heap [k >> 1]; |
485 | heap [k] = heap [k >> 1]; |
370 | heap [k]->active = k + 1; |
486 | ((W)heap [k])->active = k + 1; |
371 | k >>= 1; |
487 | k >>= 1; |
372 | } |
488 | } |
373 | |
489 | |
374 | heap [k] = w; |
490 | heap [k] = w; |
375 | heap [k]->active = k + 1; |
491 | ((W)heap [k])->active = k + 1; |
376 | |
492 | |
377 | } |
493 | } |
378 | |
494 | |
379 | static void |
495 | static void |
380 | downheap (WT *heap, int N, int k) |
496 | downheap (WT *heap, int N, int k) |
… | |
… | |
390 | |
506 | |
391 | if (w->at <= heap [j]->at) |
507 | if (w->at <= heap [j]->at) |
392 | break; |
508 | break; |
393 | |
509 | |
394 | heap [k] = heap [j]; |
510 | heap [k] = heap [j]; |
395 | heap [k]->active = k + 1; |
511 | ((W)heap [k])->active = k + 1; |
396 | k = j; |
512 | k = j; |
397 | } |
513 | } |
398 | |
514 | |
399 | heap [k] = w; |
515 | heap [k] = w; |
400 | heap [k]->active = k + 1; |
516 | ((W)heap [k])->active = k + 1; |
401 | } |
517 | } |
402 | |
518 | |
403 | /*****************************************************************************/ |
519 | /*****************************************************************************/ |
404 | |
520 | |
405 | typedef struct |
521 | typedef struct |
406 | { |
522 | { |
407 | struct ev_watcher_list *head; |
523 | WL head; |
408 | sig_atomic_t volatile gotsig; |
524 | sig_atomic_t volatile gotsig; |
409 | } ANSIG; |
525 | } ANSIG; |
410 | |
526 | |
411 | static ANSIG *signals; |
527 | static ANSIG *signals; |
412 | static int signalmax; |
528 | static int signalmax; |
413 | |
529 | |
414 | static int sigpipe [2]; |
530 | static int sigpipe [2]; |
415 | static sig_atomic_t volatile gotsig; |
531 | static sig_atomic_t volatile gotsig; |
|
|
532 | static struct ev_io sigev; |
416 | |
533 | |
417 | static void |
534 | static void |
418 | signals_init (ANSIG *base, int count) |
535 | signals_init (ANSIG *base, int count) |
419 | { |
536 | { |
420 | while (count--) |
537 | while (count--) |
… | |
… | |
427 | } |
544 | } |
428 | |
545 | |
429 | static void |
546 | static void |
430 | sighandler (int signum) |
547 | sighandler (int signum) |
431 | { |
548 | { |
|
|
549 | #if WIN32 |
|
|
550 | signal (signum, sighandler); |
|
|
551 | #endif |
|
|
552 | |
432 | signals [signum - 1].gotsig = 1; |
553 | signals [signum - 1].gotsig = 1; |
433 | |
554 | |
434 | if (!gotsig) |
555 | if (!gotsig) |
435 | { |
556 | { |
436 | int old_errno = errno; |
557 | int old_errno = errno; |
437 | gotsig = 1; |
558 | gotsig = 1; |
|
|
559 | #ifdef WIN32 |
|
|
560 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
561 | #else |
438 | write (sigpipe [1], &signum, 1); |
562 | write (sigpipe [1], &signum, 1); |
|
|
563 | #endif |
439 | errno = old_errno; |
564 | errno = old_errno; |
440 | } |
565 | } |
441 | } |
566 | } |
442 | |
567 | |
|
|
568 | void |
|
|
569 | ev_feed_signal_event (EV_P_ int signum) |
|
|
570 | { |
|
|
571 | WL w; |
|
|
572 | |
|
|
573 | #if EV_MULTIPLICITY |
|
|
574 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
575 | #endif |
|
|
576 | |
|
|
577 | --signum; |
|
|
578 | |
|
|
579 | if (signum < 0 || signum >= signalmax) |
|
|
580 | return; |
|
|
581 | |
|
|
582 | signals [signum].gotsig = 0; |
|
|
583 | |
|
|
584 | for (w = signals [signum].head; w; w = w->next) |
|
|
585 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
586 | } |
|
|
587 | |
443 | static void |
588 | static void |
444 | sigcb (EV_P_ struct ev_io *iow, int revents) |
589 | sigcb (EV_P_ struct ev_io *iow, int revents) |
445 | { |
590 | { |
446 | struct ev_watcher_list *w; |
|
|
447 | int signum; |
591 | int signum; |
448 | |
592 | |
|
|
593 | #ifdef WIN32 |
|
|
594 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
595 | #else |
449 | read (sigpipe [0], &revents, 1); |
596 | read (sigpipe [0], &revents, 1); |
|
|
597 | #endif |
450 | gotsig = 0; |
598 | gotsig = 0; |
451 | |
599 | |
452 | for (signum = signalmax; signum--; ) |
600 | for (signum = signalmax; signum--; ) |
453 | if (signals [signum].gotsig) |
601 | if (signals [signum].gotsig) |
454 | { |
602 | ev_feed_signal_event (EV_A_ signum + 1); |
455 | signals [signum].gotsig = 0; |
|
|
456 | |
|
|
457 | for (w = signals [signum].head; w; w = w->next) |
|
|
458 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
459 | } |
|
|
460 | } |
603 | } |
461 | |
604 | |
462 | static void |
605 | static void |
463 | siginit (EV_P) |
606 | siginit (EV_P) |
464 | { |
607 | { |
… | |
… | |
476 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
619 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
477 | } |
620 | } |
478 | |
621 | |
479 | /*****************************************************************************/ |
622 | /*****************************************************************************/ |
480 | |
623 | |
|
|
624 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
625 | |
481 | #ifndef WIN32 |
626 | #ifndef WIN32 |
|
|
627 | |
|
|
628 | static struct ev_signal childev; |
482 | |
629 | |
483 | #ifndef WCONTINUED |
630 | #ifndef WCONTINUED |
484 | # define WCONTINUED 0 |
631 | # define WCONTINUED 0 |
485 | #endif |
632 | #endif |
486 | |
633 | |
… | |
… | |
490 | struct ev_child *w; |
637 | struct ev_child *w; |
491 | |
638 | |
492 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
639 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
493 | if (w->pid == pid || !w->pid) |
640 | if (w->pid == pid || !w->pid) |
494 | { |
641 | { |
495 | w->priority = sw->priority; /* need to do it *now* */ |
642 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
496 | w->rpid = pid; |
643 | w->rpid = pid; |
497 | w->rstatus = status; |
644 | w->rstatus = status; |
498 | event (EV_A_ (W)w, EV_CHILD); |
645 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
499 | } |
646 | } |
500 | } |
647 | } |
501 | |
648 | |
502 | static void |
649 | static void |
503 | childcb (EV_P_ struct ev_signal *sw, int revents) |
650 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
505 | int pid, status; |
652 | int pid, status; |
506 | |
653 | |
507 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
654 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
508 | { |
655 | { |
509 | /* make sure we are called again until all childs have been reaped */ |
656 | /* make sure we are called again until all childs have been reaped */ |
510 | event (EV_A_ (W)sw, EV_SIGNAL); |
657 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
511 | |
658 | |
512 | child_reap (EV_A_ sw, pid, pid, status); |
659 | child_reap (EV_A_ sw, pid, pid, status); |
513 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
660 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
514 | } |
661 | } |
515 | } |
662 | } |
… | |
… | |
522 | # include "ev_kqueue.c" |
669 | # include "ev_kqueue.c" |
523 | #endif |
670 | #endif |
524 | #if EV_USE_EPOLL |
671 | #if EV_USE_EPOLL |
525 | # include "ev_epoll.c" |
672 | # include "ev_epoll.c" |
526 | #endif |
673 | #endif |
527 | #if EV_USEV_POLL |
674 | #if EV_USE_POLL |
528 | # include "ev_poll.c" |
675 | # include "ev_poll.c" |
529 | #endif |
676 | #endif |
530 | #if EV_USE_SELECT |
677 | #if EV_USE_SELECT |
531 | # include "ev_select.c" |
678 | # include "ev_select.c" |
532 | #endif |
679 | #endif |
… | |
… | |
584 | methods = atoi (getenv ("LIBEV_METHODS")); |
731 | methods = atoi (getenv ("LIBEV_METHODS")); |
585 | else |
732 | else |
586 | methods = EVMETHOD_ANY; |
733 | methods = EVMETHOD_ANY; |
587 | |
734 | |
588 | method = 0; |
735 | method = 0; |
|
|
736 | #if EV_USE_WIN32 |
|
|
737 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
738 | #endif |
589 | #if EV_USE_KQUEUE |
739 | #if EV_USE_KQUEUE |
590 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
740 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
591 | #endif |
741 | #endif |
592 | #if EV_USE_EPOLL |
742 | #if EV_USE_EPOLL |
593 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
743 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
594 | #endif |
744 | #endif |
595 | #if EV_USEV_POLL |
745 | #if EV_USE_POLL |
596 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
746 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
597 | #endif |
747 | #endif |
598 | #if EV_USE_SELECT |
748 | #if EV_USE_SELECT |
599 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
749 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
600 | #endif |
750 | #endif |
|
|
751 | |
|
|
752 | ev_watcher_init (&sigev, sigcb); |
|
|
753 | ev_set_priority (&sigev, EV_MAXPRI); |
601 | } |
754 | } |
602 | } |
755 | } |
603 | |
756 | |
604 | void |
757 | void |
605 | loop_destroy (EV_P) |
758 | loop_destroy (EV_P) |
606 | { |
759 | { |
|
|
760 | int i; |
|
|
761 | |
|
|
762 | #if EV_USE_WIN32 |
|
|
763 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
764 | #endif |
607 | #if EV_USE_KQUEUE |
765 | #if EV_USE_KQUEUE |
608 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
766 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
609 | #endif |
767 | #endif |
610 | #if EV_USE_EPOLL |
768 | #if EV_USE_EPOLL |
611 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
769 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
612 | #endif |
770 | #endif |
613 | #if EV_USEV_POLL |
771 | #if EV_USE_POLL |
614 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
772 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
615 | #endif |
773 | #endif |
616 | #if EV_USE_SELECT |
774 | #if EV_USE_SELECT |
617 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
775 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
618 | #endif |
776 | #endif |
619 | |
777 | |
|
|
778 | for (i = NUMPRI; i--; ) |
|
|
779 | array_free (pending, [i]); |
|
|
780 | |
|
|
781 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
782 | array_free_microshit (fdchange); |
|
|
783 | array_free_microshit (timer); |
|
|
784 | array_free_microshit (periodic); |
|
|
785 | array_free_microshit (idle); |
|
|
786 | array_free_microshit (prepare); |
|
|
787 | array_free_microshit (check); |
|
|
788 | |
620 | method = 0; |
789 | method = 0; |
621 | /*TODO*/ |
|
|
622 | } |
790 | } |
623 | |
791 | |
624 | void |
792 | static void |
625 | loop_fork (EV_P) |
793 | loop_fork (EV_P) |
626 | { |
794 | { |
627 | /*TODO*/ |
|
|
628 | #if EV_USE_EPOLL |
795 | #if EV_USE_EPOLL |
629 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
796 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
630 | #endif |
797 | #endif |
631 | #if EV_USE_KQUEUE |
798 | #if EV_USE_KQUEUE |
632 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
799 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
633 | #endif |
800 | #endif |
|
|
801 | |
|
|
802 | if (ev_is_active (&sigev)) |
|
|
803 | { |
|
|
804 | /* default loop */ |
|
|
805 | |
|
|
806 | ev_ref (EV_A); |
|
|
807 | ev_io_stop (EV_A_ &sigev); |
|
|
808 | close (sigpipe [0]); |
|
|
809 | close (sigpipe [1]); |
|
|
810 | |
|
|
811 | while (pipe (sigpipe)) |
|
|
812 | syserr ("(libev) error creating pipe"); |
|
|
813 | |
|
|
814 | siginit (EV_A); |
|
|
815 | } |
|
|
816 | |
|
|
817 | postfork = 0; |
634 | } |
818 | } |
635 | |
819 | |
636 | #if EV_MULTIPLICITY |
820 | #if EV_MULTIPLICITY |
637 | struct ev_loop * |
821 | struct ev_loop * |
638 | ev_loop_new (int methods) |
822 | ev_loop_new (int methods) |
639 | { |
823 | { |
640 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
824 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
825 | |
|
|
826 | memset (loop, 0, sizeof (struct ev_loop)); |
641 | |
827 | |
642 | loop_init (EV_A_ methods); |
828 | loop_init (EV_A_ methods); |
643 | |
829 | |
644 | if (ev_methods (EV_A)) |
830 | if (ev_method (EV_A)) |
645 | return loop; |
831 | return loop; |
646 | |
832 | |
647 | return 0; |
833 | return 0; |
648 | } |
834 | } |
649 | |
835 | |
650 | void |
836 | void |
651 | ev_loop_destroy (EV_P) |
837 | ev_loop_destroy (EV_P) |
652 | { |
838 | { |
653 | loop_destroy (EV_A); |
839 | loop_destroy (EV_A); |
654 | free (loop); |
840 | ev_free (loop); |
655 | } |
841 | } |
656 | |
842 | |
657 | void |
843 | void |
658 | ev_loop_fork (EV_P) |
844 | ev_loop_fork (EV_P) |
659 | { |
845 | { |
660 | loop_fork (EV_A); |
846 | postfork = 1; |
661 | } |
847 | } |
662 | |
848 | |
663 | #endif |
849 | #endif |
664 | |
850 | |
665 | #if EV_MULTIPLICITY |
851 | #if EV_MULTIPLICITY |
666 | struct ev_loop default_loop_struct; |
|
|
667 | static struct ev_loop *default_loop; |
|
|
668 | |
|
|
669 | struct ev_loop * |
852 | struct ev_loop * |
670 | #else |
853 | #else |
671 | static int default_loop; |
|
|
672 | |
|
|
673 | int |
854 | int |
674 | #endif |
855 | #endif |
675 | ev_default_loop (int methods) |
856 | ev_default_loop (int methods) |
676 | { |
857 | { |
677 | if (sigpipe [0] == sigpipe [1]) |
858 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
688 | |
869 | |
689 | loop_init (EV_A_ methods); |
870 | loop_init (EV_A_ methods); |
690 | |
871 | |
691 | if (ev_method (EV_A)) |
872 | if (ev_method (EV_A)) |
692 | { |
873 | { |
693 | ev_watcher_init (&sigev, sigcb); |
|
|
694 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
695 | siginit (EV_A); |
874 | siginit (EV_A); |
696 | |
875 | |
697 | #ifndef WIN32 |
876 | #ifndef WIN32 |
698 | ev_signal_init (&childev, childcb, SIGCHLD); |
877 | ev_signal_init (&childev, childcb, SIGCHLD); |
699 | ev_set_priority (&childev, EV_MAXPRI); |
878 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
713 | { |
892 | { |
714 | #if EV_MULTIPLICITY |
893 | #if EV_MULTIPLICITY |
715 | struct ev_loop *loop = default_loop; |
894 | struct ev_loop *loop = default_loop; |
716 | #endif |
895 | #endif |
717 | |
896 | |
|
|
897 | #ifndef WIN32 |
718 | ev_ref (EV_A); /* child watcher */ |
898 | ev_ref (EV_A); /* child watcher */ |
719 | ev_signal_stop (EV_A_ &childev); |
899 | ev_signal_stop (EV_A_ &childev); |
|
|
900 | #endif |
720 | |
901 | |
721 | ev_ref (EV_A); /* signal watcher */ |
902 | ev_ref (EV_A); /* signal watcher */ |
722 | ev_io_stop (EV_A_ &sigev); |
903 | ev_io_stop (EV_A_ &sigev); |
723 | |
904 | |
724 | close (sigpipe [0]); sigpipe [0] = 0; |
905 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
726 | |
907 | |
727 | loop_destroy (EV_A); |
908 | loop_destroy (EV_A); |
728 | } |
909 | } |
729 | |
910 | |
730 | void |
911 | void |
731 | ev_default_fork (EV_P) |
912 | ev_default_fork (void) |
732 | { |
913 | { |
733 | loop_fork (EV_A); |
914 | #if EV_MULTIPLICITY |
|
|
915 | struct ev_loop *loop = default_loop; |
|
|
916 | #endif |
734 | |
917 | |
735 | ev_io_stop (EV_A_ &sigev); |
918 | if (method) |
736 | close (sigpipe [0]); |
919 | postfork = 1; |
737 | close (sigpipe [1]); |
|
|
738 | pipe (sigpipe); |
|
|
739 | |
|
|
740 | ev_ref (EV_A); /* signal watcher */ |
|
|
741 | siginit (EV_A); |
|
|
742 | } |
920 | } |
743 | |
921 | |
744 | /*****************************************************************************/ |
922 | /*****************************************************************************/ |
|
|
923 | |
|
|
924 | static int |
|
|
925 | any_pending (EV_P) |
|
|
926 | { |
|
|
927 | int pri; |
|
|
928 | |
|
|
929 | for (pri = NUMPRI; pri--; ) |
|
|
930 | if (pendingcnt [pri]) |
|
|
931 | return 1; |
|
|
932 | |
|
|
933 | return 0; |
|
|
934 | } |
745 | |
935 | |
746 | static void |
936 | static void |
747 | call_pending (EV_P) |
937 | call_pending (EV_P) |
748 | { |
938 | { |
749 | int pri; |
939 | int pri; |
… | |
… | |
762 | } |
952 | } |
763 | |
953 | |
764 | static void |
954 | static void |
765 | timers_reify (EV_P) |
955 | timers_reify (EV_P) |
766 | { |
956 | { |
767 | while (timercnt && timers [0]->at <= mn_now) |
957 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
768 | { |
958 | { |
769 | struct ev_timer *w = timers [0]; |
959 | struct ev_timer *w = timers [0]; |
|
|
960 | |
|
|
961 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
770 | |
962 | |
771 | /* first reschedule or stop timer */ |
963 | /* first reschedule or stop timer */ |
772 | if (w->repeat) |
964 | if (w->repeat) |
773 | { |
965 | { |
774 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
966 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
775 | w->at = mn_now + w->repeat; |
967 | ((WT)w)->at = mn_now + w->repeat; |
776 | downheap ((WT *)timers, timercnt, 0); |
968 | downheap ((WT *)timers, timercnt, 0); |
777 | } |
969 | } |
778 | else |
970 | else |
779 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
971 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
780 | |
972 | |
781 | event (EV_A_ (W)w, EV_TIMEOUT); |
973 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
782 | } |
974 | } |
783 | } |
975 | } |
784 | |
976 | |
785 | static void |
977 | static void |
786 | periodics_reify (EV_P) |
978 | periodics_reify (EV_P) |
787 | { |
979 | { |
788 | while (periodiccnt && periodics [0]->at <= rt_now) |
980 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
789 | { |
981 | { |
790 | struct ev_periodic *w = periodics [0]; |
982 | struct ev_periodic *w = periodics [0]; |
791 | |
983 | |
|
|
984 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
|
|
985 | |
792 | /* first reschedule or stop timer */ |
986 | /* first reschedule or stop timer */ |
793 | if (w->interval) |
987 | if (w->reschedule_cb) |
794 | { |
988 | { |
|
|
989 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
990 | |
|
|
991 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
992 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
993 | } |
|
|
994 | else if (w->interval) |
|
|
995 | { |
795 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
996 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
796 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
997 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
797 | downheap ((WT *)periodics, periodiccnt, 0); |
998 | downheap ((WT *)periodics, periodiccnt, 0); |
798 | } |
999 | } |
799 | else |
1000 | else |
800 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1001 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
801 | |
1002 | |
802 | event (EV_A_ (W)w, EV_PERIODIC); |
1003 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
803 | } |
1004 | } |
804 | } |
1005 | } |
805 | |
1006 | |
806 | static void |
1007 | static void |
807 | periodics_reschedule (EV_P) |
1008 | periodics_reschedule (EV_P) |
… | |
… | |
811 | /* adjust periodics after time jump */ |
1012 | /* adjust periodics after time jump */ |
812 | for (i = 0; i < periodiccnt; ++i) |
1013 | for (i = 0; i < periodiccnt; ++i) |
813 | { |
1014 | { |
814 | struct ev_periodic *w = periodics [i]; |
1015 | struct ev_periodic *w = periodics [i]; |
815 | |
1016 | |
|
|
1017 | if (w->reschedule_cb) |
|
|
1018 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
816 | if (w->interval) |
1019 | else if (w->interval) |
817 | { |
|
|
818 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
1020 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
819 | |
|
|
820 | if (fabs (diff) >= 1e-4) |
|
|
821 | { |
|
|
822 | ev_periodic_stop (EV_A_ w); |
|
|
823 | ev_periodic_start (EV_A_ w); |
|
|
824 | |
|
|
825 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
826 | } |
|
|
827 | } |
|
|
828 | } |
1021 | } |
|
|
1022 | |
|
|
1023 | /* now rebuild the heap */ |
|
|
1024 | for (i = periodiccnt >> 1; i--; ) |
|
|
1025 | downheap ((WT *)periodics, periodiccnt, i); |
829 | } |
1026 | } |
830 | |
1027 | |
831 | inline int |
1028 | inline int |
832 | time_update_monotonic (EV_P) |
1029 | time_update_monotonic (EV_P) |
833 | { |
1030 | { |
… | |
… | |
884 | { |
1081 | { |
885 | periodics_reschedule (EV_A); |
1082 | periodics_reschedule (EV_A); |
886 | |
1083 | |
887 | /* adjust timers. this is easy, as the offset is the same for all */ |
1084 | /* adjust timers. this is easy, as the offset is the same for all */ |
888 | for (i = 0; i < timercnt; ++i) |
1085 | for (i = 0; i < timercnt; ++i) |
889 | timers [i]->at += rt_now - mn_now; |
1086 | ((WT)timers [i])->at += rt_now - mn_now; |
890 | } |
1087 | } |
891 | |
1088 | |
892 | mn_now = rt_now; |
1089 | mn_now = rt_now; |
893 | } |
1090 | } |
894 | } |
1091 | } |
… | |
… | |
920 | { |
1117 | { |
921 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1118 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
922 | call_pending (EV_A); |
1119 | call_pending (EV_A); |
923 | } |
1120 | } |
924 | |
1121 | |
|
|
1122 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1123 | if (expect_false (postfork)) |
|
|
1124 | loop_fork (EV_A); |
|
|
1125 | |
925 | /* update fd-related kernel structures */ |
1126 | /* update fd-related kernel structures */ |
926 | fd_reify (EV_A); |
1127 | fd_reify (EV_A); |
927 | |
1128 | |
928 | /* calculate blocking time */ |
1129 | /* calculate blocking time */ |
929 | |
1130 | |
930 | /* we only need this for !monotonic clockor timers, but as we basically |
1131 | /* we only need this for !monotonic clock or timers, but as we basically |
931 | always have timers, we just calculate it always */ |
1132 | always have timers, we just calculate it always */ |
932 | #if EV_USE_MONOTONIC |
1133 | #if EV_USE_MONOTONIC |
933 | if (expect_true (have_monotonic)) |
1134 | if (expect_true (have_monotonic)) |
934 | time_update_monotonic (EV_A); |
1135 | time_update_monotonic (EV_A); |
935 | else |
1136 | else |
… | |
… | |
945 | { |
1146 | { |
946 | block = MAX_BLOCKTIME; |
1147 | block = MAX_BLOCKTIME; |
947 | |
1148 | |
948 | if (timercnt) |
1149 | if (timercnt) |
949 | { |
1150 | { |
950 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1151 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
951 | if (block > to) block = to; |
1152 | if (block > to) block = to; |
952 | } |
1153 | } |
953 | |
1154 | |
954 | if (periodiccnt) |
1155 | if (periodiccnt) |
955 | { |
1156 | { |
956 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1157 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
957 | if (block > to) block = to; |
1158 | if (block > to) block = to; |
958 | } |
1159 | } |
959 | |
1160 | |
960 | if (block < 0.) block = 0.; |
1161 | if (block < 0.) block = 0.; |
961 | } |
1162 | } |
… | |
… | |
968 | /* queue pending timers and reschedule them */ |
1169 | /* queue pending timers and reschedule them */ |
969 | timers_reify (EV_A); /* relative timers called last */ |
1170 | timers_reify (EV_A); /* relative timers called last */ |
970 | periodics_reify (EV_A); /* absolute timers called first */ |
1171 | periodics_reify (EV_A); /* absolute timers called first */ |
971 | |
1172 | |
972 | /* queue idle watchers unless io or timers are pending */ |
1173 | /* queue idle watchers unless io or timers are pending */ |
973 | if (!pendingcnt) |
1174 | if (idlecnt && !any_pending (EV_A)) |
974 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1175 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
975 | |
1176 | |
976 | /* queue check watchers, to be executed first */ |
1177 | /* queue check watchers, to be executed first */ |
977 | if (checkcnt) |
1178 | if (checkcnt) |
978 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1179 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1053 | return; |
1254 | return; |
1054 | |
1255 | |
1055 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1256 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1056 | |
1257 | |
1057 | ev_start (EV_A_ (W)w, 1); |
1258 | ev_start (EV_A_ (W)w, 1); |
1058 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1259 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1059 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1260 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1060 | |
1261 | |
1061 | fd_change (EV_A_ fd); |
1262 | fd_change (EV_A_ fd); |
1062 | } |
1263 | } |
1063 | |
1264 | |
… | |
… | |
1078 | ev_timer_start (EV_P_ struct ev_timer *w) |
1279 | ev_timer_start (EV_P_ struct ev_timer *w) |
1079 | { |
1280 | { |
1080 | if (ev_is_active (w)) |
1281 | if (ev_is_active (w)) |
1081 | return; |
1282 | return; |
1082 | |
1283 | |
1083 | w->at += mn_now; |
1284 | ((WT)w)->at += mn_now; |
1084 | |
1285 | |
1085 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1286 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1086 | |
1287 | |
1087 | ev_start (EV_A_ (W)w, ++timercnt); |
1288 | ev_start (EV_A_ (W)w, ++timercnt); |
1088 | array_needsize (timers, timermax, timercnt, ); |
1289 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1089 | timers [timercnt - 1] = w; |
1290 | timers [timercnt - 1] = w; |
1090 | upheap ((WT *)timers, timercnt - 1); |
1291 | upheap ((WT *)timers, timercnt - 1); |
|
|
1292 | |
|
|
1293 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1091 | } |
1294 | } |
1092 | |
1295 | |
1093 | void |
1296 | void |
1094 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1297 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1095 | { |
1298 | { |
1096 | ev_clear_pending (EV_A_ (W)w); |
1299 | ev_clear_pending (EV_A_ (W)w); |
1097 | if (!ev_is_active (w)) |
1300 | if (!ev_is_active (w)) |
1098 | return; |
1301 | return; |
1099 | |
1302 | |
|
|
1303 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1304 | |
1100 | if (w->active < timercnt--) |
1305 | if (((W)w)->active < timercnt--) |
1101 | { |
1306 | { |
1102 | timers [w->active - 1] = timers [timercnt]; |
1307 | timers [((W)w)->active - 1] = timers [timercnt]; |
1103 | downheap ((WT *)timers, timercnt, w->active - 1); |
1308 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1104 | } |
1309 | } |
1105 | |
1310 | |
1106 | w->at = w->repeat; |
1311 | ((WT)w)->at = w->repeat; |
1107 | |
1312 | |
1108 | ev_stop (EV_A_ (W)w); |
1313 | ev_stop (EV_A_ (W)w); |
1109 | } |
1314 | } |
1110 | |
1315 | |
1111 | void |
1316 | void |
… | |
… | |
1113 | { |
1318 | { |
1114 | if (ev_is_active (w)) |
1319 | if (ev_is_active (w)) |
1115 | { |
1320 | { |
1116 | if (w->repeat) |
1321 | if (w->repeat) |
1117 | { |
1322 | { |
1118 | w->at = mn_now + w->repeat; |
1323 | ((WT)w)->at = mn_now + w->repeat; |
1119 | downheap ((WT *)timers, timercnt, w->active - 1); |
1324 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1120 | } |
1325 | } |
1121 | else |
1326 | else |
1122 | ev_timer_stop (EV_A_ w); |
1327 | ev_timer_stop (EV_A_ w); |
1123 | } |
1328 | } |
1124 | else if (w->repeat) |
1329 | else if (w->repeat) |
… | |
… | |
1129 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1334 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1130 | { |
1335 | { |
1131 | if (ev_is_active (w)) |
1336 | if (ev_is_active (w)) |
1132 | return; |
1337 | return; |
1133 | |
1338 | |
|
|
1339 | if (w->reschedule_cb) |
|
|
1340 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1341 | else if (w->interval) |
|
|
1342 | { |
1134 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1343 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1135 | |
|
|
1136 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1344 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1137 | if (w->interval) |
|
|
1138 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1345 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1346 | } |
1139 | |
1347 | |
1140 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1348 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1141 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1349 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1142 | periodics [periodiccnt - 1] = w; |
1350 | periodics [periodiccnt - 1] = w; |
1143 | upheap ((WT *)periodics, periodiccnt - 1); |
1351 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1352 | |
|
|
1353 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1144 | } |
1354 | } |
1145 | |
1355 | |
1146 | void |
1356 | void |
1147 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1357 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1148 | { |
1358 | { |
1149 | ev_clear_pending (EV_A_ (W)w); |
1359 | ev_clear_pending (EV_A_ (W)w); |
1150 | if (!ev_is_active (w)) |
1360 | if (!ev_is_active (w)) |
1151 | return; |
1361 | return; |
1152 | |
1362 | |
|
|
1363 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1364 | |
1153 | if (w->active < periodiccnt--) |
1365 | if (((W)w)->active < periodiccnt--) |
1154 | { |
1366 | { |
1155 | periodics [w->active - 1] = periodics [periodiccnt]; |
1367 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1156 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1368 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1157 | } |
1369 | } |
1158 | |
1370 | |
1159 | ev_stop (EV_A_ (W)w); |
1371 | ev_stop (EV_A_ (W)w); |
1160 | } |
1372 | } |
1161 | |
1373 | |
1162 | void |
1374 | void |
|
|
1375 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1376 | { |
|
|
1377 | ev_periodic_stop (EV_A_ w); |
|
|
1378 | ev_periodic_start (EV_A_ w); |
|
|
1379 | } |
|
|
1380 | |
|
|
1381 | void |
1163 | ev_idle_start (EV_P_ struct ev_idle *w) |
1382 | ev_idle_start (EV_P_ struct ev_idle *w) |
1164 | { |
1383 | { |
1165 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1166 | return; |
1385 | return; |
1167 | |
1386 | |
1168 | ev_start (EV_A_ (W)w, ++idlecnt); |
1387 | ev_start (EV_A_ (W)w, ++idlecnt); |
1169 | array_needsize (idles, idlemax, idlecnt, ); |
1388 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1170 | idles [idlecnt - 1] = w; |
1389 | idles [idlecnt - 1] = w; |
1171 | } |
1390 | } |
1172 | |
1391 | |
1173 | void |
1392 | void |
1174 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1393 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1175 | { |
1394 | { |
1176 | ev_clear_pending (EV_A_ (W)w); |
1395 | ev_clear_pending (EV_A_ (W)w); |
1177 | if (ev_is_active (w)) |
1396 | if (ev_is_active (w)) |
1178 | return; |
1397 | return; |
1179 | |
1398 | |
1180 | idles [w->active - 1] = idles [--idlecnt]; |
1399 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1181 | ev_stop (EV_A_ (W)w); |
1400 | ev_stop (EV_A_ (W)w); |
1182 | } |
1401 | } |
1183 | |
1402 | |
1184 | void |
1403 | void |
1185 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1404 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1186 | { |
1405 | { |
1187 | if (ev_is_active (w)) |
1406 | if (ev_is_active (w)) |
1188 | return; |
1407 | return; |
1189 | |
1408 | |
1190 | ev_start (EV_A_ (W)w, ++preparecnt); |
1409 | ev_start (EV_A_ (W)w, ++preparecnt); |
1191 | array_needsize (prepares, preparemax, preparecnt, ); |
1410 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1192 | prepares [preparecnt - 1] = w; |
1411 | prepares [preparecnt - 1] = w; |
1193 | } |
1412 | } |
1194 | |
1413 | |
1195 | void |
1414 | void |
1196 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1415 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1197 | { |
1416 | { |
1198 | ev_clear_pending (EV_A_ (W)w); |
1417 | ev_clear_pending (EV_A_ (W)w); |
1199 | if (ev_is_active (w)) |
1418 | if (ev_is_active (w)) |
1200 | return; |
1419 | return; |
1201 | |
1420 | |
1202 | prepares [w->active - 1] = prepares [--preparecnt]; |
1421 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1203 | ev_stop (EV_A_ (W)w); |
1422 | ev_stop (EV_A_ (W)w); |
1204 | } |
1423 | } |
1205 | |
1424 | |
1206 | void |
1425 | void |
1207 | ev_check_start (EV_P_ struct ev_check *w) |
1426 | ev_check_start (EV_P_ struct ev_check *w) |
1208 | { |
1427 | { |
1209 | if (ev_is_active (w)) |
1428 | if (ev_is_active (w)) |
1210 | return; |
1429 | return; |
1211 | |
1430 | |
1212 | ev_start (EV_A_ (W)w, ++checkcnt); |
1431 | ev_start (EV_A_ (W)w, ++checkcnt); |
1213 | array_needsize (checks, checkmax, checkcnt, ); |
1432 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1214 | checks [checkcnt - 1] = w; |
1433 | checks [checkcnt - 1] = w; |
1215 | } |
1434 | } |
1216 | |
1435 | |
1217 | void |
1436 | void |
1218 | ev_check_stop (EV_P_ struct ev_check *w) |
1437 | ev_check_stop (EV_P_ struct ev_check *w) |
1219 | { |
1438 | { |
1220 | ev_clear_pending (EV_A_ (W)w); |
1439 | ev_clear_pending (EV_A_ (W)w); |
1221 | if (ev_is_active (w)) |
1440 | if (ev_is_active (w)) |
1222 | return; |
1441 | return; |
1223 | |
1442 | |
1224 | checks [w->active - 1] = checks [--checkcnt]; |
1443 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1225 | ev_stop (EV_A_ (W)w); |
1444 | ev_stop (EV_A_ (W)w); |
1226 | } |
1445 | } |
1227 | |
1446 | |
1228 | #ifndef SA_RESTART |
1447 | #ifndef SA_RESTART |
1229 | # define SA_RESTART 0 |
1448 | # define SA_RESTART 0 |
… | |
… | |
1239 | return; |
1458 | return; |
1240 | |
1459 | |
1241 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1460 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1242 | |
1461 | |
1243 | ev_start (EV_A_ (W)w, 1); |
1462 | ev_start (EV_A_ (W)w, 1); |
1244 | array_needsize (signals, signalmax, w->signum, signals_init); |
1463 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1245 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1464 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1246 | |
1465 | |
1247 | if (!w->next) |
1466 | if (!((WL)w)->next) |
1248 | { |
1467 | { |
|
|
1468 | #if WIN32 |
|
|
1469 | signal (w->signum, sighandler); |
|
|
1470 | #else |
1249 | struct sigaction sa; |
1471 | struct sigaction sa; |
1250 | sa.sa_handler = sighandler; |
1472 | sa.sa_handler = sighandler; |
1251 | sigfillset (&sa.sa_mask); |
1473 | sigfillset (&sa.sa_mask); |
1252 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1474 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1253 | sigaction (w->signum, &sa, 0); |
1475 | sigaction (w->signum, &sa, 0); |
|
|
1476 | #endif |
1254 | } |
1477 | } |
1255 | } |
1478 | } |
1256 | |
1479 | |
1257 | void |
1480 | void |
1258 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1481 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1308 | void (*cb)(int revents, void *arg) = once->cb; |
1531 | void (*cb)(int revents, void *arg) = once->cb; |
1309 | void *arg = once->arg; |
1532 | void *arg = once->arg; |
1310 | |
1533 | |
1311 | ev_io_stop (EV_A_ &once->io); |
1534 | ev_io_stop (EV_A_ &once->io); |
1312 | ev_timer_stop (EV_A_ &once->to); |
1535 | ev_timer_stop (EV_A_ &once->to); |
1313 | free (once); |
1536 | ev_free (once); |
1314 | |
1537 | |
1315 | cb (revents, arg); |
1538 | cb (revents, arg); |
1316 | } |
1539 | } |
1317 | |
1540 | |
1318 | static void |
1541 | static void |
… | |
… | |
1328 | } |
1551 | } |
1329 | |
1552 | |
1330 | void |
1553 | void |
1331 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1554 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1332 | { |
1555 | { |
1333 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1556 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1334 | |
1557 | |
1335 | if (!once) |
1558 | if (!once) |
1336 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1559 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1337 | else |
1560 | else |
1338 | { |
1561 | { |