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

Comparing libev/ev.c (file contents):
Revision 1.126 by root, Sun Nov 18 01:25:23 2007 UTC vs.
Revision 1.149 by root, Tue Nov 27 19:23:31 2007 UTC

32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
34#endif 34#endif
35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
37# include "config.h" 40# include "config.h"
41# endif
38 42
39# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC 44# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
42# endif 46# endif
50# ifndef EV_USE_REALTIME 54# ifndef EV_USE_REALTIME
51# define EV_USE_REALTIME 0 55# define EV_USE_REALTIME 0
52# endif 56# endif
53# endif 57# endif
54 58
55# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 59# ifndef EV_USE_SELECT
60# if HAVE_SELECT && HAVE_SYS_SELECT_H
56# define EV_USE_SELECT 1 61# define EV_USE_SELECT 1
57# else 62# else
58# define EV_USE_SELECT 0 63# define EV_USE_SELECT 0
64# endif
59# endif 65# endif
60 66
61# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 67# ifndef EV_USE_POLL
68# if HAVE_POLL && HAVE_POLL_H
62# define EV_USE_POLL 1 69# define EV_USE_POLL 1
63# else 70# else
64# define EV_USE_POLL 0 71# define EV_USE_POLL 0
72# endif
65# endif 73# endif
66 74
67# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 75# ifndef EV_USE_EPOLL
76# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
68# define EV_USE_EPOLL 1 77# define EV_USE_EPOLL 1
69# else 78# else
70# define EV_USE_EPOLL 0 79# define EV_USE_EPOLL 0
80# endif
71# endif 81# endif
72 82
83# ifndef EV_USE_KQUEUE
73# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 84# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
74# define EV_USE_KQUEUE 1 85# define EV_USE_KQUEUE 1
75# else 86# else
76# define EV_USE_KQUEUE 0 87# define EV_USE_KQUEUE 0
88# endif
77# endif 89# endif
78 90
79# if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT) 91# ifndef EV_USE_PORT
92# if HAVE_PORT_H && HAVE_PORT_CREATE
80# define EV_USE_PORT 1 93# define EV_USE_PORT 1
81# else 94# else
82# define EV_USE_PORT 0 95# define EV_USE_PORT 0
96# endif
83# endif 97# endif
84 98
85#endif 99#endif
86 100
87#include <math.h> 101#include <math.h>
97#include <time.h> 111#include <time.h>
98 112
99#include <signal.h> 113#include <signal.h>
100 114
101#ifndef _WIN32 115#ifndef _WIN32
102# include <unistd.h>
103# include <sys/time.h> 116# include <sys/time.h>
104# include <sys/wait.h> 117# include <sys/wait.h>
118# include <unistd.h>
105#else 119#else
106# define WIN32_LEAN_AND_MEAN 120# define WIN32_LEAN_AND_MEAN
107# include <windows.h> 121# include <windows.h>
108# ifndef EV_SELECT_IS_WINSOCKET 122# ifndef EV_SELECT_IS_WINSOCKET
109# define EV_SELECT_IS_WINSOCKET 1 123# define EV_SELECT_IS_WINSOCKET 1
142 156
143#ifndef EV_USE_PORT 157#ifndef EV_USE_PORT
144# define EV_USE_PORT 0 158# define EV_USE_PORT 0
145#endif 159#endif
146 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
147/**/ 169/**/
148
149/* darwin simply cannot be helped */
150#ifdef __APPLE__
151# undef EV_USE_POLL
152# undef EV_USE_KQUEUE
153#endif
154 170
155#ifndef CLOCK_MONOTONIC 171#ifndef CLOCK_MONOTONIC
156# undef EV_USE_MONOTONIC 172# undef EV_USE_MONOTONIC
157# define EV_USE_MONOTONIC 0 173# define EV_USE_MONOTONIC 0
158#endif 174#endif
168 184
169/**/ 185/**/
170 186
171#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) */
172#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) */
173#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
174/*#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 */
175 190
176#ifdef EV_H 191#ifdef EV_H
177# include EV_H 192# include EV_H
178#else 193#else
179# include "ev.h" 194# include "ev.h"
180#endif 195#endif
181 196
182#if __GNUC__ >= 3 197#if __GNUC__ >= 3
183# 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
184# define inline static inline 205# define inline_speed static inline
206# endif
185#else 207#else
186# define expect(expr,value) (expr) 208# define expect(expr,value) (expr)
209# define inline_speed static
187# define inline static 210# define inline_size static
211# define noinline
188#endif 212#endif
189 213
190#define expect_false(expr) expect ((expr) != 0, 0) 214#define expect_false(expr) expect ((expr) != 0, 0)
191#define expect_true(expr) expect ((expr) != 0, 1) 215#define expect_true(expr) expect ((expr) != 0, 1)
192 216
194#define ABSPRI(w) ((w)->priority - EV_MINPRI) 218#define ABSPRI(w) ((w)->priority - EV_MINPRI)
195 219
196#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 220#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
197#define EMPTY2(a,b) /* used to suppress some warnings */ 221#define EMPTY2(a,b) /* used to suppress some warnings */
198 222
199typedef struct ev_watcher *W; 223typedef ev_watcher *W;
200typedef struct ev_watcher_list *WL; 224typedef ev_watcher_list *WL;
201typedef struct ev_watcher_time *WT; 225typedef ev_watcher_time *WT;
202 226
203static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 227static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
204 228
205#ifdef _WIN32 229#ifdef _WIN32
206# include "ev_win32.c" 230# include "ev_win32.c"
208 232
209/*****************************************************************************/ 233/*****************************************************************************/
210 234
211static void (*syserr_cb)(const char *msg); 235static void (*syserr_cb)(const char *msg);
212 236
237void
213void ev_set_syserr_cb (void (*cb)(const char *msg)) 238ev_set_syserr_cb (void (*cb)(const char *msg))
214{ 239{
215 syserr_cb = cb; 240 syserr_cb = cb;
216} 241}
217 242
218static void 243static void noinline
219syserr (const char *msg) 244syserr (const char *msg)
220{ 245{
221 if (!msg) 246 if (!msg)
222 msg = "(libev) system error"; 247 msg = "(libev) system error";
223 248
230 } 255 }
231} 256}
232 257
233static void *(*alloc)(void *ptr, long size); 258static void *(*alloc)(void *ptr, long size);
234 259
260void
235void ev_set_allocator (void *(*cb)(void *ptr, long size)) 261ev_set_allocator (void *(*cb)(void *ptr, long size))
236{ 262{
237 alloc = cb; 263 alloc = cb;
238} 264}
239 265
240static void * 266static void *
312 gettimeofday (&tv, 0); 338 gettimeofday (&tv, 0);
313 return tv.tv_sec + tv.tv_usec * 1e-6; 339 return tv.tv_sec + tv.tv_usec * 1e-6;
314#endif 340#endif
315} 341}
316 342
317inline ev_tstamp 343ev_tstamp inline_size
318get_clock (void) 344get_clock (void)
319{ 345{
320#if EV_USE_MONOTONIC 346#if EV_USE_MONOTONIC
321 if (expect_true (have_monotonic)) 347 if (expect_true (have_monotonic))
322 { 348 {
365#define array_free(stem, idx) \ 391#define array_free(stem, idx) \
366 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;
367 393
368/*****************************************************************************/ 394/*****************************************************************************/
369 395
370static void 396void noinline
371anfds_init (ANFD *base, int count)
372{
373 while (count--)
374 {
375 base->head = 0;
376 base->events = EV_NONE;
377 base->reify = 0;
378
379 ++base;
380 }
381}
382
383void
384ev_feed_event (EV_P_ void *w, int revents) 397ev_feed_event (EV_P_ void *w, int revents)
385{ 398{
386 W w_ = (W)w; 399 W w_ = (W)w;
387 400
388 if (expect_false (w_->pending)) 401 if (expect_false (w_->pending))
395 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);
396 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 409 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
397 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 410 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
398} 411}
399 412
400static void 413void inline_size
401queue_events (EV_P_ W *events, int eventcnt, int type) 414queue_events (EV_P_ W *events, int eventcnt, int type)
402{ 415{
403 int i; 416 int i;
404 417
405 for (i = 0; i < eventcnt; ++i) 418 for (i = 0; i < eventcnt; ++i)
406 ev_feed_event (EV_A_ events [i], type); 419 ev_feed_event (EV_A_ events [i], type);
407} 420}
408 421
409inline 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
410fd_event (EV_P_ int fd, int revents) 438fd_event (EV_P_ int fd, int revents)
411{ 439{
412 ANFD *anfd = anfds + fd; 440 ANFD *anfd = anfds + fd;
413 struct ev_io *w; 441 ev_io *w;
414 442
415 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)
416 { 444 {
417 int ev = w->events & revents; 445 int ev = w->events & revents;
418 446
419 if (ev) 447 if (ev)
420 ev_feed_event (EV_A_ (W)w, ev); 448 ev_feed_event (EV_A_ (W)w, ev);
425ev_feed_fd_event (EV_P_ int fd, int revents) 453ev_feed_fd_event (EV_P_ int fd, int revents)
426{ 454{
427 fd_event (EV_A_ fd, revents); 455 fd_event (EV_A_ fd, revents);
428} 456}
429 457
430/*****************************************************************************/ 458void inline_size
431
432inline void
433fd_reify (EV_P) 459fd_reify (EV_P)
434{ 460{
435 int i; 461 int i;
436 462
437 for (i = 0; i < fdchangecnt; ++i) 463 for (i = 0; i < fdchangecnt; ++i)
438 { 464 {
439 int fd = fdchanges [i]; 465 int fd = fdchanges [i];
440 ANFD *anfd = anfds + fd; 466 ANFD *anfd = anfds + fd;
441 struct ev_io *w; 467 ev_io *w;
442 468
443 int events = 0; 469 int events = 0;
444 470
445 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)
446 events |= w->events; 472 events |= w->events;
447 473
448#if EV_SELECT_IS_WINSOCKET 474#if EV_SELECT_IS_WINSOCKET
449 if (events) 475 if (events)
450 { 476 {
454 } 480 }
455#endif 481#endif
456 482
457 anfd->reify = 0; 483 anfd->reify = 0;
458 484
459 method_modify (EV_A_ fd, anfd->events, events); 485 backend_modify (EV_A_ fd, anfd->events, events);
460 anfd->events = events; 486 anfd->events = events;
461 } 487 }
462 488
463 fdchangecnt = 0; 489 fdchangecnt = 0;
464} 490}
465 491
466static void 492void inline_size
467fd_change (EV_P_ int fd) 493fd_change (EV_P_ int fd)
468{ 494{
469 if (expect_false (anfds [fd].reify)) 495 if (expect_false (anfds [fd].reify))
470 return; 496 return;
471 497
474 ++fdchangecnt; 500 ++fdchangecnt;
475 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 501 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
476 fdchanges [fdchangecnt - 1] = fd; 502 fdchanges [fdchangecnt - 1] = fd;
477} 503}
478 504
479static void 505void inline_speed
480fd_kill (EV_P_ int fd) 506fd_kill (EV_P_ int fd)
481{ 507{
482 struct ev_io *w; 508 ev_io *w;
483 509
484 while ((w = (struct ev_io *)anfds [fd].head)) 510 while ((w = (ev_io *)anfds [fd].head))
485 { 511 {
486 ev_io_stop (EV_A_ w); 512 ev_io_stop (EV_A_ w);
487 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);
488 } 514 }
489} 515}
490 516
491inline int 517int inline_size
492fd_valid (int fd) 518fd_valid (int fd)
493{ 519{
494#ifdef _WIN32 520#ifdef _WIN32
495 return _get_osfhandle (fd) != -1; 521 return _get_osfhandle (fd) != -1;
496#else 522#else
497 return fcntl (fd, F_GETFD) != -1; 523 return fcntl (fd, F_GETFD) != -1;
498#endif 524#endif
499} 525}
500 526
501/* called on EBADF to verify fds */ 527/* called on EBADF to verify fds */
502static void 528static void noinline
503fd_ebadf (EV_P) 529fd_ebadf (EV_P)
504{ 530{
505 int fd; 531 int fd;
506 532
507 for (fd = 0; fd < anfdmax; ++fd) 533 for (fd = 0; fd < anfdmax; ++fd)
509 if (!fd_valid (fd) == -1 && errno == EBADF) 535 if (!fd_valid (fd) == -1 && errno == EBADF)
510 fd_kill (EV_A_ fd); 536 fd_kill (EV_A_ fd);
511} 537}
512 538
513/* 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 */
514static void 540static void noinline
515fd_enomem (EV_P) 541fd_enomem (EV_P)
516{ 542{
517 int fd; 543 int fd;
518 544
519 for (fd = anfdmax; fd--; ) 545 for (fd = anfdmax; fd--; )
522 fd_kill (EV_A_ fd); 548 fd_kill (EV_A_ fd);
523 return; 549 return;
524 } 550 }
525} 551}
526 552
527/* 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 */
528static void 554static void noinline
529fd_rearm_all (EV_P) 555fd_rearm_all (EV_P)
530{ 556{
531 int fd; 557 int fd;
532 558
533 /* 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 */
539 } 565 }
540} 566}
541 567
542/*****************************************************************************/ 568/*****************************************************************************/
543 569
544static void 570void inline_speed
545upheap (WT *heap, int k) 571upheap (WT *heap, int k)
546{ 572{
547 WT w = heap [k]; 573 WT w = heap [k];
548 574
549 while (k && heap [k >> 1]->at > w->at) 575 while (k && heap [k >> 1]->at > w->at)
556 heap [k] = w; 582 heap [k] = w;
557 ((W)heap [k])->active = k + 1; 583 ((W)heap [k])->active = k + 1;
558 584
559} 585}
560 586
561static void 587void inline_speed
562downheap (WT *heap, int N, int k) 588downheap (WT *heap, int N, int k)
563{ 589{
564 WT w = heap [k]; 590 WT w = heap [k];
565 591
566 while (k < (N >> 1)) 592 while (k < (N >> 1))
580 606
581 heap [k] = w; 607 heap [k] = w;
582 ((W)heap [k])->active = k + 1; 608 ((W)heap [k])->active = k + 1;
583} 609}
584 610
585inline void 611void inline_size
586adjustheap (WT *heap, int N, int k) 612adjustheap (WT *heap, int N, int k)
587{ 613{
588 upheap (heap, k); 614 upheap (heap, k);
589 downheap (heap, N, k); 615 downheap (heap, N, k);
590} 616}
600static ANSIG *signals; 626static ANSIG *signals;
601static int signalmax; 627static int signalmax;
602 628
603static int sigpipe [2]; 629static int sigpipe [2];
604static sig_atomic_t volatile gotsig; 630static sig_atomic_t volatile gotsig;
605static struct ev_io sigev; 631static ev_io sigev;
606 632
607static void 633void inline_size
608signals_init (ANSIG *base, int count) 634signals_init (ANSIG *base, int count)
609{ 635{
610 while (count--) 636 while (count--)
611 { 637 {
612 base->head = 0; 638 base->head = 0;
632 write (sigpipe [1], &signum, 1); 658 write (sigpipe [1], &signum, 1);
633 errno = old_errno; 659 errno = old_errno;
634 } 660 }
635} 661}
636 662
637void 663void noinline
638ev_feed_signal_event (EV_P_ int signum) 664ev_feed_signal_event (EV_P_ int signum)
639{ 665{
640 WL w; 666 WL w;
641 667
642#if EV_MULTIPLICITY 668#if EV_MULTIPLICITY
653 for (w = signals [signum].head; w; w = w->next) 679 for (w = signals [signum].head; w; w = w->next)
654 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 680 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
655} 681}
656 682
657static void 683static void
658sigcb (EV_P_ struct ev_io *iow, int revents) 684sigcb (EV_P_ ev_io *iow, int revents)
659{ 685{
660 int signum; 686 int signum;
661 687
662 read (sigpipe [0], &revents, 1); 688 read (sigpipe [0], &revents, 1);
663 gotsig = 0; 689 gotsig = 0;
665 for (signum = signalmax; signum--; ) 691 for (signum = signalmax; signum--; )
666 if (signals [signum].gotsig) 692 if (signals [signum].gotsig)
667 ev_feed_signal_event (EV_A_ signum + 1); 693 ev_feed_signal_event (EV_A_ signum + 1);
668} 694}
669 695
670static void 696void inline_size
671fd_intern (int fd) 697fd_intern (int fd)
672{ 698{
673#ifdef _WIN32 699#ifdef _WIN32
674 int arg = 1; 700 int arg = 1;
675 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 701 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
677 fcntl (fd, F_SETFD, FD_CLOEXEC); 703 fcntl (fd, F_SETFD, FD_CLOEXEC);
678 fcntl (fd, F_SETFL, O_NONBLOCK); 704 fcntl (fd, F_SETFL, O_NONBLOCK);
679#endif 705#endif
680} 706}
681 707
682static void 708static void noinline
683siginit (EV_P) 709siginit (EV_P)
684{ 710{
685 fd_intern (sigpipe [0]); 711 fd_intern (sigpipe [0]);
686 fd_intern (sigpipe [1]); 712 fd_intern (sigpipe [1]);
687 713
690 ev_unref (EV_A); /* child watcher should not keep loop alive */ 716 ev_unref (EV_A); /* child watcher should not keep loop alive */
691} 717}
692 718
693/*****************************************************************************/ 719/*****************************************************************************/
694 720
695static struct ev_child *childs [PID_HASHSIZE]; 721static ev_child *childs [EV_PID_HASHSIZE];
696 722
697#ifndef _WIN32 723#ifndef _WIN32
698 724
699static struct ev_signal childev; 725static ev_signal childev;
700 726
701#ifndef WCONTINUED 727void inline_speed
702# define WCONTINUED 0
703#endif
704
705static void
706child_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)
707{ 729{
708 struct ev_child *w; 730 ev_child *w;
709 731
710 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)
711 if (w->pid == pid || !w->pid) 733 if (w->pid == pid || !w->pid)
712 { 734 {
713 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 735 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
714 w->rpid = pid; 736 w->rpid = pid;
715 w->rstatus = status; 737 w->rstatus = status;
716 ev_feed_event (EV_A_ (W)w, EV_CHILD); 738 ev_feed_event (EV_A_ (W)w, EV_CHILD);
717 } 739 }
718} 740}
719 741
742#ifndef WCONTINUED
743# define WCONTINUED 0
744#endif
745
720static void 746static void
721childcb (EV_P_ struct ev_signal *sw, int revents) 747childcb (EV_P_ ev_signal *sw, int revents)
722{ 748{
723 int pid, status; 749 int pid, status;
724 750
751 /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
725 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 752 if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
726 { 753 if (!WCONTINUED
754 || errno != EINVAL
755 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
756 return;
757
727 /* 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 */
728 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 760 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
729 761
730 child_reap (EV_A_ sw, pid, pid, status); 762 child_reap (EV_A_ sw, pid, pid, status);
763 if (EV_PID_HASHSIZE > 1)
731 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 */
732 }
733} 765}
734 766
735#endif 767#endif
736 768
737/*****************************************************************************/ 769/*****************************************************************************/
763{ 795{
764 return EV_VERSION_MINOR; 796 return EV_VERSION_MINOR;
765} 797}
766 798
767/* 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 */
768static int 800int inline_size
769enable_secure (void) 801enable_secure (void)
770{ 802{
771#ifdef _WIN32 803#ifdef _WIN32
772 return 0; 804 return 0;
773#else 805#else
775 || getgid () != getegid (); 807 || getgid () != getegid ();
776#endif 808#endif
777} 809}
778 810
779unsigned int 811unsigned int
780ev_method (EV_P) 812ev_supported_backends (void)
781{ 813{
782 return method; 814 unsigned int flags = 0;
815
816 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
817 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
818 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
819 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
820 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
821
822 return flags;
823}
824
825unsigned int
826ev_recommended_backends (void)
827{
828 unsigned int flags = ev_supported_backends ();
829
830#ifndef __NetBSD__
831 /* kqueue is borked on everything but netbsd apparently */
832 /* it usually doesn't work correctly on anything but sockets and pipes */
833 flags &= ~EVBACKEND_KQUEUE;
834#endif
835#ifdef __APPLE__
836 // flags &= ~EVBACKEND_KQUEUE; for documentation
837 flags &= ~EVBACKEND_POLL;
838#endif
839
840 return flags;
841}
842
843unsigned int
844ev_embeddable_backends (void)
845{
846 return EVBACKEND_EPOLL
847 | EVBACKEND_KQUEUE
848 | EVBACKEND_PORT;
849}
850
851unsigned int
852ev_backend (EV_P)
853{
854 return backend;
783} 855}
784 856
785static void 857static void
786loop_init (EV_P_ unsigned int flags) 858loop_init (EV_P_ unsigned int flags)
787{ 859{
788 if (!method) 860 if (!backend)
789 { 861 {
790#if EV_USE_MONOTONIC 862#if EV_USE_MONOTONIC
791 { 863 {
792 struct timespec ts; 864 struct timespec ts;
793 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 865 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
798 ev_rt_now = ev_time (); 870 ev_rt_now = ev_time ();
799 mn_now = get_clock (); 871 mn_now = get_clock ();
800 now_floor = mn_now; 872 now_floor = mn_now;
801 rtmn_diff = ev_rt_now - mn_now; 873 rtmn_diff = ev_rt_now - mn_now;
802 874
803 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) 875 if (!(flags & EVFLAG_NOENV)
876 && !enable_secure ()
877 && getenv ("LIBEV_FLAGS"))
804 flags = atoi (getenv ("LIBEV_FLAGS")); 878 flags = atoi (getenv ("LIBEV_FLAGS"));
805 879
806 if (!(flags & 0x0000ffff)) 880 if (!(flags & 0x0000ffffUL))
807 flags |= 0x0000ffff; 881 flags |= ev_recommended_backends ();
808 882
809 method = 0; 883 backend = 0;
810#if EV_USE_PORT 884#if EV_USE_PORT
811 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); 885 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
812#endif 886#endif
813#if EV_USE_KQUEUE 887#if EV_USE_KQUEUE
814 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 888 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
815#endif 889#endif
816#if EV_USE_EPOLL 890#if EV_USE_EPOLL
817 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 891 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
818#endif 892#endif
819#if EV_USE_POLL 893#if EV_USE_POLL
820 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); 894 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
821#endif 895#endif
822#if EV_USE_SELECT 896#if EV_USE_SELECT
823 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); 897 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
824#endif 898#endif
825 899
826 ev_init (&sigev, sigcb); 900 ev_init (&sigev, sigcb);
827 ev_set_priority (&sigev, EV_MAXPRI); 901 ev_set_priority (&sigev, EV_MAXPRI);
828 } 902 }
832loop_destroy (EV_P) 906loop_destroy (EV_P)
833{ 907{
834 int i; 908 int i;
835 909
836#if EV_USE_PORT 910#if EV_USE_PORT
837 if (method == EVMETHOD_PORT ) port_destroy (EV_A); 911 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
838#endif 912#endif
839#if EV_USE_KQUEUE 913#if EV_USE_KQUEUE
840 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 914 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
841#endif 915#endif
842#if EV_USE_EPOLL 916#if EV_USE_EPOLL
843 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 917 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
844#endif 918#endif
845#if EV_USE_POLL 919#if EV_USE_POLL
846 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 920 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
847#endif 921#endif
848#if EV_USE_SELECT 922#if EV_USE_SELECT
849 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 923 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
850#endif 924#endif
851 925
852 for (i = NUMPRI; i--; ) 926 for (i = NUMPRI; i--; )
853 array_free (pending, [i]); 927 array_free (pending, [i]);
854 928
855 /* have to use the microsoft-never-gets-it-right macro */ 929 /* have to use the microsoft-never-gets-it-right macro */
856 array_free (fdchange, EMPTY0); 930 array_free (fdchange, EMPTY0);
857 array_free (timer, EMPTY0); 931 array_free (timer, EMPTY0);
858#if EV_PERIODICS 932#if EV_PERIODIC_ENABLE
859 array_free (periodic, EMPTY0); 933 array_free (periodic, EMPTY0);
860#endif 934#endif
861 array_free (idle, EMPTY0); 935 array_free (idle, EMPTY0);
862 array_free (prepare, EMPTY0); 936 array_free (prepare, EMPTY0);
863 array_free (check, EMPTY0); 937 array_free (check, EMPTY0);
864 938
865 method = 0; 939 backend = 0;
866} 940}
867 941
868static void 942static void
869loop_fork (EV_P) 943loop_fork (EV_P)
870{ 944{
871#if EV_USE_PORT 945#if EV_USE_PORT
872 if (method == EVMETHOD_PORT ) port_fork (EV_A); 946 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
873#endif 947#endif
874#if EV_USE_KQUEUE 948#if EV_USE_KQUEUE
875 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 949 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
876#endif 950#endif
877#if EV_USE_EPOLL 951#if EV_USE_EPOLL
878 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 952 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
879#endif 953#endif
880 954
881 if (ev_is_active (&sigev)) 955 if (ev_is_active (&sigev))
882 { 956 {
883 /* default loop */ 957 /* default loop */
904 978
905 memset (loop, 0, sizeof (struct ev_loop)); 979 memset (loop, 0, sizeof (struct ev_loop));
906 980
907 loop_init (EV_A_ flags); 981 loop_init (EV_A_ flags);
908 982
909 if (ev_method (EV_A)) 983 if (ev_backend (EV_A))
910 return loop; 984 return loop;
911 985
912 return 0; 986 return 0;
913} 987}
914 988
947 ev_default_loop_ptr = 1; 1021 ev_default_loop_ptr = 1;
948#endif 1022#endif
949 1023
950 loop_init (EV_A_ flags); 1024 loop_init (EV_A_ flags);
951 1025
952 if (ev_method (EV_A)) 1026 if (ev_backend (EV_A))
953 { 1027 {
954 siginit (EV_A); 1028 siginit (EV_A);
955 1029
956#ifndef _WIN32 1030#ifndef _WIN32
957 ev_signal_init (&childev, childcb, SIGCHLD); 1031 ev_signal_init (&childev, childcb, SIGCHLD);
993{ 1067{
994#if EV_MULTIPLICITY 1068#if EV_MULTIPLICITY
995 struct ev_loop *loop = ev_default_loop_ptr; 1069 struct ev_loop *loop = ev_default_loop_ptr;
996#endif 1070#endif
997 1071
998 if (method) 1072 if (backend)
999 postfork = 1; 1073 postfork = 1;
1000} 1074}
1001 1075
1002/*****************************************************************************/ 1076/*****************************************************************************/
1003 1077
1004static int 1078int inline_size
1005any_pending (EV_P) 1079any_pending (EV_P)
1006{ 1080{
1007 int pri; 1081 int pri;
1008 1082
1009 for (pri = NUMPRI; pri--; ) 1083 for (pri = NUMPRI; pri--; )
1011 return 1; 1085 return 1;
1012 1086
1013 return 0; 1087 return 0;
1014} 1088}
1015 1089
1016inline void 1090void inline_speed
1017call_pending (EV_P) 1091call_pending (EV_P)
1018{ 1092{
1019 int pri; 1093 int pri;
1020 1094
1021 for (pri = NUMPRI; pri--; ) 1095 for (pri = NUMPRI; pri--; )
1023 { 1097 {
1024 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1098 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1025 1099
1026 if (expect_true (p->w)) 1100 if (expect_true (p->w))
1027 { 1101 {
1102 assert (("non-pending watcher on pending list", p->w->pending));
1103
1028 p->w->pending = 0; 1104 p->w->pending = 0;
1029 EV_CB_INVOKE (p->w, p->events); 1105 EV_CB_INVOKE (p->w, p->events);
1030 } 1106 }
1031 } 1107 }
1032} 1108}
1033 1109
1034inline void 1110void inline_size
1035timers_reify (EV_P) 1111timers_reify (EV_P)
1036{ 1112{
1037 while (timercnt && ((WT)timers [0])->at <= mn_now) 1113 while (timercnt && ((WT)timers [0])->at <= mn_now)
1038 { 1114 {
1039 struct ev_timer *w = timers [0]; 1115 ev_timer *w = timers [0];
1040 1116
1041 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1117 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1042 1118
1043 /* first reschedule or stop timer */ 1119 /* first reschedule or stop timer */
1044 if (w->repeat) 1120 if (w->repeat)
1056 1132
1057 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1133 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1058 } 1134 }
1059} 1135}
1060 1136
1061#if EV_PERIODICS 1137#if EV_PERIODIC_ENABLE
1062inline void 1138void inline_size
1063periodics_reify (EV_P) 1139periodics_reify (EV_P)
1064{ 1140{
1065 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1141 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1066 { 1142 {
1067 struct ev_periodic *w = periodics [0]; 1143 ev_periodic *w = periodics [0];
1068 1144
1069 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1145 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1070 1146
1071 /* first reschedule or stop timer */ 1147 /* first reschedule or stop timer */
1072 if (w->reschedule_cb) 1148 if (w->reschedule_cb)
1086 1162
1087 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1163 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1088 } 1164 }
1089} 1165}
1090 1166
1091static void 1167static void noinline
1092periodics_reschedule (EV_P) 1168periodics_reschedule (EV_P)
1093{ 1169{
1094 int i; 1170 int i;
1095 1171
1096 /* adjust periodics after time jump */ 1172 /* adjust periodics after time jump */
1097 for (i = 0; i < periodiccnt; ++i) 1173 for (i = 0; i < periodiccnt; ++i)
1098 { 1174 {
1099 struct ev_periodic *w = periodics [i]; 1175 ev_periodic *w = periodics [i];
1100 1176
1101 if (w->reschedule_cb) 1177 if (w->reschedule_cb)
1102 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1178 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1103 else if (w->interval) 1179 else if (w->interval)
1104 ((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;
1108 for (i = periodiccnt >> 1; i--; ) 1184 for (i = periodiccnt >> 1; i--; )
1109 downheap ((WT *)periodics, periodiccnt, i); 1185 downheap ((WT *)periodics, periodiccnt, i);
1110} 1186}
1111#endif 1187#endif
1112 1188
1113inline int 1189int inline_size
1114time_update_monotonic (EV_P) 1190time_update_monotonic (EV_P)
1115{ 1191{
1116 mn_now = get_clock (); 1192 mn_now = get_clock ();
1117 1193
1118 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1194 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1126 ev_rt_now = ev_time (); 1202 ev_rt_now = ev_time ();
1127 return 1; 1203 return 1;
1128 } 1204 }
1129} 1205}
1130 1206
1131inline void 1207void inline_size
1132time_update (EV_P) 1208time_update (EV_P)
1133{ 1209{
1134 int i; 1210 int i;
1135 1211
1136#if EV_USE_MONOTONIC 1212#if EV_USE_MONOTONIC
1138 { 1214 {
1139 if (time_update_monotonic (EV_A)) 1215 if (time_update_monotonic (EV_A))
1140 { 1216 {
1141 ev_tstamp odiff = rtmn_diff; 1217 ev_tstamp odiff = rtmn_diff;
1142 1218
1143 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; )
1144 { 1228 {
1145 rtmn_diff = ev_rt_now - mn_now; 1229 rtmn_diff = ev_rt_now - mn_now;
1146 1230
1147 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1231 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1148 return; /* all is well */ 1232 return; /* all is well */
1150 ev_rt_now = ev_time (); 1234 ev_rt_now = ev_time ();
1151 mn_now = get_clock (); 1235 mn_now = get_clock ();
1152 now_floor = mn_now; 1236 now_floor = mn_now;
1153 } 1237 }
1154 1238
1155# if EV_PERIODICS 1239# if EV_PERIODIC_ENABLE
1156 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1157# endif 1241# endif
1158 /* no timer adjustment, as the monotonic clock doesn't jump */ 1242 /* no timer adjustment, as the monotonic clock doesn't jump */
1159 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1243 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1160 } 1244 }
1164 { 1248 {
1165 ev_rt_now = ev_time (); 1249 ev_rt_now = ev_time ();
1166 1250
1167 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))
1168 { 1252 {
1169#if EV_PERIODICS 1253#if EV_PERIODIC_ENABLE
1170 periodics_reschedule (EV_A); 1254 periodics_reschedule (EV_A);
1171#endif 1255#endif
1172 1256
1173 /* 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 */
1174 for (i = 0; i < timercnt; ++i) 1258 for (i = 0; i < timercnt; ++i)
1194static int loop_done; 1278static int loop_done;
1195 1279
1196void 1280void
1197ev_loop (EV_P_ int flags) 1281ev_loop (EV_P_ int flags)
1198{ 1282{
1199 double block;
1200 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1283 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1284 ? EVUNLOOP_ONE
1285 : EVUNLOOP_CANCEL;
1201 1286
1202 while (activecnt) 1287 while (activecnt)
1203 { 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
1204 /* queue check watchers (and execute them) */ 1299 /* queue check watchers (and execute them) */
1205 if (expect_false (preparecnt)) 1300 if (expect_false (preparecnt))
1206 { 1301 {
1207 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1302 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1208 call_pending (EV_A); 1303 call_pending (EV_A);
1214 1309
1215 /* update fd-related kernel structures */ 1310 /* update fd-related kernel structures */
1216 fd_reify (EV_A); 1311 fd_reify (EV_A);
1217 1312
1218 /* calculate blocking time */ 1313 /* calculate blocking time */
1314 {
1315 double block;
1219 1316
1220 /* we only need this for !monotonic clock or timers, but as we basically 1317 if (flags & EVLOOP_NONBLOCK || idlecnt)
1221 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 */
1222#if EV_USE_MONOTONIC 1322#if EV_USE_MONOTONIC
1223 if (expect_true (have_monotonic)) 1323 if (expect_true (have_monotonic))
1224 time_update_monotonic (EV_A); 1324 time_update_monotonic (EV_A);
1225 else 1325 else
1226#endif 1326#endif
1227 { 1327 {
1228 ev_rt_now = ev_time (); 1328 ev_rt_now = ev_time ();
1229 mn_now = ev_rt_now; 1329 mn_now = ev_rt_now;
1230 } 1330 }
1231 1331
1232 if (flags & EVLOOP_NONBLOCK || idlecnt)
1233 block = 0.;
1234 else
1235 {
1236 block = MAX_BLOCKTIME; 1332 block = MAX_BLOCKTIME;
1237 1333
1238 if (timercnt) 1334 if (timercnt)
1239 { 1335 {
1240 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1336 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1241 if (block > to) block = to; 1337 if (block > to) block = to;
1242 } 1338 }
1243 1339
1244#if EV_PERIODICS 1340#if EV_PERIODIC_ENABLE
1245 if (periodiccnt) 1341 if (periodiccnt)
1246 { 1342 {
1247 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;
1248 if (block > to) block = to; 1344 if (block > to) block = to;
1249 } 1345 }
1250#endif 1346#endif
1251 1347
1252 if (expect_false (block < 0.)) block = 0.; 1348 if (expect_false (block < 0.)) block = 0.;
1253 } 1349 }
1254 1350
1255 method_poll (EV_A_ block); 1351 backend_poll (EV_A_ block);
1352 }
1256 1353
1257 /* update ev_rt_now, do magic */ 1354 /* update ev_rt_now, do magic */
1258 time_update (EV_A); 1355 time_update (EV_A);
1259 1356
1260 /* queue pending timers and reschedule them */ 1357 /* queue pending timers and reschedule them */
1261 timers_reify (EV_A); /* relative timers called last */ 1358 timers_reify (EV_A); /* relative timers called last */
1262#if EV_PERIODICS 1359#if EV_PERIODIC_ENABLE
1263 periodics_reify (EV_A); /* absolute timers called first */ 1360 periodics_reify (EV_A); /* absolute timers called first */
1264#endif 1361#endif
1265 1362
1266 /* queue idle watchers unless io or timers are pending */ 1363 /* queue idle watchers unless other events are pending */
1267 if (idlecnt && !any_pending (EV_A)) 1364 if (idlecnt && !any_pending (EV_A))
1268 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1365 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1269 1366
1270 /* queue check watchers, to be executed first */ 1367 /* queue check watchers, to be executed first */
1271 if (expect_false (checkcnt)) 1368 if (expect_false (checkcnt))
1275 1372
1276 if (expect_false (loop_done)) 1373 if (expect_false (loop_done))
1277 break; 1374 break;
1278 } 1375 }
1279 1376
1280 if (loop_done != 2) 1377 if (loop_done == EVUNLOOP_ONE)
1281 loop_done = 0; 1378 loop_done = EVUNLOOP_CANCEL;
1282} 1379}
1283 1380
1284void 1381void
1285ev_unloop (EV_P_ int how) 1382ev_unloop (EV_P_ int how)
1286{ 1383{
1287 loop_done = how; 1384 loop_done = how;
1288} 1385}
1289 1386
1290/*****************************************************************************/ 1387/*****************************************************************************/
1291 1388
1292inline void 1389void inline_size
1293wlist_add (WL *head, WL elem) 1390wlist_add (WL *head, WL elem)
1294{ 1391{
1295 elem->next = *head; 1392 elem->next = *head;
1296 *head = elem; 1393 *head = elem;
1297} 1394}
1298 1395
1299inline void 1396void inline_size
1300wlist_del (WL *head, WL elem) 1397wlist_del (WL *head, WL elem)
1301{ 1398{
1302 while (*head) 1399 while (*head)
1303 { 1400 {
1304 if (*head == elem) 1401 if (*head == elem)
1309 1406
1310 head = &(*head)->next; 1407 head = &(*head)->next;
1311 } 1408 }
1312} 1409}
1313 1410
1314inline void 1411void inline_speed
1315ev_clear_pending (EV_P_ W w) 1412ev_clear_pending (EV_P_ W w)
1316{ 1413{
1317 if (w->pending) 1414 if (w->pending)
1318 { 1415 {
1319 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1416 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1320 w->pending = 0; 1417 w->pending = 0;
1321 } 1418 }
1322} 1419}
1323 1420
1324inline void 1421void inline_speed
1325ev_start (EV_P_ W w, int active) 1422ev_start (EV_P_ W w, int active)
1326{ 1423{
1327 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1424 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1328 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1425 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1329 1426
1330 w->active = active; 1427 w->active = active;
1331 ev_ref (EV_A); 1428 ev_ref (EV_A);
1332} 1429}
1333 1430
1334inline void 1431void inline_size
1335ev_stop (EV_P_ W w) 1432ev_stop (EV_P_ W w)
1336{ 1433{
1337 ev_unref (EV_A); 1434 ev_unref (EV_A);
1338 w->active = 0; 1435 w->active = 0;
1339} 1436}
1340 1437
1341/*****************************************************************************/ 1438/*****************************************************************************/
1342 1439
1343void 1440void
1344ev_io_start (EV_P_ struct ev_io *w) 1441ev_io_start (EV_P_ ev_io *w)
1345{ 1442{
1346 int fd = w->fd; 1443 int fd = w->fd;
1347 1444
1348 if (expect_false (ev_is_active (w))) 1445 if (expect_false (ev_is_active (w)))
1349 return; 1446 return;
1356 1453
1357 fd_change (EV_A_ fd); 1454 fd_change (EV_A_ fd);
1358} 1455}
1359 1456
1360void 1457void
1361ev_io_stop (EV_P_ struct ev_io *w) 1458ev_io_stop (EV_P_ ev_io *w)
1362{ 1459{
1363 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1364 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1365 return; 1462 return;
1366 1463
1371 1468
1372 fd_change (EV_A_ w->fd); 1469 fd_change (EV_A_ w->fd);
1373} 1470}
1374 1471
1375void 1472void
1376ev_timer_start (EV_P_ struct ev_timer *w) 1473ev_timer_start (EV_P_ ev_timer *w)
1377{ 1474{
1378 if (expect_false (ev_is_active (w))) 1475 if (expect_false (ev_is_active (w)))
1379 return; 1476 return;
1380 1477
1381 ((WT)w)->at += mn_now; 1478 ((WT)w)->at += mn_now;
1382 1479
1383 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.));
1384 1481
1385 ev_start (EV_A_ (W)w, ++timercnt); 1482 ev_start (EV_A_ (W)w, ++timercnt);
1386 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1483 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1387 timers [timercnt - 1] = w; 1484 timers [timercnt - 1] = w;
1388 upheap ((WT *)timers, timercnt - 1); 1485 upheap ((WT *)timers, timercnt - 1);
1389 1486
1390 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1487 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1391} 1488}
1392 1489
1393void 1490void
1394ev_timer_stop (EV_P_ struct ev_timer *w) 1491ev_timer_stop (EV_P_ ev_timer *w)
1395{ 1492{
1396 ev_clear_pending (EV_A_ (W)w); 1493 ev_clear_pending (EV_A_ (W)w);
1397 if (expect_false (!ev_is_active (w))) 1494 if (expect_false (!ev_is_active (w)))
1398 return; 1495 return;
1399 1496
1409 1506
1410 ev_stop (EV_A_ (W)w); 1507 ev_stop (EV_A_ (W)w);
1411} 1508}
1412 1509
1413void 1510void
1414ev_timer_again (EV_P_ struct ev_timer *w) 1511ev_timer_again (EV_P_ ev_timer *w)
1415{ 1512{
1416 if (ev_is_active (w)) 1513 if (ev_is_active (w))
1417 { 1514 {
1418 if (w->repeat) 1515 if (w->repeat)
1419 { 1516 {
1428 w->at = w->repeat; 1525 w->at = w->repeat;
1429 ev_timer_start (EV_A_ w); 1526 ev_timer_start (EV_A_ w);
1430 } 1527 }
1431} 1528}
1432 1529
1433#if EV_PERIODICS 1530#if EV_PERIODIC_ENABLE
1434void 1531void
1435ev_periodic_start (EV_P_ struct ev_periodic *w) 1532ev_periodic_start (EV_P_ ev_periodic *w)
1436{ 1533{
1437 if (expect_false (ev_is_active (w))) 1534 if (expect_false (ev_is_active (w)))
1438 return; 1535 return;
1439 1536
1440 if (w->reschedule_cb) 1537 if (w->reschedule_cb)
1445 /* 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 */
1446 ((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;
1447 } 1544 }
1448 1545
1449 ev_start (EV_A_ (W)w, ++periodiccnt); 1546 ev_start (EV_A_ (W)w, ++periodiccnt);
1450 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1547 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1451 periodics [periodiccnt - 1] = w; 1548 periodics [periodiccnt - 1] = w;
1452 upheap ((WT *)periodics, periodiccnt - 1); 1549 upheap ((WT *)periodics, periodiccnt - 1);
1453 1550
1454 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1551 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1455} 1552}
1456 1553
1457void 1554void
1458ev_periodic_stop (EV_P_ struct ev_periodic *w) 1555ev_periodic_stop (EV_P_ ev_periodic *w)
1459{ 1556{
1460 ev_clear_pending (EV_A_ (W)w); 1557 ev_clear_pending (EV_A_ (W)w);
1461 if (expect_false (!ev_is_active (w))) 1558 if (expect_false (!ev_is_active (w)))
1462 return; 1559 return;
1463 1560
1471 1568
1472 ev_stop (EV_A_ (W)w); 1569 ev_stop (EV_A_ (W)w);
1473} 1570}
1474 1571
1475void 1572void
1476ev_periodic_again (EV_P_ struct ev_periodic *w) 1573ev_periodic_again (EV_P_ ev_periodic *w)
1477{ 1574{
1478 /* TODO: use adjustheap and recalculation */ 1575 /* TODO: use adjustheap and recalculation */
1479 ev_periodic_stop (EV_A_ w); 1576 ev_periodic_stop (EV_A_ w);
1480 ev_periodic_start (EV_A_ w); 1577 ev_periodic_start (EV_A_ w);
1481} 1578}
1482#endif 1579#endif
1483 1580
1484void
1485ev_idle_start (EV_P_ struct ev_idle *w)
1486{
1487 if (expect_false (ev_is_active (w)))
1488 return;
1489
1490 ev_start (EV_A_ (W)w, ++idlecnt);
1491 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1492 idles [idlecnt - 1] = w;
1493}
1494
1495void
1496ev_idle_stop (EV_P_ struct ev_idle *w)
1497{
1498 ev_clear_pending (EV_A_ (W)w);
1499 if (expect_false (!ev_is_active (w)))
1500 return;
1501
1502 idles [((W)w)->active - 1] = idles [--idlecnt];
1503 ev_stop (EV_A_ (W)w);
1504}
1505
1506void
1507ev_prepare_start (EV_P_ struct ev_prepare *w)
1508{
1509 if (expect_false (ev_is_active (w)))
1510 return;
1511
1512 ev_start (EV_A_ (W)w, ++preparecnt);
1513 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1514 prepares [preparecnt - 1] = w;
1515}
1516
1517void
1518ev_prepare_stop (EV_P_ struct ev_prepare *w)
1519{
1520 ev_clear_pending (EV_A_ (W)w);
1521 if (expect_false (!ev_is_active (w)))
1522 return;
1523
1524 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1525 ev_stop (EV_A_ (W)w);
1526}
1527
1528void
1529ev_check_start (EV_P_ struct ev_check *w)
1530{
1531 if (expect_false (ev_is_active (w)))
1532 return;
1533
1534 ev_start (EV_A_ (W)w, ++checkcnt);
1535 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1536 checks [checkcnt - 1] = w;
1537}
1538
1539void
1540ev_check_stop (EV_P_ struct ev_check *w)
1541{
1542 ev_clear_pending (EV_A_ (W)w);
1543 if (expect_false (!ev_is_active (w)))
1544 return;
1545
1546 checks [((W)w)->active - 1] = checks [--checkcnt];
1547 ev_stop (EV_A_ (W)w);
1548}
1549
1550#ifndef SA_RESTART 1581#ifndef SA_RESTART
1551# define SA_RESTART 0 1582# define SA_RESTART 0
1552#endif 1583#endif
1553 1584
1554void 1585void
1555ev_signal_start (EV_P_ struct ev_signal *w) 1586ev_signal_start (EV_P_ ev_signal *w)
1556{ 1587{
1557#if EV_MULTIPLICITY 1588#if EV_MULTIPLICITY
1558 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));
1559#endif 1590#endif
1560 if (expect_false (ev_is_active (w))) 1591 if (expect_false (ev_is_active (w)))
1579#endif 1610#endif
1580 } 1611 }
1581} 1612}
1582 1613
1583void 1614void
1584ev_signal_stop (EV_P_ struct ev_signal *w) 1615ev_signal_stop (EV_P_ ev_signal *w)
1585{ 1616{
1586 ev_clear_pending (EV_A_ (W)w); 1617 ev_clear_pending (EV_A_ (W)w);
1587 if (expect_false (!ev_is_active (w))) 1618 if (expect_false (!ev_is_active (w)))
1588 return; 1619 return;
1589 1620
1593 if (!signals [w->signum - 1].head) 1624 if (!signals [w->signum - 1].head)
1594 signal (w->signum, SIG_DFL); 1625 signal (w->signum, SIG_DFL);
1595} 1626}
1596 1627
1597void 1628void
1598ev_child_start (EV_P_ struct ev_child *w) 1629ev_child_start (EV_P_ ev_child *w)
1599{ 1630{
1600#if EV_MULTIPLICITY 1631#if EV_MULTIPLICITY
1601 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));
1602#endif 1633#endif
1603 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1604 return; 1635 return;
1605 1636
1606 ev_start (EV_A_ (W)w, 1); 1637 ev_start (EV_A_ (W)w, 1);
1607 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1638 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1608} 1639}
1609 1640
1610void 1641void
1611ev_child_stop (EV_P_ struct ev_child *w) 1642ev_child_stop (EV_P_ ev_child *w)
1612{ 1643{
1613 ev_clear_pending (EV_A_ (W)w); 1644 ev_clear_pending (EV_A_ (W)w);
1614 if (expect_false (!ev_is_active (w))) 1645 if (expect_false (!ev_is_active (w)))
1615 return; 1646 return;
1616 1647
1617 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1648 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1618 ev_stop (EV_A_ (W)w); 1649 ev_stop (EV_A_ (W)w);
1619} 1650}
1620 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
1621/*****************************************************************************/ 1879/*****************************************************************************/
1622 1880
1623struct ev_once 1881struct ev_once
1624{ 1882{
1625 struct ev_io io; 1883 ev_io io;
1626 struct ev_timer to; 1884 ev_timer to;
1627 void (*cb)(int revents, void *arg); 1885 void (*cb)(int revents, void *arg);
1628 void *arg; 1886 void *arg;
1629}; 1887};
1630 1888
1631static void 1889static void
1640 1898
1641 cb (revents, arg); 1899 cb (revents, arg);
1642} 1900}
1643 1901
1644static void 1902static void
1645once_cb_io (EV_P_ struct ev_io *w, int revents) 1903once_cb_io (EV_P_ ev_io *w, int revents)
1646{ 1904{
1647 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);
1648} 1906}
1649 1907
1650static void 1908static void
1651once_cb_to (EV_P_ struct ev_timer *w, int revents) 1909once_cb_to (EV_P_ ev_timer *w, int revents)
1652{ 1910{
1653 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);
1654} 1912}
1655 1913
1656void 1914void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines