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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines