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

Comparing libev/ev.c (file contents):
Revision 1.130 by root, Fri Nov 23 05:13:48 2007 UTC vs.
Revision 1.150 by root, Tue Nov 27 19:41:52 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 {
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--; )
527 return; 549 return;
528 } 550 }
529} 551}
530 552
531/* usually called after fork if backend 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
795} 823}
796 824
797unsigned int 825unsigned int
798ev_recommended_backends (void) 826ev_recommended_backends (void)
799{ 827{
800 unsigned int flags = ev_recommended_backends (); 828 unsigned int flags = ev_supported_backends ();
801 829
802#ifndef __NetBSD__ 830#ifndef __NetBSD__
803 /* kqueue is borked on everything but netbsd apparently */ 831 /* kqueue is borked on everything but netbsd apparently */
804 /* 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 */
805 flags &= ~EVBACKEND_KQUEUE; 833 flags &= ~EVBACKEND_KQUEUE;
808 // flags &= ~EVBACKEND_KQUEUE; for documentation 836 // flags &= ~EVBACKEND_KQUEUE; for documentation
809 flags &= ~EVBACKEND_POLL; 837 flags &= ~EVBACKEND_POLL;
810#endif 838#endif
811 839
812 return flags; 840 return flags;
841}
842
843unsigned int
844ev_embeddable_backends (void)
845{
846 return EVBACKEND_EPOLL
847 | EVBACKEND_KQUEUE
848 | EVBACKEND_PORT;
813} 849}
814 850
815unsigned int 851unsigned int
816ev_backend (EV_P) 852ev_backend (EV_P)
817{ 853{
891 array_free (pending, [i]); 927 array_free (pending, [i]);
892 928
893 /* have to use the microsoft-never-gets-it-right macro */ 929 /* have to use the microsoft-never-gets-it-right macro */
894 array_free (fdchange, EMPTY0); 930 array_free (fdchange, EMPTY0);
895 array_free (timer, EMPTY0); 931 array_free (timer, EMPTY0);
896#if EV_PERIODICS 932#if EV_PERIODIC_ENABLE
897 array_free (periodic, EMPTY0); 933 array_free (periodic, EMPTY0);
898#endif 934#endif
899 array_free (idle, EMPTY0); 935 array_free (idle, EMPTY0);
900 array_free (prepare, EMPTY0); 936 array_free (prepare, EMPTY0);
901 array_free (check, EMPTY0); 937 array_free (check, EMPTY0);
1037 postfork = 1; 1073 postfork = 1;
1038} 1074}
1039 1075
1040/*****************************************************************************/ 1076/*****************************************************************************/
1041 1077
1042static int 1078int inline_size
1043any_pending (EV_P) 1079any_pending (EV_P)
1044{ 1080{
1045 int pri; 1081 int pri;
1046 1082
1047 for (pri = NUMPRI; pri--; ) 1083 for (pri = NUMPRI; pri--; )
1049 return 1; 1085 return 1;
1050 1086
1051 return 0; 1087 return 0;
1052} 1088}
1053 1089
1054inline void 1090void inline_speed
1055call_pending (EV_P) 1091call_pending (EV_P)
1056{ 1092{
1057 int pri; 1093 int pri;
1058 1094
1059 for (pri = NUMPRI; pri--; ) 1095 for (pri = NUMPRI; pri--; )
1061 { 1097 {
1062 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1098 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1063 1099
1064 if (expect_true (p->w)) 1100 if (expect_true (p->w))
1065 { 1101 {
1102 assert (("non-pending watcher on pending list", p->w->pending));
1103
1066 p->w->pending = 0; 1104 p->w->pending = 0;
1067 EV_CB_INVOKE (p->w, p->events); 1105 EV_CB_INVOKE (p->w, p->events);
1068 } 1106 }
1069 } 1107 }
1070} 1108}
1071 1109
1072inline void 1110void inline_size
1073timers_reify (EV_P) 1111timers_reify (EV_P)
1074{ 1112{
1075 while (timercnt && ((WT)timers [0])->at <= mn_now) 1113 while (timercnt && ((WT)timers [0])->at <= mn_now)
1076 { 1114 {
1077 struct ev_timer *w = timers [0]; 1115 ev_timer *w = timers [0];
1078 1116
1079 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1117 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1080 1118
1081 /* first reschedule or stop timer */ 1119 /* first reschedule or stop timer */
1082 if (w->repeat) 1120 if (w->repeat)
1094 1132
1095 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1133 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1096 } 1134 }
1097} 1135}
1098 1136
1099#if EV_PERIODICS 1137#if EV_PERIODIC_ENABLE
1100inline void 1138void inline_size
1101periodics_reify (EV_P) 1139periodics_reify (EV_P)
1102{ 1140{
1103 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1141 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1104 { 1142 {
1105 struct ev_periodic *w = periodics [0]; 1143 ev_periodic *w = periodics [0];
1106 1144
1107 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1145 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1108 1146
1109 /* first reschedule or stop timer */ 1147 /* first reschedule or stop timer */
1110 if (w->reschedule_cb) 1148 if (w->reschedule_cb)
1124 1162
1125 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1163 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1126 } 1164 }
1127} 1165}
1128 1166
1129static void 1167static void noinline
1130periodics_reschedule (EV_P) 1168periodics_reschedule (EV_P)
1131{ 1169{
1132 int i; 1170 int i;
1133 1171
1134 /* adjust periodics after time jump */ 1172 /* adjust periodics after time jump */
1135 for (i = 0; i < periodiccnt; ++i) 1173 for (i = 0; i < periodiccnt; ++i)
1136 { 1174 {
1137 struct ev_periodic *w = periodics [i]; 1175 ev_periodic *w = periodics [i];
1138 1176
1139 if (w->reschedule_cb) 1177 if (w->reschedule_cb)
1140 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1178 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1141 else if (w->interval) 1179 else if (w->interval)
1142 ((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;
1146 for (i = periodiccnt >> 1; i--; ) 1184 for (i = periodiccnt >> 1; i--; )
1147 downheap ((WT *)periodics, periodiccnt, i); 1185 downheap ((WT *)periodics, periodiccnt, i);
1148} 1186}
1149#endif 1187#endif
1150 1188
1151inline int 1189int inline_size
1152time_update_monotonic (EV_P) 1190time_update_monotonic (EV_P)
1153{ 1191{
1154 mn_now = get_clock (); 1192 mn_now = get_clock ();
1155 1193
1156 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1194 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1164 ev_rt_now = ev_time (); 1202 ev_rt_now = ev_time ();
1165 return 1; 1203 return 1;
1166 } 1204 }
1167} 1205}
1168 1206
1169inline void 1207void inline_size
1170time_update (EV_P) 1208time_update (EV_P)
1171{ 1209{
1172 int i; 1210 int i;
1173 1211
1174#if EV_USE_MONOTONIC 1212#if EV_USE_MONOTONIC
1176 { 1214 {
1177 if (time_update_monotonic (EV_A)) 1215 if (time_update_monotonic (EV_A))
1178 { 1216 {
1179 ev_tstamp odiff = rtmn_diff; 1217 ev_tstamp odiff = rtmn_diff;
1180 1218
1181 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; )
1182 { 1228 {
1183 rtmn_diff = ev_rt_now - mn_now; 1229 rtmn_diff = ev_rt_now - mn_now;
1184 1230
1185 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1231 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1186 return; /* all is well */ 1232 return; /* all is well */
1188 ev_rt_now = ev_time (); 1234 ev_rt_now = ev_time ();
1189 mn_now = get_clock (); 1235 mn_now = get_clock ();
1190 now_floor = mn_now; 1236 now_floor = mn_now;
1191 } 1237 }
1192 1238
1193# if EV_PERIODICS 1239# if EV_PERIODIC_ENABLE
1194 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1195# endif 1241# endif
1196 /* no timer adjustment, as the monotonic clock doesn't jump */ 1242 /* no timer adjustment, as the monotonic clock doesn't jump */
1197 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1243 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1198 } 1244 }
1202 { 1248 {
1203 ev_rt_now = ev_time (); 1249 ev_rt_now = ev_time ();
1204 1250
1205 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))
1206 { 1252 {
1207#if EV_PERIODICS 1253#if EV_PERIODIC_ENABLE
1208 periodics_reschedule (EV_A); 1254 periodics_reschedule (EV_A);
1209#endif 1255#endif
1210 1256
1211 /* 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 */
1212 for (i = 0; i < timercnt; ++i) 1258 for (i = 0; i < timercnt; ++i)
1232static int loop_done; 1278static int loop_done;
1233 1279
1234void 1280void
1235ev_loop (EV_P_ int flags) 1281ev_loop (EV_P_ int flags)
1236{ 1282{
1237 double block;
1238 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1283 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1284 ? EVUNLOOP_ONE
1285 : EVUNLOOP_CANCEL;
1239 1286
1240 while (activecnt) 1287 while (activecnt)
1241 { 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
1242 /* queue check watchers (and execute them) */ 1299 /* queue check watchers (and execute them) */
1243 if (expect_false (preparecnt)) 1300 if (expect_false (preparecnt))
1244 { 1301 {
1245 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1302 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1246 call_pending (EV_A); 1303 call_pending (EV_A);
1252 1309
1253 /* update fd-related kernel structures */ 1310 /* update fd-related kernel structures */
1254 fd_reify (EV_A); 1311 fd_reify (EV_A);
1255 1312
1256 /* calculate blocking time */ 1313 /* calculate blocking time */
1314 {
1315 double block;
1257 1316
1258 /* we only need this for !monotonic clock or timers, but as we basically 1317 if (flags & EVLOOP_NONBLOCK || idlecnt)
1259 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 */
1260#if EV_USE_MONOTONIC 1322#if EV_USE_MONOTONIC
1261 if (expect_true (have_monotonic)) 1323 if (expect_true (have_monotonic))
1262 time_update_monotonic (EV_A); 1324 time_update_monotonic (EV_A);
1263 else 1325 else
1264#endif 1326#endif
1265 { 1327 {
1266 ev_rt_now = ev_time (); 1328 ev_rt_now = ev_time ();
1267 mn_now = ev_rt_now; 1329 mn_now = ev_rt_now;
1268 } 1330 }
1269 1331
1270 if (flags & EVLOOP_NONBLOCK || idlecnt)
1271 block = 0.;
1272 else
1273 {
1274 block = MAX_BLOCKTIME; 1332 block = MAX_BLOCKTIME;
1275 1333
1276 if (timercnt) 1334 if (timercnt)
1277 { 1335 {
1278 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1336 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1279 if (block > to) block = to; 1337 if (block > to) block = to;
1280 } 1338 }
1281 1339
1282#if EV_PERIODICS 1340#if EV_PERIODIC_ENABLE
1283 if (periodiccnt) 1341 if (periodiccnt)
1284 { 1342 {
1285 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1343 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1286 if (block > to) block = to; 1344 if (block > to) block = to;
1287 } 1345 }
1288#endif 1346#endif
1289 1347
1290 if (expect_false (block < 0.)) block = 0.; 1348 if (expect_false (block < 0.)) block = 0.;
1291 } 1349 }
1292 1350
1293 backend_poll (EV_A_ block); 1351 backend_poll (EV_A_ block);
1352 }
1294 1353
1295 /* update ev_rt_now, do magic */ 1354 /* update ev_rt_now, do magic */
1296 time_update (EV_A); 1355 time_update (EV_A);
1297 1356
1298 /* queue pending timers and reschedule them */ 1357 /* queue pending timers and reschedule them */
1299 timers_reify (EV_A); /* relative timers called last */ 1358 timers_reify (EV_A); /* relative timers called last */
1300#if EV_PERIODICS 1359#if EV_PERIODIC_ENABLE
1301 periodics_reify (EV_A); /* absolute timers called first */ 1360 periodics_reify (EV_A); /* absolute timers called first */
1302#endif 1361#endif
1303 1362
1304 /* queue idle watchers unless io or timers are pending */ 1363 /* queue idle watchers unless other events are pending */
1305 if (idlecnt && !any_pending (EV_A)) 1364 if (idlecnt && !any_pending (EV_A))
1306 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1365 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1307 1366
1308 /* queue check watchers, to be executed first */ 1367 /* queue check watchers, to be executed first */
1309 if (expect_false (checkcnt)) 1368 if (expect_false (checkcnt))
1313 1372
1314 if (expect_false (loop_done)) 1373 if (expect_false (loop_done))
1315 break; 1374 break;
1316 } 1375 }
1317 1376
1318 if (loop_done != 2) 1377 if (loop_done == EVUNLOOP_ONE)
1319 loop_done = 0; 1378 loop_done = EVUNLOOP_CANCEL;
1320} 1379}
1321 1380
1322void 1381void
1323ev_unloop (EV_P_ int how) 1382ev_unloop (EV_P_ int how)
1324{ 1383{
1325 loop_done = how; 1384 loop_done = how;
1326} 1385}
1327 1386
1328/*****************************************************************************/ 1387/*****************************************************************************/
1329 1388
1330inline void 1389void inline_size
1331wlist_add (WL *head, WL elem) 1390wlist_add (WL *head, WL elem)
1332{ 1391{
1333 elem->next = *head; 1392 elem->next = *head;
1334 *head = elem; 1393 *head = elem;
1335} 1394}
1336 1395
1337inline void 1396void inline_size
1338wlist_del (WL *head, WL elem) 1397wlist_del (WL *head, WL elem)
1339{ 1398{
1340 while (*head) 1399 while (*head)
1341 { 1400 {
1342 if (*head == elem) 1401 if (*head == elem)
1347 1406
1348 head = &(*head)->next; 1407 head = &(*head)->next;
1349 } 1408 }
1350} 1409}
1351 1410
1352inline void 1411void inline_speed
1353ev_clear_pending (EV_P_ W w) 1412ev_clear_pending (EV_P_ W w)
1354{ 1413{
1355 if (w->pending) 1414 if (w->pending)
1356 { 1415 {
1357 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1416 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1358 w->pending = 0; 1417 w->pending = 0;
1359 } 1418 }
1360} 1419}
1361 1420
1362inline void 1421void inline_speed
1363ev_start (EV_P_ W w, int active) 1422ev_start (EV_P_ W w, int active)
1364{ 1423{
1365 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1424 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1366 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1425 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1367 1426
1368 w->active = active; 1427 w->active = active;
1369 ev_ref (EV_A); 1428 ev_ref (EV_A);
1370} 1429}
1371 1430
1372inline void 1431void inline_size
1373ev_stop (EV_P_ W w) 1432ev_stop (EV_P_ W w)
1374{ 1433{
1375 ev_unref (EV_A); 1434 ev_unref (EV_A);
1376 w->active = 0; 1435 w->active = 0;
1377} 1436}
1378 1437
1379/*****************************************************************************/ 1438/*****************************************************************************/
1380 1439
1381void 1440void
1382ev_io_start (EV_P_ struct ev_io *w) 1441ev_io_start (EV_P_ ev_io *w)
1383{ 1442{
1384 int fd = w->fd; 1443 int fd = w->fd;
1385 1444
1386 if (expect_false (ev_is_active (w))) 1445 if (expect_false (ev_is_active (w)))
1387 return; 1446 return;
1394 1453
1395 fd_change (EV_A_ fd); 1454 fd_change (EV_A_ fd);
1396} 1455}
1397 1456
1398void 1457void
1399ev_io_stop (EV_P_ struct ev_io *w) 1458ev_io_stop (EV_P_ ev_io *w)
1400{ 1459{
1401 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1402 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1403 return; 1462 return;
1404 1463
1409 1468
1410 fd_change (EV_A_ w->fd); 1469 fd_change (EV_A_ w->fd);
1411} 1470}
1412 1471
1413void 1472void
1414ev_timer_start (EV_P_ struct ev_timer *w) 1473ev_timer_start (EV_P_ ev_timer *w)
1415{ 1474{
1416 if (expect_false (ev_is_active (w))) 1475 if (expect_false (ev_is_active (w)))
1417 return; 1476 return;
1418 1477
1419 ((WT)w)->at += mn_now; 1478 ((WT)w)->at += mn_now;
1420 1479
1421 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.));
1422 1481
1423 ev_start (EV_A_ (W)w, ++timercnt); 1482 ev_start (EV_A_ (W)w, ++timercnt);
1424 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1483 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1425 timers [timercnt - 1] = w; 1484 timers [timercnt - 1] = w;
1426 upheap ((WT *)timers, timercnt - 1); 1485 upheap ((WT *)timers, timercnt - 1);
1427 1486
1428 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1487 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1429} 1488}
1430 1489
1431void 1490void
1432ev_timer_stop (EV_P_ struct ev_timer *w) 1491ev_timer_stop (EV_P_ ev_timer *w)
1433{ 1492{
1434 ev_clear_pending (EV_A_ (W)w); 1493 ev_clear_pending (EV_A_ (W)w);
1435 if (expect_false (!ev_is_active (w))) 1494 if (expect_false (!ev_is_active (w)))
1436 return; 1495 return;
1437 1496
1447 1506
1448 ev_stop (EV_A_ (W)w); 1507 ev_stop (EV_A_ (W)w);
1449} 1508}
1450 1509
1451void 1510void
1452ev_timer_again (EV_P_ struct ev_timer *w) 1511ev_timer_again (EV_P_ ev_timer *w)
1453{ 1512{
1454 if (ev_is_active (w)) 1513 if (ev_is_active (w))
1455 { 1514 {
1456 if (w->repeat) 1515 if (w->repeat)
1457 { 1516 {
1466 w->at = w->repeat; 1525 w->at = w->repeat;
1467 ev_timer_start (EV_A_ w); 1526 ev_timer_start (EV_A_ w);
1468 } 1527 }
1469} 1528}
1470 1529
1471#if EV_PERIODICS 1530#if EV_PERIODIC_ENABLE
1472void 1531void
1473ev_periodic_start (EV_P_ struct ev_periodic *w) 1532ev_periodic_start (EV_P_ ev_periodic *w)
1474{ 1533{
1475 if (expect_false (ev_is_active (w))) 1534 if (expect_false (ev_is_active (w)))
1476 return; 1535 return;
1477 1536
1478 if (w->reschedule_cb) 1537 if (w->reschedule_cb)
1483 /* 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 */
1484 ((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;
1485 } 1544 }
1486 1545
1487 ev_start (EV_A_ (W)w, ++periodiccnt); 1546 ev_start (EV_A_ (W)w, ++periodiccnt);
1488 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1547 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1489 periodics [periodiccnt - 1] = w; 1548 periodics [periodiccnt - 1] = w;
1490 upheap ((WT *)periodics, periodiccnt - 1); 1549 upheap ((WT *)periodics, periodiccnt - 1);
1491 1550
1492 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1551 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1493} 1552}
1494 1553
1495void 1554void
1496ev_periodic_stop (EV_P_ struct ev_periodic *w) 1555ev_periodic_stop (EV_P_ ev_periodic *w)
1497{ 1556{
1498 ev_clear_pending (EV_A_ (W)w); 1557 ev_clear_pending (EV_A_ (W)w);
1499 if (expect_false (!ev_is_active (w))) 1558 if (expect_false (!ev_is_active (w)))
1500 return; 1559 return;
1501 1560
1509 1568
1510 ev_stop (EV_A_ (W)w); 1569 ev_stop (EV_A_ (W)w);
1511} 1570}
1512 1571
1513void 1572void
1514ev_periodic_again (EV_P_ struct ev_periodic *w) 1573ev_periodic_again (EV_P_ ev_periodic *w)
1515{ 1574{
1516 /* TODO: use adjustheap and recalculation */ 1575 /* TODO: use adjustheap and recalculation */
1517 ev_periodic_stop (EV_A_ w); 1576 ev_periodic_stop (EV_A_ w);
1518 ev_periodic_start (EV_A_ w); 1577 ev_periodic_start (EV_A_ w);
1519} 1578}
1520#endif 1579#endif
1521 1580
1522void
1523ev_idle_start (EV_P_ struct ev_idle *w)
1524{
1525 if (expect_false (ev_is_active (w)))
1526 return;
1527
1528 ev_start (EV_A_ (W)w, ++idlecnt);
1529 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1530 idles [idlecnt - 1] = w;
1531}
1532
1533void
1534ev_idle_stop (EV_P_ struct ev_idle *w)
1535{
1536 ev_clear_pending (EV_A_ (W)w);
1537 if (expect_false (!ev_is_active (w)))
1538 return;
1539
1540 idles [((W)w)->active - 1] = idles [--idlecnt];
1541 ev_stop (EV_A_ (W)w);
1542}
1543
1544void
1545ev_prepare_start (EV_P_ struct ev_prepare *w)
1546{
1547 if (expect_false (ev_is_active (w)))
1548 return;
1549
1550 ev_start (EV_A_ (W)w, ++preparecnt);
1551 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1552 prepares [preparecnt - 1] = w;
1553}
1554
1555void
1556ev_prepare_stop (EV_P_ struct ev_prepare *w)
1557{
1558 ev_clear_pending (EV_A_ (W)w);
1559 if (expect_false (!ev_is_active (w)))
1560 return;
1561
1562 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1563 ev_stop (EV_A_ (W)w);
1564}
1565
1566void
1567ev_check_start (EV_P_ struct ev_check *w)
1568{
1569 if (expect_false (ev_is_active (w)))
1570 return;
1571
1572 ev_start (EV_A_ (W)w, ++checkcnt);
1573 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1574 checks [checkcnt - 1] = w;
1575}
1576
1577void
1578ev_check_stop (EV_P_ struct ev_check *w)
1579{
1580 ev_clear_pending (EV_A_ (W)w);
1581 if (expect_false (!ev_is_active (w)))
1582 return;
1583
1584 checks [((W)w)->active - 1] = checks [--checkcnt];
1585 ev_stop (EV_A_ (W)w);
1586}
1587
1588#ifndef SA_RESTART 1581#ifndef SA_RESTART
1589# define SA_RESTART 0 1582# define SA_RESTART 0
1590#endif 1583#endif
1591 1584
1592void 1585void
1593ev_signal_start (EV_P_ struct ev_signal *w) 1586ev_signal_start (EV_P_ ev_signal *w)
1594{ 1587{
1595#if EV_MULTIPLICITY 1588#if EV_MULTIPLICITY
1596 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));
1597#endif 1590#endif
1598 if (expect_false (ev_is_active (w))) 1591 if (expect_false (ev_is_active (w)))
1617#endif 1610#endif
1618 } 1611 }
1619} 1612}
1620 1613
1621void 1614void
1622ev_signal_stop (EV_P_ struct ev_signal *w) 1615ev_signal_stop (EV_P_ ev_signal *w)
1623{ 1616{
1624 ev_clear_pending (EV_A_ (W)w); 1617 ev_clear_pending (EV_A_ (W)w);
1625 if (expect_false (!ev_is_active (w))) 1618 if (expect_false (!ev_is_active (w)))
1626 return; 1619 return;
1627 1620
1631 if (!signals [w->signum - 1].head) 1624 if (!signals [w->signum - 1].head)
1632 signal (w->signum, SIG_DFL); 1625 signal (w->signum, SIG_DFL);
1633} 1626}
1634 1627
1635void 1628void
1636ev_child_start (EV_P_ struct ev_child *w) 1629ev_child_start (EV_P_ ev_child *w)
1637{ 1630{
1638#if EV_MULTIPLICITY 1631#if EV_MULTIPLICITY
1639 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));
1640#endif 1633#endif
1641 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1642 return; 1635 return;
1643 1636
1644 ev_start (EV_A_ (W)w, 1); 1637 ev_start (EV_A_ (W)w, 1);
1645 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1638 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1646} 1639}
1647 1640
1648void 1641void
1649ev_child_stop (EV_P_ struct ev_child *w) 1642ev_child_stop (EV_P_ ev_child *w)
1650{ 1643{
1651 ev_clear_pending (EV_A_ (W)w); 1644 ev_clear_pending (EV_A_ (W)w);
1652 if (expect_false (!ev_is_active (w))) 1645 if (expect_false (!ev_is_active (w)))
1653 return; 1646 return;
1654 1647
1655 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1648 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1656 ev_stop (EV_A_ (W)w); 1649 ev_stop (EV_A_ (W)w);
1657} 1650}
1658 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
1659/*****************************************************************************/ 1879/*****************************************************************************/
1660 1880
1661struct ev_once 1881struct ev_once
1662{ 1882{
1663 struct ev_io io; 1883 ev_io io;
1664 struct ev_timer to; 1884 ev_timer to;
1665 void (*cb)(int revents, void *arg); 1885 void (*cb)(int revents, void *arg);
1666 void *arg; 1886 void *arg;
1667}; 1887};
1668 1888
1669static void 1889static void
1678 1898
1679 cb (revents, arg); 1899 cb (revents, arg);
1680} 1900}
1681 1901
1682static void 1902static void
1683once_cb_io (EV_P_ struct ev_io *w, int revents) 1903once_cb_io (EV_P_ ev_io *w, int revents)
1684{ 1904{
1685 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);
1686} 1906}
1687 1907
1688static void 1908static void
1689once_cb_to (EV_P_ struct ev_timer *w, int revents) 1909once_cb_to (EV_P_ ev_timer *w, int revents)
1690{ 1910{
1691 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);
1692} 1912}
1693 1913
1694void 1914void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines