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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines