… | |
… | |
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_STANDALONE |
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> |
59 | #include <unistd.h> |
… | |
… | |
58 | |
80 | |
59 | #ifndef EV_USE_SELECT |
81 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
82 | # define EV_USE_SELECT 1 |
61 | #endif |
83 | #endif |
62 | |
84 | |
63 | #ifndef EV_USEV_POLL |
85 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
86 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
87 | #endif |
66 | |
88 | |
67 | #ifndef EV_USE_EPOLL |
89 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
90 | # define EV_USE_EPOLL 0 |
69 | #endif |
91 | #endif |
70 | |
92 | |
71 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | #ifndef EV_USE_WIN32 |
|
|
98 | # ifdef WIN32 |
|
|
99 | # define EV_USE_WIN32 1 |
|
|
100 | # else |
|
|
101 | # define EV_USE_WIN32 0 |
|
|
102 | # endif |
73 | #endif |
103 | #endif |
74 | |
104 | |
75 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
77 | #endif |
107 | #endif |
… | |
… | |
115 | typedef struct ev_watcher_list *WL; |
145 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
146 | typedef struct ev_watcher_time *WT; |
117 | |
147 | |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | |
149 | |
|
|
150 | #if WIN32 |
|
|
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
120 | /*****************************************************************************/ |
156 | /*****************************************************************************/ |
121 | |
157 | |
|
|
158 | static void (*syserr_cb)(void); |
|
|
159 | |
|
|
160 | void ev_set_syserr_cb (void (*cb)(void)) |
|
|
161 | { |
|
|
162 | syserr_cb = cb; |
|
|
163 | } |
|
|
164 | |
|
|
165 | static void |
|
|
166 | syserr (void) |
|
|
167 | { |
|
|
168 | if (syserr_cb) |
|
|
169 | syserr_cb (); |
|
|
170 | else |
|
|
171 | { |
|
|
172 | perror ("libev"); |
|
|
173 | abort (); |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | static void *(*alloc)(void *ptr, long size); |
|
|
178 | |
|
|
179 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
180 | { |
|
|
181 | alloc = cb; |
|
|
182 | } |
|
|
183 | |
|
|
184 | static void * |
|
|
185 | ev_realloc (void *ptr, long size) |
|
|
186 | { |
|
|
187 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
188 | |
|
|
189 | if (!ptr && size) |
|
|
190 | { |
|
|
191 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
192 | abort (); |
|
|
193 | } |
|
|
194 | |
|
|
195 | return ptr; |
|
|
196 | } |
|
|
197 | |
|
|
198 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
199 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
200 | |
|
|
201 | /*****************************************************************************/ |
|
|
202 | |
122 | typedef struct |
203 | typedef struct |
123 | { |
204 | { |
124 | struct ev_watcher_list *head; |
205 | WL head; |
125 | unsigned char events; |
206 | unsigned char events; |
126 | unsigned char reify; |
207 | unsigned char reify; |
127 | } ANFD; |
208 | } ANFD; |
128 | |
209 | |
129 | typedef struct |
210 | typedef struct |
… | |
… | |
187 | return rt_now; |
268 | return rt_now; |
188 | } |
269 | } |
189 | |
270 | |
190 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
271 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
191 | |
272 | |
192 | #define array_needsize(base,cur,cnt,init) \ |
273 | #define array_needsize(base,cur,cnt,init) \ |
193 | if (expect_false ((cnt) > cur)) \ |
274 | if (expect_false ((cnt) > cur)) \ |
194 | { \ |
275 | { \ |
195 | int newcnt = cur; \ |
276 | int newcnt = cur; \ |
196 | do \ |
277 | do \ |
197 | { \ |
278 | { \ |
198 | newcnt = array_roundsize (base, newcnt << 1); \ |
279 | newcnt = array_roundsize (base, newcnt << 1); \ |
199 | } \ |
280 | } \ |
200 | while ((cnt) > newcnt); \ |
281 | while ((cnt) > newcnt); \ |
201 | \ |
282 | \ |
202 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
283 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
203 | init (base + cur, newcnt - cur); \ |
284 | init (base + cur, newcnt - cur); \ |
204 | cur = newcnt; \ |
285 | cur = newcnt; \ |
205 | } |
286 | } |
|
|
287 | |
|
|
288 | #define array_slim(stem) \ |
|
|
289 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
290 | { \ |
|
|
291 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
292 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
293 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
294 | } |
|
|
295 | |
|
|
296 | #define array_free(stem, idx) \ |
|
|
297 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
206 | |
298 | |
207 | /*****************************************************************************/ |
299 | /*****************************************************************************/ |
208 | |
300 | |
209 | static void |
301 | static void |
210 | anfds_init (ANFD *base, int count) |
302 | anfds_init (ANFD *base, int count) |
… | |
… | |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
368 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
277 | events |= w->events; |
369 | events |= w->events; |
278 | |
370 | |
279 | anfd->reify = 0; |
371 | anfd->reify = 0; |
280 | |
372 | |
281 | if (anfd->events != events) |
|
|
282 | { |
|
|
283 | method_modify (EV_A_ fd, anfd->events, events); |
373 | method_modify (EV_A_ fd, anfd->events, events); |
284 | anfd->events = events; |
374 | anfd->events = events; |
285 | } |
|
|
286 | } |
375 | } |
287 | |
376 | |
288 | fdchangecnt = 0; |
377 | fdchangecnt = 0; |
289 | } |
378 | } |
290 | |
379 | |
… | |
… | |
327 | |
416 | |
328 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
417 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
329 | static void |
418 | static void |
330 | fd_enomem (EV_P) |
419 | fd_enomem (EV_P) |
331 | { |
420 | { |
332 | int fd = anfdmax; |
421 | int fd; |
333 | |
422 | |
334 | while (fd--) |
423 | for (fd = anfdmax; fd--; ) |
335 | if (anfds [fd].events) |
424 | if (anfds [fd].events) |
336 | { |
425 | { |
337 | close (fd); |
|
|
338 | fd_kill (EV_A_ fd); |
426 | fd_kill (EV_A_ fd); |
339 | return; |
427 | return; |
340 | } |
428 | } |
341 | } |
429 | } |
342 | |
430 | |
… | |
… | |
349 | /* this should be highly optimised to not do anything but set a flag */ |
437 | /* this should be highly optimised to not do anything but set a flag */ |
350 | for (fd = 0; fd < anfdmax; ++fd) |
438 | for (fd = 0; fd < anfdmax; ++fd) |
351 | if (anfds [fd].events) |
439 | if (anfds [fd].events) |
352 | { |
440 | { |
353 | anfds [fd].events = 0; |
441 | anfds [fd].events = 0; |
354 | fd_change (fd); |
442 | fd_change (EV_A_ fd); |
355 | } |
443 | } |
356 | } |
444 | } |
357 | |
445 | |
358 | /*****************************************************************************/ |
446 | /*****************************************************************************/ |
359 | |
447 | |
… | |
… | |
363 | WT w = heap [k]; |
451 | WT w = heap [k]; |
364 | |
452 | |
365 | while (k && heap [k >> 1]->at > w->at) |
453 | while (k && heap [k >> 1]->at > w->at) |
366 | { |
454 | { |
367 | heap [k] = heap [k >> 1]; |
455 | heap [k] = heap [k >> 1]; |
368 | heap [k]->active = k + 1; |
456 | ((W)heap [k])->active = k + 1; |
369 | k >>= 1; |
457 | k >>= 1; |
370 | } |
458 | } |
371 | |
459 | |
372 | heap [k] = w; |
460 | heap [k] = w; |
373 | heap [k]->active = k + 1; |
461 | ((W)heap [k])->active = k + 1; |
374 | |
462 | |
375 | } |
463 | } |
376 | |
464 | |
377 | static void |
465 | static void |
378 | downheap (WT *heap, int N, int k) |
466 | downheap (WT *heap, int N, int k) |
… | |
… | |
388 | |
476 | |
389 | if (w->at <= heap [j]->at) |
477 | if (w->at <= heap [j]->at) |
390 | break; |
478 | break; |
391 | |
479 | |
392 | heap [k] = heap [j]; |
480 | heap [k] = heap [j]; |
393 | heap [k]->active = k + 1; |
481 | ((W)heap [k])->active = k + 1; |
394 | k = j; |
482 | k = j; |
395 | } |
483 | } |
396 | |
484 | |
397 | heap [k] = w; |
485 | heap [k] = w; |
398 | heap [k]->active = k + 1; |
486 | ((W)heap [k])->active = k + 1; |
399 | } |
487 | } |
400 | |
488 | |
401 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
402 | |
490 | |
403 | typedef struct |
491 | typedef struct |
404 | { |
492 | { |
405 | struct ev_watcher_list *head; |
493 | WL head; |
406 | sig_atomic_t volatile gotsig; |
494 | sig_atomic_t volatile gotsig; |
407 | } ANSIG; |
495 | } ANSIG; |
408 | |
496 | |
409 | static ANSIG *signals; |
497 | static ANSIG *signals; |
410 | static int signalmax; |
498 | static int signalmax; |
411 | |
499 | |
412 | static int sigpipe [2]; |
500 | static int sigpipe [2]; |
413 | static sig_atomic_t volatile gotsig; |
501 | static sig_atomic_t volatile gotsig; |
|
|
502 | static struct ev_io sigev; |
414 | |
503 | |
415 | static void |
504 | static void |
416 | signals_init (ANSIG *base, int count) |
505 | signals_init (ANSIG *base, int count) |
417 | { |
506 | { |
418 | while (count--) |
507 | while (count--) |
… | |
… | |
425 | } |
514 | } |
426 | |
515 | |
427 | static void |
516 | static void |
428 | sighandler (int signum) |
517 | sighandler (int signum) |
429 | { |
518 | { |
|
|
519 | #if WIN32 |
|
|
520 | signal (signum, sighandler); |
|
|
521 | #endif |
|
|
522 | |
430 | signals [signum - 1].gotsig = 1; |
523 | signals [signum - 1].gotsig = 1; |
431 | |
524 | |
432 | if (!gotsig) |
525 | if (!gotsig) |
433 | { |
526 | { |
434 | int old_errno = errno; |
527 | int old_errno = errno; |
… | |
… | |
439 | } |
532 | } |
440 | |
533 | |
441 | static void |
534 | static void |
442 | sigcb (EV_P_ struct ev_io *iow, int revents) |
535 | sigcb (EV_P_ struct ev_io *iow, int revents) |
443 | { |
536 | { |
444 | struct ev_watcher_list *w; |
537 | WL w; |
445 | int signum; |
538 | int signum; |
446 | |
539 | |
447 | read (sigpipe [0], &revents, 1); |
540 | read (sigpipe [0], &revents, 1); |
448 | gotsig = 0; |
541 | gotsig = 0; |
449 | |
542 | |
… | |
… | |
476 | |
569 | |
477 | /*****************************************************************************/ |
570 | /*****************************************************************************/ |
478 | |
571 | |
479 | #ifndef WIN32 |
572 | #ifndef WIN32 |
480 | |
573 | |
|
|
574 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
575 | static struct ev_signal childev; |
|
|
576 | |
481 | #ifndef WCONTINUED |
577 | #ifndef WCONTINUED |
482 | # define WCONTINUED 0 |
578 | # define WCONTINUED 0 |
483 | #endif |
579 | #endif |
484 | |
580 | |
485 | static void |
581 | static void |
… | |
… | |
488 | struct ev_child *w; |
584 | struct ev_child *w; |
489 | |
585 | |
490 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
586 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
491 | if (w->pid == pid || !w->pid) |
587 | if (w->pid == pid || !w->pid) |
492 | { |
588 | { |
493 | w->priority = sw->priority; /* need to do it *now* */ |
589 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
494 | w->rpid = pid; |
590 | w->rpid = pid; |
495 | w->rstatus = status; |
591 | w->rstatus = status; |
496 | event (EV_A_ (W)w, EV_CHILD); |
592 | event (EV_A_ (W)w, EV_CHILD); |
497 | } |
593 | } |
498 | } |
594 | } |
499 | |
595 | |
500 | static void |
596 | static void |
… | |
… | |
520 | # include "ev_kqueue.c" |
616 | # include "ev_kqueue.c" |
521 | #endif |
617 | #endif |
522 | #if EV_USE_EPOLL |
618 | #if EV_USE_EPOLL |
523 | # include "ev_epoll.c" |
619 | # include "ev_epoll.c" |
524 | #endif |
620 | #endif |
525 | #if EV_USEV_POLL |
621 | #if EV_USE_POLL |
526 | # include "ev_poll.c" |
622 | # include "ev_poll.c" |
527 | #endif |
623 | #endif |
528 | #if EV_USE_SELECT |
624 | #if EV_USE_SELECT |
529 | # include "ev_select.c" |
625 | # include "ev_select.c" |
530 | #endif |
626 | #endif |
… | |
… | |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
678 | methods = atoi (getenv ("LIBEV_METHODS")); |
583 | else |
679 | else |
584 | methods = EVMETHOD_ANY; |
680 | methods = EVMETHOD_ANY; |
585 | |
681 | |
586 | method = 0; |
682 | method = 0; |
|
|
683 | #if EV_USE_WIN32 |
|
|
684 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
685 | #endif |
587 | #if EV_USE_KQUEUE |
686 | #if EV_USE_KQUEUE |
588 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
687 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
589 | #endif |
688 | #endif |
590 | #if EV_USE_EPOLL |
689 | #if EV_USE_EPOLL |
591 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
690 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
592 | #endif |
691 | #endif |
593 | #if EV_USEV_POLL |
692 | #if EV_USE_POLL |
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
693 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
595 | #endif |
694 | #endif |
596 | #if EV_USE_SELECT |
695 | #if EV_USE_SELECT |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
696 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
598 | #endif |
697 | #endif |
… | |
… | |
600 | } |
699 | } |
601 | |
700 | |
602 | void |
701 | void |
603 | loop_destroy (EV_P) |
702 | loop_destroy (EV_P) |
604 | { |
703 | { |
|
|
704 | int i; |
|
|
705 | |
|
|
706 | #if EV_USE_WIN32 |
|
|
707 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
708 | #endif |
605 | #if EV_USE_KQUEUE |
709 | #if EV_USE_KQUEUE |
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
710 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
607 | #endif |
711 | #endif |
608 | #if EV_USE_EPOLL |
712 | #if EV_USE_EPOLL |
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
713 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
610 | #endif |
714 | #endif |
611 | #if EV_USEV_POLL |
715 | #if EV_USE_POLL |
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
716 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
613 | #endif |
717 | #endif |
614 | #if EV_USE_SELECT |
718 | #if EV_USE_SELECT |
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
719 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
616 | #endif |
720 | #endif |
|
|
721 | |
|
|
722 | for (i = NUMPRI; i--; ) |
|
|
723 | array_free (pending, [i]); |
|
|
724 | |
|
|
725 | array_free (fdchange, ); |
|
|
726 | array_free (timer, ); |
|
|
727 | array_free (periodic, ); |
|
|
728 | array_free (idle, ); |
|
|
729 | array_free (prepare, ); |
|
|
730 | array_free (check, ); |
617 | |
731 | |
618 | method = 0; |
732 | method = 0; |
619 | /*TODO*/ |
733 | /*TODO*/ |
620 | } |
734 | } |
621 | |
735 | |
… | |
… | |
633 | |
747 | |
634 | #if EV_MULTIPLICITY |
748 | #if EV_MULTIPLICITY |
635 | struct ev_loop * |
749 | struct ev_loop * |
636 | ev_loop_new (int methods) |
750 | ev_loop_new (int methods) |
637 | { |
751 | { |
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
752 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
753 | |
|
|
754 | memset (loop, 0, sizeof (struct ev_loop)); |
639 | |
755 | |
640 | loop_init (EV_A_ methods); |
756 | loop_init (EV_A_ methods); |
641 | |
757 | |
642 | if (ev_methods (EV_A)) |
758 | if (ev_method (EV_A)) |
643 | return loop; |
759 | return loop; |
644 | |
760 | |
645 | return 0; |
761 | return 0; |
646 | } |
762 | } |
647 | |
763 | |
648 | void |
764 | void |
649 | ev_loop_destroy (EV_P) |
765 | ev_loop_destroy (EV_P) |
650 | { |
766 | { |
651 | loop_destroy (EV_A); |
767 | loop_destroy (EV_A); |
652 | free (loop); |
768 | ev_free (loop); |
653 | } |
769 | } |
654 | |
770 | |
655 | void |
771 | void |
656 | ev_loop_fork (EV_P) |
772 | ev_loop_fork (EV_P) |
657 | { |
773 | { |
… | |
… | |
707 | } |
823 | } |
708 | |
824 | |
709 | void |
825 | void |
710 | ev_default_destroy (void) |
826 | ev_default_destroy (void) |
711 | { |
827 | { |
|
|
828 | #if EV_MULTIPLICITY |
712 | struct ev_loop *loop = default_loop; |
829 | struct ev_loop *loop = default_loop; |
|
|
830 | #endif |
713 | |
831 | |
714 | ev_ref (EV_A); /* child watcher */ |
832 | ev_ref (EV_A); /* child watcher */ |
715 | ev_signal_stop (EV_A_ &childev); |
833 | ev_signal_stop (EV_A_ &childev); |
716 | |
834 | |
717 | ev_ref (EV_A); /* signal watcher */ |
835 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
722 | |
840 | |
723 | loop_destroy (EV_A); |
841 | loop_destroy (EV_A); |
724 | } |
842 | } |
725 | |
843 | |
726 | void |
844 | void |
727 | ev_default_fork (EV_P) |
845 | ev_default_fork (void) |
728 | { |
846 | { |
|
|
847 | #if EV_MULTIPLICITY |
|
|
848 | struct ev_loop *loop = default_loop; |
|
|
849 | #endif |
|
|
850 | |
729 | loop_fork (EV_A); |
851 | loop_fork (EV_A); |
730 | |
852 | |
731 | ev_io_stop (EV_A_ &sigev); |
853 | ev_io_stop (EV_A_ &sigev); |
732 | close (sigpipe [0]); |
854 | close (sigpipe [0]); |
733 | close (sigpipe [1]); |
855 | close (sigpipe [1]); |
… | |
… | |
758 | } |
880 | } |
759 | |
881 | |
760 | static void |
882 | static void |
761 | timers_reify (EV_P) |
883 | timers_reify (EV_P) |
762 | { |
884 | { |
763 | while (timercnt && timers [0]->at <= mn_now) |
885 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
764 | { |
886 | { |
765 | struct ev_timer *w = timers [0]; |
887 | struct ev_timer *w = timers [0]; |
|
|
888 | |
|
|
889 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
766 | |
890 | |
767 | /* first reschedule or stop timer */ |
891 | /* first reschedule or stop timer */ |
768 | if (w->repeat) |
892 | if (w->repeat) |
769 | { |
893 | { |
770 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
894 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
771 | w->at = mn_now + w->repeat; |
895 | ((WT)w)->at = mn_now + w->repeat; |
772 | downheap ((WT *)timers, timercnt, 0); |
896 | downheap ((WT *)timers, timercnt, 0); |
773 | } |
897 | } |
774 | else |
898 | else |
775 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
899 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
776 | |
900 | |
… | |
… | |
779 | } |
903 | } |
780 | |
904 | |
781 | static void |
905 | static void |
782 | periodics_reify (EV_P) |
906 | periodics_reify (EV_P) |
783 | { |
907 | { |
784 | while (periodiccnt && periodics [0]->at <= rt_now) |
908 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
785 | { |
909 | { |
786 | struct ev_periodic *w = periodics [0]; |
910 | struct ev_periodic *w = periodics [0]; |
|
|
911 | |
|
|
912 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
787 | |
913 | |
788 | /* first reschedule or stop timer */ |
914 | /* first reschedule or stop timer */ |
789 | if (w->interval) |
915 | if (w->interval) |
790 | { |
916 | { |
791 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
917 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
792 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
918 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
793 | downheap ((WT *)periodics, periodiccnt, 0); |
919 | downheap ((WT *)periodics, periodiccnt, 0); |
794 | } |
920 | } |
795 | else |
921 | else |
796 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
922 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
797 | |
923 | |
… | |
… | |
809 | { |
935 | { |
810 | struct ev_periodic *w = periodics [i]; |
936 | struct ev_periodic *w = periodics [i]; |
811 | |
937 | |
812 | if (w->interval) |
938 | if (w->interval) |
813 | { |
939 | { |
814 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
940 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
815 | |
941 | |
816 | if (fabs (diff) >= 1e-4) |
942 | if (fabs (diff) >= 1e-4) |
817 | { |
943 | { |
818 | ev_periodic_stop (EV_A_ w); |
944 | ev_periodic_stop (EV_A_ w); |
819 | ev_periodic_start (EV_A_ w); |
945 | ev_periodic_start (EV_A_ w); |
… | |
… | |
880 | { |
1006 | { |
881 | periodics_reschedule (EV_A); |
1007 | periodics_reschedule (EV_A); |
882 | |
1008 | |
883 | /* adjust timers. this is easy, as the offset is the same for all */ |
1009 | /* adjust timers. this is easy, as the offset is the same for all */ |
884 | for (i = 0; i < timercnt; ++i) |
1010 | for (i = 0; i < timercnt; ++i) |
885 | timers [i]->at += rt_now - mn_now; |
1011 | ((WT)timers [i])->at += rt_now - mn_now; |
886 | } |
1012 | } |
887 | |
1013 | |
888 | mn_now = rt_now; |
1014 | mn_now = rt_now; |
889 | } |
1015 | } |
890 | } |
1016 | } |
… | |
… | |
941 | { |
1067 | { |
942 | block = MAX_BLOCKTIME; |
1068 | block = MAX_BLOCKTIME; |
943 | |
1069 | |
944 | if (timercnt) |
1070 | if (timercnt) |
945 | { |
1071 | { |
946 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1072 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
947 | if (block > to) block = to; |
1073 | if (block > to) block = to; |
948 | } |
1074 | } |
949 | |
1075 | |
950 | if (periodiccnt) |
1076 | if (periodiccnt) |
951 | { |
1077 | { |
952 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1078 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
953 | if (block > to) block = to; |
1079 | if (block > to) block = to; |
954 | } |
1080 | } |
955 | |
1081 | |
956 | if (block < 0.) block = 0.; |
1082 | if (block < 0.) block = 0.; |
957 | } |
1083 | } |
… | |
… | |
1074 | ev_timer_start (EV_P_ struct ev_timer *w) |
1200 | ev_timer_start (EV_P_ struct ev_timer *w) |
1075 | { |
1201 | { |
1076 | if (ev_is_active (w)) |
1202 | if (ev_is_active (w)) |
1077 | return; |
1203 | return; |
1078 | |
1204 | |
1079 | w->at += mn_now; |
1205 | ((WT)w)->at += mn_now; |
1080 | |
1206 | |
1081 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1207 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1082 | |
1208 | |
1083 | ev_start (EV_A_ (W)w, ++timercnt); |
1209 | ev_start (EV_A_ (W)w, ++timercnt); |
1084 | array_needsize (timers, timermax, timercnt, ); |
1210 | array_needsize (timers, timermax, timercnt, ); |
1085 | timers [timercnt - 1] = w; |
1211 | timers [timercnt - 1] = w; |
1086 | upheap ((WT *)timers, timercnt - 1); |
1212 | upheap ((WT *)timers, timercnt - 1); |
|
|
1213 | |
|
|
1214 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1087 | } |
1215 | } |
1088 | |
1216 | |
1089 | void |
1217 | void |
1090 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1218 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1091 | { |
1219 | { |
1092 | ev_clear_pending (EV_A_ (W)w); |
1220 | ev_clear_pending (EV_A_ (W)w); |
1093 | if (!ev_is_active (w)) |
1221 | if (!ev_is_active (w)) |
1094 | return; |
1222 | return; |
1095 | |
1223 | |
|
|
1224 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1225 | |
1096 | if (w->active < timercnt--) |
1226 | if (((W)w)->active < timercnt--) |
1097 | { |
1227 | { |
1098 | timers [w->active - 1] = timers [timercnt]; |
1228 | timers [((W)w)->active - 1] = timers [timercnt]; |
1099 | downheap ((WT *)timers, timercnt, w->active - 1); |
1229 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1100 | } |
1230 | } |
1101 | |
1231 | |
1102 | w->at = w->repeat; |
1232 | ((WT)w)->at = w->repeat; |
1103 | |
1233 | |
1104 | ev_stop (EV_A_ (W)w); |
1234 | ev_stop (EV_A_ (W)w); |
1105 | } |
1235 | } |
1106 | |
1236 | |
1107 | void |
1237 | void |
… | |
… | |
1109 | { |
1239 | { |
1110 | if (ev_is_active (w)) |
1240 | if (ev_is_active (w)) |
1111 | { |
1241 | { |
1112 | if (w->repeat) |
1242 | if (w->repeat) |
1113 | { |
1243 | { |
1114 | w->at = mn_now + w->repeat; |
1244 | ((WT)w)->at = mn_now + w->repeat; |
1115 | downheap ((WT *)timers, timercnt, w->active - 1); |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1116 | } |
1246 | } |
1117 | else |
1247 | else |
1118 | ev_timer_stop (EV_A_ w); |
1248 | ev_timer_stop (EV_A_ w); |
1119 | } |
1249 | } |
1120 | else if (w->repeat) |
1250 | else if (w->repeat) |
… | |
… | |
1129 | |
1259 | |
1130 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1260 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1131 | |
1261 | |
1132 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1262 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1133 | if (w->interval) |
1263 | if (w->interval) |
1134 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1264 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1135 | |
1265 | |
1136 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1266 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1137 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1267 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1138 | periodics [periodiccnt - 1] = w; |
1268 | periodics [periodiccnt - 1] = w; |
1139 | upheap ((WT *)periodics, periodiccnt - 1); |
1269 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1270 | |
|
|
1271 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1140 | } |
1272 | } |
1141 | |
1273 | |
1142 | void |
1274 | void |
1143 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1275 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1144 | { |
1276 | { |
1145 | ev_clear_pending (EV_A_ (W)w); |
1277 | ev_clear_pending (EV_A_ (W)w); |
1146 | if (!ev_is_active (w)) |
1278 | if (!ev_is_active (w)) |
1147 | return; |
1279 | return; |
1148 | |
1280 | |
|
|
1281 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1282 | |
1149 | if (w->active < periodiccnt--) |
1283 | if (((W)w)->active < periodiccnt--) |
1150 | { |
1284 | { |
1151 | periodics [w->active - 1] = periodics [periodiccnt]; |
1285 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1152 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1286 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1153 | } |
1287 | } |
1154 | |
1288 | |
1155 | ev_stop (EV_A_ (W)w); |
1289 | ev_stop (EV_A_ (W)w); |
1156 | } |
1290 | } |
1157 | |
1291 | |
… | |
… | |
1171 | { |
1305 | { |
1172 | ev_clear_pending (EV_A_ (W)w); |
1306 | ev_clear_pending (EV_A_ (W)w); |
1173 | if (ev_is_active (w)) |
1307 | if (ev_is_active (w)) |
1174 | return; |
1308 | return; |
1175 | |
1309 | |
1176 | idles [w->active - 1] = idles [--idlecnt]; |
1310 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1177 | ev_stop (EV_A_ (W)w); |
1311 | ev_stop (EV_A_ (W)w); |
1178 | } |
1312 | } |
1179 | |
1313 | |
1180 | void |
1314 | void |
1181 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1315 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1193 | { |
1327 | { |
1194 | ev_clear_pending (EV_A_ (W)w); |
1328 | ev_clear_pending (EV_A_ (W)w); |
1195 | if (ev_is_active (w)) |
1329 | if (ev_is_active (w)) |
1196 | return; |
1330 | return; |
1197 | |
1331 | |
1198 | prepares [w->active - 1] = prepares [--preparecnt]; |
1332 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1199 | ev_stop (EV_A_ (W)w); |
1333 | ev_stop (EV_A_ (W)w); |
1200 | } |
1334 | } |
1201 | |
1335 | |
1202 | void |
1336 | void |
1203 | ev_check_start (EV_P_ struct ev_check *w) |
1337 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1215 | { |
1349 | { |
1216 | ev_clear_pending (EV_A_ (W)w); |
1350 | ev_clear_pending (EV_A_ (W)w); |
1217 | if (ev_is_active (w)) |
1351 | if (ev_is_active (w)) |
1218 | return; |
1352 | return; |
1219 | |
1353 | |
1220 | checks [w->active - 1] = checks [--checkcnt]; |
1354 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1221 | ev_stop (EV_A_ (W)w); |
1355 | ev_stop (EV_A_ (W)w); |
1222 | } |
1356 | } |
1223 | |
1357 | |
1224 | #ifndef SA_RESTART |
1358 | #ifndef SA_RESTART |
1225 | # define SA_RESTART 0 |
1359 | # define SA_RESTART 0 |
… | |
… | |
1238 | |
1372 | |
1239 | ev_start (EV_A_ (W)w, 1); |
1373 | ev_start (EV_A_ (W)w, 1); |
1240 | array_needsize (signals, signalmax, w->signum, signals_init); |
1374 | array_needsize (signals, signalmax, w->signum, signals_init); |
1241 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1375 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1242 | |
1376 | |
1243 | if (!w->next) |
1377 | if (!((WL)w)->next) |
1244 | { |
1378 | { |
|
|
1379 | #if WIN32 |
|
|
1380 | signal (w->signum, sighandler); |
|
|
1381 | #else |
1245 | struct sigaction sa; |
1382 | struct sigaction sa; |
1246 | sa.sa_handler = sighandler; |
1383 | sa.sa_handler = sighandler; |
1247 | sigfillset (&sa.sa_mask); |
1384 | sigfillset (&sa.sa_mask); |
1248 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1385 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1249 | sigaction (w->signum, &sa, 0); |
1386 | sigaction (w->signum, &sa, 0); |
|
|
1387 | #endif |
1250 | } |
1388 | } |
1251 | } |
1389 | } |
1252 | |
1390 | |
1253 | void |
1391 | void |
1254 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1392 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1304 | void (*cb)(int revents, void *arg) = once->cb; |
1442 | void (*cb)(int revents, void *arg) = once->cb; |
1305 | void *arg = once->arg; |
1443 | void *arg = once->arg; |
1306 | |
1444 | |
1307 | ev_io_stop (EV_A_ &once->io); |
1445 | ev_io_stop (EV_A_ &once->io); |
1308 | ev_timer_stop (EV_A_ &once->to); |
1446 | ev_timer_stop (EV_A_ &once->to); |
1309 | free (once); |
1447 | ev_free (once); |
1310 | |
1448 | |
1311 | cb (revents, arg); |
1449 | cb (revents, arg); |
1312 | } |
1450 | } |
1313 | |
1451 | |
1314 | static void |
1452 | static void |
… | |
… | |
1324 | } |
1462 | } |
1325 | |
1463 | |
1326 | void |
1464 | void |
1327 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1465 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1328 | { |
1466 | { |
1329 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1467 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1330 | |
1468 | |
1331 | if (!once) |
1469 | if (!once) |
1332 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1470 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1333 | else |
1471 | else |
1334 | { |
1472 | { |