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.142 by root, Tue Nov 27 06:19:08 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"
212 225
213/*****************************************************************************/ 226/*****************************************************************************/
214 227
215static void (*syserr_cb)(const char *msg); 228static void (*syserr_cb)(const char *msg);
216 229
230void
217void ev_set_syserr_cb (void (*cb)(const char *msg)) 231ev_set_syserr_cb (void (*cb)(const char *msg))
218{ 232{
219 syserr_cb = cb; 233 syserr_cb = cb;
220} 234}
221 235
222static void 236static void noinline
223syserr (const char *msg) 237syserr (const char *msg)
224{ 238{
225 if (!msg) 239 if (!msg)
226 msg = "(libev) system error"; 240 msg = "(libev) system error";
227 241
234 } 248 }
235} 249}
236 250
237static void *(*alloc)(void *ptr, long size); 251static void *(*alloc)(void *ptr, long size);
238 252
253void
239void ev_set_allocator (void *(*cb)(void *ptr, long size)) 254ev_set_allocator (void *(*cb)(void *ptr, long size))
240{ 255{
241 alloc = cb; 256 alloc = cb;
242} 257}
243 258
244static void * 259static void *
316 gettimeofday (&tv, 0); 331 gettimeofday (&tv, 0);
317 return tv.tv_sec + tv.tv_usec * 1e-6; 332 return tv.tv_sec + tv.tv_usec * 1e-6;
318#endif 333#endif
319} 334}
320 335
321inline ev_tstamp 336ev_tstamp inline_size
322get_clock (void) 337get_clock (void)
323{ 338{
324#if EV_USE_MONOTONIC 339#if EV_USE_MONOTONIC
325 if (expect_true (have_monotonic)) 340 if (expect_true (have_monotonic))
326 { 341 {
369#define array_free(stem, idx) \ 384#define array_free(stem, idx) \
370 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 385 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
371 386
372/*****************************************************************************/ 387/*****************************************************************************/
373 388
374static void 389void noinline
375anfds_init (ANFD *base, int count)
376{
377 while (count--)
378 {
379 base->head = 0;
380 base->events = EV_NONE;
381 base->reify = 0;
382
383 ++base;
384 }
385}
386
387void
388ev_feed_event (EV_P_ void *w, int revents) 390ev_feed_event (EV_P_ void *w, int revents)
389{ 391{
390 W w_ = (W)w; 392 W w_ = (W)w;
391 393
392 if (expect_false (w_->pending)) 394 if (expect_false (w_->pending))
399 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); 401 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
400 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 402 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
401 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 403 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
402} 404}
403 405
404static void 406void inline_size
405queue_events (EV_P_ W *events, int eventcnt, int type) 407queue_events (EV_P_ W *events, int eventcnt, int type)
406{ 408{
407 int i; 409 int i;
408 410
409 for (i = 0; i < eventcnt; ++i) 411 for (i = 0; i < eventcnt; ++i)
410 ev_feed_event (EV_A_ events [i], type); 412 ev_feed_event (EV_A_ events [i], type);
411} 413}
412 414
413inline void 415/*****************************************************************************/
416
417void inline_size
418anfds_init (ANFD *base, int count)
419{
420 while (count--)
421 {
422 base->head = 0;
423 base->events = EV_NONE;
424 base->reify = 0;
425
426 ++base;
427 }
428}
429
430void inline_speed
414fd_event (EV_P_ int fd, int revents) 431fd_event (EV_P_ int fd, int revents)
415{ 432{
416 ANFD *anfd = anfds + fd; 433 ANFD *anfd = anfds + fd;
417 struct ev_io *w; 434 ev_io *w;
418 435
419 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 436 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
420 { 437 {
421 int ev = w->events & revents; 438 int ev = w->events & revents;
422 439
423 if (ev) 440 if (ev)
424 ev_feed_event (EV_A_ (W)w, ev); 441 ev_feed_event (EV_A_ (W)w, ev);
429ev_feed_fd_event (EV_P_ int fd, int revents) 446ev_feed_fd_event (EV_P_ int fd, int revents)
430{ 447{
431 fd_event (EV_A_ fd, revents); 448 fd_event (EV_A_ fd, revents);
432} 449}
433 450
434/*****************************************************************************/ 451void inline_size
435
436inline void
437fd_reify (EV_P) 452fd_reify (EV_P)
438{ 453{
439 int i; 454 int i;
440 455
441 for (i = 0; i < fdchangecnt; ++i) 456 for (i = 0; i < fdchangecnt; ++i)
442 { 457 {
443 int fd = fdchanges [i]; 458 int fd = fdchanges [i];
444 ANFD *anfd = anfds + fd; 459 ANFD *anfd = anfds + fd;
445 struct ev_io *w; 460 ev_io *w;
446 461
447 int events = 0; 462 int events = 0;
448 463
449 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 464 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
450 events |= w->events; 465 events |= w->events;
451 466
452#if EV_SELECT_IS_WINSOCKET 467#if EV_SELECT_IS_WINSOCKET
453 if (events) 468 if (events)
454 { 469 {
458 } 473 }
459#endif 474#endif
460 475
461 anfd->reify = 0; 476 anfd->reify = 0;
462 477
463 method_modify (EV_A_ fd, anfd->events, events); 478 backend_modify (EV_A_ fd, anfd->events, events);
464 anfd->events = events; 479 anfd->events = events;
465 } 480 }
466 481
467 fdchangecnt = 0; 482 fdchangecnt = 0;
468} 483}
469 484
470static void 485void inline_size
471fd_change (EV_P_ int fd) 486fd_change (EV_P_ int fd)
472{ 487{
473 if (expect_false (anfds [fd].reify)) 488 if (expect_false (anfds [fd].reify))
474 return; 489 return;
475 490
478 ++fdchangecnt; 493 ++fdchangecnt;
479 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 494 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
480 fdchanges [fdchangecnt - 1] = fd; 495 fdchanges [fdchangecnt - 1] = fd;
481} 496}
482 497
483static void 498void inline_speed
484fd_kill (EV_P_ int fd) 499fd_kill (EV_P_ int fd)
485{ 500{
486 struct ev_io *w; 501 ev_io *w;
487 502
488 while ((w = (struct ev_io *)anfds [fd].head)) 503 while ((w = (ev_io *)anfds [fd].head))
489 { 504 {
490 ev_io_stop (EV_A_ w); 505 ev_io_stop (EV_A_ w);
491 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 506 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
492 } 507 }
493} 508}
494 509
495inline int 510int inline_size
496fd_valid (int fd) 511fd_valid (int fd)
497{ 512{
498#ifdef _WIN32 513#ifdef _WIN32
499 return _get_osfhandle (fd) != -1; 514 return _get_osfhandle (fd) != -1;
500#else 515#else
501 return fcntl (fd, F_GETFD) != -1; 516 return fcntl (fd, F_GETFD) != -1;
502#endif 517#endif
503} 518}
504 519
505/* called on EBADF to verify fds */ 520/* called on EBADF to verify fds */
506static void 521static void noinline
507fd_ebadf (EV_P) 522fd_ebadf (EV_P)
508{ 523{
509 int fd; 524 int fd;
510 525
511 for (fd = 0; fd < anfdmax; ++fd) 526 for (fd = 0; fd < anfdmax; ++fd)
513 if (!fd_valid (fd) == -1 && errno == EBADF) 528 if (!fd_valid (fd) == -1 && errno == EBADF)
514 fd_kill (EV_A_ fd); 529 fd_kill (EV_A_ fd);
515} 530}
516 531
517/* called on ENOMEM in select/poll to kill some fds and retry */ 532/* called on ENOMEM in select/poll to kill some fds and retry */
518static void 533static void noinline
519fd_enomem (EV_P) 534fd_enomem (EV_P)
520{ 535{
521 int fd; 536 int fd;
522 537
523 for (fd = anfdmax; fd--; ) 538 for (fd = anfdmax; fd--; )
526 fd_kill (EV_A_ fd); 541 fd_kill (EV_A_ fd);
527 return; 542 return;
528 } 543 }
529} 544}
530 545
531/* usually called after fork if method needs to re-arm all fds from scratch */ 546/* usually called after fork if backend needs to re-arm all fds from scratch */
532static void 547static void noinline
533fd_rearm_all (EV_P) 548fd_rearm_all (EV_P)
534{ 549{
535 int fd; 550 int fd;
536 551
537 /* this should be highly optimised to not do anything but set a flag */ 552 /* this should be highly optimised to not do anything but set a flag */
543 } 558 }
544} 559}
545 560
546/*****************************************************************************/ 561/*****************************************************************************/
547 562
548static void 563void inline_speed
549upheap (WT *heap, int k) 564upheap (WT *heap, int k)
550{ 565{
551 WT w = heap [k]; 566 WT w = heap [k];
552 567
553 while (k && heap [k >> 1]->at > w->at) 568 while (k && heap [k >> 1]->at > w->at)
560 heap [k] = w; 575 heap [k] = w;
561 ((W)heap [k])->active = k + 1; 576 ((W)heap [k])->active = k + 1;
562 577
563} 578}
564 579
565static void 580void inline_speed
566downheap (WT *heap, int N, int k) 581downheap (WT *heap, int N, int k)
567{ 582{
568 WT w = heap [k]; 583 WT w = heap [k];
569 584
570 while (k < (N >> 1)) 585 while (k < (N >> 1))
584 599
585 heap [k] = w; 600 heap [k] = w;
586 ((W)heap [k])->active = k + 1; 601 ((W)heap [k])->active = k + 1;
587} 602}
588 603
589inline void 604void inline_size
590adjustheap (WT *heap, int N, int k) 605adjustheap (WT *heap, int N, int k)
591{ 606{
592 upheap (heap, k); 607 upheap (heap, k);
593 downheap (heap, N, k); 608 downheap (heap, N, k);
594} 609}
604static ANSIG *signals; 619static ANSIG *signals;
605static int signalmax; 620static int signalmax;
606 621
607static int sigpipe [2]; 622static int sigpipe [2];
608static sig_atomic_t volatile gotsig; 623static sig_atomic_t volatile gotsig;
609static struct ev_io sigev; 624static ev_io sigev;
610 625
611static void 626void inline_size
612signals_init (ANSIG *base, int count) 627signals_init (ANSIG *base, int count)
613{ 628{
614 while (count--) 629 while (count--)
615 { 630 {
616 base->head = 0; 631 base->head = 0;
636 write (sigpipe [1], &signum, 1); 651 write (sigpipe [1], &signum, 1);
637 errno = old_errno; 652 errno = old_errno;
638 } 653 }
639} 654}
640 655
641void 656void noinline
642ev_feed_signal_event (EV_P_ int signum) 657ev_feed_signal_event (EV_P_ int signum)
643{ 658{
644 WL w; 659 WL w;
645 660
646#if EV_MULTIPLICITY 661#if EV_MULTIPLICITY
657 for (w = signals [signum].head; w; w = w->next) 672 for (w = signals [signum].head; w; w = w->next)
658 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 673 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
659} 674}
660 675
661static void 676static void
662sigcb (EV_P_ struct ev_io *iow, int revents) 677sigcb (EV_P_ ev_io *iow, int revents)
663{ 678{
664 int signum; 679 int signum;
665 680
666 read (sigpipe [0], &revents, 1); 681 read (sigpipe [0], &revents, 1);
667 gotsig = 0; 682 gotsig = 0;
669 for (signum = signalmax; signum--; ) 684 for (signum = signalmax; signum--; )
670 if (signals [signum].gotsig) 685 if (signals [signum].gotsig)
671 ev_feed_signal_event (EV_A_ signum + 1); 686 ev_feed_signal_event (EV_A_ signum + 1);
672} 687}
673 688
674static void 689void inline_size
675fd_intern (int fd) 690fd_intern (int fd)
676{ 691{
677#ifdef _WIN32 692#ifdef _WIN32
678 int arg = 1; 693 int arg = 1;
679 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 694 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
681 fcntl (fd, F_SETFD, FD_CLOEXEC); 696 fcntl (fd, F_SETFD, FD_CLOEXEC);
682 fcntl (fd, F_SETFL, O_NONBLOCK); 697 fcntl (fd, F_SETFL, O_NONBLOCK);
683#endif 698#endif
684} 699}
685 700
686static void 701static void noinline
687siginit (EV_P) 702siginit (EV_P)
688{ 703{
689 fd_intern (sigpipe [0]); 704 fd_intern (sigpipe [0]);
690 fd_intern (sigpipe [1]); 705 fd_intern (sigpipe [1]);
691 706
694 ev_unref (EV_A); /* child watcher should not keep loop alive */ 709 ev_unref (EV_A); /* child watcher should not keep loop alive */
695} 710}
696 711
697/*****************************************************************************/ 712/*****************************************************************************/
698 713
699static struct ev_child *childs [PID_HASHSIZE]; 714static ev_child *childs [PID_HASHSIZE];
700 715
701#ifndef _WIN32 716#ifndef _WIN32
702 717
703static struct ev_signal childev; 718static ev_signal childev;
704 719
705#ifndef WCONTINUED 720void inline_speed
706# define WCONTINUED 0
707#endif
708
709static void
710child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 721child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
711{ 722{
712 struct ev_child *w; 723 ev_child *w;
713 724
714 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 725 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
715 if (w->pid == pid || !w->pid) 726 if (w->pid == pid || !w->pid)
716 { 727 {
717 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 728 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
718 w->rpid = pid; 729 w->rpid = pid;
719 w->rstatus = status; 730 w->rstatus = status;
720 ev_feed_event (EV_A_ (W)w, EV_CHILD); 731 ev_feed_event (EV_A_ (W)w, EV_CHILD);
721 } 732 }
722} 733}
723 734
735#ifndef WCONTINUED
736# define WCONTINUED 0
737#endif
738
724static void 739static void
725childcb (EV_P_ struct ev_signal *sw, int revents) 740childcb (EV_P_ ev_signal *sw, int revents)
726{ 741{
727 int pid, status; 742 int pid, status;
728 743
744 /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
729 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 745 if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
730 { 746 if (!WCONTINUED
747 || errno != EINVAL
748 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
749 return;
750
731 /* make sure we are called again until all childs have been reaped */ 751 /* make sure we are called again until all childs have been reaped */
752 /* 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); 753 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
733 754
734 child_reap (EV_A_ sw, pid, pid, status); 755 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 */ 756 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
736 }
737} 757}
738 758
739#endif 759#endif
740 760
741/*****************************************************************************/ 761/*****************************************************************************/
767{ 787{
768 return EV_VERSION_MINOR; 788 return EV_VERSION_MINOR;
769} 789}
770 790
771/* return true if we are running with elevated privileges and should ignore env variables */ 791/* return true if we are running with elevated privileges and should ignore env variables */
772static int 792int inline_size
773enable_secure (void) 793enable_secure (void)
774{ 794{
775#ifdef _WIN32 795#ifdef _WIN32
776 return 0; 796 return 0;
777#else 797#else
781} 801}
782 802
783unsigned int 803unsigned int
784ev_supported_backends (void) 804ev_supported_backends (void)
785{ 805{
786}
787
788unsigned int
789ev_recommended_backends (void)
790{
791 unsigned int flags; 806 unsigned int flags = 0;
792 807
793 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 808 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
794 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 809 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
795 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 810 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
796 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 811 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
798 813
799 return flags; 814 return flags;
800} 815}
801 816
802unsigned int 817unsigned int
803ev_backend (EV_P) 818ev_recommended_backends (void)
804{ 819{
805 unsigned int flags = ev_recommended_backends (); 820 unsigned int flags = ev_supported_backends ();
806 821
807#ifndef __NetBSD__ 822#ifndef __NetBSD__
808 /* kqueue is borked on everything but netbsd apparently */ 823 /* kqueue is borked on everything but netbsd apparently */
809 /* it usually doesn't work correctly on anything but sockets and pipes */ 824 /* it usually doesn't work correctly on anything but sockets and pipes */
810 flags &= ~EVBACKEND_KQUEUE; 825 flags &= ~EVBACKEND_KQUEUE;
815#endif 830#endif
816 831
817 return flags; 832 return flags;
818} 833}
819 834
835unsigned int
836ev_embeddable_backends (void)
837{
838 return EVBACKEND_EPOLL
839 | EVBACKEND_KQUEUE
840 | EVBACKEND_PORT;
841}
842
843unsigned int
844ev_backend (EV_P)
845{
846 return backend;
847}
848
820static void 849static void
821loop_init (EV_P_ unsigned int flags) 850loop_init (EV_P_ unsigned int flags)
822{ 851{
823 if (!method) 852 if (!backend)
824 { 853 {
825#if EV_USE_MONOTONIC 854#if EV_USE_MONOTONIC
826 { 855 {
827 struct timespec ts; 856 struct timespec ts;
828 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 857 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
841 flags = atoi (getenv ("LIBEV_FLAGS")); 870 flags = atoi (getenv ("LIBEV_FLAGS"));
842 871
843 if (!(flags & 0x0000ffffUL)) 872 if (!(flags & 0x0000ffffUL))
844 flags |= ev_recommended_backends (); 873 flags |= ev_recommended_backends ();
845 874
846 method = 0; 875 backend = 0;
847#if EV_USE_PORT 876#if EV_USE_PORT
848 if (!method && (flags & EVBACKEND_PORT )) method = port_init (EV_A_ flags); 877 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
849#endif 878#endif
850#if EV_USE_KQUEUE 879#if EV_USE_KQUEUE
851 if (!method && (flags & EVBACKEND_KQUEUE)) method = kqueue_init (EV_A_ flags); 880 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
852#endif 881#endif
853#if EV_USE_EPOLL 882#if EV_USE_EPOLL
854 if (!method && (flags & EVBACKEND_EPOLL )) method = epoll_init (EV_A_ flags); 883 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
855#endif 884#endif
856#if EV_USE_POLL 885#if EV_USE_POLL
857 if (!method && (flags & EVBACKEND_POLL )) method = poll_init (EV_A_ flags); 886 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
858#endif 887#endif
859#if EV_USE_SELECT 888#if EV_USE_SELECT
860 if (!method && (flags & EVBACKEND_SELECT)) method = select_init (EV_A_ flags); 889 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
861#endif 890#endif
862 891
863 ev_init (&sigev, sigcb); 892 ev_init (&sigev, sigcb);
864 ev_set_priority (&sigev, EV_MAXPRI); 893 ev_set_priority (&sigev, EV_MAXPRI);
865 } 894 }
869loop_destroy (EV_P) 898loop_destroy (EV_P)
870{ 899{
871 int i; 900 int i;
872 901
873#if EV_USE_PORT 902#if EV_USE_PORT
874 if (method == EVBACKEND_PORT ) port_destroy (EV_A); 903 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
875#endif 904#endif
876#if EV_USE_KQUEUE 905#if EV_USE_KQUEUE
877 if (method == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 906 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
878#endif 907#endif
879#if EV_USE_EPOLL 908#if EV_USE_EPOLL
880 if (method == EVBACKEND_EPOLL ) epoll_destroy (EV_A); 909 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
881#endif 910#endif
882#if EV_USE_POLL 911#if EV_USE_POLL
883 if (method == EVBACKEND_POLL ) poll_destroy (EV_A); 912 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
884#endif 913#endif
885#if EV_USE_SELECT 914#if EV_USE_SELECT
886 if (method == EVBACKEND_SELECT) select_destroy (EV_A); 915 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
887#endif 916#endif
888 917
889 for (i = NUMPRI; i--; ) 918 for (i = NUMPRI; i--; )
890 array_free (pending, [i]); 919 array_free (pending, [i]);
891 920
892 /* have to use the microsoft-never-gets-it-right macro */ 921 /* have to use the microsoft-never-gets-it-right macro */
893 array_free (fdchange, EMPTY0); 922 array_free (fdchange, EMPTY0);
894 array_free (timer, EMPTY0); 923 array_free (timer, EMPTY0);
895#if EV_PERIODICS 924#if EV_PERIODIC_ENABLE
896 array_free (periodic, EMPTY0); 925 array_free (periodic, EMPTY0);
897#endif 926#endif
898 array_free (idle, EMPTY0); 927 array_free (idle, EMPTY0);
899 array_free (prepare, EMPTY0); 928 array_free (prepare, EMPTY0);
900 array_free (check, EMPTY0); 929 array_free (check, EMPTY0);
901 930
902 method = 0; 931 backend = 0;
903} 932}
904 933
905static void 934static void
906loop_fork (EV_P) 935loop_fork (EV_P)
907{ 936{
908#if EV_USE_PORT 937#if EV_USE_PORT
909 if (method == EVBACKEND_PORT ) port_fork (EV_A); 938 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
910#endif 939#endif
911#if EV_USE_KQUEUE 940#if EV_USE_KQUEUE
912 if (method == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 941 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
913#endif 942#endif
914#if EV_USE_EPOLL 943#if EV_USE_EPOLL
915 if (method == EVBACKEND_EPOLL ) epoll_fork (EV_A); 944 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
916#endif 945#endif
917 946
918 if (ev_is_active (&sigev)) 947 if (ev_is_active (&sigev))
919 { 948 {
920 /* default loop */ 949 /* default loop */
941 970
942 memset (loop, 0, sizeof (struct ev_loop)); 971 memset (loop, 0, sizeof (struct ev_loop));
943 972
944 loop_init (EV_A_ flags); 973 loop_init (EV_A_ flags);
945 974
946 if (ev_method (EV_A)) 975 if (ev_backend (EV_A))
947 return loop; 976 return loop;
948 977
949 return 0; 978 return 0;
950} 979}
951 980
984 ev_default_loop_ptr = 1; 1013 ev_default_loop_ptr = 1;
985#endif 1014#endif
986 1015
987 loop_init (EV_A_ flags); 1016 loop_init (EV_A_ flags);
988 1017
989 if (ev_method (EV_A)) 1018 if (ev_backend (EV_A))
990 { 1019 {
991 siginit (EV_A); 1020 siginit (EV_A);
992 1021
993#ifndef _WIN32 1022#ifndef _WIN32
994 ev_signal_init (&childev, childcb, SIGCHLD); 1023 ev_signal_init (&childev, childcb, SIGCHLD);
1030{ 1059{
1031#if EV_MULTIPLICITY 1060#if EV_MULTIPLICITY
1032 struct ev_loop *loop = ev_default_loop_ptr; 1061 struct ev_loop *loop = ev_default_loop_ptr;
1033#endif 1062#endif
1034 1063
1035 if (method) 1064 if (backend)
1036 postfork = 1; 1065 postfork = 1;
1037} 1066}
1038 1067
1039/*****************************************************************************/ 1068/*****************************************************************************/
1040 1069
1041static int 1070int inline_size
1042any_pending (EV_P) 1071any_pending (EV_P)
1043{ 1072{
1044 int pri; 1073 int pri;
1045 1074
1046 for (pri = NUMPRI; pri--; ) 1075 for (pri = NUMPRI; pri--; )
1048 return 1; 1077 return 1;
1049 1078
1050 return 0; 1079 return 0;
1051} 1080}
1052 1081
1053inline void 1082void inline_speed
1054call_pending (EV_P) 1083call_pending (EV_P)
1055{ 1084{
1056 int pri; 1085 int pri;
1057 1086
1058 for (pri = NUMPRI; pri--; ) 1087 for (pri = NUMPRI; pri--; )
1060 { 1089 {
1061 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1090 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1062 1091
1063 if (expect_true (p->w)) 1092 if (expect_true (p->w))
1064 { 1093 {
1094 assert (("non-pending watcher on pending list", p->w->pending));
1095
1065 p->w->pending = 0; 1096 p->w->pending = 0;
1066 EV_CB_INVOKE (p->w, p->events); 1097 EV_CB_INVOKE (p->w, p->events);
1067 } 1098 }
1068 } 1099 }
1069} 1100}
1070 1101
1071inline void 1102void inline_size
1072timers_reify (EV_P) 1103timers_reify (EV_P)
1073{ 1104{
1074 while (timercnt && ((WT)timers [0])->at <= mn_now) 1105 while (timercnt && ((WT)timers [0])->at <= mn_now)
1075 { 1106 {
1076 struct ev_timer *w = timers [0]; 1107 ev_timer *w = timers [0];
1077 1108
1078 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1109 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1079 1110
1080 /* first reschedule or stop timer */ 1111 /* first reschedule or stop timer */
1081 if (w->repeat) 1112 if (w->repeat)
1093 1124
1094 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1125 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1095 } 1126 }
1096} 1127}
1097 1128
1098#if EV_PERIODICS 1129#if EV_PERIODIC_ENABLE
1099inline void 1130void inline_size
1100periodics_reify (EV_P) 1131periodics_reify (EV_P)
1101{ 1132{
1102 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1133 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1103 { 1134 {
1104 struct ev_periodic *w = periodics [0]; 1135 ev_periodic *w = periodics [0];
1105 1136
1106 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1137 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1107 1138
1108 /* first reschedule or stop timer */ 1139 /* first reschedule or stop timer */
1109 if (w->reschedule_cb) 1140 if (w->reschedule_cb)
1123 1154
1124 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1155 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1125 } 1156 }
1126} 1157}
1127 1158
1128static void 1159static void noinline
1129periodics_reschedule (EV_P) 1160periodics_reschedule (EV_P)
1130{ 1161{
1131 int i; 1162 int i;
1132 1163
1133 /* adjust periodics after time jump */ 1164 /* adjust periodics after time jump */
1134 for (i = 0; i < periodiccnt; ++i) 1165 for (i = 0; i < periodiccnt; ++i)
1135 { 1166 {
1136 struct ev_periodic *w = periodics [i]; 1167 ev_periodic *w = periodics [i];
1137 1168
1138 if (w->reschedule_cb) 1169 if (w->reschedule_cb)
1139 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1170 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1140 else if (w->interval) 1171 else if (w->interval)
1141 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1172 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1145 for (i = periodiccnt >> 1; i--; ) 1176 for (i = periodiccnt >> 1; i--; )
1146 downheap ((WT *)periodics, periodiccnt, i); 1177 downheap ((WT *)periodics, periodiccnt, i);
1147} 1178}
1148#endif 1179#endif
1149 1180
1150inline int 1181int inline_size
1151time_update_monotonic (EV_P) 1182time_update_monotonic (EV_P)
1152{ 1183{
1153 mn_now = get_clock (); 1184 mn_now = get_clock ();
1154 1185
1155 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1186 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1163 ev_rt_now = ev_time (); 1194 ev_rt_now = ev_time ();
1164 return 1; 1195 return 1;
1165 } 1196 }
1166} 1197}
1167 1198
1168inline void 1199void inline_size
1169time_update (EV_P) 1200time_update (EV_P)
1170{ 1201{
1171 int i; 1202 int i;
1172 1203
1173#if EV_USE_MONOTONIC 1204#if EV_USE_MONOTONIC
1175 { 1206 {
1176 if (time_update_monotonic (EV_A)) 1207 if (time_update_monotonic (EV_A))
1177 { 1208 {
1178 ev_tstamp odiff = rtmn_diff; 1209 ev_tstamp odiff = rtmn_diff;
1179 1210
1180 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1211 /* loop a few times, before making important decisions.
1212 * on the choice of "4": one iteration isn't enough,
1213 * in case we get preempted during the calls to
1214 * ev_time and get_clock. a second call is almost guarenteed
1215 * to succeed in that case, though. and looping a few more times
1216 * doesn't hurt either as we only do this on time-jumps or
1217 * in the unlikely event of getting preempted here.
1218 */
1219 for (i = 4; --i; )
1181 { 1220 {
1182 rtmn_diff = ev_rt_now - mn_now; 1221 rtmn_diff = ev_rt_now - mn_now;
1183 1222
1184 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1223 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1185 return; /* all is well */ 1224 return; /* all is well */
1187 ev_rt_now = ev_time (); 1226 ev_rt_now = ev_time ();
1188 mn_now = get_clock (); 1227 mn_now = get_clock ();
1189 now_floor = mn_now; 1228 now_floor = mn_now;
1190 } 1229 }
1191 1230
1192# if EV_PERIODICS 1231# if EV_PERIODIC_ENABLE
1193 periodics_reschedule (EV_A); 1232 periodics_reschedule (EV_A);
1194# endif 1233# endif
1195 /* no timer adjustment, as the monotonic clock doesn't jump */ 1234 /* no timer adjustment, as the monotonic clock doesn't jump */
1196 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1235 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1197 } 1236 }
1201 { 1240 {
1202 ev_rt_now = ev_time (); 1241 ev_rt_now = ev_time ();
1203 1242
1204 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1243 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1205 { 1244 {
1206#if EV_PERIODICS 1245#if EV_PERIODIC_ENABLE
1207 periodics_reschedule (EV_A); 1246 periodics_reschedule (EV_A);
1208#endif 1247#endif
1209 1248
1210 /* adjust timers. this is easy, as the offset is the same for all */ 1249 /* adjust timers. this is easy, as the offset is the same for all */
1211 for (i = 0; i < timercnt; ++i) 1250 for (i = 0; i < timercnt; ++i)
1231static int loop_done; 1270static int loop_done;
1232 1271
1233void 1272void
1234ev_loop (EV_P_ int flags) 1273ev_loop (EV_P_ int flags)
1235{ 1274{
1236 double block;
1237 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1275 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1276 ? EVUNLOOP_ONE
1277 : EVUNLOOP_CANCEL;
1238 1278
1239 while (activecnt) 1279 while (activecnt)
1240 { 1280 {
1241 /* queue check watchers (and execute them) */ 1281 /* queue check watchers (and execute them) */
1242 if (expect_false (preparecnt)) 1282 if (expect_false (preparecnt))
1251 1291
1252 /* update fd-related kernel structures */ 1292 /* update fd-related kernel structures */
1253 fd_reify (EV_A); 1293 fd_reify (EV_A);
1254 1294
1255 /* calculate blocking time */ 1295 /* calculate blocking time */
1296 {
1297 double block;
1256 1298
1257 /* we only need this for !monotonic clock or timers, but as we basically 1299 if (flags & EVLOOP_NONBLOCK || idlecnt)
1258 always have timers, we just calculate it always */ 1300 block = 0.; /* do not block at all */
1301 else
1302 {
1303 /* update time to cancel out callback processing overhead */
1259#if EV_USE_MONOTONIC 1304#if EV_USE_MONOTONIC
1260 if (expect_true (have_monotonic)) 1305 if (expect_true (have_monotonic))
1261 time_update_monotonic (EV_A); 1306 time_update_monotonic (EV_A);
1262 else 1307 else
1263#endif 1308#endif
1264 { 1309 {
1265 ev_rt_now = ev_time (); 1310 ev_rt_now = ev_time ();
1266 mn_now = ev_rt_now; 1311 mn_now = ev_rt_now;
1267 } 1312 }
1268 1313
1269 if (flags & EVLOOP_NONBLOCK || idlecnt)
1270 block = 0.;
1271 else
1272 {
1273 block = MAX_BLOCKTIME; 1314 block = MAX_BLOCKTIME;
1274 1315
1275 if (timercnt) 1316 if (timercnt)
1276 { 1317 {
1277 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1318 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1278 if (block > to) block = to; 1319 if (block > to) block = to;
1279 } 1320 }
1280 1321
1281#if EV_PERIODICS 1322#if EV_PERIODIC_ENABLE
1282 if (periodiccnt) 1323 if (periodiccnt)
1283 { 1324 {
1284 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1325 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1285 if (block > to) block = to; 1326 if (block > to) block = to;
1286 } 1327 }
1287#endif 1328#endif
1288 1329
1289 if (expect_false (block < 0.)) block = 0.; 1330 if (expect_false (block < 0.)) block = 0.;
1290 } 1331 }
1291 1332
1292 method_poll (EV_A_ block); 1333 backend_poll (EV_A_ block);
1334 }
1293 1335
1294 /* update ev_rt_now, do magic */ 1336 /* update ev_rt_now, do magic */
1295 time_update (EV_A); 1337 time_update (EV_A);
1296 1338
1297 /* queue pending timers and reschedule them */ 1339 /* queue pending timers and reschedule them */
1298 timers_reify (EV_A); /* relative timers called last */ 1340 timers_reify (EV_A); /* relative timers called last */
1299#if EV_PERIODICS 1341#if EV_PERIODIC_ENABLE
1300 periodics_reify (EV_A); /* absolute timers called first */ 1342 periodics_reify (EV_A); /* absolute timers called first */
1301#endif 1343#endif
1302 1344
1303 /* queue idle watchers unless io or timers are pending */ 1345 /* queue idle watchers unless other events are pending */
1304 if (idlecnt && !any_pending (EV_A)) 1346 if (idlecnt && !any_pending (EV_A))
1305 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1347 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1306 1348
1307 /* queue check watchers, to be executed first */ 1349 /* queue check watchers, to be executed first */
1308 if (expect_false (checkcnt)) 1350 if (expect_false (checkcnt))
1312 1354
1313 if (expect_false (loop_done)) 1355 if (expect_false (loop_done))
1314 break; 1356 break;
1315 } 1357 }
1316 1358
1317 if (loop_done != 2) 1359 if (loop_done == EVUNLOOP_ONE)
1318 loop_done = 0; 1360 loop_done = EVUNLOOP_CANCEL;
1319} 1361}
1320 1362
1321void 1363void
1322ev_unloop (EV_P_ int how) 1364ev_unloop (EV_P_ int how)
1323{ 1365{
1324 loop_done = how; 1366 loop_done = how;
1325} 1367}
1326 1368
1327/*****************************************************************************/ 1369/*****************************************************************************/
1328 1370
1329inline void 1371void inline_size
1330wlist_add (WL *head, WL elem) 1372wlist_add (WL *head, WL elem)
1331{ 1373{
1332 elem->next = *head; 1374 elem->next = *head;
1333 *head = elem; 1375 *head = elem;
1334} 1376}
1335 1377
1336inline void 1378void inline_size
1337wlist_del (WL *head, WL elem) 1379wlist_del (WL *head, WL elem)
1338{ 1380{
1339 while (*head) 1381 while (*head)
1340 { 1382 {
1341 if (*head == elem) 1383 if (*head == elem)
1346 1388
1347 head = &(*head)->next; 1389 head = &(*head)->next;
1348 } 1390 }
1349} 1391}
1350 1392
1351inline void 1393void inline_speed
1352ev_clear_pending (EV_P_ W w) 1394ev_clear_pending (EV_P_ W w)
1353{ 1395{
1354 if (w->pending) 1396 if (w->pending)
1355 { 1397 {
1356 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1398 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1357 w->pending = 0; 1399 w->pending = 0;
1358 } 1400 }
1359} 1401}
1360 1402
1361inline void 1403void inline_speed
1362ev_start (EV_P_ W w, int active) 1404ev_start (EV_P_ W w, int active)
1363{ 1405{
1364 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1406 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1365 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1407 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1366 1408
1367 w->active = active; 1409 w->active = active;
1368 ev_ref (EV_A); 1410 ev_ref (EV_A);
1369} 1411}
1370 1412
1371inline void 1413void inline_size
1372ev_stop (EV_P_ W w) 1414ev_stop (EV_P_ W w)
1373{ 1415{
1374 ev_unref (EV_A); 1416 ev_unref (EV_A);
1375 w->active = 0; 1417 w->active = 0;
1376} 1418}
1377 1419
1378/*****************************************************************************/ 1420/*****************************************************************************/
1379 1421
1380void 1422void
1381ev_io_start (EV_P_ struct ev_io *w) 1423ev_io_start (EV_P_ ev_io *w)
1382{ 1424{
1383 int fd = w->fd; 1425 int fd = w->fd;
1384 1426
1385 if (expect_false (ev_is_active (w))) 1427 if (expect_false (ev_is_active (w)))
1386 return; 1428 return;
1393 1435
1394 fd_change (EV_A_ fd); 1436 fd_change (EV_A_ fd);
1395} 1437}
1396 1438
1397void 1439void
1398ev_io_stop (EV_P_ struct ev_io *w) 1440ev_io_stop (EV_P_ ev_io *w)
1399{ 1441{
1400 ev_clear_pending (EV_A_ (W)w); 1442 ev_clear_pending (EV_A_ (W)w);
1401 if (expect_false (!ev_is_active (w))) 1443 if (expect_false (!ev_is_active (w)))
1402 return; 1444 return;
1403 1445
1408 1450
1409 fd_change (EV_A_ w->fd); 1451 fd_change (EV_A_ w->fd);
1410} 1452}
1411 1453
1412void 1454void
1413ev_timer_start (EV_P_ struct ev_timer *w) 1455ev_timer_start (EV_P_ ev_timer *w)
1414{ 1456{
1415 if (expect_false (ev_is_active (w))) 1457 if (expect_false (ev_is_active (w)))
1416 return; 1458 return;
1417 1459
1418 ((WT)w)->at += mn_now; 1460 ((WT)w)->at += mn_now;
1419 1461
1420 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1462 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1421 1463
1422 ev_start (EV_A_ (W)w, ++timercnt); 1464 ev_start (EV_A_ (W)w, ++timercnt);
1423 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1465 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1424 timers [timercnt - 1] = w; 1466 timers [timercnt - 1] = w;
1425 upheap ((WT *)timers, timercnt - 1); 1467 upheap ((WT *)timers, timercnt - 1);
1426 1468
1427 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1469 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1428} 1470}
1429 1471
1430void 1472void
1431ev_timer_stop (EV_P_ struct ev_timer *w) 1473ev_timer_stop (EV_P_ ev_timer *w)
1432{ 1474{
1433 ev_clear_pending (EV_A_ (W)w); 1475 ev_clear_pending (EV_A_ (W)w);
1434 if (expect_false (!ev_is_active (w))) 1476 if (expect_false (!ev_is_active (w)))
1435 return; 1477 return;
1436 1478
1446 1488
1447 ev_stop (EV_A_ (W)w); 1489 ev_stop (EV_A_ (W)w);
1448} 1490}
1449 1491
1450void 1492void
1451ev_timer_again (EV_P_ struct ev_timer *w) 1493ev_timer_again (EV_P_ ev_timer *w)
1452{ 1494{
1453 if (ev_is_active (w)) 1495 if (ev_is_active (w))
1454 { 1496 {
1455 if (w->repeat) 1497 if (w->repeat)
1456 { 1498 {
1465 w->at = w->repeat; 1507 w->at = w->repeat;
1466 ev_timer_start (EV_A_ w); 1508 ev_timer_start (EV_A_ w);
1467 } 1509 }
1468} 1510}
1469 1511
1470#if EV_PERIODICS 1512#if EV_PERIODIC_ENABLE
1471void 1513void
1472ev_periodic_start (EV_P_ struct ev_periodic *w) 1514ev_periodic_start (EV_P_ ev_periodic *w)
1473{ 1515{
1474 if (expect_false (ev_is_active (w))) 1516 if (expect_false (ev_is_active (w)))
1475 return; 1517 return;
1476 1518
1477 if (w->reschedule_cb) 1519 if (w->reschedule_cb)
1482 /* this formula differs from the one in periodic_reify because we do not always round up */ 1524 /* 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; 1525 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1484 } 1526 }
1485 1527
1486 ev_start (EV_A_ (W)w, ++periodiccnt); 1528 ev_start (EV_A_ (W)w, ++periodiccnt);
1487 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1529 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1488 periodics [periodiccnt - 1] = w; 1530 periodics [periodiccnt - 1] = w;
1489 upheap ((WT *)periodics, periodiccnt - 1); 1531 upheap ((WT *)periodics, periodiccnt - 1);
1490 1532
1491 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1533 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1492} 1534}
1493 1535
1494void 1536void
1495ev_periodic_stop (EV_P_ struct ev_periodic *w) 1537ev_periodic_stop (EV_P_ ev_periodic *w)
1496{ 1538{
1497 ev_clear_pending (EV_A_ (W)w); 1539 ev_clear_pending (EV_A_ (W)w);
1498 if (expect_false (!ev_is_active (w))) 1540 if (expect_false (!ev_is_active (w)))
1499 return; 1541 return;
1500 1542
1508 1550
1509 ev_stop (EV_A_ (W)w); 1551 ev_stop (EV_A_ (W)w);
1510} 1552}
1511 1553
1512void 1554void
1513ev_periodic_again (EV_P_ struct ev_periodic *w) 1555ev_periodic_again (EV_P_ ev_periodic *w)
1514{ 1556{
1515 /* TODO: use adjustheap and recalculation */ 1557 /* TODO: use adjustheap and recalculation */
1516 ev_periodic_stop (EV_A_ w); 1558 ev_periodic_stop (EV_A_ w);
1517 ev_periodic_start (EV_A_ w); 1559 ev_periodic_start (EV_A_ w);
1518} 1560}
1519#endif 1561#endif
1520 1562
1521void 1563void
1522ev_idle_start (EV_P_ struct ev_idle *w) 1564ev_idle_start (EV_P_ ev_idle *w)
1523{ 1565{
1524 if (expect_false (ev_is_active (w))) 1566 if (expect_false (ev_is_active (w)))
1525 return; 1567 return;
1526 1568
1527 ev_start (EV_A_ (W)w, ++idlecnt); 1569 ev_start (EV_A_ (W)w, ++idlecnt);
1528 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1570 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1529 idles [idlecnt - 1] = w; 1571 idles [idlecnt - 1] = w;
1530} 1572}
1531 1573
1532void 1574void
1533ev_idle_stop (EV_P_ struct ev_idle *w) 1575ev_idle_stop (EV_P_ ev_idle *w)
1534{ 1576{
1535 ev_clear_pending (EV_A_ (W)w); 1577 ev_clear_pending (EV_A_ (W)w);
1536 if (expect_false (!ev_is_active (w))) 1578 if (expect_false (!ev_is_active (w)))
1537 return; 1579 return;
1538 1580
1581 {
1582 int active = ((W)w)->active;
1539 idles [((W)w)->active - 1] = idles [--idlecnt]; 1583 idles [active - 1] = idles [--idlecnt];
1584 ((W)idles [active - 1])->active = active;
1585 }
1586
1540 ev_stop (EV_A_ (W)w); 1587 ev_stop (EV_A_ (W)w);
1541} 1588}
1542 1589
1543void 1590void
1544ev_prepare_start (EV_P_ struct ev_prepare *w) 1591ev_prepare_start (EV_P_ ev_prepare *w)
1545{ 1592{
1546 if (expect_false (ev_is_active (w))) 1593 if (expect_false (ev_is_active (w)))
1547 return; 1594 return;
1548 1595
1549 ev_start (EV_A_ (W)w, ++preparecnt); 1596 ev_start (EV_A_ (W)w, ++preparecnt);
1550 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1597 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1551 prepares [preparecnt - 1] = w; 1598 prepares [preparecnt - 1] = w;
1552} 1599}
1553 1600
1554void 1601void
1555ev_prepare_stop (EV_P_ struct ev_prepare *w) 1602ev_prepare_stop (EV_P_ ev_prepare *w)
1556{ 1603{
1557 ev_clear_pending (EV_A_ (W)w); 1604 ev_clear_pending (EV_A_ (W)w);
1558 if (expect_false (!ev_is_active (w))) 1605 if (expect_false (!ev_is_active (w)))
1559 return; 1606 return;
1560 1607
1608 {
1609 int active = ((W)w)->active;
1561 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1610 prepares [active - 1] = prepares [--preparecnt];
1611 ((W)prepares [active - 1])->active = active;
1612 }
1613
1562 ev_stop (EV_A_ (W)w); 1614 ev_stop (EV_A_ (W)w);
1563} 1615}
1564 1616
1565void 1617void
1566ev_check_start (EV_P_ struct ev_check *w) 1618ev_check_start (EV_P_ ev_check *w)
1567{ 1619{
1568 if (expect_false (ev_is_active (w))) 1620 if (expect_false (ev_is_active (w)))
1569 return; 1621 return;
1570 1622
1571 ev_start (EV_A_ (W)w, ++checkcnt); 1623 ev_start (EV_A_ (W)w, ++checkcnt);
1572 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1624 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1573 checks [checkcnt - 1] = w; 1625 checks [checkcnt - 1] = w;
1574} 1626}
1575 1627
1576void 1628void
1577ev_check_stop (EV_P_ struct ev_check *w) 1629ev_check_stop (EV_P_ ev_check *w)
1578{ 1630{
1579 ev_clear_pending (EV_A_ (W)w); 1631 ev_clear_pending (EV_A_ (W)w);
1580 if (expect_false (!ev_is_active (w))) 1632 if (expect_false (!ev_is_active (w)))
1581 return; 1633 return;
1582 1634
1635 {
1636 int active = ((W)w)->active;
1583 checks [((W)w)->active - 1] = checks [--checkcnt]; 1637 checks [active - 1] = checks [--checkcnt];
1638 ((W)checks [active - 1])->active = active;
1639 }
1640
1584 ev_stop (EV_A_ (W)w); 1641 ev_stop (EV_A_ (W)w);
1585} 1642}
1586 1643
1587#ifndef SA_RESTART 1644#ifndef SA_RESTART
1588# define SA_RESTART 0 1645# define SA_RESTART 0
1589#endif 1646#endif
1590 1647
1591void 1648void
1592ev_signal_start (EV_P_ struct ev_signal *w) 1649ev_signal_start (EV_P_ ev_signal *w)
1593{ 1650{
1594#if EV_MULTIPLICITY 1651#if EV_MULTIPLICITY
1595 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1652 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1596#endif 1653#endif
1597 if (expect_false (ev_is_active (w))) 1654 if (expect_false (ev_is_active (w)))
1616#endif 1673#endif
1617 } 1674 }
1618} 1675}
1619 1676
1620void 1677void
1621ev_signal_stop (EV_P_ struct ev_signal *w) 1678ev_signal_stop (EV_P_ ev_signal *w)
1622{ 1679{
1623 ev_clear_pending (EV_A_ (W)w); 1680 ev_clear_pending (EV_A_ (W)w);
1624 if (expect_false (!ev_is_active (w))) 1681 if (expect_false (!ev_is_active (w)))
1625 return; 1682 return;
1626 1683
1630 if (!signals [w->signum - 1].head) 1687 if (!signals [w->signum - 1].head)
1631 signal (w->signum, SIG_DFL); 1688 signal (w->signum, SIG_DFL);
1632} 1689}
1633 1690
1634void 1691void
1635ev_child_start (EV_P_ struct ev_child *w) 1692ev_child_start (EV_P_ ev_child *w)
1636{ 1693{
1637#if EV_MULTIPLICITY 1694#if EV_MULTIPLICITY
1638 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1695 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1639#endif 1696#endif
1640 if (expect_false (ev_is_active (w))) 1697 if (expect_false (ev_is_active (w)))
1643 ev_start (EV_A_ (W)w, 1); 1700 ev_start (EV_A_ (W)w, 1);
1644 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1701 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1645} 1702}
1646 1703
1647void 1704void
1648ev_child_stop (EV_P_ struct ev_child *w) 1705ev_child_stop (EV_P_ ev_child *w)
1649{ 1706{
1650 ev_clear_pending (EV_A_ (W)w); 1707 ev_clear_pending (EV_A_ (W)w);
1651 if (expect_false (!ev_is_active (w))) 1708 if (expect_false (!ev_is_active (w)))
1652 return; 1709 return;
1653 1710
1654 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1711 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1655 ev_stop (EV_A_ (W)w); 1712 ev_stop (EV_A_ (W)w);
1656} 1713}
1657 1714
1715#if EV_EMBED_ENABLE
1716void noinline
1717ev_embed_sweep (EV_P_ ev_embed *w)
1718{
1719 ev_loop (w->loop, EVLOOP_NONBLOCK);
1720}
1721
1722static void
1723embed_cb (EV_P_ ev_io *io, int revents)
1724{
1725 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1726
1727 if (ev_cb (w))
1728 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1729 else
1730 ev_embed_sweep (loop, w);
1731}
1732
1733void
1734ev_embed_start (EV_P_ ev_embed *w)
1735{
1736 if (expect_false (ev_is_active (w)))
1737 return;
1738
1739 {
1740 struct ev_loop *loop = w->loop;
1741 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1742 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1743 }
1744
1745 ev_set_priority (&w->io, ev_priority (w));
1746 ev_io_start (EV_A_ &w->io);
1747
1748 ev_start (EV_A_ (W)w, 1);
1749}
1750
1751void
1752ev_embed_stop (EV_P_ ev_embed *w)
1753{
1754 ev_clear_pending (EV_A_ (W)w);
1755 if (expect_false (!ev_is_active (w)))
1756 return;
1757
1758 ev_io_stop (EV_A_ &w->io);
1759
1760 ev_stop (EV_A_ (W)w);
1761}
1762#endif
1763
1764#if EV_STAT_ENABLE
1765
1766# ifdef _WIN32
1767# define lstat(a,b) stat(a,b)
1768# endif
1769
1770void
1771ev_stat_stat (EV_P_ ev_stat *w)
1772{
1773 if (lstat (w->path, &w->attr) < 0)
1774 w->attr.st_nlink = 0;
1775 else if (!w->attr.st_nlink)
1776 w->attr.st_nlink = 1;
1777}
1778
1779static void
1780stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1781{
1782 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1783
1784 /* we copy this here each the time so that */
1785 /* prev has the old value when the callback gets invoked */
1786 w->prev = w->attr;
1787 ev_stat_stat (EV_A_ w);
1788
1789 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1790 ev_feed_event (EV_A_ w, EV_STAT);
1791}
1792
1793void
1794ev_stat_start (EV_P_ ev_stat *w)
1795{
1796 if (expect_false (ev_is_active (w)))
1797 return;
1798
1799 /* since we use memcmp, we need to clear any padding data etc. */
1800 memset (&w->prev, 0, sizeof (ev_statdata));
1801 memset (&w->attr, 0, sizeof (ev_statdata));
1802
1803 ev_stat_stat (EV_A_ w);
1804
1805 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1806 ev_set_priority (&w->timer, ev_priority (w));
1807 ev_timer_start (EV_A_ &w->timer);
1808
1809 ev_start (EV_A_ (W)w, 1);
1810}
1811
1812void
1813ev_stat_stop (EV_P_ ev_stat *w)
1814{
1815 ev_clear_pending (EV_A_ (W)w);
1816 if (expect_false (!ev_is_active (w)))
1817 return;
1818
1819 ev_timer_stop (EV_A_ &w->timer);
1820
1821 ev_stop (EV_A_ (W)w);
1822}
1823#endif
1824
1658/*****************************************************************************/ 1825/*****************************************************************************/
1659 1826
1660struct ev_once 1827struct ev_once
1661{ 1828{
1662 struct ev_io io; 1829 ev_io io;
1663 struct ev_timer to; 1830 ev_timer to;
1664 void (*cb)(int revents, void *arg); 1831 void (*cb)(int revents, void *arg);
1665 void *arg; 1832 void *arg;
1666}; 1833};
1667 1834
1668static void 1835static void
1677 1844
1678 cb (revents, arg); 1845 cb (revents, arg);
1679} 1846}
1680 1847
1681static void 1848static void
1682once_cb_io (EV_P_ struct ev_io *w, int revents) 1849once_cb_io (EV_P_ ev_io *w, int revents)
1683{ 1850{
1684 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1851 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1685} 1852}
1686 1853
1687static void 1854static void
1688once_cb_to (EV_P_ struct ev_timer *w, int revents) 1855once_cb_to (EV_P_ ev_timer *w, int revents)
1689{ 1856{
1690 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1857 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1691} 1858}
1692 1859
1693void 1860void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines