… | |
… | |
111 | #include <time.h> |
111 | #include <time.h> |
112 | |
112 | |
113 | #include <signal.h> |
113 | #include <signal.h> |
114 | |
114 | |
115 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
116 | # include <unistd.h> |
|
|
117 | # include <sys/time.h> |
116 | # include <sys/time.h> |
118 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
119 | #else |
119 | #else |
120 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
121 | # include <windows.h> |
121 | # include <windows.h> |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
187 | # include "ev.h" |
187 | # include "ev.h" |
188 | #endif |
188 | #endif |
189 | |
189 | |
190 | #if __GNUC__ >= 3 |
190 | #if __GNUC__ >= 3 |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
192 | # define inline_size static inline /* inline for codesize */ |
|
|
193 | # if EV_MINIMAL |
|
|
194 | # define noinline __attribute__ ((noinline)) |
|
|
195 | # define inline_speed static noinline |
|
|
196 | # else |
|
|
197 | # define noinline |
192 | # define inline static inline |
198 | # define inline_speed static inline |
|
|
199 | # endif |
193 | #else |
200 | #else |
194 | # define expect(expr,value) (expr) |
201 | # define expect(expr,value) (expr) |
195 | # define inline static |
202 | # define inline_speed static |
|
|
203 | # define inline_minimal static |
|
|
204 | # define noinline |
196 | #endif |
205 | #endif |
197 | |
206 | |
198 | #define expect_false(expr) expect ((expr) != 0, 0) |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
199 | #define expect_true(expr) expect ((expr) != 0, 1) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
200 | |
209 | |
… | |
… | |
202 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
211 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
203 | |
212 | |
204 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
213 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
205 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
214 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
206 | |
215 | |
207 | typedef struct ev_watcher *W; |
216 | typedef ev_watcher *W; |
208 | typedef struct ev_watcher_list *WL; |
217 | typedef ev_watcher_list *WL; |
209 | typedef struct ev_watcher_time *WT; |
218 | typedef ev_watcher_time *WT; |
210 | |
219 | |
211 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
220 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
212 | |
221 | |
213 | #ifdef _WIN32 |
222 | #ifdef _WIN32 |
214 | # include "ev_win32.c" |
223 | # include "ev_win32.c" |
… | |
… | |
306 | |
315 | |
307 | #endif |
316 | #endif |
308 | |
317 | |
309 | /*****************************************************************************/ |
318 | /*****************************************************************************/ |
310 | |
319 | |
311 | ev_tstamp |
320 | ev_tstamp noinline |
312 | ev_time (void) |
321 | ev_time (void) |
313 | { |
322 | { |
314 | #if EV_USE_REALTIME |
323 | #if EV_USE_REALTIME |
315 | struct timespec ts; |
324 | struct timespec ts; |
316 | clock_gettime (CLOCK_REALTIME, &ts); |
325 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
320 | gettimeofday (&tv, 0); |
329 | gettimeofday (&tv, 0); |
321 | return tv.tv_sec + tv.tv_usec * 1e-6; |
330 | return tv.tv_sec + tv.tv_usec * 1e-6; |
322 | #endif |
331 | #endif |
323 | } |
332 | } |
324 | |
333 | |
325 | inline ev_tstamp |
334 | ev_tstamp inline_size |
326 | get_clock (void) |
335 | get_clock (void) |
327 | { |
336 | { |
328 | #if EV_USE_MONOTONIC |
337 | #if EV_USE_MONOTONIC |
329 | if (expect_true (have_monotonic)) |
338 | if (expect_true (have_monotonic)) |
330 | { |
339 | { |
… | |
… | |
373 | #define array_free(stem, idx) \ |
382 | #define array_free(stem, idx) \ |
374 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
383 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
375 | |
384 | |
376 | /*****************************************************************************/ |
385 | /*****************************************************************************/ |
377 | |
386 | |
378 | static void |
387 | void inline_size |
379 | anfds_init (ANFD *base, int count) |
388 | anfds_init (ANFD *base, int count) |
380 | { |
389 | { |
381 | while (count--) |
390 | while (count--) |
382 | { |
391 | { |
383 | base->head = 0; |
392 | base->head = 0; |
… | |
… | |
386 | |
395 | |
387 | ++base; |
396 | ++base; |
388 | } |
397 | } |
389 | } |
398 | } |
390 | |
399 | |
391 | void |
400 | void noinline |
392 | ev_feed_event (EV_P_ void *w, int revents) |
401 | ev_feed_event (EV_P_ void *w, int revents) |
393 | { |
402 | { |
394 | W w_ = (W)w; |
403 | W w_ = (W)w; |
395 | |
404 | |
396 | if (expect_false (w_->pending)) |
405 | if (expect_false (w_->pending)) |
397 | { |
406 | { |
398 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
407 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
399 | return; |
408 | return; |
400 | } |
409 | } |
401 | |
|
|
402 | if (expect_false (!w_->cb)) |
|
|
403 | return; |
|
|
404 | |
410 | |
405 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
411 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
406 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
412 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
407 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
413 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
408 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
414 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
… | |
… | |
415 | |
421 | |
416 | for (i = 0; i < eventcnt; ++i) |
422 | for (i = 0; i < eventcnt; ++i) |
417 | ev_feed_event (EV_A_ events [i], type); |
423 | ev_feed_event (EV_A_ events [i], type); |
418 | } |
424 | } |
419 | |
425 | |
420 | inline void |
426 | void inline_speed |
421 | fd_event (EV_P_ int fd, int revents) |
427 | fd_event (EV_P_ int fd, int revents) |
422 | { |
428 | { |
423 | ANFD *anfd = anfds + fd; |
429 | ANFD *anfd = anfds + fd; |
424 | struct ev_io *w; |
430 | ev_io *w; |
425 | |
431 | |
426 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
432 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
427 | { |
433 | { |
428 | int ev = w->events & revents; |
434 | int ev = w->events & revents; |
429 | |
435 | |
430 | if (ev) |
436 | if (ev) |
431 | ev_feed_event (EV_A_ (W)w, ev); |
437 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
438 | fd_event (EV_A_ fd, revents); |
444 | fd_event (EV_A_ fd, revents); |
439 | } |
445 | } |
440 | |
446 | |
441 | /*****************************************************************************/ |
447 | /*****************************************************************************/ |
442 | |
448 | |
443 | inline void |
449 | void inline_size |
444 | fd_reify (EV_P) |
450 | fd_reify (EV_P) |
445 | { |
451 | { |
446 | int i; |
452 | int i; |
447 | |
453 | |
448 | for (i = 0; i < fdchangecnt; ++i) |
454 | for (i = 0; i < fdchangecnt; ++i) |
449 | { |
455 | { |
450 | int fd = fdchanges [i]; |
456 | int fd = fdchanges [i]; |
451 | ANFD *anfd = anfds + fd; |
457 | ANFD *anfd = anfds + fd; |
452 | struct ev_io *w; |
458 | ev_io *w; |
453 | |
459 | |
454 | int events = 0; |
460 | int events = 0; |
455 | |
461 | |
456 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
462 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
457 | events |= w->events; |
463 | events |= w->events; |
458 | |
464 | |
459 | #if EV_SELECT_IS_WINSOCKET |
465 | #if EV_SELECT_IS_WINSOCKET |
460 | if (events) |
466 | if (events) |
461 | { |
467 | { |
… | |
… | |
472 | } |
478 | } |
473 | |
479 | |
474 | fdchangecnt = 0; |
480 | fdchangecnt = 0; |
475 | } |
481 | } |
476 | |
482 | |
477 | static void |
483 | void inline_size |
478 | fd_change (EV_P_ int fd) |
484 | fd_change (EV_P_ int fd) |
479 | { |
485 | { |
480 | if (expect_false (anfds [fd].reify)) |
486 | if (expect_false (anfds [fd].reify)) |
481 | return; |
487 | return; |
482 | |
488 | |
… | |
… | |
485 | ++fdchangecnt; |
491 | ++fdchangecnt; |
486 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
492 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
487 | fdchanges [fdchangecnt - 1] = fd; |
493 | fdchanges [fdchangecnt - 1] = fd; |
488 | } |
494 | } |
489 | |
495 | |
490 | static void |
496 | void inline_speed |
491 | fd_kill (EV_P_ int fd) |
497 | fd_kill (EV_P_ int fd) |
492 | { |
498 | { |
493 | struct ev_io *w; |
499 | ev_io *w; |
494 | |
500 | |
495 | while ((w = (struct ev_io *)anfds [fd].head)) |
501 | while ((w = (ev_io *)anfds [fd].head)) |
496 | { |
502 | { |
497 | ev_io_stop (EV_A_ w); |
503 | ev_io_stop (EV_A_ w); |
498 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
504 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
499 | } |
505 | } |
500 | } |
506 | } |
501 | |
507 | |
502 | inline int |
508 | int inline_size |
503 | fd_valid (int fd) |
509 | fd_valid (int fd) |
504 | { |
510 | { |
505 | #ifdef _WIN32 |
511 | #ifdef _WIN32 |
506 | return _get_osfhandle (fd) != -1; |
512 | return _get_osfhandle (fd) != -1; |
507 | #else |
513 | #else |
508 | return fcntl (fd, F_GETFD) != -1; |
514 | return fcntl (fd, F_GETFD) != -1; |
509 | #endif |
515 | #endif |
510 | } |
516 | } |
511 | |
517 | |
512 | /* called on EBADF to verify fds */ |
518 | /* called on EBADF to verify fds */ |
513 | static void |
519 | static void noinline |
514 | fd_ebadf (EV_P) |
520 | fd_ebadf (EV_P) |
515 | { |
521 | { |
516 | int fd; |
522 | int fd; |
517 | |
523 | |
518 | for (fd = 0; fd < anfdmax; ++fd) |
524 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
520 | if (!fd_valid (fd) == -1 && errno == EBADF) |
526 | if (!fd_valid (fd) == -1 && errno == EBADF) |
521 | fd_kill (EV_A_ fd); |
527 | fd_kill (EV_A_ fd); |
522 | } |
528 | } |
523 | |
529 | |
524 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
530 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
525 | static void |
531 | static void noinline |
526 | fd_enomem (EV_P) |
532 | fd_enomem (EV_P) |
527 | { |
533 | { |
528 | int fd; |
534 | int fd; |
529 | |
535 | |
530 | for (fd = anfdmax; fd--; ) |
536 | for (fd = anfdmax; fd--; ) |
… | |
… | |
534 | return; |
540 | return; |
535 | } |
541 | } |
536 | } |
542 | } |
537 | |
543 | |
538 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
544 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
539 | static void |
545 | static void noinline |
540 | fd_rearm_all (EV_P) |
546 | fd_rearm_all (EV_P) |
541 | { |
547 | { |
542 | int fd; |
548 | int fd; |
543 | |
549 | |
544 | /* this should be highly optimised to not do anything but set a flag */ |
550 | /* this should be highly optimised to not do anything but set a flag */ |
… | |
… | |
550 | } |
556 | } |
551 | } |
557 | } |
552 | |
558 | |
553 | /*****************************************************************************/ |
559 | /*****************************************************************************/ |
554 | |
560 | |
555 | static void |
561 | void inline_speed |
556 | upheap (WT *heap, int k) |
562 | upheap (WT *heap, int k) |
557 | { |
563 | { |
558 | WT w = heap [k]; |
564 | WT w = heap [k]; |
559 | |
565 | |
560 | while (k && heap [k >> 1]->at > w->at) |
566 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
567 | heap [k] = w; |
573 | heap [k] = w; |
568 | ((W)heap [k])->active = k + 1; |
574 | ((W)heap [k])->active = k + 1; |
569 | |
575 | |
570 | } |
576 | } |
571 | |
577 | |
572 | static void |
578 | void inline_speed |
573 | downheap (WT *heap, int N, int k) |
579 | downheap (WT *heap, int N, int k) |
574 | { |
580 | { |
575 | WT w = heap [k]; |
581 | WT w = heap [k]; |
576 | |
582 | |
577 | while (k < (N >> 1)) |
583 | while (k < (N >> 1)) |
… | |
… | |
591 | |
597 | |
592 | heap [k] = w; |
598 | heap [k] = w; |
593 | ((W)heap [k])->active = k + 1; |
599 | ((W)heap [k])->active = k + 1; |
594 | } |
600 | } |
595 | |
601 | |
596 | inline void |
602 | void inline_size |
597 | adjustheap (WT *heap, int N, int k) |
603 | adjustheap (WT *heap, int N, int k) |
598 | { |
604 | { |
599 | upheap (heap, k); |
605 | upheap (heap, k); |
600 | downheap (heap, N, k); |
606 | downheap (heap, N, k); |
601 | } |
607 | } |
… | |
… | |
611 | static ANSIG *signals; |
617 | static ANSIG *signals; |
612 | static int signalmax; |
618 | static int signalmax; |
613 | |
619 | |
614 | static int sigpipe [2]; |
620 | static int sigpipe [2]; |
615 | static sig_atomic_t volatile gotsig; |
621 | static sig_atomic_t volatile gotsig; |
616 | static struct ev_io sigev; |
622 | static ev_io sigev; |
617 | |
623 | |
618 | static void |
624 | void inline_size |
619 | signals_init (ANSIG *base, int count) |
625 | signals_init (ANSIG *base, int count) |
620 | { |
626 | { |
621 | while (count--) |
627 | while (count--) |
622 | { |
628 | { |
623 | base->head = 0; |
629 | base->head = 0; |
… | |
… | |
643 | write (sigpipe [1], &signum, 1); |
649 | write (sigpipe [1], &signum, 1); |
644 | errno = old_errno; |
650 | errno = old_errno; |
645 | } |
651 | } |
646 | } |
652 | } |
647 | |
653 | |
648 | void |
654 | void noinline |
649 | ev_feed_signal_event (EV_P_ int signum) |
655 | ev_feed_signal_event (EV_P_ int signum) |
650 | { |
656 | { |
651 | WL w; |
657 | WL w; |
652 | |
658 | |
653 | #if EV_MULTIPLICITY |
659 | #if EV_MULTIPLICITY |
… | |
… | |
664 | for (w = signals [signum].head; w; w = w->next) |
670 | for (w = signals [signum].head; w; w = w->next) |
665 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
671 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
666 | } |
672 | } |
667 | |
673 | |
668 | static void |
674 | static void |
669 | sigcb (EV_P_ struct ev_io *iow, int revents) |
675 | sigcb (EV_P_ ev_io *iow, int revents) |
670 | { |
676 | { |
671 | int signum; |
677 | int signum; |
672 | |
678 | |
673 | read (sigpipe [0], &revents, 1); |
679 | read (sigpipe [0], &revents, 1); |
674 | gotsig = 0; |
680 | gotsig = 0; |
… | |
… | |
676 | for (signum = signalmax; signum--; ) |
682 | for (signum = signalmax; signum--; ) |
677 | if (signals [signum].gotsig) |
683 | if (signals [signum].gotsig) |
678 | ev_feed_signal_event (EV_A_ signum + 1); |
684 | ev_feed_signal_event (EV_A_ signum + 1); |
679 | } |
685 | } |
680 | |
686 | |
681 | static void |
687 | void inline_size |
682 | fd_intern (int fd) |
688 | fd_intern (int fd) |
683 | { |
689 | { |
684 | #ifdef _WIN32 |
690 | #ifdef _WIN32 |
685 | int arg = 1; |
691 | int arg = 1; |
686 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
692 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
688 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
694 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
689 | fcntl (fd, F_SETFL, O_NONBLOCK); |
695 | fcntl (fd, F_SETFL, O_NONBLOCK); |
690 | #endif |
696 | #endif |
691 | } |
697 | } |
692 | |
698 | |
693 | static void |
699 | static void noinline |
694 | siginit (EV_P) |
700 | siginit (EV_P) |
695 | { |
701 | { |
696 | fd_intern (sigpipe [0]); |
702 | fd_intern (sigpipe [0]); |
697 | fd_intern (sigpipe [1]); |
703 | fd_intern (sigpipe [1]); |
698 | |
704 | |
… | |
… | |
701 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
707 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
702 | } |
708 | } |
703 | |
709 | |
704 | /*****************************************************************************/ |
710 | /*****************************************************************************/ |
705 | |
711 | |
706 | static struct ev_child *childs [PID_HASHSIZE]; |
712 | static ev_child *childs [PID_HASHSIZE]; |
707 | |
713 | |
708 | #ifndef _WIN32 |
714 | #ifndef _WIN32 |
709 | |
715 | |
710 | static struct ev_signal childev; |
716 | static ev_signal childev; |
711 | |
717 | |
712 | #ifndef WCONTINUED |
718 | #ifndef WCONTINUED |
713 | # define WCONTINUED 0 |
719 | # define WCONTINUED 0 |
714 | #endif |
720 | #endif |
715 | |
721 | |
716 | static void |
722 | void inline_speed |
717 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
723 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
718 | { |
724 | { |
719 | struct ev_child *w; |
725 | ev_child *w; |
720 | |
726 | |
721 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
727 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
722 | if (w->pid == pid || !w->pid) |
728 | if (w->pid == pid || !w->pid) |
723 | { |
729 | { |
724 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
730 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
725 | w->rpid = pid; |
731 | w->rpid = pid; |
726 | w->rstatus = status; |
732 | w->rstatus = status; |
727 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
733 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
728 | } |
734 | } |
729 | } |
735 | } |
730 | |
736 | |
731 | static void |
737 | static void |
732 | childcb (EV_P_ struct ev_signal *sw, int revents) |
738 | childcb (EV_P_ ev_signal *sw, int revents) |
733 | { |
739 | { |
734 | int pid, status; |
740 | int pid, status; |
735 | |
741 | |
736 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
742 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
737 | { |
743 | { |
… | |
… | |
775 | { |
781 | { |
776 | return EV_VERSION_MINOR; |
782 | return EV_VERSION_MINOR; |
777 | } |
783 | } |
778 | |
784 | |
779 | /* return true if we are running with elevated privileges and should ignore env variables */ |
785 | /* return true if we are running with elevated privileges and should ignore env variables */ |
780 | static int |
786 | int inline_size |
781 | enable_secure (void) |
787 | enable_secure (void) |
782 | { |
788 | { |
783 | #ifdef _WIN32 |
789 | #ifdef _WIN32 |
784 | return 0; |
790 | return 0; |
785 | #else |
791 | #else |
… | |
… | |
907 | array_free (pending, [i]); |
913 | array_free (pending, [i]); |
908 | |
914 | |
909 | /* have to use the microsoft-never-gets-it-right macro */ |
915 | /* have to use the microsoft-never-gets-it-right macro */ |
910 | array_free (fdchange, EMPTY0); |
916 | array_free (fdchange, EMPTY0); |
911 | array_free (timer, EMPTY0); |
917 | array_free (timer, EMPTY0); |
912 | #if EV_PERIODICS |
918 | #if EV_PERIODIC_ENABLE |
913 | array_free (periodic, EMPTY0); |
919 | array_free (periodic, EMPTY0); |
914 | #endif |
920 | #endif |
915 | array_free (idle, EMPTY0); |
921 | array_free (idle, EMPTY0); |
916 | array_free (prepare, EMPTY0); |
922 | array_free (prepare, EMPTY0); |
917 | array_free (check, EMPTY0); |
923 | array_free (check, EMPTY0); |
… | |
… | |
1053 | postfork = 1; |
1059 | postfork = 1; |
1054 | } |
1060 | } |
1055 | |
1061 | |
1056 | /*****************************************************************************/ |
1062 | /*****************************************************************************/ |
1057 | |
1063 | |
1058 | static int |
1064 | int inline_size |
1059 | any_pending (EV_P) |
1065 | any_pending (EV_P) |
1060 | { |
1066 | { |
1061 | int pri; |
1067 | int pri; |
1062 | |
1068 | |
1063 | for (pri = NUMPRI; pri--; ) |
1069 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1065 | return 1; |
1071 | return 1; |
1066 | |
1072 | |
1067 | return 0; |
1073 | return 0; |
1068 | } |
1074 | } |
1069 | |
1075 | |
1070 | inline void |
1076 | void inline_speed |
1071 | call_pending (EV_P) |
1077 | call_pending (EV_P) |
1072 | { |
1078 | { |
1073 | int pri; |
1079 | int pri; |
1074 | |
1080 | |
1075 | for (pri = NUMPRI; pri--; ) |
1081 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1077 | { |
1083 | { |
1078 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1084 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1079 | |
1085 | |
1080 | if (expect_true (p->w)) |
1086 | if (expect_true (p->w)) |
1081 | { |
1087 | { |
|
|
1088 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1089 | |
1082 | p->w->pending = 0; |
1090 | p->w->pending = 0; |
1083 | EV_CB_INVOKE (p->w, p->events); |
1091 | EV_CB_INVOKE (p->w, p->events); |
1084 | } |
1092 | } |
1085 | } |
1093 | } |
1086 | } |
1094 | } |
1087 | |
1095 | |
1088 | inline void |
1096 | void inline_size |
1089 | timers_reify (EV_P) |
1097 | timers_reify (EV_P) |
1090 | { |
1098 | { |
1091 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1099 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1092 | { |
1100 | { |
1093 | struct ev_timer *w = timers [0]; |
1101 | ev_timer *w = timers [0]; |
1094 | |
1102 | |
1095 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1103 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1096 | |
1104 | |
1097 | /* first reschedule or stop timer */ |
1105 | /* first reschedule or stop timer */ |
1098 | if (w->repeat) |
1106 | if (w->repeat) |
… | |
… | |
1110 | |
1118 | |
1111 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1119 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1112 | } |
1120 | } |
1113 | } |
1121 | } |
1114 | |
1122 | |
1115 | #if EV_PERIODICS |
1123 | #if EV_PERIODIC_ENABLE |
1116 | inline void |
1124 | void inline_size |
1117 | periodics_reify (EV_P) |
1125 | periodics_reify (EV_P) |
1118 | { |
1126 | { |
1119 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1127 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1120 | { |
1128 | { |
1121 | struct ev_periodic *w = periodics [0]; |
1129 | ev_periodic *w = periodics [0]; |
1122 | |
1130 | |
1123 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1131 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1124 | |
1132 | |
1125 | /* first reschedule or stop timer */ |
1133 | /* first reschedule or stop timer */ |
1126 | if (w->reschedule_cb) |
1134 | if (w->reschedule_cb) |
… | |
… | |
1140 | |
1148 | |
1141 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1149 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1142 | } |
1150 | } |
1143 | } |
1151 | } |
1144 | |
1152 | |
1145 | static void |
1153 | static void noinline |
1146 | periodics_reschedule (EV_P) |
1154 | periodics_reschedule (EV_P) |
1147 | { |
1155 | { |
1148 | int i; |
1156 | int i; |
1149 | |
1157 | |
1150 | /* adjust periodics after time jump */ |
1158 | /* adjust periodics after time jump */ |
1151 | for (i = 0; i < periodiccnt; ++i) |
1159 | for (i = 0; i < periodiccnt; ++i) |
1152 | { |
1160 | { |
1153 | struct ev_periodic *w = periodics [i]; |
1161 | ev_periodic *w = periodics [i]; |
1154 | |
1162 | |
1155 | if (w->reschedule_cb) |
1163 | if (w->reschedule_cb) |
1156 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1164 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1157 | else if (w->interval) |
1165 | else if (w->interval) |
1158 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1166 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
… | |
… | |
1162 | for (i = periodiccnt >> 1; i--; ) |
1170 | for (i = periodiccnt >> 1; i--; ) |
1163 | downheap ((WT *)periodics, periodiccnt, i); |
1171 | downheap ((WT *)periodics, periodiccnt, i); |
1164 | } |
1172 | } |
1165 | #endif |
1173 | #endif |
1166 | |
1174 | |
1167 | inline int |
1175 | int inline_size |
1168 | time_update_monotonic (EV_P) |
1176 | time_update_monotonic (EV_P) |
1169 | { |
1177 | { |
1170 | mn_now = get_clock (); |
1178 | mn_now = get_clock (); |
1171 | |
1179 | |
1172 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1180 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1180 | ev_rt_now = ev_time (); |
1188 | ev_rt_now = ev_time (); |
1181 | return 1; |
1189 | return 1; |
1182 | } |
1190 | } |
1183 | } |
1191 | } |
1184 | |
1192 | |
1185 | inline void |
1193 | void inline_size |
1186 | time_update (EV_P) |
1194 | time_update (EV_P) |
1187 | { |
1195 | { |
1188 | int i; |
1196 | int i; |
1189 | |
1197 | |
1190 | #if EV_USE_MONOTONIC |
1198 | #if EV_USE_MONOTONIC |
… | |
… | |
1192 | { |
1200 | { |
1193 | if (time_update_monotonic (EV_A)) |
1201 | if (time_update_monotonic (EV_A)) |
1194 | { |
1202 | { |
1195 | ev_tstamp odiff = rtmn_diff; |
1203 | ev_tstamp odiff = rtmn_diff; |
1196 | |
1204 | |
1197 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1205 | /* loop a few times, before making important decisions. |
|
|
1206 | * on the choice of "4": one iteration isn't enough, |
|
|
1207 | * in case we get preempted during the calls to |
|
|
1208 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1209 | * to succeed in that case, though. and looping a few more times |
|
|
1210 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1211 | * in the unlikely event of getting preempted here. |
|
|
1212 | */ |
|
|
1213 | for (i = 4; --i; ) |
1198 | { |
1214 | { |
1199 | rtmn_diff = ev_rt_now - mn_now; |
1215 | rtmn_diff = ev_rt_now - mn_now; |
1200 | |
1216 | |
1201 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1217 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1202 | return; /* all is well */ |
1218 | return; /* all is well */ |
… | |
… | |
1204 | ev_rt_now = ev_time (); |
1220 | ev_rt_now = ev_time (); |
1205 | mn_now = get_clock (); |
1221 | mn_now = get_clock (); |
1206 | now_floor = mn_now; |
1222 | now_floor = mn_now; |
1207 | } |
1223 | } |
1208 | |
1224 | |
1209 | # if EV_PERIODICS |
1225 | # if EV_PERIODIC_ENABLE |
1210 | periodics_reschedule (EV_A); |
1226 | periodics_reschedule (EV_A); |
1211 | # endif |
1227 | # endif |
1212 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1228 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1213 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1229 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1214 | } |
1230 | } |
… | |
… | |
1218 | { |
1234 | { |
1219 | ev_rt_now = ev_time (); |
1235 | ev_rt_now = ev_time (); |
1220 | |
1236 | |
1221 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1237 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1222 | { |
1238 | { |
1223 | #if EV_PERIODICS |
1239 | #if EV_PERIODIC_ENABLE |
1224 | periodics_reschedule (EV_A); |
1240 | periodics_reschedule (EV_A); |
1225 | #endif |
1241 | #endif |
1226 | |
1242 | |
1227 | /* adjust timers. this is easy, as the offset is the same for all */ |
1243 | /* adjust timers. this is easy, as the offset is the same for all */ |
1228 | for (i = 0; i < timercnt; ++i) |
1244 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1295 | { |
1311 | { |
1296 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1312 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1297 | if (block > to) block = to; |
1313 | if (block > to) block = to; |
1298 | } |
1314 | } |
1299 | |
1315 | |
1300 | #if EV_PERIODICS |
1316 | #if EV_PERIODIC_ENABLE |
1301 | if (periodiccnt) |
1317 | if (periodiccnt) |
1302 | { |
1318 | { |
1303 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1319 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1304 | if (block > to) block = to; |
1320 | if (block > to) block = to; |
1305 | } |
1321 | } |
… | |
… | |
1314 | /* update ev_rt_now, do magic */ |
1330 | /* update ev_rt_now, do magic */ |
1315 | time_update (EV_A); |
1331 | time_update (EV_A); |
1316 | |
1332 | |
1317 | /* queue pending timers and reschedule them */ |
1333 | /* queue pending timers and reschedule them */ |
1318 | timers_reify (EV_A); /* relative timers called last */ |
1334 | timers_reify (EV_A); /* relative timers called last */ |
1319 | #if EV_PERIODICS |
1335 | #if EV_PERIODIC_ENABLE |
1320 | periodics_reify (EV_A); /* absolute timers called first */ |
1336 | periodics_reify (EV_A); /* absolute timers called first */ |
1321 | #endif |
1337 | #endif |
1322 | |
1338 | |
1323 | /* queue idle watchers unless io or timers are pending */ |
1339 | /* queue idle watchers unless other events are pending */ |
1324 | if (idlecnt && !any_pending (EV_A)) |
1340 | if (idlecnt && !any_pending (EV_A)) |
1325 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1341 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1326 | |
1342 | |
1327 | /* queue check watchers, to be executed first */ |
1343 | /* queue check watchers, to be executed first */ |
1328 | if (expect_false (checkcnt)) |
1344 | if (expect_false (checkcnt)) |
… | |
… | |
1344 | loop_done = how; |
1360 | loop_done = how; |
1345 | } |
1361 | } |
1346 | |
1362 | |
1347 | /*****************************************************************************/ |
1363 | /*****************************************************************************/ |
1348 | |
1364 | |
1349 | inline void |
1365 | void inline_size |
1350 | wlist_add (WL *head, WL elem) |
1366 | wlist_add (WL *head, WL elem) |
1351 | { |
1367 | { |
1352 | elem->next = *head; |
1368 | elem->next = *head; |
1353 | *head = elem; |
1369 | *head = elem; |
1354 | } |
1370 | } |
1355 | |
1371 | |
1356 | inline void |
1372 | void inline_size |
1357 | wlist_del (WL *head, WL elem) |
1373 | wlist_del (WL *head, WL elem) |
1358 | { |
1374 | { |
1359 | while (*head) |
1375 | while (*head) |
1360 | { |
1376 | { |
1361 | if (*head == elem) |
1377 | if (*head == elem) |
… | |
… | |
1366 | |
1382 | |
1367 | head = &(*head)->next; |
1383 | head = &(*head)->next; |
1368 | } |
1384 | } |
1369 | } |
1385 | } |
1370 | |
1386 | |
1371 | inline void |
1387 | void inline_speed |
1372 | ev_clear_pending (EV_P_ W w) |
1388 | ev_clear_pending (EV_P_ W w) |
1373 | { |
1389 | { |
1374 | if (w->pending) |
1390 | if (w->pending) |
1375 | { |
1391 | { |
1376 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1392 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1377 | w->pending = 0; |
1393 | w->pending = 0; |
1378 | } |
1394 | } |
1379 | } |
1395 | } |
1380 | |
1396 | |
1381 | inline void |
1397 | void inline_speed |
1382 | ev_start (EV_P_ W w, int active) |
1398 | ev_start (EV_P_ W w, int active) |
1383 | { |
1399 | { |
1384 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1400 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1385 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1401 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1386 | |
1402 | |
1387 | w->active = active; |
1403 | w->active = active; |
1388 | ev_ref (EV_A); |
1404 | ev_ref (EV_A); |
1389 | } |
1405 | } |
1390 | |
1406 | |
1391 | inline void |
1407 | void inline_size |
1392 | ev_stop (EV_P_ W w) |
1408 | ev_stop (EV_P_ W w) |
1393 | { |
1409 | { |
1394 | ev_unref (EV_A); |
1410 | ev_unref (EV_A); |
1395 | w->active = 0; |
1411 | w->active = 0; |
1396 | } |
1412 | } |
1397 | |
1413 | |
1398 | /*****************************************************************************/ |
1414 | /*****************************************************************************/ |
1399 | |
1415 | |
1400 | void |
1416 | void |
1401 | ev_io_start (EV_P_ struct ev_io *w) |
1417 | ev_io_start (EV_P_ ev_io *w) |
1402 | { |
1418 | { |
1403 | int fd = w->fd; |
1419 | int fd = w->fd; |
1404 | |
1420 | |
1405 | if (expect_false (ev_is_active (w))) |
1421 | if (expect_false (ev_is_active (w))) |
1406 | return; |
1422 | return; |
… | |
… | |
1413 | |
1429 | |
1414 | fd_change (EV_A_ fd); |
1430 | fd_change (EV_A_ fd); |
1415 | } |
1431 | } |
1416 | |
1432 | |
1417 | void |
1433 | void |
1418 | ev_io_stop (EV_P_ struct ev_io *w) |
1434 | ev_io_stop (EV_P_ ev_io *w) |
1419 | { |
1435 | { |
1420 | ev_clear_pending (EV_A_ (W)w); |
1436 | ev_clear_pending (EV_A_ (W)w); |
1421 | if (expect_false (!ev_is_active (w))) |
1437 | if (expect_false (!ev_is_active (w))) |
1422 | return; |
1438 | return; |
1423 | |
1439 | |
… | |
… | |
1428 | |
1444 | |
1429 | fd_change (EV_A_ w->fd); |
1445 | fd_change (EV_A_ w->fd); |
1430 | } |
1446 | } |
1431 | |
1447 | |
1432 | void |
1448 | void |
1433 | ev_timer_start (EV_P_ struct ev_timer *w) |
1449 | ev_timer_start (EV_P_ ev_timer *w) |
1434 | { |
1450 | { |
1435 | if (expect_false (ev_is_active (w))) |
1451 | if (expect_false (ev_is_active (w))) |
1436 | return; |
1452 | return; |
1437 | |
1453 | |
1438 | ((WT)w)->at += mn_now; |
1454 | ((WT)w)->at += mn_now; |
1439 | |
1455 | |
1440 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1456 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1441 | |
1457 | |
1442 | ev_start (EV_A_ (W)w, ++timercnt); |
1458 | ev_start (EV_A_ (W)w, ++timercnt); |
1443 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1459 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1444 | timers [timercnt - 1] = w; |
1460 | timers [timercnt - 1] = w; |
1445 | upheap ((WT *)timers, timercnt - 1); |
1461 | upheap ((WT *)timers, timercnt - 1); |
1446 | |
1462 | |
1447 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1463 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1448 | } |
1464 | } |
1449 | |
1465 | |
1450 | void |
1466 | void |
1451 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1467 | ev_timer_stop (EV_P_ ev_timer *w) |
1452 | { |
1468 | { |
1453 | ev_clear_pending (EV_A_ (W)w); |
1469 | ev_clear_pending (EV_A_ (W)w); |
1454 | if (expect_false (!ev_is_active (w))) |
1470 | if (expect_false (!ev_is_active (w))) |
1455 | return; |
1471 | return; |
1456 | |
1472 | |
… | |
… | |
1466 | |
1482 | |
1467 | ev_stop (EV_A_ (W)w); |
1483 | ev_stop (EV_A_ (W)w); |
1468 | } |
1484 | } |
1469 | |
1485 | |
1470 | void |
1486 | void |
1471 | ev_timer_again (EV_P_ struct ev_timer *w) |
1487 | ev_timer_again (EV_P_ ev_timer *w) |
1472 | { |
1488 | { |
1473 | if (ev_is_active (w)) |
1489 | if (ev_is_active (w)) |
1474 | { |
1490 | { |
1475 | if (w->repeat) |
1491 | if (w->repeat) |
1476 | { |
1492 | { |
… | |
… | |
1485 | w->at = w->repeat; |
1501 | w->at = w->repeat; |
1486 | ev_timer_start (EV_A_ w); |
1502 | ev_timer_start (EV_A_ w); |
1487 | } |
1503 | } |
1488 | } |
1504 | } |
1489 | |
1505 | |
1490 | #if EV_PERIODICS |
1506 | #if EV_PERIODIC_ENABLE |
1491 | void |
1507 | void |
1492 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1508 | ev_periodic_start (EV_P_ ev_periodic *w) |
1493 | { |
1509 | { |
1494 | if (expect_false (ev_is_active (w))) |
1510 | if (expect_false (ev_is_active (w))) |
1495 | return; |
1511 | return; |
1496 | |
1512 | |
1497 | if (w->reschedule_cb) |
1513 | if (w->reschedule_cb) |
… | |
… | |
1502 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1518 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1503 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1519 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1504 | } |
1520 | } |
1505 | |
1521 | |
1506 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1522 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1507 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1523 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1508 | periodics [periodiccnt - 1] = w; |
1524 | periodics [periodiccnt - 1] = w; |
1509 | upheap ((WT *)periodics, periodiccnt - 1); |
1525 | upheap ((WT *)periodics, periodiccnt - 1); |
1510 | |
1526 | |
1511 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1527 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1512 | } |
1528 | } |
1513 | |
1529 | |
1514 | void |
1530 | void |
1515 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1531 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1516 | { |
1532 | { |
1517 | ev_clear_pending (EV_A_ (W)w); |
1533 | ev_clear_pending (EV_A_ (W)w); |
1518 | if (expect_false (!ev_is_active (w))) |
1534 | if (expect_false (!ev_is_active (w))) |
1519 | return; |
1535 | return; |
1520 | |
1536 | |
… | |
… | |
1528 | |
1544 | |
1529 | ev_stop (EV_A_ (W)w); |
1545 | ev_stop (EV_A_ (W)w); |
1530 | } |
1546 | } |
1531 | |
1547 | |
1532 | void |
1548 | void |
1533 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1549 | ev_periodic_again (EV_P_ ev_periodic *w) |
1534 | { |
1550 | { |
1535 | /* TODO: use adjustheap and recalculation */ |
1551 | /* TODO: use adjustheap and recalculation */ |
1536 | ev_periodic_stop (EV_A_ w); |
1552 | ev_periodic_stop (EV_A_ w); |
1537 | ev_periodic_start (EV_A_ w); |
1553 | ev_periodic_start (EV_A_ w); |
1538 | } |
1554 | } |
1539 | #endif |
1555 | #endif |
1540 | |
1556 | |
1541 | void |
1557 | void |
1542 | ev_idle_start (EV_P_ struct ev_idle *w) |
1558 | ev_idle_start (EV_P_ ev_idle *w) |
1543 | { |
1559 | { |
1544 | if (expect_false (ev_is_active (w))) |
1560 | if (expect_false (ev_is_active (w))) |
1545 | return; |
1561 | return; |
1546 | |
1562 | |
1547 | ev_start (EV_A_ (W)w, ++idlecnt); |
1563 | ev_start (EV_A_ (W)w, ++idlecnt); |
1548 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1564 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1549 | idles [idlecnt - 1] = w; |
1565 | idles [idlecnt - 1] = w; |
1550 | } |
1566 | } |
1551 | |
1567 | |
1552 | void |
1568 | void |
1553 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1569 | ev_idle_stop (EV_P_ ev_idle *w) |
1554 | { |
1570 | { |
1555 | ev_clear_pending (EV_A_ (W)w); |
1571 | ev_clear_pending (EV_A_ (W)w); |
1556 | if (expect_false (!ev_is_active (w))) |
1572 | if (expect_false (!ev_is_active (w))) |
1557 | return; |
1573 | return; |
1558 | |
1574 | |
|
|
1575 | { |
|
|
1576 | int active = ((W)w)->active; |
1559 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1577 | idles [active - 1] = idles [--idlecnt]; |
|
|
1578 | ((W)idles [active - 1])->active = active; |
|
|
1579 | } |
|
|
1580 | |
1560 | ev_stop (EV_A_ (W)w); |
1581 | ev_stop (EV_A_ (W)w); |
1561 | } |
1582 | } |
1562 | |
1583 | |
1563 | void |
1584 | void |
1564 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1585 | ev_prepare_start (EV_P_ ev_prepare *w) |
1565 | { |
1586 | { |
1566 | if (expect_false (ev_is_active (w))) |
1587 | if (expect_false (ev_is_active (w))) |
1567 | return; |
1588 | return; |
1568 | |
1589 | |
1569 | ev_start (EV_A_ (W)w, ++preparecnt); |
1590 | ev_start (EV_A_ (W)w, ++preparecnt); |
1570 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1591 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1571 | prepares [preparecnt - 1] = w; |
1592 | prepares [preparecnt - 1] = w; |
1572 | } |
1593 | } |
1573 | |
1594 | |
1574 | void |
1595 | void |
1575 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1596 | ev_prepare_stop (EV_P_ ev_prepare *w) |
1576 | { |
1597 | { |
1577 | ev_clear_pending (EV_A_ (W)w); |
1598 | ev_clear_pending (EV_A_ (W)w); |
1578 | if (expect_false (!ev_is_active (w))) |
1599 | if (expect_false (!ev_is_active (w))) |
1579 | return; |
1600 | return; |
1580 | |
1601 | |
|
|
1602 | { |
|
|
1603 | int active = ((W)w)->active; |
1581 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1604 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1605 | ((W)prepares [active - 1])->active = active; |
|
|
1606 | } |
|
|
1607 | |
1582 | ev_stop (EV_A_ (W)w); |
1608 | ev_stop (EV_A_ (W)w); |
1583 | } |
1609 | } |
1584 | |
1610 | |
1585 | void |
1611 | void |
1586 | ev_check_start (EV_P_ struct ev_check *w) |
1612 | ev_check_start (EV_P_ ev_check *w) |
1587 | { |
1613 | { |
1588 | if (expect_false (ev_is_active (w))) |
1614 | if (expect_false (ev_is_active (w))) |
1589 | return; |
1615 | return; |
1590 | |
1616 | |
1591 | ev_start (EV_A_ (W)w, ++checkcnt); |
1617 | ev_start (EV_A_ (W)w, ++checkcnt); |
1592 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1618 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1593 | checks [checkcnt - 1] = w; |
1619 | checks [checkcnt - 1] = w; |
1594 | } |
1620 | } |
1595 | |
1621 | |
1596 | void |
1622 | void |
1597 | ev_check_stop (EV_P_ struct ev_check *w) |
1623 | ev_check_stop (EV_P_ ev_check *w) |
1598 | { |
1624 | { |
1599 | ev_clear_pending (EV_A_ (W)w); |
1625 | ev_clear_pending (EV_A_ (W)w); |
1600 | if (expect_false (!ev_is_active (w))) |
1626 | if (expect_false (!ev_is_active (w))) |
1601 | return; |
1627 | return; |
1602 | |
1628 | |
|
|
1629 | { |
|
|
1630 | int active = ((W)w)->active; |
1603 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1631 | checks [active - 1] = checks [--checkcnt]; |
|
|
1632 | ((W)checks [active - 1])->active = active; |
|
|
1633 | } |
|
|
1634 | |
1604 | ev_stop (EV_A_ (W)w); |
1635 | ev_stop (EV_A_ (W)w); |
1605 | } |
1636 | } |
1606 | |
1637 | |
1607 | #ifndef SA_RESTART |
1638 | #ifndef SA_RESTART |
1608 | # define SA_RESTART 0 |
1639 | # define SA_RESTART 0 |
1609 | #endif |
1640 | #endif |
1610 | |
1641 | |
1611 | void |
1642 | void |
1612 | ev_signal_start (EV_P_ struct ev_signal *w) |
1643 | ev_signal_start (EV_P_ ev_signal *w) |
1613 | { |
1644 | { |
1614 | #if EV_MULTIPLICITY |
1645 | #if EV_MULTIPLICITY |
1615 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1646 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1616 | #endif |
1647 | #endif |
1617 | if (expect_false (ev_is_active (w))) |
1648 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1636 | #endif |
1667 | #endif |
1637 | } |
1668 | } |
1638 | } |
1669 | } |
1639 | |
1670 | |
1640 | void |
1671 | void |
1641 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1672 | ev_signal_stop (EV_P_ ev_signal *w) |
1642 | { |
1673 | { |
1643 | ev_clear_pending (EV_A_ (W)w); |
1674 | ev_clear_pending (EV_A_ (W)w); |
1644 | if (expect_false (!ev_is_active (w))) |
1675 | if (expect_false (!ev_is_active (w))) |
1645 | return; |
1676 | return; |
1646 | |
1677 | |
… | |
… | |
1650 | if (!signals [w->signum - 1].head) |
1681 | if (!signals [w->signum - 1].head) |
1651 | signal (w->signum, SIG_DFL); |
1682 | signal (w->signum, SIG_DFL); |
1652 | } |
1683 | } |
1653 | |
1684 | |
1654 | void |
1685 | void |
1655 | ev_child_start (EV_P_ struct ev_child *w) |
1686 | ev_child_start (EV_P_ ev_child *w) |
1656 | { |
1687 | { |
1657 | #if EV_MULTIPLICITY |
1688 | #if EV_MULTIPLICITY |
1658 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1689 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1659 | #endif |
1690 | #endif |
1660 | if (expect_false (ev_is_active (w))) |
1691 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1663 | ev_start (EV_A_ (W)w, 1); |
1694 | ev_start (EV_A_ (W)w, 1); |
1664 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1695 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1665 | } |
1696 | } |
1666 | |
1697 | |
1667 | void |
1698 | void |
1668 | ev_child_stop (EV_P_ struct ev_child *w) |
1699 | ev_child_stop (EV_P_ ev_child *w) |
1669 | { |
1700 | { |
1670 | ev_clear_pending (EV_A_ (W)w); |
1701 | ev_clear_pending (EV_A_ (W)w); |
1671 | if (expect_false (!ev_is_active (w))) |
1702 | if (expect_false (!ev_is_active (w))) |
1672 | return; |
1703 | return; |
1673 | |
1704 | |
1674 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1705 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1675 | ev_stop (EV_A_ (W)w); |
1706 | ev_stop (EV_A_ (W)w); |
1676 | } |
1707 | } |
1677 | |
1708 | |
1678 | #if EV_MULTIPLICITY |
1709 | #if EV_EMBED_ENABLE |
|
|
1710 | void noinline |
|
|
1711 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1712 | { |
|
|
1713 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1714 | } |
|
|
1715 | |
1679 | static void |
1716 | static void |
1680 | embed_cb (EV_P_ struct ev_io *io, int revents) |
1717 | embed_cb (EV_P_ ev_io *io, int revents) |
1681 | { |
1718 | { |
1682 | struct ev_embed *w = (struct ev_embed *)(((char *)io) - offsetof (struct ev_embed, io)); |
1719 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
1683 | |
1720 | |
|
|
1721 | if (ev_cb (w)) |
1684 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
1722 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
1685 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
1723 | else |
|
|
1724 | ev_embed_sweep (loop, w); |
1686 | } |
1725 | } |
1687 | |
1726 | |
1688 | void |
1727 | void |
1689 | ev_embed_start (EV_P_ struct ev_embed *w) |
1728 | ev_embed_start (EV_P_ ev_embed *w) |
1690 | { |
1729 | { |
1691 | if (expect_false (ev_is_active (w))) |
1730 | if (expect_false (ev_is_active (w))) |
1692 | return; |
1731 | return; |
1693 | |
1732 | |
1694 | { |
1733 | { |
1695 | struct ev_loop *loop = w->loop; |
1734 | struct ev_loop *loop = w->loop; |
1696 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
1735 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
1697 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
1736 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
1698 | } |
1737 | } |
1699 | |
1738 | |
|
|
1739 | ev_set_priority (&w->io, ev_priority (w)); |
1700 | ev_io_start (EV_A_ &w->io); |
1740 | ev_io_start (EV_A_ &w->io); |
|
|
1741 | |
1701 | ev_start (EV_A_ (W)w, 1); |
1742 | ev_start (EV_A_ (W)w, 1); |
1702 | } |
1743 | } |
1703 | |
1744 | |
1704 | void |
1745 | void |
1705 | ev_embed_stop (EV_P_ struct ev_embed *w) |
1746 | ev_embed_stop (EV_P_ ev_embed *w) |
1706 | { |
1747 | { |
1707 | ev_clear_pending (EV_A_ (W)w); |
1748 | ev_clear_pending (EV_A_ (W)w); |
1708 | if (expect_false (!ev_is_active (w))) |
1749 | if (expect_false (!ev_is_active (w))) |
1709 | return; |
1750 | return; |
1710 | |
1751 | |
1711 | ev_io_stop (EV_A_ &w->io); |
1752 | ev_io_stop (EV_A_ &w->io); |
|
|
1753 | |
1712 | ev_stop (EV_A_ (W)w); |
1754 | ev_stop (EV_A_ (W)w); |
1713 | } |
1755 | } |
1714 | #endif |
1756 | #endif |
1715 | |
1757 | |
|
|
1758 | #if EV_STAT_ENABLE |
|
|
1759 | |
|
|
1760 | # ifdef _WIN32 |
|
|
1761 | # define lstat(a,b) stat(a,b) |
|
|
1762 | # endif |
|
|
1763 | |
|
|
1764 | void |
|
|
1765 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1766 | { |
|
|
1767 | if (lstat (w->path, &w->attr) < 0) |
|
|
1768 | w->attr.st_nlink = 0; |
|
|
1769 | else if (!w->attr.st_nlink) |
|
|
1770 | w->attr.st_nlink = 1; |
|
|
1771 | } |
|
|
1772 | |
|
|
1773 | static void |
|
|
1774 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1775 | { |
|
|
1776 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1777 | |
|
|
1778 | /* we copy this here each the time so that */ |
|
|
1779 | /* prev has the old value when the callback gets invoked */ |
|
|
1780 | w->prev = w->attr; |
|
|
1781 | ev_stat_stat (EV_A_ w); |
|
|
1782 | |
|
|
1783 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1784 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1785 | } |
|
|
1786 | |
|
|
1787 | void |
|
|
1788 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1789 | { |
|
|
1790 | if (expect_false (ev_is_active (w))) |
|
|
1791 | return; |
|
|
1792 | |
|
|
1793 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1794 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1795 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1796 | |
|
|
1797 | ev_stat_stat (EV_A_ w); |
|
|
1798 | |
|
|
1799 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1800 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1801 | ev_timer_start (EV_A_ &w->timer); |
|
|
1802 | |
|
|
1803 | ev_start (EV_A_ (W)w, 1); |
|
|
1804 | } |
|
|
1805 | |
|
|
1806 | void |
|
|
1807 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1808 | { |
|
|
1809 | ev_clear_pending (EV_A_ (W)w); |
|
|
1810 | if (expect_false (!ev_is_active (w))) |
|
|
1811 | return; |
|
|
1812 | |
|
|
1813 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1814 | |
|
|
1815 | ev_stop (EV_A_ (W)w); |
|
|
1816 | } |
|
|
1817 | #endif |
|
|
1818 | |
1716 | /*****************************************************************************/ |
1819 | /*****************************************************************************/ |
1717 | |
1820 | |
1718 | struct ev_once |
1821 | struct ev_once |
1719 | { |
1822 | { |
1720 | struct ev_io io; |
1823 | ev_io io; |
1721 | struct ev_timer to; |
1824 | ev_timer to; |
1722 | void (*cb)(int revents, void *arg); |
1825 | void (*cb)(int revents, void *arg); |
1723 | void *arg; |
1826 | void *arg; |
1724 | }; |
1827 | }; |
1725 | |
1828 | |
1726 | static void |
1829 | static void |
… | |
… | |
1735 | |
1838 | |
1736 | cb (revents, arg); |
1839 | cb (revents, arg); |
1737 | } |
1840 | } |
1738 | |
1841 | |
1739 | static void |
1842 | static void |
1740 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1843 | once_cb_io (EV_P_ ev_io *w, int revents) |
1741 | { |
1844 | { |
1742 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1845 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1743 | } |
1846 | } |
1744 | |
1847 | |
1745 | static void |
1848 | static void |
1746 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1849 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1747 | { |
1850 | { |
1748 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1851 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1749 | } |
1852 | } |
1750 | |
1853 | |
1751 | void |
1854 | void |