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

Comparing libev/ev.c (file contents):
Revision 1.36 by root, Thu Nov 1 13:11:11 2007 UTC vs.
Revision 1.50 by root, Sat Nov 3 19:41:55 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#if EV_USE_CONFIG_H 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33#endif 33#endif
34 34
35#include <math.h> 35#include <math.h>
36#include <stdlib.h> 36#include <stdlib.h>
42#include <stdio.h> 42#include <stdio.h>
43 43
44#include <assert.h> 44#include <assert.h>
45#include <errno.h> 45#include <errno.h>
46#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
47#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
48#include <sys/time.h> 50#include <sys/time.h>
49#include <time.h> 51#include <time.h>
50 52
53/**/
54
51#ifndef EV_USE_MONOTONIC 55#ifndef EV_USE_MONOTONIC
52# ifdef CLOCK_MONOTONIC
53# define EV_USE_MONOTONIC 1 56# define EV_USE_MONOTONIC 1
54# endif
55#endif 57#endif
56 58
57#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
59#endif 61#endif
60 62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
61#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
62# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
63#endif 69#endif
64 70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
73#endif
74
75#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1
77#endif
78
79/**/
80
81#ifndef CLOCK_MONOTONIC
82# undef EV_USE_MONOTONIC
83# define EV_USE_MONOTONIC 0
84#endif
85
65#ifndef CLOCK_REALTIME 86#ifndef CLOCK_REALTIME
87# undef EV_USE_REALTIME
66# define EV_USE_REALTIME 0 88# define EV_USE_REALTIME 0
67#endif 89#endif
68#ifndef EV_USE_REALTIME 90
69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 91/**/
70#endif
71 92
72#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
73#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
74#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
75#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
76 97
77#include "ev.h" 98#include "ev.h"
99
100#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline
103#else
104# define expect(expr,value) (expr)
105# define inline static
106#endif
107
108#define expect_false(expr) expect ((expr) != 0, 0)
109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
78 113
79typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
80typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
81typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
82 117
83static ev_tstamp now, diff; /* monotonic clock */ 118static ev_tstamp now_floor, now, diff; /* monotonic clock */
84ev_tstamp ev_now; 119ev_tstamp ev_now;
85int ev_method; 120int ev_method;
86 121
87static int have_monotonic; /* runtime */ 122static int have_monotonic; /* runtime */
88 123
108 143
109static ev_tstamp 144static ev_tstamp
110get_clock (void) 145get_clock (void)
111{ 146{
112#if EV_USE_MONOTONIC 147#if EV_USE_MONOTONIC
113 if (have_monotonic) 148 if (expect_true (have_monotonic))
114 { 149 {
115 struct timespec ts; 150 struct timespec ts;
116 clock_gettime (CLOCK_MONOTONIC, &ts); 151 clock_gettime (CLOCK_MONOTONIC, &ts);
117 return ts.tv_sec + ts.tv_nsec * 1e-9; 152 return ts.tv_sec + ts.tv_nsec * 1e-9;
118 } 153 }
122} 157}
123 158
124#define array_roundsize(base,n) ((n) | 4 & ~3) 159#define array_roundsize(base,n) ((n) | 4 & ~3)
125 160
126#define array_needsize(base,cur,cnt,init) \ 161#define array_needsize(base,cur,cnt,init) \
127 if ((cnt) > cur) \ 162 if (expect_false ((cnt) > cur)) \
128 { \ 163 { \
129 int newcnt = cur; \ 164 int newcnt = cur; \
130 do \ 165 do \
131 { \ 166 { \
132 newcnt = array_roundsize (base, newcnt << 1); \ 167 newcnt = array_roundsize (base, newcnt << 1); \
140 175
141/*****************************************************************************/ 176/*****************************************************************************/
142 177
143typedef struct 178typedef struct
144{ 179{
145 struct ev_io *head; 180 struct ev_watcher_list *head;
146 unsigned char events; 181 unsigned char events;
147 unsigned char reify; 182 unsigned char reify;
148} ANFD; 183} ANFD;
149 184
150static ANFD *anfds; 185static ANFD *anfds;
167{ 202{
168 W w; 203 W w;
169 int events; 204 int events;
170} ANPENDING; 205} ANPENDING;
171 206
172static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
173static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
174 209
175static void 210static void
176event (W w, int events) 211event (W w, int events)
177{ 212{
178 if (w->pending) 213 if (w->pending)
179 { 214 {
180 pendings [w->pending - 1].events |= events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
181 return; 216 return;
182 } 217 }
183 218
184 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
185 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
186 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
187 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
188} 223}
189 224
190static void 225static void
191queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
192{ 227{
200fd_event (int fd, int events) 235fd_event (int fd, int events)
201{ 236{
202 ANFD *anfd = anfds + fd; 237 ANFD *anfd = anfds + fd;
203 struct ev_io *w; 238 struct ev_io *w;
204 239
205 for (w = anfd->head; w; w = w->next) 240 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
206 { 241 {
207 int ev = w->events & events; 242 int ev = w->events & events;
208 243
209 if (ev) 244 if (ev)
210 event ((W)w, ev); 245 event ((W)w, ev);
227 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
228 struct ev_io *w; 263 struct ev_io *w;
229 264
230 int events = 0; 265 int events = 0;
231 266
232 for (w = anfd->head; w; w = w->next) 267 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
233 events |= w->events; 268 events |= w->events;
234 269
235 anfd->reify = 0; 270 anfd->reify = 0;
236 271
237 if (anfd->events != events) 272 if (anfd->events != events)
255 ++fdchangecnt; 290 ++fdchangecnt;
256 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
257 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
258} 293}
259 294
295static void
296fd_kill (int fd)
297{
298 struct ev_io *w;
299
300 while ((w = (struct ev_io *)anfds [fd].head))
301 {
302 ev_io_stop (w);
303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
304 }
305}
306
260/* called on EBADF to verify fds */ 307/* called on EBADF to verify fds */
261static void 308static void
262fd_recheck (void) 309fd_ebadf (void)
263{ 310{
264 int fd; 311 int fd;
265 312
266 for (fd = 0; fd < anfdmax; ++fd) 313 for (fd = 0; fd < anfdmax; ++fd)
267 if (anfds [fd].events) 314 if (anfds [fd].events)
268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 315 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
269 while (anfds [fd].head) 316 fd_kill (fd);
317}
318
319/* called on ENOMEM in select/poll to kill some fds and retry */
320static void
321fd_enomem (void)
322{
323 int fd = anfdmax;
324
325 while (fd--)
326 if (anfds [fd].events)
270 { 327 {
271 ev_io_stop (anfds [fd].head); 328 close (fd);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 329 fd_kill (fd);
330 return;
273 } 331 }
274} 332}
275 333
276/*****************************************************************************/ 334/*****************************************************************************/
277 335
278static struct ev_timer **timers; 336static struct ev_timer **timers;
324 382
325/*****************************************************************************/ 383/*****************************************************************************/
326 384
327typedef struct 385typedef struct
328{ 386{
329 struct ev_signal *head; 387 struct ev_watcher_list *head;
330 sig_atomic_t volatile gotsig; 388 sig_atomic_t volatile gotsig;
331} ANSIG; 389} ANSIG;
332 390
333static ANSIG *signals; 391static ANSIG *signals;
334static int signalmax; 392static int signalmax;
354{ 412{
355 signals [signum - 1].gotsig = 1; 413 signals [signum - 1].gotsig = 1;
356 414
357 if (!gotsig) 415 if (!gotsig)
358 { 416 {
417 int old_errno = errno;
359 gotsig = 1; 418 gotsig = 1;
360 write (sigpipe [1], &signum, 1); 419 write (sigpipe [1], &signum, 1);
420 errno = old_errno;
361 } 421 }
362} 422}
363 423
364static void 424static void
365sigcb (struct ev_io *iow, int revents) 425sigcb (struct ev_io *iow, int revents)
366{ 426{
367 struct ev_signal *w; 427 struct ev_watcher_list *w;
368 int sig; 428 int signum;
369 429
370 read (sigpipe [0], &revents, 1); 430 read (sigpipe [0], &revents, 1);
371 gotsig = 0; 431 gotsig = 0;
372 432
373 for (sig = signalmax; sig--; ) 433 for (signum = signalmax; signum--; )
374 if (signals [sig].gotsig) 434 if (signals [signum].gotsig)
375 { 435 {
376 signals [sig].gotsig = 0; 436 signals [signum].gotsig = 0;
377 437
378 for (w = signals [sig].head; w; w = w->next) 438 for (w = signals [signum].head; w; w = w->next)
379 event ((W)w, EV_SIGNAL); 439 event ((W)w, EV_SIGNAL);
380 } 440 }
381} 441}
382 442
383static void 443static void
384siginit (void) 444siginit (void)
385{ 445{
446#ifndef WIN32
386 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
387 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
388 449
389 /* rather than sort out wether we really need nb, set it */ 450 /* rather than sort out wether we really need nb, set it */
390 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
391 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
453#endif
392 454
393 ev_io_set (&sigev, sigpipe [0], EV_READ); 455 ev_io_set (&sigev, sigpipe [0], EV_READ);
394 ev_io_start (&sigev); 456 ev_io_start (&sigev);
395} 457}
396 458
408/*****************************************************************************/ 470/*****************************************************************************/
409 471
410static struct ev_child *childs [PID_HASHSIZE]; 472static struct ev_child *childs [PID_HASHSIZE];
411static struct ev_signal childev; 473static struct ev_signal childev;
412 474
475#ifndef WIN32
476
413#ifndef WCONTINUED 477#ifndef WCONTINUED
414# define WCONTINUED 0 478# define WCONTINUED 0
415#endif 479#endif
416 480
417static void 481static void
482child_reap (struct ev_signal *sw, int chain, int pid, int status)
483{
484 struct ev_child *w;
485
486 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
487 if (w->pid == pid || !w->pid)
488 {
489 w->priority = sw->priority; /* need to do it *now* */
490 w->rpid = pid;
491 w->rstatus = status;
492 event ((W)w, EV_CHILD);
493 }
494}
495
496static void
418childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
419{ 498{
420 struct ev_child *w;
421 int pid, status; 499 int pid, status;
422 500
423 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
424 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 502 {
425 if (w->pid == pid || w->pid == -1) 503 /* make sure we are called again until all childs have been reaped */
426 {
427 w->status = status;
428 event ((W)w, EV_CHILD); 504 event ((W)sw, EV_SIGNAL);
429 } 505
506 child_reap (sw, pid, pid, status);
507 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
508 }
430} 509}
510
511#endif
431 512
432/*****************************************************************************/ 513/*****************************************************************************/
433 514
515#if EV_USE_KQUEUE
516# include "ev_kqueue.c"
517#endif
434#if EV_USE_EPOLL 518#if EV_USE_EPOLL
435# include "ev_epoll.c" 519# include "ev_epoll.c"
436#endif 520#endif
521#if EV_USE_POLL
522# include "ev_poll.c"
523#endif
437#if EV_USE_SELECT 524#if EV_USE_SELECT
438# include "ev_select.c" 525# include "ev_select.c"
439#endif 526#endif
440 527
441int 528int
448ev_version_minor (void) 535ev_version_minor (void)
449{ 536{
450 return EV_VERSION_MINOR; 537 return EV_VERSION_MINOR;
451} 538}
452 539
540/* return true if we are running with elevated privileges and should ignore env variables */
541static int
542enable_secure ()
543{
544#ifdef WIN32
545 return 0;
546#else
547 return getuid () != geteuid ()
548 || getgid () != getegid ();
549#endif
550}
551
453int ev_init (int flags) 552int ev_init (int methods)
454{ 553{
455 if (!ev_method) 554 if (!ev_method)
456 { 555 {
457#if EV_USE_MONOTONIC 556#if EV_USE_MONOTONIC
458 { 557 {
460 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 559 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
461 have_monotonic = 1; 560 have_monotonic = 1;
462 } 561 }
463#endif 562#endif
464 563
465 ev_now = ev_time (); 564 ev_now = ev_time ();
466 now = get_clock (); 565 now = get_clock ();
566 now_floor = now;
467 diff = ev_now - now; 567 diff = ev_now - now;
468 568
469 if (pipe (sigpipe)) 569 if (pipe (sigpipe))
470 return 0; 570 return 0;
471 571
572 if (methods == EVMETHOD_AUTO)
573 if (!enable_secure () && getenv ("LIBEV_METHODS"))
574 methods = atoi (getenv ("LIBEV_METHODS"));
575 else
472 ev_method = EVMETHOD_NONE; 576 methods = EVMETHOD_ANY;
577
578 ev_method = 0;
579#if EV_USE_KQUEUE
580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
581#endif
473#if EV_USE_EPOLL 582#if EV_USE_EPOLL
474 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 583 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
584#endif
585#if EV_USE_POLL
586 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
475#endif 587#endif
476#if EV_USE_SELECT 588#if EV_USE_SELECT
477 if (ev_method == EVMETHOD_NONE) select_init (flags); 589 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
478#endif 590#endif
479 591
480 if (ev_method) 592 if (ev_method)
481 { 593 {
482 ev_watcher_init (&sigev, sigcb); 594 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI);
483 siginit (); 596 siginit ();
484 597
598#ifndef WIN32
485 ev_signal_init (&childev, childcb, SIGCHLD); 599 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI);
486 ev_signal_start (&childev); 601 ev_signal_start (&childev);
602#endif
487 } 603 }
488 } 604 }
489 605
490 return ev_method; 606 return ev_method;
491} 607}
522/*****************************************************************************/ 638/*****************************************************************************/
523 639
524static void 640static void
525call_pending (void) 641call_pending (void)
526{ 642{
643 int pri;
644
645 for (pri = NUMPRI; pri--; )
527 while (pendingcnt) 646 while (pendingcnt [pri])
528 { 647 {
529 ANPENDING *p = pendings + --pendingcnt; 648 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
530 649
531 if (p->w) 650 if (p->w)
532 { 651 {
533 p->w->pending = 0; 652 p->w->pending = 0;
534 p->w->cb (p->w, p->events); 653 p->w->cb (p->w, p->events);
535 } 654 }
536 } 655 }
537} 656}
538 657
539static void 658static void
540timers_reify (void) 659timers_reify (void)
541{ 660{
601 } 720 }
602 } 721 }
603 } 722 }
604} 723}
605 724
725static int
726time_update_monotonic (void)
727{
728 now = get_clock ();
729
730 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
731 {
732 ev_now = now + diff;
733 return 0;
734 }
735 else
736 {
737 now_floor = now;
738 ev_now = ev_time ();
739 return 1;
740 }
741}
742
606static void 743static void
607time_update (void) 744time_update (void)
608{ 745{
609 int i; 746 int i;
610 747
611 ev_now = ev_time (); 748#if EV_USE_MONOTONIC
612
613 if (have_monotonic) 749 if (expect_true (have_monotonic))
614 { 750 {
615 ev_tstamp odiff = diff; 751 if (time_update_monotonic ())
616
617 for (i = 4; --i; ) /* loop a few times, before making important decisions */
618 { 752 {
619 now = get_clock (); 753 ev_tstamp odiff = diff;
754
755 for (i = 4; --i; ) /* loop a few times, before making important decisions */
756 {
620 diff = ev_now - now; 757 diff = ev_now - now;
621 758
622 if (fabs (odiff - diff) < MIN_TIMEJUMP) 759 if (fabs (odiff - diff) < MIN_TIMEJUMP)
623 return; /* all is well */ 760 return; /* all is well */
624 761
625 ev_now = ev_time (); 762 ev_now = ev_time ();
763 now = get_clock ();
764 now_floor = now;
765 }
766
767 periodics_reschedule (diff - odiff);
768 /* no timer adjustment, as the monotonic clock doesn't jump */
626 } 769 }
627
628 periodics_reschedule (diff - odiff);
629 /* no timer adjustment, as the monotonic clock doesn't jump */
630 } 770 }
631 else 771 else
772#endif
632 { 773 {
774 ev_now = ev_time ();
775
633 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 776 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
634 { 777 {
635 periodics_reschedule (ev_now - now); 778 periodics_reschedule (ev_now - now);
636 779
637 /* adjust timers. this is easy, as the offset is the same for all */ 780 /* adjust timers. this is easy, as the offset is the same for all */
638 for (i = 0; i < timercnt; ++i) 781 for (i = 0; i < timercnt; ++i)
651 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 794 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
652 795
653 do 796 do
654 { 797 {
655 /* queue check watchers (and execute them) */ 798 /* queue check watchers (and execute them) */
656 if (preparecnt) 799 if (expect_false (preparecnt))
657 { 800 {
658 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 801 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
659 call_pending (); 802 call_pending ();
660 } 803 }
661 804
664 807
665 /* calculate blocking time */ 808 /* calculate blocking time */
666 809
667 /* we only need this for !monotonic clockor timers, but as we basically 810 /* we only need this for !monotonic clockor timers, but as we basically
668 always have timers, we just calculate it always */ 811 always have timers, we just calculate it always */
812#if EV_USE_MONOTONIC
813 if (expect_true (have_monotonic))
814 time_update_monotonic ();
815 else
816#endif
817 {
669 ev_now = ev_time (); 818 ev_now = ev_time ();
819 now = ev_now;
820 }
670 821
671 if (flags & EVLOOP_NONBLOCK || idlecnt) 822 if (flags & EVLOOP_NONBLOCK || idlecnt)
672 block = 0.; 823 block = 0.;
673 else 824 else
674 { 825 {
675 block = MAX_BLOCKTIME; 826 block = MAX_BLOCKTIME;
676 827
677 if (timercnt) 828 if (timercnt)
678 { 829 {
679 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 830 ev_tstamp to = timers [0]->at - now + method_fudge;
680 if (block > to) block = to; 831 if (block > to) block = to;
681 } 832 }
682 833
683 if (periodiccnt) 834 if (periodiccnt)
684 { 835 {
741static void 892static void
742ev_clear_pending (W w) 893ev_clear_pending (W w)
743{ 894{
744 if (w->pending) 895 if (w->pending)
745 { 896 {
746 pendings [w->pending - 1].w = 0; 897 pendings [ABSPRI (w)][w->pending - 1].w = 0;
747 w->pending = 0; 898 w->pending = 0;
748 } 899 }
749} 900}
750 901
751static void 902static void
752ev_start (W w, int active) 903ev_start (W w, int active)
753{ 904{
905 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
906 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
907
754 w->active = active; 908 w->active = active;
755} 909}
756 910
757static void 911static void
758ev_stop (W w) 912ev_stop (W w)
763/*****************************************************************************/ 917/*****************************************************************************/
764 918
765void 919void
766ev_io_start (struct ev_io *w) 920ev_io_start (struct ev_io *w)
767{ 921{
922 int fd = w->fd;
923
768 if (ev_is_active (w)) 924 if (ev_is_active (w))
769 return; 925 return;
770
771 int fd = w->fd;
772 926
773 assert (("ev_io_start called with negative fd", fd >= 0)); 927 assert (("ev_io_start called with negative fd", fd >= 0));
774 928
775 ev_start ((W)w, 1); 929 ev_start ((W)w, 1);
776 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 930 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
875 } 1029 }
876 1030
877 ev_stop ((W)w); 1031 ev_stop ((W)w);
878} 1032}
879 1033
1034#ifndef SA_RESTART
1035# define SA_RESTART 0
1036#endif
1037
880void 1038void
881ev_signal_start (struct ev_signal *w) 1039ev_signal_start (struct ev_signal *w)
882{ 1040{
883 if (ev_is_active (w)) 1041 if (ev_is_active (w))
884 return; 1042 return;
892 if (!w->next) 1050 if (!w->next)
893 { 1051 {
894 struct sigaction sa; 1052 struct sigaction sa;
895 sa.sa_handler = sighandler; 1053 sa.sa_handler = sighandler;
896 sigfillset (&sa.sa_mask); 1054 sigfillset (&sa.sa_mask);
897 sa.sa_flags = 0; 1055 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
898 sigaction (w->signum, &sa, 0); 1056 sigaction (w->signum, &sa, 0);
899 } 1057 }
900} 1058}
901 1059
902void 1060void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines