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