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

Comparing libev/ev.c (file contents):
Revision 1.123 by root, Sat Nov 17 02:23:54 2007 UTC vs.
Revision 1.147 by root, Tue Nov 27 10:59:11 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 }
812} 895}
813 896
814void 897static void
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
910 993
911#endif 994#endif
912 995
913#if EV_MULTIPLICITY 996#if EV_MULTIPLICITY
914struct ev_loop * 997struct ev_loop *
915ev_default_loop_ (unsigned int flags) 998ev_default_loop_init (unsigned int flags)
916#else 999#else
917int 1000int
918ev_default_loop (unsigned int flags) 1001ev_default_loop (unsigned int flags)
919#endif 1002#endif
920{ 1003{
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 (expect_false (postfork))
1283 if (forkcnt)
1284 {
1285 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1286 call_pending (EV_A);
1287 }
1288
1187 /* queue check watchers (and execute them) */ 1289 /* queue check watchers (and execute them) */
1188 if (expect_false (preparecnt)) 1290 if (expect_false (preparecnt))
1189 { 1291 {
1190 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1292 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1191 call_pending (EV_A); 1293 call_pending (EV_A);
1197 1299
1198 /* update fd-related kernel structures */ 1300 /* update fd-related kernel structures */
1199 fd_reify (EV_A); 1301 fd_reify (EV_A);
1200 1302
1201 /* calculate blocking time */ 1303 /* calculate blocking time */
1304 {
1305 double block;
1202 1306
1203 /* we only need this for !monotonic clock or timers, but as we basically 1307 if (flags & EVLOOP_NONBLOCK || idlecnt)
1204 always have timers, we just calculate it always */ 1308 block = 0.; /* do not block at all */
1309 else
1310 {
1311 /* update time to cancel out callback processing overhead */
1205#if EV_USE_MONOTONIC 1312#if EV_USE_MONOTONIC
1206 if (expect_true (have_monotonic)) 1313 if (expect_true (have_monotonic))
1207 time_update_monotonic (EV_A); 1314 time_update_monotonic (EV_A);
1208 else 1315 else
1209#endif 1316#endif
1210 { 1317 {
1211 ev_rt_now = ev_time (); 1318 ev_rt_now = ev_time ();
1212 mn_now = ev_rt_now; 1319 mn_now = ev_rt_now;
1213 } 1320 }
1214 1321
1215 if (flags & EVLOOP_NONBLOCK || idlecnt)
1216 block = 0.;
1217 else
1218 {
1219 block = MAX_BLOCKTIME; 1322 block = MAX_BLOCKTIME;
1220 1323
1221 if (timercnt) 1324 if (timercnt)
1222 { 1325 {
1223 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1326 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1224 if (block > to) block = to; 1327 if (block > to) block = to;
1225 } 1328 }
1226 1329
1227#if EV_PERIODICS 1330#if EV_PERIODIC_ENABLE
1228 if (periodiccnt) 1331 if (periodiccnt)
1229 { 1332 {
1230 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1333 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1231 if (block > to) block = to; 1334 if (block > to) block = to;
1232 } 1335 }
1233#endif 1336#endif
1234 1337
1235 if (expect_false (block < 0.)) block = 0.; 1338 if (expect_false (block < 0.)) block = 0.;
1236 } 1339 }
1237 1340
1238 method_poll (EV_A_ block); 1341 backend_poll (EV_A_ block);
1342 }
1239 1343
1240 /* update ev_rt_now, do magic */ 1344 /* update ev_rt_now, do magic */
1241 time_update (EV_A); 1345 time_update (EV_A);
1242 1346
1243 /* queue pending timers and reschedule them */ 1347 /* queue pending timers and reschedule them */
1244 timers_reify (EV_A); /* relative timers called last */ 1348 timers_reify (EV_A); /* relative timers called last */
1245#if EV_PERIODICS 1349#if EV_PERIODIC_ENABLE
1246 periodics_reify (EV_A); /* absolute timers called first */ 1350 periodics_reify (EV_A); /* absolute timers called first */
1247#endif 1351#endif
1248 1352
1249 /* queue idle watchers unless io or timers are pending */ 1353 /* queue idle watchers unless other events are pending */
1250 if (idlecnt && !any_pending (EV_A)) 1354 if (idlecnt && !any_pending (EV_A))
1251 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1355 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1252 1356
1253 /* queue check watchers, to be executed first */ 1357 /* queue check watchers, to be executed first */
1254 if (expect_false (checkcnt)) 1358 if (expect_false (checkcnt))
1258 1362
1259 if (expect_false (loop_done)) 1363 if (expect_false (loop_done))
1260 break; 1364 break;
1261 } 1365 }
1262 1366
1263 if (loop_done != 2) 1367 if (loop_done == EVUNLOOP_ONE)
1264 loop_done = 0; 1368 loop_done = EVUNLOOP_CANCEL;
1265} 1369}
1266 1370
1267void 1371void
1268ev_unloop (EV_P_ int how) 1372ev_unloop (EV_P_ int how)
1269{ 1373{
1270 loop_done = how; 1374 loop_done = how;
1271} 1375}
1272 1376
1273/*****************************************************************************/ 1377/*****************************************************************************/
1274 1378
1275inline void 1379void inline_size
1276wlist_add (WL *head, WL elem) 1380wlist_add (WL *head, WL elem)
1277{ 1381{
1278 elem->next = *head; 1382 elem->next = *head;
1279 *head = elem; 1383 *head = elem;
1280} 1384}
1281 1385
1282inline void 1386void inline_size
1283wlist_del (WL *head, WL elem) 1387wlist_del (WL *head, WL elem)
1284{ 1388{
1285 while (*head) 1389 while (*head)
1286 { 1390 {
1287 if (*head == elem) 1391 if (*head == elem)
1292 1396
1293 head = &(*head)->next; 1397 head = &(*head)->next;
1294 } 1398 }
1295} 1399}
1296 1400
1297inline void 1401void inline_speed
1298ev_clear_pending (EV_P_ W w) 1402ev_clear_pending (EV_P_ W w)
1299{ 1403{
1300 if (w->pending) 1404 if (w->pending)
1301 { 1405 {
1302 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1406 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1303 w->pending = 0; 1407 w->pending = 0;
1304 } 1408 }
1305} 1409}
1306 1410
1307inline void 1411void inline_speed
1308ev_start (EV_P_ W w, int active) 1412ev_start (EV_P_ W w, int active)
1309{ 1413{
1310 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1414 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1311 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1415 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1312 1416
1313 w->active = active; 1417 w->active = active;
1314 ev_ref (EV_A); 1418 ev_ref (EV_A);
1315} 1419}
1316 1420
1317inline void 1421void inline_size
1318ev_stop (EV_P_ W w) 1422ev_stop (EV_P_ W w)
1319{ 1423{
1320 ev_unref (EV_A); 1424 ev_unref (EV_A);
1321 w->active = 0; 1425 w->active = 0;
1322} 1426}
1323 1427
1324/*****************************************************************************/ 1428/*****************************************************************************/
1325 1429
1326void 1430void
1327ev_io_start (EV_P_ struct ev_io *w) 1431ev_io_start (EV_P_ ev_io *w)
1328{ 1432{
1329 int fd = w->fd; 1433 int fd = w->fd;
1330 1434
1331 if (expect_false (ev_is_active (w))) 1435 if (expect_false (ev_is_active (w)))
1332 return; 1436 return;
1339 1443
1340 fd_change (EV_A_ fd); 1444 fd_change (EV_A_ fd);
1341} 1445}
1342 1446
1343void 1447void
1344ev_io_stop (EV_P_ struct ev_io *w) 1448ev_io_stop (EV_P_ ev_io *w)
1345{ 1449{
1346 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1347 if (expect_false (!ev_is_active (w))) 1451 if (expect_false (!ev_is_active (w)))
1348 return; 1452 return;
1349 1453
1354 1458
1355 fd_change (EV_A_ w->fd); 1459 fd_change (EV_A_ w->fd);
1356} 1460}
1357 1461
1358void 1462void
1359ev_timer_start (EV_P_ struct ev_timer *w) 1463ev_timer_start (EV_P_ ev_timer *w)
1360{ 1464{
1361 if (expect_false (ev_is_active (w))) 1465 if (expect_false (ev_is_active (w)))
1362 return; 1466 return;
1363 1467
1364 ((WT)w)->at += mn_now; 1468 ((WT)w)->at += mn_now;
1365 1469
1366 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1470 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1367 1471
1368 ev_start (EV_A_ (W)w, ++timercnt); 1472 ev_start (EV_A_ (W)w, ++timercnt);
1369 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1473 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1370 timers [timercnt - 1] = w; 1474 timers [timercnt - 1] = w;
1371 upheap ((WT *)timers, timercnt - 1); 1475 upheap ((WT *)timers, timercnt - 1);
1372 1476
1373 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1477 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1374} 1478}
1375 1479
1376void 1480void
1377ev_timer_stop (EV_P_ struct ev_timer *w) 1481ev_timer_stop (EV_P_ ev_timer *w)
1378{ 1482{
1379 ev_clear_pending (EV_A_ (W)w); 1483 ev_clear_pending (EV_A_ (W)w);
1380 if (expect_false (!ev_is_active (w))) 1484 if (expect_false (!ev_is_active (w)))
1381 return; 1485 return;
1382 1486
1392 1496
1393 ev_stop (EV_A_ (W)w); 1497 ev_stop (EV_A_ (W)w);
1394} 1498}
1395 1499
1396void 1500void
1397ev_timer_again (EV_P_ struct ev_timer *w) 1501ev_timer_again (EV_P_ ev_timer *w)
1398{ 1502{
1399 if (ev_is_active (w)) 1503 if (ev_is_active (w))
1400 { 1504 {
1401 if (w->repeat) 1505 if (w->repeat)
1402 { 1506 {
1411 w->at = w->repeat; 1515 w->at = w->repeat;
1412 ev_timer_start (EV_A_ w); 1516 ev_timer_start (EV_A_ w);
1413 } 1517 }
1414} 1518}
1415 1519
1416#if EV_PERIODICS 1520#if EV_PERIODIC_ENABLE
1417void 1521void
1418ev_periodic_start (EV_P_ struct ev_periodic *w) 1522ev_periodic_start (EV_P_ ev_periodic *w)
1419{ 1523{
1420 if (expect_false (ev_is_active (w))) 1524 if (expect_false (ev_is_active (w)))
1421 return; 1525 return;
1422 1526
1423 if (w->reschedule_cb) 1527 if (w->reschedule_cb)
1428 /* this formula differs from the one in periodic_reify because we do not always round up */ 1532 /* 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; 1533 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1430 } 1534 }
1431 1535
1432 ev_start (EV_A_ (W)w, ++periodiccnt); 1536 ev_start (EV_A_ (W)w, ++periodiccnt);
1433 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1537 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1434 periodics [periodiccnt - 1] = w; 1538 periodics [periodiccnt - 1] = w;
1435 upheap ((WT *)periodics, periodiccnt - 1); 1539 upheap ((WT *)periodics, periodiccnt - 1);
1436 1540
1437 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1541 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1438} 1542}
1439 1543
1440void 1544void
1441ev_periodic_stop (EV_P_ struct ev_periodic *w) 1545ev_periodic_stop (EV_P_ ev_periodic *w)
1442{ 1546{
1443 ev_clear_pending (EV_A_ (W)w); 1547 ev_clear_pending (EV_A_ (W)w);
1444 if (expect_false (!ev_is_active (w))) 1548 if (expect_false (!ev_is_active (w)))
1445 return; 1549 return;
1446 1550
1454 1558
1455 ev_stop (EV_A_ (W)w); 1559 ev_stop (EV_A_ (W)w);
1456} 1560}
1457 1561
1458void 1562void
1459ev_periodic_again (EV_P_ struct ev_periodic *w) 1563ev_periodic_again (EV_P_ ev_periodic *w)
1460{ 1564{
1461 /* TODO: use adjustheap and recalculation */ 1565 /* TODO: use adjustheap and recalculation */
1462 ev_periodic_stop (EV_A_ w); 1566 ev_periodic_stop (EV_A_ w);
1463 ev_periodic_start (EV_A_ w); 1567 ev_periodic_start (EV_A_ w);
1464} 1568}
1465#endif 1569#endif
1466 1570
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 1571#ifndef SA_RESTART
1534# define SA_RESTART 0 1572# define SA_RESTART 0
1535#endif 1573#endif
1536 1574
1537void 1575void
1538ev_signal_start (EV_P_ struct ev_signal *w) 1576ev_signal_start (EV_P_ ev_signal *w)
1539{ 1577{
1540#if EV_MULTIPLICITY 1578#if EV_MULTIPLICITY
1541 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1579 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1542#endif 1580#endif
1543 if (expect_false (ev_is_active (w))) 1581 if (expect_false (ev_is_active (w)))
1562#endif 1600#endif
1563 } 1601 }
1564} 1602}
1565 1603
1566void 1604void
1567ev_signal_stop (EV_P_ struct ev_signal *w) 1605ev_signal_stop (EV_P_ ev_signal *w)
1568{ 1606{
1569 ev_clear_pending (EV_A_ (W)w); 1607 ev_clear_pending (EV_A_ (W)w);
1570 if (expect_false (!ev_is_active (w))) 1608 if (expect_false (!ev_is_active (w)))
1571 return; 1609 return;
1572 1610
1576 if (!signals [w->signum - 1].head) 1614 if (!signals [w->signum - 1].head)
1577 signal (w->signum, SIG_DFL); 1615 signal (w->signum, SIG_DFL);
1578} 1616}
1579 1617
1580void 1618void
1581ev_child_start (EV_P_ struct ev_child *w) 1619ev_child_start (EV_P_ ev_child *w)
1582{ 1620{
1583#if EV_MULTIPLICITY 1621#if EV_MULTIPLICITY
1584 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1622 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1585#endif 1623#endif
1586 if (expect_false (ev_is_active (w))) 1624 if (expect_false (ev_is_active (w)))
1589 ev_start (EV_A_ (W)w, 1); 1627 ev_start (EV_A_ (W)w, 1);
1590 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1628 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1591} 1629}
1592 1630
1593void 1631void
1594ev_child_stop (EV_P_ struct ev_child *w) 1632ev_child_stop (EV_P_ ev_child *w)
1595{ 1633{
1596 ev_clear_pending (EV_A_ (W)w); 1634 ev_clear_pending (EV_A_ (W)w);
1597 if (expect_false (!ev_is_active (w))) 1635 if (expect_false (!ev_is_active (w)))
1598 return; 1636 return;
1599 1637
1600 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1638 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1601 ev_stop (EV_A_ (W)w); 1639 ev_stop (EV_A_ (W)w);
1602} 1640}
1603 1641
1642#if EV_STAT_ENABLE
1643
1644# ifdef _WIN32
1645# undef lstat
1646# define lstat(a,b) _stati64 (a,b)
1647# endif
1648
1649#define DEF_STAT_INTERVAL 5.0074891
1650#define MIN_STAT_INTERVAL 0.1074891
1651
1652void
1653ev_stat_stat (EV_P_ ev_stat *w)
1654{
1655 if (lstat (w->path, &w->attr) < 0)
1656 w->attr.st_nlink = 0;
1657 else if (!w->attr.st_nlink)
1658 w->attr.st_nlink = 1;
1659}
1660
1661static void
1662stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1663{
1664 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1665
1666 /* we copy this here each the time so that */
1667 /* prev has the old value when the callback gets invoked */
1668 w->prev = w->attr;
1669 ev_stat_stat (EV_A_ w);
1670
1671 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1672 ev_feed_event (EV_A_ w, EV_STAT);
1673}
1674
1675void
1676ev_stat_start (EV_P_ ev_stat *w)
1677{
1678 if (expect_false (ev_is_active (w)))
1679 return;
1680
1681 /* since we use memcmp, we need to clear any padding data etc. */
1682 memset (&w->prev, 0, sizeof (ev_statdata));
1683 memset (&w->attr, 0, sizeof (ev_statdata));
1684
1685 ev_stat_stat (EV_A_ w);
1686
1687 if (w->interval < MIN_STAT_INTERVAL)
1688 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1689
1690 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1691 ev_set_priority (&w->timer, ev_priority (w));
1692 ev_timer_start (EV_A_ &w->timer);
1693
1694 ev_start (EV_A_ (W)w, 1);
1695}
1696
1697void
1698ev_stat_stop (EV_P_ ev_stat *w)
1699{
1700 ev_clear_pending (EV_A_ (W)w);
1701 if (expect_false (!ev_is_active (w)))
1702 return;
1703
1704 ev_timer_stop (EV_A_ &w->timer);
1705
1706 ev_stop (EV_A_ (W)w);
1707}
1708#endif
1709
1710void
1711ev_idle_start (EV_P_ ev_idle *w)
1712{
1713 if (expect_false (ev_is_active (w)))
1714 return;
1715
1716 ev_start (EV_A_ (W)w, ++idlecnt);
1717 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1718 idles [idlecnt - 1] = w;
1719}
1720
1721void
1722ev_idle_stop (EV_P_ ev_idle *w)
1723{
1724 ev_clear_pending (EV_A_ (W)w);
1725 if (expect_false (!ev_is_active (w)))
1726 return;
1727
1728 {
1729 int active = ((W)w)->active;
1730 idles [active - 1] = idles [--idlecnt];
1731 ((W)idles [active - 1])->active = active;
1732 }
1733
1734 ev_stop (EV_A_ (W)w);
1735}
1736
1737void
1738ev_prepare_start (EV_P_ ev_prepare *w)
1739{
1740 if (expect_false (ev_is_active (w)))
1741 return;
1742
1743 ev_start (EV_A_ (W)w, ++preparecnt);
1744 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1745 prepares [preparecnt - 1] = w;
1746}
1747
1748void
1749ev_prepare_stop (EV_P_ ev_prepare *w)
1750{
1751 ev_clear_pending (EV_A_ (W)w);
1752 if (expect_false (!ev_is_active (w)))
1753 return;
1754
1755 {
1756 int active = ((W)w)->active;
1757 prepares [active - 1] = prepares [--preparecnt];
1758 ((W)prepares [active - 1])->active = active;
1759 }
1760
1761 ev_stop (EV_A_ (W)w);
1762}
1763
1764void
1765ev_check_start (EV_P_ ev_check *w)
1766{
1767 if (expect_false (ev_is_active (w)))
1768 return;
1769
1770 ev_start (EV_A_ (W)w, ++checkcnt);
1771 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1772 checks [checkcnt - 1] = w;
1773}
1774
1775void
1776ev_check_stop (EV_P_ ev_check *w)
1777{
1778 ev_clear_pending (EV_A_ (W)w);
1779 if (expect_false (!ev_is_active (w)))
1780 return;
1781
1782 {
1783 int active = ((W)w)->active;
1784 checks [active - 1] = checks [--checkcnt];
1785 ((W)checks [active - 1])->active = active;
1786 }
1787
1788 ev_stop (EV_A_ (W)w);
1789}
1790
1791#if EV_EMBED_ENABLE
1792void noinline
1793ev_embed_sweep (EV_P_ ev_embed *w)
1794{
1795 ev_loop (w->loop, EVLOOP_NONBLOCK);
1796}
1797
1798static void
1799embed_cb (EV_P_ ev_io *io, int revents)
1800{
1801 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1802
1803 if (ev_cb (w))
1804 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1805 else
1806 ev_embed_sweep (loop, w);
1807}
1808
1809void
1810ev_embed_start (EV_P_ ev_embed *w)
1811{
1812 if (expect_false (ev_is_active (w)))
1813 return;
1814
1815 {
1816 struct ev_loop *loop = w->loop;
1817 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1818 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1819 }
1820
1821 ev_set_priority (&w->io, ev_priority (w));
1822 ev_io_start (EV_A_ &w->io);
1823
1824 ev_start (EV_A_ (W)w, 1);
1825}
1826
1827void
1828ev_embed_stop (EV_P_ ev_embed *w)
1829{
1830 ev_clear_pending (EV_A_ (W)w);
1831 if (expect_false (!ev_is_active (w)))
1832 return;
1833
1834 ev_io_stop (EV_A_ &w->io);
1835
1836 ev_stop (EV_A_ (W)w);
1837}
1838#endif
1839
1840#if EV_FORK_ENABLE
1841void
1842ev_fork_start (EV_P_ ev_fork *w)
1843{
1844 if (expect_false (ev_is_active (w)))
1845 return;
1846
1847 ev_start (EV_A_ (W)w, ++forkcnt);
1848 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
1849 forks [forkcnt - 1] = w;
1850}
1851
1852void
1853ev_fork_stop (EV_P_ ev_fork *w)
1854{
1855 ev_clear_pending (EV_A_ (W)w);
1856 if (expect_false (!ev_is_active (w)))
1857 return;
1858
1859 {
1860 int active = ((W)w)->active;
1861 forks [active - 1] = forks [--forkcnt];
1862 ((W)forks [active - 1])->active = active;
1863 }
1864
1865 ev_stop (EV_A_ (W)w);
1866}
1867#endif
1868
1604/*****************************************************************************/ 1869/*****************************************************************************/
1605 1870
1606struct ev_once 1871struct ev_once
1607{ 1872{
1608 struct ev_io io; 1873 ev_io io;
1609 struct ev_timer to; 1874 ev_timer to;
1610 void (*cb)(int revents, void *arg); 1875 void (*cb)(int revents, void *arg);
1611 void *arg; 1876 void *arg;
1612}; 1877};
1613 1878
1614static void 1879static void
1623 1888
1624 cb (revents, arg); 1889 cb (revents, arg);
1625} 1890}
1626 1891
1627static void 1892static void
1628once_cb_io (EV_P_ struct ev_io *w, int revents) 1893once_cb_io (EV_P_ ev_io *w, int revents)
1629{ 1894{
1630 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1895 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1631} 1896}
1632 1897
1633static void 1898static void
1634once_cb_to (EV_P_ struct ev_timer *w, int revents) 1899once_cb_to (EV_P_ ev_timer *w, int revents)
1635{ 1900{
1636 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1901 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1637} 1902}
1638 1903
1639void 1904void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines