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

Comparing libev/ev.c (file contents):
Revision 1.31 by root, Thu Nov 1 09:05:33 2007 UTC vs.
Revision 1.41 by root, Fri Nov 2 16:54:34 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)
76 104
77typedef struct ev_watcher *W; 105typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 106typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 107typedef struct ev_watcher_time *WT;
80 108
81static ev_tstamp now, diff; /* monotonic clock */ 109static ev_tstamp now_floor, now, diff; /* monotonic clock */
82ev_tstamp ev_now; 110ev_tstamp ev_now;
83int ev_method; 111int ev_method;
84 112
85static int have_monotonic; /* runtime */ 113static int have_monotonic; /* runtime */
86 114
106 134
107static ev_tstamp 135static ev_tstamp
108get_clock (void) 136get_clock (void)
109{ 137{
110#if EV_USE_MONOTONIC 138#if EV_USE_MONOTONIC
111 if (have_monotonic) 139 if (expect_true (have_monotonic))
112 { 140 {
113 struct timespec ts; 141 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 142 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 143 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 144 }
120} 148}
121 149
122#define array_roundsize(base,n) ((n) | 4 & ~3) 150#define array_roundsize(base,n) ((n) | 4 & ~3)
123 151
124#define array_needsize(base,cur,cnt,init) \ 152#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 153 if (expect_false ((cnt) > cur)) \
126 { \ 154 { \
127 int newcnt = cur; \ 155 int newcnt = cur; \
128 do \ 156 do \
129 { \ 157 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 158 newcnt = array_roundsize (base, newcnt << 1); \
139/*****************************************************************************/ 167/*****************************************************************************/
140 168
141typedef struct 169typedef struct
142{ 170{
143 struct ev_io *head; 171 struct ev_io *head;
144 int events; 172 unsigned char events;
173 unsigned char reify;
145} ANFD; 174} ANFD;
146 175
147static ANFD *anfds; 176static ANFD *anfds;
148static int anfdmax; 177static int anfdmax;
149 178
152{ 181{
153 while (count--) 182 while (count--)
154 { 183 {
155 base->head = 0; 184 base->head = 0;
156 base->events = EV_NONE; 185 base->events = EV_NONE;
186 base->reify = 0;
187
157 ++base; 188 ++base;
158 } 189 }
159} 190}
160 191
161typedef struct 192typedef struct
168static int pendingmax, pendingcnt; 199static int pendingmax, pendingcnt;
169 200
170static void 201static void
171event (W w, int events) 202event (W w, int events)
172{ 203{
204 if (w->pending)
205 {
206 pendings [w->pending - 1].events |= events;
207 return;
208 }
209
173 w->pending = ++pendingcnt; 210 w->pending = ++pendingcnt;
174 array_needsize (pendings, pendingmax, pendingcnt, ); 211 array_needsize (pendings, pendingmax, pendingcnt, );
175 pendings [pendingcnt - 1].w = w; 212 pendings [pendingcnt - 1].w = w;
176 pendings [pendingcnt - 1].events = events; 213 pendings [pendingcnt - 1].events = events;
177} 214}
219 int events = 0; 256 int events = 0;
220 257
221 for (w = anfd->head; w; w = w->next) 258 for (w = anfd->head; w; w = w->next)
222 events |= w->events; 259 events |= w->events;
223 260
224 anfd->events &= ~EV_REIFY; 261 anfd->reify = 0;
225 262
226 if (anfd->events != events) 263 if (anfd->events != events)
227 { 264 {
228 method_modify (fd, anfd->events, events); 265 method_modify (fd, anfd->events, events);
229 anfd->events = events; 266 anfd->events = events;
234} 271}
235 272
236static void 273static void
237fd_change (int fd) 274fd_change (int fd)
238{ 275{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 276 if (anfds [fd].reify || fdchangecnt < 0)
240 return; 277 return;
241 278
242 anfds [fd].events |= EV_REIFY; 279 anfds [fd].reify = 1;
243 280
244 ++fdchangecnt; 281 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 282 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd; 283 fdchanges [fdchangecnt - 1] = fd;
247} 284}
248 285
286static void
287fd_kill (int fd)
288{
289 struct ev_io *w;
290
291 printf ("killing fd %d\n", fd);//D
292 while ((w = anfds [fd].head))
293 {
294 ev_io_stop (w);
295 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
296 }
297}
298
249/* called on EBADF to verify fds */ 299/* called on EBADF to verify fds */
250static void 300static void
251fd_recheck (void) 301fd_ebadf (void)
252{ 302{
253 int fd; 303 int fd;
254 304
255 for (fd = 0; fd < anfdmax; ++fd) 305 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events) 306 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 307 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 while (anfds [fd].head) 308 fd_kill (fd);
309}
310
311/* called on ENOMEM in select/poll to kill some fds and retry */
312static void
313fd_enomem (void)
314{
315 int fd = anfdmax;
316
317 while (fd--)
318 if (anfds [fd].events)
259 { 319 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 320 close (fd);
261 ev_io_stop (anfds [fd].head); 321 fd_kill (fd);
322 return;
262 } 323 }
263} 324}
264 325
265/*****************************************************************************/ 326/*****************************************************************************/
266 327
267static struct ev_timer **timers; 328static struct ev_timer **timers;
314/*****************************************************************************/ 375/*****************************************************************************/
315 376
316typedef struct 377typedef struct
317{ 378{
318 struct ev_signal *head; 379 struct ev_signal *head;
319 sig_atomic_t gotsig; 380 sig_atomic_t volatile gotsig;
320} ANSIG; 381} ANSIG;
321 382
322static ANSIG *signals; 383static ANSIG *signals;
323static int signalmax; 384static int signalmax;
324 385
325static int sigpipe [2]; 386static int sigpipe [2];
326static sig_atomic_t gotsig; 387static sig_atomic_t volatile gotsig;
327static struct ev_io sigev; 388static struct ev_io sigev;
328 389
329static void 390static void
330signals_init (ANSIG *base, int count) 391signals_init (ANSIG *base, int count)
331{ 392{
332 while (count--) 393 while (count--)
333 { 394 {
334 base->head = 0; 395 base->head = 0;
335 base->gotsig = 0; 396 base->gotsig = 0;
397
336 ++base; 398 ++base;
337 } 399 }
338} 400}
339 401
340static void 402static void
343 signals [signum - 1].gotsig = 1; 405 signals [signum - 1].gotsig = 1;
344 406
345 if (!gotsig) 407 if (!gotsig)
346 { 408 {
347 gotsig = 1; 409 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 410 write (sigpipe [1], &signum, 1);
349 } 411 }
350} 412}
351 413
352static void 414static void
353sigcb (struct ev_io *iow, int revents) 415sigcb (struct ev_io *iow, int revents)
354{ 416{
355 struct ev_signal *w; 417 struct ev_signal *w;
356 int sig; 418 int signum;
357 419
420 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 421 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 422
361 for (sig = signalmax; sig--; ) 423 for (signum = signalmax; signum--; )
362 if (signals [sig].gotsig) 424 if (signals [signum].gotsig)
363 { 425 {
364 signals [sig].gotsig = 0; 426 signals [signum].gotsig = 0;
365 427
366 for (w = signals [sig].head; w; w = w->next) 428 for (w = signals [signum].head; w; w = w->next)
367 event ((W)w, EV_SIGNAL); 429 event ((W)w, EV_SIGNAL);
368 } 430 }
369} 431}
370 432
371static void 433static void
408 struct ev_child *w; 470 struct ev_child *w;
409 int pid, status; 471 int pid, status;
410 472
411 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 473 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
412 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 474 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
413 if (w->pid == pid || w->pid == -1) 475 if (w->pid == pid || !w->pid)
414 { 476 {
415 w->status = status; 477 w->status = status;
416 event ((W)w, EV_CHILD); 478 event ((W)w, EV_CHILD);
417 } 479 }
418} 480}
420/*****************************************************************************/ 482/*****************************************************************************/
421 483
422#if EV_USE_EPOLL 484#if EV_USE_EPOLL
423# include "ev_epoll.c" 485# include "ev_epoll.c"
424#endif 486#endif
487#if EV_USE_POLL
488# include "ev_poll.c"
489#endif
425#if EV_USE_SELECT 490#if EV_USE_SELECT
426# include "ev_select.c" 491# include "ev_select.c"
427#endif 492#endif
428 493
429int 494int
436ev_version_minor (void) 501ev_version_minor (void)
437{ 502{
438 return EV_VERSION_MINOR; 503 return EV_VERSION_MINOR;
439} 504}
440 505
506/* return true if we are running with elevated privileges and ignore env variables */
507static int
508enable_secure ()
509{
510 return getuid () != geteuid ()
511 || getgid () != getegid ();
512}
513
441int ev_init (int flags) 514int ev_init (int methods)
442{ 515{
443 if (!ev_method) 516 if (!ev_method)
444 { 517 {
445#if EV_USE_MONOTONIC 518#if EV_USE_MONOTONIC
446 { 519 {
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 521 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1; 522 have_monotonic = 1;
450 } 523 }
451#endif 524#endif
452 525
453 ev_now = ev_time (); 526 ev_now = ev_time ();
454 now = get_clock (); 527 now = get_clock ();
528 now_floor = now;
455 diff = ev_now - now; 529 diff = ev_now - now;
456 530
457 if (pipe (sigpipe)) 531 if (pipe (sigpipe))
458 return 0; 532 return 0;
459 533
534 if (methods == EVMETHOD_AUTO)
535 if (!enable_secure () && getenv ("LIBEV_METHODS"))
536 methods = atoi (getenv ("LIBEV_METHODS"));
537 else
460 ev_method = EVMETHOD_NONE; 538 methods = EVMETHOD_ANY;
539
540 ev_method = 0;
461#if EV_USE_EPOLL 541#if EV_USE_EPOLL
462 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 542 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
543#endif
544#if EV_USE_POLL
545 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
463#endif 546#endif
464#if EV_USE_SELECT 547#if EV_USE_SELECT
465 if (ev_method == EVMETHOD_NONE) select_init (flags); 548 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
466#endif 549#endif
467 550
468 if (ev_method) 551 if (ev_method)
469 { 552 {
470 ev_watcher_init (&sigev, sigcb); 553 ev_watcher_init (&sigev, sigcb);
479} 562}
480 563
481/*****************************************************************************/ 564/*****************************************************************************/
482 565
483void 566void
484ev_prefork (void) 567ev_fork_prepare (void)
485{ 568{
486 /* nop */ 569 /* nop */
487} 570}
488 571
489void 572void
490ev_postfork_parent (void) 573ev_fork_parent (void)
491{ 574{
492 /* nop */ 575 /* nop */
493} 576}
494 577
495void 578void
496ev_postfork_child (void) 579ev_fork_child (void)
497{ 580{
498#if EV_USE_EPOLL 581#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 582 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 583 epoll_postfork_child ();
501#endif 584#endif
532 struct ev_timer *w = timers [0]; 615 struct ev_timer *w = timers [0];
533 616
534 /* first reschedule or stop timer */ 617 /* first reschedule or stop timer */
535 if (w->repeat) 618 if (w->repeat)
536 { 619 {
620 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
537 w->at = now + w->repeat; 621 w->at = now + w->repeat;
538 assert (("timer timeout in the past, negative repeat?", w->at > now));
539 downheap ((WT *)timers, timercnt, 0); 622 downheap ((WT *)timers, timercnt, 0);
540 } 623 }
541 else 624 else
542 ev_timer_stop (w); /* nonrepeating: stop timer */ 625 ev_timer_stop (w); /* nonrepeating: stop timer */
543 626
554 637
555 /* first reschedule or stop timer */ 638 /* first reschedule or stop timer */
556 if (w->interval) 639 if (w->interval)
557 { 640 {
558 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 641 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
559 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 642 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
560 downheap ((WT *)periodics, periodiccnt, 0); 643 downheap ((WT *)periodics, periodiccnt, 0);
561 } 644 }
562 else 645 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 646 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 647
565 event ((W)w, EV_TIMEOUT); 648 event ((W)w, EV_PERIODIC);
566 } 649 }
567} 650}
568 651
569static void 652static void
570periodics_reschedule (ev_tstamp diff) 653periodics_reschedule (ev_tstamp diff)
589 } 672 }
590 } 673 }
591 } 674 }
592} 675}
593 676
677static int
678time_update_monotonic (void)
679{
680 now = get_clock ();
681
682 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
683 {
684 ev_now = now + diff;
685 return 0;
686 }
687 else
688 {
689 now_floor = now;
690 ev_now = ev_time ();
691 return 1;
692 }
693}
694
594static void 695static void
595time_update (void) 696time_update (void)
596{ 697{
597 int i; 698 int i;
598 699
599 ev_now = ev_time (); 700#if EV_USE_MONOTONIC
600
601 if (have_monotonic) 701 if (expect_true (have_monotonic))
602 { 702 {
703 if (time_update_monotonic ())
704 {
603 ev_tstamp odiff = diff; 705 ev_tstamp odiff = diff;
604 706
605 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 707 for (i = 4; --i; ) /* loop a few times, before making important decisions */
606 { 708 {
607 now = get_clock ();
608 diff = ev_now - now; 709 diff = ev_now - now;
609 710
610 if (fabs (odiff - diff) < MIN_TIMEJUMP) 711 if (fabs (odiff - diff) < MIN_TIMEJUMP)
611 return; /* all is well */ 712 return; /* all is well */
612 713
613 ev_now = ev_time (); 714 ev_now = ev_time ();
715 now = get_clock ();
716 now_floor = now;
614 } 717 }
615 718
616 periodics_reschedule (diff - odiff); 719 periodics_reschedule (diff - odiff);
617 /* no timer adjustment, as the monotonic clock doesn't jump */ 720 /* no timer adjustment, as the monotonic clock doesn't jump */
721 }
618 } 722 }
619 else 723 else
724#endif
620 { 725 {
726 ev_now = ev_time ();
727
621 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 728 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
622 { 729 {
623 periodics_reschedule (ev_now - now); 730 periodics_reschedule (ev_now - now);
624 731
625 /* adjust timers. this is easy, as the offset is the same for all */ 732 /* adjust timers. this is easy, as the offset is the same for all */
626 for (i = 0; i < timercnt; ++i) 733 for (i = 0; i < timercnt; ++i)
639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 746 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
640 747
641 do 748 do
642 { 749 {
643 /* queue check watchers (and execute them) */ 750 /* queue check watchers (and execute them) */
644 if (preparecnt) 751 if (expect_false (preparecnt))
645 { 752 {
646 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 753 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
647 call_pending (); 754 call_pending ();
648 } 755 }
649 756
652 759
653 /* calculate blocking time */ 760 /* calculate blocking time */
654 761
655 /* we only need this for !monotonic clockor timers, but as we basically 762 /* we only need this for !monotonic clockor timers, but as we basically
656 always have timers, we just calculate it always */ 763 always have timers, we just calculate it always */
764#if EV_USE_MONOTONIC
765 if (expect_true (have_monotonic))
766 time_update_monotonic ();
767 else
768#endif
769 {
657 ev_now = ev_time (); 770 ev_now = ev_time ();
771 now = ev_now;
772 }
658 773
659 if (flags & EVLOOP_NONBLOCK || idlecnt) 774 if (flags & EVLOOP_NONBLOCK || idlecnt)
660 block = 0.; 775 block = 0.;
661 else 776 else
662 { 777 {
663 block = MAX_BLOCKTIME; 778 block = MAX_BLOCKTIME;
664 779
665 if (timercnt) 780 if (timercnt)
666 { 781 {
667 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 782 ev_tstamp to = timers [0]->at - now + method_fudge;
668 if (block > to) block = to; 783 if (block > to) block = to;
669 } 784 }
670 785
671 if (periodiccnt) 786 if (periodiccnt)
672 { 787 {
725 head = &(*head)->next; 840 head = &(*head)->next;
726 } 841 }
727} 842}
728 843
729static void 844static void
730ev_clear (W w) 845ev_clear_pending (W w)
731{ 846{
732 if (w->pending) 847 if (w->pending)
733 { 848 {
734 pendings [w->pending - 1].w = 0; 849 pendings [w->pending - 1].w = 0;
735 w->pending = 0; 850 w->pending = 0;
751/*****************************************************************************/ 866/*****************************************************************************/
752 867
753void 868void
754ev_io_start (struct ev_io *w) 869ev_io_start (struct ev_io *w)
755{ 870{
871 int fd = w->fd;
872
756 if (ev_is_active (w)) 873 if (ev_is_active (w))
757 return; 874 return;
758 875
759 int fd = w->fd; 876 assert (("ev_io_start called with negative fd", fd >= 0));
760 877
761 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 879 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 880 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 881
766} 883}
767 884
768void 885void
769ev_io_stop (struct ev_io *w) 886ev_io_stop (struct ev_io *w)
770{ 887{
771 ev_clear ((W)w); 888 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 889 if (!ev_is_active (w))
773 return; 890 return;
774 891
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 892 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 893 ev_stop ((W)w);
784 if (ev_is_active (w)) 901 if (ev_is_active (w))
785 return; 902 return;
786 903
787 w->at += now; 904 w->at += now;
788 905
789 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 906 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
790 907
791 ev_start ((W)w, ++timercnt); 908 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 909 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 910 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 911 upheap ((WT *)timers, timercnt - 1);
795} 912}
796 913
797void 914void
798ev_timer_stop (struct ev_timer *w) 915ev_timer_stop (struct ev_timer *w)
799{ 916{
800 ev_clear ((W)w); 917 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 918 if (!ev_is_active (w))
802 return; 919 return;
803 920
804 if (w->active < timercnt--) 921 if (w->active < timercnt--)
805 { 922 {
833ev_periodic_start (struct ev_periodic *w) 950ev_periodic_start (struct ev_periodic *w)
834{ 951{
835 if (ev_is_active (w)) 952 if (ev_is_active (w))
836 return; 953 return;
837 954
838 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 955 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
839 956
840 /* this formula differs from the one in periodic_reify because we do not always round up */ 957 /* this formula differs from the one in periodic_reify because we do not always round up */
841 if (w->interval) 958 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 959 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 960
848} 965}
849 966
850void 967void
851ev_periodic_stop (struct ev_periodic *w) 968ev_periodic_stop (struct ev_periodic *w)
852{ 969{
853 ev_clear ((W)w); 970 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 971 if (!ev_is_active (w))
855 return; 972 return;
856 973
857 if (w->active < periodiccnt--) 974 if (w->active < periodiccnt--)
858 { 975 {
866void 983void
867ev_signal_start (struct ev_signal *w) 984ev_signal_start (struct ev_signal *w)
868{ 985{
869 if (ev_is_active (w)) 986 if (ev_is_active (w))
870 return; 987 return;
988
989 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 990
872 ev_start ((W)w, 1); 991 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 992 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 993 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 994
884} 1003}
885 1004
886void 1005void
887ev_signal_stop (struct ev_signal *w) 1006ev_signal_stop (struct ev_signal *w)
888{ 1007{
889 ev_clear ((W)w); 1008 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 1009 if (!ev_is_active (w))
891 return; 1010 return;
892 1011
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1012 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 1013 ev_stop ((W)w);
909} 1028}
910 1029
911void 1030void
912ev_idle_stop (struct ev_idle *w) 1031ev_idle_stop (struct ev_idle *w)
913{ 1032{
914 ev_clear ((W)w); 1033 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 1034 if (ev_is_active (w))
916 return; 1035 return;
917 1036
918 idles [w->active - 1] = idles [--idlecnt]; 1037 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 1038 ev_stop ((W)w);
931} 1050}
932 1051
933void 1052void
934ev_prepare_stop (struct ev_prepare *w) 1053ev_prepare_stop (struct ev_prepare *w)
935{ 1054{
936 ev_clear ((W)w); 1055 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 1056 if (ev_is_active (w))
938 return; 1057 return;
939 1058
940 prepares [w->active - 1] = prepares [--preparecnt]; 1059 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 1060 ev_stop ((W)w);
953} 1072}
954 1073
955void 1074void
956ev_check_stop (struct ev_check *w) 1075ev_check_stop (struct ev_check *w)
957{ 1076{
958 ev_clear ((W)w); 1077 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 1078 if (ev_is_active (w))
960 return; 1079 return;
961 1080
962 checks [w->active - 1] = checks [--checkcnt]; 1081 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 1082 ev_stop ((W)w);
974} 1093}
975 1094
976void 1095void
977ev_child_stop (struct ev_child *w) 1096ev_child_stop (struct ev_child *w)
978{ 1097{
979 ev_clear ((W)w); 1098 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 1099 if (ev_is_active (w))
981 return; 1100 return;
982 1101
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1102 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 1103 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines