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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines