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

Comparing libev/ev.c (file contents):
Revision 1.30 by root, Thu Nov 1 08:28:33 2007 UTC vs.
Revision 1.44 by root, Fri Nov 2 20:59:14 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_KQUEUE
70# define EV_USE_KQUEUE 0
71#endif
72
63#ifndef EV_USE_REALTIME 73#ifndef EV_USE_REALTIME
64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 74# define EV_USE_REALTIME 1
65#endif 75#endif
76
77/**/
78
79#ifndef CLOCK_MONOTONIC
80# undef EV_USE_MONOTONIC
81# define EV_USE_MONOTONIC 0
82#endif
83
84#ifndef CLOCK_REALTIME
85# undef EV_USE_REALTIME
86# define EV_USE_REALTIME 0
87#endif
88
89/**/
66 90
67#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 91#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
68#define MAX_BLOCKTIME 59.731 92#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
69#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 93#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
94/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
70 95
71#include "ev.h" 96#include "ev.h"
97
98#if __GNUC__ >= 3
99# define expect(expr,value) __builtin_expect ((expr),(value))
100# define inline inline
101#else
102# define expect(expr,value) (expr)
103# define inline static
104#endif
105
106#define expect_false(expr) expect ((expr) != 0, 0)
107#define expect_true(expr) expect ((expr) != 0, 1)
108
109#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
110#define ABSPRI(w) ((w)->priority - EV_MINPRI)
72 111
73typedef struct ev_watcher *W; 112typedef struct ev_watcher *W;
74typedef struct ev_watcher_list *WL; 113typedef struct ev_watcher_list *WL;
75typedef struct ev_watcher_time *WT; 114typedef struct ev_watcher_time *WT;
76 115
77static ev_tstamp now, diff; /* monotonic clock */ 116static ev_tstamp now_floor, now, diff; /* monotonic clock */
78ev_tstamp ev_now; 117ev_tstamp ev_now;
79int ev_method; 118int ev_method;
80 119
81static int have_monotonic; /* runtime */ 120static int have_monotonic; /* runtime */
82 121
102 141
103static ev_tstamp 142static ev_tstamp
104get_clock (void) 143get_clock (void)
105{ 144{
106#if EV_USE_MONOTONIC 145#if EV_USE_MONOTONIC
107 if (have_monotonic) 146 if (expect_true (have_monotonic))
108 { 147 {
109 struct timespec ts; 148 struct timespec ts;
110 clock_gettime (CLOCK_MONOTONIC, &ts); 149 clock_gettime (CLOCK_MONOTONIC, &ts);
111 return ts.tv_sec + ts.tv_nsec * 1e-9; 150 return ts.tv_sec + ts.tv_nsec * 1e-9;
112 } 151 }
116} 155}
117 156
118#define array_roundsize(base,n) ((n) | 4 & ~3) 157#define array_roundsize(base,n) ((n) | 4 & ~3)
119 158
120#define array_needsize(base,cur,cnt,init) \ 159#define array_needsize(base,cur,cnt,init) \
121 if ((cnt) > cur) \ 160 if (expect_false ((cnt) > cur)) \
122 { \ 161 { \
123 int newcnt = cur; \ 162 int newcnt = cur; \
124 do \ 163 do \
125 { \ 164 { \
126 newcnt = array_roundsize (base, newcnt << 1); \ 165 newcnt = array_roundsize (base, newcnt << 1); \
135/*****************************************************************************/ 174/*****************************************************************************/
136 175
137typedef struct 176typedef struct
138{ 177{
139 struct ev_io *head; 178 struct ev_io *head;
140 int events; 179 unsigned char events;
180 unsigned char reify;
141} ANFD; 181} ANFD;
142 182
143static ANFD *anfds; 183static ANFD *anfds;
144static int anfdmax; 184static int anfdmax;
145 185
148{ 188{
149 while (count--) 189 while (count--)
150 { 190 {
151 base->head = 0; 191 base->head = 0;
152 base->events = EV_NONE; 192 base->events = EV_NONE;
193 base->reify = 0;
194
153 ++base; 195 ++base;
154 } 196 }
155} 197}
156 198
157typedef struct 199typedef struct
158{ 200{
159 W w; 201 W w;
160 int events; 202 int events;
161} ANPENDING; 203} ANPENDING;
162 204
163static ANPENDING *pendings; 205static ANPENDING *pendings [NUMPRI];
164static int pendingmax, pendingcnt; 206static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
165 207
166static void 208static void
167event (W w, int events) 209event (W w, int events)
168{ 210{
211 if (w->pending)
212 {
213 pendings [ABSPRI (w)][w->pending - 1].events |= events;
214 return;
215 }
216
169 w->pending = ++pendingcnt; 217 w->pending = ++pendingcnt [ABSPRI (w)];
170 array_needsize (pendings, pendingmax, pendingcnt, ); 218 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
171 pendings [pendingcnt - 1].w = w; 219 pendings [ABSPRI (w)][w->pending - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 220 pendings [ABSPRI (w)][w->pending - 1].events = events;
173} 221}
174 222
175static void 223static void
176queue_events (W *events, int eventcnt, int type) 224queue_events (W *events, int eventcnt, int type)
177{ 225{
215 int events = 0; 263 int events = 0;
216 264
217 for (w = anfd->head; w; w = w->next) 265 for (w = anfd->head; w; w = w->next)
218 events |= w->events; 266 events |= w->events;
219 267
220 anfd->events &= ~EV_REIFY; 268 anfd->reify = 0;
221 269
222 if (anfd->events != events) 270 if (anfd->events != events)
223 { 271 {
224 method_modify (fd, anfd->events, events); 272 method_modify (fd, anfd->events, events);
225 anfd->events = events; 273 anfd->events = events;
230} 278}
231 279
232static void 280static void
233fd_change (int fd) 281fd_change (int fd)
234{ 282{
235 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 283 if (anfds [fd].reify || fdchangecnt < 0)
236 return; 284 return;
237 285
238 anfds [fd].events |= EV_REIFY; 286 anfds [fd].reify = 1;
239 287
240 ++fdchangecnt; 288 ++fdchangecnt;
241 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 289 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
242 fdchanges [fdchangecnt - 1] = fd; 290 fdchanges [fdchangecnt - 1] = fd;
243} 291}
244 292
293static void
294fd_kill (int fd)
295{
296 struct ev_io *w;
297
298 printf ("killing fd %d\n", fd);//D
299 while ((w = anfds [fd].head))
300 {
301 ev_io_stop (w);
302 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
303 }
304}
305
245/* called on EBADF to verify fds */ 306/* called on EBADF to verify fds */
246static void 307static void
247fd_recheck (void) 308fd_ebadf (void)
248{ 309{
249 int fd; 310 int fd;
250 311
251 for (fd = 0; fd < anfdmax; ++fd) 312 for (fd = 0; fd < anfdmax; ++fd)
252 if (anfds [fd].events) 313 if (anfds [fd].events)
253 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 314 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
254 while (anfds [fd].head) 315 fd_kill (fd);
316}
317
318/* called on ENOMEM in select/poll to kill some fds and retry */
319static void
320fd_enomem (void)
321{
322 int fd = anfdmax;
323
324 while (fd--)
325 if (anfds [fd].events)
255 { 326 {
256 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 327 close (fd);
257 ev_io_stop (anfds [fd].head); 328 fd_kill (fd);
329 return;
258 } 330 }
259} 331}
260 332
261/*****************************************************************************/ 333/*****************************************************************************/
262 334
263static struct ev_timer **timers; 335static struct ev_timer **timers;
310/*****************************************************************************/ 382/*****************************************************************************/
311 383
312typedef struct 384typedef struct
313{ 385{
314 struct ev_signal *head; 386 struct ev_signal *head;
315 sig_atomic_t gotsig; 387 sig_atomic_t volatile gotsig;
316} ANSIG; 388} ANSIG;
317 389
318static ANSIG *signals; 390static ANSIG *signals;
319static int signalmax; 391static int signalmax;
320 392
321static int sigpipe [2]; 393static int sigpipe [2];
322static sig_atomic_t gotsig; 394static sig_atomic_t volatile gotsig;
323static struct ev_io sigev; 395static struct ev_io sigev;
324 396
325static void 397static void
326signals_init (ANSIG *base, int count) 398signals_init (ANSIG *base, int count)
327{ 399{
328 while (count--) 400 while (count--)
329 { 401 {
330 base->head = 0; 402 base->head = 0;
331 base->gotsig = 0; 403 base->gotsig = 0;
404
332 ++base; 405 ++base;
333 } 406 }
334} 407}
335 408
336static void 409static void
339 signals [signum - 1].gotsig = 1; 412 signals [signum - 1].gotsig = 1;
340 413
341 if (!gotsig) 414 if (!gotsig)
342 { 415 {
343 gotsig = 1; 416 gotsig = 1;
344 write (sigpipe [1], &gotsig, 1); 417 write (sigpipe [1], &signum, 1);
345 } 418 }
346} 419}
347 420
348static void 421static void
349sigcb (struct ev_io *iow, int revents) 422sigcb (struct ev_io *iow, int revents)
350{ 423{
351 struct ev_signal *w; 424 struct ev_signal *w;
352 int sig; 425 int signum;
353 426
427 read (sigpipe [0], &revents, 1);
354 gotsig = 0; 428 gotsig = 0;
355 read (sigpipe [0], &revents, 1);
356 429
357 for (sig = signalmax; sig--; ) 430 for (signum = signalmax; signum--; )
358 if (signals [sig].gotsig) 431 if (signals [signum].gotsig)
359 { 432 {
360 signals [sig].gotsig = 0; 433 signals [signum].gotsig = 0;
361 434
362 for (w = signals [sig].head; w; w = w->next) 435 for (w = signals [signum].head; w; w = w->next)
363 event ((W)w, EV_SIGNAL); 436 event ((W)w, EV_SIGNAL);
364 } 437 }
365} 438}
366 439
367static void 440static void
404 struct ev_child *w; 477 struct ev_child *w;
405 int pid, status; 478 int pid, status;
406 479
407 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 480 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
408 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 481 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
409 if (w->pid == pid || w->pid == -1) 482 if (w->pid == pid || !w->pid)
410 { 483 {
411 w->status = status; 484 w->status = status;
412 event ((W)w, EV_CHILD); 485 event ((W)w, EV_CHILD);
413 } 486 }
414} 487}
415 488
416/*****************************************************************************/ 489/*****************************************************************************/
417 490
491#if EV_USE_KQUEUE
492# include "ev_kqueue.c"
493#endif
418#if EV_USE_EPOLL 494#if EV_USE_EPOLL
419# include "ev_epoll.c" 495# include "ev_epoll.c"
420#endif 496#endif
497#if EV_USE_POLL
498# include "ev_poll.c"
499#endif
421#if EV_USE_SELECT 500#if EV_USE_SELECT
422# include "ev_select.c" 501# include "ev_select.c"
423#endif 502#endif
424 503
425int 504int
432ev_version_minor (void) 511ev_version_minor (void)
433{ 512{
434 return EV_VERSION_MINOR; 513 return EV_VERSION_MINOR;
435} 514}
436 515
516/* return true if we are running with elevated privileges and ignore env variables */
517static int
518enable_secure ()
519{
520 return getuid () != geteuid ()
521 || getgid () != getegid ();
522}
523
437int ev_init (int flags) 524int ev_init (int methods)
438{ 525{
439 if (!ev_method) 526 if (!ev_method)
440 { 527 {
441#if EV_USE_MONOTONIC 528#if EV_USE_MONOTONIC
442 { 529 {
444 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 531 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
445 have_monotonic = 1; 532 have_monotonic = 1;
446 } 533 }
447#endif 534#endif
448 535
449 ev_now = ev_time (); 536 ev_now = ev_time ();
450 now = get_clock (); 537 now = get_clock ();
538 now_floor = now;
451 diff = ev_now - now; 539 diff = ev_now - now;
452 540
453 if (pipe (sigpipe)) 541 if (pipe (sigpipe))
454 return 0; 542 return 0;
455 543
544 if (methods == EVMETHOD_AUTO)
545 if (!enable_secure () && getenv ("LIBEV_METHODS"))
546 methods = atoi (getenv ("LIBEV_METHODS"));
547 else
456 ev_method = EVMETHOD_NONE; 548 methods = EVMETHOD_ANY;
549
550 ev_method = 0;
551#if EV_USE_KQUEUE
552 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
553#endif
457#if EV_USE_EPOLL 554#if EV_USE_EPOLL
458 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 555 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
556#endif
557#if EV_USE_POLL
558 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
459#endif 559#endif
460#if EV_USE_SELECT 560#if EV_USE_SELECT
461 if (ev_method == EVMETHOD_NONE) select_init (flags); 561 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
462#endif 562#endif
463 563
464 if (ev_method) 564 if (ev_method)
465 { 565 {
466 ev_watcher_init (&sigev, sigcb); 566 ev_watcher_init (&sigev, sigcb);
475} 575}
476 576
477/*****************************************************************************/ 577/*****************************************************************************/
478 578
479void 579void
480ev_prefork (void) 580ev_fork_prepare (void)
481{ 581{
482 /* nop */ 582 /* nop */
483} 583}
484 584
485void 585void
486ev_postfork_parent (void) 586ev_fork_parent (void)
487{ 587{
488 /* nop */ 588 /* nop */
489} 589}
490 590
491void 591void
492ev_postfork_child (void) 592ev_fork_child (void)
493{ 593{
494#if EV_USE_EPOLL 594#if EV_USE_EPOLL
495 if (ev_method == EVMETHOD_EPOLL) 595 if (ev_method == EVMETHOD_EPOLL)
496 epoll_postfork_child (); 596 epoll_postfork_child ();
497#endif 597#endif
506/*****************************************************************************/ 606/*****************************************************************************/
507 607
508static void 608static void
509call_pending (void) 609call_pending (void)
510{ 610{
611 int pri;
612
613 for (pri = NUMPRI; pri--; )
511 while (pendingcnt) 614 while (pendingcnt [pri])
512 { 615 {
513 ANPENDING *p = pendings + --pendingcnt; 616 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
514 617
515 if (p->w) 618 if (p->w)
516 { 619 {
517 p->w->pending = 0; 620 p->w->pending = 0;
518 p->w->cb (p->w, p->events); 621 p->w->cb (p->w, p->events);
519 } 622 }
520 } 623 }
521} 624}
522 625
523static void 626static void
524timers_reify (void) 627timers_reify (void)
525{ 628{
528 struct ev_timer *w = timers [0]; 631 struct ev_timer *w = timers [0];
529 632
530 /* first reschedule or stop timer */ 633 /* first reschedule or stop timer */
531 if (w->repeat) 634 if (w->repeat)
532 { 635 {
636 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 637 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 638 downheap ((WT *)timers, timercnt, 0);
536 } 639 }
537 else 640 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 641 ev_timer_stop (w); /* nonrepeating: stop timer */
539 642
550 653
551 /* first reschedule or stop timer */ 654 /* first reschedule or stop timer */
552 if (w->interval) 655 if (w->interval)
553 { 656 {
554 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 657 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
555 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 658 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
556 downheap ((WT *)periodics, periodiccnt, 0); 659 downheap ((WT *)periodics, periodiccnt, 0);
557 } 660 }
558 else 661 else
559 ev_periodic_stop (w); /* nonrepeating: stop timer */ 662 ev_periodic_stop (w); /* nonrepeating: stop timer */
560 663
561 event ((W)w, EV_TIMEOUT); 664 event ((W)w, EV_PERIODIC);
562 } 665 }
563} 666}
564 667
565static void 668static void
566periodics_reschedule (ev_tstamp diff) 669periodics_reschedule (ev_tstamp diff)
585 } 688 }
586 } 689 }
587 } 690 }
588} 691}
589 692
693static int
694time_update_monotonic (void)
695{
696 now = get_clock ();
697
698 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
699 {
700 ev_now = now + diff;
701 return 0;
702 }
703 else
704 {
705 now_floor = now;
706 ev_now = ev_time ();
707 return 1;
708 }
709}
710
590static void 711static void
591time_update (void) 712time_update (void)
592{ 713{
593 int i; 714 int i;
594 715
595 ev_now = ev_time (); 716#if EV_USE_MONOTONIC
596
597 if (have_monotonic) 717 if (expect_true (have_monotonic))
598 { 718 {
719 if (time_update_monotonic ())
720 {
599 ev_tstamp odiff = diff; 721 ev_tstamp odiff = diff;
600 722
601 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 723 for (i = 4; --i; ) /* loop a few times, before making important decisions */
602 { 724 {
603 now = get_clock ();
604 diff = ev_now - now; 725 diff = ev_now - now;
605 726
606 if (fabs (odiff - diff) < MIN_TIMEJUMP) 727 if (fabs (odiff - diff) < MIN_TIMEJUMP)
607 return; /* all is well */ 728 return; /* all is well */
608 729
609 ev_now = ev_time (); 730 ev_now = ev_time ();
731 now = get_clock ();
732 now_floor = now;
610 } 733 }
611 734
612 periodics_reschedule (diff - odiff); 735 periodics_reschedule (diff - odiff);
613 /* no timer adjustment, as the monotonic clock doesn't jump */ 736 /* no timer adjustment, as the monotonic clock doesn't jump */
737 }
614 } 738 }
615 else 739 else
740#endif
616 { 741 {
742 ev_now = ev_time ();
743
617 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 744 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
618 { 745 {
619 periodics_reschedule (ev_now - now); 746 periodics_reschedule (ev_now - now);
620 747
621 /* adjust timers. this is easy, as the offset is the same for all */ 748 /* adjust timers. this is easy, as the offset is the same for all */
622 for (i = 0; i < timercnt; ++i) 749 for (i = 0; i < timercnt; ++i)
635 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 762 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
636 763
637 do 764 do
638 { 765 {
639 /* queue check watchers (and execute them) */ 766 /* queue check watchers (and execute them) */
640 if (preparecnt) 767 if (expect_false (preparecnt))
641 { 768 {
642 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 769 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
643 call_pending (); 770 call_pending ();
644 } 771 }
645 772
648 775
649 /* calculate blocking time */ 776 /* calculate blocking time */
650 777
651 /* we only need this for !monotonic clockor timers, but as we basically 778 /* we only need this for !monotonic clockor timers, but as we basically
652 always have timers, we just calculate it always */ 779 always have timers, we just calculate it always */
780#if EV_USE_MONOTONIC
781 if (expect_true (have_monotonic))
782 time_update_monotonic ();
783 else
784#endif
785 {
653 ev_now = ev_time (); 786 ev_now = ev_time ();
787 now = ev_now;
788 }
654 789
655 if (flags & EVLOOP_NONBLOCK || idlecnt) 790 if (flags & EVLOOP_NONBLOCK || idlecnt)
656 block = 0.; 791 block = 0.;
657 else 792 else
658 { 793 {
659 block = MAX_BLOCKTIME; 794 block = MAX_BLOCKTIME;
660 795
661 if (timercnt) 796 if (timercnt)
662 { 797 {
663 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 798 ev_tstamp to = timers [0]->at - now + method_fudge;
664 if (block > to) block = to; 799 if (block > to) block = to;
665 } 800 }
666 801
667 if (periodiccnt) 802 if (periodiccnt)
668 { 803 {
721 head = &(*head)->next; 856 head = &(*head)->next;
722 } 857 }
723} 858}
724 859
725static void 860static void
726ev_clear (W w) 861ev_clear_pending (W w)
727{ 862{
728 if (w->pending) 863 if (w->pending)
729 { 864 {
730 pendings [w->pending - 1].w = 0; 865 pendings [ABSPRI (w)][w->pending - 1].w = 0;
731 w->pending = 0; 866 w->pending = 0;
732 } 867 }
733} 868}
734 869
735static void 870static void
736ev_start (W w, int active) 871ev_start (W w, int active)
737{ 872{
873 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
874 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
875
738 w->active = active; 876 w->active = active;
739} 877}
740 878
741static void 879static void
742ev_stop (W w) 880ev_stop (W w)
747/*****************************************************************************/ 885/*****************************************************************************/
748 886
749void 887void
750ev_io_start (struct ev_io *w) 888ev_io_start (struct ev_io *w)
751{ 889{
890 int fd = w->fd;
891
752 if (ev_is_active (w)) 892 if (ev_is_active (w))
753 return; 893 return;
754 894
755 int fd = w->fd; 895 assert (("ev_io_start called with negative fd", fd >= 0));
756 896
757 ev_start ((W)w, 1); 897 ev_start ((W)w, 1);
758 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 898 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
759 wlist_add ((WL *)&anfds[fd].head, (WL)w); 899 wlist_add ((WL *)&anfds[fd].head, (WL)w);
760 900
762} 902}
763 903
764void 904void
765ev_io_stop (struct ev_io *w) 905ev_io_stop (struct ev_io *w)
766{ 906{
767 ev_clear ((W)w); 907 ev_clear_pending ((W)w);
768 if (!ev_is_active (w)) 908 if (!ev_is_active (w))
769 return; 909 return;
770 910
771 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 911 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
772 ev_stop ((W)w); 912 ev_stop ((W)w);
780 if (ev_is_active (w)) 920 if (ev_is_active (w))
781 return; 921 return;
782 922
783 w->at += now; 923 w->at += now;
784 924
785 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 925 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
786 926
787 ev_start ((W)w, ++timercnt); 927 ev_start ((W)w, ++timercnt);
788 array_needsize (timers, timermax, timercnt, ); 928 array_needsize (timers, timermax, timercnt, );
789 timers [timercnt - 1] = w; 929 timers [timercnt - 1] = w;
790 upheap ((WT *)timers, timercnt - 1); 930 upheap ((WT *)timers, timercnt - 1);
791} 931}
792 932
793void 933void
794ev_timer_stop (struct ev_timer *w) 934ev_timer_stop (struct ev_timer *w)
795{ 935{
796 ev_clear ((W)w); 936 ev_clear_pending ((W)w);
797 if (!ev_is_active (w)) 937 if (!ev_is_active (w))
798 return; 938 return;
799 939
800 if (w->active < timercnt--) 940 if (w->active < timercnt--)
801 { 941 {
829ev_periodic_start (struct ev_periodic *w) 969ev_periodic_start (struct ev_periodic *w)
830{ 970{
831 if (ev_is_active (w)) 971 if (ev_is_active (w))
832 return; 972 return;
833 973
834 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 974 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
835 975
836 /* this formula differs from the one in periodic_reify because we do not always round up */ 976 /* this formula differs from the one in periodic_reify because we do not always round up */
837 if (w->interval) 977 if (w->interval)
838 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 978 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
839 979
844} 984}
845 985
846void 986void
847ev_periodic_stop (struct ev_periodic *w) 987ev_periodic_stop (struct ev_periodic *w)
848{ 988{
849 ev_clear ((W)w); 989 ev_clear_pending ((W)w);
850 if (!ev_is_active (w)) 990 if (!ev_is_active (w))
851 return; 991 return;
852 992
853 if (w->active < periodiccnt--) 993 if (w->active < periodiccnt--)
854 { 994 {
862void 1002void
863ev_signal_start (struct ev_signal *w) 1003ev_signal_start (struct ev_signal *w)
864{ 1004{
865 if (ev_is_active (w)) 1005 if (ev_is_active (w))
866 return; 1006 return;
1007
1008 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
867 1009
868 ev_start ((W)w, 1); 1010 ev_start ((W)w, 1);
869 array_needsize (signals, signalmax, w->signum, signals_init); 1011 array_needsize (signals, signalmax, w->signum, signals_init);
870 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1012 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
871 1013
880} 1022}
881 1023
882void 1024void
883ev_signal_stop (struct ev_signal *w) 1025ev_signal_stop (struct ev_signal *w)
884{ 1026{
885 ev_clear ((W)w); 1027 ev_clear_pending ((W)w);
886 if (!ev_is_active (w)) 1028 if (!ev_is_active (w))
887 return; 1029 return;
888 1030
889 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1031 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
890 ev_stop ((W)w); 1032 ev_stop ((W)w);
905} 1047}
906 1048
907void 1049void
908ev_idle_stop (struct ev_idle *w) 1050ev_idle_stop (struct ev_idle *w)
909{ 1051{
910 ev_clear ((W)w); 1052 ev_clear_pending ((W)w);
911 if (ev_is_active (w)) 1053 if (ev_is_active (w))
912 return; 1054 return;
913 1055
914 idles [w->active - 1] = idles [--idlecnt]; 1056 idles [w->active - 1] = idles [--idlecnt];
915 ev_stop ((W)w); 1057 ev_stop ((W)w);
927} 1069}
928 1070
929void 1071void
930ev_prepare_stop (struct ev_prepare *w) 1072ev_prepare_stop (struct ev_prepare *w)
931{ 1073{
932 ev_clear ((W)w); 1074 ev_clear_pending ((W)w);
933 if (ev_is_active (w)) 1075 if (ev_is_active (w))
934 return; 1076 return;
935 1077
936 prepares [w->active - 1] = prepares [--preparecnt]; 1078 prepares [w->active - 1] = prepares [--preparecnt];
937 ev_stop ((W)w); 1079 ev_stop ((W)w);
949} 1091}
950 1092
951void 1093void
952ev_check_stop (struct ev_check *w) 1094ev_check_stop (struct ev_check *w)
953{ 1095{
954 ev_clear ((W)w); 1096 ev_clear_pending ((W)w);
955 if (ev_is_active (w)) 1097 if (ev_is_active (w))
956 return; 1098 return;
957 1099
958 checks [w->active - 1] = checks [--checkcnt]; 1100 checks [w->active - 1] = checks [--checkcnt];
959 ev_stop ((W)w); 1101 ev_stop ((W)w);
970} 1112}
971 1113
972void 1114void
973ev_child_stop (struct ev_child *w) 1115ev_child_stop (struct ev_child *w)
974{ 1116{
975 ev_clear ((W)w); 1117 ev_clear_pending ((W)w);
976 if (ev_is_active (w)) 1118 if (ev_is_active (w))
977 return; 1119 return;
978 1120
979 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1121 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
980 ev_stop ((W)w); 1122 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines