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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines