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

Comparing libev/ev.c (file contents):
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC vs.
Revision 1.43 by root, Fri Nov 2 20:21:33 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
44#include <sys/types.h> 46#include <sys/types.h>
45#include <sys/wait.h> 47#include <sys/wait.h>
46#include <sys/time.h> 48#include <sys/time.h>
47#include <time.h> 49#include <time.h>
48 50
51/**/
52
49#ifndef EV_USE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 54# define EV_USE_MONOTONIC 1
52# endif
53#endif 55#endif
54 56
55#ifndef EV_USE_SELECT 57#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 58# define EV_USE_SELECT 1
57#endif 59#endif
58 60
61#ifndef EV_USE_POLL
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
63#endif
64
59#ifndef EV_USE_EPOLL 65#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 66# define EV_USE_EPOLL 0
61#endif 67#endif
62 68
69#ifndef EV_USE_REALTIME
70# define EV_USE_REALTIME 1
71#endif
72
73/**/
74
75#ifndef CLOCK_MONOTONIC
76# undef EV_USE_MONOTONIC
77# define EV_USE_MONOTONIC 0
78#endif
79
63#ifndef CLOCK_REALTIME 80#ifndef CLOCK_REALTIME
81# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 82# define EV_USE_REALTIME 0
65#endif 83#endif
66#ifndef EV_USE_REALTIME 84
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 85/**/
68#endif
69 86
70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 87#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 88#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 89#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 90/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
74 91
75#include "ev.h" 92#include "ev.h"
93
94#if __GNUC__ >= 3
95# define expect(expr,value) __builtin_expect ((expr),(value))
96# define inline inline
97#else
98# define expect(expr,value) (expr)
99# define inline static
100#endif
101
102#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1)
104
105#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
106#define ABSPRI(w) ((w)->priority - EV_MINPRI)
76 107
77typedef struct ev_watcher *W; 108typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 109typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 110typedef struct ev_watcher_time *WT;
80 111
81static ev_tstamp now, diff; /* monotonic clock */ 112static ev_tstamp now_floor, now, diff; /* monotonic clock */
82ev_tstamp ev_now; 113ev_tstamp ev_now;
83int ev_method; 114int ev_method;
84 115
85static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
86 117
106 137
107static ev_tstamp 138static ev_tstamp
108get_clock (void) 139get_clock (void)
109{ 140{
110#if EV_USE_MONOTONIC 141#if EV_USE_MONOTONIC
111 if (have_monotonic) 142 if (expect_true (have_monotonic))
112 { 143 {
113 struct timespec ts; 144 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 145 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 146 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 147 }
120} 151}
121 152
122#define array_roundsize(base,n) ((n) | 4 & ~3) 153#define array_roundsize(base,n) ((n) | 4 & ~3)
123 154
124#define array_needsize(base,cur,cnt,init) \ 155#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 156 if (expect_false ((cnt) > cur)) \
126 { \ 157 { \
127 int newcnt = cur; \ 158 int newcnt = cur; \
128 do \ 159 do \
129 { \ 160 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 161 newcnt = array_roundsize (base, newcnt << 1); \
139/*****************************************************************************/ 170/*****************************************************************************/
140 171
141typedef struct 172typedef struct
142{ 173{
143 struct ev_io *head; 174 struct ev_io *head;
144 int events; 175 unsigned char events;
176 unsigned char reify;
145} ANFD; 177} ANFD;
146 178
147static ANFD *anfds; 179static ANFD *anfds;
148static int anfdmax; 180static int anfdmax;
149 181
152{ 184{
153 while (count--) 185 while (count--)
154 { 186 {
155 base->head = 0; 187 base->head = 0;
156 base->events = EV_NONE; 188 base->events = EV_NONE;
189 base->reify = 0;
190
157 ++base; 191 ++base;
158 } 192 }
159} 193}
160 194
161typedef struct 195typedef struct
162{ 196{
163 W w; 197 W w;
164 int events; 198 int events;
165} ANPENDING; 199} ANPENDING;
166 200
167static ANPENDING *pendings; 201static ANPENDING *pendings [NUMPRI];
168static int pendingmax, pendingcnt; 202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
169 203
170static void 204static void
171event (W w, int events) 205event (W w, int events)
172{ 206{
173 if (w->pending) 207 if (w->pending)
174 { 208 {
175 pendings [w->pending - 1].events |= events; 209 pendings [ABSPRI (w)][w->pending - 1].events |= events;
176 return; 210 return;
177 } 211 }
178 212
179 w->pending = ++pendingcnt; 213 w->pending = ++pendingcnt [ABSPRI (w)];
180 array_needsize (pendings, pendingmax, pendingcnt, ); 214 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
181 pendings [pendingcnt - 1].w = w; 215 pendings [ABSPRI (w)][w->pending - 1].w = w;
182 pendings [pendingcnt - 1].events = events; 216 pendings [ABSPRI (w)][w->pending - 1].events = events;
183} 217}
184 218
185static void 219static void
186queue_events (W *events, int eventcnt, int type) 220queue_events (W *events, int eventcnt, int type)
187{ 221{
225 int events = 0; 259 int events = 0;
226 260
227 for (w = anfd->head; w; w = w->next) 261 for (w = anfd->head; w; w = w->next)
228 events |= w->events; 262 events |= w->events;
229 263
230 anfd->events &= ~EV_REIFY; 264 anfd->reify = 0;
231 265
232 if (anfd->events != events) 266 if (anfd->events != events)
233 { 267 {
234 method_modify (fd, anfd->events, events); 268 method_modify (fd, anfd->events, events);
235 anfd->events = events; 269 anfd->events = events;
240} 274}
241 275
242static void 276static void
243fd_change (int fd) 277fd_change (int fd)
244{ 278{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 279 if (anfds [fd].reify || fdchangecnt < 0)
246 return; 280 return;
247 281
248 anfds [fd].events |= EV_REIFY; 282 anfds [fd].reify = 1;
249 283
250 ++fdchangecnt; 284 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 285 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd; 286 fdchanges [fdchangecnt - 1] = fd;
253} 287}
254 288
289static void
290fd_kill (int fd)
291{
292 struct ev_io *w;
293
294 printf ("killing fd %d\n", fd);//D
295 while ((w = anfds [fd].head))
296 {
297 ev_io_stop (w);
298 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
299 }
300}
301
255/* called on EBADF to verify fds */ 302/* called on EBADF to verify fds */
256static void 303static void
257fd_recheck (void) 304fd_ebadf (void)
258{ 305{
259 int fd; 306 int fd;
260 307
261 for (fd = 0; fd < anfdmax; ++fd) 308 for (fd = 0; fd < anfdmax; ++fd)
262 if (anfds [fd].events) 309 if (anfds [fd].events)
263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
264 while (anfds [fd].head) 311 fd_kill (fd);
312}
313
314/* called on ENOMEM in select/poll to kill some fds and retry */
315static void
316fd_enomem (void)
317{
318 int fd = anfdmax;
319
320 while (fd--)
321 if (anfds [fd].events)
265 { 322 {
266 ev_io_stop (anfds [fd].head); 323 close (fd);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 324 fd_kill (fd);
325 return;
268 } 326 }
269} 327}
270 328
271/*****************************************************************************/ 329/*****************************************************************************/
272 330
273static struct ev_timer **timers; 331static struct ev_timer **timers;
320/*****************************************************************************/ 378/*****************************************************************************/
321 379
322typedef struct 380typedef struct
323{ 381{
324 struct ev_signal *head; 382 struct ev_signal *head;
325 sig_atomic_t gotsig; 383 sig_atomic_t volatile gotsig;
326} ANSIG; 384} ANSIG;
327 385
328static ANSIG *signals; 386static ANSIG *signals;
329static int signalmax; 387static int signalmax;
330 388
331static int sigpipe [2]; 389static int sigpipe [2];
332static sig_atomic_t gotsig; 390static sig_atomic_t volatile gotsig;
333static struct ev_io sigev; 391static struct ev_io sigev;
334 392
335static void 393static void
336signals_init (ANSIG *base, int count) 394signals_init (ANSIG *base, int count)
337{ 395{
338 while (count--) 396 while (count--)
339 { 397 {
340 base->head = 0; 398 base->head = 0;
341 base->gotsig = 0; 399 base->gotsig = 0;
400
342 ++base; 401 ++base;
343 } 402 }
344} 403}
345 404
346static void 405static void
349 signals [signum - 1].gotsig = 1; 408 signals [signum - 1].gotsig = 1;
350 409
351 if (!gotsig) 410 if (!gotsig)
352 { 411 {
353 gotsig = 1; 412 gotsig = 1;
354 write (sigpipe [1], &gotsig, 1); 413 write (sigpipe [1], &signum, 1);
355 } 414 }
356} 415}
357 416
358static void 417static void
359sigcb (struct ev_io *iow, int revents) 418sigcb (struct ev_io *iow, int revents)
360{ 419{
361 struct ev_signal *w; 420 struct ev_signal *w;
362 int sig; 421 int signum;
363 422
423 read (sigpipe [0], &revents, 1);
364 gotsig = 0; 424 gotsig = 0;
365 read (sigpipe [0], &revents, 1);
366 425
367 for (sig = signalmax; sig--; ) 426 for (signum = signalmax; signum--; )
368 if (signals [sig].gotsig) 427 if (signals [signum].gotsig)
369 { 428 {
370 signals [sig].gotsig = 0; 429 signals [signum].gotsig = 0;
371 430
372 for (w = signals [sig].head; w; w = w->next) 431 for (w = signals [signum].head; w; w = w->next)
373 event ((W)w, EV_SIGNAL); 432 event ((W)w, EV_SIGNAL);
374 } 433 }
375} 434}
376 435
377static void 436static void
414 struct ev_child *w; 473 struct ev_child *w;
415 int pid, status; 474 int pid, status;
416 475
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
419 if (w->pid == pid || w->pid == -1) 478 if (w->pid == pid || !w->pid)
420 { 479 {
421 w->status = status; 480 w->status = status;
422 event ((W)w, EV_CHILD); 481 event ((W)w, EV_CHILD);
423 } 482 }
424} 483}
426/*****************************************************************************/ 485/*****************************************************************************/
427 486
428#if EV_USE_EPOLL 487#if EV_USE_EPOLL
429# include "ev_epoll.c" 488# include "ev_epoll.c"
430#endif 489#endif
490#if EV_USE_POLL
491# include "ev_poll.c"
492#endif
431#if EV_USE_SELECT 493#if EV_USE_SELECT
432# include "ev_select.c" 494# include "ev_select.c"
433#endif 495#endif
434 496
435int 497int
442ev_version_minor (void) 504ev_version_minor (void)
443{ 505{
444 return EV_VERSION_MINOR; 506 return EV_VERSION_MINOR;
445} 507}
446 508
509/* return true if we are running with elevated privileges and ignore env variables */
510static int
511enable_secure ()
512{
513 return getuid () != geteuid ()
514 || getgid () != getegid ();
515}
516
447int ev_init (int flags) 517int ev_init (int methods)
448{ 518{
449 if (!ev_method) 519 if (!ev_method)
450 { 520 {
451#if EV_USE_MONOTONIC 521#if EV_USE_MONOTONIC
452 { 522 {
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 524 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1; 525 have_monotonic = 1;
456 } 526 }
457#endif 527#endif
458 528
459 ev_now = ev_time (); 529 ev_now = ev_time ();
460 now = get_clock (); 530 now = get_clock ();
531 now_floor = now;
461 diff = ev_now - now; 532 diff = ev_now - now;
462 533
463 if (pipe (sigpipe)) 534 if (pipe (sigpipe))
464 return 0; 535 return 0;
465 536
537 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS"))
539 methods = atoi (getenv ("LIBEV_METHODS"));
540 else
466 ev_method = EVMETHOD_NONE; 541 methods = EVMETHOD_ANY;
542
543 ev_method = 0;
467#if EV_USE_EPOLL 544#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
546#endif
547#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
469#endif 549#endif
470#if EV_USE_SELECT 550#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags); 551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
472#endif 552#endif
473 553
474 if (ev_method) 554 if (ev_method)
475 { 555 {
476 ev_watcher_init (&sigev, sigcb); 556 ev_watcher_init (&sigev, sigcb);
485} 565}
486 566
487/*****************************************************************************/ 567/*****************************************************************************/
488 568
489void 569void
490ev_prefork (void) 570ev_fork_prepare (void)
491{ 571{
492 /* nop */ 572 /* nop */
493} 573}
494 574
495void 575void
496ev_postfork_parent (void) 576ev_fork_parent (void)
497{ 577{
498 /* nop */ 578 /* nop */
499} 579}
500 580
501void 581void
502ev_postfork_child (void) 582ev_fork_child (void)
503{ 583{
504#if EV_USE_EPOLL 584#if EV_USE_EPOLL
505 if (ev_method == EVMETHOD_EPOLL) 585 if (ev_method == EVMETHOD_EPOLL)
506 epoll_postfork_child (); 586 epoll_postfork_child ();
507#endif 587#endif
516/*****************************************************************************/ 596/*****************************************************************************/
517 597
518static void 598static void
519call_pending (void) 599call_pending (void)
520{ 600{
601 int pri;
602
603 for (pri = NUMPRI; pri--; )
521 while (pendingcnt) 604 while (pendingcnt [pri])
522 { 605 {
523 ANPENDING *p = pendings + --pendingcnt; 606 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
524 607
525 if (p->w) 608 if (p->w)
526 { 609 {
527 p->w->pending = 0; 610 p->w->pending = 0;
528 p->w->cb (p->w, p->events); 611 p->w->cb (p->w, p->events);
529 } 612 }
530 } 613 }
531} 614}
532 615
533static void 616static void
534timers_reify (void) 617timers_reify (void)
535{ 618{
538 struct ev_timer *w = timers [0]; 621 struct ev_timer *w = timers [0];
539 622
540 /* first reschedule or stop timer */ 623 /* first reschedule or stop timer */
541 if (w->repeat) 624 if (w->repeat)
542 { 625 {
626 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
543 w->at = now + w->repeat; 627 w->at = now + w->repeat;
544 assert (("timer timeout in the past, negative repeat?", w->at > now));
545 downheap ((WT *)timers, timercnt, 0); 628 downheap ((WT *)timers, timercnt, 0);
546 } 629 }
547 else 630 else
548 ev_timer_stop (w); /* nonrepeating: stop timer */ 631 ev_timer_stop (w); /* nonrepeating: stop timer */
549 632
560 643
561 /* first reschedule or stop timer */ 644 /* first reschedule or stop timer */
562 if (w->interval) 645 if (w->interval)
563 { 646 {
564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 647 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
565 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 648 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
566 downheap ((WT *)periodics, periodiccnt, 0); 649 downheap ((WT *)periodics, periodiccnt, 0);
567 } 650 }
568 else 651 else
569 ev_periodic_stop (w); /* nonrepeating: stop timer */ 652 ev_periodic_stop (w); /* nonrepeating: stop timer */
570 653
571 event ((W)w, EV_TIMEOUT); 654 event ((W)w, EV_PERIODIC);
572 } 655 }
573} 656}
574 657
575static void 658static void
576periodics_reschedule (ev_tstamp diff) 659periodics_reschedule (ev_tstamp diff)
595 } 678 }
596 } 679 }
597 } 680 }
598} 681}
599 682
683static int
684time_update_monotonic (void)
685{
686 now = get_clock ();
687
688 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
689 {
690 ev_now = now + diff;
691 return 0;
692 }
693 else
694 {
695 now_floor = now;
696 ev_now = ev_time ();
697 return 1;
698 }
699}
700
600static void 701static void
601time_update (void) 702time_update (void)
602{ 703{
603 int i; 704 int i;
604 705
605 ev_now = ev_time (); 706#if EV_USE_MONOTONIC
606
607 if (have_monotonic) 707 if (expect_true (have_monotonic))
608 { 708 {
709 if (time_update_monotonic ())
710 {
609 ev_tstamp odiff = diff; 711 ev_tstamp odiff = diff;
610 712
611 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 713 for (i = 4; --i; ) /* loop a few times, before making important decisions */
612 { 714 {
613 now = get_clock ();
614 diff = ev_now - now; 715 diff = ev_now - now;
615 716
616 if (fabs (odiff - diff) < MIN_TIMEJUMP) 717 if (fabs (odiff - diff) < MIN_TIMEJUMP)
617 return; /* all is well */ 718 return; /* all is well */
618 719
619 ev_now = ev_time (); 720 ev_now = ev_time ();
721 now = get_clock ();
722 now_floor = now;
620 } 723 }
621 724
622 periodics_reschedule (diff - odiff); 725 periodics_reschedule (diff - odiff);
623 /* no timer adjustment, as the monotonic clock doesn't jump */ 726 /* no timer adjustment, as the monotonic clock doesn't jump */
727 }
624 } 728 }
625 else 729 else
730#endif
626 { 731 {
732 ev_now = ev_time ();
733
627 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 734 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
628 { 735 {
629 periodics_reschedule (ev_now - now); 736 periodics_reschedule (ev_now - now);
630 737
631 /* adjust timers. this is easy, as the offset is the same for all */ 738 /* adjust timers. this is easy, as the offset is the same for all */
632 for (i = 0; i < timercnt; ++i) 739 for (i = 0; i < timercnt; ++i)
645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
646 753
647 do 754 do
648 { 755 {
649 /* queue check watchers (and execute them) */ 756 /* queue check watchers (and execute them) */
650 if (preparecnt) 757 if (expect_false (preparecnt))
651 { 758 {
652 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 759 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
653 call_pending (); 760 call_pending ();
654 } 761 }
655 762
658 765
659 /* calculate blocking time */ 766 /* calculate blocking time */
660 767
661 /* we only need this for !monotonic clockor timers, but as we basically 768 /* we only need this for !monotonic clockor timers, but as we basically
662 always have timers, we just calculate it always */ 769 always have timers, we just calculate it always */
770#if EV_USE_MONOTONIC
771 if (expect_true (have_monotonic))
772 time_update_monotonic ();
773 else
774#endif
775 {
663 ev_now = ev_time (); 776 ev_now = ev_time ();
777 now = ev_now;
778 }
664 779
665 if (flags & EVLOOP_NONBLOCK || idlecnt) 780 if (flags & EVLOOP_NONBLOCK || idlecnt)
666 block = 0.; 781 block = 0.;
667 else 782 else
668 { 783 {
669 block = MAX_BLOCKTIME; 784 block = MAX_BLOCKTIME;
670 785
671 if (timercnt) 786 if (timercnt)
672 { 787 {
673 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 788 ev_tstamp to = timers [0]->at - now + method_fudge;
674 if (block > to) block = to; 789 if (block > to) block = to;
675 } 790 }
676 791
677 if (periodiccnt) 792 if (periodiccnt)
678 { 793 {
731 head = &(*head)->next; 846 head = &(*head)->next;
732 } 847 }
733} 848}
734 849
735static void 850static void
736ev_clear (W w) 851ev_clear_pending (W w)
737{ 852{
738 if (w->pending) 853 if (w->pending)
739 { 854 {
740 pendings [w->pending - 1].w = 0; 855 pendings [ABSPRI (w)][w->pending - 1].w = 0;
741 w->pending = 0; 856 w->pending = 0;
742 } 857 }
743} 858}
744 859
745static void 860static void
746ev_start (W w, int active) 861ev_start (W w, int active)
747{ 862{
863 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
864 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
865
748 w->active = active; 866 w->active = active;
749} 867}
750 868
751static void 869static void
752ev_stop (W w) 870ev_stop (W w)
757/*****************************************************************************/ 875/*****************************************************************************/
758 876
759void 877void
760ev_io_start (struct ev_io *w) 878ev_io_start (struct ev_io *w)
761{ 879{
880 int fd = w->fd;
881
762 if (ev_is_active (w)) 882 if (ev_is_active (w))
763 return; 883 return;
764 884
765 int fd = w->fd; 885 assert (("ev_io_start called with negative fd", fd >= 0));
766 886
767 ev_start ((W)w, 1); 887 ev_start ((W)w, 1);
768 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 888 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
769 wlist_add ((WL *)&anfds[fd].head, (WL)w); 889 wlist_add ((WL *)&anfds[fd].head, (WL)w);
770 890
772} 892}
773 893
774void 894void
775ev_io_stop (struct ev_io *w) 895ev_io_stop (struct ev_io *w)
776{ 896{
777 ev_clear ((W)w); 897 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 898 if (!ev_is_active (w))
779 return; 899 return;
780 900
781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 901 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
782 ev_stop ((W)w); 902 ev_stop ((W)w);
790 if (ev_is_active (w)) 910 if (ev_is_active (w))
791 return; 911 return;
792 912
793 w->at += now; 913 w->at += now;
794 914
795 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 915 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
796 916
797 ev_start ((W)w, ++timercnt); 917 ev_start ((W)w, ++timercnt);
798 array_needsize (timers, timermax, timercnt, ); 918 array_needsize (timers, timermax, timercnt, );
799 timers [timercnt - 1] = w; 919 timers [timercnt - 1] = w;
800 upheap ((WT *)timers, timercnt - 1); 920 upheap ((WT *)timers, timercnt - 1);
801} 921}
802 922
803void 923void
804ev_timer_stop (struct ev_timer *w) 924ev_timer_stop (struct ev_timer *w)
805{ 925{
806 ev_clear ((W)w); 926 ev_clear_pending ((W)w);
807 if (!ev_is_active (w)) 927 if (!ev_is_active (w))
808 return; 928 return;
809 929
810 if (w->active < timercnt--) 930 if (w->active < timercnt--)
811 { 931 {
839ev_periodic_start (struct ev_periodic *w) 959ev_periodic_start (struct ev_periodic *w)
840{ 960{
841 if (ev_is_active (w)) 961 if (ev_is_active (w))
842 return; 962 return;
843 963
844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 964 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
845 965
846 /* this formula differs from the one in periodic_reify because we do not always round up */ 966 /* this formula differs from the one in periodic_reify because we do not always round up */
847 if (w->interval) 967 if (w->interval)
848 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 968 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
849 969
854} 974}
855 975
856void 976void
857ev_periodic_stop (struct ev_periodic *w) 977ev_periodic_stop (struct ev_periodic *w)
858{ 978{
859 ev_clear ((W)w); 979 ev_clear_pending ((W)w);
860 if (!ev_is_active (w)) 980 if (!ev_is_active (w))
861 return; 981 return;
862 982
863 if (w->active < periodiccnt--) 983 if (w->active < periodiccnt--)
864 { 984 {
872void 992void
873ev_signal_start (struct ev_signal *w) 993ev_signal_start (struct ev_signal *w)
874{ 994{
875 if (ev_is_active (w)) 995 if (ev_is_active (w))
876 return; 996 return;
997
998 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
877 999
878 ev_start ((W)w, 1); 1000 ev_start ((W)w, 1);
879 array_needsize (signals, signalmax, w->signum, signals_init); 1001 array_needsize (signals, signalmax, w->signum, signals_init);
880 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1002 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
881 1003
890} 1012}
891 1013
892void 1014void
893ev_signal_stop (struct ev_signal *w) 1015ev_signal_stop (struct ev_signal *w)
894{ 1016{
895 ev_clear ((W)w); 1017 ev_clear_pending ((W)w);
896 if (!ev_is_active (w)) 1018 if (!ev_is_active (w))
897 return; 1019 return;
898 1020
899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1021 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
900 ev_stop ((W)w); 1022 ev_stop ((W)w);
915} 1037}
916 1038
917void 1039void
918ev_idle_stop (struct ev_idle *w) 1040ev_idle_stop (struct ev_idle *w)
919{ 1041{
920 ev_clear ((W)w); 1042 ev_clear_pending ((W)w);
921 if (ev_is_active (w)) 1043 if (ev_is_active (w))
922 return; 1044 return;
923 1045
924 idles [w->active - 1] = idles [--idlecnt]; 1046 idles [w->active - 1] = idles [--idlecnt];
925 ev_stop ((W)w); 1047 ev_stop ((W)w);
937} 1059}
938 1060
939void 1061void
940ev_prepare_stop (struct ev_prepare *w) 1062ev_prepare_stop (struct ev_prepare *w)
941{ 1063{
942 ev_clear ((W)w); 1064 ev_clear_pending ((W)w);
943 if (ev_is_active (w)) 1065 if (ev_is_active (w))
944 return; 1066 return;
945 1067
946 prepares [w->active - 1] = prepares [--preparecnt]; 1068 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w); 1069 ev_stop ((W)w);
959} 1081}
960 1082
961void 1083void
962ev_check_stop (struct ev_check *w) 1084ev_check_stop (struct ev_check *w)
963{ 1085{
964 ev_clear ((W)w); 1086 ev_clear_pending ((W)w);
965 if (ev_is_active (w)) 1087 if (ev_is_active (w))
966 return; 1088 return;
967 1089
968 checks [w->active - 1] = checks [--checkcnt]; 1090 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w); 1091 ev_stop ((W)w);
980} 1102}
981 1103
982void 1104void
983ev_child_stop (struct ev_child *w) 1105ev_child_stop (struct ev_child *w)
984{ 1106{
985 ev_clear ((W)w); 1107 ev_clear_pending ((W)w);
986 if (ev_is_active (w)) 1108 if (ev_is_active (w))
987 return; 1109 return;
988 1110
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1111 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w); 1112 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines