… | |
… | |
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> |
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 |
… | |
… | |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
123 | #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) */ |
124 | #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 */ |
125 | #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 */ |
126 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
97 | |
127 | |
98 | #ifndef EV_EMBED |
|
|
99 | # include "ev.h" |
128 | #include "ev.h" |
100 | #endif |
|
|
101 | |
129 | |
102 | #if __GNUC__ >= 3 |
130 | #if __GNUC__ >= 3 |
103 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
104 | # define inline inline |
132 | # define inline inline |
105 | #else |
133 | #else |
… | |
… | |
117 | typedef struct ev_watcher_list *WL; |
145 | typedef struct ev_watcher_list *WL; |
118 | typedef struct ev_watcher_time *WT; |
146 | typedef struct ev_watcher_time *WT; |
119 | |
147 | |
120 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
121 | |
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 | |
122 | /*****************************************************************************/ |
156 | /*****************************************************************************/ |
123 | |
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 | |
124 | typedef struct |
203 | typedef struct |
125 | { |
204 | { |
126 | struct ev_watcher_list *head; |
205 | WL head; |
127 | unsigned char events; |
206 | unsigned char events; |
128 | unsigned char reify; |
207 | unsigned char reify; |
129 | } ANFD; |
208 | } ANFD; |
130 | |
209 | |
131 | typedef struct |
210 | typedef struct |
… | |
… | |
189 | return rt_now; |
268 | return rt_now; |
190 | } |
269 | } |
191 | |
270 | |
192 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
271 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
193 | |
272 | |
194 | #define array_needsize(base,cur,cnt,init) \ |
273 | #define array_needsize(base,cur,cnt,init) \ |
195 | if (expect_false ((cnt) > cur)) \ |
274 | if (expect_false ((cnt) > cur)) \ |
196 | { \ |
275 | { \ |
197 | int newcnt = cur; \ |
276 | int newcnt = cur; \ |
198 | do \ |
277 | do \ |
199 | { \ |
278 | { \ |
200 | newcnt = array_roundsize (base, newcnt << 1); \ |
279 | newcnt = array_roundsize (base, newcnt << 1); \ |
201 | } \ |
280 | } \ |
202 | while ((cnt) > newcnt); \ |
281 | while ((cnt) > newcnt); \ |
203 | \ |
282 | \ |
204 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
283 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
205 | init (base + cur, newcnt - cur); \ |
284 | init (base + cur, newcnt - cur); \ |
206 | cur = newcnt; \ |
285 | cur = newcnt; \ |
207 | } |
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; |
208 | |
298 | |
209 | /*****************************************************************************/ |
299 | /*****************************************************************************/ |
210 | |
300 | |
211 | static void |
301 | static void |
212 | anfds_init (ANFD *base, int count) |
302 | anfds_init (ANFD *base, int count) |
… | |
… | |
278 | 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) |
279 | events |= w->events; |
369 | events |= w->events; |
280 | |
370 | |
281 | anfd->reify = 0; |
371 | anfd->reify = 0; |
282 | |
372 | |
283 | if (anfd->events != events) |
|
|
284 | { |
|
|
285 | method_modify (EV_A_ fd, anfd->events, events); |
373 | method_modify (EV_A_ fd, anfd->events, events); |
286 | anfd->events = events; |
374 | anfd->events = events; |
287 | } |
|
|
288 | } |
375 | } |
289 | |
376 | |
290 | fdchangecnt = 0; |
377 | fdchangecnt = 0; |
291 | } |
378 | } |
292 | |
379 | |
… | |
… | |
329 | |
416 | |
330 | /* 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 */ |
331 | static void |
418 | static void |
332 | fd_enomem (EV_P) |
419 | fd_enomem (EV_P) |
333 | { |
420 | { |
334 | int fd = anfdmax; |
421 | int fd; |
335 | |
422 | |
336 | while (fd--) |
423 | for (fd = anfdmax; fd--; ) |
337 | if (anfds [fd].events) |
424 | if (anfds [fd].events) |
338 | { |
425 | { |
339 | close (fd); |
|
|
340 | fd_kill (EV_A_ fd); |
426 | fd_kill (EV_A_ fd); |
341 | return; |
427 | return; |
342 | } |
428 | } |
343 | } |
429 | } |
344 | |
430 | |
… | |
… | |
351 | /* 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 */ |
352 | for (fd = 0; fd < anfdmax; ++fd) |
438 | for (fd = 0; fd < anfdmax; ++fd) |
353 | if (anfds [fd].events) |
439 | if (anfds [fd].events) |
354 | { |
440 | { |
355 | anfds [fd].events = 0; |
441 | anfds [fd].events = 0; |
356 | fd_change (fd); |
442 | fd_change (EV_A_ fd); |
357 | } |
443 | } |
358 | } |
444 | } |
359 | |
445 | |
360 | /*****************************************************************************/ |
446 | /*****************************************************************************/ |
361 | |
447 | |
… | |
… | |
365 | WT w = heap [k]; |
451 | WT w = heap [k]; |
366 | |
452 | |
367 | while (k && heap [k >> 1]->at > w->at) |
453 | while (k && heap [k >> 1]->at > w->at) |
368 | { |
454 | { |
369 | heap [k] = heap [k >> 1]; |
455 | heap [k] = heap [k >> 1]; |
370 | heap [k]->active = k + 1; |
456 | ((W)heap [k])->active = k + 1; |
371 | k >>= 1; |
457 | k >>= 1; |
372 | } |
458 | } |
373 | |
459 | |
374 | heap [k] = w; |
460 | heap [k] = w; |
375 | heap [k]->active = k + 1; |
461 | ((W)heap [k])->active = k + 1; |
376 | |
462 | |
377 | } |
463 | } |
378 | |
464 | |
379 | static void |
465 | static void |
380 | downheap (WT *heap, int N, int k) |
466 | downheap (WT *heap, int N, int k) |
… | |
… | |
390 | |
476 | |
391 | if (w->at <= heap [j]->at) |
477 | if (w->at <= heap [j]->at) |
392 | break; |
478 | break; |
393 | |
479 | |
394 | heap [k] = heap [j]; |
480 | heap [k] = heap [j]; |
395 | heap [k]->active = k + 1; |
481 | ((W)heap [k])->active = k + 1; |
396 | k = j; |
482 | k = j; |
397 | } |
483 | } |
398 | |
484 | |
399 | heap [k] = w; |
485 | heap [k] = w; |
400 | heap [k]->active = k + 1; |
486 | ((W)heap [k])->active = k + 1; |
401 | } |
487 | } |
402 | |
488 | |
403 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
404 | |
490 | |
405 | typedef struct |
491 | typedef struct |
406 | { |
492 | { |
407 | struct ev_watcher_list *head; |
493 | WL head; |
408 | sig_atomic_t volatile gotsig; |
494 | sig_atomic_t volatile gotsig; |
409 | } ANSIG; |
495 | } ANSIG; |
410 | |
496 | |
411 | static ANSIG *signals; |
497 | static ANSIG *signals; |
412 | static int signalmax; |
498 | static int signalmax; |
413 | |
499 | |
414 | static int sigpipe [2]; |
500 | static int sigpipe [2]; |
415 | static sig_atomic_t volatile gotsig; |
501 | static sig_atomic_t volatile gotsig; |
|
|
502 | static struct ev_io sigev; |
416 | |
503 | |
417 | static void |
504 | static void |
418 | signals_init (ANSIG *base, int count) |
505 | signals_init (ANSIG *base, int count) |
419 | { |
506 | { |
420 | while (count--) |
507 | while (count--) |
… | |
… | |
427 | } |
514 | } |
428 | |
515 | |
429 | static void |
516 | static void |
430 | sighandler (int signum) |
517 | sighandler (int signum) |
431 | { |
518 | { |
|
|
519 | #if WIN32 |
|
|
520 | signal (signum, sighandler); |
|
|
521 | #endif |
|
|
522 | |
432 | signals [signum - 1].gotsig = 1; |
523 | signals [signum - 1].gotsig = 1; |
433 | |
524 | |
434 | if (!gotsig) |
525 | if (!gotsig) |
435 | { |
526 | { |
436 | int old_errno = errno; |
527 | int old_errno = errno; |
… | |
… | |
441 | } |
532 | } |
442 | |
533 | |
443 | static void |
534 | static void |
444 | sigcb (EV_P_ struct ev_io *iow, int revents) |
535 | sigcb (EV_P_ struct ev_io *iow, int revents) |
445 | { |
536 | { |
446 | struct ev_watcher_list *w; |
537 | WL w; |
447 | int signum; |
538 | int signum; |
448 | |
539 | |
449 | read (sigpipe [0], &revents, 1); |
540 | read (sigpipe [0], &revents, 1); |
450 | gotsig = 0; |
541 | gotsig = 0; |
451 | |
542 | |
… | |
… | |
478 | |
569 | |
479 | /*****************************************************************************/ |
570 | /*****************************************************************************/ |
480 | |
571 | |
481 | #ifndef WIN32 |
572 | #ifndef WIN32 |
482 | |
573 | |
|
|
574 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
575 | static struct ev_signal childev; |
|
|
576 | |
483 | #ifndef WCONTINUED |
577 | #ifndef WCONTINUED |
484 | # define WCONTINUED 0 |
578 | # define WCONTINUED 0 |
485 | #endif |
579 | #endif |
486 | |
580 | |
487 | static void |
581 | static void |
… | |
… | |
490 | struct ev_child *w; |
584 | struct ev_child *w; |
491 | |
585 | |
492 | 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) |
493 | if (w->pid == pid || !w->pid) |
587 | if (w->pid == pid || !w->pid) |
494 | { |
588 | { |
495 | w->priority = sw->priority; /* need to do it *now* */ |
589 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
496 | w->rpid = pid; |
590 | w->rpid = pid; |
497 | w->rstatus = status; |
591 | w->rstatus = status; |
498 | event (EV_A_ (W)w, EV_CHILD); |
592 | event (EV_A_ (W)w, EV_CHILD); |
499 | } |
593 | } |
500 | } |
594 | } |
501 | |
595 | |
502 | static void |
596 | static void |
… | |
… | |
522 | # include "ev_kqueue.c" |
616 | # include "ev_kqueue.c" |
523 | #endif |
617 | #endif |
524 | #if EV_USE_EPOLL |
618 | #if EV_USE_EPOLL |
525 | # include "ev_epoll.c" |
619 | # include "ev_epoll.c" |
526 | #endif |
620 | #endif |
527 | #if EV_USEV_POLL |
621 | #if EV_USE_POLL |
528 | # include "ev_poll.c" |
622 | # include "ev_poll.c" |
529 | #endif |
623 | #endif |
530 | #if EV_USE_SELECT |
624 | #if EV_USE_SELECT |
531 | # include "ev_select.c" |
625 | # include "ev_select.c" |
532 | #endif |
626 | #endif |
… | |
… | |
584 | methods = atoi (getenv ("LIBEV_METHODS")); |
678 | methods = atoi (getenv ("LIBEV_METHODS")); |
585 | else |
679 | else |
586 | methods = EVMETHOD_ANY; |
680 | methods = EVMETHOD_ANY; |
587 | |
681 | |
588 | method = 0; |
682 | method = 0; |
|
|
683 | #if EV_USE_WIN32 |
|
|
684 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
685 | #endif |
589 | #if EV_USE_KQUEUE |
686 | #if EV_USE_KQUEUE |
590 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
687 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
591 | #endif |
688 | #endif |
592 | #if EV_USE_EPOLL |
689 | #if EV_USE_EPOLL |
593 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
690 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
594 | #endif |
691 | #endif |
595 | #if EV_USEV_POLL |
692 | #if EV_USE_POLL |
596 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
693 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
597 | #endif |
694 | #endif |
598 | #if EV_USE_SELECT |
695 | #if EV_USE_SELECT |
599 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
696 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
600 | #endif |
697 | #endif |
… | |
… | |
602 | } |
699 | } |
603 | |
700 | |
604 | void |
701 | void |
605 | loop_destroy (EV_P) |
702 | loop_destroy (EV_P) |
606 | { |
703 | { |
|
|
704 | int i; |
|
|
705 | |
|
|
706 | #if EV_USE_WIN32 |
|
|
707 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
708 | #endif |
607 | #if EV_USE_KQUEUE |
709 | #if EV_USE_KQUEUE |
608 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
710 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
609 | #endif |
711 | #endif |
610 | #if EV_USE_EPOLL |
712 | #if EV_USE_EPOLL |
611 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
713 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
612 | #endif |
714 | #endif |
613 | #if EV_USEV_POLL |
715 | #if EV_USE_POLL |
614 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
716 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
615 | #endif |
717 | #endif |
616 | #if EV_USE_SELECT |
718 | #if EV_USE_SELECT |
617 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
719 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
618 | #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, ); |
619 | |
731 | |
620 | method = 0; |
732 | method = 0; |
621 | /*TODO*/ |
733 | /*TODO*/ |
622 | } |
734 | } |
623 | |
735 | |
… | |
… | |
635 | |
747 | |
636 | #if EV_MULTIPLICITY |
748 | #if EV_MULTIPLICITY |
637 | struct ev_loop * |
749 | struct ev_loop * |
638 | ev_loop_new (int methods) |
750 | ev_loop_new (int methods) |
639 | { |
751 | { |
640 | 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)); |
641 | |
755 | |
642 | loop_init (EV_A_ methods); |
756 | loop_init (EV_A_ methods); |
643 | |
757 | |
644 | if (ev_methods (EV_A)) |
758 | if (ev_method (EV_A)) |
645 | return loop; |
759 | return loop; |
646 | |
760 | |
647 | return 0; |
761 | return 0; |
648 | } |
762 | } |
649 | |
763 | |
650 | void |
764 | void |
651 | ev_loop_destroy (EV_P) |
765 | ev_loop_destroy (EV_P) |
652 | { |
766 | { |
653 | loop_destroy (EV_A); |
767 | loop_destroy (EV_A); |
654 | free (loop); |
768 | ev_free (loop); |
655 | } |
769 | } |
656 | |
770 | |
657 | void |
771 | void |
658 | ev_loop_fork (EV_P) |
772 | ev_loop_fork (EV_P) |
659 | { |
773 | { |
… | |
… | |
726 | |
840 | |
727 | loop_destroy (EV_A); |
841 | loop_destroy (EV_A); |
728 | } |
842 | } |
729 | |
843 | |
730 | void |
844 | void |
731 | ev_default_fork (EV_P) |
845 | ev_default_fork (void) |
732 | { |
846 | { |
|
|
847 | #if EV_MULTIPLICITY |
|
|
848 | struct ev_loop *loop = default_loop; |
|
|
849 | #endif |
|
|
850 | |
733 | loop_fork (EV_A); |
851 | loop_fork (EV_A); |
734 | |
852 | |
735 | ev_io_stop (EV_A_ &sigev); |
853 | ev_io_stop (EV_A_ &sigev); |
736 | close (sigpipe [0]); |
854 | close (sigpipe [0]); |
737 | close (sigpipe [1]); |
855 | close (sigpipe [1]); |
… | |
… | |
762 | } |
880 | } |
763 | |
881 | |
764 | static void |
882 | static void |
765 | timers_reify (EV_P) |
883 | timers_reify (EV_P) |
766 | { |
884 | { |
767 | while (timercnt && timers [0]->at <= mn_now) |
885 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
768 | { |
886 | { |
769 | 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))); |
770 | |
890 | |
771 | /* first reschedule or stop timer */ |
891 | /* first reschedule or stop timer */ |
772 | if (w->repeat) |
892 | if (w->repeat) |
773 | { |
893 | { |
774 | 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.)); |
775 | w->at = mn_now + w->repeat; |
895 | ((WT)w)->at = mn_now + w->repeat; |
776 | downheap ((WT *)timers, timercnt, 0); |
896 | downheap ((WT *)timers, timercnt, 0); |
777 | } |
897 | } |
778 | else |
898 | else |
779 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
899 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
780 | |
900 | |
… | |
… | |
783 | } |
903 | } |
784 | |
904 | |
785 | static void |
905 | static void |
786 | periodics_reify (EV_P) |
906 | periodics_reify (EV_P) |
787 | { |
907 | { |
788 | while (periodiccnt && periodics [0]->at <= rt_now) |
908 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
789 | { |
909 | { |
790 | 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))); |
791 | |
913 | |
792 | /* first reschedule or stop timer */ |
914 | /* first reschedule or stop timer */ |
793 | if (w->interval) |
915 | if (w->interval) |
794 | { |
916 | { |
795 | 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; |
796 | 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)); |
797 | downheap ((WT *)periodics, periodiccnt, 0); |
919 | downheap ((WT *)periodics, periodiccnt, 0); |
798 | } |
920 | } |
799 | else |
921 | else |
800 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
922 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
801 | |
923 | |
… | |
… | |
813 | { |
935 | { |
814 | struct ev_periodic *w = periodics [i]; |
936 | struct ev_periodic *w = periodics [i]; |
815 | |
937 | |
816 | if (w->interval) |
938 | if (w->interval) |
817 | { |
939 | { |
818 | 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; |
819 | |
941 | |
820 | if (fabs (diff) >= 1e-4) |
942 | if (fabs (diff) >= 1e-4) |
821 | { |
943 | { |
822 | ev_periodic_stop (EV_A_ w); |
944 | ev_periodic_stop (EV_A_ w); |
823 | ev_periodic_start (EV_A_ w); |
945 | ev_periodic_start (EV_A_ w); |
… | |
… | |
884 | { |
1006 | { |
885 | periodics_reschedule (EV_A); |
1007 | periodics_reschedule (EV_A); |
886 | |
1008 | |
887 | /* 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 */ |
888 | for (i = 0; i < timercnt; ++i) |
1010 | for (i = 0; i < timercnt; ++i) |
889 | timers [i]->at += rt_now - mn_now; |
1011 | ((WT)timers [i])->at += rt_now - mn_now; |
890 | } |
1012 | } |
891 | |
1013 | |
892 | mn_now = rt_now; |
1014 | mn_now = rt_now; |
893 | } |
1015 | } |
894 | } |
1016 | } |
… | |
… | |
945 | { |
1067 | { |
946 | block = MAX_BLOCKTIME; |
1068 | block = MAX_BLOCKTIME; |
947 | |
1069 | |
948 | if (timercnt) |
1070 | if (timercnt) |
949 | { |
1071 | { |
950 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1072 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
951 | if (block > to) block = to; |
1073 | if (block > to) block = to; |
952 | } |
1074 | } |
953 | |
1075 | |
954 | if (periodiccnt) |
1076 | if (periodiccnt) |
955 | { |
1077 | { |
956 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1078 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
957 | if (block > to) block = to; |
1079 | if (block > to) block = to; |
958 | } |
1080 | } |
959 | |
1081 | |
960 | if (block < 0.) block = 0.; |
1082 | if (block < 0.) block = 0.; |
961 | } |
1083 | } |
… | |
… | |
1078 | ev_timer_start (EV_P_ struct ev_timer *w) |
1200 | ev_timer_start (EV_P_ struct ev_timer *w) |
1079 | { |
1201 | { |
1080 | if (ev_is_active (w)) |
1202 | if (ev_is_active (w)) |
1081 | return; |
1203 | return; |
1082 | |
1204 | |
1083 | w->at += mn_now; |
1205 | ((WT)w)->at += mn_now; |
1084 | |
1206 | |
1085 | 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.)); |
1086 | |
1208 | |
1087 | ev_start (EV_A_ (W)w, ++timercnt); |
1209 | ev_start (EV_A_ (W)w, ++timercnt); |
1088 | array_needsize (timers, timermax, timercnt, ); |
1210 | array_needsize (timers, timermax, timercnt, ); |
1089 | timers [timercnt - 1] = w; |
1211 | timers [timercnt - 1] = w; |
1090 | upheap ((WT *)timers, timercnt - 1); |
1212 | upheap ((WT *)timers, timercnt - 1); |
|
|
1213 | |
|
|
1214 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1091 | } |
1215 | } |
1092 | |
1216 | |
1093 | void |
1217 | void |
1094 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1218 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1095 | { |
1219 | { |
1096 | ev_clear_pending (EV_A_ (W)w); |
1220 | ev_clear_pending (EV_A_ (W)w); |
1097 | if (!ev_is_active (w)) |
1221 | if (!ev_is_active (w)) |
1098 | return; |
1222 | return; |
1099 | |
1223 | |
|
|
1224 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1225 | |
1100 | if (w->active < timercnt--) |
1226 | if (((W)w)->active < timercnt--) |
1101 | { |
1227 | { |
1102 | timers [w->active - 1] = timers [timercnt]; |
1228 | timers [((W)w)->active - 1] = timers [timercnt]; |
1103 | downheap ((WT *)timers, timercnt, w->active - 1); |
1229 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1104 | } |
1230 | } |
1105 | |
1231 | |
1106 | w->at = w->repeat; |
1232 | ((WT)w)->at = w->repeat; |
1107 | |
1233 | |
1108 | ev_stop (EV_A_ (W)w); |
1234 | ev_stop (EV_A_ (W)w); |
1109 | } |
1235 | } |
1110 | |
1236 | |
1111 | void |
1237 | void |
… | |
… | |
1113 | { |
1239 | { |
1114 | if (ev_is_active (w)) |
1240 | if (ev_is_active (w)) |
1115 | { |
1241 | { |
1116 | if (w->repeat) |
1242 | if (w->repeat) |
1117 | { |
1243 | { |
1118 | w->at = mn_now + w->repeat; |
1244 | ((WT)w)->at = mn_now + w->repeat; |
1119 | downheap ((WT *)timers, timercnt, w->active - 1); |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1120 | } |
1246 | } |
1121 | else |
1247 | else |
1122 | ev_timer_stop (EV_A_ w); |
1248 | ev_timer_stop (EV_A_ w); |
1123 | } |
1249 | } |
1124 | else if (w->repeat) |
1250 | else if (w->repeat) |
… | |
… | |
1133 | |
1259 | |
1134 | 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.)); |
1135 | |
1261 | |
1136 | /* 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 */ |
1137 | if (w->interval) |
1263 | if (w->interval) |
1138 | 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; |
1139 | |
1265 | |
1140 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1266 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1141 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1267 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1142 | periodics [periodiccnt - 1] = w; |
1268 | periodics [periodiccnt - 1] = w; |
1143 | upheap ((WT *)periodics, periodiccnt - 1); |
1269 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1270 | |
|
|
1271 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1144 | } |
1272 | } |
1145 | |
1273 | |
1146 | void |
1274 | void |
1147 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1275 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1148 | { |
1276 | { |
1149 | ev_clear_pending (EV_A_ (W)w); |
1277 | ev_clear_pending (EV_A_ (W)w); |
1150 | if (!ev_is_active (w)) |
1278 | if (!ev_is_active (w)) |
1151 | return; |
1279 | return; |
1152 | |
1280 | |
|
|
1281 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1282 | |
1153 | if (w->active < periodiccnt--) |
1283 | if (((W)w)->active < periodiccnt--) |
1154 | { |
1284 | { |
1155 | periodics [w->active - 1] = periodics [periodiccnt]; |
1285 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1156 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1286 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1157 | } |
1287 | } |
1158 | |
1288 | |
1159 | ev_stop (EV_A_ (W)w); |
1289 | ev_stop (EV_A_ (W)w); |
1160 | } |
1290 | } |
1161 | |
1291 | |
… | |
… | |
1175 | { |
1305 | { |
1176 | ev_clear_pending (EV_A_ (W)w); |
1306 | ev_clear_pending (EV_A_ (W)w); |
1177 | if (ev_is_active (w)) |
1307 | if (ev_is_active (w)) |
1178 | return; |
1308 | return; |
1179 | |
1309 | |
1180 | idles [w->active - 1] = idles [--idlecnt]; |
1310 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1181 | ev_stop (EV_A_ (W)w); |
1311 | ev_stop (EV_A_ (W)w); |
1182 | } |
1312 | } |
1183 | |
1313 | |
1184 | void |
1314 | void |
1185 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1315 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1197 | { |
1327 | { |
1198 | ev_clear_pending (EV_A_ (W)w); |
1328 | ev_clear_pending (EV_A_ (W)w); |
1199 | if (ev_is_active (w)) |
1329 | if (ev_is_active (w)) |
1200 | return; |
1330 | return; |
1201 | |
1331 | |
1202 | prepares [w->active - 1] = prepares [--preparecnt]; |
1332 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1203 | ev_stop (EV_A_ (W)w); |
1333 | ev_stop (EV_A_ (W)w); |
1204 | } |
1334 | } |
1205 | |
1335 | |
1206 | void |
1336 | void |
1207 | ev_check_start (EV_P_ struct ev_check *w) |
1337 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1219 | { |
1349 | { |
1220 | ev_clear_pending (EV_A_ (W)w); |
1350 | ev_clear_pending (EV_A_ (W)w); |
1221 | if (ev_is_active (w)) |
1351 | if (ev_is_active (w)) |
1222 | return; |
1352 | return; |
1223 | |
1353 | |
1224 | checks [w->active - 1] = checks [--checkcnt]; |
1354 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1225 | ev_stop (EV_A_ (W)w); |
1355 | ev_stop (EV_A_ (W)w); |
1226 | } |
1356 | } |
1227 | |
1357 | |
1228 | #ifndef SA_RESTART |
1358 | #ifndef SA_RESTART |
1229 | # define SA_RESTART 0 |
1359 | # define SA_RESTART 0 |
… | |
… | |
1242 | |
1372 | |
1243 | ev_start (EV_A_ (W)w, 1); |
1373 | ev_start (EV_A_ (W)w, 1); |
1244 | array_needsize (signals, signalmax, w->signum, signals_init); |
1374 | array_needsize (signals, signalmax, w->signum, signals_init); |
1245 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1375 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1246 | |
1376 | |
1247 | if (!w->next) |
1377 | if (!((WL)w)->next) |
1248 | { |
1378 | { |
|
|
1379 | #if WIN32 |
|
|
1380 | signal (w->signum, sighandler); |
|
|
1381 | #else |
1249 | struct sigaction sa; |
1382 | struct sigaction sa; |
1250 | sa.sa_handler = sighandler; |
1383 | sa.sa_handler = sighandler; |
1251 | sigfillset (&sa.sa_mask); |
1384 | sigfillset (&sa.sa_mask); |
1252 | 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 */ |
1253 | sigaction (w->signum, &sa, 0); |
1386 | sigaction (w->signum, &sa, 0); |
|
|
1387 | #endif |
1254 | } |
1388 | } |
1255 | } |
1389 | } |
1256 | |
1390 | |
1257 | void |
1391 | void |
1258 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1392 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1308 | void (*cb)(int revents, void *arg) = once->cb; |
1442 | void (*cb)(int revents, void *arg) = once->cb; |
1309 | void *arg = once->arg; |
1443 | void *arg = once->arg; |
1310 | |
1444 | |
1311 | ev_io_stop (EV_A_ &once->io); |
1445 | ev_io_stop (EV_A_ &once->io); |
1312 | ev_timer_stop (EV_A_ &once->to); |
1446 | ev_timer_stop (EV_A_ &once->to); |
1313 | free (once); |
1447 | ev_free (once); |
1314 | |
1448 | |
1315 | cb (revents, arg); |
1449 | cb (revents, arg); |
1316 | } |
1450 | } |
1317 | |
1451 | |
1318 | static void |
1452 | static void |
… | |
… | |
1328 | } |
1462 | } |
1329 | |
1463 | |
1330 | void |
1464 | void |
1331 | 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) |
1332 | { |
1466 | { |
1333 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1467 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1334 | |
1468 | |
1335 | if (!once) |
1469 | if (!once) |
1336 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1470 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1337 | else |
1471 | else |
1338 | { |
1472 | { |