ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.129 by root, Fri Nov 23 05:00:44 2007 UTC vs.
Revision 1.140 by root, Mon Nov 26 19:49:36 2007 UTC

32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
34#endif 34#endif
35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
37# include "config.h" 40# include "config.h"
41# endif
38 42
39# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC 44# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
42# endif 46# endif
107#include <time.h> 111#include <time.h>
108 112
109#include <signal.h> 113#include <signal.h>
110 114
111#ifndef _WIN32 115#ifndef _WIN32
112# include <unistd.h>
113# include <sys/time.h> 116# include <sys/time.h>
114# include <sys/wait.h> 117# include <sys/wait.h>
118# include <unistd.h>
115#else 119#else
116# define WIN32_LEAN_AND_MEAN 120# define WIN32_LEAN_AND_MEAN
117# include <windows.h> 121# include <windows.h>
118# ifndef EV_SELECT_IS_WINSOCKET 122# ifndef EV_SELECT_IS_WINSOCKET
119# define EV_SELECT_IS_WINSOCKET 1 123# define EV_SELECT_IS_WINSOCKET 1
183# include "ev.h" 187# include "ev.h"
184#endif 188#endif
185 189
186#if __GNUC__ >= 3 190#if __GNUC__ >= 3
187# 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
188# define inline static inline 198# define inline_speed static inline
199# endif
189#else 200#else
190# define expect(expr,value) (expr) 201# define expect(expr,value) (expr)
191# define inline static 202# define inline_speed static
203# define inline_minimal static
204# define noinline
192#endif 205#endif
193 206
194#define expect_false(expr) expect ((expr) != 0, 0) 207#define expect_false(expr) expect ((expr) != 0, 0)
195#define expect_true(expr) expect ((expr) != 0, 1) 208#define expect_true(expr) expect ((expr) != 0, 1)
196 209
198#define ABSPRI(w) ((w)->priority - EV_MINPRI) 211#define ABSPRI(w) ((w)->priority - EV_MINPRI)
199 212
200#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 213#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
201#define EMPTY2(a,b) /* used to suppress some warnings */ 214#define EMPTY2(a,b) /* used to suppress some warnings */
202 215
203typedef struct ev_watcher *W; 216typedef ev_watcher *W;
204typedef struct ev_watcher_list *WL; 217typedef ev_watcher_list *WL;
205typedef struct ev_watcher_time *WT; 218typedef ev_watcher_time *WT;
206 219
207static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 220static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
208 221
209#ifdef _WIN32 222#ifdef _WIN32
210# include "ev_win32.c" 223# include "ev_win32.c"
302 315
303#endif 316#endif
304 317
305/*****************************************************************************/ 318/*****************************************************************************/
306 319
307ev_tstamp 320ev_tstamp noinline
308ev_time (void) 321ev_time (void)
309{ 322{
310#if EV_USE_REALTIME 323#if EV_USE_REALTIME
311 struct timespec ts; 324 struct timespec ts;
312 clock_gettime (CLOCK_REALTIME, &ts); 325 clock_gettime (CLOCK_REALTIME, &ts);
316 gettimeofday (&tv, 0); 329 gettimeofday (&tv, 0);
317 return tv.tv_sec + tv.tv_usec * 1e-6; 330 return tv.tv_sec + tv.tv_usec * 1e-6;
318#endif 331#endif
319} 332}
320 333
321inline ev_tstamp 334ev_tstamp inline_size
322get_clock (void) 335get_clock (void)
323{ 336{
324#if EV_USE_MONOTONIC 337#if EV_USE_MONOTONIC
325 if (expect_true (have_monotonic)) 338 if (expect_true (have_monotonic))
326 { 339 {
369#define array_free(stem, idx) \ 382#define array_free(stem, idx) \
370 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;
371 384
372/*****************************************************************************/ 385/*****************************************************************************/
373 386
374static void 387void inline_size
375anfds_init (ANFD *base, int count) 388anfds_init (ANFD *base, int count)
376{ 389{
377 while (count--) 390 while (count--)
378 { 391 {
379 base->head = 0; 392 base->head = 0;
382 395
383 ++base; 396 ++base;
384 } 397 }
385} 398}
386 399
387void 400void noinline
388ev_feed_event (EV_P_ void *w, int revents) 401ev_feed_event (EV_P_ void *w, int revents)
389{ 402{
390 W w_ = (W)w; 403 W w_ = (W)w;
391 404
392 if (expect_false (w_->pending)) 405 if (expect_false (w_->pending))
408 421
409 for (i = 0; i < eventcnt; ++i) 422 for (i = 0; i < eventcnt; ++i)
410 ev_feed_event (EV_A_ events [i], type); 423 ev_feed_event (EV_A_ events [i], type);
411} 424}
412 425
413inline void 426void inline_speed
414fd_event (EV_P_ int fd, int revents) 427fd_event (EV_P_ int fd, int revents)
415{ 428{
416 ANFD *anfd = anfds + fd; 429 ANFD *anfd = anfds + fd;
417 struct ev_io *w; 430 ev_io *w;
418 431
419 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)
420 { 433 {
421 int ev = w->events & revents; 434 int ev = w->events & revents;
422 435
423 if (ev) 436 if (ev)
424 ev_feed_event (EV_A_ (W)w, ev); 437 ev_feed_event (EV_A_ (W)w, ev);
431 fd_event (EV_A_ fd, revents); 444 fd_event (EV_A_ fd, revents);
432} 445}
433 446
434/*****************************************************************************/ 447/*****************************************************************************/
435 448
436inline void 449void inline_size
437fd_reify (EV_P) 450fd_reify (EV_P)
438{ 451{
439 int i; 452 int i;
440 453
441 for (i = 0; i < fdchangecnt; ++i) 454 for (i = 0; i < fdchangecnt; ++i)
442 { 455 {
443 int fd = fdchanges [i]; 456 int fd = fdchanges [i];
444 ANFD *anfd = anfds + fd; 457 ANFD *anfd = anfds + fd;
445 struct ev_io *w; 458 ev_io *w;
446 459
447 int events = 0; 460 int events = 0;
448 461
449 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)
450 events |= w->events; 463 events |= w->events;
451 464
452#if EV_SELECT_IS_WINSOCKET 465#if EV_SELECT_IS_WINSOCKET
453 if (events) 466 if (events)
454 { 467 {
458 } 471 }
459#endif 472#endif
460 473
461 anfd->reify = 0; 474 anfd->reify = 0;
462 475
463 method_modify (EV_A_ fd, anfd->events, events); 476 backend_modify (EV_A_ fd, anfd->events, events);
464 anfd->events = events; 477 anfd->events = events;
465 } 478 }
466 479
467 fdchangecnt = 0; 480 fdchangecnt = 0;
468} 481}
469 482
470static void 483void inline_size
471fd_change (EV_P_ int fd) 484fd_change (EV_P_ int fd)
472{ 485{
473 if (expect_false (anfds [fd].reify)) 486 if (expect_false (anfds [fd].reify))
474 return; 487 return;
475 488
478 ++fdchangecnt; 491 ++fdchangecnt;
479 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 492 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
480 fdchanges [fdchangecnt - 1] = fd; 493 fdchanges [fdchangecnt - 1] = fd;
481} 494}
482 495
483static void 496void inline_speed
484fd_kill (EV_P_ int fd) 497fd_kill (EV_P_ int fd)
485{ 498{
486 struct ev_io *w; 499 ev_io *w;
487 500
488 while ((w = (struct ev_io *)anfds [fd].head)) 501 while ((w = (ev_io *)anfds [fd].head))
489 { 502 {
490 ev_io_stop (EV_A_ w); 503 ev_io_stop (EV_A_ w);
491 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);
492 } 505 }
493} 506}
494 507
495inline int 508int inline_size
496fd_valid (int fd) 509fd_valid (int fd)
497{ 510{
498#ifdef _WIN32 511#ifdef _WIN32
499 return _get_osfhandle (fd) != -1; 512 return _get_osfhandle (fd) != -1;
500#else 513#else
501 return fcntl (fd, F_GETFD) != -1; 514 return fcntl (fd, F_GETFD) != -1;
502#endif 515#endif
503} 516}
504 517
505/* called on EBADF to verify fds */ 518/* called on EBADF to verify fds */
506static void 519static void noinline
507fd_ebadf (EV_P) 520fd_ebadf (EV_P)
508{ 521{
509 int fd; 522 int fd;
510 523
511 for (fd = 0; fd < anfdmax; ++fd) 524 for (fd = 0; fd < anfdmax; ++fd)
513 if (!fd_valid (fd) == -1 && errno == EBADF) 526 if (!fd_valid (fd) == -1 && errno == EBADF)
514 fd_kill (EV_A_ fd); 527 fd_kill (EV_A_ fd);
515} 528}
516 529
517/* 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 */
518static void 531static void noinline
519fd_enomem (EV_P) 532fd_enomem (EV_P)
520{ 533{
521 int fd; 534 int fd;
522 535
523 for (fd = anfdmax; fd--; ) 536 for (fd = anfdmax; fd--; )
526 fd_kill (EV_A_ fd); 539 fd_kill (EV_A_ fd);
527 return; 540 return;
528 } 541 }
529} 542}
530 543
531/* usually called after fork if method needs to re-arm all fds from scratch */ 544/* usually called after fork if backend needs to re-arm all fds from scratch */
532static void 545static void noinline
533fd_rearm_all (EV_P) 546fd_rearm_all (EV_P)
534{ 547{
535 int fd; 548 int fd;
536 549
537 /* 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 */
543 } 556 }
544} 557}
545 558
546/*****************************************************************************/ 559/*****************************************************************************/
547 560
548static void 561void inline_speed
549upheap (WT *heap, int k) 562upheap (WT *heap, int k)
550{ 563{
551 WT w = heap [k]; 564 WT w = heap [k];
552 565
553 while (k && heap [k >> 1]->at > w->at) 566 while (k && heap [k >> 1]->at > w->at)
560 heap [k] = w; 573 heap [k] = w;
561 ((W)heap [k])->active = k + 1; 574 ((W)heap [k])->active = k + 1;
562 575
563} 576}
564 577
565static void 578void inline_speed
566downheap (WT *heap, int N, int k) 579downheap (WT *heap, int N, int k)
567{ 580{
568 WT w = heap [k]; 581 WT w = heap [k];
569 582
570 while (k < (N >> 1)) 583 while (k < (N >> 1))
584 597
585 heap [k] = w; 598 heap [k] = w;
586 ((W)heap [k])->active = k + 1; 599 ((W)heap [k])->active = k + 1;
587} 600}
588 601
589inline void 602void inline_size
590adjustheap (WT *heap, int N, int k) 603adjustheap (WT *heap, int N, int k)
591{ 604{
592 upheap (heap, k); 605 upheap (heap, k);
593 downheap (heap, N, k); 606 downheap (heap, N, k);
594} 607}
604static ANSIG *signals; 617static ANSIG *signals;
605static int signalmax; 618static int signalmax;
606 619
607static int sigpipe [2]; 620static int sigpipe [2];
608static sig_atomic_t volatile gotsig; 621static sig_atomic_t volatile gotsig;
609static struct ev_io sigev; 622static ev_io sigev;
610 623
611static void 624void inline_size
612signals_init (ANSIG *base, int count) 625signals_init (ANSIG *base, int count)
613{ 626{
614 while (count--) 627 while (count--)
615 { 628 {
616 base->head = 0; 629 base->head = 0;
636 write (sigpipe [1], &signum, 1); 649 write (sigpipe [1], &signum, 1);
637 errno = old_errno; 650 errno = old_errno;
638 } 651 }
639} 652}
640 653
641void 654void noinline
642ev_feed_signal_event (EV_P_ int signum) 655ev_feed_signal_event (EV_P_ int signum)
643{ 656{
644 WL w; 657 WL w;
645 658
646#if EV_MULTIPLICITY 659#if EV_MULTIPLICITY
657 for (w = signals [signum].head; w; w = w->next) 670 for (w = signals [signum].head; w; w = w->next)
658 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 671 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
659} 672}
660 673
661static void 674static void
662sigcb (EV_P_ struct ev_io *iow, int revents) 675sigcb (EV_P_ ev_io *iow, int revents)
663{ 676{
664 int signum; 677 int signum;
665 678
666 read (sigpipe [0], &revents, 1); 679 read (sigpipe [0], &revents, 1);
667 gotsig = 0; 680 gotsig = 0;
669 for (signum = signalmax; signum--; ) 682 for (signum = signalmax; signum--; )
670 if (signals [signum].gotsig) 683 if (signals [signum].gotsig)
671 ev_feed_signal_event (EV_A_ signum + 1); 684 ev_feed_signal_event (EV_A_ signum + 1);
672} 685}
673 686
674static void 687void inline_size
675fd_intern (int fd) 688fd_intern (int fd)
676{ 689{
677#ifdef _WIN32 690#ifdef _WIN32
678 int arg = 1; 691 int arg = 1;
679 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 692 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
681 fcntl (fd, F_SETFD, FD_CLOEXEC); 694 fcntl (fd, F_SETFD, FD_CLOEXEC);
682 fcntl (fd, F_SETFL, O_NONBLOCK); 695 fcntl (fd, F_SETFL, O_NONBLOCK);
683#endif 696#endif
684} 697}
685 698
686static void 699static void noinline
687siginit (EV_P) 700siginit (EV_P)
688{ 701{
689 fd_intern (sigpipe [0]); 702 fd_intern (sigpipe [0]);
690 fd_intern (sigpipe [1]); 703 fd_intern (sigpipe [1]);
691 704
694 ev_unref (EV_A); /* child watcher should not keep loop alive */ 707 ev_unref (EV_A); /* child watcher should not keep loop alive */
695} 708}
696 709
697/*****************************************************************************/ 710/*****************************************************************************/
698 711
699static struct ev_child *childs [PID_HASHSIZE]; 712static ev_child *childs [PID_HASHSIZE];
700 713
701#ifndef _WIN32 714#ifndef _WIN32
702 715
703static struct ev_signal childev; 716static ev_signal childev;
704 717
705#ifndef WCONTINUED 718#ifndef WCONTINUED
706# define WCONTINUED 0 719# define WCONTINUED 0
707#endif 720#endif
708 721
709static void 722void inline_speed
710child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 723child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
711{ 724{
712 struct ev_child *w; 725 ev_child *w;
713 726
714 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)
715 if (w->pid == pid || !w->pid) 728 if (w->pid == pid || !w->pid)
716 { 729 {
717 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 730 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
718 w->rpid = pid; 731 w->rpid = pid;
719 w->rstatus = status; 732 w->rstatus = status;
720 ev_feed_event (EV_A_ (W)w, EV_CHILD); 733 ev_feed_event (EV_A_ (W)w, EV_CHILD);
721 } 734 }
722} 735}
723 736
724static void 737static void
725childcb (EV_P_ struct ev_signal *sw, int revents) 738childcb (EV_P_ ev_signal *sw, int revents)
726{ 739{
727 int pid, status; 740 int pid, status;
728 741
729 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 742 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
730 { 743 {
731 /* make sure we are called again until all childs have been reaped */ 744 /* make sure we are called again until all childs have been reaped */
745 /* we need to do it this way so that the callback gets called before we continue */
732 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 746 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
733 747
734 child_reap (EV_A_ sw, pid, pid, status); 748 child_reap (EV_A_ sw, pid, pid, status);
735 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 749 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
736 } 750 }
737} 751}
738 752
739#endif 753#endif
740 754
767{ 781{
768 return EV_VERSION_MINOR; 782 return EV_VERSION_MINOR;
769} 783}
770 784
771/* 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 */
772static int 786int inline_size
773enable_secure (void) 787enable_secure (void)
774{ 788{
775#ifdef _WIN32 789#ifdef _WIN32
776 return 0; 790 return 0;
777#else 791#else
781} 795}
782 796
783unsigned int 797unsigned int
784ev_supported_backends (void) 798ev_supported_backends (void)
785{ 799{
786}
787
788unsigned int
789ev_recommended_backends (void)
790{
791 unsigned int flags; 800 unsigned int flags = 0;
792 801
793 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 802 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
794 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 803 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
795 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 804 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
796 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 805 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
798 807
799 return flags; 808 return flags;
800} 809}
801 810
802unsigned int 811unsigned int
803ev_backend (EV_P) 812ev_recommended_backends (void)
804{ 813{
805 unsigned int flags = ev_recommended_backends (); 814 unsigned int flags = ev_supported_backends ();
806 815
807#ifndef __NetBSD__ 816#ifndef __NetBSD__
808 /* kqueue is borked on everything but netbsd apparently */ 817 /* kqueue is borked on everything but netbsd apparently */
809 /* it usually doesn't work correctly on anything but sockets and pipes */ 818 /* it usually doesn't work correctly on anything but sockets and pipes */
810 flags &= ~EVBACKEND_KQUEUE; 819 flags &= ~EVBACKEND_KQUEUE;
815#endif 824#endif
816 825
817 return flags; 826 return flags;
818} 827}
819 828
829unsigned int
830ev_embeddable_backends (void)
831{
832 return EVBACKEND_EPOLL
833 | EVBACKEND_KQUEUE
834 | EVBACKEND_PORT;
835}
836
837unsigned int
838ev_backend (EV_P)
839{
840 return backend;
841}
842
820static void 843static void
821loop_init (EV_P_ unsigned int flags) 844loop_init (EV_P_ unsigned int flags)
822{ 845{
823 if (!method) 846 if (!backend)
824 { 847 {
825#if EV_USE_MONOTONIC 848#if EV_USE_MONOTONIC
826 { 849 {
827 struct timespec ts; 850 struct timespec ts;
828 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 851 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
841 flags = atoi (getenv ("LIBEV_FLAGS")); 864 flags = atoi (getenv ("LIBEV_FLAGS"));
842 865
843 if (!(flags & 0x0000ffffUL)) 866 if (!(flags & 0x0000ffffUL))
844 flags |= ev_recommended_backends (); 867 flags |= ev_recommended_backends ();
845 868
846 method = 0; 869 backend = 0;
847#if EV_USE_PORT 870#if EV_USE_PORT
848 if (!method && (flags & EVBACKEND_PORT )) method = port_init (EV_A_ flags); 871 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
849#endif 872#endif
850#if EV_USE_KQUEUE 873#if EV_USE_KQUEUE
851 if (!method && (flags & EVBACKEND_KQUEUE)) method = kqueue_init (EV_A_ flags); 874 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
852#endif 875#endif
853#if EV_USE_EPOLL 876#if EV_USE_EPOLL
854 if (!method && (flags & EVBACKEND_EPOLL )) method = epoll_init (EV_A_ flags); 877 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
855#endif 878#endif
856#if EV_USE_POLL 879#if EV_USE_POLL
857 if (!method && (flags & EVBACKEND_POLL )) method = poll_init (EV_A_ flags); 880 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
858#endif 881#endif
859#if EV_USE_SELECT 882#if EV_USE_SELECT
860 if (!method && (flags & EVBACKEND_SELECT)) method = select_init (EV_A_ flags); 883 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
861#endif 884#endif
862 885
863 ev_init (&sigev, sigcb); 886 ev_init (&sigev, sigcb);
864 ev_set_priority (&sigev, EV_MAXPRI); 887 ev_set_priority (&sigev, EV_MAXPRI);
865 } 888 }
869loop_destroy (EV_P) 892loop_destroy (EV_P)
870{ 893{
871 int i; 894 int i;
872 895
873#if EV_USE_PORT 896#if EV_USE_PORT
874 if (method == EVBACKEND_PORT ) port_destroy (EV_A); 897 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
875#endif 898#endif
876#if EV_USE_KQUEUE 899#if EV_USE_KQUEUE
877 if (method == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 900 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
878#endif 901#endif
879#if EV_USE_EPOLL 902#if EV_USE_EPOLL
880 if (method == EVBACKEND_EPOLL ) epoll_destroy (EV_A); 903 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
881#endif 904#endif
882#if EV_USE_POLL 905#if EV_USE_POLL
883 if (method == EVBACKEND_POLL ) poll_destroy (EV_A); 906 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
884#endif 907#endif
885#if EV_USE_SELECT 908#if EV_USE_SELECT
886 if (method == EVBACKEND_SELECT) select_destroy (EV_A); 909 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
887#endif 910#endif
888 911
889 for (i = NUMPRI; i--; ) 912 for (i = NUMPRI; i--; )
890 array_free (pending, [i]); 913 array_free (pending, [i]);
891 914
892 /* have to use the microsoft-never-gets-it-right macro */ 915 /* have to use the microsoft-never-gets-it-right macro */
893 array_free (fdchange, EMPTY0); 916 array_free (fdchange, EMPTY0);
894 array_free (timer, EMPTY0); 917 array_free (timer, EMPTY0);
895#if EV_PERIODICS 918#if EV_PERIODIC_ENABLE
896 array_free (periodic, EMPTY0); 919 array_free (periodic, EMPTY0);
897#endif 920#endif
898 array_free (idle, EMPTY0); 921 array_free (idle, EMPTY0);
899 array_free (prepare, EMPTY0); 922 array_free (prepare, EMPTY0);
900 array_free (check, EMPTY0); 923 array_free (check, EMPTY0);
901 924
902 method = 0; 925 backend = 0;
903} 926}
904 927
905static void 928static void
906loop_fork (EV_P) 929loop_fork (EV_P)
907{ 930{
908#if EV_USE_PORT 931#if EV_USE_PORT
909 if (method == EVBACKEND_PORT ) port_fork (EV_A); 932 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
910#endif 933#endif
911#if EV_USE_KQUEUE 934#if EV_USE_KQUEUE
912 if (method == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 935 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
913#endif 936#endif
914#if EV_USE_EPOLL 937#if EV_USE_EPOLL
915 if (method == EVBACKEND_EPOLL ) epoll_fork (EV_A); 938 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
916#endif 939#endif
917 940
918 if (ev_is_active (&sigev)) 941 if (ev_is_active (&sigev))
919 { 942 {
920 /* default loop */ 943 /* default loop */
941 964
942 memset (loop, 0, sizeof (struct ev_loop)); 965 memset (loop, 0, sizeof (struct ev_loop));
943 966
944 loop_init (EV_A_ flags); 967 loop_init (EV_A_ flags);
945 968
946 if (ev_method (EV_A)) 969 if (ev_backend (EV_A))
947 return loop; 970 return loop;
948 971
949 return 0; 972 return 0;
950} 973}
951 974
984 ev_default_loop_ptr = 1; 1007 ev_default_loop_ptr = 1;
985#endif 1008#endif
986 1009
987 loop_init (EV_A_ flags); 1010 loop_init (EV_A_ flags);
988 1011
989 if (ev_method (EV_A)) 1012 if (ev_backend (EV_A))
990 { 1013 {
991 siginit (EV_A); 1014 siginit (EV_A);
992 1015
993#ifndef _WIN32 1016#ifndef _WIN32
994 ev_signal_init (&childev, childcb, SIGCHLD); 1017 ev_signal_init (&childev, childcb, SIGCHLD);
1030{ 1053{
1031#if EV_MULTIPLICITY 1054#if EV_MULTIPLICITY
1032 struct ev_loop *loop = ev_default_loop_ptr; 1055 struct ev_loop *loop = ev_default_loop_ptr;
1033#endif 1056#endif
1034 1057
1035 if (method) 1058 if (backend)
1036 postfork = 1; 1059 postfork = 1;
1037} 1060}
1038 1061
1039/*****************************************************************************/ 1062/*****************************************************************************/
1040 1063
1041static int 1064int inline_size
1042any_pending (EV_P) 1065any_pending (EV_P)
1043{ 1066{
1044 int pri; 1067 int pri;
1045 1068
1046 for (pri = NUMPRI; pri--; ) 1069 for (pri = NUMPRI; pri--; )
1048 return 1; 1071 return 1;
1049 1072
1050 return 0; 1073 return 0;
1051} 1074}
1052 1075
1053inline void 1076void inline_speed
1054call_pending (EV_P) 1077call_pending (EV_P)
1055{ 1078{
1056 int pri; 1079 int pri;
1057 1080
1058 for (pri = NUMPRI; pri--; ) 1081 for (pri = NUMPRI; pri--; )
1060 { 1083 {
1061 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1084 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1062 1085
1063 if (expect_true (p->w)) 1086 if (expect_true (p->w))
1064 { 1087 {
1088 assert (("non-pending watcher on pending list", p->w->pending));
1089
1065 p->w->pending = 0; 1090 p->w->pending = 0;
1066 EV_CB_INVOKE (p->w, p->events); 1091 EV_CB_INVOKE (p->w, p->events);
1067 } 1092 }
1068 } 1093 }
1069} 1094}
1070 1095
1071inline void 1096void inline_size
1072timers_reify (EV_P) 1097timers_reify (EV_P)
1073{ 1098{
1074 while (timercnt && ((WT)timers [0])->at <= mn_now) 1099 while (timercnt && ((WT)timers [0])->at <= mn_now)
1075 { 1100 {
1076 struct ev_timer *w = timers [0]; 1101 ev_timer *w = timers [0];
1077 1102
1078 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1103 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1079 1104
1080 /* first reschedule or stop timer */ 1105 /* first reschedule or stop timer */
1081 if (w->repeat) 1106 if (w->repeat)
1093 1118
1094 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1119 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1095 } 1120 }
1096} 1121}
1097 1122
1098#if EV_PERIODICS 1123#if EV_PERIODIC_ENABLE
1099inline void 1124void inline_size
1100periodics_reify (EV_P) 1125periodics_reify (EV_P)
1101{ 1126{
1102 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1127 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1103 { 1128 {
1104 struct ev_periodic *w = periodics [0]; 1129 ev_periodic *w = periodics [0];
1105 1130
1106 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1131 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1107 1132
1108 /* first reschedule or stop timer */ 1133 /* first reschedule or stop timer */
1109 if (w->reschedule_cb) 1134 if (w->reschedule_cb)
1123 1148
1124 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1149 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1125 } 1150 }
1126} 1151}
1127 1152
1128static void 1153static void noinline
1129periodics_reschedule (EV_P) 1154periodics_reschedule (EV_P)
1130{ 1155{
1131 int i; 1156 int i;
1132 1157
1133 /* adjust periodics after time jump */ 1158 /* adjust periodics after time jump */
1134 for (i = 0; i < periodiccnt; ++i) 1159 for (i = 0; i < periodiccnt; ++i)
1135 { 1160 {
1136 struct ev_periodic *w = periodics [i]; 1161 ev_periodic *w = periodics [i];
1137 1162
1138 if (w->reschedule_cb) 1163 if (w->reschedule_cb)
1139 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1164 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1140 else if (w->interval) 1165 else if (w->interval)
1141 ((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;
1145 for (i = periodiccnt >> 1; i--; ) 1170 for (i = periodiccnt >> 1; i--; )
1146 downheap ((WT *)periodics, periodiccnt, i); 1171 downheap ((WT *)periodics, periodiccnt, i);
1147} 1172}
1148#endif 1173#endif
1149 1174
1150inline int 1175int inline_size
1151time_update_monotonic (EV_P) 1176time_update_monotonic (EV_P)
1152{ 1177{
1153 mn_now = get_clock (); 1178 mn_now = get_clock ();
1154 1179
1155 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1180 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1163 ev_rt_now = ev_time (); 1188 ev_rt_now = ev_time ();
1164 return 1; 1189 return 1;
1165 } 1190 }
1166} 1191}
1167 1192
1168inline void 1193void inline_size
1169time_update (EV_P) 1194time_update (EV_P)
1170{ 1195{
1171 int i; 1196 int i;
1172 1197
1173#if EV_USE_MONOTONIC 1198#if EV_USE_MONOTONIC
1175 { 1200 {
1176 if (time_update_monotonic (EV_A)) 1201 if (time_update_monotonic (EV_A))
1177 { 1202 {
1178 ev_tstamp odiff = rtmn_diff; 1203 ev_tstamp odiff = rtmn_diff;
1179 1204
1180 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; )
1181 { 1214 {
1182 rtmn_diff = ev_rt_now - mn_now; 1215 rtmn_diff = ev_rt_now - mn_now;
1183 1216
1184 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1217 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1185 return; /* all is well */ 1218 return; /* all is well */
1187 ev_rt_now = ev_time (); 1220 ev_rt_now = ev_time ();
1188 mn_now = get_clock (); 1221 mn_now = get_clock ();
1189 now_floor = mn_now; 1222 now_floor = mn_now;
1190 } 1223 }
1191 1224
1192# if EV_PERIODICS 1225# if EV_PERIODIC_ENABLE
1193 periodics_reschedule (EV_A); 1226 periodics_reschedule (EV_A);
1194# endif 1227# endif
1195 /* no timer adjustment, as the monotonic clock doesn't jump */ 1228 /* no timer adjustment, as the monotonic clock doesn't jump */
1196 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1229 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1197 } 1230 }
1201 { 1234 {
1202 ev_rt_now = ev_time (); 1235 ev_rt_now = ev_time ();
1203 1236
1204 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))
1205 { 1238 {
1206#if EV_PERIODICS 1239#if EV_PERIODIC_ENABLE
1207 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1208#endif 1241#endif
1209 1242
1210 /* 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 */
1211 for (i = 0; i < timercnt; ++i) 1244 for (i = 0; i < timercnt; ++i)
1231static int loop_done; 1264static int loop_done;
1232 1265
1233void 1266void
1234ev_loop (EV_P_ int flags) 1267ev_loop (EV_P_ int flags)
1235{ 1268{
1236 double block;
1237 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1269 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1270 ? EVUNLOOP_ONE
1271 : EVUNLOOP_CANCEL;
1238 1272
1239 while (activecnt) 1273 while (activecnt)
1240 { 1274 {
1241 /* queue check watchers (and execute them) */ 1275 /* queue check watchers (and execute them) */
1242 if (expect_false (preparecnt)) 1276 if (expect_false (preparecnt))
1251 1285
1252 /* update fd-related kernel structures */ 1286 /* update fd-related kernel structures */
1253 fd_reify (EV_A); 1287 fd_reify (EV_A);
1254 1288
1255 /* calculate blocking time */ 1289 /* calculate blocking time */
1290 {
1291 double block;
1256 1292
1257 /* we only need this for !monotonic clock or timers, but as we basically 1293 if (flags & EVLOOP_NONBLOCK || idlecnt)
1258 always have timers, we just calculate it always */ 1294 block = 0.; /* do not block at all */
1295 else
1296 {
1297 /* update time to cancel out callback processing overhead */
1259#if EV_USE_MONOTONIC 1298#if EV_USE_MONOTONIC
1260 if (expect_true (have_monotonic)) 1299 if (expect_true (have_monotonic))
1261 time_update_monotonic (EV_A); 1300 time_update_monotonic (EV_A);
1262 else 1301 else
1263#endif 1302#endif
1264 { 1303 {
1265 ev_rt_now = ev_time (); 1304 ev_rt_now = ev_time ();
1266 mn_now = ev_rt_now; 1305 mn_now = ev_rt_now;
1267 } 1306 }
1268 1307
1269 if (flags & EVLOOP_NONBLOCK || idlecnt)
1270 block = 0.;
1271 else
1272 {
1273 block = MAX_BLOCKTIME; 1308 block = MAX_BLOCKTIME;
1274 1309
1275 if (timercnt) 1310 if (timercnt)
1276 { 1311 {
1277 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1312 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1278 if (block > to) block = to; 1313 if (block > to) block = to;
1279 } 1314 }
1280 1315
1281#if EV_PERIODICS 1316#if EV_PERIODIC_ENABLE
1282 if (periodiccnt) 1317 if (periodiccnt)
1283 { 1318 {
1284 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1319 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1285 if (block > to) block = to; 1320 if (block > to) block = to;
1286 } 1321 }
1287#endif 1322#endif
1288 1323
1289 if (expect_false (block < 0.)) block = 0.; 1324 if (expect_false (block < 0.)) block = 0.;
1290 } 1325 }
1291 1326
1292 method_poll (EV_A_ block); 1327 backend_poll (EV_A_ block);
1328 }
1293 1329
1294 /* update ev_rt_now, do magic */ 1330 /* update ev_rt_now, do magic */
1295 time_update (EV_A); 1331 time_update (EV_A);
1296 1332
1297 /* queue pending timers and reschedule them */ 1333 /* queue pending timers and reschedule them */
1298 timers_reify (EV_A); /* relative timers called last */ 1334 timers_reify (EV_A); /* relative timers called last */
1299#if EV_PERIODICS 1335#if EV_PERIODIC_ENABLE
1300 periodics_reify (EV_A); /* absolute timers called first */ 1336 periodics_reify (EV_A); /* absolute timers called first */
1301#endif 1337#endif
1302 1338
1303 /* queue idle watchers unless io or timers are pending */ 1339 /* queue idle watchers unless other events are pending */
1304 if (idlecnt && !any_pending (EV_A)) 1340 if (idlecnt && !any_pending (EV_A))
1305 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1341 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1306 1342
1307 /* queue check watchers, to be executed first */ 1343 /* queue check watchers, to be executed first */
1308 if (expect_false (checkcnt)) 1344 if (expect_false (checkcnt))
1312 1348
1313 if (expect_false (loop_done)) 1349 if (expect_false (loop_done))
1314 break; 1350 break;
1315 } 1351 }
1316 1352
1317 if (loop_done != 2) 1353 if (loop_done == EVUNLOOP_ONE)
1318 loop_done = 0; 1354 loop_done = EVUNLOOP_CANCEL;
1319} 1355}
1320 1356
1321void 1357void
1322ev_unloop (EV_P_ int how) 1358ev_unloop (EV_P_ int how)
1323{ 1359{
1324 loop_done = how; 1360 loop_done = how;
1325} 1361}
1326 1362
1327/*****************************************************************************/ 1363/*****************************************************************************/
1328 1364
1329inline void 1365void inline_size
1330wlist_add (WL *head, WL elem) 1366wlist_add (WL *head, WL elem)
1331{ 1367{
1332 elem->next = *head; 1368 elem->next = *head;
1333 *head = elem; 1369 *head = elem;
1334} 1370}
1335 1371
1336inline void 1372void inline_size
1337wlist_del (WL *head, WL elem) 1373wlist_del (WL *head, WL elem)
1338{ 1374{
1339 while (*head) 1375 while (*head)
1340 { 1376 {
1341 if (*head == elem) 1377 if (*head == elem)
1346 1382
1347 head = &(*head)->next; 1383 head = &(*head)->next;
1348 } 1384 }
1349} 1385}
1350 1386
1351inline void 1387void inline_speed
1352ev_clear_pending (EV_P_ W w) 1388ev_clear_pending (EV_P_ W w)
1353{ 1389{
1354 if (w->pending) 1390 if (w->pending)
1355 { 1391 {
1356 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1392 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1357 w->pending = 0; 1393 w->pending = 0;
1358 } 1394 }
1359} 1395}
1360 1396
1361inline void 1397void inline_speed
1362ev_start (EV_P_ W w, int active) 1398ev_start (EV_P_ W w, int active)
1363{ 1399{
1364 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1400 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1365 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1401 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1366 1402
1367 w->active = active; 1403 w->active = active;
1368 ev_ref (EV_A); 1404 ev_ref (EV_A);
1369} 1405}
1370 1406
1371inline void 1407void inline_size
1372ev_stop (EV_P_ W w) 1408ev_stop (EV_P_ W w)
1373{ 1409{
1374 ev_unref (EV_A); 1410 ev_unref (EV_A);
1375 w->active = 0; 1411 w->active = 0;
1376} 1412}
1377 1413
1378/*****************************************************************************/ 1414/*****************************************************************************/
1379 1415
1380void 1416void
1381ev_io_start (EV_P_ struct ev_io *w) 1417ev_io_start (EV_P_ ev_io *w)
1382{ 1418{
1383 int fd = w->fd; 1419 int fd = w->fd;
1384 1420
1385 if (expect_false (ev_is_active (w))) 1421 if (expect_false (ev_is_active (w)))
1386 return; 1422 return;
1393 1429
1394 fd_change (EV_A_ fd); 1430 fd_change (EV_A_ fd);
1395} 1431}
1396 1432
1397void 1433void
1398ev_io_stop (EV_P_ struct ev_io *w) 1434ev_io_stop (EV_P_ ev_io *w)
1399{ 1435{
1400 ev_clear_pending (EV_A_ (W)w); 1436 ev_clear_pending (EV_A_ (W)w);
1401 if (expect_false (!ev_is_active (w))) 1437 if (expect_false (!ev_is_active (w)))
1402 return; 1438 return;
1403 1439
1408 1444
1409 fd_change (EV_A_ w->fd); 1445 fd_change (EV_A_ w->fd);
1410} 1446}
1411 1447
1412void 1448void
1413ev_timer_start (EV_P_ struct ev_timer *w) 1449ev_timer_start (EV_P_ ev_timer *w)
1414{ 1450{
1415 if (expect_false (ev_is_active (w))) 1451 if (expect_false (ev_is_active (w)))
1416 return; 1452 return;
1417 1453
1418 ((WT)w)->at += mn_now; 1454 ((WT)w)->at += mn_now;
1419 1455
1420 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.));
1421 1457
1422 ev_start (EV_A_ (W)w, ++timercnt); 1458 ev_start (EV_A_ (W)w, ++timercnt);
1423 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1459 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1424 timers [timercnt - 1] = w; 1460 timers [timercnt - 1] = w;
1425 upheap ((WT *)timers, timercnt - 1); 1461 upheap ((WT *)timers, timercnt - 1);
1426 1462
1427 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1463 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1428} 1464}
1429 1465
1430void 1466void
1431ev_timer_stop (EV_P_ struct ev_timer *w) 1467ev_timer_stop (EV_P_ ev_timer *w)
1432{ 1468{
1433 ev_clear_pending (EV_A_ (W)w); 1469 ev_clear_pending (EV_A_ (W)w);
1434 if (expect_false (!ev_is_active (w))) 1470 if (expect_false (!ev_is_active (w)))
1435 return; 1471 return;
1436 1472
1446 1482
1447 ev_stop (EV_A_ (W)w); 1483 ev_stop (EV_A_ (W)w);
1448} 1484}
1449 1485
1450void 1486void
1451ev_timer_again (EV_P_ struct ev_timer *w) 1487ev_timer_again (EV_P_ ev_timer *w)
1452{ 1488{
1453 if (ev_is_active (w)) 1489 if (ev_is_active (w))
1454 { 1490 {
1455 if (w->repeat) 1491 if (w->repeat)
1456 { 1492 {
1465 w->at = w->repeat; 1501 w->at = w->repeat;
1466 ev_timer_start (EV_A_ w); 1502 ev_timer_start (EV_A_ w);
1467 } 1503 }
1468} 1504}
1469 1505
1470#if EV_PERIODICS 1506#if EV_PERIODIC_ENABLE
1471void 1507void
1472ev_periodic_start (EV_P_ struct ev_periodic *w) 1508ev_periodic_start (EV_P_ ev_periodic *w)
1473{ 1509{
1474 if (expect_false (ev_is_active (w))) 1510 if (expect_false (ev_is_active (w)))
1475 return; 1511 return;
1476 1512
1477 if (w->reschedule_cb) 1513 if (w->reschedule_cb)
1482 /* 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 */
1483 ((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;
1484 } 1520 }
1485 1521
1486 ev_start (EV_A_ (W)w, ++periodiccnt); 1522 ev_start (EV_A_ (W)w, ++periodiccnt);
1487 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1523 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1488 periodics [periodiccnt - 1] = w; 1524 periodics [periodiccnt - 1] = w;
1489 upheap ((WT *)periodics, periodiccnt - 1); 1525 upheap ((WT *)periodics, periodiccnt - 1);
1490 1526
1491 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1527 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1492} 1528}
1493 1529
1494void 1530void
1495ev_periodic_stop (EV_P_ struct ev_periodic *w) 1531ev_periodic_stop (EV_P_ ev_periodic *w)
1496{ 1532{
1497 ev_clear_pending (EV_A_ (W)w); 1533 ev_clear_pending (EV_A_ (W)w);
1498 if (expect_false (!ev_is_active (w))) 1534 if (expect_false (!ev_is_active (w)))
1499 return; 1535 return;
1500 1536
1508 1544
1509 ev_stop (EV_A_ (W)w); 1545 ev_stop (EV_A_ (W)w);
1510} 1546}
1511 1547
1512void 1548void
1513ev_periodic_again (EV_P_ struct ev_periodic *w) 1549ev_periodic_again (EV_P_ ev_periodic *w)
1514{ 1550{
1515 /* TODO: use adjustheap and recalculation */ 1551 /* TODO: use adjustheap and recalculation */
1516 ev_periodic_stop (EV_A_ w); 1552 ev_periodic_stop (EV_A_ w);
1517 ev_periodic_start (EV_A_ w); 1553 ev_periodic_start (EV_A_ w);
1518} 1554}
1519#endif 1555#endif
1520 1556
1521void 1557void
1522ev_idle_start (EV_P_ struct ev_idle *w) 1558ev_idle_start (EV_P_ ev_idle *w)
1523{ 1559{
1524 if (expect_false (ev_is_active (w))) 1560 if (expect_false (ev_is_active (w)))
1525 return; 1561 return;
1526 1562
1527 ev_start (EV_A_ (W)w, ++idlecnt); 1563 ev_start (EV_A_ (W)w, ++idlecnt);
1528 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1564 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1529 idles [idlecnt - 1] = w; 1565 idles [idlecnt - 1] = w;
1530} 1566}
1531 1567
1532void 1568void
1533ev_idle_stop (EV_P_ struct ev_idle *w) 1569ev_idle_stop (EV_P_ ev_idle *w)
1534{ 1570{
1535 ev_clear_pending (EV_A_ (W)w); 1571 ev_clear_pending (EV_A_ (W)w);
1536 if (expect_false (!ev_is_active (w))) 1572 if (expect_false (!ev_is_active (w)))
1537 return; 1573 return;
1538 1574
1575 {
1576 int active = ((W)w)->active;
1539 idles [((W)w)->active - 1] = idles [--idlecnt]; 1577 idles [active - 1] = idles [--idlecnt];
1578 ((W)idles [active - 1])->active = active;
1579 }
1580
1540 ev_stop (EV_A_ (W)w); 1581 ev_stop (EV_A_ (W)w);
1541} 1582}
1542 1583
1543void 1584void
1544ev_prepare_start (EV_P_ struct ev_prepare *w) 1585ev_prepare_start (EV_P_ ev_prepare *w)
1545{ 1586{
1546 if (expect_false (ev_is_active (w))) 1587 if (expect_false (ev_is_active (w)))
1547 return; 1588 return;
1548 1589
1549 ev_start (EV_A_ (W)w, ++preparecnt); 1590 ev_start (EV_A_ (W)w, ++preparecnt);
1550 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1591 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1551 prepares [preparecnt - 1] = w; 1592 prepares [preparecnt - 1] = w;
1552} 1593}
1553 1594
1554void 1595void
1555ev_prepare_stop (EV_P_ struct ev_prepare *w) 1596ev_prepare_stop (EV_P_ ev_prepare *w)
1556{ 1597{
1557 ev_clear_pending (EV_A_ (W)w); 1598 ev_clear_pending (EV_A_ (W)w);
1558 if (expect_false (!ev_is_active (w))) 1599 if (expect_false (!ev_is_active (w)))
1559 return; 1600 return;
1560 1601
1602 {
1603 int active = ((W)w)->active;
1561 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1604 prepares [active - 1] = prepares [--preparecnt];
1605 ((W)prepares [active - 1])->active = active;
1606 }
1607
1562 ev_stop (EV_A_ (W)w); 1608 ev_stop (EV_A_ (W)w);
1563} 1609}
1564 1610
1565void 1611void
1566ev_check_start (EV_P_ struct ev_check *w) 1612ev_check_start (EV_P_ ev_check *w)
1567{ 1613{
1568 if (expect_false (ev_is_active (w))) 1614 if (expect_false (ev_is_active (w)))
1569 return; 1615 return;
1570 1616
1571 ev_start (EV_A_ (W)w, ++checkcnt); 1617 ev_start (EV_A_ (W)w, ++checkcnt);
1572 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1618 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1573 checks [checkcnt - 1] = w; 1619 checks [checkcnt - 1] = w;
1574} 1620}
1575 1621
1576void 1622void
1577ev_check_stop (EV_P_ struct ev_check *w) 1623ev_check_stop (EV_P_ ev_check *w)
1578{ 1624{
1579 ev_clear_pending (EV_A_ (W)w); 1625 ev_clear_pending (EV_A_ (W)w);
1580 if (expect_false (!ev_is_active (w))) 1626 if (expect_false (!ev_is_active (w)))
1581 return; 1627 return;
1582 1628
1629 {
1630 int active = ((W)w)->active;
1583 checks [((W)w)->active - 1] = checks [--checkcnt]; 1631 checks [active - 1] = checks [--checkcnt];
1632 ((W)checks [active - 1])->active = active;
1633 }
1634
1584 ev_stop (EV_A_ (W)w); 1635 ev_stop (EV_A_ (W)w);
1585} 1636}
1586 1637
1587#ifndef SA_RESTART 1638#ifndef SA_RESTART
1588# define SA_RESTART 0 1639# define SA_RESTART 0
1589#endif 1640#endif
1590 1641
1591void 1642void
1592ev_signal_start (EV_P_ struct ev_signal *w) 1643ev_signal_start (EV_P_ ev_signal *w)
1593{ 1644{
1594#if EV_MULTIPLICITY 1645#if EV_MULTIPLICITY
1595 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));
1596#endif 1647#endif
1597 if (expect_false (ev_is_active (w))) 1648 if (expect_false (ev_is_active (w)))
1616#endif 1667#endif
1617 } 1668 }
1618} 1669}
1619 1670
1620void 1671void
1621ev_signal_stop (EV_P_ struct ev_signal *w) 1672ev_signal_stop (EV_P_ ev_signal *w)
1622{ 1673{
1623 ev_clear_pending (EV_A_ (W)w); 1674 ev_clear_pending (EV_A_ (W)w);
1624 if (expect_false (!ev_is_active (w))) 1675 if (expect_false (!ev_is_active (w)))
1625 return; 1676 return;
1626 1677
1630 if (!signals [w->signum - 1].head) 1681 if (!signals [w->signum - 1].head)
1631 signal (w->signum, SIG_DFL); 1682 signal (w->signum, SIG_DFL);
1632} 1683}
1633 1684
1634void 1685void
1635ev_child_start (EV_P_ struct ev_child *w) 1686ev_child_start (EV_P_ ev_child *w)
1636{ 1687{
1637#if EV_MULTIPLICITY 1688#if EV_MULTIPLICITY
1638 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));
1639#endif 1690#endif
1640 if (expect_false (ev_is_active (w))) 1691 if (expect_false (ev_is_active (w)))
1643 ev_start (EV_A_ (W)w, 1); 1694 ev_start (EV_A_ (W)w, 1);
1644 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1695 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1645} 1696}
1646 1697
1647void 1698void
1648ev_child_stop (EV_P_ struct ev_child *w) 1699ev_child_stop (EV_P_ ev_child *w)
1649{ 1700{
1650 ev_clear_pending (EV_A_ (W)w); 1701 ev_clear_pending (EV_A_ (W)w);
1651 if (expect_false (!ev_is_active (w))) 1702 if (expect_false (!ev_is_active (w)))
1652 return; 1703 return;
1653 1704
1654 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1705 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1655 ev_stop (EV_A_ (W)w); 1706 ev_stop (EV_A_ (W)w);
1656} 1707}
1657 1708
1709#if EV_EMBED_ENABLE
1710void noinline
1711ev_embed_sweep (EV_P_ ev_embed *w)
1712{
1713 ev_loop (w->loop, EVLOOP_NONBLOCK);
1714}
1715
1716static void
1717embed_cb (EV_P_ ev_io *io, int revents)
1718{
1719 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1720
1721 if (ev_cb (w))
1722 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1723 else
1724 ev_embed_sweep (loop, w);
1725}
1726
1727void
1728ev_embed_start (EV_P_ ev_embed *w)
1729{
1730 if (expect_false (ev_is_active (w)))
1731 return;
1732
1733 {
1734 struct ev_loop *loop = w->loop;
1735 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1736 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1737 }
1738
1739 ev_set_priority (&w->io, ev_priority (w));
1740 ev_io_start (EV_A_ &w->io);
1741
1742 ev_start (EV_A_ (W)w, 1);
1743}
1744
1745void
1746ev_embed_stop (EV_P_ ev_embed *w)
1747{
1748 ev_clear_pending (EV_A_ (W)w);
1749 if (expect_false (!ev_is_active (w)))
1750 return;
1751
1752 ev_io_stop (EV_A_ &w->io);
1753
1754 ev_stop (EV_A_ (W)w);
1755}
1756#endif
1757
1758#if EV_STAT_ENABLE
1759
1760# ifdef _WIN32
1761# define lstat(a,b) stat(a,b)
1762# endif
1763
1764void
1765ev_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
1773static void
1774stat_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
1787void
1788ev_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
1806void
1807ev_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
1658/*****************************************************************************/ 1819/*****************************************************************************/
1659 1820
1660struct ev_once 1821struct ev_once
1661{ 1822{
1662 struct ev_io io; 1823 ev_io io;
1663 struct ev_timer to; 1824 ev_timer to;
1664 void (*cb)(int revents, void *arg); 1825 void (*cb)(int revents, void *arg);
1665 void *arg; 1826 void *arg;
1666}; 1827};
1667 1828
1668static void 1829static void
1677 1838
1678 cb (revents, arg); 1839 cb (revents, arg);
1679} 1840}
1680 1841
1681static void 1842static void
1682once_cb_io (EV_P_ struct ev_io *w, int revents) 1843once_cb_io (EV_P_ ev_io *w, int revents)
1683{ 1844{
1684 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);
1685} 1846}
1686 1847
1687static void 1848static void
1688once_cb_to (EV_P_ struct ev_timer *w, int revents) 1849once_cb_to (EV_P_ ev_timer *w, int revents)
1689{ 1850{
1690 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);
1691} 1852}
1692 1853
1693void 1854void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines